7f0555c9910a182c9ab594591194efcc1034b7b4
[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 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 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
908 {
909         struct pdb_methods *pdb = pdb_get_methods();
910         return pdb->create_alias(pdb, name, rid);
911 }
912
913 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
914 {
915         struct pdb_methods *pdb = pdb_get_methods();
916         return pdb->delete_alias(pdb, sid);
917 }
918
919 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
920 {
921         struct pdb_methods *pdb = pdb_get_methods();
922         return pdb->get_aliasinfo(pdb, sid, info);
923 }
924
925 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
926 {
927         struct pdb_methods *pdb = pdb_get_methods();
928         return pdb->set_aliasinfo(pdb, sid, info);
929 }
930
931 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
932 {
933         struct pdb_methods *pdb = pdb_get_methods();
934         return pdb->add_aliasmem(pdb, alias, member);
935 }
936
937 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
938 {
939         struct pdb_methods *pdb = pdb_get_methods();
940         return pdb->del_aliasmem(pdb, alias, member);
941 }
942
943 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
944                            DOM_SID **pp_members, size_t *p_num_members)
945 {
946         struct pdb_methods *pdb = pdb_get_methods();
947         return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
948 }
949
950 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
951                                     const DOM_SID *domain_sid,
952                                     const DOM_SID *members, size_t num_members,
953                                     uint32 **pp_alias_rids,
954                                     size_t *p_num_alias_rids)
955 {
956         struct pdb_methods *pdb = pdb_get_methods();
957         return pdb->enum_alias_memberships(pdb, mem_ctx,
958                                                        domain_sid,
959                                                        members, num_members,
960                                                        pp_alias_rids,
961                                                        p_num_alias_rids);
962 }
963
964 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
965                          int num_rids,
966                          uint32 *rids,
967                          const char **names,
968                          enum lsa_SidType *attrs)
969 {
970         struct pdb_methods *pdb = pdb_get_methods();
971         return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
972 }
973
974 /* 
975  * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere 
976  *       in the samba code.
977  *       Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually 
978  *       also ask pdb_lookup_rids, thus looking up a bunch of rids at a time, 
979  *       the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
980  *       down to are pdb_getsampwnam and pdb_getgrnam instead of
981  *       pdb_lookup_names.
982  *       But in principle, it the call belongs to the API and might get
983  *       used in this context some day. 
984  */
985 #if 0
986 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
987                           int num_names,
988                           const char **names,
989                           uint32 *rids,
990                           enum lsa_SidType *attrs)
991 {
992         struct pdb_methods *pdb = pdb_get_methods();
993         return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
994 }
995 #endif
996
997 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
998 {
999         struct pdb_methods *pdb = pdb_get_methods();
1000         NTSTATUS status;
1001         
1002         become_root();
1003         status = pdb->get_account_policy(pdb, policy_index, value);
1004         unbecome_root();
1005         
1006         return NT_STATUS_IS_OK(status); 
1007 }
1008
1009 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1010 {
1011         struct pdb_methods *pdb = pdb_get_methods();
1012         NTSTATUS status;
1013
1014         become_root();
1015         status = pdb->set_account_policy(pdb, policy_index, value);
1016         unbecome_root();
1017
1018         return NT_STATUS_IS_OK(status);
1019 }
1020
1021 BOOL pdb_get_seq_num(time_t *seq_num)
1022 {
1023         struct pdb_methods *pdb = pdb_get_methods();
1024         return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1025 }
1026
1027 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1028 {
1029         struct pdb_methods *pdb = pdb_get_methods();
1030         return pdb->uid_to_rid(pdb, uid, rid);
1031 }
1032
1033 BOOL pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1034 {
1035         struct pdb_methods *pdb = pdb_get_methods();
1036         return pdb->uid_to_sid(pdb, uid, sid);
1037 }
1038
1039 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1040 {
1041         struct pdb_methods *pdb = pdb_get_methods();
1042         return pdb->gid_to_sid(pdb, gid, sid);
1043 }
1044
1045 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1046                    enum lsa_SidType *type)
1047 {
1048         struct pdb_methods *pdb = pdb_get_methods();
1049         return pdb->sid_to_id(pdb, sid, id, type);
1050 }
1051
1052 BOOL pdb_rid_algorithm(void)
1053 {
1054         struct pdb_methods *pdb = pdb_get_methods();
1055         return pdb->rid_algorithm(pdb);
1056 }
1057
1058 /********************************************************************
1059  Allocate a new RID from the passdb backend.  Verify that it is free
1060  by calling lookup_global_sam_rid() to verify that the RID is not
1061  in use.  This handles servers that have existing users or groups
1062  with add RIDs (assigned from previous algorithmic mappings)
1063 ********************************************************************/
1064
1065 BOOL pdb_new_rid(uint32 *rid)
1066 {
1067         struct pdb_methods *pdb = pdb_get_methods();
1068         const char *name = NULL;
1069         enum lsa_SidType type;
1070         uint32 allocated_rid = 0;
1071         int i;
1072         TALLOC_CTX *ctx;
1073
1074         if (pdb_rid_algorithm()) {
1075                 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1076                           "are active\n"));
1077                 return False;
1078         }
1079
1080         if (algorithmic_rid_base() != BASE_RID) {
1081                 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1082                           "without algorithmic RIDs is chosen.\n"));
1083                 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1084                              "add', set the maximum used RID using\n"));
1085                 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1086                 return False;
1087         }
1088
1089         if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1090                 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1091                 return False;
1092         }
1093
1094         /* Attempt to get an unused RID (max tires is 250...yes that it is 
1095            and arbitrary number I pulkled out of my head).   -- jerry */
1096
1097         for ( i=0; allocated_rid==0 && i<250; i++ ) {
1098                 /* get a new RID */
1099
1100                 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1101                         return False;
1102                 }
1103
1104                 /* validate that the RID is not in use */
1105
1106                 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1107                         allocated_rid = 0;
1108                 }
1109         }
1110
1111         TALLOC_FREE( ctx );
1112
1113         if ( allocated_rid == 0 ) {
1114                 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1115                 return False;
1116         }
1117
1118         *rid = allocated_rid;
1119
1120         return True;
1121 }
1122
1123 /***************************************************************
1124   Initialize the static context (at smbd startup etc). 
1125
1126   If uninitialised, context will auto-init on first use.
1127  ***************************************************************/
1128
1129 BOOL initialize_password_db(BOOL reload, struct event_context *event_ctx)
1130 {
1131         pdb_event_ctx = event_ctx;
1132         return (pdb_get_methods_reload(reload) != NULL);
1133 }
1134
1135
1136 /***************************************************************************
1137   Default implementations of some functions.
1138  ****************************************************************************/
1139
1140 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1141 {
1142         return NT_STATUS_NO_SUCH_USER;
1143 }
1144
1145 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1146 {
1147         return NT_STATUS_NO_SUCH_USER;
1148 }
1149
1150 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1151 {
1152         return NT_STATUS_NOT_IMPLEMENTED;
1153 }
1154
1155 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1156 {
1157         return NT_STATUS_NOT_IMPLEMENTED;
1158 }
1159
1160 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1161 {
1162         return NT_STATUS_NOT_IMPLEMENTED;
1163 }
1164
1165 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1166 {
1167         return NT_STATUS_NOT_IMPLEMENTED;
1168 }
1169
1170 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1171 {
1172         return NT_STATUS_OK;
1173 }
1174
1175 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1176 {
1177         return NT_STATUS_NOT_IMPLEMENTED;
1178 }
1179
1180 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1181 {
1182         return NT_STATUS_NOT_IMPLEMENTED;
1183 }
1184
1185 static void pdb_default_endsampwent(struct pdb_methods *methods)
1186 {
1187         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1188 }
1189
1190 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1191 {
1192         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1193 }
1194
1195 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1196 {
1197         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1198 }
1199
1200 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1201 {
1202         *seq_num = time(NULL);
1203         return NT_STATUS_OK;
1204 }
1205
1206 static BOOL pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1207                                    DOM_SID *sid)
1208 {
1209         struct samu *sampw = NULL;
1210         struct passwd *unix_pw;
1211         BOOL ret;
1212         
1213         unix_pw = sys_getpwuid( uid );
1214
1215         if ( !unix_pw ) {
1216                 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1217                          "%lu\n", (unsigned long)uid));
1218                 return False;
1219         }
1220         
1221         if ( !(sampw = samu_new( NULL )) ) {
1222                 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1223                 return False;
1224         }
1225
1226         become_root();
1227         ret = NT_STATUS_IS_OK(
1228                 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1229         unbecome_root();
1230
1231         if (!ret) {
1232                 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1233                           "%s (%d)\n", unix_pw->pw_name, uid));
1234                 TALLOC_FREE(sampw);
1235                 return False;
1236         }
1237
1238         sid_copy(sid, pdb_get_user_sid(sampw));
1239
1240         TALLOC_FREE(sampw);
1241
1242         return True;
1243 }
1244
1245 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1246                                    uint32 *rid)
1247 {
1248         DOM_SID sid;
1249         BOOL ret;
1250
1251         ret = pdb_default_uid_to_sid(methods, uid, &sid);
1252         if (!ret) {
1253                 return ret;
1254         }
1255         
1256         ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1257
1258         if (!ret) {
1259                 DEBUG(1, ("Could not peek rid out of sid %s\n",
1260                           sid_string_static(&sid)));
1261         }
1262
1263         return ret;
1264 }
1265
1266 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1267                                    DOM_SID *sid)
1268 {
1269         GROUP_MAP map;
1270
1271         if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1272                 return False;
1273         }
1274
1275         sid_copy(sid, &map.sid);
1276         return True;
1277 }
1278
1279 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1280                                   const DOM_SID *sid,
1281                                   union unid_t *id, enum lsa_SidType *type)
1282 {
1283         TALLOC_CTX *mem_ctx;
1284         BOOL ret = False;
1285         const char *name;
1286         uint32 rid;
1287
1288         mem_ctx = talloc_new(NULL);
1289
1290         if (mem_ctx == NULL) {
1291                 DEBUG(0, ("talloc_new failed\n"));
1292                 return False;
1293         }
1294
1295         if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1296                 /* Here we might have users as well as groups and aliases */
1297                 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1298                 goto done;
1299         }
1300
1301         /* check for "Unix User" */
1302
1303         if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1304                 id->uid = rid;
1305                 *type = SID_NAME_USER;
1306                 ret = True;             
1307                 goto done;              
1308         }
1309         
1310         /* check for "Unix Group" */
1311
1312         if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1313                 id->gid = rid;
1314                 *type = SID_NAME_ALIAS;
1315                 ret = True;             
1316                 goto done;              
1317         }
1318         
1319         /* BUILTIN */
1320
1321         if (sid_check_is_in_builtin(sid) ||
1322             sid_check_is_in_wellknown_domain(sid)) {
1323                 /* Here we only have aliases */
1324                 GROUP_MAP map;
1325                 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1326                         DEBUG(10, ("Could not find map for sid %s\n",
1327                                    sid_string_static(sid)));
1328                         goto done;
1329                 }
1330                 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1331                     (map.sid_name_use != SID_NAME_WKN_GRP)) {
1332                         DEBUG(10, ("Map for sid %s is a %s, expected an "
1333                                    "alias\n", sid_string_static(sid),
1334                                    sid_type_lookup(map.sid_name_use)));
1335                         goto done;
1336                 }
1337
1338                 id->gid = map.gid;
1339                 *type = SID_NAME_ALIAS;
1340                 ret = True;
1341                 goto done;
1342         }
1343
1344         DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1345                   sid_string_static(sid)));
1346
1347  done:
1348
1349         TALLOC_FREE(mem_ctx);
1350         return ret;
1351 }
1352
1353 static BOOL add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1354                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1355 {
1356         size_t i;
1357
1358         for (i=0; i<*p_num; i++) {
1359                 if ((*pp_uids)[i] == uid)
1360                         return True;
1361         }
1362         
1363         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1364
1365         if (*pp_uids == NULL)
1366                 return False;
1367
1368         (*pp_uids)[*p_num] = uid;
1369         *p_num += 1;
1370         return True;
1371 }
1372
1373 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1374 {
1375         struct group *grp;
1376         char **gr;
1377         struct passwd *pwd;
1378         BOOL winbind_env;
1379         BOOL ret = False;
1380  
1381         *pp_uids = NULL;
1382         *p_num = 0;
1383
1384         /* We only look at our own sam, so don't care about imported stuff */
1385         winbind_env = winbind_env_set();
1386         winbind_off();
1387
1388         if ((grp = getgrgid(gid)) == NULL) {
1389                 /* allow winbindd lookups, but only if they weren't already disabled */
1390                 goto done;
1391         }
1392
1393         /* Primary group members */
1394         setpwent();
1395         while ((pwd = getpwent()) != NULL) {
1396                 if (pwd->pw_gid == gid) {
1397                         if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1398                                                 pp_uids, p_num)) {
1399                                 goto done;
1400                         }
1401                 }
1402         }
1403         endpwent();
1404
1405         /* Secondary group members */
1406         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1407                 struct passwd *pw = getpwnam(*gr);
1408
1409                 if (pw == NULL)
1410                         continue;
1411                 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1412                         goto done;
1413                 }
1414         }
1415
1416         ret = True;
1417
1418   done:
1419
1420         /* allow winbindd lookups, but only if they weren't already disabled */
1421         if (!winbind_env) {
1422                 winbind_on();
1423         }
1424         
1425         return ret;
1426 }
1427
1428 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1429                                                TALLOC_CTX *mem_ctx,
1430                                                const DOM_SID *group,
1431                                                uint32 **pp_member_rids,
1432                                                size_t *p_num_members)
1433 {
1434         gid_t gid;
1435         uid_t *uids;
1436         size_t i, num_uids;
1437
1438         *pp_member_rids = NULL;
1439         *p_num_members = 0;
1440
1441         if (!sid_to_gid(group, &gid))
1442                 return NT_STATUS_NO_SUCH_GROUP;
1443
1444         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1445                 return NT_STATUS_NO_SUCH_GROUP;
1446
1447         if (num_uids == 0)
1448                 return NT_STATUS_OK;
1449
1450         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1451
1452         for (i=0; i<num_uids; i++) {
1453                 DOM_SID sid;
1454
1455                 uid_to_sid(&sid, uids[i]);
1456
1457                 if (!sid_check_is_in_our_domain(&sid)) {
1458                         DEBUG(5, ("Inconsistent SAM -- group member uid not "
1459                                   "in our domain\n"));
1460                         continue;
1461                 }
1462
1463                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1464                 *p_num_members += 1;
1465         }
1466
1467         return NT_STATUS_OK;
1468 }
1469
1470 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1471                                                    TALLOC_CTX *mem_ctx,
1472                                                    struct samu *user,
1473                                                    DOM_SID **pp_sids,
1474                                                    gid_t **pp_gids,
1475                                                    size_t *p_num_groups)
1476 {
1477         size_t i;
1478         gid_t gid;
1479         struct passwd *pw;
1480         const char *username = pdb_get_username(user);
1481         
1482
1483         /* Ignore the primary group SID.  Honor the real Unix primary group.
1484            The primary group SID is only of real use to Windows clients */
1485            
1486         if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1487                 return NT_STATUS_NO_SUCH_USER;
1488         }
1489         
1490         gid = pw->pw_gid;
1491         
1492         TALLOC_FREE( pw );
1493
1494         if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1495                 return NT_STATUS_NO_SUCH_USER;
1496         }
1497
1498         if (*p_num_groups == 0) {
1499                 smb_panic("primary group missing");
1500         }
1501
1502         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1503
1504         if (*pp_sids == NULL) {
1505                 TALLOC_FREE(*pp_gids);
1506                 return NT_STATUS_NO_MEMORY;
1507         }
1508
1509         for (i=0; i<*p_num_groups; i++) {
1510                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1511         }
1512
1513         return NT_STATUS_OK;
1514 }
1515
1516 /*******************************************************************
1517  Look up a rid in the SAM we're responsible for (i.e. passdb)
1518  ********************************************************************/
1519
1520 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1521                                   const char **name,
1522                                   enum lsa_SidType *psid_name_use,
1523                                   union unid_t *unix_id)
1524 {
1525         struct samu *sam_account = NULL;
1526         GROUP_MAP map;
1527         BOOL ret;
1528         DOM_SID sid;
1529
1530         *psid_name_use = SID_NAME_UNKNOWN;
1531         
1532         DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1533                  (unsigned int)rid));
1534
1535         sid_copy(&sid, get_global_sam_sid());
1536         sid_append_rid(&sid, rid);
1537         
1538         /* see if the passdb can help us with the name of the user */
1539
1540         if ( !(sam_account = samu_new( NULL )) ) {
1541                 return False;
1542         }
1543
1544         /* BEING ROOT BLLOCK */
1545         become_root();
1546         if (pdb_getsampwsid(sam_account, &sid)) {
1547                 struct passwd *pw;
1548
1549                 unbecome_root();                /* -----> EXIT BECOME_ROOT() */
1550                 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1551                 if (!*name) {
1552                         TALLOC_FREE(sam_account);
1553                         return False;
1554                 }
1555
1556                 *psid_name_use = SID_NAME_USER;
1557
1558                 TALLOC_FREE(sam_account);
1559
1560                 if (unix_id == NULL) {
1561                         return True;
1562                 }
1563
1564                 pw = Get_Pwnam(*name);
1565                 if (pw == NULL) {
1566                         return False;
1567                 }
1568                 unix_id->uid = pw->pw_uid;
1569                 return True;
1570         }
1571         TALLOC_FREE(sam_account);
1572         
1573         ret = pdb_getgrsid(&map, sid);
1574         unbecome_root();
1575         /* END BECOME_ROOT BLOCK */
1576   
1577         /* do not resolve SIDs to a name unless there is a valid 
1578            gid associated with it */
1579                    
1580         if ( ret && (map.gid != (gid_t)-1) ) {
1581                 *name = talloc_strdup(mem_ctx, map.nt_name);
1582                 *psid_name_use = map.sid_name_use;
1583
1584                 if ( unix_id ) {
1585                         unix_id->gid = map.gid;
1586                 }
1587
1588                 return True;
1589         }
1590         
1591         /* Windows will always map RID 513 to something.  On a non-domain 
1592            controller, this gets mapped to SERVER\None. */
1593
1594         if ( unix_id ) {
1595                 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1596                 return False;
1597         }
1598         
1599         if ( rid == DOMAIN_GROUP_RID_USERS ) {
1600                 *name = talloc_strdup(mem_ctx, "None" );
1601                 *psid_name_use = SID_NAME_DOM_GRP;
1602                 
1603                 return True;
1604         }
1605
1606         return False;
1607 }
1608
1609 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1610                                         const DOM_SID *domain_sid,
1611                                         int num_rids,
1612                                         uint32 *rids,
1613                                         const char **names,
1614                                         enum lsa_SidType *attrs)
1615 {
1616         int i;
1617         NTSTATUS result;
1618         BOOL have_mapped = False;
1619         BOOL have_unmapped = False;
1620
1621         if (sid_check_is_builtin(domain_sid)) {
1622
1623                 for (i=0; i<num_rids; i++) {
1624                         const char *name;
1625
1626                         if (lookup_builtin_rid(names, rids[i], &name)) {
1627                                 attrs[i] = SID_NAME_ALIAS;
1628                                 names[i] = name;
1629                                 DEBUG(5,("lookup_rids: %s:%d\n",
1630                                          names[i], attrs[i]));
1631                                 have_mapped = True;
1632                         } else {
1633                                 have_unmapped = True;
1634                                 attrs[i] = SID_NAME_UNKNOWN;
1635                         }
1636                 }
1637                 goto done;
1638         }
1639
1640         /* Should not happen, but better check once too many */
1641         if (!sid_check_is_domain(domain_sid)) {
1642                 return NT_STATUS_INVALID_HANDLE;
1643         }
1644
1645         for (i = 0; i < num_rids; i++) {
1646                 const char *name;
1647
1648                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1649                                           NULL)) {
1650                         if (name == NULL) {
1651                                 return NT_STATUS_NO_MEMORY;
1652                         }
1653                         names[i] = name;
1654                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1655                         have_mapped = True;
1656                 } else {
1657                         have_unmapped = True;
1658                         attrs[i] = SID_NAME_UNKNOWN;
1659                 }
1660         }
1661
1662  done:
1663
1664         result = NT_STATUS_NONE_MAPPED;
1665
1666         if (have_mapped)
1667                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1668
1669         return result;
1670 }
1671
1672 #if 0
1673 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1674                                          const DOM_SID *domain_sid,
1675                                          int num_names,
1676                                          const char **names,
1677                                          uint32 *rids,
1678                                          enum lsa_SidType *attrs)
1679 {
1680         int i;
1681         NTSTATUS result;
1682         BOOL have_mapped = False;
1683         BOOL have_unmapped = False;
1684
1685         if (sid_check_is_builtin(domain_sid)) {
1686
1687                 for (i=0; i<num_names; i++) {
1688                         uint32 rid;
1689
1690                         if (lookup_builtin_name(names[i], &rid)) {
1691                                 attrs[i] = SID_NAME_ALIAS;
1692                                 rids[i] = rid;
1693                                 DEBUG(5,("lookup_rids: %s:%d\n",
1694                                          names[i], attrs[i]));
1695                                 have_mapped = True;
1696                         } else {
1697                                 have_unmapped = True;
1698                                 attrs[i] = SID_NAME_UNKNOWN;
1699                         }
1700                 }
1701                 goto done;
1702         }
1703
1704         /* Should not happen, but better check once too many */
1705         if (!sid_check_is_domain(domain_sid)) {
1706                 return NT_STATUS_INVALID_HANDLE;
1707         }
1708
1709         for (i = 0; i < num_names; i++) {
1710                 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1711                         DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1712                                  rids[i], attrs[i]));
1713                         have_mapped = True;
1714                 } else {
1715                         have_unmapped = True;
1716                         attrs[i] = SID_NAME_UNKNOWN;
1717                 }
1718         }
1719
1720  done:
1721
1722         result = NT_STATUS_NONE_MAPPED;
1723
1724         if (have_mapped)
1725                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1726
1727         return result;
1728 }
1729 #endif
1730
1731 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1732 {
1733         TALLOC_CTX *mem_ctx;
1734         struct pdb_search *result;
1735
1736         mem_ctx = talloc_init("pdb_search");
1737         if (mem_ctx == NULL) {
1738                 DEBUG(0, ("talloc_init failed\n"));
1739                 return NULL;
1740         }
1741
1742         result = TALLOC_P(mem_ctx, struct pdb_search);
1743         if (result == NULL) {
1744                 DEBUG(0, ("talloc failed\n"));
1745                 return NULL;
1746         }
1747
1748         result->mem_ctx = mem_ctx;
1749         result->type = type;
1750         result->cache = NULL;
1751         result->num_entries = 0;
1752         result->cache_size = 0;
1753         result->search_ended = False;
1754
1755         /* Segfault appropriately if not initialized */
1756         result->next_entry = NULL;
1757         result->search_end = NULL;
1758
1759         return result;
1760 }
1761
1762 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1763                               uint16 acct_flags,
1764                               const char *account_name,
1765                               const char *fullname,
1766                               const char *description,
1767                               struct samr_displayentry *entry)
1768 {
1769         entry->rid = rid;
1770         entry->acct_flags = acct_flags;
1771
1772         if (account_name != NULL)
1773                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1774         else
1775                 entry->account_name = "";
1776
1777         if (fullname != NULL)
1778                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1779         else
1780                 entry->fullname = "";
1781
1782         if (description != NULL)
1783                 entry->description = talloc_strdup(mem_ctx, description);
1784         else
1785                 entry->description = "";
1786 }
1787
1788 static BOOL user_search_in_progress = False;
1789 struct user_search {
1790         uint16 acct_flags;
1791 };
1792
1793 static BOOL next_entry_users(struct pdb_search *s,
1794                              struct samr_displayentry *entry)
1795 {
1796         struct user_search *state = (struct user_search *)s->private_data;
1797         struct samu *user = NULL;
1798
1799  next:
1800         if ( !(user = samu_new( NULL )) ) {
1801                 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1802                 return False;
1803         }
1804
1805         if (!pdb_getsampwent(user)) {
1806                 TALLOC_FREE(user);
1807                 return False;
1808         }
1809
1810         if ((state->acct_flags != 0) &&
1811             ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1812                 TALLOC_FREE(user);
1813                 goto next;
1814         }
1815
1816         fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1817                           pdb_get_acct_ctrl(user), pdb_get_username(user),
1818                           pdb_get_fullname(user), pdb_get_acct_desc(user),
1819                           entry);
1820
1821         TALLOC_FREE(user);
1822         return True;
1823 }
1824
1825 static void search_end_users(struct pdb_search *search)
1826 {
1827         pdb_endsampwent();
1828         user_search_in_progress = False;
1829 }
1830
1831 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1832                                      struct pdb_search *search,
1833                                      uint32 acct_flags)
1834 {
1835         struct user_search *state;
1836
1837         if (user_search_in_progress) {
1838                 DEBUG(1, ("user search in progress\n"));
1839                 return False;
1840         }
1841
1842         if (!pdb_setsampwent(False, acct_flags)) {
1843                 DEBUG(5, ("Could not start search\n"));
1844                 return False;
1845         }
1846
1847         user_search_in_progress = True;
1848
1849         state = TALLOC_P(search->mem_ctx, struct user_search);
1850         if (state == NULL) {
1851                 DEBUG(0, ("talloc failed\n"));
1852                 return False;
1853         }
1854
1855         state->acct_flags = acct_flags;
1856
1857         search->private_data = state;
1858         search->next_entry = next_entry_users;
1859         search->search_end = search_end_users;
1860         return True;
1861 }
1862
1863 struct group_search {
1864         GROUP_MAP *groups;
1865         size_t num_groups, current_group;
1866 };
1867
1868 static BOOL next_entry_groups(struct pdb_search *s,
1869                               struct samr_displayentry *entry)
1870 {
1871         struct group_search *state = (struct group_search *)s->private_data;
1872         uint32 rid;
1873         GROUP_MAP *map = &state->groups[state->current_group];
1874
1875         if (state->current_group == state->num_groups)
1876                 return False;
1877
1878         sid_peek_rid(&map->sid, &rid);
1879
1880         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1881                           entry);
1882
1883         state->current_group += 1;
1884         return True;
1885 }
1886
1887 static void search_end_groups(struct pdb_search *search)
1888 {
1889         struct group_search *state =
1890                 (struct group_search *)search->private_data;
1891         SAFE_FREE(state->groups);
1892 }
1893
1894 static BOOL pdb_search_grouptype(struct pdb_search *search,
1895                                  const DOM_SID *sid, enum lsa_SidType type)
1896 {
1897         struct group_search *state;
1898
1899         state = TALLOC_P(search->mem_ctx, struct group_search);
1900         if (state == NULL) {
1901                 DEBUG(0, ("talloc failed\n"));
1902                 return False;
1903         }
1904
1905         if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1906                                     True)) {
1907                 DEBUG(0, ("Could not enum groups\n"));
1908                 return False;
1909         }
1910
1911         state->current_group = 0;
1912         search->private_data = state;
1913         search->next_entry = next_entry_groups;
1914         search->search_end = search_end_groups;
1915         return True;
1916 }
1917
1918 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1919                                       struct pdb_search *search)
1920 {
1921         return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1922 }
1923
1924 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1925                                        struct pdb_search *search,
1926                                        const DOM_SID *sid)
1927 {
1928
1929         return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1930 }
1931
1932 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1933                                                      uint32 idx)
1934 {
1935         if (idx < search->num_entries)
1936                 return &search->cache[idx];
1937
1938         if (search->search_ended)
1939                 return NULL;
1940
1941         while (idx >= search->num_entries) {
1942                 struct samr_displayentry entry;
1943
1944                 if (!search->next_entry(search, &entry)) {
1945                         search->search_end(search);
1946                         search->search_ended = True;
1947                         break;
1948                 }
1949
1950                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1951                                    entry, &search->cache, &search->num_entries,
1952                                    &search->cache_size);
1953         }
1954
1955         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1956 }
1957
1958 struct pdb_search *pdb_search_users(uint32 acct_flags)
1959 {
1960         struct pdb_methods *pdb = pdb_get_methods();
1961         struct pdb_search *result;
1962
1963         result = pdb_search_init(PDB_USER_SEARCH);
1964         if (result == NULL) {
1965                 return NULL;
1966         }
1967
1968         if (!pdb->search_users(pdb, result, acct_flags)) {
1969                 talloc_destroy(result->mem_ctx);
1970                 return NULL;
1971         }
1972         return result;
1973 }
1974
1975 struct pdb_search *pdb_search_groups(void)
1976 {
1977         struct pdb_methods *pdb = pdb_get_methods();
1978         struct pdb_search *result;
1979
1980         result = pdb_search_init(PDB_GROUP_SEARCH);
1981         if (result == NULL) {
1982                  return NULL;
1983         }
1984
1985         if (!pdb->search_groups(pdb, result)) {
1986                 talloc_destroy(result->mem_ctx);
1987                 return NULL;
1988         }
1989         return result;
1990 }
1991
1992 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1993 {
1994         struct pdb_methods *pdb = pdb_get_methods();
1995         struct pdb_search *result;
1996
1997         if (pdb == NULL) return NULL;
1998
1999         result = pdb_search_init(PDB_ALIAS_SEARCH);
2000         if (result == NULL) return NULL;
2001
2002         if (!pdb->search_aliases(pdb, result, sid)) {
2003                 talloc_destroy(result->mem_ctx);
2004                 return NULL;
2005         }
2006         return result;
2007 }
2008
2009 uint32 pdb_search_entries(struct pdb_search *search,
2010                           uint32 start_idx, uint32 max_entries,
2011                           struct samr_displayentry **result)
2012 {
2013         struct samr_displayentry *end_entry;
2014         uint32 end_idx = start_idx+max_entries-1;
2015
2016         /* The first entry needs to be searched after the last. Otherwise the
2017          * first entry might have moved due to a realloc during the search for
2018          * the last entry. */
2019
2020         end_entry = pdb_search_getentry(search, end_idx);
2021         *result = pdb_search_getentry(search, start_idx);
2022
2023         if (end_entry != NULL)
2024                 return max_entries;
2025
2026         if (start_idx >= search->num_entries)
2027                 return 0;
2028
2029         return search->num_entries - start_idx;
2030 }
2031
2032 void pdb_search_destroy(struct pdb_search *search)
2033 {
2034         if (search == NULL)
2035                 return;
2036
2037         if (!search->search_ended)
2038                 search->search_end(search);
2039
2040         talloc_destroy(search->mem_ctx);
2041 }
2042
2043 /*******************************************************************
2044  trustodm methods
2045  *******************************************************************/
2046
2047 BOOL pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid, 
2048                            time_t *pass_last_set_time)
2049 {
2050         struct pdb_methods *pdb = pdb_get_methods();
2051         return pdb->get_trusteddom_pw(pdb, domain, pwd, sid, 
2052                         pass_last_set_time);
2053 }
2054
2055 BOOL pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2056                            const DOM_SID *sid)
2057 {
2058         struct pdb_methods *pdb = pdb_get_methods();
2059         return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2060 }
2061
2062 BOOL pdb_del_trusteddom_pw(const char *domain)
2063 {
2064         struct pdb_methods *pdb = pdb_get_methods();
2065         return pdb->del_trusteddom_pw(pdb, domain);
2066 }
2067
2068 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
2069                               struct trustdom_info ***domains)
2070 {
2071         struct pdb_methods *pdb = pdb_get_methods();
2072         return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2073 }
2074
2075 /*******************************************************************
2076  the defaults for trustdom methods: 
2077  these simply call the original passdb/secrets.c actions,
2078  to be replaced by pdb_ldap.
2079  *******************************************************************/
2080
2081 static BOOL pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2082                                           const char *domain, 
2083                                           char** pwd, 
2084                                           DOM_SID *sid, 
2085                                           time_t *pass_last_set_time)
2086 {
2087         return secrets_fetch_trusted_domain_password(domain, pwd,
2088                                 sid, pass_last_set_time);
2089
2090 }
2091
2092 static BOOL pdb_default_set_trusteddom_pw(struct pdb_methods *methods, 
2093                                           const char* domain, 
2094                                           const char* pwd,
2095                                           const DOM_SID *sid)
2096 {
2097         return secrets_store_trusted_domain_password(domain, pwd, sid);
2098 }
2099
2100 static BOOL pdb_default_del_trusteddom_pw(struct pdb_methods *methods, 
2101                                           const char *domain)
2102 {
2103         return trusted_domain_password_delete(domain);
2104 }
2105
2106 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2107                                              TALLOC_CTX *mem_ctx, 
2108                                              uint32 *num_domains,
2109                                              struct trustdom_info ***domains)
2110 {
2111         return secrets_trusted_domains(mem_ctx, num_domains, domains);
2112 }
2113
2114 /*******************************************************************
2115  Create a pdb_methods structure and initialize it with the default
2116  operations.  In this way a passdb module can simply implement
2117  the functionality it cares about.  However, normally this is done 
2118  in groups of related functions.
2119 *******************************************************************/
2120
2121 NTSTATUS make_pdb_method( struct pdb_methods **methods ) 
2122 {
2123         /* allocate memory for the structure as its own talloc CTX */
2124
2125         if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2126                 return NT_STATUS_NO_MEMORY;
2127         }
2128
2129         (*methods)->setsampwent = pdb_default_setsampwent;
2130         (*methods)->endsampwent = pdb_default_endsampwent;
2131         (*methods)->getsampwent = pdb_default_getsampwent;
2132         (*methods)->getsampwnam = pdb_default_getsampwnam;
2133         (*methods)->getsampwsid = pdb_default_getsampwsid;
2134         (*methods)->create_user = pdb_default_create_user;
2135         (*methods)->delete_user = pdb_default_delete_user;
2136         (*methods)->add_sam_account = pdb_default_add_sam_account;
2137         (*methods)->update_sam_account = pdb_default_update_sam_account;
2138         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2139         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2140         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2141
2142         (*methods)->getgrsid = pdb_default_getgrsid;
2143         (*methods)->getgrgid = pdb_default_getgrgid;
2144         (*methods)->getgrnam = pdb_default_getgrnam;
2145         (*methods)->create_dom_group = pdb_default_create_dom_group;
2146         (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2147         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2148         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2149         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2150         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2151         (*methods)->enum_group_members = pdb_default_enum_group_members;
2152         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2153         (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2154         (*methods)->add_groupmem = pdb_default_add_groupmem;
2155         (*methods)->del_groupmem = pdb_default_del_groupmem;
2156         (*methods)->create_alias = pdb_default_create_alias;
2157         (*methods)->delete_alias = pdb_default_delete_alias;
2158         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2159         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2160         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2161         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2162         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2163         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2164         (*methods)->lookup_rids = pdb_default_lookup_rids;
2165         (*methods)->get_account_policy = pdb_default_get_account_policy;
2166         (*methods)->set_account_policy = pdb_default_set_account_policy;
2167         (*methods)->get_seq_num = pdb_default_get_seq_num;
2168         (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2169         (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2170         (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2171         (*methods)->sid_to_id = pdb_default_sid_to_id;
2172
2173         (*methods)->search_users = pdb_default_search_users;
2174         (*methods)->search_groups = pdb_default_search_groups;
2175         (*methods)->search_aliases = pdb_default_search_aliases;
2176
2177         (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2178         (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2179         (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2180         (*methods)->enum_trusteddoms  = pdb_default_enum_trusteddoms;
2181
2182         return NT_STATUS_OK;
2183 }