summaryrefslogtreecommitdiff
path: root/source3/nsswitch/winbindd_user.c
blob: 0b88d5eee5f760893addc799b21cce415f732900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/* 
   Unix SMB/CIFS implementation.

   Winbind daemon - user related functions

   Copyright (C) Tim Potter 2000
   Copyright (C) Jeremy Allison 2001.
   Copyright (C) Gerald (Jerry) Carter 2003.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "winbindd.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

extern userdom_struct current_user_info;

static BOOL fillup_pw_field(const char *lp_template, 
			    const char *username, 
			    const char *domname,
			    uid_t uid,
			    gid_t gid,
			    const char *in, 
			    fstring out)
{
	char *templ;

	if (out == NULL)
		return False;

	if (in && !strequal(in,"") && lp_security() == SEC_ADS && use_nss_info("sfu")) {
		safe_strcpy(out, in, sizeof(fstring) - 1);
		return True;
	}

	/* Home directory and shell - use template config parameters.  The
	   defaults are /tmp for the home directory and /bin/false for
	   shell. */
	
	/* The substitution of %U and %D in the 'template homedir' is done
	   by alloc_sub_specified() below. */

	templ = alloc_sub_specified(lp_template, username, domname, uid, gid);
		
	if (!templ)
		return False;

	safe_strcpy(out, templ, sizeof(fstring) - 1);
	SAFE_FREE(templ);
		
	return True;
	
}
/* Fill a pwent structure with information we have obtained */

static BOOL winbindd_fill_pwent(char *dom_name, char *user_name, 
				DOM_SID *user_sid, DOM_SID *group_sid,
				char *full_name, char *homedir, char *shell,
				struct winbindd_pw *pw)
{
	fstring output_username;
	fstring sid_string;
	
	if (!pw || !dom_name || !user_name)
		return False;
	
	/* Resolve the uid number */

	if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &pw->pw_uid, 0))) {
		DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
		return False;
	}
	
	/* Resolve the gid number */   

	if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &pw->pw_gid, 0))) {
		DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
		return False;
	}

	strlower_m(user_name);

	/* Username */

	fill_domain_username(output_username, dom_name, user_name); 

	safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
	
	/* Full name (gecos) */
	
	safe_strcpy(pw->pw_gecos, full_name, sizeof(pw->pw_gecos) - 1);

	/* Home directory and shell - use template config parameters.  The
	   defaults are /tmp for the home directory and /bin/false for
	   shell. */
	
	/* The substitution of %U and %D in the 'template homedir' is done
	   by alloc_sub_specified() below. */

	fstrcpy(current_user_info.domain, dom_name);

	if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name, 
			     pw->pw_uid, pw->pw_gid, homedir, pw->pw_dir))
		return False;

	if (!fillup_pw_field(lp_template_shell(), user_name, dom_name, 
			     pw->pw_uid, pw->pw_gid, shell, pw->pw_shell))
		return False;

	/* Password - set to "x" as we can't generate anything useful here.
	   Authentication can be done using the pam_winbind module. */

	safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);

	return True;
}

/* Wrapper for domain->methods->query_user, only on the parent->child pipe */

enum winbindd_result winbindd_dual_userinfo(struct winbindd_domain *domain,
					    struct winbindd_cli_state *state)
{
	DOM_SID sid;
	WINBIND_USERINFO user_info;
	NTSTATUS status;

	/* Ensure null termination */
	state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';

	DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
		  state->request.data.sid));

	if (!string_to_sid(&sid, state->request.data.sid)) {
		DEBUG(5, ("%s not a SID\n", state->request.data.sid));
		return WINBINDD_ERROR;
	}

	status = domain->methods->query_user(domain, state->mem_ctx,
					     &sid, &user_info);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(1, ("error getting user info for sid %s\n",
			  sid_string_static(&sid)));
		return WINBINDD_ERROR;
	}

	fstrcpy(state->response.data.user_info.acct_name, user_info.acct_name);
	fstrcpy(state->response.data.user_info.full_name, user_info.full_name);
	fstrcpy(state->response.data.user_info.homedir, user_info.homedir);
	fstrcpy(state->response.data.user_info.shell, user_info.shell);
	if (!sid_peek_check_rid(&domain->sid, &user_info.group_sid,
				&state->response.data.user_info.group_rid)) {
		DEBUG(1, ("Could not extract group rid out of %s\n",
			  sid_string_static(&sid)));
		return WINBINDD_ERROR;
	}

	return WINBINDD_OK;
}

