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