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