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