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