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