57f3ce7e9814ddde2b4f2d3546681fcea03ee42f
[kai/samba.git] / source3 / passdb / pdb_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Jelmer Vernooij                        2002
6
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_SET);
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_SET);
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         const char *lm_pw, *nt_pw;
236         uint16 acb_flags;
237
238         if ((!context) || (!context->pdb_methods)) {
239                 DEBUG(0, ("invalid pdb_context specified!\n"));
240                 return ret;
241         }
242
243         /* disable acccounts with no passwords (that has not 
244            been allowed by the  ACB_PWNOTREQ bit */
245         
246         lm_pw = pdb_get_lanman_passwd( sam_acct );
247         nt_pw = pdb_get_nt_passwd( sam_acct );
248         acb_flags = pdb_get_acct_ctrl( sam_acct );
249         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
250                 acb_flags |= ACB_DISABLED;
251                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_SET );
252                 pdb_set_init_flags(sam_acct, PDB_ACCTCTRL, PDB_SET);
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 char *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_SET );
287                 pdb_set_init_flags(sam_acct, PDB_ACCTCTRL, PDB_SET);
288         }
289         
290         /** @todo  This is where a 're-read on update' should be done */
291
292         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
293 }
294
295 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
296 {
297         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
298
299         struct pdb_methods *pdb_selected;
300         if (!context) {
301                 DEBUG(0, ("invalid pdb_context specified!\n"));
302                 return ret;
303         }
304
305         if (!sam_acct->methods){
306                 pdb_selected = context->pdb_methods;
307                 /* There's no passdb backend specified for this account.
308                  * Try to delete it in every passdb available 
309                  * Needed to delete accounts in smbpasswd that are not
310                  * in /etc/passwd.
311                  */
312                 while (pdb_selected){
313                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
314                                 return ret;
315                         }
316                         pdb_selected = pdb_selected->next;
317                 }
318                 return ret;
319         }
320
321         if (!sam_acct->methods->delete_sam_account){
322                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
323                 return ret;
324         }
325         
326         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
327 }
328
329 static NTSTATUS context_getgrsid(struct pdb_context *context,
330                                  GROUP_MAP *map, DOM_SID sid)
331 {
332         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
333
334         struct pdb_methods *curmethods;
335         if ((!context)) {
336                 DEBUG(0, ("invalid pdb_context specified!\n"));
337                 return ret;
338         }
339         curmethods = context->pdb_methods;
340         while (curmethods){
341                 ret = curmethods->getgrsid(curmethods, map, sid);
342                 if (NT_STATUS_IS_OK(ret)) {
343                         map->methods = curmethods;
344                         return ret;
345                 }
346                 curmethods = curmethods->next;
347         }
348
349         return ret;
350 }
351
352 static NTSTATUS context_getgrgid(struct pdb_context *context,
353                                  GROUP_MAP *map, gid_t gid)
354 {
355         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
356
357         struct pdb_methods *curmethods;
358         if ((!context)) {
359                 DEBUG(0, ("invalid pdb_context specified!\n"));
360                 return ret;
361         }
362         curmethods = context->pdb_methods;
363         while (curmethods){
364                 ret = curmethods->getgrgid(curmethods, map, gid);
365                 if (NT_STATUS_IS_OK(ret)) {
366                         map->methods = curmethods;
367                         return ret;
368                 }
369                 curmethods = curmethods->next;
370         }
371
372         return ret;
373 }
374
375 static NTSTATUS context_getgrnam(struct pdb_context *context,
376                                  GROUP_MAP *map, const char *name)
377 {
378         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
379
380         struct pdb_methods *curmethods;
381         if ((!context)) {
382                 DEBUG(0, ("invalid pdb_context specified!\n"));
383                 return ret;
384         }
385         curmethods = context->pdb_methods;
386         while (curmethods){
387                 ret = curmethods->getgrnam(curmethods, map, name);
388                 if (NT_STATUS_IS_OK(ret)) {
389                         map->methods = curmethods;
390                         return ret;
391                 }
392                 curmethods = curmethods->next;
393         }
394
395         return ret;
396 }
397
398 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
399                                                 GROUP_MAP *map)
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->pdb_methods->add_group_mapping_entry(context->pdb_methods,
409                                                              map);
410 }
411
412 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
413                                                    GROUP_MAP *map)
414 {
415         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
416
417         if ((!context) || (!context->pdb_methods)) {
418                 DEBUG(0, ("invalid pdb_context specified!\n"));
419                 return ret;
420         }
421
422         return context->
423                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
424 }
425
426 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
427                                                    DOM_SID sid)
428 {
429         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
430
431         if ((!context) || (!context->pdb_methods)) {
432                 DEBUG(0, ("invalid pdb_context specified!\n"));
433                 return ret;
434         }
435
436         return context->
437                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
438 }
439
440 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
441                                            enum SID_NAME_USE sid_name_use,
442                                            GROUP_MAP **rmap, int *num_entries,
443                                            BOOL unix_only)
444 {
445         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
446
447         if ((!context) || (!context->pdb_methods)) {
448                 DEBUG(0, ("invalid pdb_context specified!\n"));
449                 return ret;
450         }
451
452         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
453                                                         sid_name_use, rmap,
454                                                         num_entries, unix_only);
455 }
456
457 /******************************************************************
458   Free and cleanup a pdb context, any associated data and anything
459   that the attached modules might have associated.
460  *******************************************************************/
461
462 static void free_pdb_context(struct pdb_context **context)
463 {
464         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
465
466         while (pdb_selected){
467                 if(pdb_selected->free_private_data)
468                         pdb_selected->free_private_data(&(pdb_selected->private_data));
469                 pdb_selected = pdb_selected->next;
470         }
471
472         talloc_destroy((*context)->mem_ctx);
473         *context = NULL;
474 }
475
476 /******************************************************************
477   Make a pdb_methods from scratch
478  *******************************************************************/
479
480 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
481 {
482         char *module_name = smb_xstrdup(selected);
483         char *module_location = NULL, *p;
484         struct pdb_init_function_entry *entry;
485         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
486
487         lazy_initialize_passdb();
488
489         p = strchr(module_name, ':');
490
491         if (p) {
492                 *p = 0;
493                 module_location = p+1;
494                 trim_char(module_location, ' ', ' ');
495         }
496
497         trim_char(module_name, ' ', ' ');
498
499
500         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
501
502         entry = pdb_find_backend_entry(module_name);
503         
504         /* Try to find a module that contains this module */
505         if (!entry) { 
506                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
507                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
508                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
509                         SAFE_FREE(module_name);
510                         return NT_STATUS_UNSUCCESSFUL;
511                 }
512         }
513         
514         /* No such backend found */
515         if(!entry) { 
516                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
517                 SAFE_FREE(module_name);
518                 return NT_STATUS_INVALID_PARAMETER;
519         }
520
521         DEBUG(5,("Found pdb backend %s\n", module_name));
522         nt_status = entry->init(context, methods, module_location);
523         if (NT_STATUS_IS_OK(nt_status)) {
524                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
525         } else {
526                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
527         }
528         SAFE_FREE(module_name);
529         return nt_status;
530 }
531
532 /******************************************************************
533   Make a pdb_context from scratch.
534  *******************************************************************/
535
536 static NTSTATUS make_pdb_context(struct pdb_context **context) 
537 {
538         TALLOC_CTX *mem_ctx;
539
540         mem_ctx = talloc_init("pdb_context internal allocation context");
541
542         if (!mem_ctx) {
543                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
544                 return NT_STATUS_NO_MEMORY;
545         }               
546
547         *context = talloc(mem_ctx, sizeof(**context));
548         if (!*context) {
549                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
550                 return NT_STATUS_NO_MEMORY;
551         }
552
553         ZERO_STRUCTP(*context);
554
555         (*context)->mem_ctx = mem_ctx;
556
557         (*context)->pdb_setsampwent = context_setsampwent;
558         (*context)->pdb_endsampwent = context_endsampwent;
559         (*context)->pdb_getsampwent = context_getsampwent;
560         (*context)->pdb_getsampwnam = context_getsampwnam;
561         (*context)->pdb_getsampwsid = context_getsampwsid;
562         (*context)->pdb_add_sam_account = context_add_sam_account;
563         (*context)->pdb_update_sam_account = context_update_sam_account;
564         (*context)->pdb_delete_sam_account = context_delete_sam_account;
565         (*context)->pdb_getgrsid = context_getgrsid;
566         (*context)->pdb_getgrgid = context_getgrgid;
567         (*context)->pdb_getgrnam = context_getgrnam;
568         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
569         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
570         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
571         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
572
573         (*context)->free_fn = free_pdb_context;
574
575         return NT_STATUS_OK;
576 }
577
578
579 /******************************************************************
580   Make a pdb_context, given an array of strings
581  *******************************************************************/
582
583 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
584 {
585         int i = 0;
586         struct pdb_methods *curmethods, *tmpmethods;
587         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
588         BOOL have_guest = False;
589
590         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
591                 return nt_status;
592         }
593
594         if (!selected) {
595                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
596                 return nt_status;
597         }
598
599         while (selected[i]){
600                 if (strcmp(selected[i], "guest") == 0) {
601                         have_guest = True;
602                 }
603                 /* Try to initialise pdb */
604                 DEBUG(5,("Trying to load: %s\n", selected[i]));
605                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
606                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
607                         free_pdb_context(context);
608                         return nt_status;
609                 }
610                 curmethods->parent = *context;
611                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
612                 i++;
613         }
614
615         if (have_guest)
616                 return NT_STATUS_OK;
617
618         if ( (lp_guestaccount() == NULL) ||
619              (*lp_guestaccount() == '\0') ) {
620                 /* We explicitly don't want guest access. No idea what
621                    else that breaks, but be it that way. */
622                 return NT_STATUS_OK;
623         }
624
625         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
626                                                                *context,
627                                                                "guest"))) {
628                 DEBUG(1, ("Loading guest module failed!\n"));
629                 free_pdb_context(context);
630                 return nt_status;
631         }
632
633         curmethods->parent = *context;
634         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
635         
636         return NT_STATUS_OK;
637 }
638
639 /******************************************************************
640   Make a pdb_context, given a text string.
641  *******************************************************************/
642
643 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
644 {
645         NTSTATUS ret;
646         char **newsel = str_list_make(selected, NULL);
647         ret = make_pdb_context_list(context, (const char **)newsel);
648         str_list_free(&newsel);
649         return ret;
650 }
651
652 /******************************************************************
653  Return an already initialised pdb_context, to facilitate backward 
654  compatibility (see functions below).
655 *******************************************************************/
656
657 static struct pdb_context *pdb_get_static_context(BOOL reload) 
658 {
659         static struct pdb_context *pdb_context = NULL;
660
661         if ((pdb_context) && (reload)) {
662                 pdb_context->free_fn(&pdb_context);
663                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
664                         return NULL;
665                 }
666         }
667
668         if (!pdb_context) {
669                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
670                         return NULL;
671                 }
672         }
673
674         return pdb_context;
675 }
676
677 /******************************************************************
678  Backward compatibility functions for the original passdb interface
679 *******************************************************************/
680
681 BOOL pdb_setsampwent(BOOL update) 
682 {
683         struct pdb_context *pdb_context = pdb_get_static_context(False);
684
685         if (!pdb_context) {
686                 return False;
687         }
688
689         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update));
690 }
691
692 void pdb_endsampwent(void) 
693 {
694         struct pdb_context *pdb_context = pdb_get_static_context(False);
695
696         if (!pdb_context) {
697                 return;
698         }
699
700         pdb_context->pdb_endsampwent(pdb_context);
701 }
702
703 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
704 {
705         struct pdb_context *pdb_context = pdb_get_static_context(False);
706
707         if (!pdb_context) {
708                 return False;
709         }
710
711         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
712 }
713
714 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
715 {
716         struct pdb_context *pdb_context = pdb_get_static_context(False);
717
718         if (!pdb_context) {
719                 return False;
720         }
721
722         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username));
723 }
724
725 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
726 {
727         struct pdb_context *pdb_context = pdb_get_static_context(False);
728
729         if (!pdb_context) {
730                 return False;
731         }
732
733         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
734 }
735
736 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
737 {
738         struct pdb_context *pdb_context = pdb_get_static_context(False);
739
740         if (!pdb_context) {
741                 return False;
742         }
743         
744         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
745 }
746
747 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
748 {
749         struct pdb_context *pdb_context = pdb_get_static_context(False);
750
751         if (!pdb_context) {
752                 return False;
753         }
754
755         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
756 }
757
758 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
759 {
760         struct pdb_context *pdb_context = pdb_get_static_context(False);
761
762         if (!pdb_context) {
763                 return False;
764         }
765
766         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
767 }
768
769 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
770 {
771         struct pdb_context *pdb_context = pdb_get_static_context(False);
772
773         if (!pdb_context) {
774                 return False;
775         }
776
777         return NT_STATUS_IS_OK(pdb_context->
778                                pdb_getgrsid(pdb_context, map, sid));
779 }
780
781 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
782 {
783         struct pdb_context *pdb_context = pdb_get_static_context(False);
784
785         if (!pdb_context) {
786                 return False;
787         }
788
789         return NT_STATUS_IS_OK(pdb_context->
790                                pdb_getgrgid(pdb_context, map, gid));
791 }
792
793 BOOL pdb_getgrnam(GROUP_MAP *map, char *name)
794 {
795         struct pdb_context *pdb_context = pdb_get_static_context(False);
796
797         if (!pdb_context) {
798                 return False;
799         }
800
801         return NT_STATUS_IS_OK(pdb_context->
802                                pdb_getgrnam(pdb_context, map, name));
803 }
804
805 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
806 {
807         struct pdb_context *pdb_context = pdb_get_static_context(False);
808
809         if (!pdb_context) {
810                 return False;
811         }
812
813         return NT_STATUS_IS_OK(pdb_context->
814                                pdb_add_group_mapping_entry(pdb_context, map));
815 }
816
817 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
818 {
819         struct pdb_context *pdb_context = pdb_get_static_context(False);
820
821         if (!pdb_context) {
822                 return False;
823         }
824
825         return NT_STATUS_IS_OK(pdb_context->
826                                pdb_update_group_mapping_entry(pdb_context, map));
827 }
828
829 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
830 {
831         struct pdb_context *pdb_context = pdb_get_static_context(False);
832
833         if (!pdb_context) {
834                 return False;
835         }
836
837         return NT_STATUS_IS_OK(pdb_context->
838                                pdb_delete_group_mapping_entry(pdb_context, sid));
839 }
840
841 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
842                             int *num_entries, BOOL unix_only)
843 {
844         struct pdb_context *pdb_context = pdb_get_static_context(False);
845
846         if (!pdb_context) {
847                 return False;
848         }
849
850         return NT_STATUS_IS_OK(pdb_context->
851                                pdb_enum_group_mapping(pdb_context, sid_name_use,
852                                                       rmap, num_entries, unix_only));
853 }
854
855 /***************************************************************
856   Initialize the static context (at smbd startup etc). 
857
858   If uninitialised, context will auto-init on first use.
859  ***************************************************************/
860
861 BOOL initialize_password_db(BOOL reload)
862 {       
863         return (pdb_get_static_context(reload) != NULL);
864 }
865
866
867 /***************************************************************************
868   Default implementations of some functions.
869  ****************************************************************************/
870
871 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
872 {
873         return NT_STATUS_NO_SUCH_USER;
874 }
875
876 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
877 {
878         return NT_STATUS_NO_SUCH_USER;
879 }
880
881 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
882 {
883         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
884         return NT_STATUS_NOT_IMPLEMENTED;
885 }
886
887 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
888 {
889         return NT_STATUS_NOT_IMPLEMENTED;
890 }
891
892 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
893 {
894         return NT_STATUS_NOT_IMPLEMENTED;
895 }
896
897 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update)
898 {
899         return NT_STATUS_NOT_IMPLEMENTED;
900 }
901
902 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
903 {
904         return NT_STATUS_NOT_IMPLEMENTED;
905 }
906
907 static void pdb_default_endsampwent(struct pdb_methods *methods)
908 {
909         return; /* NT_STATUS_NOT_IMPLEMENTED; */
910 }
911
912 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
913 {
914         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
915
916         if (!*methods) {
917                 return NT_STATUS_NO_MEMORY;
918         }
919
920         ZERO_STRUCTP(*methods);
921
922         (*methods)->setsampwent = pdb_default_setsampwent;
923         (*methods)->endsampwent = pdb_default_endsampwent;
924         (*methods)->getsampwent = pdb_default_getsampwent;
925         (*methods)->getsampwnam = pdb_default_getsampwnam;
926         (*methods)->getsampwsid = pdb_default_getsampwsid;
927         (*methods)->add_sam_account = pdb_default_add_sam_account;
928         (*methods)->update_sam_account = pdb_default_update_sam_account;
929         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
930
931         (*methods)->getgrsid = pdb_default_getgrsid;
932         (*methods)->getgrgid = pdb_default_getgrgid;
933         (*methods)->getgrnam = pdb_default_getgrnam;
934         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
935         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
936         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
937         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
938
939         return NT_STATUS_OK;
940 }