struct getpwsid_state {
	struct winbindd_cli_state *state;
	struct winbindd_domain *domain;
	char *username;
	char *fullname;
	char *homedir;
	char *shell;
	DOM_SID user_sid;
	uid_t uid;
	DOM_SID group_sid;
	gid_t gid;
};

static void getpwsid_queryuser_recv(void *private_data, BOOL success,
				    const char *acct_name,
				    const char *full_name, 
				    const char *homedir,
				    const char *shell,
				    uint32 group_rid);
static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid);
static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid);

static void winbindd_getpwsid(struct winbindd_cli_state *state,
			      const DOM_SID *sid)
{
	struct getpwsid_state *s;

	s = TALLOC_P(state->mem_ctx, struct getpwsid_state);
	if (s == NULL) {
		DEBUG(0, ("talloc failed\n"));
		goto error;
	}

	s->state = state;
	s->domain = find_domain_from_sid_noinit(sid);
	if (s->domain == NULL) {
		DEBUG(3, ("Could not find domain for sid %s\n",
			  sid_string_static(sid)));
		goto error;
	}

	sid_copy(&s->user_sid, sid);

	query_user_async(s->state->mem_ctx, s->domain, sid,
			 getpwsid_queryuser_recv, s);
	return;

 error:
	request_error(state);
}
	
static void getpwsid_queryuser_recv(void *private_data, BOOL success,
				    const char *acct_name,
				    const char *full_name, 
				    const char *homedir,
				    const char *shell,
				    uint32 group_rid)
{
	fstring username;
	struct getpwsid_state *s =
		talloc_get_type_abort(private_data, struct getpwsid_state);

	if (!success) {
		DEBUG(5, ("Could not query user %s\\%s\n", s->domain->name,
			  s->username));
		request_error(s->state);
		return;
	}

	fstrcpy( username, acct_name );
	strlower_m( username );
	s->username = talloc_strdup(s->state->mem_ctx, username);
	s->fullname = talloc_strdup(s->state->mem_ctx, full_name);
	s->homedir = talloc_strdup(s->state->mem_ctx, homedir);
	s->shell = talloc_strdup(s->state->mem_ctx, shell);
	sid_copy(&s->group_sid, &s->domain->sid);
	sid_append_rid(&s->group_sid, group_rid);

	winbindd_sid2uid_async(s->state->mem_ctx, &s->user_sid,
			       getpwsid_sid2uid_recv, s);
}

static void getpwsid_sid2uid_recv(void *private_data, BOOL success, uid_t uid)
{
	struct getpwsid_state *s =
		talloc_get_type_abort(private_data, struct getpwsid_state);

	if (!success) {
		DEBUG(5, ("Could not query user's %s\\%s uid\n",
			  s->domain->name, s->username));
		request_error(s->state);
		return;
	}

	s->uid = uid;
	winbindd_sid2gid_async(s->state->mem_ctx, &s->group_sid,
			       getpwsid_sid2gid_recv, s);
}

static void getpwsid_sid2gid_recv(void *private_data, BOOL success, gid_t gid)
{
	struct getpwsid_state *s =
		talloc_get_type_abort(private_data, struct getpwsid_state);
	struct winbindd_pw *pw;
	fstring output_username;

	if (!success) {
		DEBUG(5, ("Could not query user's %s\\%s\n gid",
			  s->domain->name, s->username));
		goto failed;
	}

	s->gid = gid;

	pw = &s->state->response.data.pw;
	pw->pw_uid = s->uid;
	pw->pw_gid = s->gid;
	fill_domain_username(output_username, s->domain->name, s->username); 
	safe_strcpy(pw->pw_name, output_username, sizeof(pw->pw_name) - 1);
	safe_strcpy(pw->pw_gecos, s->fullname, sizeof(pw->pw_gecos) - 1);

	fstrcpy(current_user_info.domain, s->domain->name);

	if (!fillup_pw_field(lp_template_homedir(), s->username, s->domain->name, 
			     pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir)) {
		DEBUG(5, ("Could not compose homedir\n"));
		goto failed;
	}

	if (!fillup_pw_field(lp_template_shell(), s->username, s->domain->name, 
			     pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell)) {
		DEBUG(5, ("Could not compose shell\n"));
		goto failed;
	}

	/* Password - set to "x" as we can't generate anything useful here.
	   Authentication can be done using the pam_winbind module. */

	safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);

	request_ok(s->state);
	return;

 failed:
	request_error(s->state);
}

