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