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