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