summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_passdb.c
blob: 101854ae943d56417771016bd2afca4e0b56b0b9 (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
/* 
   Unix SMB/CIFS implementation.

   Winbind rpc backend functions

   Copyright (C) Tim Potter 2000-2001,2003
   Copyright (C) Simo Sorce 2003
   Copyright (C) Volker Lendecke 2004
   Copyright (C) Jeremy Allison 2008
   
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

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

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

static NTSTATUS enum_groups_internal(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries, 
				struct acct_info **info,
				enum lsa_SidType sidtype)
{
	struct pdb_search *search;
	struct samr_displayentry *entries;
	int i;
	NTSTATUS result = NT_STATUS_UNSUCCESSFUL;

	if (sidtype == SID_NAME_ALIAS) {
		search = pdb_search_aliases(&domain->sid);
	} else {
		search = pdb_search_groups();
	}

	if (search == NULL) goto done;

	*num_entries = pdb_search_entries(search, 0, 0xffffffff, &entries);
	if (*num_entries == 0) {
		/* Zero entries isn't an error */
		result = NT_STATUS_OK;
		goto done;
	}

	*info = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
	if (*info == NULL) {
		result = NT_STATUS_NO_MEMORY;
		goto done;
	}

	for (i=0; i<*num_entries; i++) {
		fstrcpy((*info)[i].acct_name, entries[i].account_name);
		fstrcpy((*info)[i].acct_desc, entries[i].description);
		(*info)[i].rid = entries[i].rid;
	}

	result = NT_STATUS_OK;
 done:
	pdb_search_destroy(search);
	return result;
}

/* List all local groups (aliases) */
static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries, 
				struct acct_info **info)
{
	return enum_groups_internal(domain,
				mem_ctx,
				num_entries,
				info,
				SID_NAME_ALIAS);
}

/* convert a single name to a sid in a domain */
static NTSTATUS name_to_sid(struct winbindd_domain *domain,
			    TALLOC_CTX *mem_ctx,
			    enum winbindd_cmd original_cmd,
			    const char *domain_name,
			    const char *name,
			    DOM_SID *sid,
			    enum lsa_SidType *type)
{
	const char *fullname;
	uint32 flags = LOOKUP_NAME_ALL;

	switch ( original_cmd ) {
	case WINBINDD_LOOKUPNAME:
		/* This call is ok */
		break;
	default:
		/* Avoid any NSS calls in the lookup_name by default */
		flags |= LOOKUP_NAME_EXPLICIT;
		DEBUG(10,("winbindd_passdb: limiting name_to_sid() to explicit mappings\n"));
		break;
	}
	
	if (domain_name && domain_name[0] && strchr_m(name, '\\') == NULL) {
		fullname = talloc_asprintf(mem_ctx, "%s\\%s",
				domain_name, name);
		if (fullname == NULL) {
			return NT_STATUS_NO_MEMORY;
		}
	} else {
		fullname = name;
	}

	DEBUG(10, ("Finding fullname %s\n", fullname));

	if ( !lookup_name( mem_ctx, fullname, flags, NULL, NULL, sid, type ) ) {
		return NT_STATUS_NONE_MAPPED;
	}

	DEBUG(10, ("name_to_sid for %s returned %s (%s)\n",
		fullname,
		sid_string_dbg(sid),
		sid_type_lookup((uint32)*type)));
		
	return NT_STATUS_OK;
}

/*
  convert a domain SID to a user or group name
*/
static NTSTATUS sid_to_name(struct winbindd_domain *domain,
			    TALLOC_CTX *mem_ctx,
			    const DOM_SID *sid,
			    char **domain_name,
			    char **name,
			    enum lsa_SidType *type)
{
	const char *dom, *nam;

	DEBUG(10, ("Converting SID %s\n", sid_string_dbg(sid)));

	/* Paranoia check */
	if (!sid_check_is_in_builtin(sid) &&
	    !sid_check_is_in_our_domain(sid) &&
	    !sid_check_is_in_unix_users(sid) &&
	    !sid_check_is_unix_users(sid) &&
	    !sid_check_is_in_unix_groups(sid) &&
	    !sid_check_is_unix_groups(sid) &&
	    !sid_check_is_in_wellknown_domain(sid))
	{
		DEBUG(0, ("Possible deadlock: Trying to lookup SID %s with "
			  "passdb backend\n", sid_string_dbg(sid)));
		return NT_STATUS_NONE_MAPPED;
	}

