Latest patch from metze <metze@metzemix.de> to move most of samba across
[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 /***************************************************************
27  Empty static struct for negative caching.
28 ****************************************************************/
29
30 /* Fill a grent structure from various other information */
31
32 static BOOL fill_grent(struct winbindd_gr *gr, const char *dom_name, 
33                        const char *gr_name, gid_t unix_gid)
34 {
35         fstring full_group_name;
36         /* Fill in uid/gid */
37         fill_domain_username(full_group_name, dom_name, gr_name);
38
39         gr->gr_gid = unix_gid;
40     
41         /* Group name and password */
42     
43         safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
44         safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
45
46         return True;
47 }
48
49 /* Fill in the group membership field of a NT group given by group_rid */
50
51 static BOOL fill_grent_mem(struct winbindd_domain *domain,
52                            uint32 group_rid, 
53                            enum SID_NAME_USE group_name_type, 
54                            int *num_gr_mem, char **gr_mem, int *gr_mem_len)
55 {
56         uint32 *rid_mem = NULL, num_names = 0;
57         uint32 *name_types = NULL;
58         int buf_len, buf_ndx, i;
59         char **names = NULL, *buf;
60         BOOL result = False;
61         TALLOC_CTX *mem_ctx;
62         NTSTATUS status;
63
64         if (!(mem_ctx = talloc_init_named("fill_grent_mem(%s)", domain->name)))
65                 return False;
66
67         /* Initialise group membership information */
68         
69         DEBUG(10, ("group %s rid 0x%x\n", domain ? domain->name : "NULL", 
70                    group_rid));
71
72         *num_gr_mem = 0;
73         
74         if (group_name_type != SID_NAME_DOM_GRP) {
75                 DEBUG(1, ("rid %d in domain %s isn't a " "domain group\n", 
76                           group_rid, domain->name));
77                 goto done;
78         }
79
80         /* Lookup group members */
81         status = domain->methods->lookup_groupmem(domain, mem_ctx, group_rid, &num_names, 
82                                                   &rid_mem, &names, &name_types);
83         if (!NT_STATUS_IS_OK(status)) {
84                 DEBUG(1, ("could not lookup membership for group rid %d in domain %s\n", 
85                           group_rid, domain->name));
86
87                 goto done;
88         }
89
90         DEBUG(10, ("looked up %d names\n", num_names));
91
92         if (DEBUGLEVEL >= 10) {
93                 for (i = 0; i < num_names; i++)
94                         DEBUG(10, ("\t%20s %x %d\n", names[i], rid_mem[i],
95                                    name_types[i]));
96         }
97
98         /* Add members to list */
99
100         buf = NULL;
101         buf_len = buf_ndx = 0;
102
103  again:
104
105         for (i = 0; i < num_names; i++) {
106                 char *the_name;
107                 fstring name;
108                 int len;
109                         
110                 the_name = names[i];
111
112                 DEBUG(10, ("processing name %s\n", the_name));
113
114                 /* FIXME: need to cope with groups within groups.  These
115                    occur in Universal groups on a Windows 2000 native mode
116                    server. */
117
118                 if (name_types[i] != SID_NAME_USER) {
119                         DEBUG(3, ("name %s isn't a domain user\n", the_name));
120                         continue;
121                 }
122
123                 /* Don't bother with machine accounts */
124                 
125                 if (the_name[strlen(the_name) - 1] == '$') {
126                         DEBUG(10, ("%s is machine account\n", the_name));
127                         continue;
128                 }
129
130                 /* Append domain name */
131
132                 fill_domain_username(name, domain->name, 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 && buf_len != 0) {
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;
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         /* Get rid and name type from name */
216         
217         if (!winbindd_lookup_sid_by_name(domain, name_group, &group_sid, 
218                                          &name_type)) {
219                 DEBUG(1, ("group %s in domain %s does not exist\n", 
220                           name_group, name_domain));
221                 return WINBINDD_ERROR;
222         }
223
224         if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
225                 DEBUG(1, ("name '%s' is not a local or domain group: %d\n", 
226                           name_group, name_type));
227                 return WINBINDD_ERROR;
228         }
229
230         /* Fill in group structure */
231         if (!sid_peek_check_rid(&domain->sid, &group_sid, &group_rid))
232                 return WINBINDD_ERROR;
233
234         if (!winbindd_idmap_get_gid_from_sid(&group_sid, &gid)) {
235                 DEBUG(1, ("error converting unix gid to sid\n"));
236                 return WINBINDD_ERROR;
237         }
238
239         if (!fill_grent(&state->response.data.gr, name_domain,
240                         name_group, gid) ||
241             !fill_grent_mem(domain, group_rid, name_type,
242                             &state->response.data.gr.num_gr_mem,
243                             &gr_mem, &gr_mem_len)) {
244                 return WINBINDD_ERROR;
245         }
246
247         /* Group membership lives at start of extra data */
248
249         state->response.data.gr.gr_mem_ofs = 0;
250
251         state->response.length += gr_mem_len;
252         state->response.extra_data = gr_mem;
253
254         return WINBINDD_OK;
255 }
256
257 /* Return a group structure from a gid number */
258
259 enum winbindd_result winbindd_getgrgid(struct winbindd_cli_state *state)
260 {
261         struct winbindd_domain *domain;
262         DOM_SID group_sid;
263         enum SID_NAME_USE name_type;
264         fstring dom_name;
265         fstring group_name;
266         uint32 group_rid;
267         int gr_mem_len;
268         char *gr_mem;
269
270         DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid, 
271                   state->request.data.gid));
272
273         /* Bug out if the gid isn't in the winbind range */
274
275         if ((state->request.data.gid < server_state.gid_low) ||
276             (state->request.data.gid > server_state.gid_high))
277                 return WINBINDD_ERROR;
278
279         /* Get rid from gid */
280
281         if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid, 
282                                              &group_rid, &domain)) {
283                 DEBUG(1, ("could not convert gid %d to rid\n", 
284                           state->request.data.gid));
285                 return WINBINDD_ERROR;
286         }
287
288         /* Get sid from gid */
289
290         sid_copy(&group_sid, &domain->sid);
291         sid_append_rid(&group_sid, group_rid);
292
293         if (!winbindd_lookup_name_by_sid(&group_sid, dom_name, group_name, &name_type)) {
294                 DEBUG(1, ("could not lookup sid\n"));
295                 return WINBINDD_ERROR;
296         }
297
298         if (!((name_type == SID_NAME_ALIAS) || 
299               (name_type == SID_NAME_DOM_GRP))) {
300                 DEBUG(1, ("name '%s' is not a local or domain group: %d\n", 
301                           group_name, name_type));
302                 return WINBINDD_ERROR;
303         }
304
305         /* Fill in group structure */
306
307         if (!fill_grent(&state->response.data.gr, dom_name, group_name, 
308                         state->request.data.gid) ||
309             !fill_grent_mem(domain, group_rid, name_type,
310                             &state->response.data.gr.num_gr_mem,
311                             &gr_mem, &gr_mem_len))
312                 return WINBINDD_ERROR;
313
314         /* Group membership lives at start of extra data */
315
316         state->response.data.gr.gr_mem_ofs = 0;
317
318         state->response.length += gr_mem_len;
319         state->response.extra_data = gr_mem;
320
321         return WINBINDD_OK;
322 }
323
324 /*
325  * set/get/endgrent functions
326  */
327
328 /* "Rewind" file pointer for group database enumeration */
329
330 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
331 {
332         struct winbindd_domain *domain;
333
334         DEBUG(3, ("[%5d]: setgrent\n", state->pid));
335
336         /* Check user has enabled this */
337
338         if (!lp_winbind_enum_groups())
339                 return WINBINDD_ERROR;
340
341         /* Free old static data if it exists */
342         
343         if (state->getgrent_state != NULL) {
344                 free_getent_state(state->getgrent_state);
345                 state->getgrent_state = NULL;
346         }
347         
348         /* Create sam pipes for each domain we know about */
349         
350         for (domain = domain_list(); domain != NULL; domain = domain->next) {
351                 struct getent_state *domain_state;
352                 
353                 /* Skip domains other than WINBINDD_DOMAIN environment 
354                    variable */
355                 
356                 if ((strcmp(state->request.domain, "") != 0) &&
357                     !check_domain_env(state->request.domain, domain->name))
358                         continue;
359                 
360                 /* Create a state record for this domain */
361                 
362                 if ((domain_state = (struct getent_state *)
363                      malloc(sizeof(struct getent_state))) == NULL)
364                         return WINBINDD_ERROR;
365                 
366                 ZERO_STRUCTP(domain_state);
367                 
368                 fstrcpy(domain_state->domain_name, domain->name);
369
370                 /* Add to list of open domains */
371                 
372                 DLIST_ADD(state->getgrent_state, domain_state);
373         }
374         
375         return WINBINDD_OK;
376 }
377
378 /* Close file pointer to ntdom group database */
379
380 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
381 {
382         DEBUG(3, ("[%5d]: endgrent\n", state->pid));
383
384         free_getent_state(state->getgrent_state);
385         state->getgrent_state = NULL;
386         
387         return WINBINDD_OK;
388 }
389
390 /* Get the list of domain groups and domain aliases for a domain.  We fill in
391    the sam_entries and num_sam_entries fields with domain group information.  
392    The dispinfo_ndx field is incremented to the index of the next group to 
393    fetch. Return True if some groups were returned, False otherwise. */
394
395 #define MAX_FETCH_SAM_ENTRIES 100
396
397 static BOOL get_sam_group_entries(struct getent_state *ent)
398 {
399         NTSTATUS status;
400         uint32 num_entries;
401         struct acct_info *name_list = NULL;
402         TALLOC_CTX *mem_ctx;
403         BOOL result = False;
404         struct acct_info *sam_grp_entries = NULL;
405         struct winbindd_domain *domain;
406         
407         if (ent->got_sam_entries)
408                 return False;
409
410         if (!(mem_ctx = talloc_init_named("get_sam_group_entries(%s)",
411                                           ent->domain_name)))
412                 return False;
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         status = domain->methods->enum_dom_groups(domain,
430                                                   mem_ctx, 
431                                                   &num_entries,
432                                                   &sam_grp_entries);
433         
434         if (!NT_STATUS_IS_OK(status)) {
435                 result = False;
436                 goto done;
437         }
438
439         /* Copy entries into return buffer */
440
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
451         ent->sam_entries = name_list;
452         ent->sam_entry_index = 0;
453
454         result = (ent->num_sam_entries > 0);
455
456  done:
457         talloc_destroy(mem_ctx);
458
459         return result;
460 }
461
462 /* Fetch next group entry from ntdom database */
463
464 #define MAX_GETGRENT_GROUPS 500
465
466 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
467 {
468         struct getent_state *ent;
469         struct winbindd_gr *group_list = NULL;
470         int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
471         char *new_extra_data, *gr_mem_list = NULL;
472
473         DEBUG(3, ("[%5d]: getgrent\n", state->pid));
474
475         /* Check user has enabled this */
476
477         if (!lp_winbind_enum_groups())
478                 return WINBINDD_ERROR;
479
480         num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
481
482         if ((state->response.extra_data = 
483              malloc(num_groups * sizeof(struct winbindd_gr))) == NULL)
484                 return WINBINDD_ERROR;
485
486         state->response.data.num_entries = 0;
487
488         group_list = (struct winbindd_gr *)state->response.extra_data;
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                 fill_domain_username(domain_group_name, ent->domain_name, 
556                          name_list[ent->sam_entry_index].acct_name);
557
558                 result = fill_grent(&group_list[group_list_ndx], 
559                                     ent->domain_name,
560                                     name_list[ent->sam_entry_index].acct_name,
561                                     group_gid);
562
563                 /* Fill in group membership entry */
564
565                 if (result) {
566                         struct winbindd_domain *domain;
567
568                         if (!(domain = 
569                               find_domain_from_name(ent->domain_name))) {
570                                 DEBUG(3, ("No such domain %s in winbindd_getgrent\n", ent->domain_name));
571                                 result = False;
572                                 goto done;
573                         }
574
575                         /* Get group membership */
576                         
577                         result = fill_grent_mem(
578                                 domain,
579                                 name_list[ent->sam_entry_index].rid,
580                                 SID_NAME_DOM_GRP,
581                                 &group_list[group_list_ndx].num_gr_mem, 
582                                 &gr_mem, &gr_mem_len);
583                 }
584
585                 if (result) {
586                         /* Append to group membership list */
587                         new_gr_mem_list = Realloc(
588                                 gr_mem_list,
589                                 gr_mem_list_len + gr_mem_len);
590
591                         if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
592                                 DEBUG(0, ("out of memory\n"));
593                                 SAFE_FREE(gr_mem_list);
594                                 gr_mem_list_len = 0;
595                                 break;
596                         }
597
598                         DEBUG(10, ("list_len = %d, mem_len = %d\n",
599                                    gr_mem_list_len, gr_mem_len));
600
601                         gr_mem_list = new_gr_mem_list;
602
603                         memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
604                                gr_mem_len);
605
606                         SAFE_FREE(gr_mem);
607
608                         group_list[group_list_ndx].gr_mem_ofs = 
609                                 gr_mem_list_len;
610
611                         gr_mem_list_len += gr_mem_len;
612                 }
613
614                 ent->sam_entry_index++;
615                 
616                 /* Add group to return list */
617                 
618                 if (result) {
619
620                         DEBUG(10, ("adding group num_entries = %d\n",
621                                    state->response.data.num_entries));
622
623                         group_list_ndx++;
624                         state->response.data.num_entries++;
625                         
626                         state->response.length +=
627                                 sizeof(struct winbindd_gr);
628                         
629                 } else {
630                         DEBUG(0, ("could not lookup domain group %s\n", 
631                                   domain_group_name));
632                 }
633         }
634
635         /* Copy the list of group memberships to the end of the extra data */
636
637         if (group_list_ndx == 0)
638                 goto done;
639
640         new_extra_data = Realloc(
641                 state->response.extra_data,
642                 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
643
644         if (!new_extra_data) {
645                 DEBUG(0, ("out of memory\n"));
646                 group_list_ndx = 0;
647                 SAFE_FREE(state->response.extra_data);
648                 SAFE_FREE(gr_mem_list);
649
650                 return WINBINDD_ERROR;
651         }
652
653         state->response.extra_data = new_extra_data;
654
655         memcpy(&((char *)state->response.extra_data)
656                [group_list_ndx * sizeof(struct winbindd_gr)], 
657                gr_mem_list, gr_mem_list_len);
658
659         SAFE_FREE(gr_mem_list);
660
661         state->response.length += gr_mem_list_len;
662
663         DEBUG(10, ("returning %d groups, length = %d\n",
664                    group_list_ndx, gr_mem_list_len));
665
666         /* Out of domains */
667
668  done:
669
670         return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
671 }
672
673 /* List domain groups without mapping to unix ids */
674
675 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
676 {
677         uint32 total_entries = 0;
678         struct winbindd_domain *domain;
679         char *extra_data = NULL;
680         char *ted = NULL;
681         int extra_data_len = 0, i;
682
683         DEBUG(3, ("[%5d]: list groups\n", state->pid));
684
685         /* Enumerate over trusted domains */
686
687         for (domain = domain_list(); domain; domain = domain->next) {
688                 struct getent_state groups;
689
690                 ZERO_STRUCT(groups);
691
692                 /* Skip domains other than WINBINDD_DOMAIN environment
693                    variable */ 
694                 if ((strcmp(state->request.domain, "") != 0) &&
695                     !check_domain_env(state->request.domain, domain->name))
696                         continue;
697
698                 /* Get list of sam groups */
699                 ZERO_STRUCT(groups);
700                 fstrcpy(groups.domain_name, domain->name);
701
702                 get_sam_group_entries(&groups);
703                         
704                 if (groups.num_sam_entries == 0) {
705                         /* this domain is empty or in an error state */
706                         continue;
707                 }
708
709                 /* keep track the of the total number of groups seen so 
710                    far over all domains */
711                 total_entries += groups.num_sam_entries;
712                 
713                 /* Allocate some memory for extra data.  Note that we limit
714                    account names to sizeof(fstring) = 128 characters.  */               
715                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
716  
717                 if (!ted) {
718                         DEBUG(0,("failed to enlarge buffer!\n"));
719                         SAFE_FREE(extra_data);
720                         return WINBINDD_ERROR;
721                 } else
722                         extra_data = ted;
723
724                 /* Pack group list into extra data fields */
725                 for (i = 0; i < groups.num_sam_entries; i++) {
726                         char *group_name = ((struct acct_info *)
727                                             groups.sam_entries)[i].acct_name; 
728                         fstring name;
729
730                         fill_domain_username(name, domain->name, group_name);
731                         /* Append to extra data */                      
732                         memcpy(&extra_data[extra_data_len], name, 
733                                strlen(name));
734                         extra_data_len += strlen(name);
735                         extra_data[extra_data_len++] = ',';
736                 }
737
738                 free(groups.sam_entries);
739         }
740
741         /* Assign extra_data fields in response structure */
742         if (extra_data) {
743                 extra_data[extra_data_len - 1] = '\0';
744                 state->response.extra_data = extra_data;
745                 state->response.length += extra_data_len;
746         }
747
748         /* No domains may have responded but that's still OK so don't
749            return an error. */
750
751         return WINBINDD_OK;
752 }
753
754 /* Get user supplementary groups.  This is much quicker than trying to
755    invert the groups database. */
756
757 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
758 {
759         fstring name_domain, name_user;
760         DOM_SID user_sid;
761         enum SID_NAME_USE name_type;
762         uint32 user_rid, num_groups, num_gids;
763         NTSTATUS status;
764         uint32 *user_gids;
765         struct winbindd_domain *domain;
766         enum winbindd_result result = WINBINDD_ERROR;
767         gid_t *gid_list;
768         int i;
769         TALLOC_CTX *mem_ctx;
770         
771         DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
772                   state->request.data.username));
773
774         if (!(mem_ctx = talloc_init_named("winbindd_getgroups(%s)",
775                                           state->request.data.username)))
776                 return WINBINDD_ERROR;
777
778         /* Parse domain and username */
779
780         if (!parse_domain_user(state->request.data.username, name_domain, 
781                           name_user))
782                 goto done;
783
784         /* Get info for the domain */
785         
786         if ((domain = find_domain_from_name(name_domain)) == NULL) {
787                 DEBUG(0, ("could not find domain entry for domain %s\n", 
788                           name_domain));
789                 goto done;
790         }
791
792         /* Get rid and name type from name.  The following costs 1 packet */
793
794         if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, 
795                                          &name_type)) {
796                 DEBUG(1, ("user '%s' does not exist\n", name_user));
797                 goto done;
798         }
799
800         if (name_type != SID_NAME_USER) {
801                 DEBUG(1, ("name '%s' is not a user name: %d\n", 
802                           name_user, name_type));
803                 goto done;
804         }
805
806         sid_split_rid(&user_sid, &user_rid);
807
808         status = domain->methods->lookup_usergroups(domain, mem_ctx, user_rid, &num_groups, &user_gids);
809         if (!NT_STATUS_IS_OK(status)) goto done;
810
811         /* Copy data back to client */
812
813         num_gids = 0;
814         gid_list = malloc(sizeof(gid_t) * num_groups);
815
816         if (state->response.extra_data)
817                 goto done;
818
819         for (i = 0; i < num_groups; i++) {
820                 if (!winbindd_idmap_get_gid_from_rid(domain->name, 
821                                                      user_gids[i], 
822                                                      &gid_list[num_gids])) {
823
824                         DEBUG(1, ("unable to convert group rid %d to gid\n", 
825                                   user_gids[i]));
826                         continue;
827                 }
828                         
829                 num_gids++;
830         }
831
832         state->response.data.num_entries = num_gids;
833         state->response.extra_data = gid_list;
834         state->response.length += num_gids * sizeof(gid_t);
835
836         result = WINBINDD_OK;
837
838  done:
839
840         talloc_destroy(mem_ctx);
841
842         return result;
843 }