This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.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 /** List of various built-in passdb modules */
28
29 const struct pdb_init_function_entry builtin_pdb_init_functions[] = {
30         { "smbpasswd", pdb_init_smbpasswd },
31         { "smbpasswd_nua", pdb_init_smbpasswd_nua },
32         { "tdbsam", pdb_init_tdbsam },
33         { "tdbsam_nua", pdb_init_tdbsam_nua },
34         { "ldapsam", pdb_init_ldapsam },
35         { "ldapsam_nua", pdb_init_ldapsam_nua },
36         { "unixsam", pdb_init_unixsam },
37         { "guest", pdb_init_guestsam },
38         { "nisplussam", pdb_init_nisplussam },
39         { "plugin", pdb_init_plugin },
40         { NULL, NULL}
41 };
42
43 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update)
44 {
45         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
46
47         if (!context) {
48                 DEBUG(0, ("invalid pdb_context specified!\n"));
49                 return ret;
50         }
51
52         context->pwent_methods = context->pdb_methods;
53
54         if (!context->pwent_methods) {
55                 /* No passdbs at all */
56                 return ret;
57         }
58
59         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->setsampwent(context->pwent_methods, update))) {
60                 context->pwent_methods = context->pwent_methods->next;
61                 if (context->pwent_methods == NULL) 
62                         return NT_STATUS_UNSUCCESSFUL;
63         }
64         return ret;
65 }
66
67 static void context_endsampwent(struct pdb_context *context)
68 {
69         if ((!context)){
70                 DEBUG(0, ("invalid pdb_context specified!\n"));
71                 return;
72         }
73
74         if (context->pwent_methods && context->pwent_methods->endsampwent)
75                 context->pwent_methods->endsampwent(context->pwent_methods);
76
77         /* So we won't get strange data when calling getsampwent now */
78         context->pwent_methods = NULL;
79 }
80
81 static NTSTATUS context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
82 {
83         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
84
85         if ((!context) || (!context->pwent_methods)) {
86                 DEBUG(0, ("invalid pdb_context specified!\n"));
87                 return ret;
88         }
89         /* Loop until we find something useful */
90         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->getsampwent(context->pwent_methods, user))) {
91
92                 context->pwent_methods->endsampwent(context->pwent_methods);
93
94                 context->pwent_methods = context->pwent_methods->next;
95
96                 /* All methods are checked now. There are no more entries */
97                 if (context->pwent_methods == NULL)
98                         return ret;
99         
100                 context->pwent_methods->setsampwent(context->pwent_methods, False);
101         }
102         user->methods = context->pwent_methods;
103         return ret;
104 }
105
106 static NTSTATUS context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
107 {
108         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
109
110         struct pdb_methods *curmethods;
111         if ((!context)) {
112                 DEBUG(0, ("invalid pdb_context specified!\n"));
113                 return ret;
114         }
115         curmethods = context->pdb_methods;
116         while (curmethods){
117                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwnam(curmethods, sam_acct, username))) {
118                         sam_acct->methods = curmethods;
119                         return ret;
120                 }
121                 curmethods = curmethods->next;
122         }
123
124         return ret;
125 }
126
127 static NTSTATUS context_getsampwsid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
128 {
129         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
130
131         struct pdb_methods *curmethods;
132         if ((!context)) {
133                 DEBUG(0, ("invalid pdb_context specified!\n"));
134                 return ret;
135         }
136         
137         curmethods = context->pdb_methods;
138
139         while (curmethods){
140                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwsid(curmethods, sam_acct, sid))) {
141                         sam_acct->methods = curmethods;
142                         return ret;
143                 }
144                 curmethods = curmethods->next;
145         }
146
147         return ret;
148 }
149
150 static NTSTATUS context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
151 {
152         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
153
154         if ((!context) || (!context->pdb_methods)) {
155                 DEBUG(0, ("invalid pdb_context specified!\n"));
156                 return ret;
157         }
158
159         /** @todo  This is where a 're-read on add' should be done */
160         /* We now add a new account to the first database listed. 
161          * Should we? */
162
163         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
164 }
165
166 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
167 {
168         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
169
170         if (!context) {
171                 DEBUG(0, ("invalid pdb_context specified!\n"));
172                 return ret;
173         }
174
175         if (!sam_acct || !sam_acct->methods){
176                 DEBUG(0, ("invalid sam_acct specified\n"));
177                 return ret;
178         }
179
180         /** @todo  This is where a 're-read on update' should be done */
181
182         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
183 }
184
185 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
186 {
187         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
188
189         struct pdb_methods *pdb_selected;
190         if (!context) {
191                 DEBUG(0, ("invalid pdb_context specified!\n"));
192                 return ret;
193         }
194
195         if (!sam_acct->methods){
196                 pdb_selected = context->pdb_methods;
197                 /* There's no passdb backend specified for this account.
198                  * Try to delete it in every passdb available 
199                  * Needed to delete accounts in smbpasswd that are not
200                  * in /etc/passwd.
201                  */
202                 while (pdb_selected){
203                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
204                                 return ret;
205                         }
206                         pdb_selected = pdb_selected->next;
207                 }
208                 return ret;
209         }
210
211         if (!sam_acct->methods->delete_sam_account){
212                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
213                 return ret;
214         }
215         
216         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
217 }
218
219 static NTSTATUS context_getgrsid(struct pdb_context *context,
220                                  GROUP_MAP *map, DOM_SID sid, BOOL with_priv)
221 {
222         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
223
224         struct pdb_methods *curmethods;
225         if ((!context)) {
226                 DEBUG(0, ("invalid pdb_context specified!\n"));
227                 return ret;
228         }
229         curmethods = context->pdb_methods;
230         while (curmethods){
231                 ret = curmethods->getgrsid(curmethods, map, sid, with_priv);
232                 if (NT_STATUS_IS_OK(ret)) {
233                         map->methods = curmethods;
234                         return ret;
235                 }
236                 curmethods = curmethods->next;
237         }
238
239         return ret;
240 }
241
242 static NTSTATUS context_getgrgid(struct pdb_context *context,
243                                  GROUP_MAP *map, gid_t gid, BOOL with_priv)
244 {
245         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
246
247         struct pdb_methods *curmethods;
248         if ((!context)) {
249                 DEBUG(0, ("invalid pdb_context specified!\n"));
250                 return ret;
251         }
252         curmethods = context->pdb_methods;
253         while (curmethods){
254                 ret = curmethods->getgrgid(curmethods, map, gid, with_priv);
255                 if (NT_STATUS_IS_OK(ret)) {
256                         map->methods = curmethods;
257                         return ret;
258                 }
259                 curmethods = curmethods->next;
260         }
261
262         return ret;
263 }
264
265 static NTSTATUS context_getgrnam(struct pdb_context *context,
266                                  GROUP_MAP *map, char *name, BOOL with_priv)
267 {
268         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
269
270         struct pdb_methods *curmethods;
271         if ((!context)) {
272                 DEBUG(0, ("invalid pdb_context specified!\n"));
273                 return ret;
274         }
275         curmethods = context->pdb_methods;
276         while (curmethods){
277                 ret = curmethods->getgrnam(curmethods, map, name, with_priv);
278                 if (NT_STATUS_IS_OK(ret)) {
279                         map->methods = curmethods;
280                         return ret;
281                 }
282                 curmethods = curmethods->next;
283         }
284
285         return ret;
286 }
287
288 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
289                                                 GROUP_MAP *map)
290 {
291         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
292
293         if ((!context) || (!context->pdb_methods)) {
294                 DEBUG(0, ("invalid pdb_context specified!\n"));
295                 return ret;
296         }
297
298         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
299                                                              map);
300 }
301
302 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
303                                                    GROUP_MAP *map)
304 {
305         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
306
307         if ((!context) || (!context->pdb_methods)) {
308                 DEBUG(0, ("invalid pdb_context specified!\n"));
309                 return ret;
310         }
311
312         return context->
313                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
314 }
315
316 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
317                                                    DOM_SID sid)
318 {
319         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
320
321         if ((!context) || (!context->pdb_methods)) {
322                 DEBUG(0, ("invalid pdb_context specified!\n"));
323                 return ret;
324         }
325
326         return context->
327                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
328 }
329
330 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
331                                            enum SID_NAME_USE sid_name_use,
332                                            GROUP_MAP **rmap, int *num_entries,
333                                            BOOL unix_only, BOOL with_priv)
334 {
335         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
336
337         if ((!context) || (!context->pdb_methods)) {
338                 DEBUG(0, ("invalid pdb_context specified!\n"));
339                 return ret;
340         }
341
342         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
343                                                         sid_name_use, rmap,
344                                                         num_entries, unix_only,
345                                                         with_priv);
346 }
347
348 /******************************************************************
349   Free and cleanup a pdb context, any associated data and anything
350   that the attached modules might have associated.
351  *******************************************************************/
352
353 static void free_pdb_context(struct pdb_context **context)
354 {
355         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
356
357         while (pdb_selected){
358                 if(pdb_selected->free_private_data)
359                         pdb_selected->free_private_data(&(pdb_selected->private_data));
360                 pdb_selected = pdb_selected->next;
361         }
362
363         talloc_destroy((*context)->mem_ctx);
364         *context = NULL;
365 }
366
367 /******************************************************************
368   Make a pdb_methods from scratch
369  *******************************************************************/
370
371 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
372 {
373         char *module_name = smb_xstrdup(selected);
374         char *module_location = NULL, *p;
375         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
376         int i;
377
378         p = strchr(module_name, ':');
379
380         if (p) {
381                 *p = 0;
382                 module_location = p+1;
383                 trim_string(module_location, " ", " ");
384         }
385
386         trim_string(module_name, " ", " ");
387
388         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
389         for (i = 0; builtin_pdb_init_functions[i].name; i++)
390         {
391                 if (strequal(builtin_pdb_init_functions[i].name, module_name))
392                 {
393                         DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, i));
394                         nt_status = builtin_pdb_init_functions[i].init(context, methods, module_location);
395                         if (NT_STATUS_IS_OK(nt_status)) {
396                                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
397                         } else {
398                                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
399                         }
400                         SAFE_FREE(module_name);
401                         return nt_status;
402                         break; /* unreached */
403                 }
404         }
405
406         /* No such backend found */
407         SAFE_FREE(module_name);
408         return NT_STATUS_INVALID_PARAMETER;
409 }
410
411 /******************************************************************
412   Make a pdb_context from scratch.
413  *******************************************************************/
414
415 static NTSTATUS make_pdb_context(struct pdb_context **context) 
416 {
417         TALLOC_CTX *mem_ctx;
418
419         mem_ctx = talloc_init("pdb_context internal allocation context");
420
421         if (!mem_ctx) {
422                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
423                 return NT_STATUS_NO_MEMORY;
424         }               
425
426         *context = talloc(mem_ctx, sizeof(**context));
427         if (!*context) {
428                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
429                 return NT_STATUS_NO_MEMORY;
430         }
431
432         ZERO_STRUCTP(*context);
433
434         (*context)->mem_ctx = mem_ctx;
435
436         (*context)->pdb_setsampwent = context_setsampwent;
437         (*context)->pdb_endsampwent = context_endsampwent;
438         (*context)->pdb_getsampwent = context_getsampwent;
439         (*context)->pdb_getsampwnam = context_getsampwnam;
440         (*context)->pdb_getsampwsid = context_getsampwsid;
441         (*context)->pdb_add_sam_account = context_add_sam_account;
442         (*context)->pdb_update_sam_account = context_update_sam_account;
443         (*context)->pdb_delete_sam_account = context_delete_sam_account;
444         (*context)->pdb_getgrsid = context_getgrsid;
445         (*context)->pdb_getgrgid = context_getgrgid;
446         (*context)->pdb_getgrnam = context_getgrnam;
447         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
448         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
449         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
450         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
451
452         (*context)->free_fn = free_pdb_context;
453
454         return NT_STATUS_OK;
455 }
456
457
458 /******************************************************************
459   Make a pdb_context, given an array of strings
460  *******************************************************************/
461
462 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
463 {
464         int i = 0;
465         struct pdb_methods *curmethods, *tmpmethods;
466         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
467
468         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
469                 return nt_status;
470         }
471
472         while (selected[i]){
473                 /* Try to initialise pdb */
474                 DEBUG(5,("Trying to load: %s\n", selected[i]));
475                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
476                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
477                         free_pdb_context(context);
478                         return nt_status;
479                 }
480                 curmethods->parent = *context;
481                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
482                 i++;
483         }
484
485         return NT_STATUS_OK;
486 }
487
488 /******************************************************************
489   Make a pdb_context, given a text string.
490  *******************************************************************/
491
492 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
493 {
494         NTSTATUS ret;
495         char **newsel = str_list_make(selected, NULL);
496         ret = make_pdb_context_list(context, (const char **)newsel);
497         str_list_free(&newsel);
498         return ret;
499 }
500
501 /******************************************************************
502  Return an already initialised pdb_context, to facilitate backward 
503  compatibility (see functions below).
504 *******************************************************************/
505
506 static struct pdb_context *pdb_get_static_context(BOOL reload) 
507 {
508         static struct pdb_context *pdb_context = NULL;
509
510         if ((pdb_context) && (reload)) {
511                 pdb_context->free_fn(&pdb_context);
512                 if (NT_STATUS_IS_ERR(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
513                         return NULL;
514                 }
515         }
516
517         if (!pdb_context) {
518                 if (NT_STATUS_IS_ERR(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
519                         return NULL;
520                 }
521         }
522
523         return pdb_context;
524 }
525
526 /******************************************************************
527  Backward compatibility functions for the original passdb interface
528 *******************************************************************/
529
530 BOOL pdb_setsampwent(BOOL update) 
531 {
532         struct pdb_context *pdb_context = pdb_get_static_context(False);
533
534         if (!pdb_context) {
535                 return False;
536         }
537
538         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update));
539 }
540
541 void pdb_endsampwent(void) 
542 {
543         struct pdb_context *pdb_context = pdb_get_static_context(False);
544
545         if (!pdb_context) {
546                 return;
547         }
548
549         pdb_context->pdb_endsampwent(pdb_context);
550 }
551
552 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
553 {
554         struct pdb_context *pdb_context = pdb_get_static_context(False);
555
556         if (!pdb_context) {
557                 return False;
558         }
559
560         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
561 }
562
563 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
564 {
565         struct pdb_context *pdb_context = pdb_get_static_context(False);
566
567         if (!pdb_context) {
568                 return False;
569         }
570
571         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username));
572 }
573
574 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
575 {
576         struct pdb_context *pdb_context = pdb_get_static_context(False);
577
578         if (!pdb_context) {
579                 return False;
580         }
581
582         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
583 }
584
585 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
586 {
587         struct pdb_context *pdb_context = pdb_get_static_context(False);
588
589         if (!pdb_context) {
590                 return False;
591         }
592
593         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
594 }
595
596 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
597 {
598         struct pdb_context *pdb_context = pdb_get_static_context(False);
599
600         if (!pdb_context) {
601                 return False;
602         }
603
604         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
605 }
606
607 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
608 {
609         struct pdb_context *pdb_context = pdb_get_static_context(False);
610
611         if (!pdb_context) {
612                 return False;
613         }
614
615         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
616 }
617
618 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid, BOOL with_priv)
619 {
620         struct pdb_context *pdb_context = pdb_get_static_context(False);
621
622         if (!pdb_context) {
623                 return False;
624         }
625
626         return NT_STATUS_IS_OK(pdb_context->
627                                pdb_getgrsid(pdb_context, map, sid, with_priv));
628 }
629
630 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid, BOOL with_priv)
631 {
632         struct pdb_context *pdb_context = pdb_get_static_context(False);
633
634         if (!pdb_context) {
635                 return False;
636         }
637
638         return NT_STATUS_IS_OK(pdb_context->
639                                pdb_getgrgid(pdb_context, map, gid, with_priv));
640 }
641
642 BOOL pdb_getgrnam(GROUP_MAP *map, char *name, BOOL with_priv)
643 {
644         struct pdb_context *pdb_context = pdb_get_static_context(False);
645
646         if (!pdb_context) {
647                 return False;
648         }
649
650         return NT_STATUS_IS_OK(pdb_context->
651                                pdb_getgrnam(pdb_context, map, name, with_priv));
652 }
653
654 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
655 {
656         struct pdb_context *pdb_context = pdb_get_static_context(False);
657
658         if (!pdb_context) {
659                 return False;
660         }
661
662         return NT_STATUS_IS_OK(pdb_context->
663                                pdb_add_group_mapping_entry(pdb_context, map));
664 }
665
666 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
667 {
668         struct pdb_context *pdb_context = pdb_get_static_context(False);
669
670         if (!pdb_context) {
671                 return False;
672         }
673
674         return NT_STATUS_IS_OK(pdb_context->
675                                pdb_update_group_mapping_entry(pdb_context, map));
676 }
677
678 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
679 {
680         struct pdb_context *pdb_context = pdb_get_static_context(False);
681
682         if (!pdb_context) {
683                 return False;
684         }
685
686         return NT_STATUS_IS_OK(pdb_context->
687                                pdb_delete_group_mapping_entry(pdb_context, sid));
688 }
689
690 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
691                             int *num_entries, BOOL unix_only, BOOL with_priv)
692 {
693         struct pdb_context *pdb_context = pdb_get_static_context(False);
694
695         if (!pdb_context) {
696                 return False;
697         }
698
699         return NT_STATUS_IS_OK(pdb_context->
700                                pdb_enum_group_mapping(pdb_context, sid_name_use,
701                                                       rmap, num_entries, unix_only,
702                                                       with_priv));
703 }
704
705 /***************************************************************
706   Initialize the static context (at smbd startup etc). 
707
708   If uninitialised, context will auto-init on first use.
709  ***************************************************************/
710
711 BOOL initialize_password_db(BOOL reload)
712 {       
713         return (pdb_get_static_context(reload) != NULL);
714 }
715
716
717 /***************************************************************************
718   Default implementations of some functions.
719  ****************************************************************************/
720
721 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
722 {
723         return NT_STATUS_NO_SUCH_USER;
724 }
725
726 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
727 {
728         return NT_STATUS_NO_SUCH_USER;
729 }
730
731 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
732 {
733         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
734         return NT_STATUS_NOT_IMPLEMENTED;
735 }
736
737 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
738 {
739         return NT_STATUS_NOT_IMPLEMENTED;
740 }
741
742 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
743 {
744         return NT_STATUS_NOT_IMPLEMENTED;
745 }
746
747 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update)
748 {
749         return NT_STATUS_NOT_IMPLEMENTED;
750 }
751
752 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
753 {
754         return NT_STATUS_NOT_IMPLEMENTED;
755 }
756
757 static void pdb_default_endsampwent(struct pdb_methods *methods)
758 {
759         return; /* NT_STATUS_NOT_IMPLEMENTED; */
760 }
761
762 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
763 {
764         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
765
766         if (!*methods) {
767                 return NT_STATUS_NO_MEMORY;
768         }
769
770         ZERO_STRUCTP(*methods);
771
772         (*methods)->setsampwent = pdb_default_setsampwent;
773         (*methods)->endsampwent = pdb_default_endsampwent;
774         (*methods)->getsampwent = pdb_default_getsampwent;
775         (*methods)->getsampwnam = pdb_default_getsampwnam;
776         (*methods)->getsampwsid = pdb_default_getsampwsid;
777         (*methods)->add_sam_account = pdb_default_add_sam_account;
778         (*methods)->update_sam_account = pdb_default_update_sam_account;
779         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
780
781         (*methods)->getgrsid = pdb_default_getgrsid;
782         (*methods)->getgrgid = pdb_default_getgrgid;
783         (*methods)->getgrnam = pdb_default_getgrnam;
784         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
785         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
786         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
787         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
788
789         return NT_STATUS_OK;
790 }