r7994: This adds support in Winbindd's "security = ads"-mode to retrieve the POSIX
[tprouty/samba.git] / source / nsswitch / winbindd_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - user related functions
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jeremy Allison 2001.
8    Copyright (C) Gerald (Jerry) Carter 2003.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "winbindd.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
30
31 extern userdom_struct current_user_info;
32
33 static BOOL fillup_pw_field(const char *lp_template, 
34                             const char *username, 
35                             const char *domname,
36                             uid_t uid,
37                             gid_t gid,
38                             const char *in, 
39                             fstring out)
40 {
41         char *templ;
42
43         if (out == NULL)
44                 return False;
45
46         if (in && !strequal(in,"") && lp_security() == SEC_ADS && lp_winbind_sfu_support()) {
47                 safe_strcpy(out, in, sizeof(fstring) - 1);
48                 return True;
49         }
50
51         /* Home directory and shell - use template config parameters.  The
52            defaults are /tmp for the home directory and /bin/false for
53            shell. */
54         
55         /* The substitution of %U and %D in the 'template homedir' is done
56            by alloc_sub_specified() below. */
57
58         templ = alloc_sub_specified(lp_template, username, domname, uid, gid);
59                 
60         if (!templ)
61                 return False;
62
63         safe_strcpy(out, templ, sizeof(fstring) - 1);
64         SAFE_FREE(templ);
65                 
66         return True;
67         
68 }
69 /* Fill a pwent structure with information we have obtained */
70
71 static BOOL winbindd_fill_pwent(char *dom_name, char *user_name, 
72                                 DOM_SID *user_sid, DOM_SID *group_sid,
73                                 char *full_name, char *homedir, char *shell,
74                                 struct winbindd_pw *pw)
75 {
76         fstring output_username;
77         fstring sid_string;
78         
79         if (!pw || !dom_name || !user_name)
80                 return False;
81         
82         /* Resolve the uid number */
83
84         if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &pw->pw_uid, 0))) {
85                 DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
86                 return False;
87         }
88         
89         /* Resolve the gid number */   
90
91         if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &pw->pw_gid, 0))) {
92                 DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
93                 return False;
94         }
95
96         strlower_m(user_name);
97
98         /* Username */
99
100         fill_domain_username(output_username, dom_name, user_name); 
101
102         safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
103         
104         /* Full name (gecos) */
105         
106         safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);
107
108         /* Home directory and shell - use template config parameters.  The
109            defaults are /tmp for the home directory and /bin/false for
110            shell. */
111         
112         /* The substitution of %U and %D in the 'template homedir' is done
113            by alloc_sub_specified() below. */
114
115         fstrcpy(current_user_info.domain, dom_name);
116
117         if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name, 
118                              pw->pw_uid, pw->pw_gid, homedir, pw->pw_dir))
119                 return False;
120
121         if (!fillup_pw_field(lp_template_shell(), user_name, dom_name, 
122                              pw->pw_uid, pw->pw_gid, shell, pw->pw_shell))
123                 return False;
124
125         /* Password - set to "x" as we can't generate anything useful here.
126            Authentication can be done using the pam_winbind module. */
127
128         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
129
130         return True;
131 }
132
133 /* Wrapper for domain->methods->query_user, only on the parent->child pipe */
134
135 enum winbindd_result winbindd_dual_userinfo(struct winbindd_domain *domain,
136                                             struct winbindd_cli_state *state)
137 {
138         DOM_SID sid;
139         WINBIND_USERINFO user_info;
140         NTSTATUS status;
141
142         /* Ensure null termination */
143         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
144
145         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
146                   state->request.data.sid));
147
148         if (!string_to_sid(&sid, state->request.data.sid)) {
149                 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
150                 return WINBINDD_ERROR;
151         }
152
153         status = domain->methods->query_user(domain, state->mem_ctx,
154                                              &sid, &user_info);
155         if (!NT_STATUS_IS_OK(status)) {
156                 DEBUG(1, ("error getting user info for sid %s\n",
157                           sid_string_static(&sid)));
158                 return WINBINDD_ERROR;
159         }
160
161         fstrcpy(state->response.data.user_info.acct_name, user_info.acct_name);
162         fstrcpy(state->response.data.user_info.full_name, user_info.full_name);
163         fstrcpy(state->response.data.user_info.homedir, user_info.homedir);
164         fstrcpy(state->response.data.user_info.shell, user_info.shell);
165         if (!sid_peek_check_rid(&domain->sid, &user_info.group_sid,
166                                 &state->response.data.user_info.group_rid)) {
167                 DEBUG(1, ("Could not extract group rid out of %s\n",
168                           sid_string_static(&sid)));
169                 return WINBINDD_ERROR;
170         }
171
172         return WINBINDD_OK;
173 }
174
175 struct getpwsid_state {
176         struct winbindd_cli_state *state;
177         struct winbindd_domain *domain;
178         char *username;
179         char *fullname;
180         char *homedir;
181         char *shell;
182         DOM_SID user_sid;
183         uid_t uid;
184         DOM_SID group_sid;
185         gid_t gid;
186 };
187
188 static void getpwsid_queryuser_recv(void *private_data, BOOL success,
189                                     const char *acct_name,
190                                     const char *full_name, 
191                                     const char *homedir,
192                                     const char *shell,
193                                     uint32 group_rid);
194 static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid);
195 static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid);
196
197 static void winbindd_getpwsid(struct winbindd_cli_state *state,
198                               const DOM_SID *sid)
199 {
200         struct getpwsid_state *s;
201
202         s = TALLOC_P(state->mem_ctx, struct getpwsid_state);
203         if (s == NULL) {
204                 DEBUG(0, ("talloc failed\n"));
205                 goto error;
206         }
207
208         s->state = state;
209         s->domain = find_domain_from_sid_noinit(sid);
210         if (s->domain == NULL) {
211                 DEBUG(3, ("Could not find domain for sid %s\n",
212                           sid_string_static(sid)));
213                 goto error;
214         }
215
216         sid_copy(&s->user_sid, sid);
217
218         query_user_async(s->state->mem_ctx, s->domain, sid,
219                          getpwsid_queryuser_recv, s);
220         return;
221
222  error:
223         request_error(state);
224 }
225         
226 static void getpwsid_queryuser_recv(void *private_data, BOOL success,
227                                     const char *acct_name,
228                                     const char *full_name, 
229                                     const char *homedir,
230                                     const char *shell,
231                                     uint32 group_rid)
232 {
233         struct getpwsid_state *s =
234                 talloc_get_type_abort(private_data, struct getpwsid_state);
235
236         if (!success) {
237                 DEBUG(5, ("Could not query user %s\\%s\n", s->domain->name,
238                           s->username));
239                 request_error(s->state);
240                 return;
241         }
242
243         s->username = talloc_strdup(s->state->mem_ctx, acct_name);
244         s->fullname = talloc_strdup(s->state->mem_ctx, full_name);
245         s->homedir = talloc_strdup(s->state->mem_ctx, homedir);
246         s->shell = talloc_strdup(s->state->mem_ctx, shell);
247         sid_copy(&s->group_sid, &s->domain->sid);
248         sid_append_rid(&s->group_sid, group_rid);
249
250         winbindd_sid2uid_async(s->state->mem_ctx, &s->user_sid,
251                                getpwsid_sid2uid_recv, s);
252 }
253
254 static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid)
255 {
256         struct getpwsid_state *s =
257                 talloc_get_type_abort(private_data, struct getpwsid_state);
258
259         if (!success) {
260                 DEBUG(5, ("Could not query user's %s\\%s uid\n",
261                           s->domain->name, s->username));
262                 request_error(s->state);
263                 return;
264         }
265
266         s->uid = uid;
267         winbindd_sid2gid_async(s->state->mem_ctx, &s->group_sid,
268                                getpwsid_sid2gid_recv, s);
269 }
270
271 static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid)
272 {
273         struct getpwsid_state *s =
274                 talloc_get_type_abort(private_data, struct getpwsid_state);
275         struct winbindd_pw *pw;
276         fstring output_username;
277
278         if (!success) {
279                 DEBUG(5, ("Could not query user's %s\\%s\n gid",
280                           s->domain->name, s->username));
281                 goto failed;
282         }
283
284         s->gid = gid;
285
286         pw = &s->state->response.data.pw;
287         pw->pw_uid = s->uid;
288         pw->pw_gid = s->gid;
289         fill_domain_username(output_username, s->domain->name, s->username); 
290         safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
291         safe_strcpy(pw->pw_gecos, s->fullname, sizeof(pw->pw_gecos) - 1);
292
293         fstrcpy(current_user_info.domain, s->domain->name);
294
295         if (!fillup_pw_field(lp_template_homedir(), s->username, s->domain->name, 
296                              pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir)) {
297                 DEBUG(5, ("Could not compose homedir\n"));
298                 goto failed;
299         }
300
301         if (!fillup_pw_field(lp_template_shell(), s->username, s->domain->name, 
302                              pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell)) {
303                 DEBUG(5, ("Could not compose shell\n"));
304                 goto failed;
305         }
306
307         /* Password - set to "x" as we can't generate anything useful here.
308            Authentication can be done using the pam_winbind module. */
309
310         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
311
312         request_ok(s->state);
313         return;
314
315  failed:
316         request_error(s->state);
317 }
318
319 /* Return a password structure from a username.  */
320
321 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
322                                    const DOM_SID *sid, enum SID_NAME_USE type);
323
324 void winbindd_getpwnam(struct winbindd_cli_state *state)
325 {
326         struct winbindd_domain *domain;
327         fstring domname, username;
328
329         /* Ensure null termination */
330         state->request.data.username[sizeof(state->request.data.username)-1]='\0';
331
332         DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
333                   state->request.data.username));
334
335         if (!parse_domain_user(state->request.data.username, domname,
336                                username)) {
337                 DEBUG(0, ("Could not parse domain user: %s\n",
338                           state->request.data.username));
339                 request_error(state);
340                 return;
341         }
342         
343         /* Get info for the domain */
344
345         domain = find_domain_from_name(domname);
346
347         if (domain == NULL) {
348                 DEBUG(7, ("could not find domain entry for domain %s\n",
349                           domname));
350                 request_error(state);
351                 return;
352         }
353
354         if ( strequal(domname, lp_workgroup()) && lp_winbind_trusted_domains_only() ) {
355                 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n", 
356                         domname, username));
357                 request_error(state);
358                 return;
359         }       
360
361         /* Get rid and name type from name.  The following costs 1 packet */
362
363         winbindd_lookupname_async(state->mem_ctx, domname, username,
364                                   getpwnam_name2sid_recv, state);
365 }
366
367 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
368                                    const DOM_SID *sid, enum SID_NAME_USE type)
369 {
370         struct winbindd_cli_state *state = private_data;
371
372         if (!success) {
373                 DEBUG(5, ("Could not lookup name for user %s\n",
374                           state->request.data.username));
375                 request_error(state);
376                 return;
377         }
378
379         if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
380                 DEBUG(5, ("%s is not a user\n", state->request.data.username));
381                 request_error(state);
382                 return;
383         }
384
385         winbindd_getpwsid(state, sid);
386 }
387
388 /* Return a password structure given a uid number */
389
390 void winbindd_getpwuid(struct winbindd_cli_state *state)
391 {
392         DOM_SID user_sid;
393         NTSTATUS status;
394         
395         /* Bug out if the uid isn't in the winbind range */
396
397         if ((state->request.data.uid < server_state.uid_low ) ||
398             (state->request.data.uid > server_state.uid_high)) {
399                 request_error(state);
400                 return;
401         }
402
403         DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
404                   (unsigned long)state->request.data.uid));
405
406         status = idmap_uid_to_sid(&user_sid, state->request.data.uid,
407                                   ID_QUERY_ONLY | ID_CACHE_ONLY);
408
409         if (!NT_STATUS_IS_OK(status)) {
410                 DEBUG(5, ("Could not find SID for uid %lu\n",
411                           (unsigned long)state->request.data.uid));
412                 request_error(state);
413                 return;
414         }
415
416         winbindd_getpwsid(state, &user_sid);
417 }
418
419 /*
420  * set/get/endpwent functions
421  */
422
423 /* Rewind file pointer for ntdom passwd database */
424
425 static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
426 {
427         struct winbindd_domain *domain;
428         
429         DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
430         
431         /* Check user has enabled this */
432         
433         if (!lp_winbind_enum_users()) {
434                 return False;
435         }
436
437         /* Free old static data if it exists */
438         
439         if (state->getpwent_state != NULL) {
440                 free_getent_state(state->getpwent_state);
441                 state->getpwent_state = NULL;
442         }
443
444 #if 0   /* JERRY */
445         /* add any local users we have */
446                 
447         if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
448                 return False;
449                 
450         ZERO_STRUCTP(domain_state);
451
452         /* Add to list of open domains */
453                 
454         DLIST_ADD(state->getpwent_state, domain_state);
455 #endif
456         
457         /* Create sam pipes for each domain we know about */
458         
459         for(domain = domain_list(); domain != NULL; domain = domain->next) {
460                 struct getent_state *domain_state;
461                 
462                 
463                 /* don't add our domaina if we are a PDC or if we 
464                    are a member of a Samba domain */
465                 
466                 if ( (IS_DC || lp_winbind_trusted_domains_only())
467                         && strequal(domain->name, lp_workgroup()) )
468                 {
469                         continue;
470                 }
471                                                 
472                 /* Create a state record for this domain */
473                 
474                 if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
475                         DEBUG(0, ("malloc failed\n"));
476                         return False;
477                 }
478                 
479                 ZERO_STRUCTP(domain_state);
480
481                 fstrcpy(domain_state->domain_name, domain->name);
482
483                 /* Add to list of open domains */
484                 
485                 DLIST_ADD(state->getpwent_state, domain_state);
486         }
487         
488         state->getpwent_initialized = True;
489         return True;
490 }
491
492 void winbindd_setpwent(struct winbindd_cli_state *state)
493 {
494         if (winbindd_setpwent_internal(state)) {
495                 request_ok(state);
496         } else {
497                 request_error(state);
498         }
499 }
500
501 /* Close file pointer to ntdom passwd database */
502
503 void winbindd_endpwent(struct winbindd_cli_state *state)
504 {
505         DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
506
507         free_getent_state(state->getpwent_state);    
508         state->getpwent_initialized = False;
509         state->getpwent_state = NULL;
510         request_ok(state);
511 }
512
513 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
514    and num_sam_entries fields with domain user information.  The dispinfo_ndx
515    field is incremented to the index of the next user to fetch.  Return True if
516    some users were returned, False otherwise. */
517
518 static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
519 {
520         NTSTATUS status;
521         uint32 num_entries;
522         WINBIND_USERINFO *info;
523         struct getpwent_user *name_list = NULL;
524         BOOL result = False;
525         struct winbindd_domain *domain;
526         struct winbindd_methods *methods;
527         unsigned int i;
528
529         if (ent->num_sam_entries)
530                 return False;
531
532         if (!(domain = find_domain_from_name(ent->domain_name))) {
533                 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
534                           ent->domain_name));
535                 return False;
536         }
537
538         methods = domain->methods;
539
540         /* Free any existing user info */
541
542         SAFE_FREE(ent->sam_entries);
543         ent->num_sam_entries = 0;
544         
545         /* Call query_user_list to get a list of usernames and user rids */
546
547         num_entries = 0;
548
549         status = methods->query_user_list(domain, mem_ctx, &num_entries, 
550                                           &info);
551                 
552         if (num_entries) {
553                 struct getpwent_user *tnl;
554                 
555                 tnl = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
556                 
557                 if (!tnl) {
558                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
559                         SAFE_FREE(name_list);
560                         goto done;
561                 } else
562                         name_list = tnl;
563         }
564
565         for (i = 0; i < num_entries; i++) {
566                 /* Store account name and gecos */
567                 if (!info[i].acct_name) {
568                         fstrcpy(name_list[ent->num_sam_entries + i].name, "");
569                 } else {
570                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
571                                 info[i].acct_name); 
572                 }
573                 if (!info[i].full_name) {
574                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
575                 } else {
576                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
577                                 info[i].full_name); 
578                 }
579                 if (!info[i].homedir) {
580                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
581                 } else {
582                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, 
583                                 info[i].homedir); 
584                 }
585                 if (!info[i].shell) {
586                         fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
587                 } else {
588                         fstrcpy(name_list[ent->num_sam_entries + i].shell, 
589                                 info[i].shell); 
590                 }
591         
592         
593                 /* User and group ids */
594                 sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
595                          &info[i].user_sid);
596                 sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
597                          &info[i].group_sid);
598         }
599                 
600         ent->num_sam_entries += num_entries;
601         
602         /* Fill in remaining fields */
603         
604         ent->sam_entries = name_list;
605         ent->sam_entry_index = 0;
606         result = ent->num_sam_entries > 0;
607
608  done:
609
610         return result;
611 }
612
613 /* Fetch next passwd entry from ntdom database */
614
615 #define MAX_GETPWENT_USERS 500
616
617 void winbindd_getpwent(struct winbindd_cli_state *state)
618 {
619         struct getent_state *ent;
620         struct winbindd_pw *user_list;
621         int num_users, user_list_ndx = 0, i;
622
623         DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
624
625         /* Check user has enabled this */
626
627         if (!lp_winbind_enum_users()) {
628                 request_error(state);
629                 return;
630         }
631
632         /* Allocate space for returning a chunk of users */
633
634         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
635         
636         if ((state->response.extra_data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
637                 request_error(state);
638                 return;
639         }
640
641         memset(state->response.extra_data, 0, num_users * 
642                sizeof(struct winbindd_pw));
643
644         user_list = (struct winbindd_pw *)state->response.extra_data;
645
646         if (!state->getpwent_initialized)
647                 winbindd_setpwent_internal(state);
648         
649         if (!(ent = state->getpwent_state)) {
650                 request_error(state);
651                 return;
652         }
653
654         /* Start sending back users */
655
656         for (i = 0; i < num_users; i++) {
657                 struct getpwent_user *name_list = NULL;
658                 uint32 result;
659
660                 /* Do we need to fetch another chunk of users? */
661
662                 if (ent->num_sam_entries == ent->sam_entry_index) {
663
664                         while(ent &&
665                               !get_sam_user_entries(ent, state->mem_ctx)) {
666                                 struct getent_state *next_ent;
667
668                                 /* Free state information for this domain */
669
670                                 SAFE_FREE(ent->sam_entries);
671
672                                 next_ent = ent->next;
673                                 DLIST_REMOVE(state->getpwent_state, ent);
674
675                                 SAFE_FREE(ent);
676                                 ent = next_ent;
677                         }
678  
679                         /* No more domains */
680
681                         if (!ent) 
682                                 break;
683                 }
684
685                 name_list = ent->sam_entries;
686
687                 /* Lookup user info */
688                 
689                 result = winbindd_fill_pwent(
690                         ent->domain_name, 
691                         name_list[ent->sam_entry_index].name,
692                         &name_list[ent->sam_entry_index].user_sid,
693                         &name_list[ent->sam_entry_index].group_sid,
694                         name_list[ent->sam_entry_index].gecos,
695                         name_list[ent->sam_entry_index].homedir,
696                         name_list[ent->sam_entry_index].shell,
697                         &user_list[user_list_ndx]);
698                 
699                 ent->sam_entry_index++;
700                 
701                 /* Add user to return list */
702                 
703                 if (result) {
704                                 
705                         user_list_ndx++;
706                         state->response.data.num_entries++;
707                         state->response.length += 
708                                 sizeof(struct winbindd_pw);
709
710                 } else
711                         DEBUG(1, ("could not lookup domain user %s\n",
712                                   name_list[ent->sam_entry_index].name));
713         }
714
715         /* Out of domains */
716
717         if (user_list_ndx > 0)
718                 request_ok(state);
719         else
720                 request_error(state);
721 }
722
723 /* List domain users without mapping to unix ids */
724
725 void winbindd_list_users(struct winbindd_cli_state *state)
726 {
727         struct winbindd_domain *domain;
728         WINBIND_USERINFO *info;
729         const char *which_domain;
730         uint32 num_entries = 0, total_entries = 0;
731         char *ted, *extra_data = NULL;
732         int extra_data_len = 0;
733         enum winbindd_result rv = WINBINDD_ERROR;
734
735         DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
736
737         /* Ensure null termination */
738         state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
739         which_domain = state->request.domain_name;
740         
741         /* Enumerate over trusted domains */
742
743         for (domain = domain_list(); domain; domain = domain->next) {
744                 NTSTATUS status;
745                 struct winbindd_methods *methods;
746                 unsigned int i;
747                 
748                 /* if we have a domain name restricting the request and this
749                    one in the list doesn't match, then just bypass the remainder
750                    of the loop */
751                    
752                 if ( *which_domain && !strequal(which_domain, domain->name) )
753                         continue;
754                         
755                 methods = domain->methods;
756
757                 /* Query display info */
758                 status = methods->query_user_list(domain, state->mem_ctx, 
759                                                   &num_entries, &info);
760
761                 if (num_entries == 0)
762                         continue;
763
764                 /* Allocate some memory for extra data */
765                 total_entries += num_entries;
766                         
767                 ted = SMB_REALLOC(extra_data, sizeof(fstring) * total_entries);
768                         
769                 if (!ted) {
770                         DEBUG(0,("failed to enlarge buffer!\n"));
771                         SAFE_FREE(extra_data);
772                         goto done;
773                 } else 
774                         extra_data = ted;
775                         
776                 /* Pack user list into extra data fields */
777                         
778                 for (i = 0; i < num_entries; i++) {
779                         fstring acct_name, name;
780                         
781                         if (!info[i].acct_name) {
782                                 fstrcpy(acct_name, "");
783                         } else {
784                                 fstrcpy(acct_name, info[i].acct_name);
785                         }
786                         
787                         fill_domain_username(name, domain->name, acct_name);
788                         
789                                 /* Append to extra data */
790                         memcpy(&extra_data[extra_data_len], name, 
791                                strlen(name));
792                         extra_data_len += strlen(name);
793                         extra_data[extra_data_len++] = ',';
794                 }   
795         }
796
797         /* Assign extra_data fields in response structure */
798
799         if (extra_data) {
800                 extra_data[extra_data_len - 1] = '\0';
801                 state->response.extra_data = extra_data;
802                 state->response.length += extra_data_len;
803         }
804
805         /* No domains responded but that's still OK so don't return an
806            error. */
807
808         rv = WINBINDD_OK;
809
810  done:
811
812         if (rv == WINBINDD_OK)
813                 request_ok(state);
814         else
815                 request_error(state);
816 }