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