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