And finally IDMAP in 3_0
[tprouty/samba.git] / source / nsswitch / winbindd_group.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon for ntdom nss module
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jeremy Allison 2001.
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 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /***************************************************************
30  Empty static struct for negative caching.
31 ****************************************************************/
32
33 /* Fill a grent structure from various other information */
34
35 static BOOL fill_grent(struct winbindd_gr *gr, const char *dom_name, 
36                        const char *gr_name, gid_t unix_gid)
37 {
38         fstring full_group_name;
39         /* Fill in uid/gid */
40         fill_domain_username(full_group_name, dom_name, gr_name);
41
42         gr->gr_gid = unix_gid;
43     
44         /* Group name and password */
45     
46         safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
47         safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
48
49         return True;
50 }
51
52 /* Fill in the group membership field of a NT group given by group_sid */
53
54 static BOOL fill_grent_mem(struct winbindd_domain *domain,
55                            DOM_SID *group_sid, 
56                            enum SID_NAME_USE group_name_type, 
57                            int *num_gr_mem, char **gr_mem, int *gr_mem_len)
58 {
59         DOM_SID **sid_mem = NULL;
60         uint32 num_names = 0;
61         uint32 *name_types = NULL;
62         unsigned int buf_len, buf_ndx, i;
63         char **names = NULL, *buf;
64         BOOL result = False;
65         TALLOC_CTX *mem_ctx;
66         NTSTATUS status;
67         fstring sid_string;
68
69         if (!(mem_ctx = talloc_init("fill_grent_mem(%s)", domain->name)))
70                 return False;
71
72         /* Initialise group membership information */
73         
74         DEBUG(10, ("group SID %s\n", sid_to_string(sid_string, group_sid)));
75
76         *num_gr_mem = 0;
77         
78         if (group_name_type != SID_NAME_DOM_GRP) {
79                 DEBUG(1, ("SID %s in domain %s isn't a domain group\n", 
80                           sid_to_string(sid_string, group_sid), domain->name));
81                 goto done;
82         }
83
84         /* Lookup group members */
85         status = domain->methods->lookup_groupmem(domain, mem_ctx, group_sid, &num_names, 
86                                                   &sid_mem, &names, &name_types);
87         if (!NT_STATUS_IS_OK(status)) {
88                 DEBUG(1, ("could not lookup membership for group rid %s in domain %s (error: %s)\n", 
89                           sid_to_string(sid_string, group_sid), domain->name, nt_errstr(status)));
90
91                 goto done;
92         }
93
94         DEBUG(10, ("looked up %d names\n", num_names));
95
96         if (DEBUGLEVEL >= 10) {
97                 for (i = 0; i < num_names; i++)
98                         DEBUG(10, ("\t%20s %s %d\n", names[i], sid_to_string(sid_string, sid_mem[i]),
99                                    name_types[i]));
100         }
101
102         /* Add members to list */
103
104         buf = NULL;
105         buf_len = buf_ndx = 0;
106
107  again:
108
109         for (i = 0; i < num_names; i++) {
110                 char *the_name;
111                 fstring name;
112                 int len;
113                         
114                 the_name = names[i];
115
116                 DEBUG(10, ("processing name %s\n", the_name));
117
118                 /* FIXME: need to cope with groups within groups.  These
119                    occur in Universal groups on a Windows 2000 native mode
120                    server. */
121
122                 if (name_types[i] != SID_NAME_USER) {
123                         DEBUG(3, ("name %s isn't a domain user\n", the_name));
124                         continue;
125                 }
126
127                 /* Don't bother with machine accounts */
128                 
129                 if (the_name[strlen(the_name) - 1] == '$') {
130                         DEBUG(10, ("%s is machine account\n", the_name));
131                         continue;
132                 }
133
134                 /* Append domain name */
135
136                 fill_domain_username(name, domain->name, the_name);
137
138                 len = strlen(name);
139                 
140                 /* Add to list or calculate buffer length */
141
142                 if (!buf) {
143                         buf_len += len + 1; /* List is comma separated */
144                         (*num_gr_mem)++;
145                         DEBUG(10, ("buf_len + %d = %d\n", len + 1, buf_len));
146                 } else {
147                         DEBUG(10, ("appending %s at ndx %d\n", name, len));
148                         safe_strcpy(&buf[buf_ndx], name, len);
149                         buf_ndx += len;
150                         buf[buf_ndx] = ',';
151                         buf_ndx++;
152                 }
153         }
154
155         /* Allocate buffer */
156
157         if (!buf && buf_len != 0) {
158                 if (!(buf = malloc(buf_len))) {
159                         DEBUG(1, ("out of memory\n"));
160                         result = False;
161                         goto done;
162                 }
163                 memset(buf, 0, buf_len);
164                 goto again;
165         }
166
167         if (buf && buf_ndx > 0) {
168                 buf[buf_ndx - 1] = '\0';
169         }
170
171         *gr_mem = buf;
172         *gr_mem_len = buf_len;
173
174         DEBUG(10, ("num_mem = %d, len = %d, mem = %s\n", *num_gr_mem, 
175                    buf_len, *num_gr_mem ? buf : "NULL")); 
176         result = True;
177
178 done:
179
180         talloc_destroy(mem_ctx);
181         
182         DEBUG(10, ("fill_grent_mem returning %d\n", result));
183
184         return result;
185 }
186
187 /* Return a group structure from a group name */
188
189 enum winbindd_result winbindd_getgrnam(struct winbindd_cli_state *state)
190 {
191         DOM_SID group_sid;
192         struct winbindd_domain *domain;
193         enum SID_NAME_USE name_type;
194         fstring name_domain, name_group;
195         char *tmp, *gr_mem;
196         int gr_mem_len;
197         gid_t gid;
198         
199         /* Ensure null termination */
200         state->request.data.groupname[sizeof(state->request.data.groupname)-1]='\0';
201
202         DEBUG(3, ("[%5d]: getgrnam %s\n", state->pid,
203                   state->request.data.groupname));
204
205         /* Parse domain and groupname */
206         
207         memset(name_group, 0, sizeof(fstring));
208
209         tmp = state->request.data.groupname;
210         if (!parse_domain_user(tmp, name_domain, name_group))
211                 return WINBINDD_ERROR;
212
213         /* Get info for the domain */
214
215         if ((domain = find_domain_from_name(name_domain)) == NULL) {
216                 DEBUG(0, ("could not get domain sid for domain %s\n",
217                           name_domain));
218                 return WINBINDD_ERROR;
219         }
220
221         /* Get rid and name type from name */
222         
223         if (!winbindd_lookup_sid_by_name(domain, name_group, &group_sid, 
224                                          &name_type)) {
225                 DEBUG(1, ("group %s in domain %s does not exist\n", 
226                           name_group, name_domain));
227                 return WINBINDD_ERROR;
228         }
229
230         if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
231                 DEBUG(1, ("name '%s' is not a local or domain group: %d\n", 
232                           name_group, name_type));
233                 return WINBINDD_ERROR;
234         }
235
236         if (NT_STATUS_IS_ERR(sid_to_gid(&group_sid, &gid))) {
237                 DEBUG(1, ("error converting unix gid to sid\n"));
238                 return WINBINDD_ERROR;
239         }
240
241         if (!fill_grent(&state->response.data.gr, name_domain,
242                         name_group, gid) ||
243             !fill_grent_mem(domain, &group_sid, name_type,
244                             &state->response.data.gr.num_gr_mem,
245                             &gr_mem, &gr_mem_len)) {
246                 return WINBINDD_ERROR;
247         }
248
249         /* Group membership lives at start of extra data */
250
251         state->response.data.gr.gr_mem_ofs = 0;
252
253         state->response.length += gr_mem_len;
254         state->response.extra_data = gr_mem;
255
256         return WINBINDD_OK;
257 }
258
259 /* Return a group structure from a gid number */
260
261 enum winbindd_result winbindd_getgrgid(struct winbindd_cli_state *state)
262 {
263         struct winbindd_domain *domain;
264         DOM_SID group_sid;
265         enum SID_NAME_USE name_type;
266         fstring dom_name;
267         fstring group_name;
268         int gr_mem_len;
269         char *gr_mem;
270
271         DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid, 
272                   state->request.data.gid));
273
274         /* Bug out if the gid isn't in the winbind range */
275
276         if ((state->request.data.gid < server_state.gid_low) ||
277             (state->request.data.gid > server_state.gid_high))
278                 return WINBINDD_ERROR;
279
280         /* Get rid from gid */
281         if (NT_STATUS_IS_ERR(uid_to_sid(&group_sid, state->request.data.gid))) {
282                 DEBUG(1, ("could not convert gid %d to rid\n", 
283                           state->request.data.gid));
284                 return WINBINDD_ERROR;
285         }
286
287         /* Get name from sid */
288
289         if (!winbindd_lookup_name_by_sid(&group_sid, dom_name, group_name, &name_type)) {
290                 DEBUG(1, ("could not lookup sid\n"));
291                 return WINBINDD_ERROR;
292         }
293
294         if (!((name_type == SID_NAME_ALIAS) || 
295               (name_type == SID_NAME_DOM_GRP))) {
296                 DEBUG(1, ("name '%s' is not a local or domain group: %d\n", 
297                           group_name, name_type));
298                 return WINBINDD_ERROR;
299         }
300
301         /* Fill in group structure */
302
303         domain = find_domain_from_sid(&group_sid);
304
305         if (!domain) {
306                 DEBUG(1,("Can't find domain from sid\n"));
307                 return WINBINDD_ERROR;
308         }
309
310         if (!fill_grent(&state->response.data.gr, dom_name, group_name, 
311                         state->request.data.gid) ||
312             !fill_grent_mem(domain, &group_sid, name_type,
313                             &state->response.data.gr.num_gr_mem,
314                             &gr_mem, &gr_mem_len))
315                 return WINBINDD_ERROR;
316
317         /* Group membership lives at start of extra data */
318
319         state->response.data.gr.gr_mem_ofs = 0;
320
321         state->response.length += gr_mem_len;
322         state->response.extra_data = gr_mem;
323
324         return WINBINDD_OK;
325 }
326
327 /*
328  * set/get/endgrent functions
329  */
330
331 /* "Rewind" file pointer for group database enumeration */
332
333 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
334 {
335         struct winbindd_domain *domain;
336
337         DEBUG(3, ("[%5d]: setgrent\n", state->pid));
338
339         /* Check user has enabled this */
340
341         if (!lp_winbind_enum_groups())
342                 return WINBINDD_ERROR;
343
344         /* Free old static data if it exists */
345         
346         if (state->getgrent_state != NULL) {
347                 free_getent_state(state->getgrent_state);
348                 state->getgrent_state = NULL;
349         }
350         
351         /* Create sam pipes for each domain we know about */
352         
353         for (domain = domain_list(); domain != NULL; domain = domain->next) {
354                 struct getent_state *domain_state;
355                 
356                 /* Create a state record for this domain */
357                 
358                 if ((domain_state = (struct getent_state *)
359                      malloc(sizeof(struct getent_state))) == NULL) {
360                         DEBUG(1, ("winbindd_setgrent: malloc failed for domain_state!\n"));
361                         return WINBINDD_ERROR;
362                 }
363                 
364                 ZERO_STRUCTP(domain_state);
365                 
366                 fstrcpy(domain_state->domain_name, domain->name);
367
368                 /* Add to list of open domains */
369                 
370                 DLIST_ADD(state->getgrent_state, domain_state);
371         }
372         
373         return WINBINDD_OK;
374 }
375
376 /* Close file pointer to ntdom group database */
377
378 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
379 {
380         DEBUG(3, ("[%5d]: endgrent\n", state->pid));
381
382         free_getent_state(state->getgrent_state);
383         state->getgrent_state = NULL;
384         
385         return WINBINDD_OK;
386 }
387
388 /* Get the list of domain groups and domain aliases for a domain.  We fill in
389    the sam_entries and num_sam_entries fields with domain group information.  
390    The dispinfo_ndx field is incremented to the index of the next group to 
391    fetch. Return True if some groups were returned, False otherwise. */
392
393 #define MAX_FETCH_SAM_ENTRIES 100
394
395 static BOOL get_sam_group_entries(struct getent_state *ent)
396 {
397         NTSTATUS status;
398         uint32 num_entries;
399         struct acct_info *name_list = NULL, *tmp_name_list = NULL;
400         TALLOC_CTX *mem_ctx;
401         BOOL result = False;
402         struct acct_info *sam_grp_entries = NULL;
403         struct winbindd_domain *domain;
404         
405         if (ent->got_sam_entries)
406                 return False;
407
408         if (!(mem_ctx = talloc_init("get_sam_group_entries(%s)",
409                                           ent->domain_name))) {
410                 DEBUG(1, ("get_sam_group_entries: could not create talloc context!\n")); 
411                 return False;
412         }
413                 
414         /* Free any existing group info */
415
416         SAFE_FREE(ent->sam_entries);
417         ent->num_sam_entries = 0;
418         ent->got_sam_entries = True;
419
420         /* Enumerate domain groups */
421
422         num_entries = 0;
423
424         if (!(domain = find_domain_from_name(ent->domain_name))) {
425                 DEBUG(3, ("no such domain %s in get_sam_group_entries\n", ent->domain_name));
426                 goto done;
427         }
428
429         /* always get the domain global groups */
430
431         status = domain->methods->enum_dom_groups(domain, mem_ctx, &num_entries, &sam_grp_entries);
432         
433         if (!NT_STATUS_IS_OK(status)) {
434                 DEBUG(3, ("get_sam_group_entries: could not enumerate domain groups! Error: %s\n", nt_errstr(status)));
435                 result = False;
436                 goto done;
437         }
438
439         /* Copy entries into return buffer */
440
441         if (num_entries) {
442                 if ( !(name_list = malloc(sizeof(struct acct_info) * num_entries)) ) {
443                         DEBUG(0,("get_sam_group_entries: Failed to malloc memory for %d domain groups!\n", 
444                                 num_entries));
445                         result = False;
446                         goto done;
447                 }
448                 memcpy( name_list, sam_grp_entries, num_entries * sizeof(struct acct_info) );
449         }
450         
451         ent->num_sam_entries = num_entries;
452         
453         /* get the domain local groups if we are a member of 
454            a native win2k domain */
455            
456         if ( domain->native_mode && domain->methods->enum_local_groups )
457         {
458                 DEBUG(4,("get_sam_group_entries: Native Mode 2k domain; enumerating local groups as well\n"));
459                 
460                 status = domain->methods->enum_local_groups(domain, mem_ctx, &num_entries, &sam_grp_entries);
461                 
462                 if ( !NT_STATUS_IS_OK(status) ) { 
463                         DEBUG(3,("get_sam_group_entries: Failed to enumerate domain local groups!\n"));
464                         num_entries = 0;
465                 }
466                 else
467                         DEBUG(4,("get_sam_group_entries: Returned %d local groups\n", num_entries));
468                 
469                 /* Copy entries into return buffer */
470
471                 if ( num_entries ) {
472                         if ( !(tmp_name_list = Realloc( name_list, sizeof(struct acct_info) * (ent->num_sam_entries+num_entries))) )
473                         {
474                                 DEBUG(0,("get_sam_group_entries: Failed to realloc more memory for %d local groups!\n", 
475                                         num_entries));
476                                 result = False;
477                                 SAFE_FREE( name_list );
478                                 goto done;
479                         }
480                         
481                         name_list = tmp_name_list;
482                                 
483                         memcpy( &name_list[ent->num_sam_entries], sam_grp_entries, 
484                                 num_entries * sizeof(struct acct_info) );
485                 }
486         
487                 ent->num_sam_entries += num_entries;
488         }
489         
490                 
491         /* Fill in remaining fields */
492
493         ent->sam_entries = name_list;
494         ent->sam_entry_index = 0;
495
496         result = (ent->num_sam_entries > 0);
497
498  done:
499         talloc_destroy(mem_ctx);
500
501         return result;
502 }
503
504 /* Fetch next group entry from ntdom database */
505
506 #define MAX_GETGRENT_GROUPS 500
507
508 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
509 {
510         struct getent_state *ent;
511         struct winbindd_gr *group_list = NULL;
512         int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
513         char *new_extra_data, *gr_mem_list = NULL;
514
515         DEBUG(3, ("[%5d]: getgrent\n", state->pid));
516
517         /* Check user has enabled this */
518
519         if (!lp_winbind_enum_groups())
520                 return WINBINDD_ERROR;
521
522         num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
523
524         if ((state->response.extra_data = 
525              malloc(num_groups * sizeof(struct winbindd_gr))) == NULL)
526                 return WINBINDD_ERROR;
527
528         state->response.data.num_entries = 0;
529
530         group_list = (struct winbindd_gr *)state->response.extra_data;
531
532         if (!(ent = state->getgrent_state))
533                 return WINBINDD_ERROR;
534
535         /* Start sending back groups */
536
537         for (i = 0; i < num_groups; i++) {
538                 struct acct_info *name_list = NULL;
539                 fstring domain_group_name;
540                 uint32 result;
541                 gid_t group_gid;
542                 int gr_mem_len;
543                 char *gr_mem, *new_gr_mem_list;
544                 DOM_SID group_sid;
545                 struct winbindd_domain *domain;
546                                 
547                 /* Do we need to fetch another chunk of groups? */
548
549         tryagain:
550
551                 DEBUG(10, ("entry_index = %d, num_entries = %d\n",
552                            ent->sam_entry_index, ent->num_sam_entries));
553
554                 if (ent->num_sam_entries == ent->sam_entry_index) {
555
556                         while(ent && !get_sam_group_entries(ent)) {
557                                 struct getent_state *next_ent;
558
559                                 DEBUG(10, ("freeing state info for domain %s\n", ent->domain_name)); 
560
561                                 /* Free state information for this domain */
562
563                                 SAFE_FREE(ent->sam_entries);
564
565                                 next_ent = ent->next;
566                                 DLIST_REMOVE(state->getgrent_state, ent);
567                                 
568                                 SAFE_FREE(ent);
569                                 ent = next_ent;
570                         }
571
572                         /* No more domains */
573
574                         if (!ent) 
575                                 break;
576                 }
577                 
578                 name_list = ent->sam_entries;
579                 
580                 if (!(domain = 
581                       find_domain_from_name(ent->domain_name))) {
582                         DEBUG(3, ("No such domain %s in winbindd_getgrent\n", ent->domain_name));
583                         result = False;
584                         goto done;
585                 }
586
587                 /* Lookup group info */
588                 
589                 sid_copy(&group_sid, &domain->sid);
590                 sid_append_rid(&group_sid, name_list[ent->sam_entry_index].rid);
591
592                 if (NT_STATUS_IS_ERR(sid_to_gid(&group_sid, &group_gid))) {
593                         
594                         DEBUG(1, ("could not look up gid for group %s\n", 
595                                   name_list[ent->sam_entry_index].acct_name));
596                         
597                         ent->sam_entry_index++;
598                         goto tryagain;
599                 }
600
601                 DEBUG(10, ("got gid %d for group %x\n", group_gid,
602                            name_list[ent->sam_entry_index].rid));
603                 
604                 /* Fill in group entry */
605
606                 fill_domain_username(domain_group_name, ent->domain_name, 
607                          name_list[ent->sam_entry_index].acct_name);
608
609                 result = fill_grent(&group_list[group_list_ndx], 
610                                     ent->domain_name,
611                                     name_list[ent->sam_entry_index].acct_name,
612                                     group_gid);
613
614                 /* Fill in group membership entry */
615
616                 if (result) {
617                         DOM_SID member_sid;
618                         group_list[group_list_ndx].num_gr_mem = 0;
619                         gr_mem = NULL;
620                         gr_mem_len = 0;
621                         
622                         /* Get group membership */                      
623                         if (state->request.cmd == WINBINDD_GETGRLST) {
624                                 result = True;
625                         } else {
626                                 sid_copy(&member_sid, &domain->sid);
627                                 sid_append_rid(&member_sid, name_list[ent->sam_entry_index].rid);
628                                 result = fill_grent_mem(
629                                         domain,
630                                         &member_sid,
631                                         SID_NAME_DOM_GRP,
632                                         &group_list[group_list_ndx].num_gr_mem, 
633                                         &gr_mem, &gr_mem_len);
634                         }
635                 }
636
637                 if (result) {
638                         /* Append to group membership list */
639                         new_gr_mem_list = Realloc(
640                                 gr_mem_list,
641                                 gr_mem_list_len + gr_mem_len);
642
643                         if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
644                                 DEBUG(0, ("out of memory\n"));
645                                 SAFE_FREE(gr_mem_list);
646                                 gr_mem_list_len = 0;
647                                 break;
648                         }
649
650                         DEBUG(10, ("list_len = %d, mem_len = %d\n",
651                                    gr_mem_list_len, gr_mem_len));
652
653                         gr_mem_list = new_gr_mem_list;
654
655                         memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
656                                gr_mem_len);
657
658                         SAFE_FREE(gr_mem);
659
660                         group_list[group_list_ndx].gr_mem_ofs = 
661                                 gr_mem_list_len;
662
663                         gr_mem_list_len += gr_mem_len;
664                 }
665
666                 ent->sam_entry_index++;
667                 
668                 /* Add group to return list */
669                 
670                 if (result) {
671
672                         DEBUG(10, ("adding group num_entries = %d\n",
673                                    state->response.data.num_entries));
674
675                         group_list_ndx++;
676                         state->response.data.num_entries++;
677                         
678                         state->response.length +=
679                                 sizeof(struct winbindd_gr);
680                         
681                 } else {
682                         DEBUG(0, ("could not lookup domain group %s\n", 
683                                   domain_group_name));
684                 }
685         }
686
687         /* Copy the list of group memberships to the end of the extra data */
688
689         if (group_list_ndx == 0)
690                 goto done;
691
692         new_extra_data = Realloc(
693                 state->response.extra_data,
694                 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
695
696         if (!new_extra_data) {
697                 DEBUG(0, ("out of memory\n"));
698                 group_list_ndx = 0;
699                 SAFE_FREE(state->response.extra_data);
700                 SAFE_FREE(gr_mem_list);
701
702                 return WINBINDD_ERROR;
703         }
704
705         state->response.extra_data = new_extra_data;
706
707         memcpy(&((char *)state->response.extra_data)
708                [group_list_ndx * sizeof(struct winbindd_gr)], 
709                gr_mem_list, gr_mem_list_len);
710
711         SAFE_FREE(gr_mem_list);
712
713         state->response.length += gr_mem_list_len;
714
715         DEBUG(10, ("returning %d groups, length = %d\n",
716                    group_list_ndx, gr_mem_list_len));
717
718         /* Out of domains */
719
720  done:
721
722         return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
723 }
724
725 /* List domain groups without mapping to unix ids */
726
727 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
728 {
729         uint32 total_entries = 0;
730         struct winbindd_domain *domain;
731         char *extra_data = NULL;
732         char *ted = NULL;
733         unsigned int extra_data_len = 0, i;
734
735         DEBUG(3, ("[%5d]: list groups\n", state->pid));
736
737         /* Enumerate over trusted domains */
738
739         for (domain = domain_list(); domain; domain = domain->next) {
740                 struct getent_state groups;
741
742                 ZERO_STRUCT(groups);
743
744                 /* Get list of sam groups */
745                 ZERO_STRUCT(groups);
746                 fstrcpy(groups.domain_name, domain->name);
747
748                 get_sam_group_entries(&groups);
749                         
750                 if (groups.num_sam_entries == 0) {
751                         /* this domain is empty or in an error state */
752                         continue;
753                 }
754
755                 /* keep track the of the total number of groups seen so 
756                    far over all domains */
757                 total_entries += groups.num_sam_entries;
758                 
759                 /* Allocate some memory for extra data.  Note that we limit
760                    account names to sizeof(fstring) = 128 characters.  */               
761                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
762  
763                 if (!ted) {
764                         DEBUG(0,("failed to enlarge buffer!\n"));
765                         SAFE_FREE(extra_data);
766                         return WINBINDD_ERROR;
767                 } else
768                         extra_data = ted;
769
770                 /* Pack group list into extra data fields */
771                 for (i = 0; i < groups.num_sam_entries; i++) {
772                         char *group_name = ((struct acct_info *)
773                                             groups.sam_entries)[i].acct_name; 
774                         fstring name;
775
776                         fill_domain_username(name, domain->name, group_name);
777                         /* Append to extra data */                      
778                         memcpy(&extra_data[extra_data_len], name, 
779                                strlen(name));
780                         extra_data_len += strlen(name);
781                         extra_data[extra_data_len++] = ',';
782                 }
783
784                 free(groups.sam_entries);
785         }
786
787         /* Assign extra_data fields in response structure */
788         if (extra_data) {
789                 extra_data[extra_data_len - 1] = '\0';
790                 state->response.extra_data = extra_data;
791                 state->response.length += extra_data_len;
792         }
793
794         /* No domains may have responded but that's still OK so don't
795            return an error. */
796
797         return WINBINDD_OK;
798 }
799
800 /* Get user supplementary groups.  This is much quicker than trying to
801    invert the groups database. */
802
803 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
804 {
805         fstring name_domain, name_user;
806         DOM_SID user_sid;
807         enum SID_NAME_USE name_type;
808         uint32 num_groups, num_gids;
809         NTSTATUS status;
810         DOM_SID **user_gids;
811         struct winbindd_domain *domain;
812         enum winbindd_result result = WINBINDD_ERROR;
813         gid_t *gid_list;
814         unsigned int i;
815         TALLOC_CTX *mem_ctx;
816         
817         /* Ensure null termination */
818         state->request.data.username[sizeof(state->request.data.username)-1]='\0';
819
820         DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
821                   state->request.data.username));
822
823         if (!(mem_ctx = talloc_init("winbindd_getgroups(%s)",
824                                           state->request.data.username)))
825                 return WINBINDD_ERROR;
826
827         /* Parse domain and username */
828
829         if (!parse_domain_user(state->request.data.username, name_domain, 
830                           name_user))
831                 goto done;
832
833         /* Get info for the domain */
834         
835         if ((domain = find_domain_from_name(name_domain)) == NULL) {
836                 DEBUG(0, ("could not find domain entry for domain %s\n", 
837                           name_domain));
838                 goto done;
839         }
840
841         /* Get rid and name type from name.  The following costs 1 packet */
842
843         if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, 
844                                          &name_type)) {
845                 DEBUG(1, ("user '%s' does not exist\n", name_user));
846                 goto done;
847         }
848
849         if (name_type != SID_NAME_USER) {
850                 DEBUG(1, ("name '%s' is not a user name: %d\n", 
851                           name_user, name_type));
852                 goto done;
853         }
854
855         status = domain->methods->lookup_usergroups(domain, mem_ctx, 
856                                                     &user_sid, &num_groups, 
857                                                     &user_gids);
858         if (!NT_STATUS_IS_OK(status)) goto done;
859
860         /* Copy data back to client */
861
862         num_gids = 0;
863         gid_list = malloc(sizeof(gid_t) * num_groups);
864
865         if (state->response.extra_data)
866                 goto done;
867
868         for (i = 0; i < num_groups; i++) {
869                 gid_t gid;
870                 
871                 if (NT_STATUS_IS_ERR(sid_to_gid(user_gids[i], &gid))) {
872                         fstring sid_string;
873
874                         DEBUG(1, ("unable to convert group sid %s to gid\n", 
875                                   sid_to_string(sid_string, user_gids[i])));
876                         continue;
877                 }
878                 gid_list[num_gids] = gid;
879                 num_gids++;
880         }
881
882         state->response.data.num_entries = num_gids;
883         state->response.extra_data = gid_list;
884         state->response.length += num_gids * sizeof(gid_t);
885
886         result = WINBINDD_OK;
887
888  done:
889
890         talloc_destroy(mem_ctx);
891
892         return result;
893 }