	if (!lookup_sid(mem_ctx, sid, &dom, &nam, type)) {
		return NT_STATUS_NONE_MAPPED;
	}

	*domain_name = talloc_strdup(mem_ctx, dom);
	*name = talloc_strdup(mem_ctx, nam);

	return NT_STATUS_OK;
}

static NTSTATUS rids_to_names(struct winbindd_domain *domain,
			      TALLOC_CTX *mem_ctx,
			      const DOM_SID *sid,
			      uint32 *rids,
			      size_t num_rids,
			      char **domain_name,
			      char ***names,
			      enum lsa_SidType **types)
{
	size_t i;
	bool have_mapped;
	bool have_unmapped;

	*domain_name = NULL;
	*names = NULL;
	*types = NULL;

	if (!num_rids) {
		return NT_STATUS_OK;
	}

	/* Paranoia check */
	if (!sid_check_is_in_builtin(sid) &&
	    !sid_check_is_in_our_domain(sid) &&
	    !sid_check_is_in_unix_users(sid) &&
	    !sid_check_is_unix_users(sid) &&
	    !sid_check_is_in_unix_groups(sid) &&
	    !sid_check_is_unix_groups(sid) &&
	    !sid_check_is_in_wellknown_domain(sid))
	{
		DEBUG(0, ("Possible deadlock: Trying to lookup SID %s with "
			  "passdb backend\n", sid_string_dbg(sid)));
		return NT_STATUS_NONE_MAPPED;
	}

	*names = TALLOC_ARRAY(mem_ctx, char *, num_rids);
	*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_rids);

	if ((*names == NULL) || (*types == NULL)) {
		return NT_STATUS_NO_MEMORY;
	}

	have_mapped = have_unmapped = false;

	for (i=0; i<num_rids; i++) {
		DOM_SID lsid;
		const char *dom = NULL, *nam = NULL;
		enum lsa_SidType type = SID_NAME_UNKNOWN;

		if (!sid_compose(&lsid, sid, rids[i])) {
			return NT_STATUS_INTERNAL_ERROR;
		}

		if (!lookup_sid(mem_ctx, &lsid, &dom, &nam, &type)) {
			have_unmapped = true;
			(*types)[i] = SID_NAME_UNKNOWN;
			(*names)[i] = talloc_strdup(mem_ctx, "");
		} else {
			have_mapped = true;
			(*types)[i] = type;
			(*names)[i] = CONST_DISCARD(char *, nam);
		}

		if (*domain_name == NULL) {
			*domain_name = CONST_DISCARD(char *, dom);
		} else {
			char *dname = CONST_DISCARD(char *, dom);
			TALLOC_FREE(dname);
		}
	}

	if (!have_mapped) {
		return NT_STATUS_NONE_MAPPED;
	}
	if (!have_unmapped) {
		return NT_STATUS_OK;
	}
	return STATUS_SOME_UNMAPPED;
}

/* Lookup groups a user is a member of.  I wish Unix had a call like this! */
static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
				  TALLOC_CTX *mem_ctx,
				  const DOM_SID *user_sid,
				  uint32 *num_groups, DOM_SID **user_gids)
{
	NTSTATUS result;
	DOM_SID *groups = NULL;
	gid_t *gids = NULL;
	size_t ngroups = 0;
	struct samu *user;

	if ( (user = samu_new(mem_ctx)) == NULL ) {
		return NT_STATUS_NO_MEMORY;
	}

	if ( !pdb_getsampwsid( user, user_sid ) ) {
		TALLOC_FREE( user );
		return NT_STATUS_NO_SUCH_USER;
	}

	result = pdb_enum_group_memberships( mem_ctx, user, &groups, &gids, &ngroups );

	TALLOC_FREE( user );

	*num_groups = (uint32)ngroups;
	*user_gids = groups;

	return result;
}

static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
				   TALLOC_CTX *mem_ctx,
				   uint32 num_sids, const DOM_SID *sids,
				   uint32 *p_num_aliases, uint32 **rids)
{
	NTSTATUS result;
	size_t num_aliases = 0;

	result = pdb_enum_alias_memberships(mem_ctx, &domain->sid,
					    sids, num_sids, rids, &num_aliases);

	*p_num_aliases = num_aliases;
	return result;
}

