r14145: Add missing WITH_KCM hunks from my local tree.
[Samba/nascimento.git] / source3 / nsswitch / winbindd_ads.c
blob721c345aa5b47d93523e3bf8e40b661e6bd10f60
1 /*
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.
25 #include "includes.h"
26 #include "winbindd.h"
28 #ifdef HAVE_ADS
30 #undef DBGC_CLASS
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)
39 ADS_STRUCT *ads;
40 ADS_STATUS status;
42 if (domain->private_data) {
43 ads = (ADS_STRUCT *)domain->private_data;
45 /* check for a valid structure */
47 DEBUG(7, ("Current tickets expire at %d, time is now %d\n",
48 (uint32) ads->auth.expire, (uint32) time(NULL)));
49 if ( ads->config.realm && (ads->auth.expire > time(NULL))) {
50 return ads;
52 else {
53 /* we own this ADS_STRUCT so make sure it goes away */
54 ads->is_mine = True;
55 ads_destroy( &ads );
56 ads_kdestroy("MEMORY:winbind_ccache");
57 domain->private_data = NULL;
61 /* we don't want this to affect the users ccache */
62 #ifdef WITH_KCM
63 setenv("KRB5CCNAME", "KCM:SYSTEM", 1);
64 #else
65 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
66 #endif
68 ads = ads_init(domain->alt_name, domain->name, NULL);
69 if (!ads) {
70 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
71 return NULL;
74 /* the machine acct password might have change - fetch it every time */
76 SAFE_FREE(ads->auth.password);
77 SAFE_FREE(ads->auth.realm);
79 if ( IS_DC ) {
80 DOM_SID sid;
81 time_t last_set_time;
83 if ( !secrets_fetch_trusted_domain_password( domain->name, &ads->auth.password, &sid, &last_set_time ) ) {
84 ads_destroy( &ads );
85 return NULL;
87 ads->auth.realm = SMB_STRDUP( ads->server.realm );
88 strupper_m( ads->auth.realm );
90 else {
91 struct winbindd_domain *our_domain = domain;
93 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
95 /* always give preference to the alt_name in our
96 primary domain if possible */
98 if ( !domain->primary )
99 our_domain = find_our_domain();
101 if ( our_domain->alt_name[0] != '\0' ) {
102 ads->auth.realm = SMB_STRDUP( our_domain->alt_name );
103 strupper_m( ads->auth.realm );
105 else
106 ads->auth.realm = SMB_STRDUP( lp_realm() );
109 ads->auth.renewable = 1;
111 status = ads_connect(ads);
112 if (!ADS_ERR_OK(status) || !ads->config.realm) {
113 extern struct winbindd_methods msrpc_methods, cache_methods;
114 DEBUG(1,("ads_connect for domain %s failed: %s\n",
115 domain->name, ads_errstr(status)));
116 ads_destroy(&ads);
118 /* if we get ECONNREFUSED then it might be a NT4
119 server, fall back to MSRPC */
120 if (status.error_type == ENUM_ADS_ERROR_SYSTEM &&
121 status.err.rc == ECONNREFUSED) {
122 DEBUG(1,("Trying MSRPC methods\n"));
123 if (domain->methods == &cache_methods) {
124 domain->backend = &msrpc_methods;
125 } else {
126 domain->methods = &msrpc_methods;
129 return NULL;
132 if (use_nss_info("sfu") && (!ads_check_sfu_mapping(ads))) {
133 DEBUG(0,("ads_cached_connection: failed to check sfu attributes\n"));
134 return NULL;
137 /* set the flag that says we don't own the memory even
138 though we do so that ads_destroy() won't destroy the
139 structure we pass back by reference */
141 ads->is_mine = False;
143 domain->private_data = (void *)ads;
144 return ads;
148 /* Query display info for a realm. This is the basic user list fn */
149 static NTSTATUS query_user_list(struct winbindd_domain *domain,
150 TALLOC_CTX *mem_ctx,
151 uint32 *num_entries,
152 WINBIND_USERINFO **info)
154 ADS_STRUCT *ads = NULL;
155 const char *attrs[] = {"userPrincipalName",
156 "sAMAccountName",
157 "name", "objectSid", "primaryGroupID",
158 "sAMAccountType",
159 ADS_ATTR_SFU_HOMEDIR_OID,
160 ADS_ATTR_SFU_SHELL_OID,
161 ADS_ATTR_SFU_GECOS_OID,
162 NULL};
163 int i, count;
164 ADS_STATUS rc;
165 void *res = NULL;
166 void *msg = NULL;
167 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
169 *num_entries = 0;
171 DEBUG(3,("ads: query_user_list\n"));
173 ads = ads_cached_connection(domain);
175 if (!ads) {
176 domain->last_status = NT_STATUS_SERVER_DISABLED;
177 goto done;
180 rc = ads_search_retry(ads, &res, "(objectClass=user)", attrs);
181 if (!ADS_ERR_OK(rc) || !res) {
182 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
183 goto done;
186 count = ads_count_replies(ads, res);
187 if (count == 0) {
188 DEBUG(1,("query_user_list: No users found\n"));
189 goto done;
192 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, count);
193 if (!*info) {
194 status = NT_STATUS_NO_MEMORY;
195 goto done;
198 i = 0;
200 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
201 char *name, *gecos = NULL;
202 char *homedir = NULL;
203 char *shell = NULL;
204 uint32 group;
205 uint32 atype;
207 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
208 ads_atype_map(atype) != SID_NAME_USER) {
209 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
210 continue;
213 name = ads_pull_username(ads, mem_ctx, msg);
215 if (use_nss_info("sfu")) {
216 homedir = ads_pull_string(ads, mem_ctx, msg,
217 ads->schema.sfu_homedir_attr);
218 shell = ads_pull_string(ads, mem_ctx, msg,
219 ads->schema.sfu_shell_attr);
220 gecos = ads_pull_string(ads, mem_ctx, msg,
221 ads->schema.sfu_gecos_attr);
224 if (gecos == NULL) {
225 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
228 if (!ads_pull_sid(ads, msg, "objectSid",
229 &(*info)[i].user_sid)) {
230 DEBUG(1,("No sid for %s !?\n", name));
231 continue;
233 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
234 DEBUG(1,("No primary group for %s !?\n", name));
235 continue;
238 (*info)[i].acct_name = name;
239 (*info)[i].full_name = gecos;
240 (*info)[i].homedir = homedir;
241 (*info)[i].shell = shell;
242 sid_compose(&(*info)[i].group_sid, &domain->sid, group);
243 i++;
246 (*num_entries) = i;
247 status = NT_STATUS_OK;
249 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
251 done:
252 if (res)
253 ads_msgfree(ads, res);
255 return status;
258 /* list all domain groups */
259 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
260 TALLOC_CTX *mem_ctx,
261 uint32 *num_entries,
262 struct acct_info **info)
264 ADS_STRUCT *ads = NULL;
265 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
266 "name", "objectSid", NULL};
267 int i, count;
268 ADS_STATUS rc;
269 void *res = NULL;
270 void *msg = NULL;
271 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
272 const char *filter;
273 BOOL enum_dom_local_groups = False;
275 *num_entries = 0;
277 DEBUG(3,("ads: enum_dom_groups\n"));
279 /* only grab domain local groups for our domain */
280 if ( domain->native_mode && strequal(lp_realm(), domain->alt_name) ) {
281 enum_dom_local_groups = True;
284 /* Workaround ADS LDAP bug present in MS W2K3 SP0 and W2K SP4 w/o
285 * rollup-fixes:
287 * According to Section 5.1(4) of RFC 2251 if a value of a type is it's
288 * default value, it MUST be absent. In case of extensible matching the
289 * "dnattr" boolean defaults to FALSE and so it must be only be present
290 * when set to TRUE.
292 * When it is set to FALSE and the OpenLDAP lib (correctly) encodes a
293 * filter using bitwise matching rule then the buggy AD fails to decode
294 * the extensible match. As a workaround set it to TRUE and thereby add
295 * the dnAttributes "dn" field to cope with those older AD versions.
296 * It should not harm and won't put any additional load on the AD since
297 * none of the dn components have a bitmask-attribute.
299 * Thanks to Ralf Haferkamp for input and testing - Guenther */
301 filter = talloc_asprintf(mem_ctx, "(&(objectCategory=group)(&(groupType:dn:%s:=%d)(!(groupType:dn:%s:=%d))))",
302 ADS_LDAP_MATCHING_RULE_BIT_AND, GROUP_TYPE_SECURITY_ENABLED,
303 ADS_LDAP_MATCHING_RULE_BIT_AND,
304 enum_dom_local_groups ? GROUP_TYPE_BUILTIN_LOCAL_GROUP : GROUP_TYPE_RESOURCE_GROUP);
306 if (filter == NULL) {
307 status = NT_STATUS_NO_MEMORY;
308 goto done;
311 ads = ads_cached_connection(domain);
313 if (!ads) {
314 domain->last_status = NT_STATUS_SERVER_DISABLED;
315 goto done;
318 rc = ads_search_retry(ads, &res, filter, attrs);
319 if (!ADS_ERR_OK(rc) || !res) {
320 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
321 goto done;
324 count = ads_count_replies(ads, res);
325 if (count == 0) {
326 DEBUG(1,("enum_dom_groups: No groups found\n"));
327 goto done;
330 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, struct acct_info, count);
331 if (!*info) {
332 status = NT_STATUS_NO_MEMORY;
333 goto done;
336 i = 0;
338 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
339 char *name, *gecos;
340 DOM_SID sid;
341 uint32 rid;
343 name = ads_pull_username(ads, mem_ctx, msg);
344 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
345 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
346 DEBUG(1,("No sid for %s !?\n", name));
347 continue;
350 if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
351 DEBUG(1,("No rid for %s !?\n", name));
352 continue;
355 fstrcpy((*info)[i].acct_name, name);
356 fstrcpy((*info)[i].acct_desc, gecos);
357 (*info)[i].rid = rid;
358 i++;
361 (*num_entries) = i;
363 status = NT_STATUS_OK;
365 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
367 done:
368 if (res)
369 ads_msgfree(ads, res);
371 return status;
374 /* list all domain local groups */
375 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
376 TALLOC_CTX *mem_ctx,
377 uint32 *num_entries,
378 struct acct_info **info)
381 * This is a stub function only as we returned the domain
382 * local groups in enum_dom_groups() if the domain->native field
383 * was true. This is a simple performance optimization when
384 * using LDAP.
386 * if we ever need to enumerate domain local groups separately,
387 * then this the optimization in enum_dom_groups() will need
388 * to be split out
390 *num_entries = 0;
392 return NT_STATUS_OK;
395 /* convert a DN to a name, SID and name type
396 this might become a major speed bottleneck if groups have
397 lots of users, in which case we could cache the results
399 static BOOL dn_lookup(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
400 const char *dn,
401 char **name, uint32 *name_type, DOM_SID *sid)
403 void *res = NULL;
404 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
405 "objectSid", "sAMAccountType", NULL};
406 ADS_STATUS rc;
407 uint32 atype;
408 DEBUG(3,("ads: dn_lookup\n"));
410 rc = ads_search_retry_dn(ads, &res, dn, attrs);
412 if (!ADS_ERR_OK(rc) || !res) {
413 goto failed;
416 (*name) = ads_pull_username(ads, mem_ctx, res);
418 if (!ads_pull_uint32(ads, res, "sAMAccountType", &atype)) {
419 goto failed;
421 (*name_type) = ads_atype_map(atype);
423 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
424 goto failed;
427 if (res)
428 ads_msgfree(ads, res);
430 return True;
432 failed:
433 if (res)
434 ads_msgfree(ads, res);
436 return False;
439 /* Lookup user information from a rid */
440 static NTSTATUS query_user(struct winbindd_domain *domain,
441 TALLOC_CTX *mem_ctx,
442 const DOM_SID *sid,
443 WINBIND_USERINFO *info)
445 ADS_STRUCT *ads = NULL;
446 const char *attrs[] = {"userPrincipalName",
447 "sAMAccountName",
448 "name",
449 "primaryGroupID",
450 ADS_ATTR_SFU_HOMEDIR_OID,
451 ADS_ATTR_SFU_SHELL_OID,
452 ADS_ATTR_SFU_GECOS_OID,
453 NULL};
454 ADS_STATUS rc;
455 int count;
456 void *msg = NULL;
457 char *ldap_exp;
458 char *sidstr;
459 uint32 group_rid;
460 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
462 DEBUG(3,("ads: query_user\n"));
464 ads = ads_cached_connection(domain);
466 if (!ads) {
467 domain->last_status = NT_STATUS_SERVER_DISABLED;
468 goto done;
471 sidstr = sid_binstring(sid);
472 asprintf(&ldap_exp, "(objectSid=%s)", sidstr);
473 rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
474 free(ldap_exp);
475 free(sidstr);
476 if (!ADS_ERR_OK(rc) || !msg) {
477 DEBUG(1,("query_user(sid=%s) ads_search: %s\n",
478 sid_string_static(sid), ads_errstr(rc)));
479 goto done;
482 count = ads_count_replies(ads, msg);
483 if (count != 1) {
484 DEBUG(1,("query_user(sid=%s): Not found\n",
485 sid_string_static(sid)));
486 goto done;
489 info->acct_name = ads_pull_username(ads, mem_ctx, msg);
491 if (use_nss_info("sfu")) {
492 info->homedir = ads_pull_string(ads, mem_ctx, msg,
493 ads->schema.sfu_homedir_attr);
494 info->shell = ads_pull_string(ads, mem_ctx, msg,
495 ads->schema.sfu_shell_attr);
496 info->full_name = ads_pull_string(ads, mem_ctx, msg,
497 ads->schema.sfu_gecos_attr);
500 if (info->full_name == NULL) {
501 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
504 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group_rid)) {
505 DEBUG(1,("No primary group for %s !?\n",
506 sid_string_static(sid)));
507 goto done;
510 sid_copy(&info->user_sid, sid);
511 sid_compose(&info->group_sid, &domain->sid, group_rid);
513 status = NT_STATUS_OK;
515 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
516 done:
517 if (msg)
518 ads_msgfree(ads, msg);
520 return status;
523 /* Lookup groups a user is a member of - alternate method, for when
524 tokenGroups are not available. */
525 static NTSTATUS lookup_usergroups_alt(struct winbindd_domain *domain,
526 TALLOC_CTX *mem_ctx,
527 const char *user_dn,
528 DOM_SID *primary_group,
529 size_t *p_num_groups, DOM_SID **user_sids)
531 ADS_STATUS rc;
532 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
533 int count;
534 void *res = NULL;
535 void *msg = NULL;
536 char *ldap_exp;
537 ADS_STRUCT *ads;
538 const char *group_attrs[] = {"objectSid", NULL};
539 char *escaped_dn;
540 size_t num_groups = 0;
542 DEBUG(3,("ads: lookup_usergroups_alt\n"));
544 ads = ads_cached_connection(domain);
546 if (!ads) {
547 domain->last_status = NT_STATUS_SERVER_DISABLED;
548 goto done;
551 if (!(escaped_dn = escape_ldap_string_alloc(user_dn))) {
552 status = NT_STATUS_NO_MEMORY;
553 goto done;
556 /* buggy server, no tokenGroups. Instead lookup what groups this user
557 is a member of by DN search on member*/
559 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(&(member=%s)(objectClass=group))", escaped_dn))) {
560 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn));
561 SAFE_FREE(escaped_dn);
562 status = NT_STATUS_NO_MEMORY;
563 goto done;
566 SAFE_FREE(escaped_dn);
568 rc = ads_search_retry(ads, &res, ldap_exp, group_attrs);
570 if (!ADS_ERR_OK(rc) || !res) {
571 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn, ads_errstr(rc)));
572 return ads_ntstatus(rc);
575 count = ads_count_replies(ads, res);
577 *user_sids = NULL;
578 num_groups = 0;
580 /* always add the primary group to the sid array */
581 add_sid_to_array(mem_ctx, primary_group, user_sids, &num_groups);
583 if (count > 0) {
584 for (msg = ads_first_entry(ads, res); msg;
585 msg = ads_next_entry(ads, msg)) {
586 DOM_SID group_sid;
588 if (!ads_pull_sid(ads, msg, "objectSid", &group_sid)) {
589 DEBUG(1,("No sid for this group ?!?\n"));
590 continue;
593 add_sid_to_array(mem_ctx, &group_sid, user_sids,
594 &num_groups);
599 *p_num_groups = num_groups;
600 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
602 DEBUG(3,("ads lookup_usergroups (alt) for dn=%s\n", user_dn));
603 done:
604 if (res)
605 ads_msgfree(ads, res);
607 return status;
610 /* Lookup groups a user is a member of. */
611 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
612 TALLOC_CTX *mem_ctx,
613 const DOM_SID *sid,
614 uint32 *p_num_groups, DOM_SID **user_sids)
616 ADS_STRUCT *ads = NULL;
617 const char *attrs[] = {"tokenGroups", "primaryGroupID", NULL};
618 ADS_STATUS rc;
619 int count;
620 LDAPMessage *msg = NULL;
621 char *user_dn;
622 DOM_SID *sids;
623 int i;
624 DOM_SID primary_group;
625 uint32 primary_group_rid;
626 fstring sid_string;
627 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
628 size_t num_groups = 0;
630 DEBUG(3,("ads: lookup_usergroups\n"));
631 *p_num_groups = 0;
633 ads = ads_cached_connection(domain);
635 if (!ads) {
636 domain->last_status = NT_STATUS_SERVER_DISABLED;
637 status = NT_STATUS_SERVER_DISABLED;
638 goto done;
641 rc = ads_sid_to_dn(ads, mem_ctx, sid, &user_dn);
642 if (!ADS_ERR_OK(rc)) {
643 status = ads_ntstatus(rc);
644 goto done;
647 rc = ads_search_retry_dn(ads, (void**)(void *)&msg, user_dn, attrs);
648 if (!ADS_ERR_OK(rc)) {
649 status = ads_ntstatus(rc);
650 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n",
651 sid_to_string(sid_string, sid), ads_errstr(rc)));
652 goto done;
655 if (!msg) {
656 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
657 sid_to_string(sid_string, sid)));
658 status = NT_STATUS_UNSUCCESSFUL;
659 goto done;
662 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group_rid)) {
663 DEBUG(1,("%s: No primary group for sid=%s !?\n",
664 domain->name, sid_to_string(sid_string, sid)));
665 goto done;
668 sid_copy(&primary_group, &domain->sid);
669 sid_append_rid(&primary_group, primary_group_rid);
671 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids);
673 if (msg)
674 ads_msgfree(ads, msg);
676 /* there must always be at least one group in the token,
677 unless we are talking to a buggy Win2k server */
678 if (count == 0) {
679 status = lookup_usergroups_alt(domain, mem_ctx, user_dn,
680 &primary_group,
681 &num_groups, user_sids);
682 *p_num_groups = (uint32)num_groups;
683 return status;
686 *user_sids = NULL;
687 num_groups = 0;
689 add_sid_to_array(mem_ctx, &primary_group, user_sids, &num_groups);
691 for (i=0;i<count;i++) {
693 /* ignore Builtin groups from ADS - Guenther */
694 if (sid_check_is_in_builtin(&sids[i])) {
695 continue;
698 add_sid_to_array_unique(mem_ctx, &sids[i],
699 user_sids, &num_groups);
702 *p_num_groups = (uint32)num_groups;
703 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
705 DEBUG(3,("ads lookup_usergroups for sid=%s\n",
706 sid_to_string(sid_string, sid)));
707 done:
708 return status;
712 find the members of a group, given a group rid and domain
714 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
715 TALLOC_CTX *mem_ctx,
716 const DOM_SID *group_sid, uint32 *num_names,
717 DOM_SID **sid_mem, char ***names,
718 uint32 **name_types)
720 ADS_STATUS rc;
721 int count;
722 void *res=NULL;
723 ADS_STRUCT *ads = NULL;
724 char *ldap_exp;
725 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
726 char *sidstr;
727 char **members;
728 int i;
729 size_t num_members;
730 fstring sid_string;
731 BOOL more_values;
732 const char **attrs;
733 uint32 first_usn;
734 uint32 current_usn;
735 int num_retries = 0;
737 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain->name,
738 sid_string_static(group_sid)));
740 *num_names = 0;
742 ads = ads_cached_connection(domain);
744 if (!ads) {
745 domain->last_status = NT_STATUS_SERVER_DISABLED;
746 goto done;
749 sidstr = sid_binstring(group_sid);
751 /* search for all members of the group */
752 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(objectSid=%s)",sidstr))) {
753 SAFE_FREE(sidstr);
754 DEBUG(1, ("ads: lookup_groupmem: tallloc_asprintf for ldap_exp failed!\n"));
755 status = NT_STATUS_NO_MEMORY;
756 goto done;
758 SAFE_FREE(sidstr);
760 members = NULL;
761 num_members = 0;
763 attrs = TALLOC_ARRAY(mem_ctx, const char *, 3);
764 attrs[1] = talloc_strdup(mem_ctx, "usnChanged");
765 attrs[2] = NULL;
767 do {
768 if (num_members == 0)
769 attrs[0] = talloc_strdup(mem_ctx, "member");
771 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs[0], attrs[1]));
773 rc = ads_search_retry(ads, &res, ldap_exp, attrs);
775 if (!ADS_ERR_OK(rc) || !res) {
776 DEBUG(1,("ads: lookup_groupmem ads_search: %s\n",
777 ads_errstr(rc)));
778 status = ads_ntstatus(rc);
779 goto done;
782 count = ads_count_replies(ads, res);
783 if (count == 0)
784 break;
786 if (num_members == 0) {
787 if (!ads_pull_uint32(ads, res, "usnChanged", &first_usn)) {
788 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
789 goto done;
793 if (!ads_pull_uint32(ads, res, "usnChanged", &current_usn)) {
794 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
795 goto done;
798 if (first_usn != current_usn) {
799 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
800 " - restarting search\n"));
801 if (num_retries < 5) {
802 num_retries++;
803 num_members = 0;
804 continue;
805 } else {
806 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
807 " - restarted search too many times, aborting!\n"));
808 status = NT_STATUS_UNSUCCESSFUL;
809 goto done;
813 members = ads_pull_strings_range(ads, mem_ctx, res,
814 "member",
815 members,
816 &attrs[0],
817 &num_members,
818 &more_values);
820 if ((members == NULL) || (num_members == 0))
821 break;
823 } while (more_values);
825 /* now we need to turn a list of members into rids, names and name types
826 the problem is that the members are in the form of distinguised names
829 (*sid_mem) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
830 (*name_types) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_members);
831 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_members);
833 for (i=0;i<num_members;i++) {
834 uint32 name_type;
835 char *name;
836 DOM_SID sid;
838 if (dn_lookup(ads, mem_ctx, members[i], &name, &name_type, &sid)) {
839 (*names)[*num_names] = name;
840 (*name_types)[*num_names] = name_type;
841 sid_copy(&(*sid_mem)[*num_names], &sid);
842 (*num_names)++;
846 status = NT_STATUS_OK;
847 DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string, group_sid)));
848 done:
850 if (res)
851 ads_msgfree(ads, res);
853 return status;
856 /* find the sequence number for a domain */
857 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
859 ADS_STRUCT *ads = NULL;
860 ADS_STATUS rc;
862 DEBUG(3,("ads: fetch sequence_number for %s\n", domain->name));
864 *seq = DOM_SEQUENCE_NONE;
866 ads = ads_cached_connection(domain);
868 if (!ads) {
869 domain->last_status = NT_STATUS_SERVER_DISABLED;
870 return NT_STATUS_UNSUCCESSFUL;
873 rc = ads_USN(ads, seq);
875 if (!ADS_ERR_OK(rc)) {
877 /* its a dead connection ; don't destroy it
878 through since ads_USN() has already done
879 that indirectly */
881 domain->private_data = NULL;
883 return ads_ntstatus(rc);
886 /* get a list of trusted domains */
887 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
888 TALLOC_CTX *mem_ctx,
889 uint32 *num_domains,
890 char ***names,
891 char ***alt_names,
892 DOM_SID **dom_sids)
894 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
895 struct ds_domain_trust *domains = NULL;
896 int count = 0;
897 int i;
898 uint32 flags = DS_DOMAIN_DIRECT_OUTBOUND;
899 struct rpc_pipe_client *cli;
901 DEBUG(3,("ads: trusted_domains\n"));
903 *num_domains = 0;
904 *alt_names = NULL;
905 *names = NULL;
906 *dom_sids = NULL;
908 result = cm_connect_netlogon(domain, &cli);
910 if (!NT_STATUS_IS_OK(result)) {
911 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
912 "for PIPE_NETLOGON (%s)\n",
913 domain->name, nt_errstr(result)));
914 return NT_STATUS_UNSUCCESSFUL;
917 if ( NT_STATUS_IS_OK(result) ) {
918 result = rpccli_ds_enum_domain_trusts(cli, mem_ctx,
919 cli->cli->desthost,
920 flags, &domains,
921 (unsigned int *)&count);
924 if ( NT_STATUS_IS_OK(result) && count) {
926 /* Allocate memory for trusted domain names and sids */
928 if ( !(*names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
929 DEBUG(0, ("trusted_domains: out of memory\n"));
930 return NT_STATUS_NO_MEMORY;
933 if ( !(*alt_names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
934 DEBUG(0, ("trusted_domains: out of memory\n"));
935 return NT_STATUS_NO_MEMORY;
938 if ( !(*dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, count)) ) {
939 DEBUG(0, ("trusted_domains: out of memory\n"));
940 return NT_STATUS_NO_MEMORY;
943 /* Copy across names and sids */
945 for (i = 0; i < count; i++) {
946 (*names)[i] = domains[i].netbios_domain;
947 (*alt_names)[i] = domains[i].dns_domain;
949 sid_copy(&(*dom_sids)[i], &domains[i].sid);
952 *num_domains = count;
955 return result;
958 /* the ADS backend methods are exposed via this structure */
959 struct winbindd_methods ads_methods = {
960 True,
961 query_user_list,
962 enum_dom_groups,
963 enum_local_groups,
964 msrpc_name_to_sid,
965 msrpc_sid_to_name,
966 query_user,
967 lookup_usergroups,
968 msrpc_lookup_useraliases,
969 lookup_groupmem,
970 sequence_number,
971 msrpc_lockout_policy,
972 msrpc_password_policy,
973 trusted_domains,
976 #endif