/* Return a password structure from a username.  */

static void getpwnam_name2sid_recv(void *private_data, BOOL success,
				   const DOM_SID *sid, enum SID_NAME_USE type);

void winbindd_getpwnam(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain;
	fstring domname, username;

	/* Ensure null termination */
	state->request.data.username[sizeof(state->request.data.username)-1]='\0';

	DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
		  state->request.data.username));

	if (!parse_domain_user(state->request.data.username, domname,
			       username)) {
		DEBUG(0, ("Could not parse domain user: %s\n",
			  state->request.data.username));
		request_error(state);
		return;
	}
	
	/* Get info for the domain */

	domain = find_domain_from_name(domname);

	if (domain == NULL) {
		DEBUG(7, ("could not find domain entry for domain %s\n",
			  domname));
		request_error(state);
		return;
	}

	if ( strequal(domname, lp_workgroup()) && lp_winbind_trusted_domains_only() ) {
		DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n", 
			domname, username));
		request_error(state);
		return;
	}	

	/* Get rid and name type from name.  The following costs 1 packet */

	winbindd_lookupname_async(state->mem_ctx, domname, username,
				  getpwnam_name2sid_recv, state);
}

static void getpwnam_name2sid_recv(void *private_data, BOOL success,
				   const DOM_SID *sid, enum SID_NAME_USE type)
{
	struct winbindd_cli_state *state = private_data;

	if (!success) {
		DEBUG(5, ("Could not lookup name for user %s\n",
			  state->request.data.username));
		request_error(state);
		return;
	}

	if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
		DEBUG(5, ("%s is not a user\n", state->request.data.username));
		request_error(state);
		return;
	}

	winbindd_getpwsid(state, sid);
}

/* Return a password structure given a uid number */

void winbindd_getpwuid(struct winbindd_cli_state *state)
{
	DOM_SID user_sid;
	NTSTATUS status;
	
	/* Bug out if the uid isn't in the winbind range */

	if ((state->request.data.uid < server_state.uid_low ) ||
	    (state->request.data.uid > server_state.uid_high)) {
		request_error(state);
		return;
	}

	DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
		  (unsigned long)state->request.data.uid));

	status = idmap_uid_to_sid(&user_sid, state->request.data.uid,
				  ID_QUERY_ONLY | ID_CACHE_ONLY);

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(5, ("Could not find SID for uid %lu\n",
			  (unsigned long)state->request.data.uid));
		request_error(state);
		return;
	}

	winbindd_getpwsid(state, &user_sid);
}

/*
 * set/get/endpwent functions
 */

/* Rewind file pointer for ntdom passwd database */

static BOOL winbindd_setpwent_internal(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain;
        
	DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
        
	/* Check user has enabled this */
        
	if (!lp_winbind_enum_users()) {
		return False;
	}

	/* Free old static data if it exists */
        
	if (state->getpwent_state != NULL) {
		free_getent_state(state->getpwent_state);
		state->getpwent_state = NULL;
	}

#if 0	/* JERRY */
	/* add any local users we have */
	        
	if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
		return False;
                
	ZERO_STRUCTP(domain_state);

	/* Add to list of open domains */
                
	DLIST_ADD(state->getpwent_state, domain_state);
#endif
        
	/* Create sam pipes for each domain we know about */
        
	for(domain = domain_list(); domain != NULL; domain = domain->next) {
		struct getent_state *domain_state;
                
		
		/* don't add our domaina if we are a PDC or if we 
		   are a member of a Samba domain */
		
		if ( (IS_DC || lp_winbind_trusted_domains_only())
			&& strequal(domain->name, lp_workgroup()) )
		{
			continue;
		}
						
		/* Create a state record for this domain */
                
		if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL) {
			DEBUG(0, ("malloc failed\n"));
			return False;
		}
                
		ZERO_STRUCTP(domain_state);

		fstrcpy(domain_state->domain_name, domain->name);

		/* Add to list of open domains */
                
		DLIST_ADD(state->getpwent_state, domain_state);
	}
        
	state->getpwent_initialized = True;
	return True;
}