/* find the sequence number for a domain */
static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
{
	bool result;
	time_t seq_num;

	result = pdb_get_seq_num(&seq_num);
	if (!result) {
		*seq = 1;
	}

	*seq = (int) seq_num;
	/* *seq = 1; */
	return NT_STATUS_OK;
}

static NTSTATUS lockout_policy(struct winbindd_domain *domain,
			       TALLOC_CTX *mem_ctx,
			       struct samr_DomInfo12 *policy)
{
	/* actually we have that */
	return NT_STATUS_NOT_IMPLEMENTED;
}

static NTSTATUS password_policy(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				struct samr_DomInfo1 *policy)
{
	uint32 min_pass_len,pass_hist,password_properties;
	time_t u_expire, u_min_age;
	NTTIME nt_expire, nt_min_age;
	uint32 account_policy_temp;

	if ((policy = TALLOC_ZERO_P(mem_ctx, struct samr_DomInfo1)) == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (!pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp)) {
		return NT_STATUS_ACCESS_DENIED;
	}
	min_pass_len = account_policy_temp;

	if (!pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp)) {
		return NT_STATUS_ACCESS_DENIED;
	}
	pass_hist = account_policy_temp;

	if (!pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp)) {
		return NT_STATUS_ACCESS_DENIED;
	}
	password_properties = account_policy_temp;
	
	if (!pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp)) {
		return NT_STATUS_ACCESS_DENIED;
	}
	u_expire = account_policy_temp;

	if (!pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp)) {
		return NT_STATUS_ACCESS_DENIED;
	}
	u_min_age = account_policy_temp;

	unix_to_nt_time_abs(&nt_expire, u_expire);
	unix_to_nt_time_abs(&nt_min_age, u_min_age);

	init_samr_DomInfo1(policy,
			   (uint16)min_pass_len,
			   (uint16)pass_hist,
			   password_properties,
			   nt_expire,
			   nt_min_age);

	return NT_STATUS_OK;
}

/*********************************************************************
 BUILTIN specific functions.
*********************************************************************/

/* list all domain groups */
static NTSTATUS builtin_enum_dom_groups(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries, 
				struct acct_info **info)
{
	/* BUILTIN doesn't have domain groups */
	*num_entries = 0;
	*info = NULL;
	return NT_STATUS_OK;
}

/* Query display info for a domain.  This returns enough information plus a
   bit extra to give an overview of domain users for the User Manager
   application. */
static NTSTATUS builtin_query_user_list(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries,
				WINBIND_USERINFO **info)
{
	/* We don't have users */
	*num_entries = 0;
	*info = NULL;
	return NT_STATUS_OK;
}

/* Lookup user information from a rid or username. */
static NTSTATUS builtin_query_user(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				const DOM_SID *user_sid,
				WINBIND_USERINFO *user_info)
{
	return NT_STATUS_NO_SUCH_USER;
}

static NTSTATUS builtin_lookup_groupmem(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				const DOM_SID *group_sid, uint32 *num_names,
				DOM_SID **sid_mem, char ***names,
				uint32 **name_types)
{
	*num_names = 0;
	*sid_mem = NULL;
	*names = NULL;
	*name_types = 0;
	return NT_STATUS_NO_SUCH_GROUP;
}

/* get a list of trusted domains - builtin domain */
static NTSTATUS builtin_trusted_domains(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_domains,
				char ***names,
				char ***alt_names,
				DOM_SID **dom_sids)
{
	*num_domains = 0;
	*names = NULL;
	*alt_names = NULL;
	*dom_sids = NULL;
	return NT_STATUS_OK;
}

/*********************************************************************
 SAM specific functions.
*********************************************************************/

/* list all domain groups */
static NTSTATUS sam_enum_dom_groups(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries, 
				struct acct_info **info)
{
	return enum_groups_internal(domain,
				mem_ctx,
				num_entries,
				info,
				SID_NAME_DOM_GRP);
}

