Make memcache_add_talloc NULL out the source pointer
[kai/samba-autobuild/.git] / source3 / passdb / pdb_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Jelmer Vernooij                        2002
6    Copyright (C) Simo Sorce                             2003
7    Copyright (C) Volker Lendecke                        2006
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
27
28 static_decl_pdb;
29
30 static struct pdb_init_function_entry *backends = NULL;
31
32 static void lazy_initialize_passdb(void)
33 {
34         static bool initialized = False;
35         if(initialized) {
36                 return;
37         }
38         static_init_pdb;
39         initialized = True;
40 }
41
42 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
43                                   const char **name,
44                                   enum lsa_SidType *psid_name_use,
45                                   union unid_t *unix_id);
46
47 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
48 {
49         struct pdb_init_function_entry *entry = backends;
50
51         if(version != PASSDB_INTERFACE_VERSION) {
52                 DEBUG(0,("Can't register passdb backend!\n"
53                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
54                          "while this version of samba uses version %d\n", 
55                          version,PASSDB_INTERFACE_VERSION));
56                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
57         }
58
59         if (!name || !init) {
60                 return NT_STATUS_INVALID_PARAMETER;
61         }
62
63         DEBUG(5,("Attempting to register passdb backend %s\n", name));
64
65         /* Check for duplicates */
66         if (pdb_find_backend_entry(name)) {
67                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
68                 return NT_STATUS_OBJECT_NAME_COLLISION;
69         }
70
71         entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
72         entry->name = smb_xstrdup(name);
73         entry->init = init;
74
75         DLIST_ADD(backends, entry);
76         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
77         return NT_STATUS_OK;
78 }
79
80 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
81 {
82         struct pdb_init_function_entry *entry = backends;
83
84         while(entry) {
85                 if (strcmp(entry->name, name)==0) return entry;
86                 entry = entry->next;
87         }
88
89         return NULL;
90 }
91
92 /*
93  * The event context for the passdb backend. I know this is a bad hack and yet
94  * another static variable, but our pdb API is a global thing per
95  * definition. The first use for this is the LDAP idle function, more might be
96  * added later.
97  *
98  * I don't feel too bad about this static variable, it replaces the
99  * smb_idle_event_list that used to exist in lib/module.c.  -- VL
100  */
101
102 static struct event_context *pdb_event_ctx;
103
104 struct event_context *pdb_get_event_context(void)
105 {
106         return pdb_event_ctx;
107 }
108
109 /******************************************************************
110   Make a pdb_methods from scratch
111  *******************************************************************/
112
113 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
114 {
115         char *module_name = smb_xstrdup(selected);
116         char *module_location = NULL, *p;
117         struct pdb_init_function_entry *entry;
118         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
119
120         lazy_initialize_passdb();
121
122         p = strchr(module_name, ':');
123
124         if (p) {
125                 *p = 0;
126                 module_location = p+1;
127                 trim_char(module_location, ' ', ' ');
128         }
129
130         trim_char(module_name, ' ', ' ');
131
132
133         DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
134
135         entry = pdb_find_backend_entry(module_name);
136         
137         /* Try to find a module that contains this module */
138         if (!entry) { 
139                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
140                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
141                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
142                         SAFE_FREE(module_name);
143                         return NT_STATUS_UNSUCCESSFUL;
144                 }
145         }
146         
147         /* No such backend found */
148         if(!entry) { 
149                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
150                 SAFE_FREE(module_name);
151                 return NT_STATUS_INVALID_PARAMETER;
152         }
153
154         DEBUG(5,("Found pdb backend %s\n", module_name));
155
156         if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
157                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", 
158                         selected, nt_errstr(nt_status)));
159                 SAFE_FREE(module_name);
160                 return nt_status;
161         }
162
163         SAFE_FREE(module_name);
164
165         DEBUG(5,("pdb backend %s has a valid init\n", selected));
166
167         return nt_status;
168 }
169
170 /******************************************************************
171  Return an already initialized pdb_methods structure
172 *******************************************************************/
173
174 static struct pdb_methods *pdb_get_methods_reload( bool reload ) 
175 {
176         static struct pdb_methods *pdb = NULL;
177
178         if ( pdb && reload ) {
179                 pdb->free_private_data( &(pdb->private_data) );
180                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
181                         char *msg = NULL;
182                         asprintf(&msg, "pdb_get_methods_reload: "
183                                 "failed to get pdb methods for backend %s\n",
184                                 lp_passdb_backend());
185                         smb_panic(msg);
186                 }
187         }
188
189         if ( !pdb ) {
190                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
191                         char *msg = NULL;
192                         asprintf(&msg, "pdb_get_methods_reload: "
193                                 "failed to get pdb methods for backend %s\n",
194                                 lp_passdb_backend());
195                         smb_panic(msg);
196                 }
197         }
198
199         return pdb;
200 }
201
202 static struct pdb_methods *pdb_get_methods(void)
203 {
204         return pdb_get_methods_reload(False);
205 }
206
207 bool pdb_getsampwnam(struct samu *sam_acct, const char *username) 
208 {
209         struct pdb_methods *pdb = pdb_get_methods();
210         struct samu *for_cache;
211         const struct dom_sid *user_sid;
212
213         if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
214                 return False;
215         }
216
217         for_cache = samu_new(NULL);
218         if (for_cache == NULL) {
219                 return False;
220         }
221
222         if (!pdb_copy_sam_account(for_cache, sam_acct)) {
223                 TALLOC_FREE(for_cache);
224                 return False;
225         }
226
227         user_sid = pdb_get_user_sid(for_cache);
228
229         memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
230                             data_blob_const(user_sid, sizeof(*user_sid)),
231                             &for_cache);
232
233         return True;
234 }
235
236 /**********************************************************************
237 **********************************************************************/
238
239 bool guest_user_info( struct samu *user )
240 {
241         struct passwd *pwd;
242         NTSTATUS result;
243         const char *guestname = lp_guestaccount();
244         
245         if ( !(pwd = getpwnam_alloc(talloc_autofree_context(), guestname ) ) ) {
246                 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n", 
247                         guestname));
248                 return False;
249         }
250         
251         result = samu_set_unix(user, pwd );
252
253         TALLOC_FREE( pwd );
254
255         return NT_STATUS_IS_OK( result );
256 }
257
258 /**********************************************************************
259 **********************************************************************/
260
261 bool pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid) 
262 {
263         struct pdb_methods *pdb = pdb_get_methods();
264         uint32 rid;
265         void *cache_data;
266
267         /* hard code the Guest RID of 501 */
268
269         if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
270                 return False;
271
272         if ( rid == DOMAIN_USER_RID_GUEST ) {
273                 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
274                 return guest_user_info( sam_acct );
275         }
276         
277         /* check the cache first */
278
279         cache_data = memcache_lookup_talloc(
280                 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
281
282         if (cache_data != NULL) {
283                 struct samu *cache_copy = talloc_get_type_abort(
284                         cache_data, struct samu);
285
286                 return pdb_copy_sam_account(sam_acct, cache_copy);
287         }
288
289         return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
290 }
291
292 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
293                                         TALLOC_CTX *tmp_ctx, const char *name,
294                                         uint32 acb_info, uint32 *rid)
295 {
296         struct samu *sam_pass;
297         NTSTATUS status;
298         struct passwd *pwd;
299
300         if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
301                 return NT_STATUS_NO_MEMORY;
302         }
303
304         if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
305                 char *add_script = NULL;
306                 int add_ret;
307                 fstring name2;
308
309                 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
310                         add_script = talloc_strdup(tmp_ctx,
311                                         lp_adduser_script());
312                 } else {
313                         add_script = talloc_strdup(tmp_ctx,
314                                         lp_addmachine_script());
315                 }
316
317                 if (!add_script || add_script[0] == '\0') {
318                         DEBUG(3, ("Could not find user %s and no add script "
319                                   "defined\n", name));
320                         return NT_STATUS_NO_SUCH_USER;
321                 }
322
323                 /* lowercase the username before creating the Unix account for 
324                    compatibility with previous Samba releases */
325                 fstrcpy( name2, name );
326                 strlower_m( name2 );
327                 add_script = talloc_all_string_sub(tmp_ctx,
328                                         add_script,
329                                         "%u",
330                                         name2);
331                 if (!add_script) {
332                         return NT_STATUS_NO_MEMORY;
333                 }
334                 add_ret = smbrun(add_script,NULL);
335                 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
336                                         add_script, add_ret));
337                 if (add_ret == 0) {
338                         smb_nscd_flush_user_cache();
339                 }
340
341                 flush_pwnam_cache();
342
343                 pwd = Get_Pwnam_alloc(tmp_ctx, name);
344         }
345
346         /* we have a valid SID coming out of this call */
347
348         status = samu_alloc_rid_unix( sam_pass, pwd );
349
350         TALLOC_FREE( pwd );
351
352         if (!NT_STATUS_IS_OK(status)) {
353                 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
354                 return status;
355         }
356
357         if (!sid_peek_check_rid(get_global_sam_sid(),
358                                 pdb_get_user_sid(sam_pass), rid)) {
359                 DEBUG(0, ("Could not get RID of fresh user\n"));
360                 return NT_STATUS_INTERNAL_ERROR;
361         }
362
363         /* Use the username case specified in the original request */
364
365         pdb_set_username( sam_pass, name, PDB_SET );
366
367         /* Disable the account on creation, it does not have a reasonable password yet. */
368
369         acb_info |= ACB_DISABLED;
370
371         pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
372
373         status = pdb_add_sam_account(sam_pass);
374
375         TALLOC_FREE(sam_pass);
376
377         return status;
378 }
379
380 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
381                          uint32 *rid)
382 {
383         struct pdb_methods *pdb = pdb_get_methods();
384         return pdb->create_user(pdb, mem_ctx, name, flags, rid);
385 }
386
387 /****************************************************************************
388  Delete a UNIX user on demand.
389 ****************************************************************************/
390
391 static int smb_delete_user(const char *unix_user)
392 {
393         char *del_script = NULL;
394         int ret;
395
396         /* safety check */
397
398         if ( strequal( unix_user, "root" ) ) {
399                 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
400                 return -1;
401         }
402
403         del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
404         if (!del_script || !*del_script) {
405                 return -1;
406         }
407         del_script = talloc_all_string_sub(talloc_tos(),
408                                 del_script,
409                                 "%u",
410                                 unix_user);
411         if (!del_script) {
412                 return -1;
413         }
414         ret = smbrun(del_script,NULL);
415         flush_pwnam_cache();
416         if (ret == 0) {
417                 smb_nscd_flush_user_cache();
418         }
419         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
420
421         return ret;
422 }
423
424 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
425                                         TALLOC_CTX *mem_ctx,
426                                         struct samu *sam_acct)
427 {
428         NTSTATUS status;
429         fstring username;
430
431         status = pdb_delete_sam_account(sam_acct);
432         if (!NT_STATUS_IS_OK(status)) {
433                 return status;
434         }
435
436         /*
437          * Now delete the unix side ....
438          * note: we don't check if the delete really happened as the script is
439          * not necessary present and maybe the sysadmin doesn't want to delete
440          * the unix side
441          */
442
443         /* always lower case the username before handing it off to 
444            external scripts */
445
446         fstrcpy( username, pdb_get_username(sam_acct) );
447         strlower_m( username );
448
449         smb_delete_user( username );
450         
451         return status;
452 }
453
454 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
455 {
456         struct pdb_methods *pdb = pdb_get_methods();
457         uid_t uid = -1;
458
459         /* sanity check to make sure we don't delete root */
460
461         if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
462                 return NT_STATUS_NO_SUCH_USER;
463         }
464
465         if ( uid == 0 ) {
466                 return NT_STATUS_ACCESS_DENIED;
467         }
468
469         return pdb->delete_user(pdb, mem_ctx, sam_acct);
470 }
471
472 NTSTATUS pdb_add_sam_account(struct samu *sam_acct) 
473 {
474         struct pdb_methods *pdb = pdb_get_methods();
475         return pdb->add_sam_account(pdb, sam_acct);
476 }
477
478 NTSTATUS pdb_update_sam_account(struct samu *sam_acct) 
479 {
480         struct pdb_methods *pdb = pdb_get_methods();
481
482         memcache_flush(NULL, PDB_GETPWSID_CACHE);
483
484         return pdb->update_sam_account(pdb, sam_acct);
485 }
486
487 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct) 
488 {
489         struct pdb_methods *pdb = pdb_get_methods();
490
491         memcache_flush(NULL, PDB_GETPWSID_CACHE);
492
493         return pdb->delete_sam_account(pdb, sam_acct);
494 }
495
496 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
497 {
498         struct pdb_methods *pdb = pdb_get_methods();
499         uid_t uid;
500         NTSTATUS status;
501
502         memcache_flush(NULL, PDB_GETPWSID_CACHE);
503
504         /* sanity check to make sure we don't rename root */
505
506         if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
507                 return NT_STATUS_NO_SUCH_USER;
508         }
509
510         if ( uid == 0 ) {
511                 return NT_STATUS_ACCESS_DENIED;
512         }
513
514         status = pdb->rename_sam_account(pdb, oldname, newname);
515
516         /* always flush the cache here just to be safe */
517         flush_pwnam_cache();
518
519         return status;
520 }
521
522 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
523 {
524         struct pdb_methods *pdb = pdb_get_methods();
525         return pdb->update_login_attempts(pdb, sam_acct, success);
526 }
527
528 bool pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
529 {
530         struct pdb_methods *pdb = pdb_get_methods();
531         return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
532 }
533
534 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
535 {
536         struct pdb_methods *pdb = pdb_get_methods();
537         return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
538 }
539
540 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
541 {
542         struct pdb_methods *pdb = pdb_get_methods();
543         return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
544 }
545
546 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
547                                              TALLOC_CTX *mem_ctx,
548                                              const char *name,
549                                              uint32 *rid)
550 {
551         DOM_SID group_sid;
552         struct group *grp;
553         fstring tmp;
554
555         grp = getgrnam(name);
556
557         if (grp == NULL) {
558                 gid_t gid;
559
560                 if (smb_create_group(name, &gid) != 0) {
561                         return NT_STATUS_ACCESS_DENIED;
562                 }
563
564                 grp = getgrgid(gid);
565         }
566
567         if (grp == NULL) {
568                 return NT_STATUS_ACCESS_DENIED;
569         }
570
571         if (pdb_rid_algorithm()) {
572                 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
573         } else {
574                 if (!pdb_new_rid(rid)) {
575                         return NT_STATUS_ACCESS_DENIED;
576                 }
577         }
578
579         sid_compose(&group_sid, get_global_sam_sid(), *rid);
580                 
581         return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
582                                  SID_NAME_DOM_GRP, name, NULL);
583 }
584
585 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
586                               uint32 *rid)
587 {
588         struct pdb_methods *pdb = pdb_get_methods();
589         return pdb->create_dom_group(pdb, mem_ctx, name, rid);
590 }
591
592 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
593                                              TALLOC_CTX *mem_ctx,
594                                              uint32 rid)
595 {
596         DOM_SID group_sid;
597         GROUP_MAP map;
598         NTSTATUS status;
599         struct group *grp;
600         const char *grp_name;
601
602         sid_compose(&group_sid, get_global_sam_sid(), rid);
603
604         if (!get_domain_group_from_sid(group_sid, &map)) {
605                 DEBUG(10, ("Could not find group for rid %d\n", rid));
606                 return NT_STATUS_NO_SUCH_GROUP;
607         }
608
609         /* We need the group name for the smb_delete_group later on */
610
611         if (map.gid == (gid_t)-1) {
612                 return NT_STATUS_NO_SUCH_GROUP;
613         }
614
615         grp = getgrgid(map.gid);
616         if (grp == NULL) {
617                 return NT_STATUS_NO_SUCH_GROUP;
618         }
619
620         /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
621
622         grp_name = talloc_strdup(mem_ctx, grp->gr_name);
623         if (grp_name == NULL) {
624                 return NT_STATUS_NO_MEMORY;
625         }
626
627         status = pdb_delete_group_mapping_entry(group_sid);
628
629         if (!NT_STATUS_IS_OK(status)) {
630                 return status;
631         }
632
633         /* Don't check the result of smb_delete_group */
634         
635         smb_delete_group(grp_name);
636
637         return NT_STATUS_OK;
638 }
639
640 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
641 {
642         struct pdb_methods *pdb = pdb_get_methods();
643         return pdb->delete_dom_group(pdb, mem_ctx, rid);
644 }
645
646 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
647 {
648         struct pdb_methods *pdb = pdb_get_methods();
649         return pdb->add_group_mapping_entry(pdb, map);
650 }
651
652 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
653 {
654         struct pdb_methods *pdb = pdb_get_methods();
655         return pdb->update_group_mapping_entry(pdb, map);
656 }
657
658 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
659 {
660         struct pdb_methods *pdb = pdb_get_methods();
661         return pdb->delete_group_mapping_entry(pdb, sid);
662 }
663
664 bool pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
665                             size_t *p_num_entries, bool unix_only)
666 {
667         struct pdb_methods *pdb = pdb_get_methods();
668         return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
669                 pp_rmap, p_num_entries, unix_only));
670 }
671
672 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
673                                 const DOM_SID *sid,
674                                 uint32 **pp_member_rids,
675                                 size_t *p_num_members)
676 {
677         struct pdb_methods *pdb = pdb_get_methods();
678         NTSTATUS result;
679
680         result = pdb->enum_group_members(pdb, mem_ctx, 
681                         sid, pp_member_rids, p_num_members);
682                 
683         /* special check for rid 513 */
684                 
685         if ( !NT_STATUS_IS_OK( result ) ) {
686                 uint32 rid;
687                 
688                 sid_peek_rid( sid, &rid );
689                 
690                 if ( rid == DOMAIN_GROUP_RID_USERS ) {
691                         *p_num_members = 0;
692                         *pp_member_rids = NULL;
693                         
694                         return NT_STATUS_OK;
695                 }
696         }
697         
698         return result;
699 }
700
701 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
702                                     DOM_SID **pp_sids, gid_t **pp_gids,
703                                     size_t *p_num_groups)
704 {
705         struct pdb_methods *pdb = pdb_get_methods();
706         return pdb->enum_group_memberships(
707                 pdb, mem_ctx, user,
708                 pp_sids, pp_gids, p_num_groups);
709 }
710
711 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
712                                                    TALLOC_CTX *mem_ctx,
713                                                    struct samu *sampass)
714 {
715         struct group *grp;
716         gid_t gid;
717
718         if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
719             (grp = getgrgid(gid)) == NULL) {
720                 return NT_STATUS_INVALID_PRIMARY_GROUP;
721         }
722
723         if (smb_set_primary_group(grp->gr_name,
724                                   pdb_get_username(sampass)) != 0) {
725                 return NT_STATUS_ACCESS_DENIED;
726         }
727
728         return NT_STATUS_OK;
729 }
730
731 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
732 {
733         struct pdb_methods *pdb = pdb_get_methods();
734         return pdb->set_unix_primary_group(pdb, mem_ctx, user);
735 }
736
737 /*
738  * Helper function to see whether a user is in a group. We can't use
739  * user_in_group_sid here because this creates dependencies only smbd can
740  * fulfil.
741  */
742
743 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
744                               const DOM_SID *group_sid)
745 {
746         DOM_SID *sids;
747         gid_t *gids;
748         size_t i, num_groups;
749
750         if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
751                                                         &sids, &gids,
752                                                         &num_groups))) {
753                 return False;
754         }
755
756         for (i=0; i<num_groups; i++) {
757                 if (sid_equal(group_sid, &sids[i])) {
758                         return True;
759                 }
760         }
761         return False;
762 }
763
764 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
765                                          TALLOC_CTX *mem_ctx,
766                                          uint32 group_rid,
767                                          uint32 member_rid)
768 {
769         DOM_SID group_sid, member_sid;
770         struct samu *account = NULL;
771         GROUP_MAP map;
772         struct group *grp;
773         struct passwd *pwd;
774         const char *group_name;
775         uid_t uid;
776
777         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
778         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
779
780         if (!get_domain_group_from_sid(group_sid, &map) ||
781             (map.gid == (gid_t)-1) ||
782             ((grp = getgrgid(map.gid)) == NULL)) {
783                 return NT_STATUS_NO_SUCH_GROUP;
784         }
785
786         group_name = talloc_strdup(mem_ctx, grp->gr_name);
787         if (group_name == NULL) {
788                 return NT_STATUS_NO_MEMORY;
789         }
790
791         if ( !(account = samu_new( NULL )) ) {
792                 return NT_STATUS_NO_MEMORY;
793         }
794
795         if (!pdb_getsampwsid(account, &member_sid) ||
796             !sid_to_uid(&member_sid, &uid) ||
797             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
798                 return NT_STATUS_NO_SUCH_USER;
799         }
800
801         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
802                 return NT_STATUS_MEMBER_IN_GROUP;
803         }
804
805         /* 
806          * ok, the group exist, the user exist, the user is not in the group,
807          * we can (finally) add it to the group !
808          */
809
810         smb_add_user_group(group_name, pwd->pw_name);
811
812         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
813                 return NT_STATUS_ACCESS_DENIED;
814         }
815
816         return NT_STATUS_OK;
817 }
818
819 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
820                           uint32 member_rid)
821 {
822         struct pdb_methods *pdb = pdb_get_methods();
823         return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
824 }
825
826 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
827                                          TALLOC_CTX *mem_ctx,
828                                          uint32 group_rid,
829                                          uint32 member_rid)
830 {
831         DOM_SID group_sid, member_sid;
832         struct samu *account = NULL;
833         GROUP_MAP map;
834         struct group *grp;
835         struct passwd *pwd;
836         const char *group_name;
837         uid_t uid;
838
839         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
840         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
841
842         if (!get_domain_group_from_sid(group_sid, &map) ||
843             (map.gid == (gid_t)-1) ||
844             ((grp = getgrgid(map.gid)) == NULL)) {
845                 return NT_STATUS_NO_SUCH_GROUP;
846         }
847
848         group_name = talloc_strdup(mem_ctx, grp->gr_name);
849         if (group_name == NULL) {
850                 return NT_STATUS_NO_MEMORY;
851         }
852
853         if ( !(account = samu_new( NULL )) ) {
854                 return NT_STATUS_NO_MEMORY;
855         }
856
857         if (!pdb_getsampwsid(account, &member_sid) ||
858             !sid_to_uid(&member_sid, &uid) ||
859             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
860                 return NT_STATUS_NO_SUCH_USER;
861         }
862
863         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
864                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
865         }
866
867         /* 
868          * ok, the group exist, the user exist, the user is in the group,
869          * we can (finally) delete it from the group!
870          */
871
872         smb_delete_user_group(group_name, pwd->pw_name);
873
874         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
875                 return NT_STATUS_ACCESS_DENIED;
876         }
877
878         return NT_STATUS_OK;
879 }
880
881 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
882                           uint32 member_rid)
883 {
884         struct pdb_methods *pdb = pdb_get_methods();
885         return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
886 }
887
888 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
889 {
890         struct pdb_methods *pdb = pdb_get_methods();
891         return pdb->create_alias(pdb, name, rid);
892 }
893
894 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
895 {
896         struct pdb_methods *pdb = pdb_get_methods();
897         return pdb->delete_alias(pdb, sid);
898 }
899
900 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
901 {
902         struct pdb_methods *pdb = pdb_get_methods();
903         return pdb->get_aliasinfo(pdb, sid, info);
904 }
905
906 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
907 {
908         struct pdb_methods *pdb = pdb_get_methods();
909         return pdb->set_aliasinfo(pdb, sid, info);
910 }
911
912 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
913 {
914         struct pdb_methods *pdb = pdb_get_methods();
915         return pdb->add_aliasmem(pdb, alias, member);
916 }
917
918 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
919 {
920         struct pdb_methods *pdb = pdb_get_methods();
921         return pdb->del_aliasmem(pdb, alias, member);
922 }
923
924 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
925                            DOM_SID **pp_members, size_t *p_num_members)
926 {
927         struct pdb_methods *pdb = pdb_get_methods();
928         return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
929 }
930
931 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
932                                     const DOM_SID *domain_sid,
933                                     const DOM_SID *members, size_t num_members,
934                                     uint32 **pp_alias_rids,
935                                     size_t *p_num_alias_rids)
936 {
937         struct pdb_methods *pdb = pdb_get_methods();
938         return pdb->enum_alias_memberships(pdb, mem_ctx,
939                                                        domain_sid,
940                                                        members, num_members,
941                                                        pp_alias_rids,
942                                                        p_num_alias_rids);
943 }
944
945 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
946                          int num_rids,
947                          uint32 *rids,
948                          const char **names,
949                          enum lsa_SidType *attrs)
950 {
951         struct pdb_methods *pdb = pdb_get_methods();
952         return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
953 }
954
955 /* 
956  * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere 
957  *       in the samba code.
958  *       Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually 
959  *       also ask pdb_lookup_rids, thus looking up a bunch of rids at a time, 
960  *       the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
961  *       down to are pdb_getsampwnam and pdb_getgrnam instead of
962  *       pdb_lookup_names.
963  *       But in principle, it the call belongs to the API and might get
964  *       used in this context some day. 
965  */
966 #if 0
967 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
968                           int num_names,
969                           const char **names,
970                           uint32 *rids,
971                           enum lsa_SidType *attrs)
972 {
973         struct pdb_methods *pdb = pdb_get_methods();
974         return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
975 }
976 #endif
977
978 bool pdb_get_account_policy(int policy_index, uint32 *value)
979 {
980         struct pdb_methods *pdb = pdb_get_methods();
981         NTSTATUS status;
982         
983         become_root();
984         status = pdb->get_account_policy(pdb, policy_index, value);
985         unbecome_root();
986         
987         return NT_STATUS_IS_OK(status); 
988 }
989
990 bool pdb_set_account_policy(int policy_index, uint32 value)
991 {
992         struct pdb_methods *pdb = pdb_get_methods();
993         NTSTATUS status;
994
995         become_root();
996         status = pdb->set_account_policy(pdb, policy_index, value);
997         unbecome_root();
998
999         return NT_STATUS_IS_OK(status);
1000 }
1001
1002 bool pdb_get_seq_num(time_t *seq_num)
1003 {
1004         struct pdb_methods *pdb = pdb_get_methods();
1005         return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1006 }
1007
1008 bool pdb_uid_to_rid(uid_t uid, uint32 *rid)
1009 {
1010         struct pdb_methods *pdb = pdb_get_methods();
1011         return pdb->uid_to_rid(pdb, uid, rid);
1012 }
1013
1014 bool pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1015 {
1016         struct pdb_methods *pdb = pdb_get_methods();
1017         return pdb->uid_to_sid(pdb, uid, sid);
1018 }
1019
1020 bool pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1021 {
1022         struct pdb_methods *pdb = pdb_get_methods();
1023         return pdb->gid_to_sid(pdb, gid, sid);
1024 }
1025
1026 bool pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1027                    enum lsa_SidType *type)
1028 {
1029         struct pdb_methods *pdb = pdb_get_methods();
1030         return pdb->sid_to_id(pdb, sid, id, type);
1031 }
1032
1033 bool pdb_rid_algorithm(void)
1034 {
1035         struct pdb_methods *pdb = pdb_get_methods();
1036         return pdb->rid_algorithm(pdb);
1037 }
1038
1039 /********************************************************************
1040  Allocate a new RID from the passdb backend.  Verify that it is free
1041  by calling lookup_global_sam_rid() to verify that the RID is not
1042  in use.  This handles servers that have existing users or groups
1043  with add RIDs (assigned from previous algorithmic mappings)
1044 ********************************************************************/
1045
1046 bool pdb_new_rid(uint32 *rid)
1047 {
1048         struct pdb_methods *pdb = pdb_get_methods();
1049         const char *name = NULL;
1050         enum lsa_SidType type;
1051         uint32 allocated_rid = 0;
1052         int i;
1053         TALLOC_CTX *ctx;
1054
1055         if (pdb_rid_algorithm()) {
1056                 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1057                           "are active\n"));
1058                 return False;
1059         }
1060
1061         if (algorithmic_rid_base() != BASE_RID) {
1062                 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1063                           "without algorithmic RIDs is chosen.\n"));
1064                 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1065                              "add', set the maximum used RID using\n"));
1066                 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1067                 return False;
1068         }
1069
1070         if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1071                 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1072                 return False;
1073         }
1074
1075         /* Attempt to get an unused RID (max tires is 250...yes that it is 
1076            and arbitrary number I pulkled out of my head).   -- jerry */
1077
1078         for ( i=0; allocated_rid==0 && i<250; i++ ) {
1079                 /* get a new RID */
1080
1081                 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1082                         return False;
1083                 }
1084
1085                 /* validate that the RID is not in use */
1086
1087                 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1088                         allocated_rid = 0;
1089                 }
1090         }
1091
1092         TALLOC_FREE( ctx );
1093
1094         if ( allocated_rid == 0 ) {
1095                 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1096                 return False;
1097         }
1098
1099         *rid = allocated_rid;
1100
1101         return True;
1102 }
1103
1104 /***************************************************************
1105   Initialize the static context (at smbd startup etc). 
1106
1107   If uninitialised, context will auto-init on first use.
1108  ***************************************************************/
1109
1110 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1111 {
1112         pdb_event_ctx = event_ctx;
1113         return (pdb_get_methods_reload(reload) != NULL);
1114 }
1115
1116
1117 /***************************************************************************
1118   Default implementations of some functions.
1119  ****************************************************************************/
1120
1121 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1122 {
1123         return NT_STATUS_NO_SUCH_USER;
1124 }
1125
1126 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1127 {
1128         return NT_STATUS_NO_SUCH_USER;
1129 }
1130
1131 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1132 {
1133         return NT_STATUS_NOT_IMPLEMENTED;
1134 }
1135
1136 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1137 {
1138         return NT_STATUS_NOT_IMPLEMENTED;
1139 }
1140
1141 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1142 {
1143         return NT_STATUS_NOT_IMPLEMENTED;
1144 }
1145
1146 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1147 {
1148         return NT_STATUS_NOT_IMPLEMENTED;
1149 }
1150
1151 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1152 {
1153         /* Only the pdb_nds backend implements this, by
1154          * default just return ok. */
1155         return NT_STATUS_OK;
1156 }
1157
1158 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1159 {
1160         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1161 }
1162
1163 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1164 {
1165         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1166 }
1167
1168 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1169 {
1170         *seq_num = time(NULL);
1171         return NT_STATUS_OK;
1172 }
1173
1174 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1175                                    DOM_SID *sid)
1176 {
1177         struct samu *sampw = NULL;
1178         struct passwd *unix_pw;
1179         bool ret;
1180         
1181         unix_pw = sys_getpwuid( uid );
1182
1183         if ( !unix_pw ) {
1184                 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1185                          "%lu\n", (unsigned long)uid));
1186                 return False;
1187         }
1188         
1189         if ( !(sampw = samu_new( NULL )) ) {
1190                 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1191                 return False;
1192         }
1193
1194         become_root();
1195         ret = NT_STATUS_IS_OK(
1196                 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1197         unbecome_root();
1198
1199         if (!ret) {
1200                 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1201                           "%s (%d)\n", unix_pw->pw_name, uid));
1202                 TALLOC_FREE(sampw);
1203                 return False;
1204         }
1205
1206         sid_copy(sid, pdb_get_user_sid(sampw));
1207
1208         TALLOC_FREE(sampw);
1209
1210         return True;
1211 }
1212
1213 static bool pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1214                                    uint32 *rid)
1215 {
1216         DOM_SID sid;
1217         bool ret;
1218
1219         ret = pdb_default_uid_to_sid(methods, uid, &sid);
1220         if (!ret) {
1221                 return ret;
1222         }
1223         
1224         ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1225
1226         if (!ret) {
1227                 DEBUG(1, ("Could not peek rid out of sid %s\n",
1228                           sid_string_dbg(&sid)));
1229         }
1230
1231         return ret;
1232 }
1233
1234 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1235                                    DOM_SID *sid)
1236 {
1237         GROUP_MAP map;
1238
1239         if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1240                 return False;
1241         }
1242
1243         sid_copy(sid, &map.sid);
1244         return True;
1245 }
1246
1247 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1248                                   const DOM_SID *sid,
1249                                   union unid_t *id, enum lsa_SidType *type)
1250 {
1251         TALLOC_CTX *mem_ctx;
1252         bool ret = False;
1253         const char *name;
1254         uint32 rid;
1255
1256         mem_ctx = talloc_new(NULL);
1257
1258         if (mem_ctx == NULL) {
1259                 DEBUG(0, ("talloc_new failed\n"));
1260                 return False;
1261         }
1262
1263         if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1264                 /* Here we might have users as well as groups and aliases */
1265                 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1266                 goto done;
1267         }
1268
1269         /* check for "Unix User" */
1270
1271         if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1272                 id->uid = rid;
1273                 *type = SID_NAME_USER;
1274                 ret = True;             
1275                 goto done;              
1276         }
1277         
1278         /* check for "Unix Group" */
1279
1280         if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1281                 id->gid = rid;
1282                 *type = SID_NAME_ALIAS;
1283                 ret = True;             
1284                 goto done;              
1285         }
1286         
1287         /* BUILTIN */
1288
1289         if (sid_check_is_in_builtin(sid) ||
1290             sid_check_is_in_wellknown_domain(sid)) {
1291                 /* Here we only have aliases */
1292                 GROUP_MAP map;
1293                 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1294                         DEBUG(10, ("Could not find map for sid %s\n",
1295                                    sid_string_dbg(sid)));
1296                         goto done;
1297                 }
1298                 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1299                     (map.sid_name_use != SID_NAME_WKN_GRP)) {
1300                         DEBUG(10, ("Map for sid %s is a %s, expected an "
1301                                    "alias\n", sid_string_dbg(sid),
1302                                    sid_type_lookup(map.sid_name_use)));
1303                         goto done;
1304                 }
1305
1306                 id->gid = map.gid;
1307                 *type = SID_NAME_ALIAS;
1308                 ret = True;
1309                 goto done;
1310         }
1311
1312         DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1313                   sid_string_dbg(sid)));
1314
1315  done:
1316
1317         TALLOC_FREE(mem_ctx);
1318         return ret;
1319 }
1320
1321 static bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1322                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1323 {
1324         size_t i;
1325
1326         for (i=0; i<*p_num; i++) {
1327                 if ((*pp_uids)[i] == uid)
1328                         return True;
1329         }
1330         
1331         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1332
1333         if (*pp_uids == NULL)
1334                 return False;
1335
1336         (*pp_uids)[*p_num] = uid;
1337         *p_num += 1;
1338         return True;
1339 }
1340
1341 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1342 {
1343         struct group *grp;
1344         char **gr;
1345         struct passwd *pwd;
1346         bool winbind_env;
1347         bool ret = False;
1348  
1349         *pp_uids = NULL;
1350         *p_num = 0;
1351
1352         /* We only look at our own sam, so don't care about imported stuff */
1353         winbind_env = winbind_env_set();
1354         (void)winbind_off();
1355
1356         if ((grp = getgrgid(gid)) == NULL) {
1357                 /* allow winbindd lookups, but only if they weren't already disabled */
1358                 goto done;
1359         }
1360
1361         /* Primary group members */
1362         setpwent();
1363         while ((pwd = getpwent()) != NULL) {
1364                 if (pwd->pw_gid == gid) {
1365                         if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1366                                                 pp_uids, p_num)) {
1367                                 goto done;
1368                         }
1369                 }
1370         }
1371         endpwent();
1372
1373         /* Secondary group members */
1374         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1375                 struct passwd *pw = getpwnam(*gr);
1376
1377                 if (pw == NULL)
1378                         continue;
1379                 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1380                         goto done;
1381                 }
1382         }
1383
1384         ret = True;
1385
1386   done:
1387
1388         /* allow winbindd lookups, but only if they weren't already disabled */
1389         if (!winbind_env) {
1390                 (void)winbind_on();
1391         }
1392         
1393         return ret;
1394 }
1395
1396 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1397                                                TALLOC_CTX *mem_ctx,
1398                                                const DOM_SID *group,
1399                                                uint32 **pp_member_rids,
1400                                                size_t *p_num_members)
1401 {
1402         gid_t gid;
1403         uid_t *uids;
1404         size_t i, num_uids;
1405
1406         *pp_member_rids = NULL;
1407         *p_num_members = 0;
1408
1409         if (!sid_to_gid(group, &gid))
1410                 return NT_STATUS_NO_SUCH_GROUP;
1411
1412         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1413                 return NT_STATUS_NO_SUCH_GROUP;
1414
1415         if (num_uids == 0)
1416                 return NT_STATUS_OK;
1417
1418         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1419
1420         for (i=0; i<num_uids; i++) {
1421                 DOM_SID sid;
1422
1423                 uid_to_sid(&sid, uids[i]);
1424
1425                 if (!sid_check_is_in_our_domain(&sid)) {
1426                         DEBUG(5, ("Inconsistent SAM -- group member uid not "
1427                                   "in our domain\n"));
1428                         continue;
1429                 }
1430
1431                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1432                 *p_num_members += 1;
1433         }
1434
1435         return NT_STATUS_OK;
1436 }
1437
1438 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1439                                                    TALLOC_CTX *mem_ctx,
1440                                                    struct samu *user,
1441                                                    DOM_SID **pp_sids,
1442                                                    gid_t **pp_gids,
1443                                                    size_t *p_num_groups)
1444 {
1445         size_t i;
1446         gid_t gid;
1447         struct passwd *pw;
1448         const char *username = pdb_get_username(user);
1449         
1450
1451         /* Ignore the primary group SID.  Honor the real Unix primary group.
1452            The primary group SID is only of real use to Windows clients */
1453            
1454         if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1455                 return NT_STATUS_NO_SUCH_USER;
1456         }
1457         
1458         gid = pw->pw_gid;
1459         
1460         TALLOC_FREE( pw );
1461
1462         if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1463                 return NT_STATUS_NO_SUCH_USER;
1464         }
1465
1466         if (*p_num_groups == 0) {
1467                 smb_panic("primary group missing");
1468         }
1469
1470         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1471
1472         if (*pp_sids == NULL) {
1473                 TALLOC_FREE(*pp_gids);
1474                 return NT_STATUS_NO_MEMORY;
1475         }
1476
1477         for (i=0; i<*p_num_groups; i++) {
1478                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1479         }
1480
1481         return NT_STATUS_OK;
1482 }
1483
1484 /*******************************************************************
1485  Look up a rid in the SAM we're responsible for (i.e. passdb)
1486  ********************************************************************/
1487
1488 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1489                                   const char **name,
1490                                   enum lsa_SidType *psid_name_use,
1491                                   union unid_t *unix_id)
1492 {
1493         struct samu *sam_account = NULL;
1494         GROUP_MAP map;
1495         bool ret;
1496         DOM_SID sid;
1497
1498         *psid_name_use = SID_NAME_UNKNOWN;
1499         
1500         DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1501                  (unsigned int)rid));
1502
1503         sid_copy(&sid, get_global_sam_sid());
1504         sid_append_rid(&sid, rid);
1505         
1506         /* see if the passdb can help us with the name of the user */
1507
1508         if ( !(sam_account = samu_new( NULL )) ) {
1509                 return False;
1510         }
1511
1512         /* BEING ROOT BLLOCK */
1513         become_root();
1514         if (pdb_getsampwsid(sam_account, &sid)) {
1515                 struct passwd *pw;
1516
1517                 unbecome_root();                /* -----> EXIT BECOME_ROOT() */
1518                 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1519                 if (!*name) {
1520                         TALLOC_FREE(sam_account);
1521                         return False;
1522                 }
1523
1524                 *psid_name_use = SID_NAME_USER;
1525
1526                 TALLOC_FREE(sam_account);
1527
1528                 if (unix_id == NULL) {
1529                         return True;
1530                 }
1531
1532                 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1533                 if (pw == NULL) {
1534                         return False;
1535                 }
1536                 unix_id->uid = pw->pw_uid;
1537                 TALLOC_FREE(pw);
1538                 return True;
1539         }
1540         TALLOC_FREE(sam_account);
1541         
1542         ret = pdb_getgrsid(&map, sid);
1543         unbecome_root();
1544         /* END BECOME_ROOT BLOCK */
1545   
1546         /* do not resolve SIDs to a name unless there is a valid 
1547            gid associated with it */
1548                    
1549         if ( ret && (map.gid != (gid_t)-1) ) {
1550                 *name = talloc_strdup(mem_ctx, map.nt_name);
1551                 *psid_name_use = map.sid_name_use;
1552
1553                 if ( unix_id ) {
1554                         unix_id->gid = map.gid;
1555                 }
1556
1557                 return True;
1558         }
1559         
1560         /* Windows will always map RID 513 to something.  On a non-domain 
1561            controller, this gets mapped to SERVER\None. */
1562
1563         if ( unix_id ) {
1564                 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1565                 return False;
1566         }
1567         
1568         if ( rid == DOMAIN_GROUP_RID_USERS ) {
1569                 *name = talloc_strdup(mem_ctx, "None" );
1570                 *psid_name_use = SID_NAME_DOM_GRP;
1571                 
1572                 return True;
1573         }
1574
1575         return False;
1576 }
1577
1578 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1579                                         const DOM_SID *domain_sid,
1580                                         int num_rids,
1581                                         uint32 *rids,
1582                                         const char **names,
1583                                         enum lsa_SidType *attrs)
1584 {
1585         int i;
1586         NTSTATUS result;
1587         bool have_mapped = False;
1588         bool have_unmapped = False;
1589
1590         if (sid_check_is_builtin(domain_sid)) {
1591
1592                 for (i=0; i<num_rids; i++) {
1593                         const char *name;
1594
1595                         if (lookup_builtin_rid(names, rids[i], &name)) {
1596                                 attrs[i] = SID_NAME_ALIAS;
1597                                 names[i] = name;
1598                                 DEBUG(5,("lookup_rids: %s:%d\n",
1599                                          names[i], attrs[i]));
1600                                 have_mapped = True;
1601                         } else {
1602                                 have_unmapped = True;
1603                                 attrs[i] = SID_NAME_UNKNOWN;
1604                         }
1605                 }
1606                 goto done;
1607         }
1608
1609         /* Should not happen, but better check once too many */
1610         if (!sid_check_is_domain(domain_sid)) {
1611                 return NT_STATUS_INVALID_HANDLE;
1612         }
1613
1614         for (i = 0; i < num_rids; i++) {
1615                 const char *name;
1616
1617                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1618                                           NULL)) {
1619                         if (name == NULL) {
1620                                 return NT_STATUS_NO_MEMORY;
1621                         }
1622                         names[i] = name;
1623                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1624                         have_mapped = True;
1625                 } else {
1626                         have_unmapped = True;
1627                         attrs[i] = SID_NAME_UNKNOWN;
1628                 }
1629         }
1630
1631  done:
1632
1633         result = NT_STATUS_NONE_MAPPED;
1634
1635         if (have_mapped)
1636                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1637
1638         return result;
1639 }
1640
1641 #if 0
1642 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1643                                          const DOM_SID *domain_sid,
1644                                          int num_names,
1645                                          const char **names,
1646                                          uint32 *rids,
1647                                          enum lsa_SidType *attrs)
1648 {
1649         int i;
1650         NTSTATUS result;
1651         bool have_mapped = False;
1652         bool have_unmapped = False;
1653
1654         if (sid_check_is_builtin(domain_sid)) {
1655
1656                 for (i=0; i<num_names; i++) {
1657                         uint32 rid;
1658
1659                         if (lookup_builtin_name(names[i], &rid)) {
1660                                 attrs[i] = SID_NAME_ALIAS;
1661                                 rids[i] = rid;
1662                                 DEBUG(5,("lookup_rids: %s:%d\n",
1663                                          names[i], attrs[i]));
1664                                 have_mapped = True;
1665                         } else {
1666                                 have_unmapped = True;
1667                                 attrs[i] = SID_NAME_UNKNOWN;
1668                         }
1669                 }
1670                 goto done;
1671         }
1672
1673         /* Should not happen, but better check once too many */
1674         if (!sid_check_is_domain(domain_sid)) {
1675                 return NT_STATUS_INVALID_HANDLE;
1676         }
1677
1678         for (i = 0; i < num_names; i++) {
1679                 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1680                         DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1681                                  rids[i], attrs[i]));
1682                         have_mapped = True;
1683                 } else {
1684                         have_unmapped = True;
1685                         attrs[i] = SID_NAME_UNKNOWN;
1686                 }
1687         }
1688
1689  done:
1690
1691         result = NT_STATUS_NONE_MAPPED;
1692
1693         if (have_mapped)
1694                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1695
1696         return result;
1697 }
1698 #endif
1699
1700 struct pdb_search *pdb_search_init(enum pdb_search_type type)
1701 {
1702         TALLOC_CTX *mem_ctx;
1703         struct pdb_search *result;
1704
1705         mem_ctx = talloc_init("pdb_search");
1706         if (mem_ctx == NULL) {
1707                 DEBUG(0, ("talloc_init failed\n"));
1708                 return NULL;
1709         }
1710
1711         result = TALLOC_P(mem_ctx, struct pdb_search);
1712         if (result == NULL) {
1713                 DEBUG(0, ("talloc failed\n"));
1714                 return NULL;
1715         }
1716
1717         result->mem_ctx = mem_ctx;
1718         result->type = type;
1719         result->cache = NULL;
1720         result->num_entries = 0;
1721         result->cache_size = 0;
1722         result->search_ended = False;
1723
1724         /* Segfault appropriately if not initialized */
1725         result->next_entry = NULL;
1726         result->search_end = NULL;
1727
1728         return result;
1729 }
1730
1731 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1732                               uint16 acct_flags,
1733                               const char *account_name,
1734                               const char *fullname,
1735                               const char *description,
1736                               struct samr_displayentry *entry)
1737 {
1738         entry->rid = rid;
1739         entry->acct_flags = acct_flags;
1740
1741         if (account_name != NULL)
1742                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1743         else
1744                 entry->account_name = "";
1745
1746         if (fullname != NULL)
1747                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1748         else
1749                 entry->fullname = "";
1750
1751         if (description != NULL)
1752                 entry->description = talloc_strdup(mem_ctx, description);
1753         else
1754                 entry->description = "";
1755 }
1756
1757 struct group_search {
1758         GROUP_MAP *groups;
1759         size_t num_groups, current_group;
1760 };
1761
1762 static bool next_entry_groups(struct pdb_search *s,
1763                               struct samr_displayentry *entry)
1764 {
1765         struct group_search *state = (struct group_search *)s->private_data;
1766         uint32 rid;
1767         GROUP_MAP *map = &state->groups[state->current_group];
1768
1769         if (state->current_group == state->num_groups)
1770                 return False;
1771
1772         sid_peek_rid(&map->sid, &rid);
1773
1774         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1775                           entry);
1776
1777         state->current_group += 1;
1778         return True;
1779 }
1780
1781 static void search_end_groups(struct pdb_search *search)
1782 {
1783         struct group_search *state =
1784                 (struct group_search *)search->private_data;
1785         SAFE_FREE(state->groups);
1786 }
1787
1788 static bool pdb_search_grouptype(struct pdb_search *search,
1789                                  const DOM_SID *sid, enum lsa_SidType type)
1790 {
1791         struct group_search *state;
1792
1793         state = TALLOC_P(search->mem_ctx, struct group_search);
1794         if (state == NULL) {
1795                 DEBUG(0, ("talloc failed\n"));
1796                 return False;
1797         }
1798
1799         if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1800                                     True)) {
1801                 DEBUG(0, ("Could not enum groups\n"));
1802                 return False;
1803         }
1804
1805         state->current_group = 0;
1806         search->private_data = state;
1807         search->next_entry = next_entry_groups;
1808         search->search_end = search_end_groups;
1809         return True;
1810 }
1811
1812 static bool pdb_default_search_groups(struct pdb_methods *methods,
1813                                       struct pdb_search *search)
1814 {
1815         return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1816 }
1817
1818 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1819                                        struct pdb_search *search,
1820                                        const DOM_SID *sid)
1821 {
1822
1823         return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1824 }
1825
1826 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1827                                                      uint32 idx)
1828 {
1829         if (idx < search->num_entries)
1830                 return &search->cache[idx];
1831
1832         if (search->search_ended)
1833                 return NULL;
1834
1835         while (idx >= search->num_entries) {
1836                 struct samr_displayentry entry;
1837
1838                 if (!search->next_entry(search, &entry)) {
1839                         search->search_end(search);
1840                         search->search_ended = True;
1841                         break;
1842                 }
1843
1844                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1845                                    entry, &search->cache, &search->num_entries,
1846                                    &search->cache_size);
1847         }
1848
1849         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1850 }
1851
1852 struct pdb_search *pdb_search_users(uint32 acct_flags)
1853 {
1854         struct pdb_methods *pdb = pdb_get_methods();
1855         struct pdb_search *result;
1856
1857         result = pdb_search_init(PDB_USER_SEARCH);
1858         if (result == NULL) {
1859                 return NULL;
1860         }
1861
1862         if (!pdb->search_users(pdb, result, acct_flags)) {
1863                 talloc_destroy(result->mem_ctx);
1864                 return NULL;
1865         }
1866         return result;
1867 }
1868
1869 struct pdb_search *pdb_search_groups(void)
1870 {
1871         struct pdb_methods *pdb = pdb_get_methods();
1872         struct pdb_search *result;
1873
1874         result = pdb_search_init(PDB_GROUP_SEARCH);
1875         if (result == NULL) {
1876                  return NULL;
1877         }
1878
1879         if (!pdb->search_groups(pdb, result)) {
1880                 talloc_destroy(result->mem_ctx);
1881                 return NULL;
1882         }
1883         return result;
1884 }
1885
1886 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1887 {
1888         struct pdb_methods *pdb = pdb_get_methods();
1889         struct pdb_search *result;
1890
1891         if (pdb == NULL) return NULL;
1892
1893         result = pdb_search_init(PDB_ALIAS_SEARCH);
1894         if (result == NULL) return NULL;
1895
1896         if (!pdb->search_aliases(pdb, result, sid)) {
1897                 talloc_destroy(result->mem_ctx);
1898                 return NULL;
1899         }
1900         return result;
1901 }
1902
1903 uint32 pdb_search_entries(struct pdb_search *search,
1904                           uint32 start_idx, uint32 max_entries,
1905                           struct samr_displayentry **result)
1906 {
1907         struct samr_displayentry *end_entry;
1908         uint32 end_idx = start_idx+max_entries-1;
1909
1910         /* The first entry needs to be searched after the last. Otherwise the
1911          * first entry might have moved due to a realloc during the search for
1912          * the last entry. */
1913
1914         end_entry = pdb_search_getentry(search, end_idx);
1915         *result = pdb_search_getentry(search, start_idx);
1916
1917         if (end_entry != NULL)
1918                 return max_entries;
1919
1920         if (start_idx >= search->num_entries)
1921                 return 0;
1922
1923         return search->num_entries - start_idx;
1924 }
1925
1926 void pdb_search_destroy(struct pdb_search *search)
1927 {
1928         if (search == NULL)
1929                 return;
1930
1931         if (!search->search_ended)
1932                 search->search_end(search);
1933
1934         talloc_destroy(search->mem_ctx);
1935 }
1936
1937 /*******************************************************************
1938  trustodm methods
1939  *******************************************************************/
1940
1941 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid, 
1942                            time_t *pass_last_set_time)
1943 {
1944         struct pdb_methods *pdb = pdb_get_methods();
1945         return pdb->get_trusteddom_pw(pdb, domain, pwd, sid, 
1946                         pass_last_set_time);
1947 }
1948
1949 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
1950                            const DOM_SID *sid)
1951 {
1952         struct pdb_methods *pdb = pdb_get_methods();
1953         return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
1954 }
1955
1956 bool pdb_del_trusteddom_pw(const char *domain)
1957 {
1958         struct pdb_methods *pdb = pdb_get_methods();
1959         return pdb->del_trusteddom_pw(pdb, domain);
1960 }
1961
1962 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
1963                               struct trustdom_info ***domains)
1964 {
1965         struct pdb_methods *pdb = pdb_get_methods();
1966         return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
1967 }
1968
1969 /*******************************************************************
1970  the defaults for trustdom methods: 
1971  these simply call the original passdb/secrets.c actions,
1972  to be replaced by pdb_ldap.
1973  *******************************************************************/
1974
1975 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
1976                                           const char *domain, 
1977                                           char** pwd, 
1978                                           DOM_SID *sid, 
1979                                           time_t *pass_last_set_time)
1980 {
1981         return secrets_fetch_trusted_domain_password(domain, pwd,
1982                                 sid, pass_last_set_time);
1983
1984 }
1985
1986 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods, 
1987                                           const char* domain, 
1988                                           const char* pwd,
1989                                           const DOM_SID *sid)
1990 {
1991         return secrets_store_trusted_domain_password(domain, pwd, sid);
1992 }
1993
1994 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods, 
1995                                           const char *domain)
1996 {
1997         return trusted_domain_password_delete(domain);
1998 }
1999
2000 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2001                                              TALLOC_CTX *mem_ctx, 
2002                                              uint32 *num_domains,
2003                                              struct trustdom_info ***domains)
2004 {
2005         return secrets_trusted_domains(mem_ctx, num_domains, domains);
2006 }
2007
2008 /*******************************************************************
2009  Create a pdb_methods structure and initialize it with the default
2010  operations.  In this way a passdb module can simply implement
2011  the functionality it cares about.  However, normally this is done 
2012  in groups of related functions.
2013 *******************************************************************/
2014
2015 NTSTATUS make_pdb_method( struct pdb_methods **methods ) 
2016 {
2017         /* allocate memory for the structure as its own talloc CTX */
2018
2019         if ( !(*methods = TALLOC_ZERO_P(talloc_autofree_context(), struct pdb_methods) ) ) {
2020                 return NT_STATUS_NO_MEMORY;
2021         }
2022
2023         (*methods)->getsampwnam = pdb_default_getsampwnam;
2024         (*methods)->getsampwsid = pdb_default_getsampwsid;
2025         (*methods)->create_user = pdb_default_create_user;
2026         (*methods)->delete_user = pdb_default_delete_user;
2027         (*methods)->add_sam_account = pdb_default_add_sam_account;
2028         (*methods)->update_sam_account = pdb_default_update_sam_account;
2029         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2030         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2031         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2032
2033         (*methods)->getgrsid = pdb_default_getgrsid;
2034         (*methods)->getgrgid = pdb_default_getgrgid;
2035         (*methods)->getgrnam = pdb_default_getgrnam;
2036         (*methods)->create_dom_group = pdb_default_create_dom_group;
2037         (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2038         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2039         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2040         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2041         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2042         (*methods)->enum_group_members = pdb_default_enum_group_members;
2043         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2044         (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2045         (*methods)->add_groupmem = pdb_default_add_groupmem;
2046         (*methods)->del_groupmem = pdb_default_del_groupmem;
2047         (*methods)->create_alias = pdb_default_create_alias;
2048         (*methods)->delete_alias = pdb_default_delete_alias;
2049         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2050         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2051         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2052         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2053         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2054         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2055         (*methods)->lookup_rids = pdb_default_lookup_rids;
2056         (*methods)->get_account_policy = pdb_default_get_account_policy;
2057         (*methods)->set_account_policy = pdb_default_set_account_policy;
2058         (*methods)->get_seq_num = pdb_default_get_seq_num;
2059         (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2060         (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2061         (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2062         (*methods)->sid_to_id = pdb_default_sid_to_id;
2063
2064         (*methods)->search_groups = pdb_default_search_groups;
2065         (*methods)->search_aliases = pdb_default_search_aliases;
2066
2067         (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2068         (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2069         (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2070         (*methods)->enum_trusteddoms  = pdb_default_enum_trusteddoms;
2071
2072         return NT_STATUS_OK;
2073 }