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