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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #define DBGC_CLASS DBGC_WINBIND
34 return our ads connections structure for a domain. We keep the connection
35 open to make things faster
37 static ADS_STRUCT
*ads_cached_connection(struct winbindd_domain
*domain
)
41 enum wb_posix_mapping map_type
;
43 DEBUG(10,("ads_cached_connection\n"));
45 if (domain
->private_data
) {
46 ads
= (ADS_STRUCT
*)domain
->private_data
;
48 /* check for a valid structure */
50 DEBUG(7, ("Current tickets expire at %d, time is now %d\n",
51 (uint32
) ads
->auth
.expire
, (uint32
) time(NULL
)));
52 if ( ads
->config
.realm
&& (ads
->auth
.expire
> time(NULL
))) {
56 /* we own this ADS_STRUCT so make sure it goes away */
59 ads_kdestroy("MEMORY:winbind_ccache");
60 domain
->private_data
= NULL
;
64 /* we don't want this to affect the users ccache */
65 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
67 ads
= ads_init(domain
->alt_name
, domain
->name
, NULL
);
69 DEBUG(1,("ads_init for domain %s failed\n", domain
->name
));
73 /* the machine acct password might have change - fetch it every time */
75 SAFE_FREE(ads
->auth
.password
);
76 SAFE_FREE(ads
->auth
.realm
);
82 if ( !secrets_fetch_trusted_domain_password( domain
->name
, &ads
->auth
.password
, &sid
, &last_set_time
) ) {
86 ads
->auth
.realm
= SMB_STRDUP( ads
->server
.realm
);
87 strupper_m( ads
->auth
.realm
);
90 struct winbindd_domain
*our_domain
= domain
;
92 ads
->auth
.password
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
94 /* always give preference to the alt_name in our
95 primary domain if possible */
97 if ( !domain
->primary
)
98 our_domain
= find_our_domain();
100 if ( our_domain
->alt_name
[0] != '\0' ) {
101 ads
->auth
.realm
= SMB_STRDUP( our_domain
->alt_name
);
102 strupper_m( ads
->auth
.realm
);
105 ads
->auth
.realm
= SMB_STRDUP( lp_realm() );
108 ads
->auth
.renewable
= WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
;
110 status
= ads_connect(ads
);
111 if (!ADS_ERR_OK(status
) || !ads
->config
.realm
) {
112 extern struct winbindd_methods msrpc_methods
, cache_methods
;
113 DEBUG(1,("ads_connect for domain %s failed: %s\n",
114 domain
->name
, ads_errstr(status
)));
117 /* if we get ECONNREFUSED then it might be a NT4
118 server, fall back to MSRPC */
119 if (status
.error_type
== ENUM_ADS_ERROR_SYSTEM
&&
120 status
.err
.rc
== ECONNREFUSED
) {
121 DEBUG(1,("Trying MSRPC methods\n"));
122 if (domain
->methods
== &cache_methods
) {
123 domain
->backend
= &msrpc_methods
;
125 domain
->methods
= &msrpc_methods
;
131 map_type
= get_nss_info(domain
->name
);
133 if ((map_type
== WB_POSIX_MAP_RFC2307
)||
134 (map_type
== WB_POSIX_MAP_SFU
)) {
136 status
= ads_check_posix_schema_mapping(ads
, map_type
);
137 if (!ADS_ERR_OK(status
)) {
138 DEBUG(10,("ads_check_posix_schema_mapping failed "
139 "with: %s\n", ads_errstr(status
)));
143 /* set the flag that says we don't own the memory even
144 though we do so that ads_destroy() won't destroy the
145 structure we pass back by reference */
147 ads
->is_mine
= False
;
149 domain
->private_data
= (void *)ads
;
154 /* Query display info for a realm. This is the basic user list fn */
155 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
158 WINBIND_USERINFO
**info
)
160 ADS_STRUCT
*ads
= NULL
;
161 const char *attrs
[] = {"userPrincipalName",
163 "name", "objectSid", "primaryGroupID",
165 ADS_ATTR_SFU_HOMEDIR_OID
,
166 ADS_ATTR_SFU_SHELL_OID
,
167 ADS_ATTR_SFU_GECOS_OID
,
168 ADS_ATTR_RFC2307_HOMEDIR_OID
,
169 ADS_ATTR_RFC2307_SHELL_OID
,
170 ADS_ATTR_RFC2307_GECOS_OID
,
176 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
180 DEBUG(3,("ads: query_user_list\n"));
182 ads
= ads_cached_connection(domain
);
185 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
189 rc
= ads_search_retry(ads
, &res
, "(objectCategory=user)", attrs
);
190 if (!ADS_ERR_OK(rc
) || !res
) {
191 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc
)));
195 count
= ads_count_replies(ads
, res
);
197 DEBUG(1,("query_user_list: No users found\n"));
201 (*info
) = TALLOC_ZERO_ARRAY(mem_ctx
, WINBIND_USERINFO
, count
);
203 status
= NT_STATUS_NO_MEMORY
;
209 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
210 char *name
, *gecos
= NULL
;
211 char *homedir
= NULL
;
216 if (!ads_pull_uint32(ads
, msg
, "sAMAccountType", &atype
) ||
217 ads_atype_map(atype
) != SID_NAME_USER
) {
218 DEBUG(1,("Not a user account? atype=0x%x\n", atype
));
222 name
= ads_pull_username(ads
, mem_ctx
, msg
);
224 if (get_nss_info(domain
->name
) && ads
->schema
.map_type
) {
226 DEBUG(10,("pulling posix attributes (%s schema)\n",
227 wb_posix_map_str(ads
->schema
.map_type
)));
229 homedir
= ads_pull_string(ads
, mem_ctx
, msg
,
230 ads
->schema
.posix_homedir_attr
);
231 shell
= ads_pull_string(ads
, mem_ctx
, msg
,
232 ads
->schema
.posix_shell_attr
);
233 gecos
= ads_pull_string(ads
, mem_ctx
, msg
,
234 ads
->schema
.posix_gecos_attr
);
238 gecos
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
241 if (!ads_pull_sid(ads
, msg
, "objectSid",
242 &(*info
)[i
].user_sid
)) {
243 DEBUG(1,("No sid for %s !?\n", name
));
246 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &group
)) {
247 DEBUG(1,("No primary group for %s !?\n", name
));
251 (*info
)[i
].acct_name
= name
;
252 (*info
)[i
].full_name
= gecos
;
253 (*info
)[i
].homedir
= homedir
;
254 (*info
)[i
].shell
= shell
;
255 sid_compose(&(*info
)[i
].group_sid
, &domain
->sid
, group
);
260 status
= NT_STATUS_OK
;
262 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries
)));
266 ads_msgfree(ads
, res
);
271 /* list all domain groups */
272 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
275 struct acct_info
**info
)
277 ADS_STRUCT
*ads
= NULL
;
278 const char *attrs
[] = {"userPrincipalName", "sAMAccountName",
279 "name", "objectSid", NULL
};
284 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
286 BOOL enum_dom_local_groups
= False
;
290 DEBUG(3,("ads: enum_dom_groups\n"));
292 /* only grab domain local groups for our domain */
293 if ( domain
->native_mode
&& strequal(lp_realm(), domain
->alt_name
) ) {
294 enum_dom_local_groups
= True
;
297 /* Workaround ADS LDAP bug present in MS W2K3 SP0 and W2K SP4 w/o
300 * According to Section 5.1(4) of RFC 2251 if a value of a type is it's
301 * default value, it MUST be absent. In case of extensible matching the
302 * "dnattr" boolean defaults to FALSE and so it must be only be present
305 * When it is set to FALSE and the OpenLDAP lib (correctly) encodes a
306 * filter using bitwise matching rule then the buggy AD fails to decode
307 * the extensible match. As a workaround set it to TRUE and thereby add
308 * the dnAttributes "dn" field to cope with those older AD versions.
309 * It should not harm and won't put any additional load on the AD since
310 * none of the dn components have a bitmask-attribute.
312 * Thanks to Ralf Haferkamp for input and testing - Guenther */
314 filter
= talloc_asprintf(mem_ctx
, "(&(objectCategory=group)(&(groupType:dn:%s:=%d)(!(groupType:dn:%s:=%d))))",
315 ADS_LDAP_MATCHING_RULE_BIT_AND
, GROUP_TYPE_SECURITY_ENABLED
,
316 ADS_LDAP_MATCHING_RULE_BIT_AND
,
317 enum_dom_local_groups
? GROUP_TYPE_BUILTIN_LOCAL_GROUP
: GROUP_TYPE_RESOURCE_GROUP
);
319 if (filter
== NULL
) {
320 status
= NT_STATUS_NO_MEMORY
;
324 ads
= ads_cached_connection(domain
);
327 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
331 rc
= ads_search_retry(ads
, &res
, filter
, attrs
);
332 if (!ADS_ERR_OK(rc
) || !res
) {
333 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc
)));
337 count
= ads_count_replies(ads
, res
);
339 DEBUG(1,("enum_dom_groups: No groups found\n"));
343 (*info
) = TALLOC_ZERO_ARRAY(mem_ctx
, struct acct_info
, count
);
345 status
= NT_STATUS_NO_MEMORY
;
351 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
356 name
= ads_pull_username(ads
, mem_ctx
, msg
);
357 gecos
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
358 if (!ads_pull_sid(ads
, msg
, "objectSid", &sid
)) {
359 DEBUG(1,("No sid for %s !?\n", name
));
363 if (!sid_peek_check_rid(&domain
->sid
, &sid
, &rid
)) {
364 DEBUG(1,("No rid for %s !?\n", name
));
368 fstrcpy((*info
)[i
].acct_name
, name
);
369 fstrcpy((*info
)[i
].acct_desc
, gecos
);
370 (*info
)[i
].rid
= rid
;
376 status
= NT_STATUS_OK
;
378 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries
)));
382 ads_msgfree(ads
, res
);
387 /* list all domain local groups */
388 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
391 struct acct_info
**info
)
394 * This is a stub function only as we returned the domain
395 * local groups in enum_dom_groups() if the domain->native field
396 * was true. This is a simple performance optimization when
399 * if we ever need to enumerate domain local groups separately,
400 * then this the optimization in enum_dom_groups() will need
408 /* convert a DN to a name, SID and name type
409 this might become a major speed bottleneck if groups have
410 lots of users, in which case we could cache the results
412 static BOOL
dn_lookup(ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
,
414 char **name
, uint32
*name_type
, DOM_SID
*sid
)
417 const char *attrs
[] = {"userPrincipalName", "sAMAccountName",
418 "objectSid", "sAMAccountType", NULL
};
421 DEBUG(3,("ads: dn_lookup\n"));
423 rc
= ads_search_retry_dn(ads
, &res
, dn
, attrs
);
425 if (!ADS_ERR_OK(rc
) || !res
) {
429 (*name
) = ads_pull_username(ads
, mem_ctx
, res
);
431 if (!ads_pull_uint32(ads
, res
, "sAMAccountType", &atype
)) {
434 (*name_type
) = ads_atype_map(atype
);
436 if (!ads_pull_sid(ads
, res
, "objectSid", sid
)) {
441 ads_msgfree(ads
, res
);
447 ads_msgfree(ads
, res
);
452 /* Lookup user information from a rid */
453 static NTSTATUS
query_user(struct winbindd_domain
*domain
,
456 WINBIND_USERINFO
*info
)
458 ADS_STRUCT
*ads
= NULL
;
459 const char *attrs
[] = {"userPrincipalName",
463 ADS_ATTR_SFU_HOMEDIR_OID
,
464 ADS_ATTR_SFU_SHELL_OID
,
465 ADS_ATTR_SFU_GECOS_OID
,
466 ADS_ATTR_RFC2307_HOMEDIR_OID
,
467 ADS_ATTR_RFC2307_SHELL_OID
,
468 ADS_ATTR_RFC2307_GECOS_OID
,
476 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
478 DEBUG(3,("ads: query_user\n"));
480 ads
= ads_cached_connection(domain
);
483 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
487 sidstr
= sid_binstring(sid
);
488 asprintf(&ldap_exp
, "(objectSid=%s)", sidstr
);
489 rc
= ads_search_retry(ads
, &msg
, ldap_exp
, attrs
);
492 if (!ADS_ERR_OK(rc
) || !msg
) {
493 DEBUG(1,("query_user(sid=%s) ads_search: %s\n",
494 sid_string_static(sid
), ads_errstr(rc
)));
498 count
= ads_count_replies(ads
, msg
);
500 DEBUG(1,("query_user(sid=%s): Not found\n",
501 sid_string_static(sid
)));
505 info
->acct_name
= ads_pull_username(ads
, mem_ctx
, msg
);
507 if (get_nss_info(domain
->name
) && ads
->schema
.map_type
) {
509 DEBUG(10,("pulling posix attributes (%s schema)\n",
510 wb_posix_map_str(ads
->schema
.map_type
)));
512 info
->homedir
= ads_pull_string(ads
, mem_ctx
, msg
,
513 ads
->schema
.posix_homedir_attr
);
514 info
->shell
= ads_pull_string(ads
, mem_ctx
, msg
,
515 ads
->schema
.posix_shell_attr
);
516 info
->full_name
= ads_pull_string(ads
, mem_ctx
, msg
,
517 ads
->schema
.posix_gecos_attr
);
520 if (info
->full_name
== NULL
) {
521 info
->full_name
= ads_pull_string(ads
, mem_ctx
, msg
, "name");
524 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &group_rid
)) {
525 DEBUG(1,("No primary group for %s !?\n",
526 sid_string_static(sid
)));
530 sid_copy(&info
->user_sid
, sid
);
531 sid_compose(&info
->group_sid
, &domain
->sid
, group_rid
);
533 status
= NT_STATUS_OK
;
535 DEBUG(3,("ads query_user gave %s\n", info
->acct_name
));
538 ads_msgfree(ads
, msg
);
543 /* Lookup groups a user is a member of - alternate method, for when
544 tokenGroups are not available. */
545 static NTSTATUS
lookup_usergroups_member(struct winbindd_domain
*domain
,
548 DOM_SID
*primary_group
,
549 size_t *p_num_groups
, DOM_SID
**user_sids
)
552 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
558 const char *group_attrs
[] = {"objectSid", NULL
};
560 size_t num_groups
= 0;
562 DEBUG(3,("ads: lookup_usergroups_member\n"));
564 ads
= ads_cached_connection(domain
);
567 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
571 if (!(escaped_dn
= escape_ldap_string_alloc(user_dn
))) {
572 status
= NT_STATUS_NO_MEMORY
;
576 if (!(ldap_exp
= talloc_asprintf(mem_ctx
, "(&(member=%s)(objectCategory=group))", escaped_dn
))) {
577 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn
));
578 SAFE_FREE(escaped_dn
);
579 status
= NT_STATUS_NO_MEMORY
;
583 SAFE_FREE(escaped_dn
);
585 rc
= ads_search_retry(ads
, &res
, ldap_exp
, group_attrs
);
587 if (!ADS_ERR_OK(rc
) || !res
) {
588 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn
, ads_errstr(rc
)));
589 return ads_ntstatus(rc
);
592 count
= ads_count_replies(ads
, res
);
597 /* always add the primary group to the sid array */
598 add_sid_to_array(mem_ctx
, primary_group
, user_sids
, &num_groups
);
601 for (msg
= ads_first_entry(ads
, res
); msg
;
602 msg
= ads_next_entry(ads
, msg
)) {
605 if (!ads_pull_sid(ads
, msg
, "objectSid", &group_sid
)) {
606 DEBUG(1,("No sid for this group ?!?\n"));
610 /* ignore Builtin groups from ADS - Guenther */
611 if (sid_check_is_in_builtin(&group_sid
)) {
615 add_sid_to_array(mem_ctx
, &group_sid
, user_sids
,
621 *p_num_groups
= num_groups
;
622 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
624 DEBUG(3,("ads lookup_usergroups (member) succeeded for dn=%s\n", user_dn
));
627 ads_msgfree(ads
, res
);
632 /* Lookup groups a user is a member of - alternate method, for when
633 tokenGroups are not available. */
634 static NTSTATUS
lookup_usergroups_memberof(struct winbindd_domain
*domain
,
637 DOM_SID
*primary_group
,
638 size_t *p_num_groups
, DOM_SID
**user_sids
)
641 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
645 const char *attrs
[] = {"memberOf", NULL
};
646 size_t num_groups
= 0;
647 DOM_SID
*group_sids
= NULL
;
650 DEBUG(3,("ads: lookup_usergroups_memberof\n"));
652 ads
= ads_cached_connection(domain
);
655 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
659 rc
= ads_search_retry_extended_dn(ads
, &res
, user_dn
, attrs
,
660 ADS_EXTENDED_DN_HEX_STRING
);
662 if (!ADS_ERR_OK(rc
) || !res
) {
663 DEBUG(1,("lookup_usergroups_memberof ads_search member=%s: %s\n",
664 user_dn
, ads_errstr(rc
)));
665 return ads_ntstatus(rc
);
668 count
= ads_count_replies(ads
, res
);
671 status
= NT_STATUS_NO_SUCH_USER
;
678 /* always add the primary group to the sid array */
679 add_sid_to_array(mem_ctx
, primary_group
, user_sids
, &num_groups
);
681 count
= ads_pull_sids_from_extendeddn(ads
, mem_ctx
, res
, "memberOf",
682 ADS_EXTENDED_DN_HEX_STRING
,
685 DEBUG(1,("No memberOf for this user?!?\n"));
686 status
= NT_STATUS_NO_MEMORY
;
690 for (i
=0; i
<count
; i
++) {
692 /* ignore Builtin groups from ADS - Guenther */
693 if (sid_check_is_in_builtin(&group_sids
[i
])) {
697 add_sid_to_array(mem_ctx
, &group_sids
[i
], user_sids
,
702 *p_num_groups
= num_groups
;
703 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
705 DEBUG(3,("ads lookup_usergroups (memberof) succeeded for dn=%s\n", user_dn
));
707 TALLOC_FREE(group_sids
);
709 ads_msgfree(ads
, res
);
715 /* Lookup groups a user is a member of. */
716 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
719 uint32
*p_num_groups
, DOM_SID
**user_sids
)
721 ADS_STRUCT
*ads
= NULL
;
722 const char *attrs
[] = {"tokenGroups", "primaryGroupID", NULL
};
725 LDAPMessage
*msg
= NULL
;
729 DOM_SID primary_group
;
730 uint32 primary_group_rid
;
732 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
733 size_t num_groups
= 0;
735 DEBUG(3,("ads: lookup_usergroups\n"));
738 status
= lookup_usergroups_cached(domain
, mem_ctx
, sid
,
739 p_num_groups
, user_sids
);
740 if (NT_STATUS_IS_OK(status
)) {
744 ads
= ads_cached_connection(domain
);
747 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
748 status
= NT_STATUS_SERVER_DISABLED
;
752 rc
= ads_search_retry_sid(ads
, (void**)(void *)&msg
, sid
, attrs
);
754 if (!ADS_ERR_OK(rc
)) {
755 status
= ads_ntstatus(rc
);
756 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n",
757 sid_to_string(sid_string
, sid
), ads_errstr(rc
)));
761 count
= ads_count_replies(ads
, msg
);
763 status
= NT_STATUS_UNSUCCESSFUL
;
764 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: "
765 "invalid number of results (count=%d)\n",
766 sid_to_string(sid_string
, sid
), count
));
771 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
772 sid_to_string(sid_string
, sid
)));
773 status
= NT_STATUS_UNSUCCESSFUL
;
777 user_dn
= ads_get_dn(ads
, msg
);
778 if (user_dn
== NULL
) {
779 status
= NT_STATUS_NO_MEMORY
;
783 if (!ads_pull_uint32(ads
, msg
, "primaryGroupID", &primary_group_rid
)) {
784 DEBUG(1,("%s: No primary group for sid=%s !?\n",
785 domain
->name
, sid_to_string(sid_string
, sid
)));
789 sid_copy(&primary_group
, &domain
->sid
);
790 sid_append_rid(&primary_group
, primary_group_rid
);
792 count
= ads_pull_sids(ads
, mem_ctx
, msg
, "tokenGroups", &sids
);
795 ads_msgfree(ads
, msg
);
797 /* there must always be at least one group in the token,
798 unless we are talking to a buggy Win2k server */
800 /* actually this only happens when the machine account has no read
801 * permissions on the tokenGroup attribute - gd */
807 /* lookup what groups this user is a member of by DN search on
810 status
= lookup_usergroups_memberof(domain
, mem_ctx
, user_dn
,
812 p_num_groups
, user_sids
);
813 if (NT_STATUS_IS_OK(status
)) {
817 /* lookup what groups this user is a member of by DN search on
820 return lookup_usergroups_member(domain
, mem_ctx
, user_dn
,
822 p_num_groups
, user_sids
);
828 add_sid_to_array(mem_ctx
, &primary_group
, user_sids
, &num_groups
);
830 for (i
=0;i
<count
;i
++) {
832 /* ignore Builtin groups from ADS - Guenther */
833 if (sid_check_is_in_builtin(&sids
[i
])) {
837 add_sid_to_array_unique(mem_ctx
, &sids
[i
],
838 user_sids
, &num_groups
);
841 *p_num_groups
= (uint32
)num_groups
;
842 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
844 DEBUG(3,("ads lookup_usergroups (tokenGroups) succeeded for sid=%s\n",
845 sid_to_string(sid_string
, sid
)));
851 find the members of a group, given a group rid and domain
853 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
855 const DOM_SID
*group_sid
, uint32
*num_names
,
856 DOM_SID
**sid_mem
, char ***names
,
862 ADS_STRUCT
*ads
= NULL
;
864 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
876 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain
->name
,
877 sid_string_static(group_sid
)));
881 ads
= ads_cached_connection(domain
);
884 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
888 sidstr
= sid_binstring(group_sid
);
890 /* search for all members of the group */
891 if (!(ldap_exp
= talloc_asprintf(mem_ctx
, "(objectSid=%s)",sidstr
))) {
893 DEBUG(1, ("ads: lookup_groupmem: tallloc_asprintf for ldap_exp failed!\n"));
894 status
= NT_STATUS_NO_MEMORY
;
902 attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 3);
903 attrs
[1] = talloc_strdup(mem_ctx
, "usnChanged");
907 if (num_members
== 0)
908 attrs
[0] = talloc_strdup(mem_ctx
, "member");
910 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs
[0], attrs
[1]));
912 rc
= ads_search_retry(ads
, &res
, ldap_exp
, attrs
);
914 if (!ADS_ERR_OK(rc
) || !res
) {
915 DEBUG(1,("ads: lookup_groupmem ads_search: %s\n",
917 status
= ads_ntstatus(rc
);
921 count
= ads_count_replies(ads
, res
);
925 if (num_members
== 0) {
926 if (!ads_pull_uint32(ads
, res
, "usnChanged", &first_usn
)) {
927 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
932 if (!ads_pull_uint32(ads
, res
, "usnChanged", ¤t_usn
)) {
933 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
937 if (first_usn
!= current_usn
) {
938 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
939 " - restarting search\n"));
940 if (num_retries
< 5) {
945 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
946 " - restarted search too many times, aborting!\n"));
947 status
= NT_STATUS_UNSUCCESSFUL
;
952 members
= ads_pull_strings_range(ads
, mem_ctx
, res
,
959 if ((members
== NULL
) || (num_members
== 0))
962 } while (more_values
);
964 /* now we need to turn a list of members into rids, names and name types
965 the problem is that the members are in the form of distinguised names
968 (*sid_mem
) = TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
969 (*name_types
) = TALLOC_ZERO_ARRAY(mem_ctx
, uint32
, num_members
);
970 (*names
) = TALLOC_ZERO_ARRAY(mem_ctx
, char *, num_members
);
972 if ((num_members
!= 0) &&
973 ((members
== NULL
) || (*sid_mem
== NULL
) ||
974 (*name_types
== NULL
) || (*names
== NULL
))) {
975 DEBUG(1, ("talloc failed\n"));
976 status
= NT_STATUS_NO_MEMORY
;
980 for (i
=0;i
<num_members
;i
++) {
985 if (dn_lookup(ads
, mem_ctx
, members
[i
], &name
, &name_type
, &sid
)) {
986 (*names
)[*num_names
] = name
;
987 (*name_types
)[*num_names
] = name_type
;
988 sid_copy(&(*sid_mem
)[*num_names
], &sid
);
993 status
= NT_STATUS_OK
;
994 DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string
, group_sid
)));
998 ads_msgfree(ads
, res
);
1003 /* find the sequence number for a domain */
1004 static NTSTATUS
sequence_number(struct winbindd_domain
*domain
, uint32
*seq
)
1006 ADS_STRUCT
*ads
= NULL
;
1009 DEBUG(3,("ads: fetch sequence_number for %s\n", domain
->name
));
1011 *seq
= DOM_SEQUENCE_NONE
;
1013 ads
= ads_cached_connection(domain
);
1016 domain
->last_status
= NT_STATUS_SERVER_DISABLED
;
1017 return NT_STATUS_UNSUCCESSFUL
;
1020 rc
= ads_USN(ads
, seq
);
1022 if (!ADS_ERR_OK(rc
)) {
1024 /* its a dead connection ; don't destroy it
1025 through since ads_USN() has already done
1028 domain
->private_data
= NULL
;
1030 return ads_ntstatus(rc
);
1033 /* get a list of trusted domains */
1034 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1035 TALLOC_CTX
*mem_ctx
,
1036 uint32
*num_domains
,
1041 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1042 struct ds_domain_trust
*domains
= NULL
;
1045 uint32 flags
= DS_DOMAIN_DIRECT_OUTBOUND
;
1046 struct rpc_pipe_client
*cli
;
1048 DEBUG(3,("ads: trusted_domains\n"));
1055 result
= cm_connect_netlogon(domain
, &cli
);
1057 if (!NT_STATUS_IS_OK(result
)) {
1058 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
1059 "for PIPE_NETLOGON (%s)\n",
1060 domain
->name
, nt_errstr(result
)));
1061 return NT_STATUS_UNSUCCESSFUL
;
1064 if ( NT_STATUS_IS_OK(result
) ) {
1065 result
= rpccli_ds_enum_domain_trusts(cli
, mem_ctx
,
1068 (unsigned int *)&count
);
1071 if ( NT_STATUS_IS_OK(result
) && count
) {
1073 /* Allocate memory for trusted domain names and sids */
1075 if ( !(*names
= TALLOC_ARRAY(mem_ctx
, char *, count
)) ) {
1076 DEBUG(0, ("trusted_domains: out of memory\n"));
1077 return NT_STATUS_NO_MEMORY
;
1080 if ( !(*alt_names
= TALLOC_ARRAY(mem_ctx
, char *, count
)) ) {
1081 DEBUG(0, ("trusted_domains: out of memory\n"));
1082 return NT_STATUS_NO_MEMORY
;
1085 if ( !(*dom_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, count
)) ) {
1086 DEBUG(0, ("trusted_domains: out of memory\n"));
1087 return NT_STATUS_NO_MEMORY
;
1090 /* Copy across names and sids */
1092 for (i
= 0; i
< count
; i
++) {
1093 (*names
)[i
] = domains
[i
].netbios_domain
;
1094 (*alt_names
)[i
] = domains
[i
].dns_domain
;
1096 sid_copy(&(*dom_sids
)[i
], &domains
[i
].sid
);
1099 *num_domains
= count
;
1105 /* the ADS backend methods are exposed via this structure */
1106 struct winbindd_methods ads_methods
= {
1115 msrpc_lookup_useraliases
,
1118 msrpc_lockout_policy
,
1119 msrpc_password_policy
,