98df77c0f218e114fbf9ebf3d510c4b6e7a450e0
[gd/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 =
366                 (struct winbindd_cli_state *)private_data;
367
368         if (!success) {
369                 DEBUG(5, ("Could not lookup name for user %s\n",
370                           state->request.data.username));
371                 request_error(state);
372                 return;
373         }
374
375         if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
376                 DEBUG(5, ("%s is not a user\n", state->request.data.username));
377                 request_error(state);
378                 return;
379         }
380
381         winbindd_getpwsid(state, sid);
382 }
383
384 static void getpwuid_recv(void *private_data, BOOL success, const char *sid)
385 {
386         struct winbindd_cli_state *state =
387                 (struct winbindd_cli_state *)private_data;
388         DOM_SID user_sid;
389
390         if (!success) {
391                 DEBUG(10,("uid2sid_recv: uid [%lu] to sid mapping failed\n.",
392                           (unsigned long)(state->request.data.uid)));
393                 request_error(state);
394                 return;
395         }
396         
397         DEBUG(10,("uid2sid_recv: uid %lu has sid %s\n",
398                   (unsigned long)(state->request.data.uid), sid));
399
400         string_to_sid(&user_sid, sid);
401         winbindd_getpwsid(state, &user_sid);
402 }
403
404 /* Return a password structure given a uid number */
405 void winbindd_getpwuid(struct winbindd_cli_state *state)
406 {
407         DOM_SID user_sid;
408         NTSTATUS status;
409         
410         /* Bug out if the uid isn't in the winbind range */
411         if ((state->request.data.uid < server_state.uid_low ) ||
412             (state->request.data.uid > server_state.uid_high)) {
413                 request_error(state);
414                 return;
415         }
416
417         DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
418                   (unsigned long)state->request.data.uid));
419
420         status = idmap_uid_to_sid(&user_sid, state->request.data.uid,
421                                   IDMAP_FLAG_QUERY_ONLY | IDMAP_FLAG_CACHE_ONLY);
422
423         if (NT_STATUS_IS_OK(status)) {
424                 winbindd_getpwsid(state, &user_sid);
425                 return;
426         }
427
428         DEBUG(10,("Could not find SID for uid %lu in the cache. Querying idmap backend\n",
429                   (unsigned long)state->request.data.uid));
430
431         winbindd_uid2sid_async(state->mem_ctx, state->request.data.uid, getpwuid_recv, state);
432 }
433
434 /*
435  * set/get/endpwent functions
436  */
437
438 /* Rewind file pointer for ntdom passwd database */
439
440 static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
441 {
442         struct winbindd_domain *domain;
443         
444         DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
445         
446         /* Check user has enabled this */
447         
448         if (!lp_winbind_enum_users()) {
449                 return False;
450         }
451
452         /* Free old static data if it exists */
453         
454         if (state->getpwent_state != NULL) {
455                 free_getent_state(state->getpwent_state);
456                 state->getpwent_state = NULL;
457         }
458
459 #if 0   /* JERRY */
460         /* add any local users we have */
461                 
462         if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
463                 return False;
464                 
465         ZERO_STRUCTP(domain_state);
466
467         /* Add to list of open domains */
468                 
469         DLIST_ADD(state->getpwent_state, domain_state);
470 #endif
471         
472         /* Create sam pipes for each domain we know about */
473         
474         for(domain = domain_list(); domain != NULL; domain = domain->next) {
475                 struct getent_state *domain_state;
476                 
477                 
478                 /* don't add our domaina if we are a PDC or if we 
479                    are a member of a Samba domain */
480                 
481                 if ( (IS_DC || lp_winbind_trusted_domains_only())
482                         && strequal(domain->name, lp_workgroup()) )
483                 {
484                         continue;
485                 }
486                                                 
487                 /* Create a state record for this domain */
488                 
489                 if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
490                         DEBUG(0, ("malloc failed\n"));
491                         return False;
492                 }
493                 
494                 ZERO_STRUCTP(domain_state);
495
496                 fstrcpy(domain_state->domain_name, domain->name);
497
498                 /* Add to list of open domains */
499                 
500                 DLIST_ADD(state->getpwent_state, domain_state);
501         }
502         
503         state->getpwent_initialized = True;
504         return True;
505 }
506
507 void winbindd_setpwent(struct winbindd_cli_state *state)
508 {
509         if (winbindd_setpwent_internal(state)) {
510                 request_ok(state);
511         } else {
512                 request_error(state);
513         }
514 }
515
516 /* Close file pointer to ntdom passwd database */
517
518 void winbindd_endpwent(struct winbindd_cli_state *state)
519 {
520         DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
521
522         free_getent_state(state->getpwent_state);    
523         state->getpwent_initialized = False;
524         state->getpwent_state = NULL;
525         request_ok(state);
526 }
527
528 /* Get partial list of domain users for a domain.  We fill in the sam_entries,
529    and num_sam_entries fields with domain user information.  The dispinfo_ndx
530    field is incremented to the index of the next user to fetch.  Return True if
531    some users were returned, False otherwise. */
532
533 static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
534 {
535         NTSTATUS status;
536         uint32 num_entries;
537         WINBIND_USERINFO *info;
538         struct getpwent_user *name_list = NULL;
539         struct winbindd_domain *domain;
540         struct winbindd_methods *methods;
541         unsigned int i;
542
543         if (ent->num_sam_entries)
544                 return False;
545
546         if (!(domain = find_domain_from_name(ent->domain_name))) {
547                 DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
548                           ent->domain_name));
549                 return False;
550         }
551
552         methods = domain->methods;
553
554         /* Free any existing user info */
555
556         SAFE_FREE(ent->sam_entries);
557         ent->num_sam_entries = 0;
558         
559         /* Call query_user_list to get a list of usernames and user rids */
560
561         num_entries = 0;
562
563         status = methods->query_user_list(domain, mem_ctx, &num_entries, 
564                                           &info);
565                 
566         if (!NT_STATUS_IS_OK(status)) {
567                 DEBUG(10,("get_sam_user_entries: query_user_list failed with %s\n",
568                         nt_errstr(status) ));
569                 return False;
570         }
571
572         if (num_entries) {
573                 name_list = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
574                 
575                 if (!name_list) {
576                         DEBUG(0,("get_sam_user_entries realloc failed.\n"));
577                         return False;
578                 }
579         }
580
581         for (i = 0; i < num_entries; i++) {
582                 /* Store account name and gecos */
583                 if (!info[i].acct_name) {
584                         fstrcpy(name_list[ent->num_sam_entries + i].name, "");
585                 } else {
586                         fstrcpy(name_list[ent->num_sam_entries + i].name, 
587                                 info[i].acct_name); 
588                 }
589                 if (!info[i].full_name) {
590                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
591                 } else {
592                         fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
593                                 info[i].full_name); 
594                 }
595                 if (!info[i].homedir) {
596                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
597                 } else {
598                         fstrcpy(name_list[ent->num_sam_entries + i].homedir, 
599                                 info[i].homedir); 
600                 }
601                 if (!info[i].shell) {
602                         fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
603                 } else {
604                         fstrcpy(name_list[ent->num_sam_entries + i].shell, 
605                                 info[i].shell); 
606                 }
607         
608         
609                 /* User and group ids */
610                 sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
611                          &info[i].user_sid);
612                 sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
613                          &info[i].group_sid);
614         }
615                 
616         ent->num_sam_entries += num_entries;
617         
618         /* Fill in remaining fields */
619         
620         ent->sam_entries = name_list;
621         ent->sam_entry_index = 0;
622         return ent->num_sam_entries > 0;
623 }
624
625 /* Fetch next passwd entry from ntdom database */
626
627 #define MAX_GETPWENT_USERS 500
628
629 void winbindd_getpwent(struct winbindd_cli_state *state)
630 {
631         struct getent_state *ent;
632         struct winbindd_pw *user_list;
633         int num_users, user_list_ndx = 0, i;
634
635         DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
636
637         /* Check user has enabled this */
638
639         if (!lp_winbind_enum_users()) {
640                 request_error(state);
641                 return;
642         }
643
644         /* Allocate space for returning a chunk of users */
645
646         num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
647         
648         if ((state->response.extra_data.data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
649                 request_error(state);
650                 return;
651         }
652
653         memset(state->response.extra_data.data, 0, num_users * 
654                sizeof(struct winbindd_pw));
655
656         user_list = (struct winbindd_pw *)state->response.extra_data.data;
657
658         if (!state->getpwent_initialized)
659                 winbindd_setpwent_internal(state);
660         
661         if (!(ent = state->getpwent_state)) {
662                 request_error(state);
663                 return;
664         }
665
666         /* Start sending back users */
667
668         for (i = 0; i < num_users; i++) {
669                 struct getpwent_user *name_list = NULL;
670                 uint32 result;
671
672                 /* Do we need to fetch another chunk of users? */
673
674                 if (ent->num_sam_entries == ent->sam_entry_index) {
675
676                         while(ent &&
677                               !get_sam_user_entries(ent, state->mem_ctx)) {
678                                 struct getent_state *next_ent;
679
680                                 /* Free state information for this domain */
681
682                                 SAFE_FREE(ent->sam_entries);
683
684                                 next_ent = ent->next;
685                                 DLIST_REMOVE(state->getpwent_state, ent);
686
687                                 SAFE_FREE(ent);
688                                 ent = next_ent;
689                         }
690  
691                         /* No more domains */
692
693                         if (!ent) 
694                                 break;
695                 }
696
697                 name_list = (struct getpwent_user *)ent->sam_entries;
698
699                 /* Lookup user info */
700                 
701                 result = winbindd_fill_pwent(
702                         ent->domain_name, 
703                         name_list[ent->sam_entry_index].name,
704                         &name_list[ent->sam_entry_index].user_sid,
705                         &name_list[ent->sam_entry_index].group_sid,
706                         name_list[ent->sam_entry_index].gecos,
707                         name_list[ent->sam_entry_index].homedir,
708                         name_list[ent->sam_entry_index].shell,
709                         &user_list[user_list_ndx]);
710                 
711                 ent->sam_entry_index++;
712                 
713                 /* Add user to return list */
714                 
715                 if (result) {
716                                 
717                         user_list_ndx++;
718                         state->response.data.num_entries++;
719                         state->response.length += 
720                                 sizeof(struct winbindd_pw);
721
722                 } else
723                         DEBUG(1, ("could not lookup domain user %s\n",
724                                   name_list[ent->sam_entry_index].name));
725         }
726
727         /* Out of domains */
728
729         if (user_list_ndx > 0)
730                 request_ok(state);
731         else
732                 request_error(state);
733 }
734
735 /* List domain users without mapping to unix ids */
736
737 void winbindd_list_users(struct winbindd_cli_state *state)
738 {
739         struct winbindd_domain *domain;
740         WINBIND_USERINFO *info;
741         const char *which_domain;
742         uint32 num_entries = 0, total_entries = 0;
743         char *extra_data = NULL;
744         int extra_data_len = 0;
745         enum winbindd_result rv = WINBINDD_ERROR;
746
747         DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
748
749         /* Ensure null termination */
750         state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
751         which_domain = state->request.domain_name;
752         
753         /* Enumerate over trusted domains */
754
755         for (domain = domain_list(); domain; domain = domain->next) {
756                 NTSTATUS status;
757                 struct winbindd_methods *methods;
758                 unsigned int i;
759                 
760                 /* if we have a domain name restricting the request and this
761                    one in the list doesn't match, then just bypass the remainder
762                    of the loop */
763                    
764                 if ( *which_domain && !strequal(which_domain, domain->name) )
765                         continue;
766                         
767                 methods = domain->methods;
768
769                 /* Query display info */
770                 status = methods->query_user_list(domain, state->mem_ctx, 
771                                                   &num_entries, &info);
772
773                 if (!NT_STATUS_IS_OK(status)) {
774                         continue;
775                 }
776
777                 if (num_entries == 0)
778                         continue;
779
780                 /* Allocate some memory for extra data */
781                 total_entries += num_entries;
782                         
783                 extra_data = (char *)SMB_REALLOC(
784                         extra_data, sizeof(fstring) * total_entries);
785                         
786                 if (!extra_data) {
787                         DEBUG(0,("failed to enlarge buffer!\n"));
788                         goto done;
789                 }
790
791                 /* Pack user list into extra data fields */
792                         
793                 for (i = 0; i < num_entries; i++) {
794                         fstring acct_name, name;
795                         
796                         if (!info[i].acct_name) {
797                                 fstrcpy(acct_name, "");
798                         } else {
799                                 fstrcpy(acct_name, info[i].acct_name);
800                         }
801                         
802                         fill_domain_username(name, domain->name, acct_name, True);
803                         
804                                 /* Append to extra data */
805                         memcpy(&extra_data[extra_data_len], name, 
806                                strlen(name));
807                         extra_data_len += strlen(name);
808                         extra_data[extra_data_len++] = ',';
809                 }   
810         }
811
812         /* Assign extra_data fields in response structure */
813
814         if (extra_data) {
815                 extra_data[extra_data_len - 1] = '\0';
816                 state->response.extra_data.data = extra_data;
817                 state->response.length += extra_data_len;
818         }
819
820         /* No domains responded but that's still OK so don't return an
821            error. */
822
823         rv = WINBINDD_OK;
824
825  done:
826
827         if (rv == WINBINDD_OK)
828                 request_ok(state);
829         else
830                 request_error(state);
831 }