static NTSTATUS sam_query_user_list(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_entries,
				WINBIND_USERINFO **info)
{
	struct pdb_search *ps = pdb_search_users(ACB_NORMAL);
	struct samr_displayentry *entries = NULL;
	uint32 i;

	*num_entries = 0;
	*info = NULL;

	if (!ps) {
		return NT_STATUS_NO_MEMORY;
	}

	*num_entries = pdb_search_entries(ps,
					1, 0xffffffff,
					&entries);

	*info = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, *num_entries);
	if (!(*info)) {
		pdb_search_destroy(ps);
		return NT_STATUS_NO_MEMORY;
	}

	for (i = 0; i < *num_entries; i++) {
		struct samr_displayentry *e = &entries[i];

		(*info)[i].acct_name = talloc_strdup(mem_ctx, e->account_name );
		(*info)[i].full_name = talloc_strdup(mem_ctx, e->fullname );
		(*info)[i].homedir = NULL;
		(*info)[i].shell = NULL;
		sid_compose(&(*info)[i].user_sid, &domain->sid, e->rid);

		/* For the moment we set the primary group for
		   every user to be the Domain Users group.
		   There are serious problems with determining
		   the actual primary group for large domains.
		   This should really be made into a 'winbind
		   force group' smb.conf parameter or
		   something like that. */

		sid_compose(&(*info)[i].group_sid, &domain->sid,
				DOMAIN_GROUP_RID_USERS);
	}

	pdb_search_destroy(ps);
	return NT_STATUS_OK;
}

/* Lookup user information from a rid or username. */
static NTSTATUS sam_query_user(struct winbindd_domain *domain,
                           TALLOC_CTX *mem_ctx,
                           const DOM_SID *user_sid,
                           WINBIND_USERINFO *user_info)
{
	struct samu *sampass = NULL;

	ZERO_STRUCTP(user_info);

	if (!sid_check_is_in_our_domain(user_sid)) {
		return NT_STATUS_NO_SUCH_USER;
	}

	DEBUG(10,("sam_query_user: getting samu info for sid %s\n",
		sid_string_dbg(user_sid) ));

	if (!(sampass = samu_new(mem_ctx))) {
		return NT_STATUS_NO_MEMORY;
	}

	if (!pdb_getsampwsid(sampass, user_sid)) {
		TALLOC_FREE(sampass);
		return NT_STATUS_NO_SUCH_USER;
	}

	if (pdb_get_group_sid(sampass) == NULL) {
		TALLOC_FREE(sampass);
		return NT_STATUS_NO_SUCH_GROUP;
	}

	DEBUG(10,("sam_query_user: group sid %s\n",
		sid_string_dbg(sampass->group_sid) ));

	sid_copy(&user_info->user_sid, user_sid);
	sid_copy(&user_info->group_sid, sampass->group_sid);

	user_info->acct_name = talloc_strdup(mem_ctx, sampass->username ?
					sampass->username : "");
	user_info->full_name = talloc_strdup(mem_ctx, sampass->full_name ?
					sampass->full_name : "");
	user_info->homedir = talloc_strdup(mem_ctx, sampass->home_dir ?
					sampass->home_dir : "");
	if (sampass->unix_pw && sampass->unix_pw->pw_shell) {
		user_info->shell = talloc_strdup(mem_ctx, sampass->unix_pw->pw_shell);
	} else {
		user_info->shell = talloc_strdup(mem_ctx, "");
	}
	user_info->primary_gid = sampass->unix_pw ? sampass->unix_pw->pw_gid : (gid_t)-1;

	TALLOC_FREE(sampass);
	return NT_STATUS_OK;
}

