c64359a2241d37817179656e2918efc7b2baa592
[sfrench/samba-autobuild/.git] / source / nsswitch / winbindd_ads.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind ADS backend functions
5
6    Copyright (C) Andrew Tridgell 2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "winbindd.h"
25
26 #ifdef HAVE_ADS
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
30
31 /*
32   return our ads connections structure for a domain. We keep the connection
33   open to make things faster
34 */
35 static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
36 {
37         ADS_STRUCT *ads;
38         ADS_STATUS status;
39
40         if (domain->private) {
41                 return (ADS_STRUCT *)domain->private;
42         }
43
44         /* we don't want this to affect the users ccache */
45         setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
46
47         ads = ads_init(domain->alt_name, domain->name, NULL);
48         if (!ads) {
49                 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
50                 return NULL;
51         }
52
53         /* the machine acct password might have change - fetch it every time */
54         SAFE_FREE(ads->auth.password);
55         ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
56
57         SAFE_FREE(ads->auth.realm);
58         ads->auth.realm = strdup(lp_realm());
59
60         status = ads_connect(ads);
61         if (!ADS_ERR_OK(status) || !ads->config.realm) {
62                 extern struct winbindd_methods msrpc_methods, cache_methods;
63                 DEBUG(1,("ads_connect for domain %s failed: %s\n", 
64                          domain->name, ads_errstr(status)));
65                 ads_destroy(&ads);
66
67                 /* if we get ECONNREFUSED then it might be a NT4
68                    server, fall back to MSRPC */
69                 if (status.error_type == ADS_ERROR_SYSTEM &&
70                     status.err.rc == ECONNREFUSED) {
71                         DEBUG(1,("Trying MSRPC methods\n"));
72                         if (domain->methods == &cache_methods) {
73                                 domain->backend = &msrpc_methods;
74                         } else {
75                                 domain->methods = &msrpc_methods;
76                         }
77                 }
78                 return NULL;
79         }
80
81         domain->private = (void *)ads;
82         return ads;
83 }
84
85
86 /* Query display info for a realm. This is the basic user list fn */
87 static NTSTATUS query_user_list(struct winbindd_domain *domain,
88                                TALLOC_CTX *mem_ctx,
89                                uint32 *num_entries, 
90                                WINBIND_USERINFO **info)
91 {
92         ADS_STRUCT *ads = NULL;
93         const char *attrs[] = {"userPrincipalName",
94                                "sAMAccountName",
95                                "name", "objectSid", "primaryGroupID", 
96                                "sAMAccountType", NULL};
97         int i, count;
98         ADS_STATUS rc;
99         void *res = NULL;
100         void *msg = NULL;
101         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
102
103         *num_entries = 0;
104
105         DEBUG(3,("ads: query_user_list\n"));
106
107         ads = ads_cached_connection(domain);
108         
109         if (!ads) {
110                 domain->last_status = NT_STATUS_SERVER_DISABLED;
111                 goto done;
112         }
113
114         rc = ads_search_retry(ads, &res, "(objectCategory=user)", attrs);
115         if (!ADS_ERR_OK(rc) || !res) {
116                 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
117                 goto done;
118         }
119
120         count = ads_count_replies(ads, res);
121         if (count == 0) {
122                 DEBUG(1,("query_user_list: No users found\n"));
123                 goto done;
124         }
125
126         (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
127         if (!*info) {
128                 status = NT_STATUS_NO_MEMORY;
129                 goto done;
130         }
131
132         i = 0;
133
134         for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
135                 char *name, *gecos;
136                 DOM_SID sid;
137                 DOM_SID *sid2;
138                 DOM_SID *group_sid;
139                 uint32 group;
140                 uint32 atype;
141
142                 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
143                     ads_atype_map(atype) != SID_NAME_USER) {
144                         DEBUG(1,("Not a user account? atype=0x%x\n", atype));
145                         continue;
146                 }
147
148                 name = ads_pull_username(ads, mem_ctx, msg);
149                 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
150                 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
151                         DEBUG(1,("No sid for %s !?\n", name));
152                         continue;
153                 }
154                 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
155                         DEBUG(1,("No primary group for %s !?\n", name));
156                         continue;
157                 }
158
159                 sid2 = talloc(mem_ctx, sizeof(*sid2));
160                 if (!sid2) {
161                         status = NT_STATUS_NO_MEMORY;
162                         goto done;
163                 }
164
165                 sid_copy(sid2, &sid);
166
167                 group_sid = rid_to_talloced_sid(domain, mem_ctx, group);
168
169                 (*info)[i].acct_name = name;
170                 (*info)[i].full_name = gecos;
171                 (*info)[i].user_sid = sid2;
172                 (*info)[i].group_sid = group_sid;
173                 i++;
174         }
175
176         (*num_entries) = i;
177         status = NT_STATUS_OK;
178
179         DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
180
181 done:
182         if (res) 
183                 ads_msgfree(ads, res);
184
185         return status;
186 }
187
188 /* list all domain groups */
189 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
190                                 TALLOC_CTX *mem_ctx,
191                                 uint32 *num_entries, 
192                                 struct acct_info **info)
193 {
194         ADS_STRUCT *ads = NULL;
195         const char *attrs[] = {"userPrincipalName", "sAMAccountName",
196                                "name", "objectSid", 
197                                "sAMAccountType", NULL};
198         int i, count;
199         ADS_STATUS rc;
200         void *res = NULL;
201         void *msg = NULL;
202         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
203         uint32 group_flags;
204
205         *num_entries = 0;
206
207         DEBUG(3,("ads: enum_dom_groups\n"));
208
209         ads = ads_cached_connection(domain);
210
211         if (!ads) {
212                 domain->last_status = NT_STATUS_SERVER_DISABLED;
213                 goto done;
214         }
215
216         rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
217         if (!ADS_ERR_OK(rc) || !res) {
218                 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
219                 goto done;
220         }
221
222         count = ads_count_replies(ads, res);
223         if (count == 0) {
224                 DEBUG(1,("enum_dom_groups: No groups found\n"));
225                 goto done;
226         }
227
228         (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
229         if (!*info) {
230                 status = NT_STATUS_NO_MEMORY;
231                 goto done;
232         }
233
234         i = 0;
235         
236         group_flags = ATYPE_GLOBAL_GROUP;
237
238         /* only grab domain local groups for our domain */
239         if ( domain->native_mode && strequal(lp_realm(), domain->alt_name)  )
240                 group_flags |= ATYPE_LOCAL_GROUP;
241
242         for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
243                 char *name, *gecos;
244                 DOM_SID sid;
245                 uint32 rid;
246                 uint32 account_type;
247
248                 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &account_type) || !(account_type & group_flags) ) 
249                         continue; 
250                         
251                 name = ads_pull_username(ads, mem_ctx, msg);
252                 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
253                 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
254                         DEBUG(1,("No sid for %s !?\n", name));
255                         continue;
256                 }
257
258                 if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
259                         DEBUG(1,("No rid for %s !?\n", name));
260                         continue;
261                 }
262
263                 fstrcpy((*info)[i].acct_name, name);
264                 fstrcpy((*info)[i].acct_desc, gecos);
265                 (*info)[i].rid = rid;
266                 i++;
267         }
268
269         (*num_entries) = i;
270
271         status = NT_STATUS_OK;
272
273         DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
274
275 done:
276         if (res) 
277                 ads_msgfree(ads, res);
278
279         return status;
280 }
281
282 /* list all domain local groups */
283 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
284                                 TALLOC_CTX *mem_ctx,
285                                 uint32 *num_entries, 
286                                 struct acct_info **info)
287 {
288         /*
289          * This is a stub function only as we returned the domain 
290          * local groups in enum_dom_groups() if the domain->native field
291          * was true.  This is a simple performance optimization when
292          * using LDAP.
293          *
294          * if we ever need to enumerate domain local groups separately, 
295          * then this the optimization in enum_dom_groups() will need 
296          * to be split out
297          */
298         *num_entries = 0;
299         
300         return NT_STATUS_OK;
301 }
302
303 /* convert a single name to a sid in a domain */
304 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
305                             TALLOC_CTX *mem_ctx,
306                             const char *name,
307                             DOM_SID *sid,
308                             enum SID_NAME_USE *type)
309 {
310         ADS_STRUCT *ads;
311
312         DEBUG(3,("ads: name_to_sid\n"));
313
314         ads = ads_cached_connection(domain);
315         
316         if (!ads) {
317                 domain->last_status = NT_STATUS_SERVER_DISABLED;
318                 return NT_STATUS_UNSUCCESSFUL;
319         }
320
321         return ads_name_to_sid(ads, name, sid, type);
322 }
323
324 /* convert a sid to a user or group name */
325 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
326                             TALLOC_CTX *mem_ctx,
327                             DOM_SID *sid,
328                             char **name,
329                             enum SID_NAME_USE *type)
330 {
331         ADS_STRUCT *ads = NULL;
332         DEBUG(3,("ads: sid_to_name\n"));
333
334         ads = ads_cached_connection(domain);
335         
336         if (!ads) {
337                 domain->last_status = NT_STATUS_SERVER_DISABLED;
338                 return NT_STATUS_UNSUCCESSFUL;
339         }
340
341         return ads_sid_to_name(ads, mem_ctx, sid, name, type);
342 }
343
344
345 /* convert a DN to a name, SID and name type 
346    this might become a major speed bottleneck if groups have
347    lots of users, in which case we could cache the results
348 */
349 static BOOL dn_lookup(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
350                       const char *dn,
351                       char **name, uint32 *name_type, DOM_SID *sid)
352 {
353         char *ldap_exp;
354         void *res = NULL;
355         const char *attrs[] = {"userPrincipalName", "sAMAccountName",
356                                "objectSid", "sAMAccountType", NULL};
357         ADS_STATUS rc;
358         uint32 atype;
359         char *escaped_dn = escape_ldap_string_alloc(dn);
360
361         DEBUG(3,("ads: dn_lookup\n"));
362
363         if (!escaped_dn) {
364                 return False;
365         }
366
367         asprintf(&ldap_exp, "(distinguishedName=%s)", dn);
368         rc = ads_search_retry(ads, &res, ldap_exp, attrs);
369         SAFE_FREE(ldap_exp);
370         SAFE_FREE(escaped_dn);
371
372         if (!ADS_ERR_OK(rc) || !res) {
373                 goto failed;
374         }
375
376         (*name) = ads_pull_username(ads, mem_ctx, res);
377
378         if (!ads_pull_uint32(ads, res, "sAMAccountType", &atype)) {
379                 goto failed;
380         }
381         (*name_type) = ads_atype_map(atype);
382
383         if (!ads_pull_sid(ads, res, "objectSid", sid)) {
384                 goto failed;
385         }
386
387         if (res) 
388                 ads_msgfree(ads, res);
389
390         return True;
391
392 failed:
393         if (res) 
394                 ads_msgfree(ads, res);
395
396         return False;
397 }
398
399 /* Lookup user information from a rid */
400 static NTSTATUS query_user(struct winbindd_domain *domain, 
401                            TALLOC_CTX *mem_ctx, 
402                            DOM_SID *sid, 
403                            WINBIND_USERINFO *info)
404 {
405         ADS_STRUCT *ads = NULL;
406         const char *attrs[] = {"userPrincipalName", 
407                                "sAMAccountName",
408                                "name", 
409                                "primaryGroupID", NULL};
410         ADS_STATUS rc;
411         int count;
412         void *msg = NULL;
413         char *ldap_exp;
414         char *sidstr;
415         uint32 group_rid;
416         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
417         DOM_SID *sid2;
418         fstring sid_string;
419
420         DEBUG(3,("ads: query_user\n"));
421
422         ads = ads_cached_connection(domain);
423         
424         if (!ads) {
425                 domain->last_status = NT_STATUS_SERVER_DISABLED;
426                 goto done;
427         }
428
429         sidstr = sid_binstring(sid);
430         asprintf(&ldap_exp, "(objectSid=%s)", sidstr);
431         rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
432         free(ldap_exp);
433         free(sidstr);
434         if (!ADS_ERR_OK(rc) || !msg) {
435                 DEBUG(1,("query_user(sid=%s) ads_search: %s\n", sid_to_string(sid_string, sid), ads_errstr(rc)));
436                 goto done;
437         }
438
439         count = ads_count_replies(ads, msg);
440         if (count != 1) {
441                 DEBUG(1,("query_user(sid=%s): Not found\n", sid_to_string(sid_string, sid)));
442                 goto done;
443         }
444
445         info->acct_name = ads_pull_username(ads, mem_ctx, msg);
446         info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
447
448         if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group_rid)) {
449                 DEBUG(1,("No primary group for %s !?\n", sid_to_string(sid_string, sid)));
450                 goto done;
451         }
452         
453         sid2 = talloc(mem_ctx, sizeof(*sid2));
454         if (!sid2) {
455                 status = NT_STATUS_NO_MEMORY;
456                 goto done;
457         }
458         sid_copy(sid2, sid);
459         
460         info->user_sid = sid2;
461
462         info->group_sid = rid_to_talloced_sid(domain, mem_ctx, group_rid);
463
464         status = NT_STATUS_OK;
465
466         DEBUG(3,("ads query_user gave %s\n", info->acct_name));
467 done:
468         if (msg) 
469                 ads_msgfree(ads, msg);
470
471         return status;
472 }
473
474 /* Lookup groups a user is a member of - alternate method, for when
475    tokenGroups are not available. */
476 static NTSTATUS lookup_usergroups_alt(struct winbindd_domain *domain,
477                                       TALLOC_CTX *mem_ctx,
478                                       const char *user_dn, 
479                                       DOM_SID *primary_group,
480                                       uint32 *num_groups, DOM_SID ***user_gids)
481 {
482         ADS_STATUS rc;
483         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
484         int count;
485         void *res = NULL;
486         void *msg = NULL;
487         char *ldap_exp;
488         ADS_STRUCT *ads;
489         const char *group_attrs[] = {"objectSid", NULL};
490
491         DEBUG(3,("ads: lookup_usergroups_alt\n"));
492
493         ads = ads_cached_connection(domain);
494
495         if (!ads) {
496                 domain->last_status = NT_STATUS_SERVER_DISABLED;
497                 goto done;
498         }
499
500         /* buggy server, no tokenGroups.  Instead lookup what groups this user
501            is a member of by DN search on member*/
502         if (asprintf(&ldap_exp, "(&(member=%s)(objectClass=group))", user_dn) == -1) {
503                 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn));
504                 return NT_STATUS_NO_MEMORY;
505         }
506         
507         rc = ads_search_retry(ads, &res, ldap_exp, group_attrs);
508         free(ldap_exp);
509         
510         if (!ADS_ERR_OK(rc) || !res) {
511                 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn, ads_errstr(rc)));
512                 return ads_ntstatus(rc);
513         }
514         
515         count = ads_count_replies(ads, res);
516         if (count == 0) {
517                 DEBUG(5,("lookup_usergroups: No supp groups found\n"));
518                 
519                 status = ads_ntstatus(rc);
520                 goto done;
521         }
522         
523         (*user_gids) = talloc_zero(mem_ctx, sizeof(**user_gids) * (count + 1));
524         (*user_gids)[0] = primary_group;
525         
526         *num_groups = 1;
527         
528         for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
529                 DOM_SID group_sid;
530                 
531                 if (!ads_pull_sid(ads, msg, "objectSid", &group_sid)) {
532                         DEBUG(1,("No sid for this group ?!?\n"));
533                         continue;
534                 }
535                 
536                 if (sid_equal(&group_sid, primary_group)) continue;
537                 
538                 (*user_gids)[*num_groups] = talloc(mem_ctx, sizeof(***user_gids));
539                 if (!(*user_gids)[*num_groups]) {
540                         status = NT_STATUS_NO_MEMORY;
541                         goto done;
542                 }
543
544                 sid_copy((*user_gids)[*num_groups], &group_sid);
545
546                 (*num_groups)++;
547                         
548         }
549
550         status = NT_STATUS_OK;
551
552         DEBUG(3,("ads lookup_usergroups (alt) for dn=%s\n", user_dn));
553 done:
554         if (res) 
555                 ads_msgfree(ads, res);
556         if (msg) 
557                 ads_msgfree(ads, msg);
558
559         return status;
560 }
561
562 /* Lookup groups a user is a member of. */
563 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
564                                   TALLOC_CTX *mem_ctx,
565                                   DOM_SID *sid, 
566                                   uint32 *num_groups, DOM_SID ***user_gids)
567 {
568         ADS_STRUCT *ads = NULL;
569         const char *attrs[] = {"distinguishedName", NULL};
570         const char *attrs2[] = {"tokenGroups", "primaryGroupID", NULL};
571         ADS_STATUS rc;
572         int count;
573         void *msg = NULL;
574         char *ldap_exp;
575         char *user_dn;
576         DOM_SID *sids;
577         int i;
578         DOM_SID *primary_group;
579         uint32 primary_group_rid;
580         char *sidstr;
581         fstring sid_string;
582         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
583
584         DEBUG(3,("ads: lookup_usergroups\n"));
585         *num_groups = 0;
586
587         ads = ads_cached_connection(domain);
588         
589         if (!ads) {
590                 domain->last_status = NT_STATUS_SERVER_DISABLED;
591                 goto done;
592         }
593
594         if (!(sidstr = sid_binstring(sid))) {
595                 DEBUG(1,("lookup_usergroups(sid=%s) sid_binstring returned NULL\n", sid_to_string(sid_string, sid)));
596                 status = NT_STATUS_NO_MEMORY;
597                 goto done;
598         }
599         if (asprintf(&ldap_exp, "(objectSid=%s)", sidstr) == -1) {
600                 free(sidstr);
601                 DEBUG(1,("lookup_usergroups(sid=%s) asprintf failed!\n", sid_to_string(sid_string, sid)));
602                 status = NT_STATUS_NO_MEMORY;
603                 goto done;
604         }
605
606         rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
607         free(ldap_exp);
608         free(sidstr);
609
610         if (!ADS_ERR_OK(rc) || !msg) {
611                 DEBUG(1,("lookup_usergroups(sid=%s) ads_search: %s\n", sid_to_string(sid_string, sid), ads_errstr(rc)));
612                 goto done;
613         }
614
615         user_dn = ads_pull_string(ads, mem_ctx, msg, "distinguishedName");
616         if (!user_dn) {
617                 DEBUG(1,("lookup_usergroups(sid=%s) ads_search did not return a a distinguishedName!\n", sid_to_string(sid_string, sid)));
618                 if (msg) 
619                         ads_msgfree(ads, msg);
620                 goto done;
621         }
622
623         if (msg) 
624                 ads_msgfree(ads, msg);
625
626         rc = ads_search_retry_dn(ads, &msg, user_dn, attrs2);
627         if (!ADS_ERR_OK(rc) || !msg) {
628                 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n", sid_to_string(sid_string, sid), ads_errstr(rc)));
629                 goto done;
630         }
631
632         if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group_rid)) {
633                 DEBUG(1,("%s: No primary group for sid=%s !?\n", domain->name, sid_to_string(sid_string, sid)));
634                 goto done;
635         }
636
637         primary_group = rid_to_talloced_sid(domain, mem_ctx, primary_group_rid);
638
639         count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids);
640
641         if (msg) 
642                 ads_msgfree(ads, msg);
643
644         /* there must always be at least one group in the token, 
645            unless we are talking to a buggy Win2k server */
646         if (count == 0) {
647                 return lookup_usergroups_alt(domain, mem_ctx, user_dn, 
648                                              primary_group,
649                                              num_groups, user_gids);
650         }
651
652         (*user_gids) = talloc_zero(mem_ctx, sizeof(**user_gids) * (count + 1));
653         (*user_gids)[0] = primary_group;
654         
655         *num_groups = 1;
656         
657         for (i=0;i<count;i++) {
658                 if (sid_equal(&sids[i], primary_group)) continue;
659                 
660                 (*user_gids)[*num_groups] = talloc(mem_ctx, sizeof(***user_gids));
661                 if (!(*user_gids)[*num_groups]) {
662                         status = NT_STATUS_NO_MEMORY;
663                         goto done;
664                 }
665
666                 sid_copy((*user_gids)[*num_groups], &sids[i]);
667                 (*num_groups)++;
668         }
669
670         status = NT_STATUS_OK;
671         DEBUG(3,("ads lookup_usergroups for sid=%s\n", sid_to_string(sid_string, sid)));
672 done:
673         return status;
674 }
675
676 /*
677   find the members of a group, given a group rid and domain
678  */
679 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
680                                 TALLOC_CTX *mem_ctx,
681                                 DOM_SID *group_sid, uint32 *num_names, 
682                                 DOM_SID ***sid_mem, char ***names, 
683                                 uint32 **name_types)
684 {
685         ADS_STATUS rc;
686         int count;
687         void *res=NULL;
688         ADS_STRUCT *ads = NULL;
689         char *ldap_exp;
690         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
691         char *sidstr;
692         const char *attrs[] = {"member", NULL};
693         char **members;
694         int i, num_members;
695         fstring sid_string;
696
697         DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain->name, sid_string_static(group_sid)));
698
699         *num_names = 0;
700
701         ads = ads_cached_connection(domain);
702         
703         if (!ads) {
704                 domain->last_status = NT_STATUS_SERVER_DISABLED;
705                 goto done;
706         }
707
708         sidstr = sid_binstring(group_sid);
709
710         /* search for all members of the group */
711         asprintf(&ldap_exp, "(objectSid=%s)",sidstr);
712         rc = ads_search_retry(ads, &res, ldap_exp, attrs);
713         free(ldap_exp);
714         free(sidstr);
715
716         if (!ADS_ERR_OK(rc) || !res) {
717                 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
718                 goto done;
719         }
720
721         count = ads_count_replies(ads, res);
722         if (count == 0) {
723                 status = NT_STATUS_OK;
724                 goto done;
725         }
726
727         members = ads_pull_strings(ads, mem_ctx, res, "member");
728         if (!members) {
729                 /* no members? ok ... */
730                 status = NT_STATUS_OK;
731                 goto done;
732         }
733
734         /* now we need to turn a list of members into rids, names and name types 
735            the problem is that the members are in the form of distinguised names
736         */
737         for (i=0;members[i];i++) /* noop */ ;
738         num_members = i;
739
740         (*sid_mem) = talloc_zero(mem_ctx, sizeof(**sid_mem) * num_members);
741         (*name_types) = talloc_zero(mem_ctx, sizeof(**name_types) * num_members);
742         (*names) = talloc_zero(mem_ctx, sizeof(**names) * num_members);
743
744         for (i=0;i<num_members;i++) {
745                 uint32 name_type;
746                 char *name;
747                 DOM_SID sid;
748
749                 if (dn_lookup(ads, mem_ctx, members[i], &name, &name_type, &sid)) {
750                     (*names)[*num_names] = name;
751                     (*name_types)[*num_names] = name_type;
752                     (*sid_mem)[*num_names] = talloc(mem_ctx, sizeof(***sid_mem));
753                     if (!(*sid_mem)[*num_names]) {
754                             status = NT_STATUS_NO_MEMORY;
755                             goto done;
756                     }
757                     sid_copy((*sid_mem)[*num_names], &sid);
758                     (*num_names)++;
759                 }
760         }       
761
762         status = NT_STATUS_OK;
763         DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string, group_sid)));
764 done:
765         if (res) 
766                 ads_msgfree(ads, res);
767
768         return status;
769 }
770
771
772 /* find the sequence number for a domain */
773 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
774 {
775         ADS_STRUCT *ads = NULL;
776         ADS_STATUS rc;
777
778         DEBUG(3,("ads: fetch sequence_number for %s\n", domain->name));
779
780         *seq = DOM_SEQUENCE_NONE;
781
782         ads = ads_cached_connection(domain);
783         
784         if (!ads) {
785                 domain->last_status = NT_STATUS_SERVER_DISABLED;
786                 return NT_STATUS_UNSUCCESSFUL;
787         }
788
789         rc = ads_USN(ads, seq);
790         if (!ADS_ERR_OK(rc)) {
791                 /* its a dead connection */
792                 ads_destroy(&ads);
793                 domain->private = NULL;
794         }
795         return ads_ntstatus(rc);
796 }
797
798 /* get a list of trusted domains */
799 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
800                                 TALLOC_CTX *mem_ctx,
801                                 uint32 *num_domains,
802                                 char ***names,
803                                 char ***alt_names,
804                                 DOM_SID **dom_sids)
805 {
806         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
807         DS_DOMAIN_TRUSTS        *domains = NULL;
808         int                     count = 0;
809         int                     i;
810         struct cli_state        *cli = NULL;
811                                 /* i think we only need our forest and downlevel trusted domains */
812         uint32                  flags = DS_DOMAIN_IN_FOREST | DS_DOMAIN_DIRECT_OUTBOUND;
813         char                    *contact_domain_name;
814
815         DEBUG(3,("ads: trusted_domains\n"));
816
817         *num_domains = 0;
818         *alt_names   = NULL;
819         *names       = NULL;
820         *dom_sids    = NULL;
821                 
822         contact_domain_name = *domain->alt_name ? domain->alt_name : domain->name;
823         if ( !NT_STATUS_IS_OK(result = cm_fresh_connection(contact_domain_name, PI_NETLOGON, &cli)) ) {
824                 DEBUG(5, ("trusted_domains: Could not open a connection to %s for PIPE_NETLOGON (%s)\n", 
825                           contact_domain_name, nt_errstr(result)));
826                 return NT_STATUS_UNSUCCESSFUL;
827         }
828         
829         if ( NT_STATUS_IS_OK(result) )
830                 result = cli_ds_enum_domain_trusts( cli, mem_ctx, cli->desthost, flags, &domains, (unsigned int *)&count );
831         
832         if ( NT_STATUS_IS_OK(result) && count) {
833         
834                 /* Allocate memory for trusted domain names and sids */
835
836                 if ( !(*names = (char **)talloc(mem_ctx, sizeof(char *) * count)) ) {
837                         DEBUG(0, ("trusted_domains: out of memory\n"));
838                         result = NT_STATUS_NO_MEMORY;
839                         goto done;
840                 }
841
842                 if ( !(*alt_names = (char **)talloc(mem_ctx, sizeof(char *) * count)) ) {
843                         DEBUG(0, ("trusted_domains: out of memory\n"));
844                         result = NT_STATUS_NO_MEMORY;
845                         goto done;
846                 }
847
848                 if ( !(*dom_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * count)) ) {
849                         DEBUG(0, ("trusted_domains: out of memory\n"));
850                         result = NT_STATUS_NO_MEMORY;
851                         goto done;
852                 }
853
854                 /* Copy across names and sids */
855
856                 for (i = 0; i < count; i++) {
857                         fstring tmp;
858                         fstring tmp2;
859
860                         (*names)[i] = NULL;
861                         (*alt_names)[i] = NULL;
862                         ZERO_STRUCT( (*dom_sids)[i] );
863
864                         if ( domains[i].netbios_ptr ) {
865                                 unistr2_to_ascii(tmp, &domains[i].netbios_domain, sizeof(tmp) - 1);
866                                 (*names)[i] = talloc_strdup(mem_ctx, tmp);
867                         }
868                         
869                         if ( domains[i].dns_ptr ) {
870                                 unistr2_to_ascii(tmp2, &domains[i].dns_domain, sizeof(tmp2) - 1);
871                                 (*alt_names)[i] = talloc_strdup(mem_ctx, tmp2);
872                         }
873                         
874                         /* sometimes we will get back a NULL SID from this call */
875                         
876                         if ( domains[i].sid_ptr )
877                                 sid_copy(&(*dom_sids)[i], &domains[i].sid.sid);
878                 }
879
880                 *num_domains = count;   
881         }
882
883 done:
884
885         SAFE_FREE( domains );
886         
887         /* remove connection;  This is a special case to the \NETLOGON pipe */
888         
889         if ( cli )
890                 cli_shutdown( cli );
891
892         return result;
893 }
894
895 /* find the domain sid for a domain */
896 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
897 {
898         ADS_STRUCT *ads;
899         ADS_STATUS rc;
900
901         DEBUG(3,("ads: domain_sid\n"));
902
903         ads = ads_cached_connection(domain);
904
905         if (!ads) {
906                 domain->last_status = NT_STATUS_SERVER_DISABLED;
907                 return NT_STATUS_UNSUCCESSFUL;
908         }
909
910         rc = ads_domain_sid(ads, sid);
911
912         if (!ADS_ERR_OK(rc)) {
913                 /* its a dead connection */
914                 ads_destroy(&ads);
915                 domain->private = NULL;
916         }
917
918         return ads_ntstatus(rc);
919 }
920
921
922 /* find alternate names list for the domain - for ADS this is the
923    netbios name */
924 static NTSTATUS alternate_name(struct winbindd_domain *domain)
925 {
926         ADS_STRUCT *ads;
927         ADS_STATUS rc;
928         TALLOC_CTX *ctx;
929         char *workgroup;
930
931         DEBUG(3,("ads: alternate_name\n"));
932
933         ads = ads_cached_connection(domain);
934         
935         if (!ads) {
936                 domain->last_status = NT_STATUS_SERVER_DISABLED;
937                 return NT_STATUS_UNSUCCESSFUL;
938         }
939
940         if (!(ctx = talloc_init("alternate_name"))) {
941                 return NT_STATUS_NO_MEMORY;
942         }
943
944         rc = ads_workgroup_name(ads, ctx, &workgroup);
945
946         if (ADS_ERR_OK(rc)) {
947                 fstrcpy(domain->name, workgroup);
948                 fstrcpy(domain->alt_name, ads->config.realm);
949                 strupper_m(domain->alt_name);
950                 strupper_m(domain->name);
951         }
952
953         talloc_destroy(ctx);
954
955         return ads_ntstatus(rc);        
956 }
957
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         name_to_sid,
965         sid_to_name,
966         query_user,
967         lookup_usergroups,
968         lookup_groupmem,
969         sequence_number,
970         trusted_domains,
971         domain_sid,
972         alternate_name
973 };
974
975 #endif