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 "utils/net.h"
29 #include "auth/gensec/gensec.h"
30 #include "librpc/gen_ndr/ndr_netlogon.h"
31 #include "libads/ldap_schema_oids.h"
32 #include "../libds/common/flags.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "libcli/security/dom_sid.h"
36 struct idmap_ad_schema_names
;
38 struct idmap_ad_context
{
39 struct idmap_domain
*dom
;
40 struct tldap_context
*ld
;
41 struct idmap_ad_schema_names
*schema
;
42 const char *default_nc
;
44 bool unix_primary_group
;
48 static NTSTATUS
idmap_ad_get_context(struct idmap_domain
*dom
,
49 struct idmap_ad_context
**pctx
);
51 static char *get_schema_path(TALLOC_CTX
*mem_ctx
, struct tldap_context
*ld
)
53 struct tldap_message
*rootdse
;
55 rootdse
= tldap_rootdse(ld
);
56 if (rootdse
== NULL
) {
60 return tldap_talloc_single_attribute(rootdse
, "schemaNamingContext",
64 static char *get_default_nc(TALLOC_CTX
*mem_ctx
, struct tldap_context
*ld
)
66 struct tldap_message
*rootdse
;
68 rootdse
= tldap_rootdse(ld
);
69 if (rootdse
== NULL
) {
73 return tldap_talloc_single_attribute(rootdse
, "defaultNamingContext",
77 struct idmap_ad_schema_names
{
86 static TLDAPRC
get_attrnames_by_oids(struct tldap_context
*ld
,
88 const char *schema_path
,
94 const char *attrs
[] = { "lDAPDisplayName", "attributeId" };
97 struct tldap_message
**msgs
;
100 filter
= talloc_strdup(mem_ctx
, "(|");
101 if (filter
== NULL
) {
102 return TLDAP_NO_MEMORY
;
105 for (i
=0; i
<num_oids
; i
++) {
106 filter
= talloc_asprintf_append_buffer(
107 filter
, "(attributeId=%s)", oids
[i
]);
108 if (filter
== NULL
) {
109 return TLDAP_NO_MEMORY
;
113 filter
= talloc_asprintf_append_buffer(filter
, ")");
114 if (filter
== NULL
) {
115 return TLDAP_NO_MEMORY
;
118 rc
= tldap_search(ld
, schema_path
, TLDAP_SCOPE_SUB
, filter
,
119 attrs
, ARRAY_SIZE(attrs
), 0, NULL
, 0, NULL
, 0,
120 0, 0, 0, mem_ctx
, &msgs
);;
122 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
126 for (i
=0; i
<num_oids
; i
++) {
130 num_msgs
= talloc_array_length(msgs
);
132 for (i
=0; i
<num_msgs
; i
++) {
133 struct tldap_message
*msg
= msgs
[i
];
137 if (tldap_msg_type(msg
) != TLDAP_RES_SEARCH_ENTRY
) {
138 /* Could be a TLDAP_RES_SEARCH_REFERENCE */
142 oid
= tldap_talloc_single_attribute(
143 msg
, "attributeId", msg
);
148 for (j
=0; j
<num_oids
; j
++) {
149 if (strequal(oid
, oids
[j
])) {
160 names
[j
] = tldap_talloc_single_attribute(
161 msg
, "lDAPDisplayName", mem_ctx
);
166 return TLDAP_SUCCESS
;
169 static TLDAPRC
get_posix_schema_names(struct tldap_context
*ld
,
170 const char *schema_mode
,
172 struct idmap_ad_schema_names
**pschema
)
175 struct idmap_ad_schema_names
*schema
;
177 const char *oids_sfu
[] = {
178 ADS_ATTR_SFU_UIDNUMBER_OID
,
179 ADS_ATTR_SFU_GIDNUMBER_OID
,
180 ADS_ATTR_SFU_HOMEDIR_OID
,
181 ADS_ATTR_SFU_SHELL_OID
,
182 ADS_ATTR_SFU_GECOS_OID
,
185 const char *oids_sfu20
[] = {
186 ADS_ATTR_SFU20_UIDNUMBER_OID
,
187 ADS_ATTR_SFU20_GIDNUMBER_OID
,
188 ADS_ATTR_SFU20_HOMEDIR_OID
,
189 ADS_ATTR_SFU20_SHELL_OID
,
190 ADS_ATTR_SFU20_GECOS_OID
,
191 ADS_ATTR_SFU20_UID_OID
193 const char *oids_rfc2307
[] = {
194 ADS_ATTR_RFC2307_UIDNUMBER_OID
,
195 ADS_ATTR_RFC2307_GIDNUMBER_OID
,
196 ADS_ATTR_RFC2307_HOMEDIR_OID
,
197 ADS_ATTR_RFC2307_SHELL_OID
,
198 ADS_ATTR_RFC2307_GECOS_OID
,
199 ADS_ATTR_RFC2307_UID_OID
205 schema
= talloc(mem_ctx
, struct idmap_ad_schema_names
);
206 if (schema
== NULL
) {
207 return TLDAP_NO_MEMORY
;
210 schema_path
= get_schema_path(schema
, ld
);
211 if (schema_path
== NULL
) {
213 return TLDAP_NO_MEMORY
;
218 if ((schema_mode
!= NULL
) && (schema_mode
[0] != '\0')) {
219 if (strequal(schema_mode
, "sfu")) {
221 } else if (strequal(schema_mode
, "sfu20")) {
223 } else if (strequal(schema_mode
, "rfc2307" )) {
226 DBG_WARNING("Unknown schema mode %s\n", schema_mode
);
230 rc
= get_attrnames_by_oids(ld
, schema
, schema_path
, 6, oids
, names
);
231 TALLOC_FREE(schema_path
);
232 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
237 schema
->uid
= names
[0];
238 schema
->gid
= names
[1];
239 schema
->dir
= names
[2];
240 schema
->shell
= names
[3];
241 schema
->gecos
= names
[4];
242 schema
->name
= names
[5];
246 return TLDAP_SUCCESS
;
249 static NTSTATUS
idmap_ad_get_tldap_ctx(TALLOC_CTX
*mem_ctx
,
251 struct tldap_context
**pld
)
253 struct netr_DsRGetDCNameInfo
*dcinfo
;
254 struct sockaddr_storage dcaddr
;
255 struct cli_credentials
*creds
;
256 struct loadparm_context
*lp_ctx
;
257 struct tldap_context
*ld
;
263 status
= wb_dsgetdcname_gencache_get(mem_ctx
, domname
, &dcinfo
);
264 if (!NT_STATUS_IS_OK(status
)) {
265 DBG_DEBUG("Could not get dcinfo for %s: %s\n", domname
,
270 if (dcinfo
->dc_unc
== NULL
) {
272 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
274 if (dcinfo
->dc_unc
[0] == '\\') {
277 if (dcinfo
->dc_unc
[0] == '\\') {
281 ok
= resolve_name(dcinfo
->dc_unc
, &dcaddr
, 0x20, true);
283 DBG_DEBUG("Could not resolve name %s\n", dcinfo
->dc_unc
);
285 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
288 status
= open_socket_out(&dcaddr
, 389, 10000, &fd
);
289 if (!NT_STATUS_IS_OK(status
)) {
290 DBG_DEBUG("open_socket_out failed: %s\n", nt_errstr(status
));
295 ld
= tldap_context_create(dcinfo
, fd
);
297 DBG_DEBUG("tldap_context_create failed\n");
300 return NT_STATUS_NO_MEMORY
;
304 * Here we use or own machine account as
305 * we run as domain member.
307 status
= pdb_get_trust_credentials(lp_workgroup(),
311 if (!NT_STATUS_IS_OK(status
)) {
312 DBG_DEBUG("pdb_get_trust_credentials() failed - %s\n",
318 lp_ctx
= loadparm_init_s3(dcinfo
, loadparm_s3_helpers());
319 if (lp_ctx
== NULL
) {
320 DBG_DEBUG("loadparm_init_s3 failed\n");
322 return NT_STATUS_NO_MEMORY
;
325 rc
= tldap_gensec_bind(ld
, creds
, "ldap", dcinfo
->dc_unc
, NULL
, lp_ctx
,
326 GENSEC_FEATURE_SIGN
| GENSEC_FEATURE_SEAL
);
327 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
328 DBG_DEBUG("tldap_gensec_bind failed: %s\n",
329 tldap_errstr(dcinfo
, ld
, rc
));
331 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
334 rc
= tldap_fetch_rootdse(ld
);
335 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
336 DBG_DEBUG("tldap_fetch_rootdse failed: %s\n",
337 tldap_errstr(dcinfo
, ld
, rc
));
339 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
342 *pld
= talloc_move(mem_ctx
, &ld
);
347 static int idmap_ad_context_destructor(struct idmap_ad_context
*ctx
)
349 if ((ctx
->dom
!= NULL
) && (ctx
->dom
->private_data
== ctx
)) {
350 ctx
->dom
->private_data
= NULL
;
355 static NTSTATUS
idmap_ad_context_create(TALLOC_CTX
*mem_ctx
,
356 struct idmap_domain
*dom
,
358 struct idmap_ad_context
**pctx
)
360 struct idmap_ad_context
*ctx
;
361 const char *schema_mode
;
365 ctx
= talloc(mem_ctx
, struct idmap_ad_context
);
367 return NT_STATUS_NO_MEMORY
;
371 talloc_set_destructor(ctx
, idmap_ad_context_destructor
);
373 status
= idmap_ad_get_tldap_ctx(ctx
, domname
, &ctx
->ld
);
374 if (!NT_STATUS_IS_OK(status
)) {
375 DBG_DEBUG("idmap_ad_get_tldap_ctx failed: %s\n",
381 ctx
->default_nc
= get_default_nc(ctx
, ctx
->ld
);
382 if (ctx
->default_nc
== NULL
) {
383 DBG_DEBUG("No default nc\n");
388 ctx
->unix_primary_group
= idmap_config_bool(
389 domname
, "unix_primary_group", false);
390 ctx
->unix_nss_info
= idmap_config_bool(
391 domname
, "unix_nss_info", false);
393 schema_mode
= idmap_config_const_string(
394 domname
, "schema_mode", "rfc2307");
396 rc
= get_posix_schema_names(ctx
->ld
, schema_mode
, ctx
, &ctx
->schema
);
397 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
398 DBG_DEBUG("get_posix_schema_names failed: %s\n",
399 tldap_errstr(ctx
, ctx
->ld
, rc
));
401 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
408 static NTSTATUS
idmap_ad_query_user(struct idmap_domain
*domain
,
409 struct wbint_userinfo
*info
)
411 struct idmap_ad_context
*ctx
;
414 char *sidstr
, *filter
;
415 const char *attrs
[4];
417 struct tldap_message
**msgs
;
419 status
= idmap_ad_get_context(domain
, &ctx
);
420 if (!NT_STATUS_IS_OK(status
)) {
424 if (!(ctx
->unix_primary_group
|| ctx
->unix_nss_info
)) {
428 attrs
[0] = ctx
->schema
->gid
;
429 attrs
[1] = ctx
->schema
->gecos
;
430 attrs
[2] = ctx
->schema
->dir
;
431 attrs
[3] = ctx
->schema
->shell
;
433 sidstr
= ldap_encode_ndr_dom_sid(talloc_tos(), &info
->user_sid
);
434 if (sidstr
== NULL
) {
435 return NT_STATUS_NO_MEMORY
;
438 filter
= talloc_asprintf(talloc_tos(), "(objectsid=%s)", sidstr
);
440 if (filter
== NULL
) {
441 return NT_STATUS_NO_MEMORY
;
444 DBG_DEBUG("Filter: [%s]\n", filter
);
446 rc
= tldap_search(ctx
->ld
, ctx
->default_nc
, TLDAP_SCOPE_SUB
, filter
,
447 attrs
, ARRAY_SIZE(attrs
), 0, NULL
, 0, NULL
, 0,
448 0, 0, 0, talloc_tos(), &msgs
);
449 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
450 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
455 num_msgs
= talloc_array_length(msgs
);
457 for (i
=0; i
<num_msgs
; i
++) {
458 struct tldap_message
*msg
= msgs
[i
];
460 if (tldap_msg_type(msg
) != TLDAP_RES_SEARCH_ENTRY
) {
464 if (ctx
->unix_primary_group
) {
468 ok
= tldap_pull_uint32(msg
, ctx
->schema
->gid
, &gid
);
470 DBG_DEBUG("Setting primary group "
471 "to %"PRIu32
" from attr %s\n",
472 gid
, ctx
->schema
->gid
);
473 info
->primary_gid
= gid
;
477 if (ctx
->unix_nss_info
) {
480 attr
= tldap_talloc_single_attribute(
481 msg
, ctx
->schema
->dir
, talloc_tos());
483 info
->homedir
= talloc_move(info
, &attr
);
487 attr
= tldap_talloc_single_attribute(
488 msg
, ctx
->schema
->shell
, talloc_tos());
490 info
->shell
= talloc_move(info
, &attr
);
494 attr
= tldap_talloc_single_attribute(
495 msg
, ctx
->schema
->gecos
, talloc_tos());
497 info
->full_name
= talloc_move(info
, &attr
);
506 static NTSTATUS
idmap_ad_query_user_retry(struct idmap_domain
*domain
,
507 struct wbint_userinfo
*info
)
509 const NTSTATUS status_server_down
=
510 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN
));
513 status
= idmap_ad_query_user(domain
, info
);
515 if (NT_STATUS_EQUAL(status
, status_server_down
)) {
516 TALLOC_FREE(domain
->private_data
);
517 status
= idmap_ad_query_user(domain
, info
);
523 static NTSTATUS
idmap_ad_initialize(struct idmap_domain
*dom
)
525 dom
->query_user
= idmap_ad_query_user_retry
;
526 dom
->private_data
= NULL
;
530 static NTSTATUS
idmap_ad_get_context(struct idmap_domain
*dom
,
531 struct idmap_ad_context
**pctx
)
533 struct idmap_ad_context
*ctx
= NULL
;
538 * Make sure we never try to use LDAP against
539 * a trusted domain as AD_DC.
541 * This shouldn't be called currently,
542 * but you never know what happens in future.
544 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
547 if (dom
->private_data
!= NULL
) {
548 *pctx
= talloc_get_type_abort(dom
->private_data
,
549 struct idmap_ad_context
);
553 status
= idmap_ad_context_create(dom
, dom
, dom
->name
, &ctx
);
554 if (!NT_STATUS_IS_OK(status
)) {
555 DBG_DEBUG("idmap_ad_context_create failed: %s\n",
560 dom
->private_data
= ctx
;
565 static NTSTATUS
idmap_ad_unixids_to_sids(struct idmap_domain
*dom
,
568 struct idmap_ad_context
*ctx
;
571 struct tldap_message
**msgs
;
574 char *u_filter
, *g_filter
, *filter
;
576 const char *attrs
[] = {
579 NULL
, /* attr_uidnumber */
580 NULL
, /* attr_gidnumber */
583 status
= idmap_ad_get_context(dom
, &ctx
);
584 if (!NT_STATUS_IS_OK(status
)) {
588 attrs
[2] = ctx
->schema
->uid
;
589 attrs
[3] = ctx
->schema
->gid
;
591 u_filter
= talloc_strdup(talloc_tos(), "");
592 if (u_filter
== NULL
) {
593 return NT_STATUS_NO_MEMORY
;
596 g_filter
= talloc_strdup(talloc_tos(), "");
597 if (g_filter
== NULL
) {
598 return NT_STATUS_NO_MEMORY
;
601 for (i
=0; ids
[i
] != NULL
; i
++) {
602 struct id_map
*id
= ids
[i
];
604 id
->status
= ID_UNKNOWN
;
606 switch (id
->xid
.type
) {
608 u_filter
= talloc_asprintf_append_buffer(
609 u_filter
, "(%s=%ju)", ctx
->schema
->uid
,
610 (uintmax_t)id
->xid
.id
);
611 if (u_filter
== NULL
) {
612 return NT_STATUS_NO_MEMORY
;
618 g_filter
= talloc_asprintf_append_buffer(
619 g_filter
, "(%s=%ju)", ctx
->schema
->gid
,
620 (uintmax_t)id
->xid
.id
);
621 if (g_filter
== NULL
) {
622 return NT_STATUS_NO_MEMORY
;
628 DBG_WARNING("Unknown id type: %u\n",
629 (unsigned)id
->xid
.type
);
634 filter
= talloc_strdup(talloc_tos(), "(|");
635 if (filter
== NULL
) {
636 return NT_STATUS_NO_MEMORY
;
639 if (*u_filter
!= '\0') {
640 filter
= talloc_asprintf_append_buffer(
642 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)"
643 "(sAMAccountType=%d))(|%s))",
644 ATYPE_NORMAL_ACCOUNT
, ATYPE_WORKSTATION_TRUST
,
645 ATYPE_INTERDOMAIN_TRUST
, u_filter
);
646 if (filter
== NULL
) {
647 return NT_STATUS_NO_MEMORY
;
650 TALLOC_FREE(u_filter
);
652 if (*g_filter
!= '\0') {
653 filter
= talloc_asprintf_append_buffer(
655 "(&(|(sAMAccountType=%d)(sAMAccountType=%d))(|%s))",
656 ATYPE_SECURITY_GLOBAL_GROUP
,
657 ATYPE_SECURITY_LOCAL_GROUP
,
659 if (filter
== NULL
) {
660 return NT_STATUS_NO_MEMORY
;
663 TALLOC_FREE(g_filter
);
665 filter
= talloc_asprintf_append_buffer(filter
, ")");
666 if (filter
== NULL
) {
667 return NT_STATUS_NO_MEMORY
;
670 DBG_DEBUG("Filter: [%s]\n", filter
);
672 rc
= tldap_search(ctx
->ld
, ctx
->default_nc
, TLDAP_SCOPE_SUB
, filter
,
673 attrs
, ARRAY_SIZE(attrs
), 0, NULL
, 0, NULL
, 0,
674 0, 0, 0, talloc_tos(), &msgs
);
675 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
676 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
681 num_msgs
= talloc_array_length(msgs
);
683 for (i
=0; i
<num_msgs
; i
++) {
684 struct tldap_message
*msg
= msgs
[i
];
692 struct dom_sid_buf sidbuf
;
694 if (tldap_msg_type(msg
) != TLDAP_RES_SEARCH_ENTRY
) {
698 ok
= tldap_entry_dn(msg
, &dn
);
700 DBG_DEBUG("No dn found in msg %zu\n", i
);
704 ok
= tldap_pull_uint32(msg
, "sAMAccountType", &atype
);
706 DBG_DEBUG("No atype in object %s\n", dn
);
710 switch (atype
& 0xF0000000) {
711 case ATYPE_SECURITY_GLOBAL_GROUP
:
712 case ATYPE_SECURITY_LOCAL_GROUP
:
715 case ATYPE_NORMAL_ACCOUNT
:
716 case ATYPE_WORKSTATION_TRUST
:
717 case ATYPE_INTERDOMAIN_TRUST
:
721 DBG_WARNING("unrecognized SAM account type %08x\n",
726 ok
= tldap_pull_uint32(msg
, (type
== ID_TYPE_UID
) ?
727 ctx
->schema
->uid
: ctx
->schema
->gid
,
730 DBG_WARNING("No unix id in object %s\n", dn
);
734 ok
= tldap_pull_binsid(msg
, "objectSid", &sid
);
736 DBG_DEBUG("No objectSid in object %s\n", dn
);
741 for (j
=0; ids
[j
]; j
++) {
742 if ((type
== ids
[j
]->xid
.type
) &&
743 (xid
== ids
[j
]->xid
.id
)) {
749 DBG_DEBUG("Got unexpected sid %s from object %s\n",
750 dom_sid_str_buf(&sid
, &sidbuf
),
755 sid_copy(map
->sid
, &sid
);
756 map
->status
= ID_MAPPED
;
758 DBG_DEBUG("Mapped %s -> %ju (%d)\n",
759 dom_sid_str_buf(map
->sid
, &sidbuf
),
760 (uintmax_t)map
->xid
.id
, map
->xid
.type
);
768 static NTSTATUS
idmap_ad_sids_to_unixids(struct idmap_domain
*dom
,
771 struct idmap_ad_context
*ctx
;
774 struct tldap_message
**msgs
;
779 const char *attrs
[] = {
782 NULL
, /* attr_uidnumber */
783 NULL
, /* attr_gidnumber */
786 status
= idmap_ad_get_context(dom
, &ctx
);
787 if (!NT_STATUS_IS_OK(status
)) {
791 attrs
[2] = ctx
->schema
->uid
;
792 attrs
[3] = ctx
->schema
->gid
;
794 filter
= talloc_asprintf(
796 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)"
797 "(sAMAccountType=%d)(sAMAccountType=%d))(|",
798 ATYPE_NORMAL_ACCOUNT
, ATYPE_WORKSTATION_TRUST
,
799 ATYPE_INTERDOMAIN_TRUST
, ATYPE_SECURITY_GLOBAL_GROUP
,
800 ATYPE_SECURITY_LOCAL_GROUP
);
801 if (filter
== NULL
) {
802 return NT_STATUS_NO_MEMORY
;
805 for (i
=0; ids
[i
]; i
++) {
808 ids
[i
]->status
= ID_UNKNOWN
;
810 sidstr
= ldap_encode_ndr_dom_sid(talloc_tos(), ids
[i
]->sid
);
811 if (sidstr
== NULL
) {
812 return NT_STATUS_NO_MEMORY
;
815 filter
= talloc_asprintf_append_buffer(
816 filter
, "(objectSid=%s)", sidstr
);
818 if (filter
== NULL
) {
819 return NT_STATUS_NO_MEMORY
;
823 filter
= talloc_asprintf_append_buffer(filter
, "))");
824 if (filter
== NULL
) {
825 return NT_STATUS_NO_MEMORY
;
828 DBG_DEBUG("Filter: [%s]\n", filter
);
830 rc
= tldap_search(ctx
->ld
, ctx
->default_nc
, TLDAP_SCOPE_SUB
, filter
,
831 attrs
, ARRAY_SIZE(attrs
), 0, NULL
, 0, NULL
, 0,
832 0, 0, 0, talloc_tos(), &msgs
);
833 if (!TLDAP_RC_IS_SUCCESS(rc
)) {
834 return NT_STATUS_LDAP(TLDAP_RC_V(rc
));
839 num_msgs
= talloc_array_length(msgs
);
841 for (i
=0; i
<num_msgs
; i
++) {
842 struct tldap_message
*msg
= msgs
[i
];
848 uint64_t account_type
, xid
;
850 struct dom_sid_buf buf
;
852 if (tldap_msg_type(msg
) != TLDAP_RES_SEARCH_ENTRY
) {
856 ok
= tldap_entry_dn(msg
, &dn
);
858 DBG_DEBUG("No dn found in msg %zu\n", i
);
862 ok
= tldap_pull_binsid(msg
, "objectSid", &sid
);
864 DBG_DEBUG("No objectSid in object %s\n", dn
);
869 for (j
=0; ids
[j
]; j
++) {
870 if (dom_sid_equal(&sid
, ids
[j
]->sid
)) {
876 DBG_DEBUG("Got unexpected sid %s from object %s\n",
877 dom_sid_str_buf(&sid
, &buf
),
882 ok
= tldap_pull_uint64(msg
, "sAMAccountType", &account_type
);
884 DBG_DEBUG("No sAMAccountType in %s\n", dn
);
888 switch (account_type
& 0xF0000000) {
889 case ATYPE_SECURITY_GLOBAL_GROUP
:
890 case ATYPE_SECURITY_LOCAL_GROUP
:
893 case ATYPE_NORMAL_ACCOUNT
:
894 case ATYPE_WORKSTATION_TRUST
:
895 case ATYPE_INTERDOMAIN_TRUST
:
899 DBG_WARNING("unrecognized SAM account type %"PRIu64
"\n",
904 ok
= tldap_pull_uint64(msg
,
905 type
== ID_TYPE_UID
?
906 ctx
->schema
->uid
: ctx
->schema
->gid
,
909 DBG_DEBUG("No xid in %s\n", dn
);
914 map
->xid
.type
= type
;
916 map
->status
= ID_MAPPED
;
918 DEBUG(10, ("Mapped %s -> %lu (%d)\n",
919 dom_sid_str_buf(map
->sid
, &buf
),
920 (unsigned long)map
->xid
.id
, map
->xid
.type
));
928 static NTSTATUS
idmap_ad_unixids_to_sids_retry(struct idmap_domain
*dom
,
931 const NTSTATUS status_server_down
=
932 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN
));
935 status
= idmap_ad_unixids_to_sids(dom
, ids
);
937 if (NT_STATUS_EQUAL(status
, status_server_down
)) {
938 TALLOC_FREE(dom
->private_data
);
939 status
= idmap_ad_unixids_to_sids(dom
, ids
);
945 static NTSTATUS
idmap_ad_sids_to_unixids_retry(struct idmap_domain
*dom
,
948 const NTSTATUS status_server_down
=
949 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN
));
952 status
= idmap_ad_sids_to_unixids(dom
, ids
);
954 if (NT_STATUS_EQUAL(status
, status_server_down
)) {
955 TALLOC_FREE(dom
->private_data
);
956 status
= idmap_ad_sids_to_unixids(dom
, ids
);
962 static struct idmap_methods ad_methods
= {
963 .init
= idmap_ad_initialize
,
964 .unixids_to_sids
= idmap_ad_unixids_to_sids_retry
,
965 .sids_to_unixids
= idmap_ad_sids_to_unixids_retry
,
969 NTSTATUS
idmap_ad_init(TALLOC_CTX
*ctx
)
973 status
= smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
975 if (!NT_STATUS_IS_OK(status
)) {
979 status
= idmap_ad_nss_init(ctx
);
980 if (!NT_STATUS_IS_OK(status
)) {