Initial import
[samba] / 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 && use_nss_info("sfu")) {
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         fstring username;
234         struct getpwsid_state *s =
235                 talloc_get_type_abort(private_data, struct getpwsid_state);
236
237         if (!success) {
238                 DEBUG(5, ("Could not query user %s\\%s\n", s->domain->name,
239                           s->username));
240                 request_error(s->state);
241                 return;
242         }
243
244         fstrcpy( username, acct_name );
245         strlower_m( username );
246         s->username = talloc_strdup(s->state->mem_ctx, username);
247         s->fullname = talloc_strdup(s->state->mem_ctx, full_name);
248         s->homedir = talloc_strdup(s->state->mem_ctx, homedir);
249         s->shell = talloc_strdup(s->state->mem_ctx, shell);
250         sid_copy(&s->group_sid, &s->domain->sid);
251         sid_append_rid(&s->group_sid, group_rid);
252
253         winbindd_sid2uid_async(s->state->mem_ctx, &s->user_sid,
254                                getpwsid_sid2uid_recv, s);
255 }
256
257 static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid)
258 {
259         struct getpwsid_state *s =
260                 talloc_get_type_abort(private_data, struct getpwsid_state);
261
262         if (!success) {
263                 DEBUG(5, ("Could not query user's %s\\%s uid\n",
264                           s->domain->name, s->username));
265                 request_error(s->state);
266                 return;
267         }
268
269         s->uid = uid;
270         winbindd_sid2gid_async(s->state->mem_ctx, &s->group_sid,
271                                getpwsid_sid2gid_recv, s);
272 }
273
274 static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid)
275 {
276         struct getpwsid_state *s =
277                 talloc_get_type_abort(private_data, struct getpwsid_state);
278         struct winbindd_pw *pw;
279         fstring output_username;
280
281         if (!success) {
282                 DEBUG(5, ("Could not query user's %s\\%s\n gid",
283                           s->domain->name, s->username));
284                 goto failed;
285         }
286
287         s->gid = gid;
288
289         pw = &s->state->response.data.pw;
290         pw->pw_uid = s->uid;
291         pw->pw_gid = s->gid;
292         fill_domain_username(output_username, s->domain->name, s->username); 
293         safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
294         safe_strcpy(pw->pw_gecos, s->fullname, sizeof(pw->pw_gecos) - 1);
295
296         fstrcpy(current_user_info.domain, s->domain->name);
297
298         if (!fillup_pw_field(lp_template_homedir(), s->username, s->domain->name, 
299                              pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir)) {
300                 DEBUG(5, ("Could not compose homedir\n"));
301                 goto failed;
302         }
303
304         if (!fillup_pw_field(lp_template_shell(), s->username, s->domain->name, 
305                              pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell)) {
306                 DEBUG(5, ("Could not compose shell\n"));
307                 goto failed;
308         }
309
310         /* Password - set to "x" as we can't generate anything useful here.
311            Authentication can be done using the pam_winbind module. */
312
313         safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
314
315         request_ok(s->state);
316         return;
317
318  failed:
319         request_error(s->state);
320 }
321
322 /* Return a password structure from a username.  */
323
324 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
325                                    const DOM_SID *sid, enum SID_NAME_USE type);
326
327 void winbindd_getpwnam(struct winbindd_cli_state *state)
328 {
329         struct winbindd_domain *domain;
330         fstring domname, username;
331
332         /* Ensure null termination */
333         state->request.data.username[sizeof(state->request.data.username)-1]='\0';
334
335         DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
336                   state->request.data.username));
337
338         if (!parse_domain_user(state->request.data.username, domname,
339                                username)) {
340                 DEBUG(0, ("Could not parse domain user: %s\n",
341                           state->request.data.username));
342                 request_error(state);
343                 return;
344         }
345         
346         /* Get info for the domain */
347
348         domain = find_domain_from_name(domname);
349
350         if (domain == NULL) {
351                 DEBUG(7, ("could not find domain entry for domain %s\n",
352                           domname));
353                 request_error(state);
354                 return;
355         }
356
357         if ( strequal(domname, lp_workgroup()) && lp_winbind_trusted_domains_only() ) {
358                 DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n", 
359                         domname, username));
360                 request_error(state);
361                 return;
362         }       
363
364         /* Get rid and name type from name.  The following costs 1 packet */
365
366         winbindd_lookupname_async(state->mem_ctx, domname, username,
367                                   getpwnam_name2sid_recv, state);
368 }
369
370 static void getpwnam_name2sid_recv(void *private_data, BOOL success,
371                                    const DOM_SID *sid, enum SID_NAME_USE type)
372 {
373         struct winbindd_cli_state *state = private_data;
374
375         if (!success) {
376                 DEBUG(5, ("Could not lookup name for user %s\n",
377                           state->request.data.username));
378                 request_error(state);
379                 return;
380         }
381
382         if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
383                 DEBUG(5, ("%s is not a user\n", state->request.data.username));
384                 request_error(state);
385                 return;
386         }
387
388         winbindd_getpwsid(state, sid);
389 }
390
391 /* Return a password structure given a uid number */
392
393 void winbindd_getpwuid(struct winbindd_cli_state *state)
394 {
395         DOM_SID user_sid;
396         NTSTATUS status;
397         
398         /* Bug out if the uid isn't in the winbind range */
399
400         if ((state->request.data.uid < server_state.uid_low ) ||
401             (state->request.data.uid > server_state.uid_high)) {
402                 request_error(state);
403                 return;
404         }
405
406         DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
407                   (unsigned long)state->request.data.uid));
408
409         status = idmap_uid_to_sid(&user_sid, state->request.data.uid,
410                                   ID_QUERY_ONLY | ID_CACHE_ONLY);
411
412         if (!NT_STATUS_IS_OK(status)) {
413                 DEBUG(5, ("Could not find SID for uid %lu\n",
414                           (unsigned long)state->request.data.uid));
415                 request_error(state);
416                 return;
417         }
418
419         winbindd_getpwsid(state, &user_sid);
420 }
421
422 /*
423  * set/get/endpwent functions
424  */
425
426 /* Rewind file pointer for ntdom passwd database */
427
428 static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
429 {
430         struct winbindd_domain *domain;
431         
432         DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
433         
434         /* Check user has enabled this */
435         
436         if (!lp_winbind_enum_users()) {
437                 return False;
438         }
439
440         /* Free old static data if it exists */
441         
442         if (state->getpwent_state != NULL) {
443                 free_getent_state(state->getpwent_state);
444                 state->getpwent_state = NULL;
445         }
446
447 #if 0   /* JERRY */
448         /* add any local users we have */
449                 
450         if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
451                 return False;
452                 
453         ZERO_STRUCTP(domain_state);
454
455         /* Add to list of open domains */
456                 
457         DLIST_ADD(state->getpwent_state, domain_state);
458 #endif
459         
460         /* Create sam pipes for each domain we know about */
461         
462         for(domain = domain_list(); domain != NULL; domain = domain->next) {
463                 struct getent_state *domain_state;
464                 
465                 
466                 /* don't add our domaina if we are a PDC or if we 
467                    are a member of a Samba domain */
468                 
469                 if ( (IS_DC || lp_winbind_trusted_domains_only())
470                         && strequal(domain->name, lp_workgroup()) )
471                 {
472                         continue;
473                 }
474                                                 
475                 /* Create a state record for this domain */
476                 
477                 if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
478                         DEBUG(0, ("malloc failed\n"));
479                         return False;
480                 }
481                 
482                 ZERO_STRUCTP(domain_state);
483
484                 fstrcpy(domain_state->domain_name, domain->name);
485
486                 /* Add to list of open domains */
487                 
488                 DLIST_ADD(state->getpwent_state, domain_state);
489         }
490         
491         state->getpwent_initialized = True;
492         return True;
493 }
494
495 void winbindd_setpwent(struct winbindd_cli_state *state)
496 {
497         if (winbindd_setpwent_internal(state)) {
498                 request_ok(state);
499         } else {
500                 request_error(state);
501         }
502 }
503
504 /* Close file pointer to ntdom passwd database */
505
506 void winbindd_endpwent(struct winbindd_cli_state *state)
507 {
508         DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
509
510         free_getent_state(state->getpwent_state);    
511         state->getpwent_initialized = False;
512         state->getpwent_state = NULL;
513         request_ok(state);
514 }
515
516 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
517    and num_sam_entries fields with domain user information.  The dispinfo_ndx
518    field is incremented to the index of the next user to fetch.  Return True if
519    some users were returned, False otherwise. */
520
521 static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
522 {
523         NTSTATUS status;
524         uint32 num_entries;
525         WINBIND_USERINFO *info;
526         struct getpwent_user *name_list = NULL;
527         BOOL result = False;
528         struct winbindd_domain *domain;
529         struct winbindd_methods *methods;
530         unsigned int i;
531
532         if (ent->num_sam_entries)
533                 return False;
534
535         if (!(domain = find_domain_from_name(ent->domain_name))) {
536                 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
537                           ent->domain_name));
538                 return False;
539         }
540
541         methods = domain->methods;
542
543         /* Free any existing user info */
544
545         SAFE_FREE(ent->sam_entries);
546         ent->num_sam_entries = 0;
547         
548         /* Call query_user_list to get a list of usernames and user rids */
549
550         num_entries = 0;
551
552         status = methods->query_user_list(domain, mem_ctx, &num_entries, 
553                                           &info);
554                 
555         if (num_entries) {
556                 struct getpwent_user *tnl;
557                 
558                 tnl = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
559                 
560                 if (!tnl) {
561                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
562                         SAFE_FREE(name_list);
563                         goto done;
564                 } else
565                         name_list = tnl;
566         }
567
568         for (i = 0; i < num_entries; i++) {
569                 /* Store account name and gecos */
570                 if (!info[i].acct_name) {
571                         fstrcpy(name_list[ent->num_sam_entries + i].name, "");
572                 } else {
573                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
574                                 info[i].acct_name); 
575                 }
576                 if (!info[i].full_name) {
577                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
578                 } else {
579                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
580                                 info[i].full_name); 
581                 }
582                 if (!info[i].homedir) {
583                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
584                 } else {
585                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, 
586                                 info[i].homedir); 
587                 }
588                 if (!info[i].shell) {
589                         fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
590                 } else {
591                         fstrcpy(name_list[ent->num_sam_entries + i].shell, 
592                                 info[i].shell); 
593                 }
594         
595         
596                 /* User and group ids */
597                 sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
598                          &info[i].user_sid);
599                 sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
600                          &info[i].group_sid);
601         }
602                 
603         ent->num_sam_entries += num_entries;
604         
605         /* Fill in remaining fields */
606         
607         ent->sam_entries = name_list;
608         ent->sam_entry_index = 0;
609         result = ent->num_sam_entries > 0;
610
611  done:
612
613         return result;
614 }
615
616 /* Fetch next passwd entry from ntdom database */
617
618 #define MAX_GETPWENT_USERS 500
619
620 void winbindd_getpwent(struct winbindd_cli_state *state)
621 {
622         struct getent_state *ent;
623         struct winbindd_pw *user_list;
624         int num_users, user_list_ndx = 0, i;
625
626         DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
627
628         /* Check user has enabled this */
629
630         if (!lp_winbind_enum_users()) {
631                 request_error(state);
632                 return;
633         }
634
635         /* Allocate space for returning a chunk of users */
636
637         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
638         
639         if ((state->response.extra_data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
640                 request_error(state);
641                 return;
642         }
643
644         memset(state->response.extra_data, 0, num_users * 
645                sizeof(struct winbindd_pw));
646
647         user_list = (struct winbindd_pw *)state->response.extra_data;
648
649         if (!state->getpwent_initialized)
650                 winbindd_setpwent_internal(state);
651         
652         if (!(ent = state->getpwent_state)) {
653                 request_error(state);
654                 return;
655         }
656
657         /* Start sending back users */
658
659         for (i = 0; i < num_users; i++) {
660                 struct getpwent_user *name_list = NULL;
661                 uint32 result;
662
663                 /* Do we need to fetch another chunk of users? */
664
665                 if (ent->num_sam_entries == ent->sam_entry_index) {
666
667                         while(ent &&
668                               !get_sam_user_entries(ent, state->mem_ctx)) {
669                                 struct getent_state *next_ent;
670
671                                 /* Free state information for this domain */
672
673                                 SAFE_FREE(ent->sam_entries);
674
675                                 next_ent = ent->next;
676                                 DLIST_REMOVE(state->getpwent_state, ent);
677
678                                 SAFE_FREE(ent);
679                                 ent = next_ent;
680                         }
681  
682                         /* No more domains */
683
684                         if (!ent) 
685                                 break;
686                 }
687
688                 name_list = ent->sam_entries;
689
690                 /* Lookup user info */
691                 
692                 result = winbindd_fill_pwent(
693                         ent->domain_name, 
694                         name_list[ent->sam_entry_index].name,
695                         &name_list[ent->sam_entry_index].user_sid,
696                         &name_list[ent->sam_entry_index].group_sid,
697                         name_list[ent->sam_entry_index].gecos,
698                         name_list[ent->sam_entry_index].homedir,
699                         name_list[ent->sam_entry_index].shell,
700                         &user_list[user_list_ndx]);
701                 
702                 ent->sam_entry_index++;
703                 
704                 /* Add user to return list */
705                 
706                 if (result) {
707                                 
708                         user_list_ndx++;
709                         state->response.data.num_entries++;
710                         state->response.length += 
711                                 sizeof(struct winbindd_pw);
712
713                 } else
714                         DEBUG(1, ("could not lookup domain user %s\n",
715                                   name_list[ent->sam_entry_index].name));
716         }
717
718         /* Out of domains */
719
720         if (user_list_ndx > 0)
721                 request_ok(state);
722         else
723                 request_error(state);
724 }
725
726 /* List domain users without mapping to unix ids */
727
728 void winbindd_list_users(struct winbindd_cli_state *state)
729 {
730         struct winbindd_domain *domain;
731         WINBIND_USERINFO *info;
732         const char *which_domain;
733         uint32 num_entries = 0, total_entries = 0;
734         char *ted, *extra_data = NULL;
735         int extra_data_len = 0;
736         enum winbindd_result rv = WINBINDD_ERROR;
737
738         DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
739
740         /* Ensure null termination */
741         state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
742         which_domain = state->request.domain_name;
743         
744         /* Enumerate over trusted domains */
745
746         for (domain = domain_list(); domain; domain = domain->next) {
747                 NTSTATUS status;
748                 struct winbindd_methods *methods;
749                 unsigned int i;
750                 
751                 /* if we have a domain name restricting the request and this
752                    one in the list doesn't match, then just bypass the remainder
753                    of the loop */
754                    
755                 if ( *which_domain && !strequal(which_domain, domain->name) )
756                         continue;
757                         
758                 methods = domain->methods;
759
760                 /* Query display info */
761                 status = methods->query_user_list(domain, state->mem_ctx, 
762                                                   &num_entries, &info);
763
764                 if (num_entries == 0)
765                         continue;
766
767                 /* Allocate some memory for extra data */
768                 total_entries += num_entries;
769                         
770                 ted = SMB_REALLOC(extra_data, sizeof(fstring) * total_entries);
771                         
772                 if (!ted) {
773                         DEBUG(0,("failed to enlarge buffer!\n"));
774                         SAFE_FREE(extra_data);
775                         goto done;
776                 } else 
777                         extra_data = ted;
778                         
779                 /* Pack user list into extra data fields */
780                         
781                 for (i = 0; i < num_entries; i++) {
782                         fstring acct_name, name;
783                         
784                         if (!info[i].acct_name) {
785                                 fstrcpy(acct_name, "");
786                         } else {
787                                 fstrcpy(acct_name, info[i].acct_name);
788                         }
789                         
790                         fill_domain_username(name, domain->name, acct_name);
791                         
792                                 /* Append to extra data */
793                         memcpy(&extra_data[extra_data_len], name, 
794                                strlen(name));
795                         extra_data_len += strlen(name);
796                         extra_data[extra_data_len++] = ',';
797                 }   
798         }
799
800         /* Assign extra_data fields in response structure */
801
802         if (extra_data) {
803                 extra_data[extra_data_len - 1] = '\0';
804                 state->response.extra_data = extra_data;
805                 state->response.length += extra_data_len;
806         }
807
808         /* No domains responded but that's still OK so don't return an
809            error. */
810
811         rv = WINBINDD_OK;
812
813  done:
814
815         if (rv == WINBINDD_OK)
816                 request_ok(state);
817         else
818                 request_error(state);
819 }