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