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