move to SAFE_FREE()
[ira/wip.git] / source3 / 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         
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         NTSTATUS status;
469         uint32 num_entries;
470         struct acct_info *name_list = NULL;
471         
472         if (ent->got_all_sam_entries) {
473                 return False;
474         }
475
476 #if 0
477         if (winbindd_fetch_group_cache(ent->domain->name, 
478                                        &ent->sam_entries,
479                                        &ent->num_sam_entries)) {
480                 return True;
481         }
482 #endif
483                 
484         /* Fetch group entries */
485                 
486         if (!domain_handles_open(ent->domain)) {
487                 return False;
488         }
489
490         /* Free any existing group info */
491
492         SAFE_FREE(ent->sam_entries);
493         ent->num_sam_entries = 0;
494                 
495         /* Enumerate domain groups */
496                 
497         do {
498                 struct acct_info *sam_grp_entries = NULL;
499
500                 num_entries = 0;
501
502                 status = wb_samr_enum_dom_groups(&ent->domain->sam_dom_handle,
503                                              &ent->grp_query_start_ndx,
504                                              0x8000, /* buffer size? */
505                                              (struct acct_info **)
506                                              &sam_grp_entries,
507                                              &num_entries);
508
509                 /* Copy entries into return buffer */
510
511                 if (num_entries) {
512
513                         name_list = Realloc(name_list,
514                                             sizeof(struct acct_info) *
515                                             (ent->num_sam_entries +
516                                              num_entries));
517
518                         memcpy(&name_list[ent->num_sam_entries],
519                                sam_grp_entries, 
520                                num_entries * sizeof(struct acct_info));
521
522                         SAFE_FREE(sam_grp_entries);
523                 }
524
525                 ent->num_sam_entries += num_entries;
526                 
527                 if (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES))
528                         break;
529
530         } while (ent->num_sam_entries < MAX_FETCH_SAM_ENTRIES);
531                 
532 #if 0
533         /* Fill cache with received entries */
534
535         winbindd_store_group_cache(ent->domain->name, ent->sam_entries,
536                                    ent->num_sam_entries);
537 #endif
538
539         /* Fill in remaining fields */
540
541         ent->sam_entries = name_list;
542         ent->sam_entry_index = 0;
543         ent->got_all_sam_entries = (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES));
544
545         return ent->num_sam_entries > 0;
546 }
547
548 /* Fetch next group entry from ntdom database */
549
550 #define MAX_GETGRENT_GROUPS 500
551
552 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
553 {
554         struct getent_state *ent;
555         struct winbindd_gr *group_list = NULL;
556         int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
557         char *sep, *new_extra_data, *gr_mem_list = NULL;
558
559         DEBUG(3, ("[%5d]: getgrent\n", state->pid));
560
561         if (state == NULL) return WINBINDD_ERROR;
562
563         /* Check user has enabled this */
564
565         if (!lp_winbind_enum_groups()) {
566                 return WINBINDD_ERROR;
567         }
568
569         num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
570
571         if ((state->response.extra_data = 
572              malloc(num_groups * sizeof(struct winbindd_gr))) == NULL) {
573                 return WINBINDD_ERROR;
574         }
575
576         state->response.data.num_entries = 0;
577
578         group_list = (struct winbindd_gr *)state->response.extra_data;
579         sep = lp_winbind_separator();
580
581         if (!(ent = state->getgrent_state)) {
582                 return WINBINDD_ERROR;
583         }
584
585         /* Start sending back groups */
586
587         for (i = 0; i < num_groups; i++) {
588                 struct acct_info *name_list = NULL;
589                 fstring domain_group_name;
590                 uint32 result;
591                 gid_t group_gid;
592                 
593                 /* Do we need to fetch another chunk of groups? */
594
595         tryagain:
596
597                 DEBUG(10, ("getgrent(): entry_index = %d, num_entries = %d\n",
598                            ent->sam_entry_index, ent->num_sam_entries));
599
600                 if (ent->num_sam_entries == ent->sam_entry_index) {
601
602                         while(ent && !get_sam_group_entries(ent)) {
603                                 struct getent_state *next_ent;
604
605                                 DEBUG(10, ("getgrent(): freeing state info for "
606                                            "domain %s\n", ent->domain->name)); 
607
608                                 /* Free state information for this domain */
609
610                                 SAFE_FREE(ent->sam_entries);
611                                 ent->sam_entries = NULL;
612
613                                 next_ent = ent->next;
614                                 DLIST_REMOVE(state->getgrent_state, ent);
615                                 
616                                 SAFE_FREE(ent);
617                                 ent = next_ent;
618                         }
619
620                         /* No more domains */
621
622                         if (!ent) break;
623                 }
624                 
625                 name_list = ent->sam_entries;
626                 
627                 /* Lookup group info */
628                 
629                 if (!winbindd_idmap_get_gid_from_rid(
630                         ent->domain->name,
631                         name_list[ent->sam_entry_index].rid,
632                         &group_gid)) {
633                         
634                         DEBUG(1, ("getgrent(): could not look up gid for group %s\n",
635                                   name_list[ent->sam_entry_index].acct_name));
636
637                         ent->sam_entry_index++;
638                         goto tryagain;
639                 }
640
641                 DEBUG(10, ("getgrent(): got gid %d for group %x\n", group_gid,
642                            name_list[ent->sam_entry_index].rid));
643                 
644                 /* Fill in group entry */
645
646                 slprintf(domain_group_name, sizeof(domain_group_name) - 1,
647                          "%s%s%s", ent->domain->name, lp_winbind_separator(), 
648                          name_list[ent->sam_entry_index].acct_name);
649    
650                 result = fill_grent(&group_list[group_list_ndx], 
651                                     domain_group_name, group_gid);
652
653                 /* Fill in group membership entry */
654
655                 if (result) {
656                         int gr_mem_len;
657                         char *gr_mem, *new_gr_mem_list;
658
659                         /* Get group membership */
660
661                         result = fill_grent_mem(
662                                 ent->domain,
663                                 name_list[ent->sam_entry_index].rid,
664                                 SID_NAME_DOM_GRP,
665                                 &group_list[group_list_ndx].num_gr_mem, 
666                                 &gr_mem, &gr_mem_len);
667
668                         /* Append to group membership list */
669
670                         new_gr_mem_list = Realloc(
671                                 gr_mem_list,
672                                 gr_mem_list_len + gr_mem_len);
673
674                         if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
675                                 DEBUG(0, ("getgrent(): out of memory\n"));
676                                 SAFE_FREE(gr_mem_list);
677                                 gr_mem_list_len = 0;
678                                 break;
679                         }
680
681                         DEBUG(10, ("getgrent(): list_len = %d, mem_len = %d\n",
682                                    gr_mem_list_len, gr_mem_len));
683
684                         gr_mem_list = new_gr_mem_list;
685
686                         memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
687                                gr_mem_len);
688
689                         SAFE_FREE(gr_mem);
690
691                         group_list[group_list_ndx].gr_mem_ofs = 
692                                 gr_mem_list_len;
693
694                         gr_mem_list_len += gr_mem_len;
695                 }
696
697                 ent->sam_entry_index++;
698                 
699                 /* Add group to return list */
700                 
701                 if (result) {
702
703                         DEBUG(10, ("getgrent(): adding group num_entries = %d\n",
704                                    state->response.data.num_entries));
705
706                         group_list_ndx++;
707                         state->response.data.num_entries++;
708                         
709                         state->response.length +=
710                                 sizeof(struct winbindd_gr);
711                         
712                 } else {
713                         DEBUG(0, ("could not lookup domain group %s\n", 
714                                   domain_group_name));
715                 }
716         }
717
718         /* Copy the list of group memberships to the end of the extra data */
719
720         if (group_list_ndx == 0) {
721                 goto done;
722         }
723
724         new_extra_data = Realloc(
725                 state->response.extra_data,
726                 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
727
728         if (!new_extra_data) {
729                 DEBUG(0, ("out of memory\n"));
730                 group_list_ndx = 0;
731                 SAFE_FREE(state->response.extra_data);
732                 SAFE_FREE(gr_mem_list);
733
734                 return WINBINDD_ERROR;
735         }
736
737         state->response.extra_data = new_extra_data;
738
739         memcpy(&((char *)state->response.extra_data)
740                [group_list_ndx * sizeof(struct winbindd_gr)], 
741                gr_mem_list, gr_mem_list_len);
742
743         SAFE_FREE(gr_mem_list);
744
745         state->response.length += gr_mem_list_len;
746
747         DEBUG(10, ("getgrent(): returning %d groups, length = %d\n",
748                    group_list_ndx, gr_mem_list_len));
749
750         /* Out of domains */
751
752  done:
753         return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
754 }
755
756 /* List domain groups without mapping to unix ids */
757
758 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
759 {
760         uint32 total_entries = 0;
761         uint32 num_domain_entries;
762         struct winbindd_domain *domain;
763         struct getent_state groups;
764         char *extra_data = NULL;
765         char *ted = NULL;
766         int extra_data_len = 0, i;
767         void *sam_entries = NULL;
768
769         DEBUG(3, ("[%5d]: list groups\n", state->pid));
770
771         /* Enumerate over trusted domains */
772         ZERO_STRUCT(groups);
773         for (domain = domain_list; domain; domain = domain->next) {
774
775                 /* Skip domains other than WINBINDD_DOMAIN environment
776                    variable */ 
777
778                 if ((strcmp(state->request.domain, "") != 0) &&
779                     !check_domain_env(state->request.domain, domain->name)) {
780                         continue;
781                 }
782
783                 /* Get list of sam groups */
784
785                 ZERO_STRUCT(groups);
786                 groups.domain = domain;
787
788                 /* 
789                  * iterate through all groups 
790                  * total_entries          :  maintains a total count over **all domains**
791                  * num_domain_entries :  is the running count for this domain
792                  */
793                  
794                 num_domain_entries = 0;
795                 while (get_sam_group_entries(&groups))
796                 {
797                         int new_size;
798                         int offset;
799                         
800                         offset = sizeof(struct acct_info) * num_domain_entries;
801                         new_size = sizeof(struct acct_info) 
802                                 * (groups.num_sam_entries + num_domain_entries);
803                         sam_entries = Realloc(sam_entries, new_size);
804                         
805                         if (!sam_entries)
806                                 return WINBINDD_ERROR;
807                         
808                         num_domain_entries += groups.num_sam_entries;
809                         memcpy (((char *)sam_entries)+offset, groups.sam_entries, 
810                                 sizeof(struct acct_info) * groups.num_sam_entries);
811                         
812                         groups.sam_entries = NULL;
813                         groups.num_sam_entries = 0;
814                 }
815
816                 /* skip remainder of loop if we idn;t retrieve any groups */
817                 
818                 if (num_domain_entries == 0) 
819                         continue;
820
821                 /* setup the groups struct to contain all the groups 
822                    retrieved for this domain */
823                   
824                 groups.num_sam_entries = num_domain_entries;
825                 groups.sam_entries = sam_entries;
826
827                 /* keep track the of the total number of groups seen so 
828                    far over all domains */
829
830                 total_entries += groups.num_sam_entries;
831                 
832                 /* Allocate some memory for extra data.  Note that we limit
833                    account names to sizeof(fstring) = 128 characters.  */
834                 
835                 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
836  
837                 if (!ted) {
838                         DEBUG(0,("winbindd_list_groups: failed to enlarge buffer!\n"));
839                         SAFE_FREE(extra_data);
840                         return WINBINDD_ERROR;
841                 } else
842                         extra_data = ted;
843
844                 /* Pack group list into extra data fields */
845
846                 for (i = 0; i < groups.num_sam_entries; i++) {
847                         char *group_name = ((struct acct_info *)
848                                             groups.sam_entries)[i].acct_name; 
849                         fstring name;
850
851                         /* Convert unistring to ascii */
852
853                         snprintf(name, sizeof(name), "%s%s%s", domain->name, 
854                                 lp_winbind_separator(), group_name);
855
856                         /* Append to extra data */
857                         
858                         memcpy(&extra_data[extra_data_len], name, strlen(name));
859                         extra_data_len += strlen(name);
860
861                         extra_data[extra_data_len++] = ',';
862                 }
863         }
864
865         /* Assign extra_data fields in response structure */
866
867         if (extra_data) {
868                 extra_data[extra_data_len - 1] = '\0';
869                 state->response.extra_data = extra_data;
870                 state->response.length += extra_data_len;
871         }
872
873         /* No domains may have responded but that's still OK so don't
874            return an error. */
875
876         return WINBINDD_OK;
877 }
878
879 /* Get user supplementary groups.  This is much quicker than trying to
880    invert the groups database. */
881
882 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
883 {
884         fstring name_domain, name_user, name;
885         DOM_SID user_sid;
886         enum SID_NAME_USE name_type;
887         uint32 user_rid, num_groups, num_gids;
888         DOM_GID *user_groups = NULL;
889         struct winbindd_domain *domain;
890         enum winbindd_result result;
891         gid_t *gid_list;
892         int i;
893         
894         DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
895                   state->request.data.username));
896
897         if (state == NULL) return WINBINDD_ERROR;
898
899         /* Parse domain and username */
900
901         parse_domain_user(state->request.data.username, name_domain, 
902                           name_user);
903
904         /* Reject names that don't have a domain - i.e name_domain contains 
905            the entire name. */
906  
907         if (strequal(name_domain, "")) {
908                 return WINBINDD_ERROR;
909         }
910
911         /* Get info for the domain */
912         
913         if ((domain = find_domain_from_name(name_domain)) == NULL) {
914                 DEBUG(0, ("could not find domain entry for domain %s\n", 
915                           name_domain));
916                 return WINBINDD_ERROR;
917         }
918
919         if (!domain_handles_open(domain)) {
920                 return WINBINDD_ERROR;
921         }
922
923         slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
924         
925         /* Get rid and name type from name.  The following costs 1 packet */
926
927         if (!winbindd_lookup_sid_by_name(name, &user_sid, &name_type)) {
928                 DEBUG(1, ("user '%s' does not exist\n", name_user));
929                 return WINBINDD_ERROR;
930         }
931
932         if (name_type != SID_NAME_USER) {
933                 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, 
934                           name_type));
935                 return WINBINDD_ERROR;
936         }
937
938         sid_split_rid(&user_sid, &user_rid);
939
940         if (!winbindd_lookup_usergroups(domain, user_rid, &num_groups,
941                                         &user_groups)) {
942                 return WINBINDD_ERROR;
943         }
944
945         /* Copy data back to client */
946
947         num_gids = 0;
948         gid_list = malloc(sizeof(gid_t) * num_groups);
949
950         if (state->response.extra_data) {
951                 result = WINBINDD_ERROR;
952                 goto done;
953         }
954
955         for (i = 0; i < num_groups; i++) {
956                 if (!winbindd_idmap_get_gid_from_rid(
957                         domain->name, user_groups[i].g_rid, 
958                         &gid_list[num_gids])) {
959
960                         DEBUG(1, ("unable to convert group rid %d to gid\n", 
961                                   user_groups[i].g_rid));
962                         continue;
963                 }
964                         
965                 num_gids++;
966         }
967
968         state->response.data.num_entries = num_gids;
969         state->response.extra_data = gid_list;
970         state->response.length += num_gids * sizeof(gid_t);
971
972         result = WINBINDD_OK;
973
974  done:
975         SAFE_FREE(user_groups);
976
977         return result;
978 }