r10400: commit merge patch from jra
[Samba/gbeck.git] / source / nsswitch / winbindd_ads.c
blob7851fdabc36c9c2ef500cdc5c7f379bf5a06cdcb
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 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
64 ads = ads_init(domain->alt_name, domain->name, NULL);
65 if (!ads) {
66 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
67 return NULL;
70 /* the machine acct password might have change - fetch it every time */
71 SAFE_FREE(ads->auth.password);
72 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
74 SAFE_FREE(ads->auth.realm);
75 ads->auth.realm = SMB_STRDUP(lp_realm());
77 status = ads_connect(ads);
78 if (!ADS_ERR_OK(status) || !ads->config.realm) {
79 extern struct winbindd_methods msrpc_methods, cache_methods;
80 DEBUG(1,("ads_connect for domain %s failed: %s\n",
81 domain->name, ads_errstr(status)));
82 ads_destroy(&ads);
84 /* if we get ECONNREFUSED then it might be a NT4
85 server, fall back to MSRPC */
86 if (status.error_type == ENUM_ADS_ERROR_SYSTEM &&
87 status.err.rc == ECONNREFUSED) {
88 DEBUG(1,("Trying MSRPC methods\n"));
89 if (domain->methods == &cache_methods) {
90 domain->backend = &msrpc_methods;
91 } else {
92 domain->methods = &msrpc_methods;
95 return NULL;
98 if (use_nss_info("sfu") && (!ads_check_sfu_mapping(ads))) {
99 DEBUG(0,("ads_cached_connection: failed to check sfu attributes\n"));
100 return NULL;
103 /* set the flag that says we don't own the memory even
104 though we do so that ads_destroy() won't destroy the
105 structure we pass back by reference */
107 ads->is_mine = False;
109 domain->private_data = (void *)ads;
110 return ads;
114 /* Query display info for a realm. This is the basic user list fn */
115 static NTSTATUS query_user_list(struct winbindd_domain *domain,
116 TALLOC_CTX *mem_ctx,
117 uint32 *num_entries,
118 WINBIND_USERINFO **info)
120 ADS_STRUCT *ads = NULL;
121 const char *attrs[] = {"userPrincipalName",
122 "sAMAccountName",
123 "name", "objectSid", "primaryGroupID",
124 "sAMAccountType",
125 ADS_ATTR_SFU_HOMEDIR_OID,
126 ADS_ATTR_SFU_SHELL_OID,
127 NULL};
128 int i, count;
129 ADS_STATUS rc;
130 void *res = NULL;
131 void *msg = NULL;
132 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
134 *num_entries = 0;
136 DEBUG(3,("ads: query_user_list\n"));
138 ads = ads_cached_connection(domain);
140 if (!ads) {
141 domain->last_status = NT_STATUS_SERVER_DISABLED;
142 goto done;
145 rc = ads_search_retry(ads, &res, "(objectClass=user)", attrs);
146 if (!ADS_ERR_OK(rc) || !res) {
147 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
148 goto done;
151 count = ads_count_replies(ads, res);
152 if (count == 0) {
153 DEBUG(1,("query_user_list: No users found\n"));
154 goto done;
157 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, count);
158 if (!*info) {
159 status = NT_STATUS_NO_MEMORY;
160 goto done;
163 i = 0;
165 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
166 char *name, *gecos;
167 char *homedir = NULL;
168 char *shell = NULL;
169 uint32 group;
170 uint32 atype;
172 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
173 ads_atype_map(atype) != SID_NAME_USER) {
174 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
175 continue;
178 name = ads_pull_username(ads, mem_ctx, msg);
179 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
180 if (use_nss_info("sfu")) {
181 homedir = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_homedir_attr);
182 shell = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_shell_attr);
185 if (!ads_pull_sid(ads, msg, "objectSid",
186 &(*info)[i].user_sid)) {
187 DEBUG(1,("No sid for %s !?\n", name));
188 continue;
190 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
191 DEBUG(1,("No primary group for %s !?\n", name));
192 continue;
195 (*info)[i].acct_name = name;
196 (*info)[i].full_name = gecos;
197 (*info)[i].homedir = homedir;
198 (*info)[i].shell = shell;
199 sid_compose(&(*info)[i].group_sid, &domain->sid, group);
200 i++;
203 (*num_entries) = i;
204 status = NT_STATUS_OK;
206 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
208 done:
209 if (res)
210 ads_msgfree(ads, res);
212 return status;
215 /* list all domain groups */
216 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
217 TALLOC_CTX *mem_ctx,
218 uint32 *num_entries,
219 struct acct_info **info)
221 ADS_STRUCT *ads = NULL;
222 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
223 "name", "objectSid",
224 "sAMAccountType", NULL};
225 int i, count;
226 ADS_STATUS rc;
227 void *res = NULL;
228 void *msg = NULL;
229 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
230 uint32 group_flags;
232 *num_entries = 0;
234 DEBUG(3,("ads: enum_dom_groups\n"));
236 ads = ads_cached_connection(domain);
238 if (!ads) {
239 domain->last_status = NT_STATUS_SERVER_DISABLED;
240 goto done;
243 rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
244 if (!ADS_ERR_OK(rc) || !res) {
245 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
246 goto done;
249 count = ads_count_replies(ads, res);
250 if (count == 0) {
251 DEBUG(1,("enum_dom_groups: No groups found\n"));
252 goto done;
255 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, struct acct_info, count);
256 if (!*info) {
257 status = NT_STATUS_NO_MEMORY;
258 goto done;
261 i = 0;
263 group_flags = ATYPE_GLOBAL_GROUP;
265 /* only grab domain local groups for our domain */
266 if ( domain->native_mode && strequal(lp_realm(), domain->alt_name) )
267 group_flags |= ATYPE_LOCAL_GROUP;
269 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
270 char *name, *gecos;
271 DOM_SID sid;
272 uint32 rid;
273 uint32 account_type;
275 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &account_type) || !(account_type & group_flags) )
276 continue;
278 name = ads_pull_username(ads, mem_ctx, msg);
279 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
280 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
281 DEBUG(1,("No sid for %s !?\n", name));
282 continue;
285 if (sid_check_is_in_builtin(&sid))
286 continue;
288 if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
289 DEBUG(1,("No rid for %s !?\n", name));
290 continue;
293 fstrcpy((*info)[i].acct_name, name);
294 fstrcpy((*info)[i].acct_desc, gecos);
295 (*info)[i].rid = rid;
296 i++;
299 (*num_entries) = i;
301 status = NT_STATUS_OK;
303 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
305 done:
306 if (res)
307 ads_msgfree(ads, res);
309 return status;
312 /* list all domain local groups */
313 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
314 TALLOC_CTX *mem_ctx,
315 uint32 *num_entries,
316 struct acct_info **info)
319 * This is a stub function only as we returned the domain
320 * local groups in enum_dom_groups() if the domain->native field
321 * was true. This is a simple performance optimization when
322 * using LDAP.
324 * if we ever need to enumerate domain local groups separately,
325 * then this the optimization in enum_dom_groups() will need
326 * to be split out
328 *num_entries = 0;
330 return NT_STATUS_OK;
333 /* convert a DN to a name, SID and name type
334 this might become a major speed bottleneck if groups have
335 lots of users, in which case we could cache the results
337 static BOOL dn_lookup(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
338 const char *dn,
339 char **name, uint32 *name_type, DOM_SID *sid)
341 void *res = NULL;
342 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
343 "objectSid", "sAMAccountType", NULL};
344 ADS_STATUS rc;
345 uint32 atype;
346 DEBUG(3,("ads: dn_lookup\n"));
348 rc = ads_search_retry_dn(ads, &res, dn, attrs);
350 if (!ADS_ERR_OK(rc) || !res) {
351 goto failed;
354 (*name) = ads_pull_username(ads, mem_ctx, res);
356 if (!ads_pull_uint32(ads, res, "sAMAccountType", &atype)) {
357 goto failed;
359 (*name_type) = ads_atype_map(atype);
361 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
362 goto failed;
365 if (res)
366 ads_msgfree(ads, res);
368 return True;
370 failed:
371 if (res)
372 ads_msgfree(ads, res);
374 return False;
377 /* Lookup user information from a rid */
378 static NTSTATUS query_user(struct winbindd_domain *domain,
379 TALLOC_CTX *mem_ctx,
380 const DOM_SID *sid,
381 WINBIND_USERINFO *info)
383 ADS_STRUCT *ads = NULL;
384 const char *attrs[] = {"userPrincipalName",
385 "sAMAccountName",
386 "name",
387 "primaryGroupID",
388 ADS_ATTR_SFU_HOMEDIR_OID,
389 ADS_ATTR_SFU_SHELL_OID,
390 NULL};
391 ADS_STATUS rc;
392 int count;
393 void *msg = NULL;
394 char *ldap_exp;
395 char *sidstr;
396 uint32 group_rid;
397 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
399 DEBUG(3,("ads: query_user\n"));
401 ads = ads_cached_connection(domain);
403 if (!ads) {
404 domain->last_status = NT_STATUS_SERVER_DISABLED;
405 goto done;
408 sidstr = sid_binstring(sid);
409 asprintf(&ldap_exp, "(objectSid=%s)", sidstr);
410 rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
411 free(ldap_exp);
412 free(sidstr);
413 if (!ADS_ERR_OK(rc) || !msg) {
414 DEBUG(1,("query_user(sid=%s) ads_search: %s\n",
415 sid_string_static(sid), ads_errstr(rc)));
416 goto done;
419 count = ads_count_replies(ads, msg);
420 if (count != 1) {
421 DEBUG(1,("query_user(sid=%s): Not found\n",
422 sid_string_static(sid)));
423 goto done;
426 info->acct_name = ads_pull_username(ads, mem_ctx, msg);
427 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
429 if (use_nss_info("sfu")) {
430 info->homedir = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_homedir_attr);
431 info->shell = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_shell_attr);
434 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group_rid)) {
435 DEBUG(1,("No primary group for %s !?\n",
436 sid_string_static(sid)));
437 goto done;
440 sid_copy(&info->user_sid, sid);
441 sid_compose(&info->group_sid, &domain->sid, group_rid);
443 status = NT_STATUS_OK;
445 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
446 done:
447 if (msg)
448 ads_msgfree(ads, msg);
450 return status;
453 /* Lookup groups a user is a member of - alternate method, for when
454 tokenGroups are not available. */
455 static NTSTATUS lookup_usergroups_alt(struct winbindd_domain *domain,
456 TALLOC_CTX *mem_ctx,
457 const char *user_dn,
458 DOM_SID *primary_group,
459 uint32 *num_groups, DOM_SID **user_sids)
461 ADS_STATUS rc;
462 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
463 int count;
464 void *res = NULL;
465 void *msg = NULL;
466 char *ldap_exp;
467 ADS_STRUCT *ads;
468 const char *group_attrs[] = {"objectSid", NULL};
469 char *escaped_dn;
471 DEBUG(3,("ads: lookup_usergroups_alt\n"));
473 ads = ads_cached_connection(domain);
475 if (!ads) {
476 domain->last_status = NT_STATUS_SERVER_DISABLED;
477 goto done;
480 if (!(escaped_dn = escape_ldap_string_alloc(user_dn))) {
481 status = NT_STATUS_NO_MEMORY;
482 goto done;
485 /* buggy server, no tokenGroups. Instead lookup what groups this user
486 is a member of by DN search on member*/
488 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(&(member=%s)(objectClass=group))", escaped_dn))) {
489 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn));
490 SAFE_FREE(escaped_dn);
491 status = NT_STATUS_NO_MEMORY;
492 goto done;
495 SAFE_FREE(escaped_dn);
497 rc = ads_search_retry(ads, &res, ldap_exp, group_attrs);
499 if (!ADS_ERR_OK(rc) || !res) {
500 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn, ads_errstr(rc)));
501 return ads_ntstatus(rc);
504 count = ads_count_replies(ads, res);
506 *user_sids = NULL;
507 *num_groups = 0;
509 /* always add the primary group to the sid array */
510 add_sid_to_array(mem_ctx, primary_group, user_sids, num_groups);
512 if (count > 0) {
513 for (msg = ads_first_entry(ads, res); msg;
514 msg = ads_next_entry(ads, msg)) {
515 DOM_SID group_sid;
517 if (!ads_pull_sid(ads, msg, "objectSid", &group_sid)) {
518 DEBUG(1,("No sid for this group ?!?\n"));
519 continue;
522 add_sid_to_array(mem_ctx, &group_sid, user_sids,
523 num_groups);
528 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
530 DEBUG(3,("ads lookup_usergroups (alt) for dn=%s\n", user_dn));
531 done:
532 if (res)
533 ads_msgfree(ads, res);
535 return status;
538 /* Lookup groups a user is a member of. */
539 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
540 TALLOC_CTX *mem_ctx,
541 const DOM_SID *sid,
542 uint32 *num_groups, DOM_SID **user_sids)
544 ADS_STRUCT *ads = NULL;
545 const char *attrs[] = {"tokenGroups", "primaryGroupID", NULL};
546 ADS_STATUS rc;
547 int count;
548 LDAPMessage *msg = NULL;
549 char *user_dn;
550 DOM_SID *sids;
551 int i;
552 DOM_SID primary_group;
553 uint32 primary_group_rid;
554 fstring sid_string;
555 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
557 DEBUG(3,("ads: lookup_usergroups\n"));
558 *num_groups = 0;
560 ads = ads_cached_connection(domain);
562 if (!ads) {
563 domain->last_status = NT_STATUS_SERVER_DISABLED;
564 status = NT_STATUS_SERVER_DISABLED;
565 goto done;
568 rc = ads_sid_to_dn(ads, mem_ctx, sid, &user_dn);
569 if (!ADS_ERR_OK(rc)) {
570 status = ads_ntstatus(rc);
571 goto done;
574 rc = ads_search_retry_dn(ads, (void**)&msg, user_dn, attrs);
575 if (!ADS_ERR_OK(rc)) {
576 status = ads_ntstatus(rc);
577 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n",
578 sid_to_string(sid_string, sid), ads_errstr(rc)));
579 goto done;
582 if (!msg) {
583 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
584 sid_to_string(sid_string, sid)));
585 status = NT_STATUS_UNSUCCESSFUL;
586 goto done;
589 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group_rid)) {
590 DEBUG(1,("%s: No primary group for sid=%s !?\n",
591 domain->name, sid_to_string(sid_string, sid)));
592 goto done;
595 sid_copy(&primary_group, &domain->sid);
596 sid_append_rid(&primary_group, primary_group_rid);
598 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids);
600 if (msg)
601 ads_msgfree(ads, msg);
603 /* there must always be at least one group in the token,
604 unless we are talking to a buggy Win2k server */
605 if (count == 0) {
606 return lookup_usergroups_alt(domain, mem_ctx, user_dn,
607 &primary_group,
608 num_groups, user_sids);
611 *user_sids = NULL;
612 *num_groups = 0;
614 add_sid_to_array(mem_ctx, &primary_group, user_sids, num_groups);
616 for (i=0;i<count;i++)
617 add_sid_to_array_unique(mem_ctx, &sids[i],
618 user_sids, num_groups);
620 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
622 DEBUG(3,("ads lookup_usergroups for sid=%s\n",
623 sid_to_string(sid_string, sid)));
624 done:
625 return status;
629 find the members of a group, given a group rid and domain
631 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
632 TALLOC_CTX *mem_ctx,
633 const DOM_SID *group_sid, uint32 *num_names,
634 DOM_SID **sid_mem, char ***names,
635 uint32 **name_types)
637 ADS_STATUS rc;
638 int count;
639 void *res=NULL;
640 ADS_STRUCT *ads = NULL;
641 char *ldap_exp;
642 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
643 char *sidstr;
644 char **members;
645 int i;
646 size_t num_members;
647 fstring sid_string;
648 BOOL more_values;
649 const char **attrs;
650 uint32 first_usn;
651 uint32 current_usn;
652 int num_retries = 0;
654 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain->name,
655 sid_string_static(group_sid)));
657 *num_names = 0;
659 ads = ads_cached_connection(domain);
661 if (!ads) {
662 domain->last_status = NT_STATUS_SERVER_DISABLED;
663 goto done;
666 sidstr = sid_binstring(group_sid);
668 /* search for all members of the group */
669 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(objectSid=%s)",sidstr))) {
670 SAFE_FREE(sidstr);
671 DEBUG(1, ("ads: lookup_groupmem: tallloc_asprintf for ldap_exp failed!\n"));
672 status = NT_STATUS_NO_MEMORY;
673 goto done;
675 SAFE_FREE(sidstr);
677 members = NULL;
678 num_members = 0;
680 attrs = TALLOC_ARRAY(mem_ctx, const char *, 3);
681 attrs[1] = talloc_strdup(mem_ctx, "usnChanged");
682 attrs[2] = NULL;
684 do {
685 if (num_members == 0)
686 attrs[0] = talloc_strdup(mem_ctx, "member");
688 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs[0], attrs[1]));
690 rc = ads_search_retry(ads, &res, ldap_exp, attrs);
692 if (!ADS_ERR_OK(rc) || !res) {
693 DEBUG(1,("ads: lookup_groupmem ads_search: %s\n",
694 ads_errstr(rc)));
695 status = ads_ntstatus(rc);
696 goto done;
699 count = ads_count_replies(ads, res);
700 if (count == 0)
701 break;
703 if (num_members == 0) {
704 if (!ads_pull_uint32(ads, res, "usnChanged", &first_usn)) {
705 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
706 goto done;
710 if (!ads_pull_uint32(ads, res, "usnChanged", &current_usn)) {
711 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
712 goto done;
715 if (first_usn != current_usn) {
716 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
717 " - restarting search\n"));
718 if (num_retries < 5) {
719 num_retries++;
720 num_members = 0;
721 continue;
722 } else {
723 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
724 " - restarted search too many times, aborting!\n"));
725 status = NT_STATUS_UNSUCCESSFUL;
726 goto done;
730 members = ads_pull_strings_range(ads, mem_ctx, res,
731 "member",
732 members,
733 &attrs[0],
734 &num_members,
735 &more_values);
737 if ((members == NULL) || (num_members == 0))
738 break;
740 } while (more_values);
742 /* now we need to turn a list of members into rids, names and name types
743 the problem is that the members are in the form of distinguised names
746 (*sid_mem) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
747 (*name_types) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_members);
748 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_members);
750 for (i=0;i<num_members;i++) {
751 uint32 name_type;
752 char *name;
753 DOM_SID sid;
755 if (dn_lookup(ads, mem_ctx, members[i], &name, &name_type, &sid)) {
756 (*names)[*num_names] = name;
757 (*name_types)[*num_names] = name_type;
758 sid_copy(&(*sid_mem)[*num_names], &sid);
759 (*num_names)++;
763 status = NT_STATUS_OK;
764 DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string, group_sid)));
765 done:
767 if (res)
768 ads_msgfree(ads, res);
770 return status;
773 /* find the sequence number for a domain */
774 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
776 ADS_STRUCT *ads = NULL;
777 ADS_STATUS rc;
779 DEBUG(3,("ads: fetch sequence_number for %s\n", domain->name));
781 *seq = DOM_SEQUENCE_NONE;
783 ads = ads_cached_connection(domain);
785 if (!ads) {
786 domain->last_status = NT_STATUS_SERVER_DISABLED;
787 return NT_STATUS_UNSUCCESSFUL;
790 rc = ads_USN(ads, seq);
792 if (!ADS_ERR_OK(rc)) {
794 /* its a dead connection ; don't destroy it
795 through since ads_USN() has already done
796 that indirectly */
798 domain->private_data = NULL;
800 return ads_ntstatus(rc);
803 /* get a list of trusted domains */
804 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
805 TALLOC_CTX *mem_ctx,
806 uint32 *num_domains,
807 char ***names,
808 char ***alt_names,
809 DOM_SID **dom_sids)
811 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
812 struct ds_domain_trust *domains = NULL;
813 int count = 0;
814 int i;
815 /* i think we only need our forest and downlevel trusted domains */
816 uint32 flags = DS_DOMAIN_IN_FOREST | DS_DOMAIN_DIRECT_OUTBOUND;
817 struct rpc_pipe_client *cli;
819 DEBUG(3,("ads: trusted_domains\n"));
821 *num_domains = 0;
822 *alt_names = NULL;
823 *names = NULL;
824 *dom_sids = NULL;
827 unsigned char *session_key;
828 DOM_CRED *creds;
830 result = cm_connect_netlogon(domain, mem_ctx, &cli,
831 &session_key, &creds);
834 if (!NT_STATUS_IS_OK(result)) {
835 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
836 "for PIPE_NETLOGON (%s)\n",
837 domain->name, nt_errstr(result)));
838 return NT_STATUS_UNSUCCESSFUL;
841 if ( NT_STATUS_IS_OK(result) )
842 result = rpccli_ds_enum_domain_trusts(cli, mem_ctx,
843 cli->cli->desthost,
844 flags, &domains,
845 (unsigned int *)&count);
847 if ( NT_STATUS_IS_OK(result) && count) {
849 /* Allocate memory for trusted domain names and sids */
851 if ( !(*names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
852 DEBUG(0, ("trusted_domains: out of memory\n"));
853 return NT_STATUS_NO_MEMORY;
856 if ( !(*alt_names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
857 DEBUG(0, ("trusted_domains: out of memory\n"));
858 return NT_STATUS_NO_MEMORY;
861 if ( !(*dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, count)) ) {
862 DEBUG(0, ("trusted_domains: out of memory\n"));
863 return NT_STATUS_NO_MEMORY;
866 /* Copy across names and sids */
868 for (i = 0; i < count; i++) {
869 (*names)[i] = domains[i].netbios_domain;
870 (*alt_names)[i] = domains[i].dns_domain;
872 sid_copy(&(*dom_sids)[i], &domains[i].sid);
875 *num_domains = count;
878 return result;
881 /* find alternate names list for the domain - for ADS this is the
882 netbios name */
883 static NTSTATUS alternate_name(struct winbindd_domain *domain)
885 ADS_STRUCT *ads;
886 ADS_STATUS rc;
887 TALLOC_CTX *ctx;
888 const char *workgroup;
890 DEBUG(3,("ads: alternate_name\n"));
892 ads = ads_cached_connection(domain);
894 if (!ads) {
895 domain->last_status = NT_STATUS_SERVER_DISABLED;
896 return NT_STATUS_UNSUCCESSFUL;
899 if (!(ctx = talloc_init("alternate_name"))) {
900 return NT_STATUS_NO_MEMORY;
903 rc = ads_workgroup_name(ads, ctx, &workgroup);
905 if (ADS_ERR_OK(rc)) {
906 fstrcpy(domain->name, workgroup);
907 fstrcpy(domain->alt_name, ads->config.realm);
908 strupper_m(domain->alt_name);
909 strupper_m(domain->name);
912 talloc_destroy(ctx);
914 return ads_ntstatus(rc);
917 /* the ADS backend methods are exposed via this structure */
918 struct winbindd_methods ads_methods = {
919 True,
920 query_user_list,
921 enum_dom_groups,
922 enum_local_groups,
923 msrpc_name_to_sid,
924 msrpc_sid_to_name,
925 query_user,
926 lookup_usergroups,
927 msrpc_lookup_useraliases,
928 lookup_groupmem,
929 sequence_number,
930 trusted_domains,
931 alternate_name
934 #endif