2 * idmap_ad: map between Active Directory and RFC 2307 accounts
4 * Copyright (C) Volker Lendecke 2015
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "libsmb/namequery.h"
24 #include "tldap_gensec_bind.h"
25 #include "tldap_util.h"
27 #include "lib/param/param.h"
28 #include "auth/gensec/gensec.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "libads/ldap_schema_oids.h"
31 #include "../libds/common/flags.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "libcli/security/dom_sid.h"
34 #include "source3/libads/sitename_cache.h"
35 #include "source3/libads/kerberos_proto.h"
37 struct idmap_ad_schema_names;
39 struct idmap_ad_context {
40 struct idmap_domain *dom;
41 struct tldap_context *ld;
42 struct idmap_ad_schema_names *schema;
43 const char *default_nc;
45 bool unix_primary_group;
49 static NTSTATUS idmap_ad_get_context(struct idmap_domain *dom,
50 struct idmap_ad_context **pctx);
52 static char *get_schema_path(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
54 struct tldap_message *rootdse;
56 rootdse = tldap_rootdse(ld);
57 if (rootdse == NULL) {
61 return tldap_talloc_single_attribute(rootdse, "schemaNamingContext",
65 static char *get_default_nc(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
67 struct tldap_message *rootdse;
69 rootdse = tldap_rootdse(ld);
70 if (rootdse == NULL) {
74 return tldap_talloc_single_attribute(rootdse, "defaultNamingContext",
78 struct idmap_ad_schema_names {
87 static TLDAPRC get_attrnames_by_oids(struct tldap_context *ld,
89 const char *schema_path,
95 const char *attrs[] = { "lDAPDisplayName", "attributeId" };
98 struct tldap_message **msgs;
101 filter = talloc_strdup(mem_ctx, "(|");
103 for (i=0; i<num_oids; i++) {
104 talloc_asprintf_addbuf(&filter, "(attributeId=%s)", oids[i]);
106 talloc_asprintf_addbuf(&filter, ")");
108 if (filter == NULL) {
109 return TLDAP_NO_MEMORY;
112 rc = tldap_search(ld, schema_path, TLDAP_SCOPE_SUB, filter,
113 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
114 0, 0, 0, mem_ctx, &msgs);;
116 if (!TLDAP_RC_IS_SUCCESS(rc)) {
120 for (i=0; i<num_oids; i++) {
124 num_msgs = talloc_array_length(msgs);
126 for (i=0; i<num_msgs; i++) {
127 struct tldap_message *msg = msgs[i];
131 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
132 /* Could be a TLDAP_RES_SEARCH_REFERENCE */
136 oid = tldap_talloc_single_attribute(
137 msg, "attributeId", msg);
142 for (j=0; j<num_oids; j++) {
143 if (strequal(oid, oids[j])) {
154 names[j] = tldap_talloc_single_attribute(
155 msg, "lDAPDisplayName", mem_ctx);
159 for (i=0; i<num_oids; i++) {
160 if (names[i] == NULL) {
161 DBG_ERR("Failed to retrieve schema name for "
162 "oid [%s]. Schema mode is incorrect "
163 "for this domain.\n", oids[i]);
164 return TLDAP_FILTER_ERROR;
168 return TLDAP_SUCCESS;
171 static TLDAPRC get_posix_schema_names(struct tldap_context *ld,
172 const char *schema_mode,
174 struct idmap_ad_schema_names **pschema)
177 struct idmap_ad_schema_names *schema;
179 const char *oids_sfu[] = {
180 ADS_ATTR_SFU_UIDNUMBER_OID,
181 ADS_ATTR_SFU_GIDNUMBER_OID,
182 ADS_ATTR_SFU_HOMEDIR_OID,
183 ADS_ATTR_SFU_SHELL_OID,
184 ADS_ATTR_SFU_GECOS_OID,
187 const char *oids_sfu20[] = {
188 ADS_ATTR_SFU20_UIDNUMBER_OID,
189 ADS_ATTR_SFU20_GIDNUMBER_OID,
190 ADS_ATTR_SFU20_HOMEDIR_OID,
191 ADS_ATTR_SFU20_SHELL_OID,
192 ADS_ATTR_SFU20_GECOS_OID,
193 ADS_ATTR_SFU20_UID_OID
195 const char *oids_rfc2307[] = {
196 ADS_ATTR_RFC2307_UIDNUMBER_OID,
197 ADS_ATTR_RFC2307_GIDNUMBER_OID,
198 ADS_ATTR_RFC2307_HOMEDIR_OID,
199 ADS_ATTR_RFC2307_SHELL_OID,
200 ADS_ATTR_RFC2307_GECOS_OID,
201 ADS_ATTR_RFC2307_UID_OID
207 schema = talloc(mem_ctx, struct idmap_ad_schema_names);
208 if (schema == NULL) {
209 return TLDAP_NO_MEMORY;
212 schema_path = get_schema_path(schema, ld);
213 if (schema_path == NULL) {
215 return TLDAP_NO_MEMORY;
220 if ((schema_mode != NULL) && (schema_mode[0] != '\0')) {
221 if (strequal(schema_mode, "sfu")) {
223 } else if (strequal(schema_mode, "sfu20")) {
225 } else if (strequal(schema_mode, "rfc2307" )) {
228 DBG_WARNING("Unknown schema mode %s\n", schema_mode);
232 rc = get_attrnames_by_oids(ld, schema, schema_path, 6, oids, names);
233 TALLOC_FREE(schema_path);
234 if (!TLDAP_RC_IS_SUCCESS(rc)) {
239 schema->uid = names[0];
240 schema->gid = names[1];
241 schema->dir = names[2];
242 schema->shell = names[3];
243 schema->gecos = names[4];
244 schema->name = names[5];
248 return TLDAP_SUCCESS;
251 static void PRINTF_ATTRIBUTE(3, 0) idmap_ad_tldap_debug(
253 enum tldap_debug_level level,
257 int samba_level = -1;
260 case TLDAP_DEBUG_FATAL:
261 samba_level = DBGLVL_ERR;
263 case TLDAP_DEBUG_ERROR:
264 samba_level = DBGLVL_ERR;
266 case TLDAP_DEBUG_WARNING:
267 samba_level = DBGLVL_WARNING;
269 case TLDAP_DEBUG_TRACE:
270 samba_level = DBGLVL_DEBUG;
274 if (CHECK_DEBUGLVL(samba_level)) {
278 ret = vasprintf(&s, fmt, ap);
282 DEBUG(samba_level, ("idmap_ad_tldap: %s", s));
287 static uint32_t gensec_features_from_ldap_sasl_wrapping(void)
290 uint32_t gensec_features = 0;
292 wrap_flags = lp_client_ldap_sasl_wrapping();
293 if (wrap_flags == -1) {
297 if (wrap_flags & ADS_AUTH_SASL_SEAL) {
298 gensec_features |= GENSEC_FEATURE_SEAL;
300 if (wrap_flags & ADS_AUTH_SASL_SIGN) {
301 gensec_features |= GENSEC_FEATURE_SIGN;
304 if (gensec_features != 0) {
305 gensec_features |= GENSEC_FEATURE_LDAP_STYLE;
308 return gensec_features;
311 static NTSTATUS idmap_ad_get_tldap_ctx(TALLOC_CTX *mem_ctx,
313 struct tldap_context **pld)
315 struct netr_DsRGetDCNameInfo *dcinfo;
316 struct sockaddr_storage dcaddr;
317 struct cli_credentials *creds;
318 struct loadparm_context *lp_ctx;
319 struct tldap_context *ld;
320 uint32_t gensec_features = gensec_features_from_ldap_sasl_wrapping();
321 char *sitename = NULL;
327 status = wb_dsgetdcname_gencache_get(mem_ctx, domname, &dcinfo);
328 if (!NT_STATUS_IS_OK(status)) {
329 DBG_DEBUG("Could not get dcinfo for %s: %s\n", domname,
334 if (dcinfo->dc_unc == NULL) {
336 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
338 if (dcinfo->dc_unc[0] == '\\') {
341 if (dcinfo->dc_unc[0] == '\\') {
345 ok = resolve_name(dcinfo->dc_unc, &dcaddr, 0x20, true);
347 DBG_DEBUG("Could not resolve name %s\n", dcinfo->dc_unc);
349 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
352 sitename = sitename_fetch(talloc_tos(), lp_realm());
355 * create_local_private_krb5_conf_for_domain() can deal with
359 ok = create_local_private_krb5_conf_for_domain(
360 lp_realm(), lp_workgroup(), sitename, &dcaddr);
361 TALLOC_FREE(sitename);
363 DBG_DEBUG("Could not create private krb5.conf\n");
365 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
368 status = open_socket_out(&dcaddr, 389, 10000, &fd);
369 if (!NT_STATUS_IS_OK(status)) {
370 DBG_DEBUG("open_socket_out failed: %s\n", nt_errstr(status));
375 ld = tldap_context_create(dcinfo, fd);
377 DBG_DEBUG("tldap_context_create failed\n");
380 return NT_STATUS_NO_MEMORY;
382 tldap_set_debug(ld, idmap_ad_tldap_debug, NULL);
385 * Here we use or own machine account as
386 * we run as domain member.
388 status = pdb_get_trust_credentials(lp_workgroup(),
392 if (!NT_STATUS_IS_OK(status)) {
393 DBG_DEBUG("pdb_get_trust_credentials() failed - %s\n",
399 lp_ctx = loadparm_init_s3(dcinfo, loadparm_s3_helpers());
400 if (lp_ctx == NULL) {
401 DBG_DEBUG("loadparm_init_s3 failed\n");
403 return NT_STATUS_NO_MEMORY;
406 rc = tldap_gensec_bind(ld, creds, "ldap", dcinfo->dc_unc, NULL, lp_ctx,
408 if (!TLDAP_RC_IS_SUCCESS(rc)) {
409 DBG_DEBUG("tldap_gensec_bind failed: %s\n",
410 tldap_errstr(dcinfo, ld, rc));
412 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
415 rc = tldap_fetch_rootdse(ld);
416 if (!TLDAP_RC_IS_SUCCESS(rc)) {
417 DBG_DEBUG("tldap_fetch_rootdse failed: %s\n",
418 tldap_errstr(dcinfo, ld, rc));
420 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
423 *pld = talloc_move(mem_ctx, &ld);
428 static int idmap_ad_context_destructor(struct idmap_ad_context *ctx)
430 if ((ctx->dom != NULL) && (ctx->dom->private_data == ctx)) {
431 ctx->dom->private_data = NULL;
436 static NTSTATUS idmap_ad_context_create(TALLOC_CTX *mem_ctx,
437 struct idmap_domain *dom,
439 struct idmap_ad_context **pctx)
441 struct idmap_ad_context *ctx;
442 const char *schema_mode;
446 ctx = talloc(mem_ctx, struct idmap_ad_context);
448 return NT_STATUS_NO_MEMORY;
452 talloc_set_destructor(ctx, idmap_ad_context_destructor);
454 status = idmap_ad_get_tldap_ctx(ctx, domname, &ctx->ld);
455 if (!NT_STATUS_IS_OK(status)) {
456 DBG_DEBUG("idmap_ad_get_tldap_ctx failed: %s\n",
462 ctx->default_nc = get_default_nc(ctx, ctx->ld);
463 if (ctx->default_nc == NULL) {
464 DBG_DEBUG("No default nc\n");
469 ctx->unix_primary_group = idmap_config_bool(
470 domname, "unix_primary_group", false);
471 ctx->unix_nss_info = idmap_config_bool(
472 domname, "unix_nss_info", false);
474 schema_mode = idmap_config_const_string(
475 domname, "schema_mode", "rfc2307");
477 rc = get_posix_schema_names(ctx->ld, schema_mode, ctx, &ctx->schema);
478 if (!TLDAP_RC_IS_SUCCESS(rc)) {
479 DBG_DEBUG("get_posix_schema_names failed: %s\n",
480 tldap_errstr(ctx, ctx->ld, rc));
482 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
489 static NTSTATUS idmap_ad_query_user(struct idmap_domain *domain,
490 struct wbint_userinfo *info)
492 struct idmap_ad_context *ctx;
495 char *sidstr, *filter;
496 const char *attrs[4];
498 struct tldap_message **msgs;
500 status = idmap_ad_get_context(domain, &ctx);
501 if (!NT_STATUS_IS_OK(status)) {
505 if (!(ctx->unix_primary_group || ctx->unix_nss_info)) {
509 attrs[0] = ctx->schema->gid;
510 attrs[1] = ctx->schema->gecos;
511 attrs[2] = ctx->schema->dir;
512 attrs[3] = ctx->schema->shell;
514 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), &info->user_sid);
515 if (sidstr == NULL) {
516 return NT_STATUS_NO_MEMORY;
519 filter = talloc_asprintf(talloc_tos(), "(objectsid=%s)", sidstr);
521 if (filter == NULL) {
522 return NT_STATUS_NO_MEMORY;
525 DBG_DEBUG("Filter: [%s]\n", filter);
527 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
528 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
529 0, 0, 0, talloc_tos(), &msgs);
530 if (!TLDAP_RC_IS_SUCCESS(rc)) {
531 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
536 num_msgs = talloc_array_length(msgs);
538 for (i=0; i<num_msgs; i++) {
539 struct tldap_message *msg = msgs[i];
541 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
545 if (ctx->unix_primary_group) {
549 ok = tldap_pull_uint32(msg, ctx->schema->gid, &gid);
551 DBG_DEBUG("Setting primary group "
552 "to %"PRIu32" from attr %s\n",
553 gid, ctx->schema->gid);
554 info->primary_gid = gid;
558 if (ctx->unix_nss_info) {
561 attr = tldap_talloc_single_attribute(
562 msg, ctx->schema->dir, talloc_tos());
564 info->homedir = talloc_move(info, &attr);
568 attr = tldap_talloc_single_attribute(
569 msg, ctx->schema->shell, talloc_tos());
571 info->shell = talloc_move(info, &attr);
575 attr = tldap_talloc_single_attribute(
576 msg, ctx->schema->gecos, talloc_tos());
578 info->full_name = talloc_move(info, &attr);
587 static NTSTATUS idmap_ad_query_user_retry(struct idmap_domain *domain,
588 struct wbint_userinfo *info)
590 const NTSTATUS status_server_down =
591 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
594 status = idmap_ad_query_user(domain, info);
596 if (NT_STATUS_EQUAL(status, status_server_down)) {
597 TALLOC_FREE(domain->private_data);
598 status = idmap_ad_query_user(domain, info);
604 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom)
606 dom->query_user = idmap_ad_query_user_retry;
607 dom->private_data = NULL;
611 static NTSTATUS idmap_ad_get_context(struct idmap_domain *dom,
612 struct idmap_ad_context **pctx)
614 struct idmap_ad_context *ctx = NULL;
619 * Make sure we never try to use LDAP against
620 * a trusted domain as AD_DC.
622 * This shouldn't be called currently,
623 * but you never know what happens in future.
625 return NT_STATUS_REQUEST_NOT_ACCEPTED;
628 if (dom->private_data != NULL) {
629 *pctx = talloc_get_type_abort(dom->private_data,
630 struct idmap_ad_context);
634 status = idmap_ad_context_create(dom, dom, dom->name, &ctx);
635 if (!NT_STATUS_IS_OK(status)) {
636 DBG_DEBUG("idmap_ad_context_create failed: %s\n",
641 dom->private_data = ctx;
646 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom,
649 struct idmap_ad_context *ctx;
652 struct tldap_message **msgs;
655 char *u_filter, *g_filter, *filter;
657 const char *attrs[] = {
660 NULL, /* attr_uidnumber */
661 NULL, /* attr_gidnumber */
664 status = idmap_ad_get_context(dom, &ctx);
665 if (!NT_STATUS_IS_OK(status)) {
669 attrs[2] = ctx->schema->uid;
670 attrs[3] = ctx->schema->gid;
672 u_filter = talloc_strdup(talloc_tos(), "");
673 if (u_filter == NULL) {
674 return NT_STATUS_NO_MEMORY;
677 g_filter = talloc_strdup(talloc_tos(), "");
678 if (g_filter == NULL) {
679 return NT_STATUS_NO_MEMORY;
682 for (i=0; ids[i] != NULL; i++) {
683 struct id_map *id = ids[i];
685 id->status = ID_UNKNOWN;
687 switch (id->xid.type) {
689 u_filter = talloc_asprintf_append_buffer(
690 u_filter, "(%s=%ju)", ctx->schema->uid,
691 (uintmax_t)id->xid.id);
692 if (u_filter == NULL) {
693 return NT_STATUS_NO_MEMORY;
699 g_filter = talloc_asprintf_append_buffer(
700 g_filter, "(%s=%ju)", ctx->schema->gid,
701 (uintmax_t)id->xid.id);
702 if (g_filter == NULL) {
703 return NT_STATUS_NO_MEMORY;
709 DBG_WARNING("Unknown id type: %u\n",
710 (unsigned)id->xid.type);
715 filter = talloc_strdup(talloc_tos(), "(|");
716 if (filter == NULL) {
717 return NT_STATUS_NO_MEMORY;
720 if (*u_filter != '\0') {
721 filter = talloc_asprintf_append_buffer(
723 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)"
724 "(sAMAccountType=%d))(|%s))",
725 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
726 ATYPE_INTERDOMAIN_TRUST, u_filter);
727 if (filter == NULL) {
728 return NT_STATUS_NO_MEMORY;
731 TALLOC_FREE(u_filter);
733 if (*g_filter != '\0') {
734 filter = talloc_asprintf_append_buffer(
736 "(&(|(sAMAccountType=%d)(sAMAccountType=%d))(|%s))",
737 ATYPE_SECURITY_GLOBAL_GROUP,
738 ATYPE_SECURITY_LOCAL_GROUP,
740 if (filter == NULL) {
741 return NT_STATUS_NO_MEMORY;
744 TALLOC_FREE(g_filter);
746 filter = talloc_asprintf_append_buffer(filter, ")");
747 if (filter == NULL) {
748 return NT_STATUS_NO_MEMORY;
751 DBG_DEBUG("Filter: [%s]\n", filter);
753 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
754 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
755 0, 0, 0, talloc_tos(), &msgs);
756 if (!TLDAP_RC_IS_SUCCESS(rc)) {
757 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
762 num_msgs = talloc_array_length(msgs);
764 for (i=0; i<num_msgs; i++) {
765 struct tldap_message *msg = msgs[i];
773 struct dom_sid_buf sidbuf;
775 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
779 ok = tldap_entry_dn(msg, &dn);
781 DBG_DEBUG("No dn found in msg %zu\n", i);
785 ok = tldap_pull_uint32(msg, "sAMAccountType", &atype);
787 DBG_DEBUG("No atype in object %s\n", dn);
791 switch (atype & 0xF0000000) {
792 case ATYPE_SECURITY_GLOBAL_GROUP:
793 case ATYPE_SECURITY_LOCAL_GROUP:
796 case ATYPE_NORMAL_ACCOUNT:
797 case ATYPE_WORKSTATION_TRUST:
798 case ATYPE_INTERDOMAIN_TRUST:
802 DBG_WARNING("unrecognized SAM account type %08x\n",
807 ok = tldap_pull_uint32(msg, (type == ID_TYPE_UID) ?
808 ctx->schema->uid : ctx->schema->gid,
811 DBG_WARNING("No unix id in object %s\n", dn);
815 ok = tldap_pull_binsid(msg, "objectSid", &sid);
817 DBG_DEBUG("No objectSid in object %s\n", dn);
822 for (j=0; ids[j]; j++) {
823 if ((type == ids[j]->xid.type) &&
824 (xid == ids[j]->xid.id)) {
830 DBG_DEBUG("Got unexpected sid %s from object %s\n",
831 dom_sid_str_buf(&sid, &sidbuf),
836 sid_copy(map->sid, &sid);
837 map->status = ID_MAPPED;
839 DBG_DEBUG("Mapped %s -> %ju (%d)\n",
840 dom_sid_str_buf(map->sid, &sidbuf),
841 (uintmax_t)map->xid.id, map->xid.type);
849 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom,
852 struct idmap_ad_context *ctx;
855 struct tldap_message **msgs;
860 const char *attrs[] = {
863 NULL, /* attr_uidnumber */
864 NULL, /* attr_gidnumber */
867 status = idmap_ad_get_context(dom, &ctx);
868 if (!NT_STATUS_IS_OK(status)) {
872 attrs[2] = ctx->schema->uid;
873 attrs[3] = ctx->schema->gid;
875 filter = talloc_asprintf(
877 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)"
878 "(sAMAccountType=%d)(sAMAccountType=%d))(|",
879 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
880 ATYPE_INTERDOMAIN_TRUST, ATYPE_SECURITY_GLOBAL_GROUP,
881 ATYPE_SECURITY_LOCAL_GROUP);
882 if (filter == NULL) {
883 return NT_STATUS_NO_MEMORY;
886 for (i=0; ids[i]; i++) {
889 ids[i]->status = ID_UNKNOWN;
891 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), ids[i]->sid);
892 if (sidstr == NULL) {
893 return NT_STATUS_NO_MEMORY;
896 filter = talloc_asprintf_append_buffer(
897 filter, "(objectSid=%s)", sidstr);
899 if (filter == NULL) {
900 return NT_STATUS_NO_MEMORY;
904 filter = talloc_asprintf_append_buffer(filter, "))");
905 if (filter == NULL) {
906 return NT_STATUS_NO_MEMORY;
909 DBG_DEBUG("Filter: [%s]\n", filter);
911 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
912 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
913 0, 0, 0, talloc_tos(), &msgs);
914 if (!TLDAP_RC_IS_SUCCESS(rc)) {
915 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
920 num_msgs = talloc_array_length(msgs);
922 for (i=0; i<num_msgs; i++) {
923 struct tldap_message *msg = msgs[i];
929 uint64_t account_type, xid;
931 struct dom_sid_buf buf;
933 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
937 ok = tldap_entry_dn(msg, &dn);
939 DBG_DEBUG("No dn found in msg %zu\n", i);
943 ok = tldap_pull_binsid(msg, "objectSid", &sid);
945 DBG_DEBUG("No objectSid in object %s\n", dn);
950 for (j=0; ids[j]; j++) {
951 if (dom_sid_equal(&sid, ids[j]->sid)) {
957 DBG_DEBUG("Got unexpected sid %s from object %s\n",
958 dom_sid_str_buf(&sid, &buf),
963 ok = tldap_pull_uint64(msg, "sAMAccountType", &account_type);
965 DBG_DEBUG("No sAMAccountType in %s\n", dn);
969 switch (account_type & 0xF0000000) {
970 case ATYPE_SECURITY_GLOBAL_GROUP:
971 case ATYPE_SECURITY_LOCAL_GROUP:
974 case ATYPE_NORMAL_ACCOUNT:
975 case ATYPE_WORKSTATION_TRUST:
976 case ATYPE_INTERDOMAIN_TRUST:
980 DBG_WARNING("unrecognized SAM account type %"PRIu64"\n",
985 ok = tldap_pull_uint64(msg,
986 type == ID_TYPE_UID ?
987 ctx->schema->uid : ctx->schema->gid,
990 DBG_DEBUG("No xid in %s\n", dn);
995 map->xid.type = type;
997 map->status = ID_MAPPED;
999 DEBUG(10, ("Mapped %s -> %lu (%d)\n",
1000 dom_sid_str_buf(map->sid, &buf),
1001 (unsigned long)map->xid.id, map->xid.type));
1006 return NT_STATUS_OK;
1009 static NTSTATUS idmap_ad_unixids_to_sids_retry(struct idmap_domain *dom,
1010 struct id_map **ids)
1012 const NTSTATUS status_server_down =
1013 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
1016 status = idmap_ad_unixids_to_sids(dom, ids);
1018 if (NT_STATUS_EQUAL(status, status_server_down)) {
1019 TALLOC_FREE(dom->private_data);
1020 status = idmap_ad_unixids_to_sids(dom, ids);
1026 static NTSTATUS idmap_ad_sids_to_unixids_retry(struct idmap_domain *dom,
1027 struct id_map **ids)
1029 const NTSTATUS status_server_down =
1030 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
1033 status = idmap_ad_sids_to_unixids(dom, ids);
1035 if (NT_STATUS_EQUAL(status, status_server_down)) {
1036 TALLOC_FREE(dom->private_data);
1037 status = idmap_ad_sids_to_unixids(dom, ids);
1043 static const struct idmap_methods ad_methods = {
1044 .init = idmap_ad_initialize,
1045 .unixids_to_sids = idmap_ad_unixids_to_sids_retry,
1046 .sids_to_unixids = idmap_ad_sids_to_unixids_retry,
1050 NTSTATUS idmap_ad_init(TALLOC_CTX *ctx)
1054 status = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1056 if (!NT_STATUS_IS_OK(status)) {
1060 status = idmap_ad_nss_init(ctx);
1061 if (!NT_STATUS_IS_OK(status)) {
1065 return NT_STATUS_OK;