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