import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / 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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_PASSDB
26
27 static struct pdb_init_function_entry *backends = NULL;
28
29 static void lazy_initialize_passdb(void)
30 {
31         static BOOL initialized = False;
32         if(initialized)return;
33         static_init_pdb;
34         initialized = True;
35 }
36
37 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
38
39 /*******************************************************************
40  Clean up uninitialised passwords.  The only way to tell 
41  that these values are not 'real' is that they do not
42  have a valid last set time.  Instead, the value is fixed at 0. 
43  Therefore we use that as the key for 'is this a valid password'.
44  However, it is perfectly valid to have a 'default' last change
45  time, such LDAP with a missing attribute would produce.
46 ********************************************************************/
47
48 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass) 
49 {
50         const char *lm_pwd, *nt_pwd;
51         
52         /* only reset a password if the last set time has been 
53            explicitly been set to zero.  A default last set time 
54            is ignored */
55
56         if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT) 
57                 && (pdb_get_pass_last_set_time(pass) == 0) ) 
58         {
59                 
60                 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT) 
61                 {
62                         lm_pwd = pdb_get_lanman_passwd(pass);
63                         if (lm_pwd) 
64                                 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
65                 }
66                 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT) 
67                 {
68                         nt_pwd = pdb_get_nt_passwd(pass);
69                         if (nt_pwd) 
70                                 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
71                 }
72         }
73
74         return;
75 }
76
77 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
78 {
79         struct pdb_init_function_entry *entry = backends;
80
81         if(version != PASSDB_INTERFACE_VERSION) {
82                 DEBUG(0,("Can't register passdb backend!\n"
83                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
84                          "while this version of samba uses version %d\n", 
85                          version,PASSDB_INTERFACE_VERSION));
86                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
87         }
88
89         if (!name || !init) {
90                 return NT_STATUS_INVALID_PARAMETER;
91         }
92
93         DEBUG(5,("Attempting to register passdb backend %s\n", name));
94
95         /* Check for duplicates */
96         if (pdb_find_backend_entry(name)) {
97                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
98                 return NT_STATUS_OBJECT_NAME_COLLISION;
99         }
100
101         entry = smb_xmalloc(sizeof(struct pdb_init_function_entry));
102         entry->name = smb_xstrdup(name);
103         entry->init = init;
104
105         DLIST_ADD(backends, entry);
106         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
107         return NT_STATUS_OK;
108 }
109
110 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
111 {
112         struct pdb_init_function_entry *entry = backends;
113
114         while(entry) {
115                 if (strcmp(entry->name, name)==0) return entry;
116                 entry = entry->next;
117         }
118
119         return NULL;
120 }
121
122 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update)
123 {
124         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
125
126         if (!context) {
127                 DEBUG(0, ("invalid pdb_context specified!\n"));
128                 return ret;
129         }
130
131         context->pwent_methods = context->pdb_methods;
132
133         if (!context->pwent_methods) {
134                 /* No passdbs at all */
135                 return ret;
136         }
137
138         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->setsampwent(context->pwent_methods, update))) {
139                 context->pwent_methods = context->pwent_methods->next;
140                 if (context->pwent_methods == NULL) 
141                         return NT_STATUS_UNSUCCESSFUL;
142         }
143         return ret;
144 }
145
146 static void context_endsampwent(struct pdb_context *context)
147 {
148         if ((!context)){
149                 DEBUG(0, ("invalid pdb_context specified!\n"));
150                 return;
151         }
152
153         if (context->pwent_methods && context->pwent_methods->endsampwent)
154                 context->pwent_methods->endsampwent(context->pwent_methods);
155
156         /* So we won't get strange data when calling getsampwent now */
157         context->pwent_methods = NULL;
158 }
159
160 static NTSTATUS context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
161 {
162         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
163
164         if ((!context) || (!context->pwent_methods)) {
165                 DEBUG(0, ("invalid pdb_context specified!\n"));
166                 return ret;
167         }
168         /* Loop until we find something useful */
169         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->getsampwent(context->pwent_methods, user))) {
170
171                 context->pwent_methods->endsampwent(context->pwent_methods);
172
173                 context->pwent_methods = context->pwent_methods->next;
174
175                 /* All methods are checked now. There are no more entries */
176                 if (context->pwent_methods == NULL)
177                         return ret;
178         
179                 context->pwent_methods->setsampwent(context->pwent_methods, False);
180         }
181         user->methods = context->pwent_methods;
182         pdb_force_pw_initialization(user);
183         return ret;
184 }
185
186 static NTSTATUS context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
187 {
188         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
189
190         struct pdb_methods *curmethods;
191         if ((!context)) {
192                 DEBUG(0, ("invalid pdb_context specified!\n"));
193                 return ret;
194         }
195         curmethods = context->pdb_methods;
196         while (curmethods){
197                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwnam(curmethods, sam_acct, username))) {
198                         pdb_force_pw_initialization(sam_acct);
199                         sam_acct->methods = curmethods;
200                         return ret;
201                 }
202                 curmethods = curmethods->next;
203         }
204
205         return ret;
206 }
207
208 static NTSTATUS context_getsampwsid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
209 {
210         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
211
212         struct pdb_methods *curmethods;
213         if ((!context)) {
214                 DEBUG(0, ("invalid pdb_context specified!\n"));
215                 return ret;
216         }
217         
218         curmethods = context->pdb_methods;
219
220         while (curmethods){
221                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwsid(curmethods, sam_acct, sid))) {
222                         pdb_force_pw_initialization(sam_acct);
223                         sam_acct->methods = curmethods;
224                         return ret;
225                 }
226                 curmethods = curmethods->next;
227         }
228
229         return ret;
230 }
231
232 static NTSTATUS context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
233 {
234         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
235
236         if ((!context) || (!context->pdb_methods)) {
237                 DEBUG(0, ("invalid pdb_context specified!\n"));
238                 return ret;
239         }
240
241         /** @todo  This is where a 're-read on add' should be done */
242         /* We now add a new account to the first database listed. 
243          * Should we? */
244
245         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
246 }
247
248 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
249 {
250         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
251
252         if (!context) {
253                 DEBUG(0, ("invalid pdb_context specified!\n"));
254                 return ret;
255         }
256
257         if (!sam_acct || !sam_acct->methods){
258                 DEBUG(0, ("invalid sam_acct specified\n"));
259                 return ret;
260         }
261
262         /** @todo  This is where a 're-read on update' should be done */
263
264         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
265 }
266
267 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
268 {
269         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
270
271         struct pdb_methods *pdb_selected;
272         if (!context) {
273                 DEBUG(0, ("invalid pdb_context specified!\n"));
274                 return ret;
275         }
276
277         if (!sam_acct->methods){
278                 pdb_selected = context->pdb_methods;
279                 /* There's no passdb backend specified for this account.
280                  * Try to delete it in every passdb available 
281                  * Needed to delete accounts in smbpasswd that are not
282                  * in /etc/passwd.
283                  */
284                 while (pdb_selected){
285                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
286                                 return ret;
287                         }
288                         pdb_selected = pdb_selected->next;
289                 }
290                 return ret;
291         }
292
293         if (!sam_acct->methods->delete_sam_account){
294                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
295                 return ret;
296         }
297         
298         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
299 }
300
301 static NTSTATUS context_getgrsid(struct pdb_context *context,
302                                  GROUP_MAP *map, DOM_SID sid)
303 {
304         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
305
306         struct pdb_methods *curmethods;
307         if ((!context)) {
308                 DEBUG(0, ("invalid pdb_context specified!\n"));
309                 return ret;
310         }
311         curmethods = context->pdb_methods;
312         while (curmethods){
313                 ret = curmethods->getgrsid(curmethods, map, sid);
314                 if (NT_STATUS_IS_OK(ret)) {
315                         map->methods = curmethods;
316                         return ret;
317                 }
318                 curmethods = curmethods->next;
319         }
320
321         return ret;
322 }
323
324 static NTSTATUS context_getgrgid(struct pdb_context *context,
325                                  GROUP_MAP *map, gid_t gid)
326 {
327         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
328
329         struct pdb_methods *curmethods;
330         if ((!context)) {
331                 DEBUG(0, ("invalid pdb_context specified!\n"));
332                 return ret;
333         }
334         curmethods = context->pdb_methods;
335         while (curmethods){
336                 ret = curmethods->getgrgid(curmethods, map, gid);
337                 if (NT_STATUS_IS_OK(ret)) {
338                         map->methods = curmethods;
339                         return ret;
340                 }
341                 curmethods = curmethods->next;
342         }
343
344         return ret;
345 }
346
347 static NTSTATUS context_getgrnam(struct pdb_context *context,
348                                  GROUP_MAP *map, const char *name)
349 {
350         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
351
352         struct pdb_methods *curmethods;
353         if ((!context)) {
354                 DEBUG(0, ("invalid pdb_context specified!\n"));
355                 return ret;
356         }
357         curmethods = context->pdb_methods;
358         while (curmethods){
359                 ret = curmethods->getgrnam(curmethods, map, name);
360                 if (NT_STATUS_IS_OK(ret)) {
361                         map->methods = curmethods;
362                         return ret;
363                 }
364                 curmethods = curmethods->next;
365         }
366
367         return ret;
368 }
369
370 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
371                                                 GROUP_MAP *map)
372 {
373         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
374
375         if ((!context) || (!context->pdb_methods)) {
376                 DEBUG(0, ("invalid pdb_context specified!\n"));
377                 return ret;
378         }
379
380         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
381                                                              map);
382 }
383
384 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
385                                                    GROUP_MAP *map)
386 {
387         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
388
389         if ((!context) || (!context->pdb_methods)) {
390                 DEBUG(0, ("invalid pdb_context specified!\n"));
391                 return ret;
392         }
393
394         return context->
395                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
396 }
397
398 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
399                                                    DOM_SID sid)
400 {
401         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
402
403         if ((!context) || (!context->pdb_methods)) {
404                 DEBUG(0, ("invalid pdb_context specified!\n"));
405                 return ret;
406         }
407
408         return context->
409                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
410 }
411
412 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
413                                            enum SID_NAME_USE sid_name_use,
414                                            GROUP_MAP **rmap, int *num_entries,
415                                            BOOL unix_only)
416 {
417         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
418
419         if ((!context) || (!context->pdb_methods)) {
420                 DEBUG(0, ("invalid pdb_context specified!\n"));
421                 return ret;
422         }
423
424         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
425                                                         sid_name_use, rmap,
426                                                         num_entries, unix_only);
427 }
428
429 static NTSTATUS context_find_alias(struct pdb_context *context,
430                                    const char *name, DOM_SID *sid)
431 {
432         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
433
434         if ((!context) || (!context->pdb_methods)) {
435                 DEBUG(0, ("invalid pdb_context specified!\n"));
436                 return ret;
437         }
438
439         return context->pdb_methods->find_alias(context->pdb_methods,
440                                                 name, sid);
441 }
442
443 static NTSTATUS context_create_alias(struct pdb_context *context,
444                                      const char *name, uint32 *rid)
445 {
446         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
447
448         if ((!context) || (!context->pdb_methods)) {
449                 DEBUG(0, ("invalid pdb_context specified!\n"));
450                 return ret;
451         }
452
453         return context->pdb_methods->create_alias(context->pdb_methods,
454                                                   name, rid);
455 }
456
457 static NTSTATUS context_delete_alias(struct pdb_context *context,
458                                      const DOM_SID *sid)
459 {
460         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
461
462         if ((!context) || (!context->pdb_methods)) {
463                 DEBUG(0, ("invalid pdb_context specified!\n"));
464                 return ret;
465         }
466
467         return context->pdb_methods->delete_alias(context->pdb_methods, sid);
468 }
469
470 static NTSTATUS context_enum_aliases(struct pdb_context *context,
471                                      const DOM_SID *sid,
472                                      uint32 start_idx, uint32 max_entries,
473                                      uint32 *num_aliases,
474                                      struct acct_info **info)
475 {
476         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
477
478         if ((!context) || (!context->pdb_methods)) {
479                 DEBUG(0, ("invalid pdb_context specified!\n"));
480                 return ret;
481         }
482
483         return context->pdb_methods->enum_aliases(context->pdb_methods,
484                                                   sid, start_idx, max_entries,
485                                                   num_aliases, info);
486 }
487
488 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
489                                       const DOM_SID *sid,
490                                       struct acct_info *info)
491 {
492         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
493
494         if ((!context) || (!context->pdb_methods)) {
495                 DEBUG(0, ("invalid pdb_context specified!\n"));
496                 return ret;
497         }
498
499         return context->pdb_methods->get_aliasinfo(context->pdb_methods,
500                                                    sid, info);
501 }
502
503 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
504                                       const DOM_SID *sid,
505                                       struct acct_info *info)
506 {
507         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
508
509         if ((!context) || (!context->pdb_methods)) {
510                 DEBUG(0, ("invalid pdb_context specified!\n"));
511                 return ret;
512         }
513
514         return context->pdb_methods->set_aliasinfo(context->pdb_methods,
515                                                    sid, info);
516 }
517
518 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
519                                      const DOM_SID *alias,
520                                      const DOM_SID *member)
521 {
522         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
523
524         if ((!context) || (!context->pdb_methods)) {
525                 DEBUG(0, ("invalid pdb_context specified!\n"));
526                 return ret;
527         }
528
529         return context->pdb_methods->add_aliasmem(context->pdb_methods,
530                                                   alias, member);
531 }
532         
533 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
534                                      const DOM_SID *alias,
535                                      const DOM_SID *member)
536 {
537         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
538
539         if ((!context) || (!context->pdb_methods)) {
540                 DEBUG(0, ("invalid pdb_context specified!\n"));
541                 return ret;
542         }
543
544         return context->pdb_methods->del_aliasmem(context->pdb_methods,
545                                                   alias, member);
546 }
547         
548 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
549                                       const DOM_SID *alias, DOM_SID **members,
550                                       int *num)
551 {
552         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
553
554         if ((!context) || (!context->pdb_methods)) {
555                 DEBUG(0, ("invalid pdb_context specified!\n"));
556                 return ret;
557         }
558
559         return context->pdb_methods->enum_aliasmem(context->pdb_methods,
560                                                    alias, members, num);
561 }
562         
563 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
564                                                const DOM_SID *sid,
565                                                DOM_SID **aliases, int *num)
566 {
567         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
568
569         if ((!context) || (!context->pdb_methods)) {
570                 DEBUG(0, ("invalid pdb_context specified!\n"));
571                 return ret;
572         }
573
574         return context->pdb_methods->
575                 enum_alias_memberships(context->pdb_methods, sid, aliases,
576                                        num);
577 }
578         
579 static NTSTATUS context_settrustpwent(struct pdb_context *context)
580 {
581         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
582         struct pdb_methods *cur_methods;
583         
584         if (!context) {
585                 DEBUG(0, ("invalid pdb_context specified!\n"));
586                 return ret;
587         }
588         
589         cur_methods = context->pdb_methods;
590         
591         while (cur_methods) {
592                 ret = cur_methods->settrustpwent(cur_methods);
593                 if (NT_STATUS_IS_OK(ret)) {
594                         context->pdb_methods = cur_methods;
595                         return ret;
596                 }
597                 cur_methods = cur_methods->next;
598         }
599         
600         return ret;
601 }
602
603 static NTSTATUS context_gettrustpwent(struct pdb_context *context,
604                                       SAM_TRUST_PASSWD *trust)
605 {
606         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
607         struct pdb_methods *cur_methods;
608         
609         if (!context) {
610                 DEBUG(0, ("invalid pdb_context specified!\n"));
611                 return ret;
612         }
613         
614         cur_methods = context->pdb_methods;
615         
616         while (cur_methods) {
617                 ret = cur_methods->gettrustpwent(cur_methods, trust);
618                 if (!NT_STATUS_IS_ERR(ret)) {
619                         /* prevent from segfaulting when gettrustpwent
620                            was called just to rewind enumeration */
621                         if (trust) trust->methods = cur_methods;
622                         return ret;
623                 }
624                 cur_methods = cur_methods->next;
625         }
626         
627         return ret;
628 }
629
630 static NTSTATUS context_gettrustpwnam(struct pdb_context *context,
631                                       SAM_TRUST_PASSWD *trust,
632                                       const char *name)
633 {
634         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
635         struct pdb_methods *cur_methods;
636         
637         if (!context) {
638                 DEBUG(0, ("invalid pdb_context specified!\n"));
639                 return ret;
640         }
641         
642         cur_methods = context->pdb_methods;
643         
644         while (cur_methods) {
645                 ret = cur_methods->gettrustpwnam(cur_methods, trust, name);
646                 if (NT_STATUS_IS_OK(ret)) {
647                         trust->methods = cur_methods;
648                         return ret;
649                 }
650                 cur_methods = cur_methods->next;
651         }
652         
653         return ret;
654 }
655
656 static NTSTATUS context_gettrustpwsid(struct pdb_context *context,
657                                       SAM_TRUST_PASSWD *trust,
658                                       const DOM_SID *sid)
659 {
660         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
661         struct pdb_methods *cur_methods;
662         
663         if (!context) {
664                 DEBUG(0, ("invalid pdb_context specified!\n"));
665                 return ret;
666         }
667         
668         cur_methods = context->pdb_methods;
669         
670         while (cur_methods) {
671                 ret = cur_methods->gettrustpwsid(cur_methods, trust, sid);
672                 if (NT_STATUS_IS_OK(ret)) {
673                         trust->methods = cur_methods;
674                         return ret;
675                 }
676                 cur_methods = cur_methods->next;
677         }
678         
679         return ret;
680 }
681
682 static NTSTATUS context_add_trust_passwd(struct pdb_context *context,
683                                          SAM_TRUST_PASSWD *trust)
684 {
685         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
686         
687         if (!context) {
688                 DEBUG(0, ("invalid pdb_context specified!\n"));
689                 return ret;
690         }
691         
692         return context->pdb_methods->add_trust_passwd(context->pdb_methods, trust);
693 }
694
695 static NTSTATUS context_update_trust_passwd(struct pdb_context *context,
696                                             SAM_TRUST_PASSWD *trust)
697 {
698         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
699         
700         if (!context) {
701                 DEBUG(0, ("invalid pdb_context specified!\n"));
702                 return ret;
703         }
704         
705         if (!trust || !trust->methods) {
706                 DEBUG(0, ("invalid trust pointer specified!\n"));
707                 return ret;
708         }
709         
710         return trust->methods->update_trust_passwd(trust->methods, trust);
711 }
712
713 static NTSTATUS context_delete_trust_passwd(struct pdb_context *context,
714                                             SAM_TRUST_PASSWD *trust)
715 {
716         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
717         
718         if (!context) {
719                 DEBUG(0, ("invalid pdb_context specified!\n"));
720                 return ret;
721         }
722         
723         if (!trust || !trust->methods) {
724                 DEBUG(0, ("invalid trust pointer specified!\n"));
725                 return ret;
726         }
727         
728         return trust->methods->delete_trust_passwd(trust->methods, trust);
729 }
730
731 static NTSTATUS context_add_sid_to_privilege(struct pdb_context *context, const char *priv_name, const DOM_SID *sid)
732 {
733         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
734
735         struct pdb_methods *curmethods;
736         if ((!context)) {
737                 DEBUG(0, ("invalid pdb_context specified!\n"));
738                 return ret;
739         }
740         curmethods = context->pdb_methods;
741         while (curmethods){
742                 if (NT_STATUS_IS_OK(ret = curmethods->add_sid_to_privilege(curmethods, priv_name, sid))) {
743                         return ret;
744                 }
745                 curmethods = curmethods->next;
746         }
747
748         return ret;
749 }
750
751 static NTSTATUS context_remove_sid_from_privilege(struct pdb_context *context, const char *priv_name, const DOM_SID *sid)
752 {
753         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
754
755         struct pdb_methods *curmethods;
756         if ((!context)) {
757                 DEBUG(0, ("invalid pdb_context specified!\n"));
758                 return ret;
759         }
760         curmethods = context->pdb_methods;
761         while (curmethods){
762                 if (NT_STATUS_IS_OK(ret = curmethods->remove_sid_from_privilege(curmethods, priv_name, sid))) {
763                         return ret;
764                 }
765                 curmethods = curmethods->next;
766         }
767
768         return ret;
769 }
770
771 static NTSTATUS context_get_privilege_set(struct pdb_context *context, DOM_SID *sid_list, int num_sids, PRIVILEGE_SET *privset)
772 {
773         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
774
775         struct pdb_methods *curmethods;
776         if ((!context)) {
777                 DEBUG(0, ("invalid pdb_context specified!\n"));
778                 return ret;
779         }
780         curmethods = context->pdb_methods;
781         while (curmethods){
782                 if (NT_STATUS_IS_OK(ret = curmethods->get_privilege_set(curmethods, sid_list, num_sids, privset))) {
783                         return ret;
784                 }
785                 curmethods = curmethods->next;
786         }
787
788         return ret;
789 }
790
791 static NTSTATUS context_get_privilege_entry(struct pdb_context *context, const char *privname, char **sid_list)
792 {
793         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
794
795         struct pdb_methods *curmethods;
796         if ((!context)) {
797                 DEBUG(0, ("invalid pdb_context specified!\n"));
798                 return ret;
799         }
800         curmethods = context->pdb_methods;
801         while (curmethods){
802                 if (NT_STATUS_IS_OK(ret = curmethods->get_privilege_entry(curmethods, privname, sid_list))) {
803                         return ret;
804                 }
805                 curmethods = curmethods->next;
806         }
807
808         return ret;
809 }
810
811 /******************************************************************
812   Free and cleanup a pdb context, any associated data and anything
813   that the attached modules might have associated.
814  *******************************************************************/
815
816 static void free_pdb_context(struct pdb_context **context)
817 {
818         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
819
820         while (pdb_selected){
821                 if(pdb_selected->free_private_data)
822                         pdb_selected->free_private_data(&(pdb_selected->private_data));
823                 pdb_selected = pdb_selected->next;
824         }
825
826         talloc_destroy((*context)->mem_ctx);
827         *context = NULL;
828 }
829
830 /******************************************************************
831   Make a pdb_methods from scratch
832  *******************************************************************/
833
834 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
835 {
836         char *module_name = smb_xstrdup(selected);
837         char *module_location = NULL, *p;
838         struct pdb_init_function_entry *entry;
839         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
840
841         lazy_initialize_passdb();
842
843         p = strchr(module_name, ':');
844
845         if (p) {
846                 *p = 0;
847                 module_location = p+1;
848                 trim_char(module_location, ' ', ' ');
849         }
850
851         trim_char(module_name, ' ', ' ');
852
853
854         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
855
856         entry = pdb_find_backend_entry(module_name);
857         
858         /* Try to find a module that contains this module */
859         if (!entry) { 
860                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
861                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
862                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
863                         SAFE_FREE(module_name);
864                         return NT_STATUS_UNSUCCESSFUL;
865                 }
866         }
867         
868         /* No such backend found */
869         if(!entry) { 
870                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
871                 SAFE_FREE(module_name);
872                 return NT_STATUS_INVALID_PARAMETER;
873         }
874
875         DEBUG(5,("Found pdb backend %s\n", module_name));
876         nt_status = entry->init(context, methods, module_location);
877         if (NT_STATUS_IS_OK(nt_status)) {
878                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
879         } else {
880                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
881         }
882         SAFE_FREE(module_name);
883         return nt_status;
884 }
885
886 /******************************************************************
887   Make a pdb_context from scratch.
888  *******************************************************************/
889
890 static NTSTATUS make_pdb_context(struct pdb_context **context) 
891 {
892         TALLOC_CTX *mem_ctx;
893
894         mem_ctx = talloc_init("pdb_context internal allocation context");
895
896         if (!mem_ctx) {
897                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
898                 return NT_STATUS_NO_MEMORY;
899         }               
900
901         *context = talloc(mem_ctx, sizeof(**context));
902         if (!*context) {
903                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
904                 return NT_STATUS_NO_MEMORY;
905         }
906
907         ZERO_STRUCTP(*context);
908
909         (*context)->mem_ctx = mem_ctx;
910
911         (*context)->pdb_setsampwent = context_setsampwent;
912         (*context)->pdb_endsampwent = context_endsampwent;
913         (*context)->pdb_getsampwent = context_getsampwent;
914         (*context)->pdb_getsampwnam = context_getsampwnam;
915         (*context)->pdb_getsampwsid = context_getsampwsid;
916         (*context)->pdb_add_sam_account = context_add_sam_account;
917         (*context)->pdb_update_sam_account = context_update_sam_account;
918         (*context)->pdb_delete_sam_account = context_delete_sam_account;
919         (*context)->pdb_getgrsid = context_getgrsid;
920         (*context)->pdb_getgrgid = context_getgrgid;
921         (*context)->pdb_getgrnam = context_getgrnam;
922         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
923         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
924         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
925         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
926         (*context)->pdb_find_alias = context_find_alias;
927         (*context)->pdb_create_alias = context_create_alias;
928         (*context)->pdb_delete_alias = context_delete_alias;
929         (*context)->pdb_enum_aliases = context_enum_aliases;
930         (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
931         (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
932         (*context)->pdb_add_aliasmem = context_add_aliasmem;
933         (*context)->pdb_del_aliasmem = context_del_aliasmem;
934         (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
935         (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
936         (*context)->pdb_settrustpwent = context_settrustpwent;
937         (*context)->pdb_gettrustpwent = context_gettrustpwent;
938         (*context)->pdb_gettrustpwnam = context_gettrustpwnam;
939         (*context)->pdb_gettrustpwsid = context_gettrustpwsid;
940         (*context)->pdb_add_trust_passwd = context_add_trust_passwd;
941         (*context)->pdb_update_trust_passwd = context_update_trust_passwd;
942         (*context)->pdb_delete_trust_passwd = context_delete_trust_passwd;
943         (*context)->pdb_add_sid_to_privilege = context_add_sid_to_privilege;
944         (*context)->pdb_remove_sid_from_privilege = context_remove_sid_from_privilege;
945         (*context)->pdb_get_privilege_set = context_get_privilege_set;
946         (*context)->pdb_get_privilege_entry = context_get_privilege_entry;
947
948         (*context)->free_fn = free_pdb_context;
949
950         return NT_STATUS_OK;
951 }
952
953
954 /******************************************************************
955   Make a pdb_context, given an array of strings
956  *******************************************************************/
957
958 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
959 {
960         int i = 0;
961         struct pdb_methods *curmethods, *tmpmethods;
962         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
963         BOOL have_guest = False;
964
965         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
966                 return nt_status;
967         }
968
969         if (!selected) {
970                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
971                 return nt_status;
972         }
973
974         while (selected[i]){
975                 if (strcmp(selected[i], "guest") == 0) {
976                         have_guest = True;
977                 }
978                 /* Try to initialise pdb */
979                 DEBUG(5,("Trying to load: %s\n", selected[i]));
980                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
981                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
982                         free_pdb_context(context);
983                         return nt_status;
984                 }
985                 curmethods->parent = *context;
986                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
987                 i++;
988         }
989
990         if (have_guest)
991                 return NT_STATUS_OK;
992
993         if ( (lp_guestaccount() == NULL) ||
994              (*lp_guestaccount() == '\0') ) {
995                 /* We explicitly don't want guest access. No idea what
996                    else that breaks, but be it that way. */
997                 return NT_STATUS_OK;
998         }
999
1000         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
1001                                                                *context,
1002                                                                "guest"))) {
1003                 DEBUG(1, ("Loading guest module failed!\n"));
1004                 free_pdb_context(context);
1005                 return nt_status;
1006         }
1007
1008         curmethods->parent = *context;
1009         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
1010         
1011         return NT_STATUS_OK;
1012 }
1013
1014 /******************************************************************
1015   Make a pdb_context, given a text string.
1016  *******************************************************************/
1017
1018 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
1019 {
1020         NTSTATUS ret;
1021         char **newsel = str_list_make(selected, NULL);
1022         ret = make_pdb_context_list(context, (const char **)newsel);
1023         str_list_free(&newsel);
1024         return ret;
1025 }
1026
1027 /******************************************************************
1028  Return an already initialised pdb_context, to facilitate backward 
1029  compatibility (see functions below).
1030 *******************************************************************/
1031
1032 static struct pdb_context *pdb_get_static_context(BOOL reload) 
1033 {
1034         static struct pdb_context *pdb_context = NULL;
1035
1036         if ((pdb_context) && (reload)) {
1037                 pdb_context->free_fn(&pdb_context);
1038                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1039                         return NULL;
1040                 }
1041         }
1042
1043         if (!pdb_context) {
1044                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1045                         return NULL;
1046                 }
1047         }
1048
1049         return pdb_context;
1050 }
1051
1052 /******************************************************************
1053  Backward compatibility functions for the original passdb interface
1054 *******************************************************************/
1055
1056 BOOL pdb_setsampwent(BOOL update) 
1057 {
1058         struct pdb_context *pdb_context = pdb_get_static_context(False);
1059
1060         if (!pdb_context) {
1061                 return False;
1062         }
1063
1064         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update));
1065 }
1066
1067 void pdb_endsampwent(void) 
1068 {
1069         struct pdb_context *pdb_context = pdb_get_static_context(False);
1070
1071         if (!pdb_context) {
1072                 return;
1073         }
1074
1075         pdb_context->pdb_endsampwent(pdb_context);
1076 }
1077
1078 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
1079 {
1080         struct pdb_context *pdb_context = pdb_get_static_context(False);
1081
1082         if (!pdb_context) {
1083                 return False;
1084         }
1085
1086         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
1087 }
1088
1089 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
1090 {
1091         struct pdb_context *pdb_context = pdb_get_static_context(False);
1092
1093         if (!pdb_context) {
1094                 return False;
1095         }
1096
1097         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username));
1098 }
1099
1100 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
1101 {
1102         struct pdb_context *pdb_context = pdb_get_static_context(False);
1103
1104         if (!pdb_context) {
1105                 return False;
1106         }
1107
1108         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1109 }
1110
1111 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
1112 {
1113         struct pdb_context *pdb_context = pdb_get_static_context(False);
1114         const char *lm_pw, *nt_pw;
1115         uint16 acb_flags;
1116
1117         if (!pdb_context) {
1118                 return False;
1119         }
1120         
1121         /* disable acccounts with no passwords (that has not 
1122            been allowed by the  ACB_PWNOTREQ bit */
1123
1124         lm_pw = pdb_get_lanman_passwd( sam_acct );
1125         nt_pw = pdb_get_nt_passwd( sam_acct );
1126         acb_flags = pdb_get_acct_ctrl( sam_acct );
1127         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
1128                 acb_flags |= ACB_DISABLED;
1129                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
1130         }
1131
1132         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1133 }
1134
1135 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
1136 {
1137         struct pdb_context *pdb_context = pdb_get_static_context(False);
1138         const char *lm_pw, *nt_pw;
1139         uint16 acb_flags;
1140
1141         if (!pdb_context) {
1142                 return False;
1143         }
1144
1145         /* disable acccounts with no passwords (that has not 
1146            been allowed by the  ACB_PWNOTREQ bit */
1147         
1148         lm_pw = pdb_get_lanman_passwd( sam_acct );
1149         nt_pw = pdb_get_nt_passwd( sam_acct );
1150         acb_flags = pdb_get_acct_ctrl( sam_acct );
1151         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
1152                 acb_flags |= ACB_DISABLED;
1153                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
1154         }
1155
1156         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1157 }
1158
1159 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
1160 {
1161         struct pdb_context *pdb_context = pdb_get_static_context(False);
1162
1163         if (!pdb_context) {
1164                 return False;
1165         }
1166
1167         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1168 }
1169
1170 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1171 {
1172         struct pdb_context *pdb_context = pdb_get_static_context(False);
1173
1174         if (!pdb_context) {
1175                 return False;
1176         }
1177
1178         return NT_STATUS_IS_OK(pdb_context->
1179                                pdb_getgrsid(pdb_context, map, sid));
1180 }
1181
1182 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1183 {
1184         struct pdb_context *pdb_context = pdb_get_static_context(False);
1185
1186         if (!pdb_context) {
1187                 return False;
1188         }
1189
1190         return NT_STATUS_IS_OK(pdb_context->
1191                                pdb_getgrgid(pdb_context, map, gid));
1192 }
1193
1194 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1195 {
1196         struct pdb_context *pdb_context = pdb_get_static_context(False);
1197
1198         if (!pdb_context) {
1199                 return False;
1200         }
1201
1202         return NT_STATUS_IS_OK(pdb_context->
1203                                pdb_getgrnam(pdb_context, map, name));
1204 }
1205
1206 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1207 {
1208         struct pdb_context *pdb_context = pdb_get_static_context(False);
1209
1210         if (!pdb_context) {
1211                 return False;
1212         }
1213
1214         return NT_STATUS_IS_OK(pdb_context->
1215                                pdb_add_group_mapping_entry(pdb_context, map));
1216 }
1217
1218 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1219 {
1220         struct pdb_context *pdb_context = pdb_get_static_context(False);
1221
1222         if (!pdb_context) {
1223                 return False;
1224         }
1225
1226         return NT_STATUS_IS_OK(pdb_context->
1227                                pdb_update_group_mapping_entry(pdb_context, map));
1228 }
1229
1230 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1231 {
1232         struct pdb_context *pdb_context = pdb_get_static_context(False);
1233
1234         if (!pdb_context) {
1235                 return False;
1236         }
1237
1238         return NT_STATUS_IS_OK(pdb_context->
1239                                pdb_delete_group_mapping_entry(pdb_context, sid));
1240 }
1241
1242 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
1243                             int *num_entries, BOOL unix_only)
1244 {
1245         struct pdb_context *pdb_context = pdb_get_static_context(False);
1246
1247         if (!pdb_context) {
1248                 return False;
1249         }
1250
1251         return NT_STATUS_IS_OK(pdb_context->
1252                                pdb_enum_group_mapping(pdb_context, sid_name_use,
1253                                                       rmap, num_entries, unix_only));
1254 }
1255
1256 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1257 {
1258         struct pdb_context *pdb_context = pdb_get_static_context(False);
1259
1260         if (!pdb_context) {
1261                 return False;
1262         }
1263
1264         return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1265                                                              name, sid));
1266 }
1267
1268 BOOL pdb_create_alias(const char *name, uint32 *rid)
1269 {
1270         struct pdb_context *pdb_context = pdb_get_static_context(False);
1271
1272         if (!pdb_context) {
1273                 return False;
1274         }
1275
1276         return NT_STATUS_IS_OK(pdb_context->pdb_create_alias(pdb_context,
1277                                                              name, rid));
1278 }
1279
1280 BOOL pdb_delete_alias(const DOM_SID *sid)
1281 {
1282         struct pdb_context *pdb_context = pdb_get_static_context(False);
1283
1284         if (!pdb_context) {
1285                 return False;
1286         }
1287
1288         return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1289                                                              sid));
1290                                                             
1291 }
1292
1293 BOOL pdb_enum_aliases(const DOM_SID *sid, uint32 start_idx, uint32 max_entries,
1294                       uint32 *num_aliases, struct acct_info **info)
1295 {
1296         struct pdb_context *pdb_context = pdb_get_static_context(False);
1297
1298         if (!pdb_context) {
1299                 return False;
1300         }
1301
1302         return NT_STATUS_IS_OK(pdb_context->pdb_enum_aliases(pdb_context, sid,
1303                                                              start_idx,
1304                                                              max_entries,
1305                                                              num_aliases,
1306                                                              info));
1307 }
1308
1309 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1310 {
1311         struct pdb_context *pdb_context = pdb_get_static_context(False);
1312
1313         if (!pdb_context) {
1314                 return False;
1315         }
1316
1317         return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1318                                                               info));
1319 }
1320
1321 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1322 {
1323         struct pdb_context *pdb_context = pdb_get_static_context(False);
1324
1325         if (!pdb_context) {
1326                 return False;
1327         }
1328
1329         return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1330                                                               info));
1331 }
1332
1333 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1334 {
1335         struct pdb_context *pdb_context = pdb_get_static_context(False);
1336
1337         if (!pdb_context) {
1338                 return False;
1339         }
1340
1341         return NT_STATUS_IS_OK(pdb_context->
1342                                pdb_add_aliasmem(pdb_context, alias, member));
1343 }
1344
1345 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1346 {
1347         struct pdb_context *pdb_context = pdb_get_static_context(False);
1348
1349         if (!pdb_context) {
1350                 return False;
1351         }
1352
1353         return NT_STATUS_IS_OK(pdb_context->
1354                                pdb_del_aliasmem(pdb_context, alias, member));
1355 }
1356
1357 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1358                        DOM_SID **members, int *num_members)
1359 {
1360         struct pdb_context *pdb_context = pdb_get_static_context(False);
1361
1362         if (!pdb_context) {
1363                 return False;
1364         }
1365
1366         return NT_STATUS_IS_OK(pdb_context->
1367                                pdb_enum_aliasmem(pdb_context, alias,
1368                                                  members, num_members));
1369 }
1370
1371 BOOL pdb_enum_alias_memberships(const DOM_SID *sid,
1372                                 DOM_SID **aliases, int *num)
1373 {
1374         struct pdb_context *pdb_context = pdb_get_static_context(False);
1375
1376         if (!pdb_context) {
1377                 return False;
1378         }
1379
1380         return NT_STATUS_IS_OK(pdb_context->
1381                                pdb_enum_alias_memberships(pdb_context, sid,
1382                                                           aliases, num));
1383 }
1384
1385 BOOL pdb_add_sid_to_privilege(char *priv_name, DOM_SID *sid)
1386 {
1387         struct pdb_context *pdb_context = pdb_get_static_context(False);
1388
1389         if (!pdb_context) {
1390                 return False;
1391         }
1392
1393         return NT_STATUS_IS_OK(pdb_context->
1394                                pdb_add_sid_to_privilege(pdb_context, priv_name, sid));
1395 }
1396
1397 BOOL pdb_remove_sid_from_privilege(char *priv_name, DOM_SID *sid)
1398 {
1399         struct pdb_context *pdb_context = pdb_get_static_context(False);
1400
1401         if (!pdb_context) {
1402                 return False;
1403         }
1404
1405         return NT_STATUS_IS_OK(pdb_context->
1406                                pdb_remove_sid_from_privilege(pdb_context, priv_name, sid));
1407 }
1408
1409 BOOL pdb_get_privilege_set(DOM_SID *sid_list, int num_sids, PRIVILEGE_SET *privset)
1410 {
1411         struct pdb_context *pdb_context = pdb_get_static_context(False);
1412
1413         if (!pdb_context) {
1414                 return False;
1415         }
1416
1417         return NT_STATUS_IS_OK(pdb_context->
1418                                pdb_get_privilege_set(pdb_context, sid_list, num_sids, privset));
1419 }
1420
1421 BOOL pdb_get_privilege_entry(const char *privname, char **sid_list)
1422 {
1423         struct pdb_context *pdb_context = pdb_get_static_context(False);
1424
1425         if (!pdb_context) {
1426                 return False;
1427         }
1428
1429         return NT_STATUS_IS_OK(pdb_context->
1430                                pdb_get_privilege_entry(pdb_context, privname, sid_list));
1431 }
1432
1433 /***************************************************************
1434   Initialize the static context (at smbd startup etc). 
1435
1436   If uninitialised, context will auto-init on first use.
1437  ***************************************************************/
1438
1439 BOOL initialize_password_db(BOOL reload)
1440 {       
1441         return (pdb_get_static_context(reload) != NULL);
1442 }
1443
1444
1445 /***************************************************************************
1446   Default implementations of some functions.
1447  ****************************************************************************/
1448
1449 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1450 {
1451         return NT_STATUS_NO_SUCH_USER;
1452 }
1453
1454 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1455 {
1456         return NT_STATUS_NO_SUCH_USER;
1457 }
1458
1459 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1460 {
1461         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1462         return NT_STATUS_NOT_IMPLEMENTED;
1463 }
1464
1465 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1466 {
1467         return NT_STATUS_NOT_IMPLEMENTED;
1468 }
1469
1470 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1471 {
1472         return NT_STATUS_NOT_IMPLEMENTED;
1473 }
1474
1475 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update)
1476 {
1477         return NT_STATUS_NOT_IMPLEMENTED;
1478 }
1479
1480 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1481 {
1482         return NT_STATUS_NOT_IMPLEMENTED;
1483 }
1484
1485 static void pdb_default_endsampwent(struct pdb_methods *methods)
1486 {
1487         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1488 }
1489
1490 static NTSTATUS pdb_default_settrustpwent(struct pdb_methods *methods)
1491 {
1492         return NT_STATUS_NOT_IMPLEMENTED;
1493 }
1494
1495 static NTSTATUS pdb_default_gettrustpwent(struct pdb_methods *methods, SAM_TRUST_PASSWD* trust)
1496 {
1497         return NT_STATUS_NOT_IMPLEMENTED;
1498 }
1499
1500 static NTSTATUS pdb_default_gettrustpwnam(struct pdb_methods *methods, SAM_TRUST_PASSWD* trust,
1501                                           const char* name)
1502 {
1503         return NT_STATUS_NOT_IMPLEMENTED;
1504 }
1505
1506 static NTSTATUS pdb_default_gettrustpwsid(struct pdb_methods *methods, SAM_TRUST_PASSWD* trust,
1507                                           const DOM_SID* sid)
1508 {
1509         return NT_STATUS_NOT_IMPLEMENTED;
1510 }
1511
1512 static NTSTATUS pdb_default_add_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1513 {
1514         return NT_STATUS_NOT_IMPLEMENTED;
1515 }
1516
1517 static NTSTATUS pdb_default_update_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1518 {
1519         return NT_STATUS_NOT_IMPLEMENTED;
1520 }
1521
1522 static NTSTATUS pdb_default_delete_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1523 {
1524         return NT_STATUS_NOT_IMPLEMENTED;
1525 }
1526
1527 static NTSTATUS pdb_default_add_sid_to_privilege(struct pdb_methods *methods, const char *priv_name, const DOM_SID *sid)
1528 {
1529         return NT_STATUS_NOT_IMPLEMENTED;
1530 }
1531
1532 static NTSTATUS pdb_default_remove_sid_from_privilege(struct pdb_methods *methods, const char *priv_name, const DOM_SID *sid)
1533 {
1534         return NT_STATUS_NOT_IMPLEMENTED;
1535 }
1536
1537 static NTSTATUS pdb_default_get_privilege_set(struct pdb_methods *methods, DOM_SID *sid_list, int num_sids, PRIVILEGE_SET *privset)
1538 {
1539         /* by default return the empty privilege set as otherwise login will
1540          * be denied if a backend does not support privilege sets */
1541         return NT_STATUS_OK;
1542 }
1543
1544 static NTSTATUS pdb_default_get_privilege_entry(struct pdb_methods *methods, const char *privname, char **sid_list)
1545 {
1546         return NT_STATUS_NOT_IMPLEMENTED;
1547 }
1548
1549
1550 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
1551 {
1552         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
1553
1554         if (!*methods) {
1555                 return NT_STATUS_NO_MEMORY;
1556         }
1557
1558         ZERO_STRUCTP(*methods);
1559
1560         (*methods)->setsampwent = pdb_default_setsampwent;
1561         (*methods)->endsampwent = pdb_default_endsampwent;
1562         (*methods)->getsampwent = pdb_default_getsampwent;
1563         (*methods)->getsampwnam = pdb_default_getsampwnam;
1564         (*methods)->getsampwsid = pdb_default_getsampwsid;
1565         (*methods)->add_sam_account = pdb_default_add_sam_account;
1566         (*methods)->update_sam_account = pdb_default_update_sam_account;
1567         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1568
1569         (*methods)->getgrsid = pdb_default_getgrsid;
1570         (*methods)->getgrgid = pdb_default_getgrgid;
1571         (*methods)->getgrnam = pdb_default_getgrnam;
1572         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1573         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1574         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1575         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1576         (*methods)->find_alias = pdb_default_find_alias;
1577         (*methods)->create_alias = pdb_default_create_alias;
1578         (*methods)->delete_alias = pdb_default_delete_alias;
1579         (*methods)->enum_aliases = pdb_default_enum_aliases;
1580         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
1581         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
1582         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
1583         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
1584         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
1585         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
1586         
1587         (*methods)->settrustpwent = pdb_default_settrustpwent;
1588         (*methods)->gettrustpwent = pdb_default_gettrustpwent;
1589         (*methods)->gettrustpwnam = pdb_default_gettrustpwnam;
1590         (*methods)->gettrustpwsid = pdb_default_gettrustpwsid;
1591         (*methods)->add_trust_passwd = pdb_default_add_trust_passwd;
1592         (*methods)->update_trust_passwd = pdb_default_update_trust_passwd;
1593         (*methods)->delete_trust_passwd = pdb_default_delete_trust_passwd;
1594
1595         (*methods)->add_sid_to_privilege = pdb_default_add_sid_to_privilege;
1596         (*methods)->remove_sid_from_privilege = pdb_default_remove_sid_from_privilege;
1597         (*methods)->get_privilege_set = pdb_default_get_privilege_set;
1598         (*methods)->get_privilege_entry = pdb_default_get_privilege_entry;
1599
1600         return NT_STATUS_OK;
1601 }