Initial import
[samba] / source / 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
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
27
28 static struct pdb_init_function_entry *backends = NULL;
29
30 static void lazy_initialize_passdb(void)
31 {
32         static BOOL initialized = False;
33         if(initialized)return;
34         static_init_pdb;
35         initialized = True;
36 }
37
38 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
39
40 /*******************************************************************
41  Clean up uninitialised passwords.  The only way to tell 
42  that these values are not 'real' is that they do not
43  have a valid last set time.  Instead, the value is fixed at 0. 
44  Therefore we use that as the key for 'is this a valid password'.
45  However, it is perfectly valid to have a 'default' last change
46  time, such LDAP with a missing attribute would produce.
47 ********************************************************************/
48
49 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass) 
50 {
51         const uint8 *lm_pwd, *nt_pwd;
52         
53         /* only reset a password if the last set time has been 
54            explicitly been set to zero.  A default last set time 
55            is ignored */
56
57         if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT) 
58                 && (pdb_get_pass_last_set_time(pass) == 0) ) 
59         {
60                 
61                 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT) 
62                 {
63                         lm_pwd = pdb_get_lanman_passwd(pass);
64                         if (lm_pwd) 
65                                 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
66                 }
67                 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT) 
68                 {
69                         nt_pwd = pdb_get_nt_passwd(pass);
70                         if (nt_pwd) 
71                                 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
72                 }
73         }
74
75         return;
76 }
77
78 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
79 {
80         struct pdb_init_function_entry *entry = backends;
81
82         if(version != PASSDB_INTERFACE_VERSION) {
83                 DEBUG(0,("Can't register passdb backend!\n"
84                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
85                          "while this version of samba uses version %d\n", 
86                          version,PASSDB_INTERFACE_VERSION));
87                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
88         }
89
90         if (!name || !init) {
91                 return NT_STATUS_INVALID_PARAMETER;
92         }
93
94         DEBUG(5,("Attempting to register passdb backend %s\n", name));
95
96         /* Check for duplicates */
97         if (pdb_find_backend_entry(name)) {
98                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
99                 return NT_STATUS_OBJECT_NAME_COLLISION;
100         }
101
102         entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
103         entry->name = smb_xstrdup(name);
104         entry->init = init;
105
106         DLIST_ADD(backends, entry);
107         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
108         return NT_STATUS_OK;
109 }
110
111 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
112 {
113         struct pdb_init_function_entry *entry = backends;
114
115         while(entry) {
116                 if (strcmp(entry->name, name)==0) return entry;
117                 entry = entry->next;
118         }
119
120         return NULL;
121 }
122
123 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update, uint16 acb_mask)
124 {
125         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
126
127         if (!context) {
128                 DEBUG(0, ("invalid pdb_context specified!\n"));
129                 return ret;
130         }
131
132         context->pwent_methods = context->pdb_methods;
133
134         if (!context->pwent_methods) {
135                 /* No passdbs at all */
136                 return ret;
137         }
138
139         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->setsampwent(context->pwent_methods, update, acb_mask))) {
140                 context->pwent_methods = context->pwent_methods->next;
141                 if (context->pwent_methods == NULL) 
142                         return NT_STATUS_UNSUCCESSFUL;
143         }
144         return ret;
145 }
146
147 static void context_endsampwent(struct pdb_context *context)
148 {
149         if ((!context)){
150                 DEBUG(0, ("invalid pdb_context specified!\n"));
151                 return;
152         }
153
154         if (context->pwent_methods && context->pwent_methods->endsampwent)
155                 context->pwent_methods->endsampwent(context->pwent_methods);
156
157         /* So we won't get strange data when calling getsampwent now */
158         context->pwent_methods = NULL;
159 }
160
161 static NTSTATUS context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
162 {
163         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
164
165         if ((!context) || (!context->pwent_methods)) {
166                 DEBUG(0, ("invalid pdb_context specified!\n"));
167                 return ret;
168         }
169         /* Loop until we find something useful */
170         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->getsampwent(context->pwent_methods, user))) {
171
172                 context->pwent_methods->endsampwent(context->pwent_methods);
173
174                 context->pwent_methods = context->pwent_methods->next;
175
176                 /* All methods are checked now. There are no more entries */
177                 if (context->pwent_methods == NULL)
178                         return ret;
179         
180                 context->pwent_methods->setsampwent(context->pwent_methods, False, 0);
181         }
182         user->methods = context->pwent_methods;
183         pdb_force_pw_initialization(user);
184         return ret;
185 }
186
187 static NTSTATUS context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
188 {
189         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
190
191         struct pdb_methods *curmethods;
192         if ((!context)) {
193                 DEBUG(0, ("invalid pdb_context specified!\n"));
194                 return ret;
195         }
196         curmethods = context->pdb_methods;
197         while (curmethods){
198                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwnam(curmethods, sam_acct, username))) {
199                         pdb_force_pw_initialization(sam_acct);
200                         sam_acct->methods = curmethods;
201                         return ret;
202                 }
203                 curmethods = curmethods->next;
204         }
205
206         return ret;
207 }
208
209 static NTSTATUS context_getsampwsid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
210 {
211         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
212
213         struct pdb_methods *curmethods;
214         if ((!context)) {
215                 DEBUG(0, ("invalid pdb_context specified!\n"));
216                 return ret;
217         }
218         
219         curmethods = context->pdb_methods;
220
221         while (curmethods){
222                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwsid(curmethods, sam_acct, sid))) {
223                         pdb_force_pw_initialization(sam_acct);
224                         sam_acct->methods = curmethods;
225                         return ret;
226                 }
227                 curmethods = curmethods->next;
228         }
229
230         return ret;
231 }
232
233 static NTSTATUS context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
234 {
235         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
236         const uint8 *lm_pw, *nt_pw;
237         uint16 acb_flags;
238
239         if ((!context) || (!context->pdb_methods)) {
240                 DEBUG(0, ("invalid pdb_context specified!\n"));
241                 return ret;
242         }
243
244         /* disable acccounts with no passwords (that has not 
245            been allowed by the  ACB_PWNOTREQ bit */
246         
247         lm_pw = pdb_get_lanman_passwd( sam_acct );
248         nt_pw = pdb_get_nt_passwd( sam_acct );
249         acb_flags = pdb_get_acct_ctrl( sam_acct );
250         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
251                 acb_flags |= ACB_DISABLED;
252                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
253         }
254         
255         /** @todo  This is where a 're-read on add' should be done */
256         /* We now add a new account to the first database listed. 
257          * Should we? */
258
259         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
260 }
261
262 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
263 {
264         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
265         const uint8 *lm_pw, *nt_pw;
266         uint16 acb_flags;
267
268         if (!context) {
269                 DEBUG(0, ("invalid pdb_context specified!\n"));
270                 return ret;
271         }
272
273         if (!sam_acct || !sam_acct->methods){
274                 DEBUG(0, ("invalid sam_acct specified\n"));
275                 return ret;
276         }
277
278         /* disable acccounts with no passwords (that has not 
279            been allowed by the  ACB_PWNOTREQ bit */
280         
281         lm_pw = pdb_get_lanman_passwd( sam_acct );
282         nt_pw = pdb_get_nt_passwd( sam_acct );
283         acb_flags = pdb_get_acct_ctrl( sam_acct );
284         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
285                 acb_flags |= ACB_DISABLED;
286                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
287         }
288         
289         /** @todo  This is where a 're-read on update' should be done */
290
291         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
292 }
293
294 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
295 {
296         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
297
298         struct pdb_methods *pdb_selected;
299         if (!context) {
300                 DEBUG(0, ("invalid pdb_context specified!\n"));
301                 return ret;
302         }
303
304         if (!sam_acct->methods){
305                 pdb_selected = context->pdb_methods;
306                 /* There's no passdb backend specified for this account.
307                  * Try to delete it in every passdb available 
308                  * Needed to delete accounts in smbpasswd that are not
309                  * in /etc/passwd.
310                  */
311                 while (pdb_selected){
312                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
313                                 return ret;
314                         }
315                         pdb_selected = pdb_selected->next;
316                 }
317                 return ret;
318         }
319
320         if (!sam_acct->methods->delete_sam_account){
321                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
322                 return ret;
323         }
324         
325         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
326 }
327
328 static NTSTATUS context_rename_sam_account(struct pdb_context *context, SAM_ACCOUNT *oldname, const char *newname)
329 {
330         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
331
332         struct pdb_methods *pdb_selected;
333         if (!context) {
334                 DEBUG(0, ("invalid pdb_context specified!\n"));
335                 return ret;
336         }
337
338         if (!oldname->methods){
339                 pdb_selected = context->pdb_methods;
340                 /* There's no passdb backend specified for this account.
341                  * Try to delete it in every passdb available 
342                  * Needed to delete accounts in smbpasswd that are not
343                  * in /etc/passwd.
344                  */
345                 while (pdb_selected){
346                         if (NT_STATUS_IS_OK(ret = pdb_selected->rename_sam_account(pdb_selected, oldname, newname))) {
347                                 return ret;
348                         }
349                         pdb_selected = pdb_selected->next;
350                 }
351                 return ret;
352         }
353
354         if (!oldname->methods->rename_sam_account){
355                 DEBUG(0,("invalid oldname->methods->rename_sam_account\n"));
356                 return ret;
357         }
358         
359         return oldname->methods->rename_sam_account(oldname->methods, oldname, newname);
360 }
361
362
363 static NTSTATUS context_update_login_attempts(struct pdb_context *context,
364                                                 SAM_ACCOUNT *sam_acct, BOOL success)
365 {
366         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
367
368         if (!context) {
369                 DEBUG(0, ("invalid pdb_context specified!\n"));
370                 return ret;
371         }
372
373         if (!sam_acct || !sam_acct->methods){
374                 DEBUG(0, ("invalid sam_acct specified\n"));
375                 return ret;
376         }
377
378         return sam_acct->methods->update_login_attempts(sam_acct->methods, sam_acct, success);
379 }
380
381 static NTSTATUS context_getgrsid(struct pdb_context *context,
382                                  GROUP_MAP *map, DOM_SID sid)
383 {
384         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
385
386         struct pdb_methods *curmethods;
387         if ((!context)) {
388                 DEBUG(0, ("invalid pdb_context specified!\n"));
389                 return ret;
390         }
391         curmethods = context->pdb_methods;
392         while (curmethods){
393                 ret = curmethods->getgrsid(curmethods, map, sid);
394                 if (NT_STATUS_IS_OK(ret)) {
395                         map->methods = curmethods;
396                         return ret;
397                 }
398                 curmethods = curmethods->next;
399         }
400
401         return ret;
402 }
403
404 static NTSTATUS context_getgrgid(struct pdb_context *context,
405                                  GROUP_MAP *map, gid_t gid)
406 {
407         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
408
409         struct pdb_methods *curmethods;
410         if ((!context)) {
411                 DEBUG(0, ("invalid pdb_context specified!\n"));
412                 return ret;
413         }
414         curmethods = context->pdb_methods;
415         while (curmethods){
416                 ret = curmethods->getgrgid(curmethods, map, gid);
417                 if (NT_STATUS_IS_OK(ret)) {
418                         map->methods = curmethods;
419                         return ret;
420                 }
421                 curmethods = curmethods->next;
422         }
423
424         return ret;
425 }
426
427 static NTSTATUS context_getgrnam(struct pdb_context *context,
428                                  GROUP_MAP *map, const char *name)
429 {
430         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
431
432         struct pdb_methods *curmethods;
433         if ((!context)) {
434                 DEBUG(0, ("invalid pdb_context specified!\n"));
435                 return ret;
436         }
437         curmethods = context->pdb_methods;
438         while (curmethods){
439                 ret = curmethods->getgrnam(curmethods, map, name);
440                 if (NT_STATUS_IS_OK(ret)) {
441                         map->methods = curmethods;
442                         return ret;
443                 }
444                 curmethods = curmethods->next;
445         }
446
447         return ret;
448 }
449
450 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
451                                                 GROUP_MAP *map)
452 {
453         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
454
455         if ((!context) || (!context->pdb_methods)) {
456                 DEBUG(0, ("invalid pdb_context specified!\n"));
457                 return ret;
458         }
459
460         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
461                                                              map);
462 }
463
464 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
465                                                    GROUP_MAP *map)
466 {
467         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
468
469         if ((!context) || (!context->pdb_methods)) {
470                 DEBUG(0, ("invalid pdb_context specified!\n"));
471                 return ret;
472         }
473
474         return context->
475                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
476 }
477
478 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
479                                                    DOM_SID sid)
480 {
481         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
482
483         if ((!context) || (!context->pdb_methods)) {
484                 DEBUG(0, ("invalid pdb_context specified!\n"));
485                 return ret;
486         }
487
488         return context->
489                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
490 }
491
492 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
493                                            enum SID_NAME_USE sid_name_use,
494                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
495                                            BOOL unix_only)
496 {
497         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
498
499         if ((!context) || (!context->pdb_methods)) {
500                 DEBUG(0, ("invalid pdb_context specified!\n"));
501                 return ret;
502         }
503
504         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
505                                                         sid_name_use, pp_rmap,
506                                                         p_num_entries, unix_only);
507 }
508
509 static NTSTATUS context_enum_group_members(struct pdb_context *context,
510                                            TALLOC_CTX *mem_ctx,
511                                            const DOM_SID *group,
512                                            uint32 **pp_member_rids,
513                                            size_t *p_num_members)
514 {
515         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
516
517         if ((!context) || (!context->pdb_methods)) {
518                 DEBUG(0, ("invalid pdb_context specified!\n"));
519                 return ret;
520         }
521
522         return context->pdb_methods->enum_group_members(context->pdb_methods,
523                                                         mem_ctx, group,
524                                                         pp_member_rids,
525                                                         p_num_members);
526 }
527
528 static NTSTATUS context_enum_group_memberships(struct pdb_context *context,
529                                                const char *username,
530                                                gid_t primary_gid,
531                                                DOM_SID **pp_sids, gid_t **pp_gids,
532                                                size_t *p_num_groups)
533 {
534         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
535
536         if ((!context) || (!context->pdb_methods)) {
537                 DEBUG(0, ("invalid pdb_context specified!\n"));
538                 return ret;
539         }
540
541         return context->pdb_methods->
542                 enum_group_memberships(context->pdb_methods, username,
543                                        primary_gid, pp_sids, pp_gids, p_num_groups);
544 }
545
546 static NTSTATUS context_find_alias(struct pdb_context *context,
547                                    const char *name, DOM_SID *sid)
548 {
549         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
550
551         if ((!context) || (!context->pdb_methods)) {
552                 DEBUG(0, ("invalid pdb_context specified!\n"));
553                 return ret;
554         }
555
556         return context->pdb_methods->find_alias(context->pdb_methods,
557                                                 name, sid);
558 }
559
560 static NTSTATUS context_create_alias(struct pdb_context *context,
561                                      const char *name, uint32 *rid)
562 {
563         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
564
565         if ((!context) || (!context->pdb_methods)) {
566                 DEBUG(0, ("invalid pdb_context specified!\n"));
567                 return ret;
568         }
569
570         return context->pdb_methods->create_alias(context->pdb_methods,
571                                                   name, rid);
572 }
573
574 static NTSTATUS context_delete_alias(struct pdb_context *context,
575                                      const DOM_SID *sid)
576 {
577         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
578
579         if ((!context) || (!context->pdb_methods)) {
580                 DEBUG(0, ("invalid pdb_context specified!\n"));
581                 return ret;
582         }
583
584         return context->pdb_methods->delete_alias(context->pdb_methods, sid);
585 }
586
587 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
588                                       const DOM_SID *sid,
589                                       struct acct_info *info)
590 {
591         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
592
593         if ((!context) || (!context->pdb_methods)) {
594                 DEBUG(0, ("invalid pdb_context specified!\n"));
595                 return ret;
596         }
597
598         return context->pdb_methods->get_aliasinfo(context->pdb_methods,
599                                                    sid, info);
600 }
601
602 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
603                                       const DOM_SID *sid,
604                                       struct acct_info *info)
605 {
606         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
607
608         if ((!context) || (!context->pdb_methods)) {
609                 DEBUG(0, ("invalid pdb_context specified!\n"));
610                 return ret;
611         }
612
613         return context->pdb_methods->set_aliasinfo(context->pdb_methods,
614                                                    sid, info);
615 }
616
617 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
618                                      const DOM_SID *alias,
619                                      const DOM_SID *member)
620 {
621         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
622
623         if ((!context) || (!context->pdb_methods)) {
624                 DEBUG(0, ("invalid pdb_context specified!\n"));
625                 return ret;
626         }
627
628         return context->pdb_methods->add_aliasmem(context->pdb_methods,
629                                                   alias, member);
630 }
631         
632 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
633                                      const DOM_SID *alias,
634                                      const DOM_SID *member)
635 {
636         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
637
638         if ((!context) || (!context->pdb_methods)) {
639                 DEBUG(0, ("invalid pdb_context specified!\n"));
640                 return ret;
641         }
642
643         return context->pdb_methods->del_aliasmem(context->pdb_methods,
644                                                   alias, member);
645 }
646         
647 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
648                                       const DOM_SID *alias, DOM_SID **pp_members,
649                                       size_t *p_num)
650 {
651         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
652
653         if ((!context) || (!context->pdb_methods)) {
654                 DEBUG(0, ("invalid pdb_context specified!\n"));
655                 return ret;
656         }
657
658         return context->pdb_methods->enum_aliasmem(context->pdb_methods,
659                                                    alias, pp_members, p_num);
660 }
661         
662 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
663                                                TALLOC_CTX *mem_ctx,
664                                                const DOM_SID *domain_sid,
665                                                const DOM_SID *members,
666                                                size_t num_members,
667                                                uint32 **pp_alias_rids,
668                                                size_t *p_num_alias_rids)
669 {
670         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
671
672         if ((!context) || (!context->pdb_methods)) {
673                 DEBUG(0, ("invalid pdb_context specified!\n"));
674                 return ret;
675         }
676
677         return context->pdb_methods->
678                 enum_alias_memberships(context->pdb_methods, mem_ctx,
679                                        domain_sid, members, num_members,
680                                        pp_alias_rids, p_num_alias_rids);
681 }
682
683 static NTSTATUS context_lookup_rids(struct pdb_context *context,
684                                     const DOM_SID *domain_sid,
685                                     size_t num_rids,
686                                     uint32 *rids,
687                                     const char **pp_names,
688                                     uint32 *pp_attrs)
689 {
690         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
691
692         if ((!context) || (!context->pdb_methods)) {
693                 DEBUG(0, ("invalid pdb_context specified!\n"));
694                 return ret;
695         }
696
697         return context->pdb_methods->lookup_rids(context->pdb_methods,
698                                                  domain_sid, num_rids,
699                                                  rids, pp_names, pp_attrs);
700 }
701
702 static NTSTATUS context_get_account_policy(struct pdb_context *context,
703                                            int policy_index, uint32 *value)
704 {
705         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
706
707         if ((!context) || (!context->pdb_methods)) {
708                 DEBUG(0, ("invalid pdb_context specified!\n"));
709                 return ret;
710         }
711
712         return context->pdb_methods->get_account_policy(context->pdb_methods,
713                                                         policy_index, value);
714 }
715
716 static NTSTATUS context_set_account_policy(struct pdb_context *context,
717                                            int policy_index, uint32 value)
718 {
719         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
720
721         if ((!context) || (!context->pdb_methods)) {
722                 DEBUG(0, ("invalid pdb_context specified!\n"));
723                 return ret;
724         }
725
726         return context->pdb_methods->set_account_policy(context->pdb_methods,
727                                                         policy_index, value);
728 }
729
730 static NTSTATUS context_get_seq_num(struct pdb_context *context, time_t *seq_num)
731 {
732         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
733
734         if ((!context) || (!context->pdb_methods)) {
735                 DEBUG(0, ("invalid pdb_context specified!\n"));
736                 return ret;
737         }
738
739         return context->pdb_methods->get_seq_num(context->pdb_methods, seq_num);
740 }
741         
742 /******************************************************************
743   Free and cleanup a pdb context, any associated data and anything
744   that the attached modules might have associated.
745  *******************************************************************/
746
747 static void free_pdb_context(struct pdb_context **context)
748 {
749         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
750
751         while (pdb_selected){
752                 if(pdb_selected->free_private_data)
753                         pdb_selected->free_private_data(&(pdb_selected->private_data));
754                 pdb_selected = pdb_selected->next;
755         }
756
757         talloc_destroy((*context)->mem_ctx);
758         *context = NULL;
759 }
760
761 static BOOL context_search_users(struct pdb_context *context,
762                                  struct pdb_search *search, uint16 acct_flags)
763 {
764         if ((!context) || (!context->pdb_methods)) {
765                 DEBUG(0, ("invalid pdb_context specified!\n"));
766                 return False;
767         }
768
769         return context->pdb_methods->search_users(context->pdb_methods,
770                                                   search, acct_flags);
771 }
772
773 static BOOL context_search_groups(struct pdb_context *context,
774                                   struct pdb_search *search)
775 {
776         if ((!context) || (!context->pdb_methods)) {
777                 DEBUG(0, ("invalid pdb_context specified!\n"));
778                 return False;
779         }
780
781         return context->pdb_methods->search_groups(context->pdb_methods,
782                                                    search);
783 }
784
785 static BOOL context_search_aliases(struct pdb_context *context,
786                                    struct pdb_search *search,
787                                    const DOM_SID *sid)
788 {
789         if ((!context) || (!context->pdb_methods)) {
790                 DEBUG(0, ("invalid pdb_context specified!\n"));
791                 return False;
792         }
793
794         return context->pdb_methods->search_aliases(context->pdb_methods,
795                                                     search, sid);
796 }
797
798 /******************************************************************
799   Make a pdb_methods from scratch
800  *******************************************************************/
801
802 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
803 {
804         char *module_name = smb_xstrdup(selected);
805         char *module_location = NULL, *p;
806         struct pdb_init_function_entry *entry;
807         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
808
809         lazy_initialize_passdb();
810
811         p = strchr(module_name, ':');
812
813         if (p) {
814                 *p = 0;
815                 module_location = p+1;
816                 trim_char(module_location, ' ', ' ');
817         }
818
819         trim_char(module_name, ' ', ' ');
820
821
822         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
823
824         entry = pdb_find_backend_entry(module_name);
825         
826         /* Try to find a module that contains this module */
827         if (!entry) { 
828                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
829                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
830                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
831                         SAFE_FREE(module_name);
832                         return NT_STATUS_UNSUCCESSFUL;
833                 }
834         }
835         
836         /* No such backend found */
837         if(!entry) { 
838                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
839                 SAFE_FREE(module_name);
840                 return NT_STATUS_INVALID_PARAMETER;
841         }
842
843         DEBUG(5,("Found pdb backend %s\n", module_name));
844         nt_status = entry->init(context, methods, module_location);
845         if (NT_STATUS_IS_OK(nt_status)) {
846                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
847         } else {
848                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
849         }
850         SAFE_FREE(module_name);
851         return nt_status;
852 }
853
854 /******************************************************************
855   Make a pdb_context from scratch.
856  *******************************************************************/
857
858 static NTSTATUS make_pdb_context(struct pdb_context **context) 
859 {
860         TALLOC_CTX *mem_ctx;
861
862         mem_ctx = talloc_init("pdb_context internal allocation context");
863
864         if (!mem_ctx) {
865                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
866                 return NT_STATUS_NO_MEMORY;
867         }               
868
869         *context = TALLOC_P(mem_ctx, struct pdb_context);
870         if (!*context) {
871                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
872                 return NT_STATUS_NO_MEMORY;
873         }
874
875         ZERO_STRUCTP(*context);
876
877         (*context)->mem_ctx = mem_ctx;
878
879         (*context)->pdb_setsampwent = context_setsampwent;
880         (*context)->pdb_endsampwent = context_endsampwent;
881         (*context)->pdb_getsampwent = context_getsampwent;
882         (*context)->pdb_getsampwnam = context_getsampwnam;
883         (*context)->pdb_getsampwsid = context_getsampwsid;
884         (*context)->pdb_add_sam_account = context_add_sam_account;
885         (*context)->pdb_update_sam_account = context_update_sam_account;
886         (*context)->pdb_delete_sam_account = context_delete_sam_account;
887         (*context)->pdb_rename_sam_account = context_rename_sam_account;
888         (*context)->pdb_update_login_attempts = context_update_login_attempts;
889         (*context)->pdb_getgrsid = context_getgrsid;
890         (*context)->pdb_getgrgid = context_getgrgid;
891         (*context)->pdb_getgrnam = context_getgrnam;
892         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
893         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
894         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
895         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
896         (*context)->pdb_enum_group_members = context_enum_group_members;
897         (*context)->pdb_enum_group_memberships = context_enum_group_memberships;
898
899         (*context)->pdb_find_alias = context_find_alias;
900         (*context)->pdb_create_alias = context_create_alias;
901         (*context)->pdb_delete_alias = context_delete_alias;
902         (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
903         (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
904         (*context)->pdb_add_aliasmem = context_add_aliasmem;
905         (*context)->pdb_del_aliasmem = context_del_aliasmem;
906         (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
907         (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
908         (*context)->pdb_lookup_rids = context_lookup_rids;
909
910         (*context)->pdb_get_account_policy = context_get_account_policy;
911         (*context)->pdb_set_account_policy = context_set_account_policy;
912
913         (*context)->pdb_get_seq_num = context_get_seq_num;
914
915         (*context)->pdb_search_users = context_search_users;
916         (*context)->pdb_search_groups = context_search_groups;
917         (*context)->pdb_search_aliases = context_search_aliases;
918
919         (*context)->free_fn = free_pdb_context;
920
921         return NT_STATUS_OK;
922 }
923
924
925 /******************************************************************
926   Make a pdb_context, given an array of strings
927  *******************************************************************/
928
929 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
930 {
931         int i = 0;
932         struct pdb_methods *curmethods, *tmpmethods;
933         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
934         BOOL have_guest = False;
935
936         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
937                 return nt_status;
938         }
939
940         if (!selected) {
941                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
942                 return nt_status;
943         }
944
945         while (selected[i]){
946                 if (strcmp(selected[i], "guest") == 0) {
947                         have_guest = True;
948                 }
949                 /* Try to initialise pdb */
950                 DEBUG(5,("Trying to load: %s\n", selected[i]));
951                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
952                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
953                         free_pdb_context(context);
954                         return nt_status;
955                 }
956                 curmethods->parent = *context;
957                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
958                 i++;
959         }
960
961         if (have_guest)
962                 return NT_STATUS_OK;
963
964         if ( (lp_guestaccount() == NULL) ||
965              (*lp_guestaccount() == '\0') ) {
966                 /* We explicitly don't want guest access. No idea what
967                    else that breaks, but be it that way. */
968                 return NT_STATUS_OK;
969         }
970
971         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
972                                                                *context,
973                                                                "guest"))) {
974                 DEBUG(1, ("Loading guest module failed!\n"));
975                 free_pdb_context(context);
976                 return nt_status;
977         }
978
979         curmethods->parent = *context;
980         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
981         
982         return NT_STATUS_OK;
983 }
984
985 /******************************************************************
986   Make a pdb_context, given a text string.
987  *******************************************************************/
988
989 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
990 {
991         NTSTATUS ret;
992         char **newsel = str_list_make(selected, NULL);
993         ret = make_pdb_context_list(context, (const char **)newsel);
994         str_list_free(&newsel);
995         return ret;
996 }
997
998 /******************************************************************
999  Return an already initialised pdb_context, to facilitate backward 
1000  compatibility (see functions below).
1001 *******************************************************************/
1002
1003 static struct pdb_context *pdb_get_static_context(BOOL reload) 
1004 {
1005         static struct pdb_context *pdb_context = NULL;
1006
1007         if ((pdb_context) && (reload)) {
1008                 pdb_context->free_fn(&pdb_context);
1009                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1010                         return NULL;
1011                 }
1012         }
1013
1014         if (!pdb_context) {
1015                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1016                         return NULL;
1017                 }
1018         }
1019
1020         return pdb_context;
1021 }
1022
1023 /******************************************************************
1024  Backward compatibility functions for the original passdb interface
1025 *******************************************************************/
1026
1027 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask) 
1028 {
1029         struct pdb_context *pdb_context = pdb_get_static_context(False);
1030
1031         if (!pdb_context) {
1032                 return False;
1033         }
1034
1035         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update, acb_mask));
1036 }
1037
1038 void pdb_endsampwent(void) 
1039 {
1040         struct pdb_context *pdb_context = pdb_get_static_context(False);
1041
1042         if (!pdb_context) {
1043                 return;
1044         }
1045
1046         pdb_context->pdb_endsampwent(pdb_context);
1047 }
1048
1049 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
1050 {
1051         struct pdb_context *pdb_context = pdb_get_static_context(False);
1052
1053         if (!pdb_context) {
1054                 return False;
1055         }
1056
1057         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
1058 }
1059
1060 static SAM_ACCOUNT *sam_account_cache = NULL;
1061
1062 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
1063 {
1064         struct pdb_context *pdb_context = pdb_get_static_context(False);
1065
1066         if (!pdb_context) {
1067                 return False;
1068         }
1069
1070         if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
1071                                                           sam_acct, username)))
1072                 return False;
1073
1074         if (sam_account_cache != NULL) {
1075                 pdb_free_sam(&sam_account_cache);
1076                 sam_account_cache = NULL;
1077         }
1078
1079         pdb_copy_sam_account(sam_acct, &sam_account_cache);
1080         return True;
1081 }
1082
1083 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
1084 {
1085         struct pdb_context *pdb_context = pdb_get_static_context(False);
1086
1087         if (!pdb_context) {
1088                 return False;
1089         }
1090
1091         if ((sam_account_cache != NULL) &&
1092             (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
1093                 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
1094
1095         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1096 }
1097
1098 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
1099 {
1100         struct pdb_context *pdb_context = pdb_get_static_context(False);
1101
1102         if (!pdb_context) {
1103                 return False;
1104         }
1105         
1106         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1107 }
1108
1109 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
1110 {
1111         struct pdb_context *pdb_context = pdb_get_static_context(False);
1112
1113         if (!pdb_context) {
1114                 return False;
1115         }
1116
1117         if (sam_account_cache != NULL) {
1118                 pdb_free_sam(&sam_account_cache);
1119                 sam_account_cache = NULL;
1120         }
1121
1122         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1123 }
1124
1125 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
1126 {
1127         struct pdb_context *pdb_context = pdb_get_static_context(False);
1128
1129         if (!pdb_context) {
1130                 return False;
1131         }
1132
1133         if (sam_account_cache != NULL) {
1134                 pdb_free_sam(&sam_account_cache);
1135                 sam_account_cache = NULL;
1136         }
1137
1138         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1139 }
1140
1141 NTSTATUS pdb_rename_sam_account(SAM_ACCOUNT *oldname, const char *newname)
1142 {
1143         struct pdb_context *pdb_context = pdb_get_static_context(False);
1144
1145         if (!pdb_context) {
1146                 return NT_STATUS_NOT_IMPLEMENTED;
1147         }
1148
1149         if (sam_account_cache != NULL) {
1150                 pdb_free_sam(&sam_account_cache);
1151                 sam_account_cache = NULL;
1152         }
1153
1154         return pdb_context->pdb_rename_sam_account(pdb_context, oldname, newname);
1155 }
1156
1157 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
1158 {
1159         struct pdb_context *pdb_context = pdb_get_static_context(False);
1160
1161         if (!pdb_context) {
1162                 return NT_STATUS_NOT_IMPLEMENTED;
1163         }
1164
1165         return pdb_context->pdb_update_login_attempts(pdb_context, sam_acct, success);
1166 }
1167
1168 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1169 {
1170         struct pdb_context *pdb_context = pdb_get_static_context(False);
1171
1172         if (!pdb_context) {
1173                 return False;
1174         }
1175
1176         return NT_STATUS_IS_OK(pdb_context->
1177                                pdb_getgrsid(pdb_context, map, sid));
1178 }
1179
1180 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1181 {
1182         struct pdb_context *pdb_context = pdb_get_static_context(False);
1183
1184         if (!pdb_context) {
1185                 return False;
1186         }
1187
1188         return NT_STATUS_IS_OK(pdb_context->
1189                                pdb_getgrgid(pdb_context, map, gid));
1190 }
1191
1192 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1193 {
1194         struct pdb_context *pdb_context = pdb_get_static_context(False);
1195
1196         if (!pdb_context) {
1197                 return False;
1198         }
1199
1200         return NT_STATUS_IS_OK(pdb_context->
1201                                pdb_getgrnam(pdb_context, map, name));
1202 }
1203
1204 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1205 {
1206         struct pdb_context *pdb_context = pdb_get_static_context(False);
1207
1208         if (!pdb_context) {
1209                 return False;
1210         }
1211
1212         return NT_STATUS_IS_OK(pdb_context->
1213                                pdb_add_group_mapping_entry(pdb_context, map));
1214 }
1215
1216 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1217 {
1218         struct pdb_context *pdb_context = pdb_get_static_context(False);
1219
1220         if (!pdb_context) {
1221                 return False;
1222         }
1223
1224         return NT_STATUS_IS_OK(pdb_context->
1225                                pdb_update_group_mapping_entry(pdb_context, map));
1226 }
1227
1228 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1229 {
1230         struct pdb_context *pdb_context = pdb_get_static_context(False);
1231
1232         if (!pdb_context) {
1233                 return False;
1234         }
1235
1236         return NT_STATUS_IS_OK(pdb_context->
1237                                pdb_delete_group_mapping_entry(pdb_context, sid));
1238 }
1239
1240 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
1241                             size_t *p_num_entries, BOOL unix_only)
1242 {
1243         struct pdb_context *pdb_context = pdb_get_static_context(False);
1244
1245         if (!pdb_context) {
1246                 return False;
1247         }
1248
1249         return NT_STATUS_IS_OK(pdb_context->
1250                                pdb_enum_group_mapping(pdb_context, sid_name_use,
1251                                                       pp_rmap, p_num_entries, unix_only));
1252 }
1253
1254 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
1255                                 const DOM_SID *sid,
1256                                 uint32 **pp_member_rids,
1257                                 size_t *p_num_members)
1258 {
1259         struct pdb_context *pdb_context = pdb_get_static_context(False);
1260
1261         if (!pdb_context) {
1262                 return NT_STATUS_UNSUCCESSFUL;
1263         }
1264
1265         return pdb_context->pdb_enum_group_members(pdb_context, mem_ctx, sid, 
1266                                                    pp_member_rids, p_num_members);
1267 }
1268
1269 NTSTATUS pdb_enum_group_memberships(const char *username, gid_t primary_gid,
1270                                     DOM_SID **pp_sids, gid_t **pp_gids,
1271                                     size_t *p_num_groups)
1272 {
1273         struct pdb_context *pdb_context = pdb_get_static_context(False);
1274
1275         if (!pdb_context) {
1276                 return NT_STATUS_UNSUCCESSFUL;
1277         }
1278
1279         return pdb_context->pdb_enum_group_memberships(pdb_context, username,
1280                                                        primary_gid, pp_sids, pp_gids,
1281                                                        p_num_groups);
1282 }
1283
1284 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1285 {
1286         struct pdb_context *pdb_context = pdb_get_static_context(False);
1287
1288         if (!pdb_context) {
1289                 return False;
1290         }
1291
1292         return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1293                                                              name, sid));
1294 }
1295
1296 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1297 {
1298         struct pdb_context *pdb_context = pdb_get_static_context(False);
1299
1300         if (!pdb_context) {
1301                 return NT_STATUS_NOT_IMPLEMENTED;
1302         }
1303
1304         return pdb_context->pdb_create_alias(pdb_context, name, rid);
1305 }
1306
1307 BOOL pdb_delete_alias(const DOM_SID *sid)
1308 {
1309         struct pdb_context *pdb_context = pdb_get_static_context(False);
1310
1311         if (!pdb_context) {
1312                 return False;
1313         }
1314
1315         return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1316                                                              sid));
1317                                                             
1318 }
1319
1320 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1321 {
1322         struct pdb_context *pdb_context = pdb_get_static_context(False);
1323
1324         if (!pdb_context) {
1325                 return False;
1326         }
1327
1328         return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1329                                                               info));
1330 }
1331
1332 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1333 {
1334         struct pdb_context *pdb_context = pdb_get_static_context(False);
1335
1336         if (!pdb_context) {
1337                 return False;
1338         }
1339
1340         return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1341                                                               info));
1342 }
1343
1344 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1345 {
1346         struct pdb_context *pdb_context = pdb_get_static_context(False);
1347
1348         if (!pdb_context) {
1349                 return False;
1350         }
1351
1352         return NT_STATUS_IS_OK(pdb_context->
1353                                pdb_add_aliasmem(pdb_context, alias, member));
1354 }
1355
1356 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1357 {
1358         struct pdb_context *pdb_context = pdb_get_static_context(False);
1359
1360         if (!pdb_context) {
1361                 return False;
1362         }
1363
1364         return NT_STATUS_IS_OK(pdb_context->
1365                                pdb_del_aliasmem(pdb_context, alias, member));
1366 }
1367
1368 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1369                        DOM_SID **pp_members, size_t *p_num_members)
1370 {
1371         struct pdb_context *pdb_context = pdb_get_static_context(False);
1372
1373         if (!pdb_context) {
1374                 return False;
1375         }
1376
1377         return NT_STATUS_IS_OK(pdb_context->
1378                                pdb_enum_aliasmem(pdb_context, alias,
1379                                                  pp_members, p_num_members));
1380 }
1381
1382 BOOL pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
1383                                 const DOM_SID *members, size_t num_members,
1384                                 uint32 **pp_alias_rids, size_t *p_num_alias_rids)
1385 {
1386         struct pdb_context *pdb_context = pdb_get_static_context(False);
1387
1388         if (!pdb_context) {
1389                 return False;
1390         }
1391
1392         return NT_STATUS_IS_OK(pdb_context->
1393                                pdb_enum_alias_memberships(pdb_context, mem_ctx,
1394                                                           domain_sid,
1395                                                           members, num_members,
1396                                                           pp_alias_rids,
1397                                                           p_num_alias_rids));
1398 }
1399
1400 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
1401                          int num_rids,
1402                          uint32 *rids,
1403                          const char **names,
1404                          uint32 *attrs)
1405 {
1406         struct pdb_context *pdb_context = pdb_get_static_context(False);
1407
1408         if (!pdb_context) {
1409                 return NT_STATUS_NOT_IMPLEMENTED;
1410         }
1411
1412         return pdb_context->pdb_lookup_rids(pdb_context, domain_sid,
1413                                             num_rids, rids, names, attrs);
1414 }
1415
1416 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1417 {
1418         struct pdb_context *pdb_context = pdb_get_static_context(False);
1419
1420         if (!pdb_context) {
1421                 return False;
1422         }
1423
1424         return NT_STATUS_IS_OK(pdb_context->
1425                                pdb_get_account_policy(pdb_context, policy_index, value));
1426 }
1427
1428 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1429 {
1430         struct pdb_context *pdb_context = pdb_get_static_context(False);
1431
1432         if (!pdb_context) {
1433                 return False;
1434         }
1435
1436         return NT_STATUS_IS_OK(pdb_context->
1437                                pdb_set_account_policy(pdb_context, policy_index, value));
1438 }
1439
1440 BOOL pdb_get_seq_num(time_t *seq_num)
1441 {
1442         struct pdb_context *pdb_context = pdb_get_static_context(False);
1443
1444         if (!pdb_context) {
1445                 return False;
1446         }
1447
1448         return NT_STATUS_IS_OK(pdb_context->
1449                                pdb_get_seq_num(pdb_context, seq_num));
1450 }
1451 /***************************************************************
1452   Initialize the static context (at smbd startup etc). 
1453
1454   If uninitialised, context will auto-init on first use.
1455  ***************************************************************/
1456
1457 BOOL initialize_password_db(BOOL reload)
1458 {       
1459         return (pdb_get_static_context(reload) != NULL);
1460 }
1461
1462
1463 /***************************************************************************
1464   Default implementations of some functions.
1465  ****************************************************************************/
1466
1467 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1468 {
1469         return NT_STATUS_NO_SUCH_USER;
1470 }
1471
1472 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1473 {
1474         return NT_STATUS_NO_SUCH_USER;
1475 }
1476
1477 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1478 {
1479         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1480         return NT_STATUS_NOT_IMPLEMENTED;
1481 }
1482
1483 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1484 {
1485         return NT_STATUS_NOT_IMPLEMENTED;
1486 }
1487
1488 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1489 {
1490         return NT_STATUS_NOT_IMPLEMENTED;
1491 }
1492
1493 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd, const char *newname)
1494 {
1495         return NT_STATUS_NOT_IMPLEMENTED;
1496 }
1497
1498 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1499 {
1500         return NT_STATUS_OK;
1501 }
1502
1503 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1504 {
1505         return NT_STATUS_NOT_IMPLEMENTED;
1506 }
1507
1508 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1509 {
1510         return NT_STATUS_NOT_IMPLEMENTED;
1511 }
1512
1513 static void pdb_default_endsampwent(struct pdb_methods *methods)
1514 {
1515         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1516 }
1517
1518 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1519 {
1520         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1521 }
1522
1523 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1524 {
1525         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1526 }
1527
1528 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1529 {
1530         *seq_num = time(NULL);
1531         return NT_STATUS_OK;
1532 }
1533
1534 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1535                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1536 {
1537         size_t i;
1538
1539         for (i=0; i<*p_num; i++) {
1540                 if ((*pp_uids)[i] == uid)
1541                         return;
1542         }
1543         
1544         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1545
1546         if (*pp_uids == NULL)
1547                 return;
1548
1549         (*pp_uids)[*p_num] = uid;
1550         *p_num += 1;
1551 }
1552
1553 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1554 {
1555         struct group *grp;
1556         char **gr;
1557         struct sys_pwent *userlist, *user;
1558  
1559         *pp_uids = NULL;
1560         *p_num = 0;
1561
1562         /* We only look at our own sam, so don't care about imported stuff */
1563
1564         winbind_off();
1565
1566         if ((grp = getgrgid(gid)) == NULL) {
1567                 winbind_on();
1568                 return False;
1569         }
1570
1571         /* Primary group members */
1572
1573         userlist = getpwent_list();
1574
1575         for (user = userlist; user != NULL; user = user->next) {
1576                 if (user->pw_gid != gid)
1577                         continue;
1578                 add_uid_to_array_unique(mem_ctx, user->pw_uid, pp_uids, p_num);
1579         }
1580
1581         pwent_free(userlist);
1582
1583         /* Secondary group members */
1584
1585         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1586                 struct passwd *pw = getpwnam(*gr);
1587
1588                 if (pw == NULL)
1589                         continue;
1590                 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1591         }
1592
1593         winbind_on();
1594
1595         return True;
1596 }
1597
1598 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1599                                         TALLOC_CTX *mem_ctx,
1600                                         const DOM_SID *group,
1601                                         uint32 **pp_member_rids,
1602                                         size_t *p_num_members)
1603 {
1604         gid_t gid;
1605         uid_t *uids;
1606         size_t i, num_uids;
1607
1608         *pp_member_rids = NULL;
1609         *p_num_members = 0;
1610
1611         if (!NT_STATUS_IS_OK(sid_to_gid(group, &gid)))
1612                 return NT_STATUS_NO_SUCH_GROUP;
1613
1614         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1615                 return NT_STATUS_NO_SUCH_GROUP;
1616
1617         if (num_uids == 0)
1618                 return NT_STATUS_OK;
1619
1620         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1621
1622         for (i=0; i<num_uids; i++) {
1623                 DOM_SID sid;
1624
1625                 if (!NT_STATUS_IS_OK(uid_to_sid(&sid, uids[i]))) {
1626                         DEBUG(1, ("Could not map member uid to SID\n"));
1627                         continue;
1628                 }
1629
1630                 if (!sid_check_is_in_our_domain(&sid)) {
1631                         DEBUG(1, ("Inconsistent SAM -- group member uid not "
1632                                   "in our domain\n"));
1633                         continue;
1634                 }
1635
1636                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1637                 *p_num_members += 1;
1638         }
1639
1640         return NT_STATUS_OK;
1641 }
1642
1643 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1644                                  const DOM_SID *domain_sid,
1645                                  int num_rids,
1646                                  uint32 *rids,
1647                                  const char **names,
1648                                  uint32 *attrs)
1649 {
1650         int i;
1651         NTSTATUS result;
1652         BOOL have_mapped = False;
1653         BOOL have_unmapped = False;
1654
1655         if (sid_check_is_builtin(domain_sid)) {
1656
1657                 for (i=0; i<num_rids; i++) {
1658                         fstring name;
1659
1660                         if (lookup_builtin_rid(rids[i], name)) {
1661                                 attrs[i] = SID_NAME_ALIAS;
1662                                 names[i] = talloc_strdup(names, name);
1663                                 if (names[i] == NULL) {
1664                                         return NT_STATUS_NO_MEMORY;
1665                                 }
1666                                 DEBUG(5,("lookup_rids: %s:%d\n",
1667                                          names[i], attrs[i]));
1668                                 have_mapped = True;
1669                         } else {
1670                                 have_unmapped = True;
1671                                 attrs[i] = SID_NAME_UNKNOWN;
1672                         }
1673                 }
1674                 goto done;
1675         }
1676
1677         /* Should not happen, but better check once too many */
1678         if (!sid_check_is_domain(domain_sid)) {
1679                 return NT_STATUS_INVALID_HANDLE;
1680         }
1681
1682         for (i = 0; i < num_rids; i++) {
1683                 fstring tmpname;
1684                 enum SID_NAME_USE type;
1685
1686                 if (lookup_global_sam_rid(rids[i], tmpname, &type)) {
1687                         attrs[i] = (uint32)type;
1688                         names[i] = talloc_strdup(names, tmpname);
1689                         if (names[i] == NULL)
1690                                 return NT_STATUS_NO_MEMORY;
1691                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1692                         have_mapped = True;
1693                 } else {
1694                         have_unmapped = True;
1695                         attrs[i] = SID_NAME_UNKNOWN;
1696                 }
1697         }
1698
1699  done:
1700
1701         result = NT_STATUS_NONE_MAPPED;
1702
1703         if (have_mapped)
1704                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1705
1706         return result;
1707 }
1708
1709 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1710 {
1711         TALLOC_CTX *mem_ctx;
1712         struct pdb_search *result;
1713
1714         mem_ctx = talloc_init("pdb_search");
1715         if (mem_ctx == NULL) {
1716                 DEBUG(0, ("talloc_init failed\n"));
1717                 return NULL;
1718         }
1719
1720         result = TALLOC_P(mem_ctx, struct pdb_search);
1721         if (result == NULL) {
1722                 DEBUG(0, ("talloc failed\n"));
1723                 return NULL;
1724         }
1725
1726         result->mem_ctx = mem_ctx;
1727         result->type = type;
1728         result->cache = NULL;
1729         result->num_entries = 0;
1730         result->cache_size = 0;
1731         result->search_ended = False;
1732
1733         /* Segfault appropriately if not initialized */
1734         result->next_entry = NULL;
1735         result->search_end = NULL;
1736
1737         return result;
1738 }
1739
1740 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1741                               uint16 acct_flags,
1742                               const char *account_name,
1743                               const char *fullname,
1744                               const char *description,
1745                               struct samr_displayentry *entry)
1746 {
1747         entry->rid = rid;
1748         entry->acct_flags = acct_flags;
1749
1750         if (account_name != NULL)
1751                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1752         else
1753                 entry->account_name = "";
1754
1755         if (fullname != NULL)
1756                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1757         else
1758                 entry->fullname = "";
1759
1760         if (description != NULL)
1761                 entry->description = talloc_strdup(mem_ctx, description);
1762         else
1763                 entry->description = "";
1764 }
1765
1766 static BOOL user_search_in_progress = False;
1767 struct user_search {
1768         uint16 acct_flags;
1769 };
1770
1771 static BOOL next_entry_users(struct pdb_search *s,
1772                              struct samr_displayentry *entry)
1773 {
1774         struct user_search *state = s->private_data;
1775         SAM_ACCOUNT *user = NULL;
1776         NTSTATUS status;
1777
1778  next:
1779         status = pdb_init_sam(&user);
1780         if (!NT_STATUS_IS_OK(status)) {
1781                 DEBUG(0, ("Could not pdb_init_sam\n"));
1782                 return False;
1783         }
1784
1785         if (!pdb_getsampwent(user)) {
1786                 pdb_free_sam(&user);
1787                 return False;
1788         }
1789
1790         if ((state->acct_flags != 0) &&
1791             ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1792                 pdb_free_sam(&user);
1793                 goto next;
1794         }
1795
1796         fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1797                           pdb_get_acct_ctrl(user), pdb_get_username(user),
1798                           pdb_get_fullname(user), pdb_get_acct_desc(user),
1799                           entry);
1800
1801         pdb_free_sam(&user);
1802         return True;
1803 }
1804
1805 static void search_end_users(struct pdb_search *search)
1806 {
1807         pdb_endsampwent();
1808         user_search_in_progress = False;
1809 }
1810
1811 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1812                                      struct pdb_search *search,
1813                                      uint16 acct_flags)
1814 {
1815         struct user_search *state;
1816
1817         if (user_search_in_progress) {
1818                 DEBUG(1, ("user search in progress\n"));
1819                 return False;
1820         }
1821
1822         if (!pdb_setsampwent(False, acct_flags)) {
1823                 DEBUG(5, ("Could not start search\n"));
1824                 return False;
1825         }
1826
1827         user_search_in_progress = True;
1828
1829         state = TALLOC_P(search->mem_ctx, struct user_search);
1830         if (state == NULL) {
1831                 DEBUG(0, ("talloc failed\n"));
1832                 return False;
1833         }
1834
1835         state->acct_flags = acct_flags;
1836
1837         search->private_data = state;
1838         search->next_entry = next_entry_users;
1839         search->search_end = search_end_users;
1840         return True;
1841 }
1842
1843 struct group_search {
1844         GROUP_MAP *groups;
1845         size_t num_groups, current_group;
1846 };
1847
1848 static BOOL next_entry_groups(struct pdb_search *s,
1849                               struct samr_displayentry *entry)
1850 {
1851         struct group_search *state = s->private_data;
1852         uint32 rid;
1853         GROUP_MAP *map = &state->groups[state->current_group];
1854
1855         if (state->current_group == state->num_groups)
1856                 return False;
1857
1858         sid_peek_rid(&map->sid, &rid);
1859
1860         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1861                           entry);
1862
1863         state->current_group += 1;
1864         return True;
1865 }
1866
1867 static void search_end_groups(struct pdb_search *search)
1868 {
1869         struct group_search *state = search->private_data;
1870         SAFE_FREE(state->groups);
1871 }
1872
1873 static BOOL pdb_search_grouptype(struct pdb_search *search,
1874                                  enum SID_NAME_USE type)
1875 {
1876         struct group_search *state;
1877
1878         state = TALLOC_P(search->mem_ctx, struct group_search);
1879         if (state == NULL) {
1880                 DEBUG(0, ("talloc failed\n"));
1881                 return False;
1882         }
1883
1884         if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1885                                     True)) {
1886                 DEBUG(0, ("Could not enum groups\n"));
1887                 return False;
1888         }
1889
1890         state->current_group = 0;
1891         search->private_data = state;
1892         search->next_entry = next_entry_groups;
1893         search->search_end = search_end_groups;
1894         return True;
1895 }
1896
1897 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1898                                       struct pdb_search *search)
1899 {
1900         return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1901 }
1902
1903 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1904                                        struct pdb_search *search,
1905                                        const DOM_SID *sid)
1906 {
1907
1908         if (sid_equal(sid, get_global_sam_sid()))
1909                 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1910
1911         if (sid_equal(sid, &global_sid_Builtin))
1912                 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
1913
1914         DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1915         return False;
1916 }
1917
1918 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1919                                                      uint32 idx)
1920 {
1921         if (idx < search->num_entries)
1922                 return &search->cache[idx];
1923
1924         if (search->search_ended)
1925                 return NULL;
1926
1927         while (idx >= search->num_entries) {
1928                 struct samr_displayentry entry;
1929
1930                 if (!search->next_entry(search, &entry)) {
1931                         search->search_end(search);
1932                         search->search_ended = True;
1933                         break;
1934                 }
1935
1936                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1937                                    entry, &search->cache, &search->num_entries,
1938                                    &search->cache_size);
1939         }
1940
1941         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1942 }
1943
1944 struct pdb_search *pdb_search_users(uint16 acct_flags)
1945 {
1946         struct pdb_context *pdb_context = pdb_get_static_context(False);
1947         struct pdb_search *result;
1948
1949         if (pdb_context == NULL) return NULL;
1950
1951         result = pdb_search_init(PDB_USER_SEARCH);
1952         if (result == NULL) return NULL;
1953
1954         if (!pdb_context->pdb_search_users(pdb_context, result, acct_flags)) {
1955                 talloc_destroy(result->mem_ctx);
1956                 return NULL;
1957         }
1958         return result;
1959 }
1960
1961 struct pdb_search *pdb_search_groups(void)
1962 {
1963         struct pdb_context *pdb_context = pdb_get_static_context(False);
1964         struct pdb_search *result;
1965
1966         if (pdb_context == NULL) return NULL;
1967
1968         result = pdb_search_init(PDB_GROUP_SEARCH);
1969         if (result == NULL) return NULL;
1970
1971         if (!pdb_context->pdb_search_groups(pdb_context, result)) {
1972                 talloc_destroy(result->mem_ctx);
1973                 return NULL;
1974         }
1975         return result;
1976 }
1977
1978 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1979 {
1980         struct pdb_context *pdb_context = pdb_get_static_context(False);
1981         struct pdb_search *result;
1982
1983         if (pdb_context == NULL) return NULL;
1984
1985         result = pdb_search_init(PDB_ALIAS_SEARCH);
1986         if (result == NULL) return NULL;
1987
1988         if (!pdb_context->pdb_search_aliases(pdb_context, result, sid)) {
1989                 talloc_destroy(result->mem_ctx);
1990                 return NULL;
1991         }
1992         return result;
1993 }
1994
1995 uint32 pdb_search_entries(struct pdb_search *search,
1996                           uint32 start_idx, uint32 max_entries,
1997                           struct samr_displayentry **result)
1998 {
1999         struct samr_displayentry *end_entry;
2000         uint32 end_idx = start_idx+max_entries-1;
2001
2002         /* The first entry needs to be searched after the last. Otherwise the
2003          * first entry might have moved due to a realloc during the search for
2004          * the last entry. */
2005
2006         end_entry = pdb_search_getentry(search, end_idx);
2007         *result = pdb_search_getentry(search, start_idx);
2008
2009         if (end_entry != NULL)
2010                 return max_entries;
2011
2012         if (start_idx >= search->num_entries)
2013                 return 0;
2014
2015         return search->num_entries - start_idx;
2016 }
2017
2018 void pdb_search_destroy(struct pdb_search *search)
2019 {
2020         if (search == NULL)
2021                 return;
2022
2023         if (!search->search_ended)
2024                 search->search_end(search);
2025
2026         talloc_destroy(search->mem_ctx);
2027 }
2028
2029 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
2030 {
2031         *methods = TALLOC_P(mem_ctx, struct pdb_methods);
2032
2033         if (!*methods) {
2034                 return NT_STATUS_NO_MEMORY;
2035         }
2036
2037         ZERO_STRUCTP(*methods);
2038
2039         (*methods)->setsampwent = pdb_default_setsampwent;
2040         (*methods)->endsampwent = pdb_default_endsampwent;
2041         (*methods)->getsampwent = pdb_default_getsampwent;
2042         (*methods)->getsampwnam = pdb_default_getsampwnam;
2043         (*methods)->getsampwsid = pdb_default_getsampwsid;
2044         (*methods)->add_sam_account = pdb_default_add_sam_account;
2045         (*methods)->update_sam_account = pdb_default_update_sam_account;
2046         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2047         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2048         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2049
2050         (*methods)->getgrsid = pdb_default_getgrsid;
2051         (*methods)->getgrgid = pdb_default_getgrgid;
2052         (*methods)->getgrnam = pdb_default_getgrnam;
2053         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2054         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2055         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2056         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2057         (*methods)->enum_group_members = pdb_default_enum_group_members;
2058         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2059         (*methods)->find_alias = pdb_default_find_alias;
2060         (*methods)->create_alias = pdb_default_create_alias;
2061         (*methods)->delete_alias = pdb_default_delete_alias;
2062         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2063         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2064         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2065         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2066         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2067         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2068         (*methods)->lookup_rids = pdb_default_lookup_rids;
2069         (*methods)->get_account_policy = pdb_default_get_account_policy;
2070         (*methods)->set_account_policy = pdb_default_set_account_policy;
2071         (*methods)->get_seq_num = pdb_default_get_seq_num;
2072
2073         (*methods)->search_users = pdb_default_search_users;
2074         (*methods)->search_groups = pdb_default_search_groups;
2075         (*methods)->search_aliases = pdb_default_search_aliases;
2076
2077         return NT_STATUS_OK;
2078 }