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/>.
30 #define DBGC_CLASS DBGC_WINBIND
32 extern struct winbindd_methods reconnect_methods
;
35 return our ads connections structure for a domain. We keep the connection
36 open to make things faster
38 static ADS_STRUCT
*ads_cached_connection(struct winbindd_domain
*domain
)
43 struct sockaddr_storage dc_ss
;
45 DEBUG(10,("ads_cached_connection\n"));
47 if (domain
->private_data
) {
50 time_t now
= time(NULL
);
52 /* check for a valid structure */
53 ads
= (ADS_STRUCT
*)domain
->private_data
;
55 expire
= MIN(ads
->auth
.tgt_expire
, ads
->auth
.tgs_expire
);
57 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
58 (uint32
)expire
-(uint32
)now
, (uint32
) expire
, (uint32
) now
));
60 if ( ads
->config
.realm
&& (expire
> now
)) {
63 /* we own this ADS_STRUCT so make sure it goes away */
64 DEBUG(7,("Deleting expired krb5 credential cache\n"));
67 ads_kdestroy("MEMORY:winbind_ccache");
68 domain
->private_data
= NULL
;
72 /* we don't want this to affect the users ccache */
73 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
75 ads
= ads_init(domain
->alt_name
, domain
->name
, NULL
);
77 DEBUG(1,("ads_init for domain %s failed\n", domain
->name
));
81 /* the machine acct password might have change - fetch it every time */
83 SAFE_FREE(ads
->auth
.password
);
84 SAFE_FREE(ads
->auth
.realm
);
88 if ( !pdb_get_trusteddom_pw( domain
->name
, &ads
->auth
.password
, NULL
, NULL
) ) {
92 ads
->auth
.realm
= SMB_STRDUP( ads
->server
.realm
);
93 strupper_m( ads
->auth
.realm
);
96 struct winbindd_domain
*our_domain
= domain
;
98 ads
->auth
.password
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
100 /* always give preference to the alt_name in our
101 primary domain if possible */
103 if ( !domain
->primary
)
104 our_domain
= find_our_domain();
106 if ( our_domain
->alt_name
[0] != '\0' ) {
107 ads
->auth
.realm
= SMB_STRDUP( our_domain
->alt_name
);
108 strupper_m( ads
->auth
.realm
);
111 ads
->auth
.realm
= SMB_STRDUP( lp_realm() );
114 ads
->auth
.renewable
= WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
;
116 /* Setup the server affinity cache. We don't reaally care
117 about the name. Just setup affinity and the KRB5_CONFIG
120 get_dc_name( ads
->server
.workgroup
, ads
->server
.realm
, dc_name
, &dc_ss
);
122 status
= ads_connect(ads
);
123 if (!ADS_ERR_OK(status
) || !ads
->config
.realm
) {
124 DEBUG(1,("ads_connect for domain %s failed: %s\n",
125 domain
->name
, ads_errstr(status
)));
128 /* if we get ECONNREFUSED then it might be a NT4
129 server, fall back to MSRPC */
130 if (status
.error_type
== ENUM_ADS_ERROR_SYSTEM
&&
131 status
.err
.rc
== ECONNREFUSED
) {
132 /* 'reconnect_methods' is the MS-RPC backend. */
133 DEBUG(1,("Trying MSRPC methods\n"));
134 domain
->backend
= &reconnect_methods
;
139 /* set the flag that says we don't own the memory even
140 though we do so that ads_destroy() won't destroy the
141 structure we pass back by reference */
143 ads
->is_mine
= False
;
145 domain
->private_data
= (void *)ads
;
150 /* Query display info for a realm. This is the basic user list fn */
151 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
154 struct wbint_userinfo
**info
)
156 ADS_STRUCT
*ads
= NULL
;
157 const char *attrs
[] = { "*", NULL
};
160 LDAPMessage
*res
= NULL
;
161 LDAPMessage
*msg
= NULL
;
162 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
166 DEBUG(3,("ads: query_user_list\n"));
168 if ( !winbindd_can_contact_domain( domain
) ) {
169 DEBUG(10,("query_user_list: No incoming trust for domain %s\n",
174 ads
= ads_cached_connection(domain
);
177 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
181 rc
= ads_search_retry(ads
, &res
, "(objectCategory=user)", attrs
);
182 if (!ADS_ERR_OK(rc
) || !res
) {
183 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc
)));
187 count
= ads_count_replies(ads
, res
);
189 DEBUG(1,("query_user_list: No users found\n"));
193 (*info
) = TALLOC_ZERO_ARRAY(mem_ctx
, struct wbint_userinfo
, count
);
195 status
= NT_STATUS_NO_MEMORY
;
201 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
203 const char *gecos
= NULL
;
204 const char *homedir
= NULL
;
205 const char *shell
= NULL
;
209 gid_t primary_gid
= (gid_t
)-1;
211 if (!ads_pull_uint32(ads
, msg
, "sAMAccountType", &atype
) ||
212 ds_atype_map(atype
) != SID_NAME_USER
) {
213 DEBUG(1,("Not a user account? atype=0x%x\n", atype
));
217 name
= ads_pull_username(ads
, mem_ctx
, msg
);
219 if ( ads_pull_sid( ads
, msg
, "objectSid", &user_sid
) ) {
220 status
= nss_get_info_cached( domain
, &user_sid
, mem_ctx
,
221 ads
, msg
, &homedir
, &shell
, &gecos
,
226 gecos
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
229 if (!ads_pull_sid(ads
, msg
, "objectSid",
230 &(*info
)[i
].user_sid
)) {
231 DEBUG(1,("No sid for %s !?\n", name
));
234 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &group
)) {
235 DEBUG(1,("No primary group for %s !?\n", name
));
239 (*info
)[i
].acct_name
= name
;
240 (*info
)[i
].full_name
= gecos
;
241 (*info
)[i
].homedir
= homedir
;
242 (*info
)[i
].shell
= shell
;
243 (*info
)[i
].primary_gid
= primary_gid
;
244 sid_compose(&(*info
)[i
].group_sid
, &domain
->sid
, group
);
249 status
= NT_STATUS_OK
;
251 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries
)));
255 ads_msgfree(ads
, res
);
260 /* list all domain groups */
261 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
264 struct acct_info
**info
)
266 ADS_STRUCT
*ads
= NULL
;
267 const char *attrs
[] = {"userPrincipalName", "sAMAccountName",
268 "name", "objectSid", NULL
};
271 LDAPMessage
*res
= NULL
;
272 LDAPMessage
*msg
= NULL
;
273 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
275 bool enum_dom_local_groups
= False
;
279 DEBUG(3,("ads: enum_dom_groups\n"));
281 if ( !winbindd_can_contact_domain( domain
) ) {
282 DEBUG(10,("enum_dom_groups: No incoming trust for domain %s\n",
287 /* only grab domain local groups for our domain */
288 if ( domain
->active_directory
&& strequal(lp_realm(), domain
->alt_name
) ) {
289 enum_dom_local_groups
= True
;
292 /* Workaround ADS LDAP bug present in MS W2K3 SP0 and W2K SP4 w/o
295 * According to Section 5.1(4) of RFC 2251 if a value of a type is it's
296 * default value, it MUST be absent. In case of extensible matching the
297 * "dnattr" boolean defaults to FALSE and so it must be only be present
300 * When it is set to FALSE and the OpenLDAP lib (correctly) encodes a
301 * filter using bitwise matching rule then the buggy AD fails to decode
302 * the extensible match. As a workaround set it to TRUE and thereby add
303 * the dnAttributes "dn" field to cope with those older AD versions.
304 * It should not harm and won't put any additional load on the AD since
305 * none of the dn components have a bitmask-attribute.
307 * Thanks to Ralf Haferkamp for input and testing - Guenther */
309 filter
= talloc_asprintf(mem_ctx
, "(&(objectCategory=group)(&(groupType:dn:%s:=%d)(!(groupType:dn:%s:=%d))))",
310 ADS_LDAP_MATCHING_RULE_BIT_AND
, GROUP_TYPE_SECURITY_ENABLED
,
311 ADS_LDAP_MATCHING_RULE_BIT_AND
,
312 enum_dom_local_groups
? GROUP_TYPE_BUILTIN_LOCAL_GROUP
: GROUP_TYPE_RESOURCE_GROUP
);
314 if (filter
== NULL
) {
315 status
= NT_STATUS_NO_MEMORY
;
319 ads
= ads_cached_connection(domain
);
322 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
326 rc
= ads_search_retry(ads
, &res
, filter
, attrs
);
327 if (!ADS_ERR_OK(rc
) || !res
) {
328 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc
)));
332 count
= ads_count_replies(ads
, res
);
334 DEBUG(1,("enum_dom_groups: No groups found\n"));
338 (*info
) = TALLOC_ZERO_ARRAY(mem_ctx
, struct acct_info
, count
);
340 status
= NT_STATUS_NO_MEMORY
;
346 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
351 name
= ads_pull_username(ads
, mem_ctx
, msg
);
352 gecos
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
353 if (!ads_pull_sid(ads
, msg
, "objectSid", &sid
)) {
354 DEBUG(1,("No sid for %s !?\n", name
));
358 if (!sid_peek_check_rid(&domain
->sid
, &sid
, &rid
)) {
359 DEBUG(1,("No rid for %s !?\n", name
));
363 fstrcpy((*info
)[i
].acct_name
, name
);
364 fstrcpy((*info
)[i
].acct_desc
, gecos
);
365 (*info
)[i
].rid
= rid
;
371 status
= NT_STATUS_OK
;
373 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries
)));
377 ads_msgfree(ads
, res
);
382 /* list all domain local groups */
383 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
386 struct acct_info
**info
)
389 * This is a stub function only as we returned the domain
390 * local groups in enum_dom_groups() if the domain->native field
391 * was true. This is a simple performance optimization when
394 * if we ever need to enumerate domain local groups separately,
395 * then this optimization in enum_dom_groups() will need
403 /* convert a single name to a sid in a domain - use rpc methods */
404 static NTSTATUS
name_to_sid(struct winbindd_domain
*domain
,
406 const char *domain_name
,
410 enum lsa_SidType
*type
)
412 return reconnect_methods
.name_to_sid(domain
, mem_ctx
,
413 domain_name
, name
, flags
,
417 /* convert a domain SID to a user or group name - use rpc methods */
418 static NTSTATUS
sid_to_name(struct winbindd_domain
*domain
,
423 enum lsa_SidType
*type
)
425 return reconnect_methods
.sid_to_name(domain
, mem_ctx
, sid
,
426 domain_name
, name
, type
);
429 /* convert a list of rids to names - use rpc methods */
430 static NTSTATUS
rids_to_names(struct winbindd_domain
*domain
,
437 enum lsa_SidType
**types
)
439 return reconnect_methods
.rids_to_names(domain
, mem_ctx
, sid
,
441 domain_name
, names
, types
);
444 /* If you are looking for "dn_lookup": Yes, it used to be here!
445 * It has gone now since it was a major speed bottleneck in
446 * lookup_groupmem (its only use). It has been replaced by
447 * an rpc lookup sids call... R.I.P. */
449 /* Lookup user information from a rid */
450 static NTSTATUS
query_user(struct winbindd_domain
*domain
,
453 struct wbint_userinfo
*info
)
455 ADS_STRUCT
*ads
= NULL
;
456 const char *attrs
[] = { "*", NULL
};
459 LDAPMessage
*msg
= NULL
;
463 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
464 struct netr_SamInfo3
*user
= NULL
;
467 DEBUG(3,("ads: query_user\n"));
469 info
->homedir
= NULL
;
471 info
->primary_gid
= (gid_t
)-1;
473 /* try netsamlogon cache first */
475 if ( (user
= netsamlogon_cache_get( mem_ctx
, sid
)) != NULL
)
477 DEBUG(5,("query_user: Cache lookup succeeded for %s\n",
478 sid_string_dbg(sid
)));
480 sid_compose(&info
->user_sid
, &domain
->sid
, user
->base
.rid
);
481 sid_compose(&info
->group_sid
, &domain
->sid
, user
->base
.primary_gid
);
483 info
->acct_name
= talloc_strdup(mem_ctx
, user
->base
.account_name
.string
);
484 info
->full_name
= talloc_strdup(mem_ctx
, user
->base
.full_name
.string
);
486 nss_get_info_cached( domain
, sid
, mem_ctx
, NULL
, NULL
,
487 &info
->homedir
, &info
->shell
, &info
->full_name
,
489 info
->primary_gid
= gid
;
496 if ( !winbindd_can_contact_domain(domain
)) {
497 DEBUG(8,("query_user: No incoming trust from domain %s\n",
500 /* We still need to generate some basic information
501 about the user even if we cannot contact the
502 domain. Most of this stuff we can deduce. */
504 sid_copy( &info
->user_sid
, sid
);
506 /* Assume "Domain Users" for the primary group */
508 sid_compose(&info
->group_sid
, &domain
->sid
, DOMAIN_GROUP_RID_USERS
);
510 /* Try to fill in what the nss_info backend can do */
512 nss_get_info_cached( domain
, sid
, mem_ctx
, NULL
, NULL
,
513 &info
->homedir
, &info
->shell
, &info
->full_name
,
515 info
->primary_gid
= gid
;
517 status
= NT_STATUS_OK
;
521 /* no cache...do the query */
523 if ( (ads
= ads_cached_connection(domain
)) == NULL
) {
524 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
528 sidstr
= sid_binstring(talloc_tos(), sid
);
529 if (asprintf(&ldap_exp
, "(objectSid=%s)", sidstr
) == -1) {
530 status
= NT_STATUS_NO_MEMORY
;
533 rc
= ads_search_retry(ads
, &msg
, ldap_exp
, attrs
);
536 if (!ADS_ERR_OK(rc
) || !msg
) {
537 DEBUG(1,("query_user(sid=%s) ads_search: %s\n",
538 sid_string_dbg(sid
), ads_errstr(rc
)));
542 count
= ads_count_replies(ads
, msg
);
544 DEBUG(1,("query_user(sid=%s): Not found\n",
545 sid_string_dbg(sid
)));
549 info
->acct_name
= ads_pull_username(ads
, mem_ctx
, msg
);
551 nss_get_info_cached( domain
, sid
, mem_ctx
, ads
, msg
,
552 &info
->homedir
, &info
->shell
, &info
->full_name
,
554 info
->primary_gid
= gid
;
556 if (info
->full_name
== NULL
) {
557 info
->full_name
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
560 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &group_rid
)) {
561 DEBUG(1,("No primary group for %s !?\n",
562 sid_string_dbg(sid
)));
566 sid_copy(&info
->user_sid
, sid
);
567 sid_compose(&info
->group_sid
, &domain
->sid
, group_rid
);
569 status
= NT_STATUS_OK
;
571 DEBUG(3,("ads query_user gave %s\n", info
->acct_name
));
574 ads_msgfree(ads
, msg
);
579 /* Lookup groups a user is a member of - alternate method, for when
580 tokenGroups are not available. */
581 static NTSTATUS
lookup_usergroups_member(struct winbindd_domain
*domain
,
584 DOM_SID
*primary_group
,
585 size_t *p_num_groups
, DOM_SID
**user_sids
)
588 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
590 LDAPMessage
*res
= NULL
;
591 LDAPMessage
*msg
= NULL
;
594 const char *group_attrs
[] = {"objectSid", NULL
};
596 size_t num_groups
= 0;
598 DEBUG(3,("ads: lookup_usergroups_member\n"));
600 if ( !winbindd_can_contact_domain( domain
) ) {
601 DEBUG(10,("lookup_usergroups_members: No incoming trust for domain %s\n",
606 ads
= ads_cached_connection(domain
);
609 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
613 if (!(escaped_dn
= escape_ldap_string(talloc_tos(), user_dn
))) {
614 status
= NT_STATUS_NO_MEMORY
;
618 ldap_exp
= talloc_asprintf(mem_ctx
,
619 "(&(member=%s)(objectCategory=group)(groupType:dn:%s:=%d))",
621 ADS_LDAP_MATCHING_RULE_BIT_AND
,
622 GROUP_TYPE_SECURITY_ENABLED
);
624 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn
));
625 TALLOC_FREE(escaped_dn
);
626 status
= NT_STATUS_NO_MEMORY
;
630 TALLOC_FREE(escaped_dn
);
632 rc
= ads_search_retry(ads
, &res
, ldap_exp
, group_attrs
);
634 if (!ADS_ERR_OK(rc
) || !res
) {
635 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn
, ads_errstr(rc
)));
636 return ads_ntstatus(rc
);
639 count
= ads_count_replies(ads
, res
);
644 /* always add the primary group to the sid array */
645 status
= add_sid_to_array(mem_ctx
, primary_group
, user_sids
,
647 if (!NT_STATUS_IS_OK(status
)) {
652 for (msg
= ads_first_entry(ads
, res
); msg
;
653 msg
= ads_next_entry(ads
, msg
)) {
656 if (!ads_pull_sid(ads
, msg
, "objectSid", &group_sid
)) {
657 DEBUG(1,("No sid for this group ?!?\n"));
661 /* ignore Builtin groups from ADS - Guenther */
662 if (sid_check_is_in_builtin(&group_sid
)) {
666 status
= add_sid_to_array(mem_ctx
, &group_sid
,
667 user_sids
, &num_groups
);
668 if (!NT_STATUS_IS_OK(status
)) {
675 *p_num_groups
= num_groups
;
676 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
678 DEBUG(3,("ads lookup_usergroups (member) succeeded for dn=%s\n", user_dn
));
681 ads_msgfree(ads
, res
);
686 /* Lookup groups a user is a member of - alternate method, for when
687 tokenGroups are not available. */
688 static NTSTATUS
lookup_usergroups_memberof(struct winbindd_domain
*domain
,
691 DOM_SID
*primary_group
,
692 size_t *p_num_groups
,
696 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
698 const char *attrs
[] = {"memberOf", NULL
};
699 size_t num_groups
= 0;
700 DOM_SID
*group_sids
= NULL
;
702 char **strings
= NULL
;
703 size_t num_strings
= 0, num_sids
= 0;
706 DEBUG(3,("ads: lookup_usergroups_memberof\n"));
708 if ( !winbindd_can_contact_domain( domain
) ) {
709 DEBUG(10,("lookup_usergroups_memberof: No incoming trust for "
710 "domain %s\n", domain
->name
));
714 ads
= ads_cached_connection(domain
);
717 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
718 return NT_STATUS_UNSUCCESSFUL
;
721 rc
= ads_search_retry_extended_dn_ranged(ads
, mem_ctx
, user_dn
, attrs
,
722 ADS_EXTENDED_DN_HEX_STRING
,
723 &strings
, &num_strings
);
725 if (!ADS_ERR_OK(rc
)) {
726 DEBUG(1,("lookup_usergroups_memberof ads_search "
727 "member=%s: %s\n", user_dn
, ads_errstr(rc
)));
728 return ads_ntstatus(rc
);
734 /* always add the primary group to the sid array */
735 status
= add_sid_to_array(mem_ctx
, primary_group
, user_sids
,
737 if (!NT_STATUS_IS_OK(status
)) {
741 group_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_strings
+ 1);
743 status
= NT_STATUS_NO_MEMORY
;
747 for (i
=0; i
<num_strings
; i
++) {
748 rc
= ads_get_sid_from_extended_dn(mem_ctx
, strings
[i
],
749 ADS_EXTENDED_DN_HEX_STRING
,
751 if (!ADS_ERR_OK(rc
)) {
752 /* ignore members without SIDs */
753 if (NT_STATUS_EQUAL(ads_ntstatus(rc
),
754 NT_STATUS_NOT_FOUND
)) {
758 status
= ads_ntstatus(rc
);
766 DEBUG(1,("No memberOf for this user?!?\n"));
767 status
= NT_STATUS_NO_MEMORY
;
771 for (i
=0; i
<num_sids
; i
++) {
773 /* ignore Builtin groups from ADS - Guenther */
774 if (sid_check_is_in_builtin(&group_sids
[i
])) {
778 status
= add_sid_to_array(mem_ctx
, &group_sids
[i
], user_sids
,
780 if (!NT_STATUS_IS_OK(status
)) {
786 *p_num_groups
= num_groups
;
787 status
= (*user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
789 DEBUG(3,("ads lookup_usergroups (memberof) succeeded for dn=%s\n",
793 TALLOC_FREE(strings
);
794 TALLOC_FREE(group_sids
);
800 /* Lookup groups a user is a member of. */
801 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
804 uint32
*p_num_groups
, DOM_SID
**user_sids
)
806 ADS_STRUCT
*ads
= NULL
;
807 const char *attrs
[] = {"tokenGroups", "primaryGroupID", NULL
};
810 LDAPMessage
*msg
= NULL
;
811 char *user_dn
= NULL
;
814 DOM_SID primary_group
;
815 uint32 primary_group_rid
;
816 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
817 size_t num_groups
= 0;
819 DEBUG(3,("ads: lookup_usergroups\n"));
822 status
= lookup_usergroups_cached(domain
, mem_ctx
, sid
,
823 p_num_groups
, user_sids
);
824 if (NT_STATUS_IS_OK(status
)) {
828 if ( !winbindd_can_contact_domain( domain
) ) {
829 DEBUG(10,("lookup_usergroups: No incoming trust for domain %s\n",
832 /* Tell the cache manager not to remember this one */
834 return NT_STATUS_SYNCHRONIZATION_REQUIRED
;
837 ads
= ads_cached_connection(domain
);
840 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
841 status
= NT_STATUS_SERVER_DISABLED
;
845 rc
= ads_search_retry_sid(ads
, &msg
, sid
, attrs
);
847 if (!ADS_ERR_OK(rc
)) {
848 status
= ads_ntstatus(rc
);
849 DEBUG(1, ("lookup_usergroups(sid=%s) ads_search tokenGroups: "
850 "%s\n", sid_string_dbg(sid
), ads_errstr(rc
)));
854 count
= ads_count_replies(ads
, msg
);
856 status
= NT_STATUS_UNSUCCESSFUL
;
857 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: "
858 "invalid number of results (count=%d)\n",
859 sid_string_dbg(sid
), count
));
864 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
865 sid_string_dbg(sid
)));
866 status
= NT_STATUS_UNSUCCESSFUL
;
870 user_dn
= ads_get_dn(ads
, mem_ctx
, msg
);
871 if (user_dn
== NULL
) {
872 status
= NT_STATUS_NO_MEMORY
;
876 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &primary_group_rid
)) {
877 DEBUG(1,("%s: No primary group for sid=%s !?\n",
878 domain
->name
, sid_string_dbg(sid
)));
882 sid_copy(&primary_group
, &domain
->sid
);
883 sid_append_rid(&primary_group
, primary_group_rid
);
885 count
= ads_pull_sids(ads
, mem_ctx
, msg
, "tokenGroups", &sids
);
887 /* there must always be at least one group in the token,
888 unless we are talking to a buggy Win2k server */
890 /* actually this only happens when the machine account has no read
891 * permissions on the tokenGroup attribute - gd */
897 /* lookup what groups this user is a member of by DN search on
900 status
= lookup_usergroups_memberof(domain
, mem_ctx
, user_dn
,
902 &num_groups
, user_sids
);
903 *p_num_groups
= (uint32
)num_groups
;
904 if (NT_STATUS_IS_OK(status
)) {
908 /* lookup what groups this user is a member of by DN search on
911 status
= lookup_usergroups_member(domain
, mem_ctx
, user_dn
,
913 &num_groups
, user_sids
);
914 *p_num_groups
= (uint32
)num_groups
;
921 status
= add_sid_to_array(mem_ctx
, &primary_group
, user_sids
,
923 if (!NT_STATUS_IS_OK(status
)) {
927 for (i
=0;i
<count
;i
++) {
929 /* ignore Builtin groups from ADS - Guenther */
930 if (sid_check_is_in_builtin(&sids
[i
])) {
934 status
= add_sid_to_array_unique(mem_ctx
, &sids
[i
],
935 user_sids
, &num_groups
);
936 if (!NT_STATUS_IS_OK(status
)) {
941 *p_num_groups
= (uint32
)num_groups
;
942 status
= (*user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
944 DEBUG(3,("ads lookup_usergroups (tokenGroups) succeeded for sid=%s\n",
945 sid_string_dbg(sid
)));
947 TALLOC_FREE(user_dn
);
948 ads_msgfree(ads
, msg
);
952 /* Lookup aliases a user is member of - use rpc methods */
953 static NTSTATUS
lookup_useraliases(struct winbindd_domain
*domain
,
955 uint32 num_sids
, const DOM_SID
*sids
,
956 uint32
*num_aliases
, uint32
**alias_rids
)
958 return reconnect_methods
.lookup_useraliases(domain
, mem_ctx
,
965 find the members of a group, given a group rid and domain
967 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
969 const DOM_SID
*group_sid
,
970 enum lsa_SidType type
,
972 DOM_SID
**sid_mem
, char ***names
,
976 ADS_STRUCT
*ads
= NULL
;
978 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
980 char **members
= NULL
;
982 size_t num_members
= 0;
984 DOM_SID
*sid_mem_nocache
= NULL
;
985 char **names_nocache
= NULL
;
986 enum lsa_SidType
*name_types_nocache
= NULL
;
987 char **domains_nocache
= NULL
; /* only needed for rpccli_lsa_lookup_sids */
988 uint32 num_nocache
= 0;
989 TALLOC_CTX
*tmp_ctx
= NULL
;
991 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain
->name
,
992 sid_string_dbg(group_sid
)));
996 tmp_ctx
= talloc_new(mem_ctx
);
998 DEBUG(1, ("ads: lookup_groupmem: talloc failed\n"));
999 status
= NT_STATUS_NO_MEMORY
;
1003 if ( !winbindd_can_contact_domain( domain
) ) {
1004 DEBUG(10,("lookup_groupmem: No incoming trust for domain %s\n",
1006 return NT_STATUS_OK
;
1009 ads
= ads_cached_connection(domain
);
1012 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
1016 if ((sidbinstr
= sid_binstring(talloc_tos(), group_sid
)) == NULL
) {
1017 status
= NT_STATUS_NO_MEMORY
;
1021 /* search for all members of the group */
1022 ldap_exp
= talloc_asprintf(tmp_ctx
, "(objectSid=%s)", sidbinstr
);
1023 TALLOC_FREE(sidbinstr
);
1024 if (ldap_exp
== NULL
) {
1025 DEBUG(1, ("ads: lookup_groupmem: talloc_asprintf for ldap_exp failed!\n"));
1026 status
= NT_STATUS_NO_MEMORY
;
1030 args
.control
= ADS_EXTENDED_DN_OID
;
1031 args
.val
= ADS_EXTENDED_DN_HEX_STRING
;
1032 args
.critical
= True
;
1034 rc
= ads_ranged_search(ads
, tmp_ctx
, LDAP_SCOPE_SUBTREE
, ads
->config
.bind_path
,
1035 ldap_exp
, &args
, "member", &members
, &num_members
);
1037 if (!ADS_ERR_OK(rc
)) {
1038 DEBUG(0,("ads_ranged_search failed with: %s\n", ads_errstr(rc
)));
1039 status
= NT_STATUS_UNSUCCESSFUL
;
1043 DEBUG(10, ("ads lookup_groupmem: got %d sids via extended dn call\n", (int)num_members
));
1045 /* Now that we have a list of sids, we need to get the
1046 * lists of names and name_types belonging to these sids.
1047 * even though conceptually not quite clean, we use the
1048 * RPC call lsa_lookup_sids for this since it can handle a
1049 * list of sids. ldap calls can just resolve one sid at a time.
1051 * At this stage, the sids are still hidden in the exetended dn
1052 * member output format. We actually do a little better than
1053 * stated above: In extracting the sids from the member strings,
1054 * we try to resolve as many sids as possible from the
1055 * cache. Only the rest is passed to the lsa_lookup_sids call. */
1058 (*sid_mem
) = TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
1059 (*names
) = TALLOC_ZERO_ARRAY(mem_ctx
, char *, num_members
);
1060 (*name_types
) = TALLOC_ZERO_ARRAY(mem_ctx
, uint32
, num_members
);
1061 (sid_mem_nocache
) = TALLOC_ZERO_ARRAY(tmp_ctx
, DOM_SID
, num_members
);
1063 if ((members
== NULL
) || (*sid_mem
== NULL
) ||
1064 (*names
== NULL
) || (*name_types
== NULL
) ||
1065 (sid_mem_nocache
== NULL
))
1067 DEBUG(1, ("ads: lookup_groupmem: talloc failed\n"));
1068 status
= NT_STATUS_NO_MEMORY
;
1075 (*name_types
) = NULL
;
1078 for (i
=0; i
<num_members
; i
++) {
1079 enum lsa_SidType name_type
;
1080 char *name
, *domain_name
;
1083 rc
= ads_get_sid_from_extended_dn(tmp_ctx
, members
[i
], args
.val
,
1085 if (!ADS_ERR_OK(rc
)) {
1086 if (NT_STATUS_EQUAL(ads_ntstatus(rc
),
1087 NT_STATUS_NOT_FOUND
)) {
1088 /* Group members can be objects, like Exchange
1089 * Public Folders, that don't have a SID. Skip
1094 status
= ads_ntstatus(rc
);
1098 if (lookup_cached_sid(mem_ctx
, &sid
, &domain_name
, &name
,
1100 DEBUG(10,("ads: lookup_groupmem: got sid %s from "
1101 "cache\n", sid_string_dbg(&sid
)));
1102 sid_copy(&(*sid_mem
)[*num_names
], &sid
);
1103 (*names
)[*num_names
] = fill_domain_username_talloc(
1109 (*name_types
)[*num_names
] = name_type
;
1113 DEBUG(10, ("ads: lookup_groupmem: sid %s not found in "
1114 "cache\n", sid_string_dbg(&sid
)));
1115 sid_copy(&(sid_mem_nocache
)[num_nocache
], &sid
);
1120 DEBUG(10, ("ads: lookup_groupmem: %d sids found in cache, "
1121 "%d left for lsa_lookupsids\n", *num_names
, num_nocache
));
1123 /* handle sids not resolved from cache by lsa_lookup_sids */
1124 if (num_nocache
> 0) {
1126 status
= winbindd_lookup_sids(tmp_ctx
,
1132 &name_types_nocache
);
1134 if (!(NT_STATUS_IS_OK(status
) ||
1135 NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
) ||
1136 NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)))
1138 DEBUG(1, ("lsa_lookupsids call failed with %s "
1139 "- retrying...\n", nt_errstr(status
)));
1141 status
= winbindd_lookup_sids(tmp_ctx
,
1147 &name_types_nocache
);
1150 if (NT_STATUS_IS_OK(status
) ||
1151 NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
))
1153 /* Copy the entries over from the "_nocache" arrays
1154 * to the result arrays, skipping the gaps the
1155 * lookup_sids call left. */
1156 for (i
=0; i
< num_nocache
; i
++) {
1157 if (((names_nocache
)[i
] != NULL
) &&
1158 ((name_types_nocache
)[i
] != SID_NAME_UNKNOWN
))
1160 sid_copy(&(*sid_mem
)[*num_names
],
1161 &sid_mem_nocache
[i
]);
1162 (*names
)[*num_names
] =
1163 fill_domain_username_talloc(
1168 (*name_types
)[*num_names
] = name_types_nocache
[i
];
1173 else if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
1174 DEBUG(10, ("lookup_groupmem: lsa_lookup_sids could "
1175 "not map any SIDs at all.\n"));
1176 /* Don't handle this as an error here.
1177 * There is nothing left to do with respect to the
1178 * overall result... */
1180 else if (!NT_STATUS_IS_OK(status
)) {
1181 DEBUG(10, ("lookup_groupmem: Error looking up %d "
1182 "sids via rpc_lsa_lookup_sids: %s\n",
1183 (int)num_members
, nt_errstr(status
)));
1188 status
= NT_STATUS_OK
;
1189 DEBUG(3,("ads lookup_groupmem for sid=%s succeeded\n",
1190 sid_string_dbg(group_sid
)));
1194 TALLOC_FREE(tmp_ctx
);
1199 /* find the sequence number for a domain */
1200 static NTSTATUS
sequence_number(struct winbindd_domain
*domain
, uint32
*seq
)
1202 ADS_STRUCT
*ads
= NULL
;
1205 DEBUG(3,("ads: fetch sequence_number for %s\n", domain
->name
));
1207 if ( !winbindd_can_contact_domain( domain
) ) {
1208 DEBUG(10,("sequence: No incoming trust for domain %s\n",
1211 return NT_STATUS_OK
;
1214 *seq
= DOM_SEQUENCE_NONE
;
1216 ads
= ads_cached_connection(domain
);
1219 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
1220 return NT_STATUS_UNSUCCESSFUL
;
1223 rc
= ads_USN(ads
, seq
);
1225 if (!ADS_ERR_OK(rc
)) {
1227 /* its a dead connection, destroy it */
1229 if (domain
->private_data
) {
1230 ads
= (ADS_STRUCT
*)domain
->private_data
;
1231 ads
->is_mine
= True
;
1233 ads_kdestroy("MEMORY:winbind_ccache");
1234 domain
->private_data
= NULL
;
1237 return ads_ntstatus(rc
);
1240 /* find the lockout policy of a domain - use rpc methods */
1241 static NTSTATUS
lockout_policy(struct winbindd_domain
*domain
,
1242 TALLOC_CTX
*mem_ctx
,
1243 struct samr_DomInfo12
*policy
)
1245 return reconnect_methods
.lockout_policy(domain
, mem_ctx
, policy
);
1248 /* find the password policy of a domain - use rpc methods */
1249 static NTSTATUS
password_policy(struct winbindd_domain
*domain
,
1250 TALLOC_CTX
*mem_ctx
,
1251 struct samr_DomInfo1
*policy
)
1253 return reconnect_methods
.password_policy(domain
, mem_ctx
, policy
);
1256 /* get a list of trusted domains */
1257 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1258 TALLOC_CTX
*mem_ctx
,
1259 uint32
*num_domains
,
1264 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1265 struct netr_DomainTrustList trusts
;
1268 struct rpc_pipe_client
*cli
;
1269 uint32 fr_flags
= (NETR_TRUST_FLAG_IN_FOREST
| NETR_TRUST_FLAG_TREEROOT
);
1272 DEBUG(3,("ads: trusted_domains\n"));
1279 /* If this is our primary domain or a root in our forest,
1280 query for all trusts. If not, then just look for domain
1281 trusts in the target forest */
1283 if ( domain
->primary
||
1284 ((domain
->domain_flags
&fr_flags
) == fr_flags
) )
1286 flags
= NETR_TRUST_FLAG_OUTBOUND
|
1287 NETR_TRUST_FLAG_INBOUND
|
1288 NETR_TRUST_FLAG_IN_FOREST
;
1290 flags
= NETR_TRUST_FLAG_IN_FOREST
;
1293 result
= cm_connect_netlogon(domain
, &cli
);
1295 if (!NT_STATUS_IS_OK(result
)) {
1296 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
1297 "for PIPE_NETLOGON (%s)\n",
1298 domain
->name
, nt_errstr(result
)));
1299 return NT_STATUS_UNSUCCESSFUL
;
1302 result
= rpccli_netr_DsrEnumerateDomainTrusts(cli
, mem_ctx
,
1307 if ( NT_STATUS_IS_OK(result
) && trusts
.count
) {
1309 /* Allocate memory for trusted domain names and sids */
1311 if ( !(*names
= TALLOC_ARRAY(mem_ctx
, char *, trusts
.count
)) ) {
1312 DEBUG(0, ("trusted_domains: out of memory\n"));
1313 return NT_STATUS_NO_MEMORY
;
1316 if ( !(*alt_names
= TALLOC_ARRAY(mem_ctx
, char *, trusts
.count
)) ) {
1317 DEBUG(0, ("trusted_domains: out of memory\n"));
1318 return NT_STATUS_NO_MEMORY
;
1321 if ( !(*dom_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, trusts
.count
)) ) {
1322 DEBUG(0, ("trusted_domains: out of memory\n"));
1323 return NT_STATUS_NO_MEMORY
;
1326 /* Copy across names and sids */
1330 for (i
= 0; i
< trusts
.count
; i
++) {
1331 struct winbindd_domain d
;
1335 /* drop external trusts if this is not our primary
1336 domain. This means that the returned number of
1337 domains may be less that the ones actually trusted
1340 if ( (trusts
.array
[i
].trust_attributes
== NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
) &&
1343 DEBUG(10,("trusted_domains: Skipping external trusted domain "
1344 "%s because it is outside of our primary domain\n",
1345 trusts
.array
[i
].netbios_name
));
1349 /* We must check that the SID of each trusted domain
1350 * was returned to work around a bug in Windows:
1351 * http://support.microsoft.com/kb/922832 */
1353 (*names
)[ret_count
] = CONST_DISCARD(char *, trusts
.array
[i
].netbios_name
);
1354 (*alt_names
)[ret_count
] = CONST_DISCARD(char *, trusts
.array
[i
].dns_name
);
1355 if (trusts
.array
[i
].sid
) {
1356 sid_copy(&(*dom_sids
)[ret_count
], trusts
.array
[i
].sid
);
1358 sid_copy(&(*dom_sids
)[ret_count
], &global_sid_NULL
);
1361 /* add to the trusted domain cache */
1363 fstrcpy( d
.name
, trusts
.array
[i
].netbios_name
);
1364 fstrcpy( d
.alt_name
, trusts
.array
[i
].dns_name
);
1365 if (trusts
.array
[i
].sid
) {
1366 sid_copy( &d
.sid
, trusts
.array
[i
].sid
);
1368 sid_copy(&d
.sid
, &global_sid_NULL
);
1371 if ( domain
->primary
) {
1372 DEBUG(10,("trusted_domains(ads): Searching "
1373 "trusted domain list of %s and storing "
1374 "trust flags for domain %s\n",
1375 domain
->name
, d
.alt_name
));
1377 d
.domain_flags
= trusts
.array
[i
].trust_flags
;
1378 d
.domain_type
= trusts
.array
[i
].trust_type
;
1379 d
.domain_trust_attribs
= trusts
.array
[i
].trust_attributes
;
1381 wcache_tdc_add_domain( &d
);
1383 } else if ( (domain
->domain_flags
&fr_flags
) == fr_flags
) {
1384 /* Check if we already have this record. If
1385 * we are following our forest root that is not
1386 * our primary domain, we want to keep trust
1387 * flags from the perspective of our primary
1388 * domain not our forest root. */
1389 struct winbindd_tdc_domain
*exist
= NULL
;
1392 wcache_tdc_fetch_domain(NULL
, trusts
.array
[i
].netbios_name
);
1394 DEBUG(10,("trusted_domains(ads): Searching "
1395 "trusted domain list of %s and storing "
1396 "trust flags for domain %s\n",
1397 domain
->name
, d
.alt_name
));
1398 d
.domain_flags
= trusts
.array
[i
].trust_flags
;
1399 d
.domain_type
= trusts
.array
[i
].trust_type
;
1400 d
.domain_trust_attribs
= trusts
.array
[i
].trust_attributes
;
1402 wcache_tdc_add_domain( &d
);
1407 /* This gets a little tricky. If we are
1408 following a transitive forest trust, then
1409 innerit the flags, type, and attribs from
1410 the domain we queried to make sure we don't
1411 record the view of the trust from the wrong
1412 side. Always view it from the side of our
1413 primary domain. --jerry */
1414 struct winbindd_tdc_domain
*parent
= NULL
;
1416 DEBUG(10,("trusted_domains(ads): Searching "
1417 "trusted domain list of %s and inheriting "
1418 "trust flags for domain %s\n",
1419 domain
->name
, d
.alt_name
));
1421 parent
= wcache_tdc_fetch_domain(NULL
, domain
->name
);
1423 d
.domain_flags
= parent
->trust_flags
;
1424 d
.domain_type
= parent
->trust_type
;
1425 d
.domain_trust_attribs
= parent
->trust_attribs
;
1427 d
.domain_flags
= domain
->domain_flags
;
1428 d
.domain_type
= domain
->domain_type
;
1429 d
.domain_trust_attribs
= domain
->domain_trust_attribs
;
1431 TALLOC_FREE(parent
);
1433 wcache_tdc_add_domain( &d
);
1438 *num_domains
= ret_count
;
1444 /* the ADS backend methods are exposed via this structure */
1445 struct winbindd_methods ads_methods
= {