void winbindd_setpwent(struct winbindd_cli_state *state)
{
	if (winbindd_setpwent_internal(state)) {
		request_ok(state);
	} else {
		request_error(state);
	}
}

/* Close file pointer to ntdom passwd database */

void winbindd_endpwent(struct winbindd_cli_state *state)
{
	DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));

	free_getent_state(state->getpwent_state);    
	state->getpwent_initialized = False;
	state->getpwent_state = NULL;
	request_ok(state);
}

/* Get partial list of domain users for a domain.  We fill in the sam_entries,
   and num_sam_entries fields with domain user information.  The dispinfo_ndx
   field is incremented to the index of the next user to fetch.  Return True if
   some users were returned, False otherwise. */

static BOOL get_sam_user_entries(struct getent_state *ent, TALLOC_CTX *mem_ctx)
{
	NTSTATUS status;
	uint32 num_entries;
	WINBIND_USERINFO *info;
	struct getpwent_user *name_list = NULL;
	BOOL result = False;
	struct winbindd_domain *domain;
	struct winbindd_methods *methods;
	unsigned int i;

	if (ent->num_sam_entries)
		return False;

	if (!(domain = find_domain_from_name(ent->domain_name))) {
		DEBUG(3, ("no such domain %s in get_sam_user_entries\n",
			  ent->domain_name));
		return False;
	}

	methods = domain->methods;

	/* Free any existing user info */

	SAFE_FREE(ent->sam_entries);
	ent->num_sam_entries = 0;
	
	/* Call query_user_list to get a list of usernames and user rids */

	num_entries = 0;

	status = methods->query_user_list(domain, mem_ctx, &num_entries, 
					  &info);
		
	if (num_entries) {
		struct getpwent_user *tnl;
		
		tnl = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
		
		if (!tnl) {
			DEBUG(0,("get_sam_user_entries realloc failed.\n"));
			SAFE_FREE(name_list);
			goto done;
		} else
			name_list = tnl;
	}

	for (i = 0; i < num_entries; i++) {
		/* Store account name and gecos */
		if (!info[i].acct_name) {
			fstrcpy(name_list[ent->num_sam_entries + i].name, "");
		} else {
			fstrcpy(name_list[ent->num_sam_entries + i].name, 
				info[i].acct_name); 
		}
		if (!info[i].full_name) {
			fstrcpy(name_list[ent->num_sam_entries + i].gecos, "");
		} else {
			fstrcpy(name_list[ent->num_sam_entries + i].gecos, 
				info[i].full_name); 
		}
		if (!info[i].homedir) {
			fstrcpy(name_list[ent->num_sam_entries + i].homedir, "");
		} else {
			fstrcpy(name_list[ent->num_sam_entries + i].homedir, 
				info[i].homedir); 
		}
		if (!info[i].shell) {
			fstrcpy(name_list[ent->num_sam_entries + i].shell, "");
		} else {
			fstrcpy(name_list[ent->num_sam_entries + i].shell, 
				info[i].shell); 
		}
	
	
		/* User and group ids */
		sid_copy(&name_list[ent->num_sam_entries+i].user_sid,
			 &info[i].user_sid);
		sid_copy(&name_list[ent->num_sam_entries+i].group_sid,
			 &info[i].group_sid);
	}
		
	ent->num_sam_entries += num_entries;
	
	/* Fill in remaining fields */
	
	ent->sam_entries = name_list;
	ent->sam_entry_index = 0;
	result = ent->num_sam_entries > 0;

 done:

	return result;
}

/* Fetch next passwd entry from ntdom database */

#define MAX_GETPWENT_USERS 500