/* Lookup group membership given a rid.   */
static NTSTATUS sam_lookup_groupmem(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				const DOM_SID *group_sid, uint32 *num_names, 
				DOM_SID **sid_mem, char ***names, 
				uint32 **name_types)
{
	size_t i, num_members, num_mapped;
	uint32 *rids;
	NTSTATUS result;
	const DOM_SID **sids;
	struct lsa_dom_info *lsa_domains;
	struct lsa_name_info *lsa_names;
	TALLOC_CTX *tmp_ctx;

	if (!sid_check_is_in_our_domain(group_sid)) {
		/* There's no groups, only aliases in BUILTIN */
		return NT_STATUS_NO_SUCH_GROUP;
	}

	if (!(tmp_ctx = talloc_init("lookup_groupmem"))) {
		return NT_STATUS_NO_MEMORY;
	}

	result = pdb_enum_group_members(tmp_ctx, group_sid, &rids,
					&num_members);
	if (!NT_STATUS_IS_OK(result)) {
		TALLOC_FREE(tmp_ctx);
		return result;
	}

	if (num_members == 0) {
		*num_names = 0;
		*sid_mem = NULL;
		*names = NULL;
		*name_types = NULL;
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_OK;
	}

	*sid_mem = TALLOC_ARRAY(mem_ctx, DOM_SID, num_members);
	*names = TALLOC_ARRAY(mem_ctx, char *, num_members);
	*name_types = TALLOC_ARRAY(mem_ctx, uint32, num_members);
	sids = TALLOC_ARRAY(tmp_ctx, const DOM_SID *, num_members);

	if (((*sid_mem) == NULL) || ((*names) == NULL) ||
	    ((*name_types) == NULL) || (sids == NULL)) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	/*
	 * Prepare an array of sid pointers for the lookup_sids calling
	 * convention.
	 */

	for (i=0; i<num_members; i++) {
		DOM_SID *sid = &((*sid_mem)[i]);
		if (!sid_compose(sid, &domain->sid, rids[i])) {
			TALLOC_FREE(tmp_ctx);
			return NT_STATUS_INTERNAL_ERROR;
		}
		sids[i] = sid;
	}

	result = lookup_sids(tmp_ctx, num_members, sids, 1,
			     &lsa_domains, &lsa_names);
	if (!NT_STATUS_IS_OK(result)) {
		TALLOC_FREE(tmp_ctx);
		return result;
	}

	num_mapped = 0;
	for (i=0; i<num_members; i++) {
		if (lsa_names[i].type != SID_NAME_USER) {
			DEBUG(2, ("Got %s as group member -- ignoring\n",
				  sid_type_lookup(lsa_names[i].type)));
			continue;
		}
		if (!((*names)[num_mapped] = talloc_strdup((*names),
						  lsa_names[i].name))) {
			TALLOC_FREE(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}

		(*name_types)[num_mapped] = lsa_names[i].type;

		num_mapped += 1;
	}

	*num_names = num_mapped;

	TALLOC_FREE(tmp_ctx);
	return NT_STATUS_OK;
}

/* get a list of trusted domains */
static NTSTATUS sam_trusted_domains(struct winbindd_domain *domain,
				TALLOC_CTX *mem_ctx,
				uint32 *num_domains,
				char ***names,
				char ***alt_names,
				DOM_SID **dom_sids)
{
	NTSTATUS nt_status;
	struct trustdom_info **domains;
	int i;
	TALLOC_CTX *tmp_ctx;

	*num_domains = 0;
	*names = NULL;
	*alt_names = NULL;
	*dom_sids = NULL;

	if (!(tmp_ctx = talloc_init("trusted_domains"))) {
		return NT_STATUS_NO_MEMORY;
	}

	nt_status = pdb_enum_trusteddoms(tmp_ctx, num_domains, &domains);
	if (!NT_STATUS_IS_OK(nt_status)) {
		TALLOC_FREE(tmp_ctx);
		return nt_status;
	}

	if (*num_domains) {
		*names = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
		*alt_names = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
		*dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_domains);

		if ((*alt_names == NULL) || (*names == NULL) || (*dom_sids == NULL)) {
			TALLOC_FREE(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}
	} else {
		*names = NULL;
		*alt_names = NULL;
		*dom_sids = NULL;
	}

	for (i=0; i<*num_domains; i++) {
		(*alt_names)[i] = NULL;
		if (!((*names)[i] = talloc_strdup((*names),
						  domains[i]->name))) {
			TALLOC_FREE(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}
		sid_copy(&(*dom_sids)[i], &domains[i]->sid);
	}

	TALLOC_FREE(tmp_ctx);
	return NT_STATUS_OK;
}

/* the rpc backend methods are exposed via this structure */
struct winbindd_methods builtin_passdb_methods = {
	false,
	builtin_query_user_list,
	builtin_enum_dom_groups,
	enum_local_groups,
	name_to_sid,
	sid_to_name,
	rids_to_names,
	builtin_query_user,
	lookup_usergroups,
	lookup_useraliases,
	builtin_lookup_groupmem,
	sequence_number,
	lockout_policy,
	password_policy,
	builtin_trusted_domains,
};

/* the rpc backend methods are exposed via this structure */
struct winbindd_methods sam_passdb_methods = {
	false,
	sam_query_user_list,
	sam_enum_dom_groups,
	enum_local_groups,
	name_to_sid,
	sid_to_name,
	rids_to_names,
	sam_query_user,
	lookup_usergroups,
	lookup_useraliases,
	sam_lookup_groupmem,
	sequence_number,
	lockout_policy,
	password_policy,
	sam_trusted_domains,
};