And another little const
[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 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_gettrustpwent(struct pdb_context *context,
430                                       SAM_TRUST_PASSWD *trust)
431 {
432         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
433         struct pdb_methods *cur_methods;
434         
435         if (!context) {
436                 DEBUG(0, ("invalid pdb_context specified!\n"));
437                 return ret;
438         }
439         
440         cur_methods = context->pdb_methods;
441         
442         while (cur_methods) {
443                 ret = cur_methods->gettrustpwent(cur_methods, trust);
444                 if (NT_STATUS_IS_OK(ret)) {
445                         trust->methods = cur_methods;
446                         return ret;
447                 }
448                 cur_methods = cur_methods->next;
449         }
450         
451         return ret;
452 }
453
454 static NTSTATUS context_gettrustpwsid(struct pdb_context *context,
455                                       SAM_TRUST_PASSWD *trust,
456                                       const DOM_SID *sid)
457 {
458         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
459         struct pdb_methods *cur_methods;
460         
461         if (!context) {
462                 DEBUG(0, ("invalid pdb_context specified!\n"));
463                 return ret;
464         }
465         
466         cur_methods = context->pdb_methods;
467         
468         while (cur_methods) {
469                 ret = cur_methods->gettrustpwsid(cur_methods, trust, sid);
470                 if (NT_STATUS_IS_OK(ret)) {
471                         trust->methods = cur_methods;
472                         return ret;
473                 }
474                 cur_methods = cur_methods->next;
475         }
476         
477         return ret;
478 }
479
480 static NTSTATUS context_add_trust_passwd(struct pdb_context *context,
481                                          SAM_TRUST_PASSWD *trust)
482 {
483         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
484         
485         if (!context) {
486                 DEBUG(0, ("invalid pdb_context specified!\n"));
487                 return ret;
488         }
489         
490         return context->pdb_methods->add_trust_passwd(context->pdb_methods, trust);
491 }
492
493 static NTSTATUS context_update_trust_passwd(struct pdb_context *context,
494                                             SAM_TRUST_PASSWD *trust)
495 {
496         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
497         
498         if (!context) {
499                 DEBUG(0, ("invalid pdb_context specified!\n"));
500                 return ret;
501         }
502         
503         if (!trust || !trust->methods) {
504                 DEBUG(0, ("invalid trust pointer specified!\n"));
505                 return ret;
506         }
507         
508         return trust->methods->update_trust_passwd(trust->methods, trust);
509 }
510
511 static NTSTATUS context_delete_trust_passwd(struct pdb_context *context,
512                                             SAM_TRUST_PASSWD *trust)
513 {
514         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
515         
516         if (!context) {
517                 DEBUG(0, ("invalid pdb_context specified!\n"));
518                 return ret;
519         }
520         
521         if (!trust || !trust->methods) {
522                 DEBUG(0, ("invalid trust pointer specified!\n"));
523                 return ret;
524         }
525         
526         return trust->methods->delete_trust_passwd(trust->methods, trust);
527 }
528
529 /******************************************************************
530   Free and cleanup a pdb context, any associated data and anything
531   that the attached modules might have associated.
532  *******************************************************************/
533
534 static void free_pdb_context(struct pdb_context **context)
535 {
536         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
537
538         while (pdb_selected){
539                 if(pdb_selected->free_private_data)
540                         pdb_selected->free_private_data(&(pdb_selected->private_data));
541                 pdb_selected = pdb_selected->next;
542         }
543
544         talloc_destroy((*context)->mem_ctx);
545         *context = NULL;
546 }
547
548 /******************************************************************
549   Make a pdb_methods from scratch
550  *******************************************************************/
551
552 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
553 {
554         char *module_name = smb_xstrdup(selected);
555         char *module_location = NULL, *p;
556         struct pdb_init_function_entry *entry;
557         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
558
559         lazy_initialize_passdb();
560
561         p = strchr(module_name, ':');
562
563         if (p) {
564                 *p = 0;
565                 module_location = p+1;
566                 trim_char(module_location, ' ', ' ');
567         }
568
569         trim_char(module_name, ' ', ' ');
570
571
572         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
573
574         entry = pdb_find_backend_entry(module_name);
575         
576         /* Try to find a module that contains this module */
577         if (!entry) { 
578                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
579                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
580                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
581                         SAFE_FREE(module_name);
582                         return NT_STATUS_UNSUCCESSFUL;
583                 }
584         }
585         
586         /* No such backend found */
587         if(!entry) { 
588                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
589                 SAFE_FREE(module_name);
590                 return NT_STATUS_INVALID_PARAMETER;
591         }
592
593         DEBUG(5,("Found pdb backend %s\n", module_name));
594         nt_status = entry->init(context, methods, module_location);
595         if (NT_STATUS_IS_OK(nt_status)) {
596                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
597         } else {
598                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
599         }
600         SAFE_FREE(module_name);
601         return nt_status;
602 }
603
604 /******************************************************************
605   Make a pdb_context from scratch.
606  *******************************************************************/
607
608 static NTSTATUS make_pdb_context(struct pdb_context **context) 
609 {
610         TALLOC_CTX *mem_ctx;
611
612         mem_ctx = talloc_init("pdb_context internal allocation context");
613
614         if (!mem_ctx) {
615                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
616                 return NT_STATUS_NO_MEMORY;
617         }               
618
619         *context = talloc(mem_ctx, sizeof(**context));
620         if (!*context) {
621                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
622                 return NT_STATUS_NO_MEMORY;
623         }
624
625         ZERO_STRUCTP(*context);
626
627         (*context)->mem_ctx = mem_ctx;
628
629         (*context)->pdb_setsampwent = context_setsampwent;
630         (*context)->pdb_endsampwent = context_endsampwent;
631         (*context)->pdb_getsampwent = context_getsampwent;
632         (*context)->pdb_getsampwnam = context_getsampwnam;
633         (*context)->pdb_getsampwsid = context_getsampwsid;
634         (*context)->pdb_add_sam_account = context_add_sam_account;
635         (*context)->pdb_update_sam_account = context_update_sam_account;
636         (*context)->pdb_delete_sam_account = context_delete_sam_account;
637         (*context)->pdb_getgrsid = context_getgrsid;
638         (*context)->pdb_getgrgid = context_getgrgid;
639         (*context)->pdb_getgrnam = context_getgrnam;
640         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
641         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
642         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
643         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
644         (*context)->pdb_gettrustpwent = context_gettrustpwent;
645         (*context)->pdb_gettrustpwsid = context_gettrustpwsid;
646         (*context)->pdb_add_trust_passwd = context_add_trust_passwd;
647         (*context)->pdb_update_trust_passwd = context_update_trust_passwd;
648         (*context)->pdb_delete_trust_passwd = context_delete_trust_passwd;
649
650         (*context)->free_fn = free_pdb_context;
651
652         return NT_STATUS_OK;
653 }
654
655
656 /******************************************************************
657   Make a pdb_context, given an array of strings
658  *******************************************************************/
659
660 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
661 {
662         int i = 0;
663         struct pdb_methods *curmethods, *tmpmethods;
664         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
665         BOOL have_guest = False;
666
667         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
668                 return nt_status;
669         }
670
671         if (!selected) {
672                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
673                 return nt_status;
674         }
675
676         while (selected[i]){
677                 if (strcmp(selected[i], "guest") == 0) {
678                         have_guest = True;
679                 }
680                 /* Try to initialise pdb */
681                 DEBUG(5,("Trying to load: %s\n", selected[i]));
682                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
683                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
684                         free_pdb_context(context);
685                         return nt_status;
686                 }
687                 curmethods->parent = *context;
688                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
689                 i++;
690         }
691
692         if (have_guest)
693                 return NT_STATUS_OK;
694
695         if ( (lp_guestaccount() == NULL) ||
696              (*lp_guestaccount() == '\0') ) {
697                 /* We explicitly don't want guest access. No idea what
698                    else that breaks, but be it that way. */
699                 return NT_STATUS_OK;
700         }
701
702         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
703                                                                *context,
704                                                                "guest"))) {
705                 DEBUG(1, ("Loading guest module failed!\n"));
706                 free_pdb_context(context);
707                 return nt_status;
708         }
709
710         curmethods->parent = *context;
711         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
712         
713         return NT_STATUS_OK;
714 }
715
716 /******************************************************************
717   Make a pdb_context, given a text string.
718  *******************************************************************/
719
720 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
721 {
722         NTSTATUS ret;
723         char **newsel = str_list_make(selected, NULL);
724         ret = make_pdb_context_list(context, (const char **)newsel);
725         str_list_free(&newsel);
726         return ret;
727 }
728
729 /******************************************************************
730  Return an already initialised pdb_context, to facilitate backward 
731  compatibility (see functions below).
732 *******************************************************************/
733
734 static struct pdb_context *pdb_get_static_context(BOOL reload) 
735 {
736         static struct pdb_context *pdb_context = NULL;
737
738         if ((pdb_context) && (reload)) {
739                 pdb_context->free_fn(&pdb_context);
740                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
741                         return NULL;
742                 }
743         }
744
745         if (!pdb_context) {
746                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
747                         return NULL;
748                 }
749         }
750
751         return pdb_context;
752 }
753
754 /******************************************************************
755  Backward compatibility functions for the original passdb interface
756 *******************************************************************/
757
758 BOOL pdb_setsampwent(BOOL update) 
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_setsampwent(pdb_context, update));
767 }
768
769 void pdb_endsampwent(void) 
770 {
771         struct pdb_context *pdb_context = pdb_get_static_context(False);
772
773         if (!pdb_context) {
774                 return;
775         }
776
777         pdb_context->pdb_endsampwent(pdb_context);
778 }
779
780 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
781 {
782         struct pdb_context *pdb_context = pdb_get_static_context(False);
783
784         if (!pdb_context) {
785                 return False;
786         }
787
788         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
789 }
790
791 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
792 {
793         struct pdb_context *pdb_context = pdb_get_static_context(False);
794
795         if (!pdb_context) {
796                 return False;
797         }
798
799         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username));
800 }
801
802 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
803 {
804         struct pdb_context *pdb_context = pdb_get_static_context(False);
805
806         if (!pdb_context) {
807                 return False;
808         }
809
810         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
811 }
812
813 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
814 {
815         struct pdb_context *pdb_context = pdb_get_static_context(False);
816         const char *lm_pw, *nt_pw;
817         uint16 acb_flags;
818
819         if (!pdb_context) {
820                 return False;
821         }
822         
823         /* disable acccounts with no passwords (that has not 
824            been allowed by the  ACB_PWNOTREQ bit */
825
826         lm_pw = pdb_get_lanman_passwd( sam_acct );
827         nt_pw = pdb_get_nt_passwd( sam_acct );
828         acb_flags = pdb_get_acct_ctrl( sam_acct );
829         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
830                 acb_flags |= ACB_DISABLED;
831                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
832         }
833
834         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
835 }
836
837 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
838 {
839         struct pdb_context *pdb_context = pdb_get_static_context(False);
840         const char *lm_pw, *nt_pw;
841         uint16 acb_flags;
842
843         if (!pdb_context) {
844                 return False;
845         }
846
847         /* disable acccounts with no passwords (that has not 
848            been allowed by the  ACB_PWNOTREQ bit */
849         
850         lm_pw = pdb_get_lanman_passwd( sam_acct );
851         nt_pw = pdb_get_nt_passwd( sam_acct );
852         acb_flags = pdb_get_acct_ctrl( sam_acct );
853         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
854                 acb_flags |= ACB_DISABLED;
855                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
856         }
857
858         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
859 }
860
861 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
862 {
863         struct pdb_context *pdb_context = pdb_get_static_context(False);
864
865         if (!pdb_context) {
866                 return False;
867         }
868
869         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
870 }
871
872 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
873 {
874         struct pdb_context *pdb_context = pdb_get_static_context(False);
875
876         if (!pdb_context) {
877                 return False;
878         }
879
880         return NT_STATUS_IS_OK(pdb_context->
881                                pdb_getgrsid(pdb_context, map, sid));
882 }
883
884 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
885 {
886         struct pdb_context *pdb_context = pdb_get_static_context(False);
887
888         if (!pdb_context) {
889                 return False;
890         }
891
892         return NT_STATUS_IS_OK(pdb_context->
893                                pdb_getgrgid(pdb_context, map, gid));
894 }
895
896 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
897 {
898         struct pdb_context *pdb_context = pdb_get_static_context(False);
899
900         if (!pdb_context) {
901                 return False;
902         }
903
904         return NT_STATUS_IS_OK(pdb_context->
905                                pdb_getgrnam(pdb_context, map, name));
906 }
907
908 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
909 {
910         struct pdb_context *pdb_context = pdb_get_static_context(False);
911
912         if (!pdb_context) {
913                 return False;
914         }
915
916         return NT_STATUS_IS_OK(pdb_context->
917                                pdb_add_group_mapping_entry(pdb_context, map));
918 }
919
920 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
921 {
922         struct pdb_context *pdb_context = pdb_get_static_context(False);
923
924         if (!pdb_context) {
925                 return False;
926         }
927
928         return NT_STATUS_IS_OK(pdb_context->
929                                pdb_update_group_mapping_entry(pdb_context, map));
930 }
931
932 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
933 {
934         struct pdb_context *pdb_context = pdb_get_static_context(False);
935
936         if (!pdb_context) {
937                 return False;
938         }
939
940         return NT_STATUS_IS_OK(pdb_context->
941                                pdb_delete_group_mapping_entry(pdb_context, sid));
942 }
943
944 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
945                             int *num_entries, BOOL unix_only)
946 {
947         struct pdb_context *pdb_context = pdb_get_static_context(False);
948
949         if (!pdb_context) {
950                 return False;
951         }
952
953         return NT_STATUS_IS_OK(pdb_context->
954                                pdb_enum_group_mapping(pdb_context, sid_name_use,
955                                                       rmap, num_entries, unix_only));
956 }
957
958 /***************************************************************
959   Initialize the static context (at smbd startup etc). 
960
961   If uninitialised, context will auto-init on first use.
962  ***************************************************************/
963
964 BOOL initialize_password_db(BOOL reload)
965 {       
966         return (pdb_get_static_context(reload) != NULL);
967 }
968
969
970 /***************************************************************************
971   Default implementations of some functions.
972  ****************************************************************************/
973
974 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
975 {
976         return NT_STATUS_NO_SUCH_USER;
977 }
978
979 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
980 {
981         return NT_STATUS_NO_SUCH_USER;
982 }
983
984 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
985 {
986         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
987         return NT_STATUS_NOT_IMPLEMENTED;
988 }
989
990 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
991 {
992         return NT_STATUS_NOT_IMPLEMENTED;
993 }
994
995 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
996 {
997         return NT_STATUS_NOT_IMPLEMENTED;
998 }
999
1000 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update)
1001 {
1002         return NT_STATUS_NOT_IMPLEMENTED;
1003 }
1004
1005 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1006 {
1007         return NT_STATUS_NOT_IMPLEMENTED;
1008 }
1009
1010 static void pdb_default_endsampwent(struct pdb_methods *methods)
1011 {
1012         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1013 }
1014
1015 static NTSTATUS pdb_default_gettrustpwent(struct pdb_methods *methods, SAM_TRUST_PASSWD* trust)
1016 {
1017         return NT_STATUS_NOT_IMPLEMENTED;
1018 }
1019
1020 static NTSTATUS pdb_default_gettrustpwsid(struct pdb_methods *methods, SAM_TRUST_PASSWD* trust,
1021                                           const DOM_SID* sid)
1022 {
1023         return NT_STATUS_NOT_IMPLEMENTED;
1024 }
1025
1026 static NTSTATUS pdb_default_add_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1027 {
1028         return NT_STATUS_NOT_IMPLEMENTED;
1029 }
1030
1031 static NTSTATUS pdb_default_update_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1032 {
1033         return NT_STATUS_NOT_IMPLEMENTED;
1034 }
1035
1036 static NTSTATUS pdb_default_delete_trust_passwd(struct pdb_methods *methods, const SAM_TRUST_PASSWD* trust)
1037 {
1038         return NT_STATUS_NOT_IMPLEMENTED;
1039 }
1040
1041
1042 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
1043 {
1044         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
1045
1046         if (!*methods) {
1047                 return NT_STATUS_NO_MEMORY;
1048         }
1049
1050         ZERO_STRUCTP(*methods);
1051
1052         (*methods)->setsampwent = pdb_default_setsampwent;
1053         (*methods)->endsampwent = pdb_default_endsampwent;
1054         (*methods)->getsampwent = pdb_default_getsampwent;
1055         (*methods)->getsampwnam = pdb_default_getsampwnam;
1056         (*methods)->getsampwsid = pdb_default_getsampwsid;
1057         (*methods)->add_sam_account = pdb_default_add_sam_account;
1058         (*methods)->update_sam_account = pdb_default_update_sam_account;
1059         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1060
1061         (*methods)->getgrsid = pdb_default_getgrsid;
1062         (*methods)->getgrgid = pdb_default_getgrgid;
1063         (*methods)->getgrnam = pdb_default_getgrnam;
1064         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1065         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1066         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1067         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1068         
1069         (*methods)->gettrustpwent = pdb_default_gettrustpwent;
1070         (*methods)->gettrustpwsid = pdb_default_gettrustpwsid;
1071         (*methods)->add_trust_passwd = pdb_default_add_trust_passwd;
1072         (*methods)->update_trust_passwd = pdb_default_update_trust_passwd;
1073         (*methods)->delete_trust_passwd = pdb_default_delete_trust_passwd;
1074
1075         return NT_STATUS_OK;
1076 }