void winbindd_getpwent(struct winbindd_cli_state *state)
{
	struct getent_state *ent;
	struct winbindd_pw *user_list;
	int num_users, user_list_ndx = 0, i;

	DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));

	/* Check user has enabled this */

	if (!lp_winbind_enum_users()) {
		request_error(state);
		return;
	}

	/* Allocate space for returning a chunk of users */

	num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
	
	if ((state->response.extra_data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL) {
		request_error(state);
		return;
	}

	memset(state->response.extra_data, 0, num_users * 
	       sizeof(struct winbindd_pw));

	user_list = (struct winbindd_pw *)state->response.extra_data;

	if (!state->getpwent_initialized)
		winbindd_setpwent_internal(state);
	
	if (!(ent = state->getpwent_state)) {
		request_error(state);
		return;
	}

	/* Start sending back users */

	for (i = 0; i < num_users; i++) {
		struct getpwent_user *name_list = NULL;
		uint32 result;

		/* Do we need to fetch another chunk of users? */

		if (ent->num_sam_entries == ent->sam_entry_index) {

			while(ent &&
			      !get_sam_user_entries(ent, state->mem_ctx)) {
				struct getent_state *next_ent;

				/* Free state information for this domain */

				SAFE_FREE(ent->sam_entries);

				next_ent = ent->next;
				DLIST_REMOVE(state->getpwent_state, ent);

				SAFE_FREE(ent);
				ent = next_ent;
			}
 
			/* No more domains */

			if (!ent) 
				break;
		}

		name_list = ent->sam_entries;

		/* Lookup user info */
		
		result = winbindd_fill_pwent(
			ent->domain_name, 
			name_list[ent->sam_entry_index].name,
			&name_list[ent->sam_entry_index].user_sid,
			&name_list[ent->sam_entry_index].group_sid,
			name_list[ent->sam_entry_index].gecos,
			name_list[ent->sam_entry_index].homedir,
			name_list[ent->sam_entry_index].shell,
			&user_list[user_list_ndx]);
		
		ent->sam_entry_index++;
		
		/* Add user to return list */
		
		if (result) {
				
			user_list_ndx++;
			state->response.data.num_entries++;
			state->response.length += 
				sizeof(struct winbindd_pw);

		} else
			DEBUG(1, ("could not lookup domain user %s\n",
				  name_list[ent->sam_entry_index].name));
	}

	/* Out of domains */

	if (user_list_ndx > 0)
		request_ok(state);
	else
		request_error(state);
}

/* List domain users without mapping to unix ids */

void winbindd_list_users(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain;
	WINBIND_USERINFO *info;
	const char *which_domain;
	uint32 num_entries = 0, total_entries = 0;
	char *ted, *extra_data = NULL;
	int extra_data_len = 0;
	enum winbindd_result rv = WINBINDD_ERROR;

	DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));

	/* Ensure null termination */
	state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';	
	which_domain = state->request.domain_name;
	
	/* Enumerate over trusted domains */

	for (domain = domain_list(); domain; domain = domain->next) {
		NTSTATUS status;
		struct winbindd_methods *methods;
		unsigned int i;
		
		/* if we have a domain name restricting the request and this
		   one in the list doesn't match, then just bypass the remainder
		   of the loop */
		   
		if ( *which_domain && !strequal(which_domain, domain->name) )
			continue;
			
		methods = domain->methods;

		/* Query display info */
		status = methods->query_user_list(domain, state->mem_ctx, 
						  &num_entries, &info);

		if (num_entries == 0)
			continue;

		/* Allocate some memory for extra data */
		total_entries += num_entries;
			
		ted = SMB_REALLOC(extra_data, sizeof(fstring) * total_entries);
			
		if (!ted) {
			DEBUG(0,("failed to enlarge buffer!\n"));
			SAFE_FREE(extra_data);
			goto done;
		} else 
			extra_data = ted;
			
		/* Pack user list into extra data fields */
			
		for (i = 0; i < num_entries; i++) {
			fstring acct_name, name;
			
			if (!info[i].acct_name) {
				fstrcpy(acct_name, "");
			} else {
				fstrcpy(acct_name, info[i].acct_name);
			}
			
			fill_domain_username(name, domain->name, acct_name);
			
				/* Append to extra data */
			memcpy(&extra_data[extra_data_len], name, 
			       strlen(name));
			extra_data_len += strlen(name);
			extra_data[extra_data_len++] = ',';
		}   
        }

	/* Assign extra_data fields in response structure */

	if (extra_data) {
		extra_data[extra_data_len - 1] = '\0';
		state->response.extra_data = extra_data;
		state->response.length += extra_data_len;
	}

	/* No domains responded but that's still OK so don't return an
	   error. */

	rv = WINBINDD_OK;

 done:

	if (rv == WINBINDD_OK)
		request_ok(state);
	else
		request_error(state);
}