2 Unix SMB/CIFS implementation.
4 Winbind ADS backend functions
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8 Copyright (C) Gerald (Jerry) Carter 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "winbindd_ads.h"
27 #include "libsmb/namequery.h"
28 #include "rpc_client/rpc_client.h"
29 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
30 #include "../libds/common/flags.h"
32 #include "../libcli/ldap/ldap_ndr.h"
33 #include "../libcli/security/security.h"
34 #include "../libds/common/flag_mapping.h"
35 #include "libsmb/samlogon_cache.h"
37 #include "auth/credentials/credentials.h"
42 #define DBGC_CLASS DBGC_WINBIND
44 extern struct winbindd_methods reconnect_methods
;
45 extern struct winbindd_methods msrpc_methods
;
48 * Check if cached connection can be reused. If the connection cannot
49 * be reused the ADS_STRUCT is freed and the pointer is set to NULL.
51 static void ads_cached_connection_reuse(ADS_STRUCT
**adsp
)
54 ADS_STRUCT
*ads
= *adsp
;
58 time_t now
= time(NULL
);
60 expire
= nt_time_to_unix(ads
->auth
.expire_time
);
62 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time "
63 "is now %d)\n", (uint32_t)expire
- (uint32_t)now
,
64 (uint32_t) expire
, (uint32_t) now
));
66 if ( ads
->config
.realm
&& (expire
> now
)) {
69 /* we own this ADS_STRUCT so make sure it goes away */
70 DEBUG(7,("Deleting expired ads struct\n"));
78 static NTSTATUS
ads_cached_connection_reconnect_creds(struct ads_struct
*ads
,
81 struct cli_credentials
**creds
)
83 struct winbindd_domain
*target_domain
= NULL
;
85 if (ads
->server
.realm
!= NULL
) {
86 target_domain
= find_domain_from_name_noinit(ads
->server
.realm
);
88 if (target_domain
== NULL
&& ads
->server
.workgroup
!= NULL
) {
89 target_domain
= find_domain_from_name_noinit(ads
->server
.workgroup
);
92 if (target_domain
== NULL
) {
93 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
96 return winbindd_get_trust_credentials(target_domain
,
99 false, /* ipc_fallback */
104 * @brief Establish a connection to a DC
106 * @param[out] adsp ADS_STRUCT that will be created
107 * @param[in] target_domain target domain
108 * @param[in] ldap_server DNS name of server to connect to
109 * @param[in] creds credentials to use
110 * @param[in] auth_realm Realm of local domain for creating krb token
114 static ADS_STATUS
ads_cached_connection_connect(struct winbindd_domain
*target_domain
,
115 const char *ldap_server
,
119 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
120 const char *target_realm
= target_domain
->alt_name
;
121 const char *target_dom_name
= target_domain
->name
;
122 struct cli_credentials
*creds
= NULL
;
126 struct sockaddr_storage dc_ss
;
129 /* the machine acct password might have change - fetch it every time */
131 ntstatus
= winbindd_get_trust_credentials(target_domain
,
133 false, /* netlogon */
134 false, /* ipc_fallback */
136 if (!NT_STATUS_IS_OK(ntstatus
)) {
137 status
= ADS_ERROR_NT(ntstatus
);
141 ads
= ads_init(tmp_ctx
,
147 DEBUG(1,("ads_init for domain %s failed\n", target_dom_name
));
148 status
= ADS_ERROR(LDAP_NO_MEMORY
);
152 ads_set_reconnect_fn(ads
, ads_cached_connection_reconnect_creds
, NULL
);
154 /* Setup the server affinity cache. We don't reaally care
155 about the name. Just setup affinity and the KRB5_CONFIG
157 get_dc_name(ads
->server
.workgroup
, ads
->server
.realm
, dc_name
, &dc_ss
);
159 status
= ads_connect_creds(ads
, creds
);
160 if (!ADS_ERR_OK(status
)) {
161 DEBUG(1,("ads_connect for domain %s failed: %s\n",
162 target_dom_name
, ads_errstr(status
)));
166 *adsp
= talloc_move(mem_ctx
, &ads
);
168 TALLOC_FREE(tmp_ctx
);
172 ADS_STATUS
ads_idmap_cached_connection(const char *dom_name
,
176 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
177 char *ldap_server
= NULL
;
178 struct winbindd_domain
*wb_dom
= NULL
;
183 * Make sure we never try to use LDAP against
184 * a trusted domain as AD DC.
186 TALLOC_FREE(tmp_ctx
);
187 return ADS_ERROR_NT(NT_STATUS_REQUEST_NOT_ACCEPTED
);
190 ads_cached_connection_reuse(adsp
);
192 TALLOC_FREE(tmp_ctx
);
197 * At this point we only have the NetBIOS domain name.
198 * Check if we can get server name and realm from SAF cache
199 * and the domain list.
201 ldap_server
= saf_fetch(tmp_ctx
, dom_name
);
203 DBG_DEBUG("ldap_server from saf cache: '%s'\n",
204 ldap_server
? ldap_server
: "");
206 wb_dom
= find_domain_from_name(dom_name
);
207 if (wb_dom
== NULL
) {
208 DBG_DEBUG("could not find domain '%s'\n", dom_name
);
209 TALLOC_FREE(tmp_ctx
);
210 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
213 DBG_DEBUG("find_domain_from_name found realm '%s' for "
214 " domain '%s'\n", wb_dom
->alt_name
, dom_name
);
216 status
= ads_cached_connection_connect(
218 ldap_server
, /* DNS name to connect to. */
219 mem_ctx
, /* memory context for ads struct */
220 adsp
); /* Returns ads struct. */
222 TALLOC_FREE(tmp_ctx
);
228 return our ads connections structure for a domain. We keep the connection
229 open to make things faster
231 static ADS_STATUS
ads_cached_connection(struct winbindd_domain
*domain
,
234 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
239 * Make sure we never try to use LDAP against
240 * a trusted domain as AD DC.
242 TALLOC_FREE(tmp_ctx
);
243 return ADS_ERROR_NT(NT_STATUS_REQUEST_NOT_ACCEPTED
);
246 DBG_DEBUG("ads_cached_connection\n");
248 ads_cached_connection_reuse(&domain
->backend_data
.ads_conn
);
249 if (domain
->backend_data
.ads_conn
!= NULL
) {
250 *adsp
= domain
->backend_data
.ads_conn
;
251 TALLOC_FREE(tmp_ctx
);
255 status
= ads_cached_connection_connect(
259 &domain
->backend_data
.ads_conn
);
260 if (!ADS_ERR_OK(status
)) {
261 /* if we get ECONNREFUSED then it might be a NT4
262 server, fall back to MSRPC */
263 if (status
.error_type
== ENUM_ADS_ERROR_SYSTEM
&&
264 status
.err
.rc
== ECONNREFUSED
) {
265 /* 'reconnect_methods' is the MS-RPC backend. */
266 DBG_NOTICE("Trying MSRPC methods for domain '%s'\n",
268 domain
->backend
= &reconnect_methods
;
270 TALLOC_FREE(tmp_ctx
);
274 *adsp
= domain
->backend_data
.ads_conn
;
275 TALLOC_FREE(tmp_ctx
);
279 /* Query display info for a realm. This is the basic user list fn */
280 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
284 ADS_STRUCT
*ads
= NULL
;
285 const char *attrs
[] = { "sAMAccountType", "objectSid", NULL
};
287 uint32_t *rids
= NULL
;
289 LDAPMessage
*res
= NULL
;
290 LDAPMessage
*msg
= NULL
;
291 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
293 DEBUG(3,("ads: query_user_list\n"));
295 if ( !winbindd_can_contact_domain( domain
) ) {
296 DEBUG(10,("query_user_list: No incoming trust for domain %s\n",
301 rc
= ads_cached_connection(domain
, &ads
);
302 if (!ADS_ERR_OK(rc
)) {
303 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
307 rc
= ads_search_retry(ads
, &res
, "(objectCategory=user)", attrs
);
308 if (!ADS_ERR_OK(rc
)) {
309 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc
)));
310 status
= ads_ntstatus(rc
);
313 DEBUG(1,("query_user_list ads_search returned NULL res\n"));
317 count
= ads_count_replies(ads
, res
);
319 DEBUG(1,("query_user_list: No users found\n"));
323 rids
= talloc_zero_array(mem_ctx
, uint32_t, count
);
325 status
= NT_STATUS_NO_MEMORY
;
331 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
332 struct dom_sid user_sid
;
336 ok
= ads_pull_uint32(ads
, msg
, "sAMAccountType", &atype
);
338 DBG_INFO("Object lacks sAMAccountType attribute\n");
341 if (ds_atype_map(atype
) != SID_NAME_USER
) {
342 DBG_INFO("Not a user account? atype=0x%x\n", atype
);
346 if (!ads_pull_sid(ads
, msg
, "objectSid", &user_sid
)) {
347 char *dn
= ads_get_dn(ads
, talloc_tos(), msg
);
348 DBG_INFO("No sid for %s !?\n", dn
);
353 if (!dom_sid_in_domain(&domain
->sid
, &user_sid
)) {
354 struct dom_sid_buf sidstr
, domstr
;
355 DBG_WARNING("Got sid %s in domain %s\n",
356 dom_sid_str_buf(&user_sid
, &sidstr
),
357 dom_sid_str_buf(&domain
->sid
, &domstr
));
361 sid_split_rid(&user_sid
, &rids
[count
]);
365 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, count
);
372 status
= NT_STATUS_OK
;
374 DBG_NOTICE("ads query_user_list gave %d entries\n", count
);
377 ads_msgfree(ads
, res
);
381 /* list all domain groups */
382 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
384 uint32_t *num_entries
,
385 struct wb_acct_info
**info
)
387 ADS_STRUCT
*ads
= NULL
;
388 const char *attrs
[] = {"userPrincipalName", "sAMAccountName",
389 "name", "objectSid", NULL
};
392 LDAPMessage
*res
= NULL
;
393 LDAPMessage
*msg
= NULL
;
394 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
396 bool enum_dom_local_groups
= False
;
400 DEBUG(3,("ads: enum_dom_groups\n"));
402 if ( !winbindd_can_contact_domain( domain
) ) {
403 DEBUG(10,("enum_dom_groups: No incoming trust for domain %s\n",
408 /* only grab domain local groups for our domain */
409 if ( domain
->active_directory
&& strequal(lp_realm(), domain
->alt_name
) ) {
410 enum_dom_local_groups
= True
;
413 /* Workaround ADS LDAP bug present in MS W2K3 SP0 and W2K SP4 w/o
416 * According to Section 5.1(4) of RFC 2251 if a value of a type is it's
417 * default value, it MUST be absent. In case of extensible matching the
418 * "dnattr" boolean defaults to FALSE and so it must be only be present
421 * When it is set to FALSE and the OpenLDAP lib (correctly) encodes a
422 * filter using bitwise matching rule then the buggy AD fails to decode
423 * the extensible match. As a workaround set it to TRUE and thereby add
424 * the dnAttributes "dn" field to cope with those older AD versions.
425 * It should not harm and won't put any additional load on the AD since
426 * none of the dn components have a bitmask-attribute.
428 * Thanks to Ralf Haferkamp for input and testing - Guenther */
430 filter
= talloc_asprintf(mem_ctx
, "(&(objectCategory=group)"
431 "(&(groupType:dn:"ADS_LDAP_MATCHING_RULE_BIT_AND
":=%d)"
432 "(!(groupType:dn:"ADS_LDAP_MATCHING_RULE_BIT_AND
":=%d))))",
433 GROUP_TYPE_SECURITY_ENABLED
,
434 enum_dom_local_groups
? GROUP_TYPE_BUILTIN_LOCAL_GROUP
: GROUP_TYPE_RESOURCE_GROUP
);
436 if (filter
== NULL
) {
437 status
= NT_STATUS_NO_MEMORY
;
441 rc
= ads_cached_connection(domain
, &ads
);
442 if (!ADS_ERR_OK(rc
)) {
443 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
447 rc
= ads_search_retry(ads
, &res
, filter
, attrs
);
448 if (!ADS_ERR_OK(rc
)) {
449 status
= ads_ntstatus(rc
);
450 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc
)));
453 DEBUG(1,("enum_dom_groups ads_search returned NULL res\n"));
457 count
= ads_count_replies(ads
, res
);
459 DEBUG(1,("enum_dom_groups: No groups found\n"));
463 (*info
) = talloc_zero_array(mem_ctx
, struct wb_acct_info
, count
);
465 status
= NT_STATUS_NO_MEMORY
;
471 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
476 name
= ads_pull_username(ads
, (*info
), msg
);
477 gecos
= ads_pull_string(ads
, (*info
), msg
, "name");
478 if (!ads_pull_sid(ads
, msg
, "objectSid", &sid
)) {
479 DEBUG(1,("No sid for %s !?\n", name
));
483 if (!sid_peek_check_rid(&domain
->sid
, &sid
, &rid
)) {
484 DEBUG(1,("No rid for %s !?\n", name
));
488 (*info
)[i
].acct_name
= name
;
489 (*info
)[i
].acct_desc
= gecos
;
490 (*info
)[i
].rid
= rid
;
496 status
= NT_STATUS_OK
;
498 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries
)));
502 ads_msgfree(ads
, res
);
507 /* list all domain local groups */
508 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
510 uint32_t *num_entries
,
511 struct wb_acct_info
**info
)
514 * This is a stub function only as we returned the domain
515 * local groups in enum_dom_groups() if the domain->native field
516 * was true. This is a simple performance optimization when
519 * if we ever need to enumerate domain local groups separately,
520 * then this optimization in enum_dom_groups() will need
528 /* convert a single name to a sid in a domain - use rpc methods */
529 static NTSTATUS
name_to_sid(struct winbindd_domain
*domain
,
531 const char *domain_name
,
534 const char **pdom_name
,
536 enum lsa_SidType
*type
)
538 return msrpc_methods
.name_to_sid(domain
, mem_ctx
, domain_name
, name
,
539 flags
, pdom_name
, sid
, type
);
542 /* convert a domain SID to a user or group name - use rpc methods */
543 static NTSTATUS
sid_to_name(struct winbindd_domain
*domain
,
545 const struct dom_sid
*sid
,
548 enum lsa_SidType
*type
)
550 return msrpc_methods
.sid_to_name(domain
, mem_ctx
, sid
,
551 domain_name
, name
, type
);
554 /* convert a list of rids to names - use rpc methods */
555 static NTSTATUS
rids_to_names(struct winbindd_domain
*domain
,
557 const struct dom_sid
*sid
,
562 enum lsa_SidType
**types
)
564 return msrpc_methods
.rids_to_names(domain
, mem_ctx
, sid
,
566 domain_name
, names
, types
);
569 /* Lookup groups a user is a member of - alternate method, for when
570 tokenGroups are not available. */
571 static NTSTATUS
lookup_usergroups_member(struct winbindd_domain
*domain
,
574 struct dom_sid
*primary_group
,
575 uint32_t *p_num_groups
, struct dom_sid
**user_sids
)
578 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
580 LDAPMessage
*res
= NULL
;
581 LDAPMessage
*msg
= NULL
;
583 ADS_STRUCT
*ads
= NULL
;
584 const char *group_attrs
[] = {"objectSid", NULL
};
586 uint32_t num_groups
= 0;
588 DEBUG(3,("ads: lookup_usergroups_member\n"));
590 if ( !winbindd_can_contact_domain( domain
) ) {
591 DEBUG(10,("lookup_usergroups_members: No incoming trust for domain %s\n",
596 rc
= ads_cached_connection(domain
, &ads
);
597 if (!ADS_ERR_OK(rc
)) {
598 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
602 if (!(escaped_dn
= escape_ldap_string(talloc_tos(), user_dn
))) {
603 status
= NT_STATUS_NO_MEMORY
;
607 ldap_exp
= talloc_asprintf(mem_ctx
,
608 "(&(member=%s)(objectCategory=group)"
609 "(groupType:dn:"ADS_LDAP_MATCHING_RULE_BIT_AND
":=%d))",
611 GROUP_TYPE_SECURITY_ENABLED
);
613 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn
));
614 TALLOC_FREE(escaped_dn
);
615 status
= NT_STATUS_NO_MEMORY
;
619 TALLOC_FREE(escaped_dn
);
621 rc
= ads_search_retry(ads
, &res
, ldap_exp
, group_attrs
);
623 if (!ADS_ERR_OK(rc
)) {
624 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn
, ads_errstr(rc
)));
625 return ads_ntstatus(rc
);
627 DEBUG(1,("lookup_usergroups ads_search returned NULL res\n"));
628 return NT_STATUS_INTERNAL_ERROR
;
632 count
= ads_count_replies(ads
, res
);
637 /* always add the primary group to the sid array */
638 status
= add_sid_to_array(mem_ctx
, primary_group
, user_sids
,
640 if (!NT_STATUS_IS_OK(status
)) {
645 for (msg
= ads_first_entry(ads
, res
); msg
;
646 msg
= ads_next_entry(ads
, msg
)) {
647 struct dom_sid group_sid
;
649 if (!ads_pull_sid(ads
, msg
, "objectSid", &group_sid
)) {
650 DEBUG(1,("No sid for this group ?!?\n"));
654 /* ignore Builtin groups from ADS - Guenther */
655 if (sid_check_is_in_builtin(&group_sid
)) {
659 status
= add_sid_to_array(mem_ctx
, &group_sid
,
660 user_sids
, &num_groups
);
661 if (!NT_STATUS_IS_OK(status
)) {
668 *p_num_groups
= num_groups
;
669 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
671 DEBUG(3,("ads lookup_usergroups (member) succeeded for dn=%s\n", user_dn
));
674 ads_msgfree(ads
, res
);
679 /* Lookup groups a user is a member of - alternate method, for when
680 tokenGroups are not available. */
681 static NTSTATUS
lookup_usergroups_memberof(struct winbindd_domain
*domain
,
684 struct dom_sid
*primary_group
,
685 uint32_t *p_num_groups
,
686 struct dom_sid
**user_sids
)
689 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
690 ADS_STRUCT
*ads
= NULL
;
691 const char *attrs
[] = {"memberOf", NULL
};
692 uint32_t num_groups
= 0;
693 struct dom_sid
*group_sids
= NULL
;
695 char **strings
= NULL
;
696 size_t num_strings
= 0, num_sids
= 0;
699 DEBUG(3,("ads: lookup_usergroups_memberof\n"));
701 if ( !winbindd_can_contact_domain( domain
) ) {
702 DEBUG(10,("lookup_usergroups_memberof: No incoming trust for "
703 "domain %s\n", domain
->name
));
707 rc
= ads_cached_connection(domain
, &ads
);
708 if (!ADS_ERR_OK(rc
)) {
709 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
710 return NT_STATUS_UNSUCCESSFUL
;
713 rc
= ads_search_retry_extended_dn_ranged(ads
, mem_ctx
, user_dn
, attrs
,
714 ADS_EXTENDED_DN_HEX_STRING
,
715 &strings
, &num_strings
);
717 if (!ADS_ERR_OK(rc
)) {
718 DEBUG(1,("lookup_usergroups_memberof ads_search "
719 "member=%s: %s\n", user_dn
, ads_errstr(rc
)));
720 return ads_ntstatus(rc
);
726 /* always add the primary group to the sid array */
727 status
= add_sid_to_array(mem_ctx
, primary_group
, user_sids
,
729 if (!NT_STATUS_IS_OK(status
)) {
733 group_sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_strings
+ 1);
735 status
= NT_STATUS_NO_MEMORY
;
739 for (i
=0; i
<num_strings
; i
++) {
740 rc
= ads_get_sid_from_extended_dn(mem_ctx
, strings
[i
],
741 ADS_EXTENDED_DN_HEX_STRING
,
743 if (!ADS_ERR_OK(rc
)) {
744 /* ignore members without SIDs */
745 if (NT_STATUS_EQUAL(ads_ntstatus(rc
),
746 NT_STATUS_NOT_FOUND
)) {
750 status
= ads_ntstatus(rc
);
758 DEBUG(1,("No memberOf for this user?!?\n"));
759 status
= NT_STATUS_NO_MEMORY
;
763 for (i
=0; i
<num_sids
; i
++) {
765 /* ignore Builtin groups from ADS - Guenther */
766 if (sid_check_is_in_builtin(&group_sids
[i
])) {
770 status
= add_sid_to_array(mem_ctx
, &group_sids
[i
], user_sids
,
772 if (!NT_STATUS_IS_OK(status
)) {
778 *p_num_groups
= num_groups
;
779 status
= (*user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
781 DEBUG(3,("ads lookup_usergroups (memberof) succeeded for dn=%s\n",
785 TALLOC_FREE(strings
);
786 TALLOC_FREE(group_sids
);
792 /* Lookup groups a user is a member of. */
793 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
795 const struct dom_sid
*sid
,
796 uint32_t *p_num_groups
, struct dom_sid
**user_sids
)
798 ADS_STRUCT
*ads
= NULL
;
799 const char *attrs
[] = {"tokenGroups", "primaryGroupID", NULL
};
802 LDAPMessage
*msg
= NULL
;
803 char *user_dn
= NULL
;
804 struct dom_sid
*sids
;
806 struct dom_sid primary_group
;
807 uint32_t primary_group_rid
;
808 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
809 uint32_t num_groups
= 0;
810 struct dom_sid_buf buf
;
812 DEBUG(3,("ads: lookup_usergroups\n"));
815 status
= lookup_usergroups_cached(mem_ctx
, sid
,
816 p_num_groups
, user_sids
);
817 if (NT_STATUS_IS_OK(status
)) {
821 if ( !winbindd_can_contact_domain( domain
) ) {
822 DEBUG(10,("lookup_usergroups: No incoming trust for domain %s\n",
825 /* Tell the cache manager not to remember this one */
827 return NT_STATUS_SYNCHRONIZATION_REQUIRED
;
830 rc
= ads_cached_connection(domain
, &ads
);
831 if (!ADS_ERR_OK(rc
)) {
832 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
833 status
= NT_STATUS_SERVER_DISABLED
;
837 rc
= ads_search_retry_sid(ads
, &msg
, sid
, attrs
);
839 if (!ADS_ERR_OK(rc
)) {
840 status
= ads_ntstatus(rc
);
841 DEBUG(1, ("lookup_usergroups(sid=%s) ads_search tokenGroups: "
843 dom_sid_str_buf(sid
, &buf
),
848 count
= ads_count_replies(ads
, msg
);
850 status
= NT_STATUS_UNSUCCESSFUL
;
851 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: "
852 "invalid number of results (count=%d)\n",
853 dom_sid_str_buf(sid
, &buf
),
859 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
860 dom_sid_str_buf(sid
, &buf
)));
861 status
= NT_STATUS_UNSUCCESSFUL
;
865 user_dn
= ads_get_dn(ads
, mem_ctx
, msg
);
866 if (user_dn
== NULL
) {
867 status
= NT_STATUS_NO_MEMORY
;
871 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &primary_group_rid
)) {
872 DEBUG(1,("%s: No primary group for sid=%s !?\n",
874 dom_sid_str_buf(sid
, &buf
)));
878 sid_compose(&primary_group
, &domain
->sid
, primary_group_rid
);
880 count
= ads_pull_sids(ads
, mem_ctx
, msg
, "tokenGroups", &sids
);
882 /* there must always be at least one group in the token,
883 unless we are talking to a buggy Win2k server */
885 /* actually this only happens when the machine account has no read
886 * permissions on the tokenGroup attribute - gd */
892 /* lookup what groups this user is a member of by DN search on
895 status
= lookup_usergroups_memberof(domain
, mem_ctx
, user_dn
,
897 &num_groups
, user_sids
);
898 *p_num_groups
= num_groups
;
899 if (NT_STATUS_IS_OK(status
)) {
903 /* lookup what groups this user is a member of by DN search on
906 status
= lookup_usergroups_member(domain
, mem_ctx
, user_dn
,
908 &num_groups
, user_sids
);
909 *p_num_groups
= num_groups
;
916 status
= add_sid_to_array(mem_ctx
, &primary_group
, user_sids
,
918 if (!NT_STATUS_IS_OK(status
)) {
922 for (i
=0;i
<count
;i
++) {
924 /* ignore Builtin groups from ADS - Guenther */
925 if (sid_check_is_in_builtin(&sids
[i
])) {
929 status
= add_sid_to_array_unique(mem_ctx
, &sids
[i
],
930 user_sids
, &num_groups
);
931 if (!NT_STATUS_IS_OK(status
)) {
936 *p_num_groups
= (uint32_t)num_groups
;
937 status
= (*user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
939 DEBUG(3,("ads lookup_usergroups (tokenGroups) succeeded for sid=%s\n",
940 dom_sid_str_buf(sid
, &buf
)));
942 TALLOC_FREE(user_dn
);
943 ads_msgfree(ads
, msg
);
947 /* Lookup aliases a user is member of - use rpc methods */
948 static NTSTATUS
lookup_useraliases(struct winbindd_domain
*domain
,
950 uint32_t num_sids
, const struct dom_sid
*sids
,
951 uint32_t *num_aliases
, uint32_t **alias_rids
)
953 return msrpc_methods
.lookup_useraliases(domain
, mem_ctx
, num_sids
, sids
,
954 num_aliases
, alias_rids
);
957 static NTSTATUS
add_primary_group_members(
958 ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
, uint32_t rid
, const char *domname
,
959 char ***all_members
, size_t *num_all_members
)
962 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
964 const char *attrs
[] = { "dn", NULL
};
965 LDAPMessage
*res
= NULL
;
970 bool all_groupmem
= idmap_config_bool(domname
, "all_groupmem", false);
972 filter
= talloc_asprintf(
974 "(&(objectCategory=user)(primaryGroupID=%u)%s)",
976 all_groupmem
? "" : "(uidNumber=*)(!(uidNumber=0))");
977 if (filter
== NULL
) {
981 args
.control
= ADS_EXTENDED_DN_OID
;
982 args
.val
= ADS_EXTENDED_DN_HEX_STRING
;
983 args
.critical
= True
;
985 rc
= ads_do_search_all_args(ads
, ads
->config
.bind_path
,
986 LDAP_SCOPE_SUBTREE
, filter
, attrs
, &args
,
989 if (!ADS_ERR_OK(rc
)) {
990 status
= ads_ntstatus(rc
);
991 DEBUG(1,("%s: ads_search: %s\n", __func__
, ads_errstr(rc
)));
995 DEBUG(1,("%s: ads_search returned NULL res\n", __func__
));
999 num_members
= ads_count_replies(ads
, res
);
1001 DEBUG(10, ("%s: Got %ju primary group members\n", __func__
,
1002 (uintmax_t)num_members
));
1004 if (num_members
== 0) {
1005 status
= NT_STATUS_OK
;
1009 members
= talloc_realloc(mem_ctx
, *all_members
, char *,
1010 *num_all_members
+ num_members
);
1011 if (members
== NULL
) {
1012 DEBUG(1, ("%s: talloc_realloc failed\n", __func__
));
1015 *all_members
= members
;
1017 for (msg
= ads_first_entry(ads
, res
); msg
!= NULL
;
1018 msg
= ads_next_entry(ads
, msg
)) {
1021 dn
= ads_get_dn(ads
, members
, msg
);
1023 DEBUG(1, ("%s: ads_get_dn failed\n", __func__
));
1027 members
[*num_all_members
] = dn
;
1028 *num_all_members
+= 1;
1031 status
= NT_STATUS_OK
;
1034 ads_msgfree(ads
, res
);
1036 TALLOC_FREE(filter
);
1041 find the members of a group, given a group rid and domain
1043 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
1044 TALLOC_CTX
*mem_ctx
,
1045 const struct dom_sid
*group_sid
,
1046 enum lsa_SidType type
,
1047 uint32_t *num_names
,
1048 struct dom_sid
**sid_mem
, char ***names
,
1049 uint32_t **name_types
)
1052 ADS_STRUCT
*ads
= NULL
;
1054 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1056 char **members
= NULL
;
1058 size_t num_members
= 0;
1060 struct dom_sid
*sid_mem_nocache
= NULL
;
1061 char **names_nocache
= NULL
;
1062 enum lsa_SidType
*name_types_nocache
= NULL
;
1063 char **domains_nocache
= NULL
; /* only needed for rpccli_lsa_lookup_sids */
1064 uint32_t num_nocache
= 0;
1065 TALLOC_CTX
*tmp_ctx
= NULL
;
1067 struct dom_sid_buf buf
;
1069 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain
->name
,
1070 dom_sid_str_buf(group_sid
, &buf
)));
1074 tmp_ctx
= talloc_new(mem_ctx
);
1076 DEBUG(1, ("ads: lookup_groupmem: talloc failed\n"));
1077 status
= NT_STATUS_NO_MEMORY
;
1081 if (!sid_peek_rid(group_sid
, &rid
)) {
1082 DEBUG(1, ("%s: sid_peek_rid failed\n", __func__
));
1083 status
= NT_STATUS_INVALID_PARAMETER
;
1087 if ( !winbindd_can_contact_domain( domain
) ) {
1088 DEBUG(10,("lookup_groupmem: No incoming trust for domain %s\n",
1090 return NT_STATUS_OK
;
1093 rc
= ads_cached_connection(domain
, &ads
);
1094 if (!ADS_ERR_OK(rc
)) {
1095 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
1099 if ((sidbinstr
= ldap_encode_ndr_dom_sid(talloc_tos(), group_sid
)) == NULL
) {
1100 status
= NT_STATUS_NO_MEMORY
;
1104 /* search for all members of the group */
1105 ldap_exp
= talloc_asprintf(tmp_ctx
, "(objectSid=%s)", sidbinstr
);
1106 TALLOC_FREE(sidbinstr
);
1107 if (ldap_exp
== NULL
) {
1108 DEBUG(1, ("ads: lookup_groupmem: talloc_asprintf for ldap_exp failed!\n"));
1109 status
= NT_STATUS_NO_MEMORY
;
1113 args
.control
= ADS_EXTENDED_DN_OID
;
1114 args
.val
= ADS_EXTENDED_DN_HEX_STRING
;
1115 args
.critical
= True
;
1117 rc
= ads_ranged_search(ads
, tmp_ctx
, LDAP_SCOPE_SUBTREE
, ads
->config
.bind_path
,
1118 ldap_exp
, &args
, "member", &members
, &num_members
);
1120 if (!ADS_ERR_OK(rc
)) {
1121 DEBUG(0,("ads_ranged_search failed with: %s\n", ads_errstr(rc
)));
1122 status
= NT_STATUS_UNSUCCESSFUL
;
1126 DEBUG(10, ("ads lookup_groupmem: got %d sids via extended dn call\n", (int)num_members
));
1128 status
= add_primary_group_members(ads
, mem_ctx
, rid
, domain
->name
,
1129 &members
, &num_members
);
1130 if (!NT_STATUS_IS_OK(status
)) {
1131 DEBUG(10, ("%s: add_primary_group_members failed: %s\n",
1132 __func__
, nt_errstr(status
)));
1136 DEBUG(10, ("%s: Got %d sids after adding primary group members\n",
1137 __func__
, (int)num_members
));
1139 /* Now that we have a list of sids, we need to get the
1140 * lists of names and name_types belonging to these sids.
1141 * even though conceptually not quite clean, we use the
1142 * RPC call lsa_lookup_sids for this since it can handle a
1143 * list of sids. ldap calls can just resolve one sid at a time.
1145 * At this stage, the sids are still hidden in the exetended dn
1146 * member output format. We actually do a little better than
1147 * stated above: In extracting the sids from the member strings,
1148 * we try to resolve as many sids as possible from the
1149 * cache. Only the rest is passed to the lsa_lookup_sids call. */
1152 (*sid_mem
) = talloc_zero_array(mem_ctx
, struct dom_sid
, num_members
);
1153 (*names
) = talloc_zero_array(mem_ctx
, char *, num_members
);
1154 (*name_types
) = talloc_zero_array(mem_ctx
, uint32_t, num_members
);
1155 (sid_mem_nocache
) = talloc_zero_array(tmp_ctx
, struct dom_sid
, num_members
);
1157 if ((members
== NULL
) || (*sid_mem
== NULL
) ||
1158 (*names
== NULL
) || (*name_types
== NULL
) ||
1159 (sid_mem_nocache
== NULL
))
1161 DEBUG(1, ("ads: lookup_groupmem: talloc failed\n"));
1162 status
= NT_STATUS_NO_MEMORY
;
1169 (*name_types
) = NULL
;
1172 for (i
=0; i
<num_members
; i
++) {
1173 enum lsa_SidType name_type
;
1174 char *name
, *domain_name
;
1177 rc
= ads_get_sid_from_extended_dn(tmp_ctx
, members
[i
], args
.val
,
1179 if (!ADS_ERR_OK(rc
)) {
1180 if (NT_STATUS_EQUAL(ads_ntstatus(rc
),
1181 NT_STATUS_NOT_FOUND
)) {
1182 /* Group members can be objects, like Exchange
1183 * Public Folders, that don't have a SID. Skip
1188 status
= ads_ntstatus(rc
);
1192 if (lookup_cached_sid(mem_ctx
, &sid
, &domain_name
, &name
,
1194 DEBUG(10,("ads: lookup_groupmem: got sid %s from "
1196 dom_sid_str_buf(&sid
, &buf
)));
1197 sid_copy(&(*sid_mem
)[*num_names
], &sid
);
1198 (*names
)[*num_names
] = fill_domain_username_talloc(
1204 (*name_types
)[*num_names
] = name_type
;
1208 DEBUG(10, ("ads: lookup_groupmem: sid %s not found in "
1210 dom_sid_str_buf(&sid
, &buf
)));
1211 sid_copy(&(sid_mem_nocache
)[num_nocache
], &sid
);
1216 DEBUG(10, ("ads: lookup_groupmem: %d sids found in cache, "
1217 "%d left for lsa_lookupsids\n", *num_names
, num_nocache
));
1219 /* handle sids not resolved from cache by lsa_lookup_sids */
1220 if (num_nocache
> 0) {
1222 status
= winbindd_lookup_sids(tmp_ctx
,
1228 &name_types_nocache
);
1230 if (!(NT_STATUS_IS_OK(status
) ||
1231 NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
) ||
1232 NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)))
1234 DEBUG(1, ("lsa_lookupsids call failed with %s "
1235 "- retrying...\n", nt_errstr(status
)));
1237 status
= winbindd_lookup_sids(tmp_ctx
,
1243 &name_types_nocache
);
1246 if (NT_STATUS_IS_OK(status
) ||
1247 NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
))
1249 /* Copy the entries over from the "_nocache" arrays
1250 * to the result arrays, skipping the gaps the
1251 * lookup_sids call left. */
1252 for (i
=0; i
< num_nocache
; i
++) {
1253 if (((names_nocache
)[i
] != NULL
) &&
1254 ((name_types_nocache
)[i
] != SID_NAME_UNKNOWN
))
1256 sid_copy(&(*sid_mem
)[*num_names
],
1257 &sid_mem_nocache
[i
]);
1258 (*names
)[*num_names
] =
1259 fill_domain_username_talloc(
1264 (*name_types
)[*num_names
] = name_types_nocache
[i
];
1269 else if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
1270 DEBUG(10, ("lookup_groupmem: lsa_lookup_sids could "
1271 "not map any SIDs at all.\n"));
1272 /* Don't handle this as an error here.
1273 * There is nothing left to do with respect to the
1274 * overall result... */
1276 else if (!NT_STATUS_IS_OK(status
)) {
1277 DEBUG(10, ("lookup_groupmem: Error looking up %d "
1278 "sids via rpc_lsa_lookup_sids: %s\n",
1279 (int)num_members
, nt_errstr(status
)));
1284 status
= NT_STATUS_OK
;
1285 DEBUG(3,("ads lookup_groupmem for sid=%s succeeded\n",
1286 dom_sid_str_buf(group_sid
, &buf
)));
1290 TALLOC_FREE(tmp_ctx
);
1295 static NTSTATUS
lookup_aliasmem(struct winbindd_domain
*domain
,
1296 TALLOC_CTX
*mem_ctx
,
1297 const struct dom_sid
*sid
,
1298 enum lsa_SidType type
,
1300 struct dom_sid
**sids
)
1302 char **names
= NULL
;
1303 uint32_t *name_types
= NULL
;
1304 struct dom_sid_buf buf
;
1306 DBG_DEBUG("ads: lookup_aliasmem %s sid=%s\n",
1308 dom_sid_str_buf(sid
, &buf
));
1309 /* Search for alias and group membership uses the same LDAP command. */
1310 return lookup_groupmem(domain
,
1320 /* find the lockout policy of a domain - use rpc methods */
1321 static NTSTATUS
lockout_policy(struct winbindd_domain
*domain
,
1322 TALLOC_CTX
*mem_ctx
,
1323 struct samr_DomInfo12
*policy
)
1325 return msrpc_methods
.lockout_policy(domain
, mem_ctx
, policy
);
1328 /* find the password policy of a domain - use rpc methods */
1329 static NTSTATUS
password_policy(struct winbindd_domain
*domain
,
1330 TALLOC_CTX
*mem_ctx
,
1331 struct samr_DomInfo1
*policy
)
1333 return msrpc_methods
.password_policy(domain
, mem_ctx
, policy
);
1336 /* get a list of trusted domains */
1337 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1338 TALLOC_CTX
*mem_ctx
,
1339 struct netr_DomainTrustList
*trusts
)
1341 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1345 struct rpc_pipe_client
*cli
;
1346 struct dcerpc_binding_handle
*b
;
1348 DEBUG(3,("ads: trusted_domains\n"));
1350 ZERO_STRUCTP(trusts
);
1352 /* If this is our primary domain or a root in our forest,
1353 query for all trusts. If not, then just look for domain
1354 trusts in the target forest */
1356 if (domain
->primary
|| domain_is_forest_root(domain
)) {
1357 flags
= NETR_TRUST_FLAG_OUTBOUND
|
1358 NETR_TRUST_FLAG_INBOUND
|
1359 NETR_TRUST_FLAG_IN_FOREST
;
1361 flags
= NETR_TRUST_FLAG_IN_FOREST
;
1364 result
= cm_connect_netlogon(domain
, &cli
);
1366 if (!NT_STATUS_IS_OK(result
)) {
1367 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
1368 "for PIPE_NETLOGON (%s)\n",
1369 domain
->name
, nt_errstr(result
)));
1370 return NT_STATUS_UNSUCCESSFUL
;
1373 b
= cli
->binding_handle
;
1375 result
= dcerpc_netr_DsrEnumerateDomainTrusts(b
, mem_ctx
,
1380 if (!NT_STATUS_IS_OK(result
)) {
1384 if (!W_ERROR_IS_OK(werr
)) {
1385 return werror_to_ntstatus(werr
);
1387 if (trusts
->count
== 0) {
1388 return NT_STATUS_OK
;
1391 /* Copy across names and sids */
1393 for (i
= 0; i
< trusts
->count
; i
++) {
1394 struct netr_DomainTrust
*trust
= &trusts
->array
[i
];
1395 struct winbindd_domain d
;
1400 * drop external trusts if this is not our primary
1401 * domain. This means that the returned number of
1402 * domains may be less that the ones actually trusted
1406 if ((trust
->trust_attributes
1407 & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
) &&
1410 DEBUG(10,("trusted_domains: Skipping external trusted "
1411 "domain %s because it is outside of our "
1413 trust
->netbios_name
));
1417 /* add to the trusted domain cache */
1419 d
.name
= discard_const_p(char, trust
->netbios_name
);
1420 d
.alt_name
= discard_const_p(char, trust
->dns_name
);
1423 sid_copy(&d
.sid
, trust
->sid
);
1425 sid_copy(&d
.sid
, &global_sid_NULL
);
1428 if ( domain
->primary
) {
1429 DEBUG(10,("trusted_domains(ads): Searching "
1430 "trusted domain list of %s and storing "
1431 "trust flags for domain %s\n",
1432 domain
->name
, d
.alt_name
));
1434 d
.domain_flags
= trust
->trust_flags
;
1435 d
.domain_type
= trust
->trust_type
;
1436 d
.domain_trust_attribs
= trust
->trust_attributes
;
1438 wcache_tdc_add_domain( &d
);
1439 } else if (domain_is_forest_root(domain
)) {
1440 /* Check if we already have this record. If
1441 * we are following our forest root that is not
1442 * our primary domain, we want to keep trust
1443 * flags from the perspective of our primary
1444 * domain not our forest root. */
1445 struct winbindd_tdc_domain
*exist
= NULL
;
1447 exist
= wcache_tdc_fetch_domain(
1448 talloc_tos(), trust
->netbios_name
);
1450 DEBUG(10,("trusted_domains(ads): Searching "
1451 "trusted domain list of %s and "
1452 "storing trust flags for domain "
1453 "%s\n", domain
->name
, d
.alt_name
));
1454 d
.domain_flags
= trust
->trust_flags
;
1455 d
.domain_type
= trust
->trust_type
;
1456 d
.domain_trust_attribs
=
1457 trust
->trust_attributes
;
1459 wcache_tdc_add_domain( &d
);
1463 /* This gets a little tricky. If we are
1464 following a transitive forest trust, then
1465 innerit the flags, type, and attribs from
1466 the domain we queried to make sure we don't
1467 record the view of the trust from the wrong
1468 side. Always view it from the side of our
1469 primary domain. --jerry */
1470 struct winbindd_tdc_domain
*parent
= NULL
;
1472 DEBUG(10,("trusted_domains(ads): Searching "
1473 "trusted domain list of %s and inheriting "
1474 "trust flags for domain %s\n",
1475 domain
->name
, d
.alt_name
));
1477 parent
= wcache_tdc_fetch_domain(talloc_tos(),
1480 d
.domain_flags
= parent
->trust_flags
;
1481 d
.domain_type
= parent
->trust_type
;
1482 d
.domain_trust_attribs
= parent
->trust_attribs
;
1484 d
.domain_flags
= domain
->domain_flags
;
1485 d
.domain_type
= domain
->domain_type
;
1486 d
.domain_trust_attribs
=
1487 domain
->domain_trust_attribs
;
1489 TALLOC_FREE(parent
);
1492 * We need to pass the modified properties
1495 trust
->trust_flags
= d
.domain_flags
;
1496 trust
->trust_type
= d
.domain_type
;
1497 trust
->trust_attributes
= d
.domain_trust_attribs
;
1499 wcache_tdc_add_domain( &d
);
1505 /* the ADS backend methods are exposed via this structure */
1506 struct winbindd_methods ads_methods
= {