r3704: Implement a cache get saves the result of a pdb_getsampwnam for later
[amitay/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_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         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_CHANGED );
252         }
253         
254         /** @todo  This is where a 're-read on add' should be done */
255         /* We now add a new account to the first database listed. 
256          * Should we? */
257
258         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
259 }
260
261 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
262 {
263         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
264         const char *lm_pw, *nt_pw;
265         uint16 acb_flags;
266
267         if (!context) {
268                 DEBUG(0, ("invalid pdb_context specified!\n"));
269                 return ret;
270         }
271
272         if (!sam_acct || !sam_acct->methods){
273                 DEBUG(0, ("invalid sam_acct specified\n"));
274                 return ret;
275         }
276
277         /* disable acccounts with no passwords (that has not 
278            been allowed by the  ACB_PWNOTREQ bit */
279         
280         lm_pw = pdb_get_lanman_passwd( sam_acct );
281         nt_pw = pdb_get_nt_passwd( sam_acct );
282         acb_flags = pdb_get_acct_ctrl( sam_acct );
283         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
284                 acb_flags |= ACB_DISABLED;
285                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
286         }
287         
288         /** @todo  This is where a 're-read on update' should be done */
289
290         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
291 }
292
293 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
294 {
295         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
296
297         struct pdb_methods *pdb_selected;
298         if (!context) {
299                 DEBUG(0, ("invalid pdb_context specified!\n"));
300                 return ret;
301         }
302
303         if (!sam_acct->methods){
304                 pdb_selected = context->pdb_methods;
305                 /* There's no passdb backend specified for this account.
306                  * Try to delete it in every passdb available 
307                  * Needed to delete accounts in smbpasswd that are not
308                  * in /etc/passwd.
309                  */
310                 while (pdb_selected){
311                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
312                                 return ret;
313                         }
314                         pdb_selected = pdb_selected->next;
315                 }
316                 return ret;
317         }
318
319         if (!sam_acct->methods->delete_sam_account){
320                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
321                 return ret;
322         }
323         
324         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
325 }
326
327 static NTSTATUS context_getgrsid(struct pdb_context *context,
328                                  GROUP_MAP *map, DOM_SID sid)
329 {
330         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
331
332         struct pdb_methods *curmethods;
333         if ((!context)) {
334                 DEBUG(0, ("invalid pdb_context specified!\n"));
335                 return ret;
336         }
337         curmethods = context->pdb_methods;
338         while (curmethods){
339                 ret = curmethods->getgrsid(curmethods, map, sid);
340                 if (NT_STATUS_IS_OK(ret)) {
341                         map->methods = curmethods;
342                         return ret;
343                 }
344                 curmethods = curmethods->next;
345         }
346
347         return ret;
348 }
349
350 static NTSTATUS context_getgrgid(struct pdb_context *context,
351                                  GROUP_MAP *map, gid_t gid)
352 {
353         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
354
355         struct pdb_methods *curmethods;
356         if ((!context)) {
357                 DEBUG(0, ("invalid pdb_context specified!\n"));
358                 return ret;
359         }
360         curmethods = context->pdb_methods;
361         while (curmethods){
362                 ret = curmethods->getgrgid(curmethods, map, gid);
363                 if (NT_STATUS_IS_OK(ret)) {
364                         map->methods = curmethods;
365                         return ret;
366                 }
367                 curmethods = curmethods->next;
368         }
369
370         return ret;
371 }
372
373 static NTSTATUS context_getgrnam(struct pdb_context *context,
374                                  GROUP_MAP *map, const char *name)
375 {
376         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
377
378         struct pdb_methods *curmethods;
379         if ((!context)) {
380                 DEBUG(0, ("invalid pdb_context specified!\n"));
381                 return ret;
382         }
383         curmethods = context->pdb_methods;
384         while (curmethods){
385                 ret = curmethods->getgrnam(curmethods, map, name);
386                 if (NT_STATUS_IS_OK(ret)) {
387                         map->methods = curmethods;
388                         return ret;
389                 }
390                 curmethods = curmethods->next;
391         }
392
393         return ret;
394 }
395
396 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
397                                                 GROUP_MAP *map)
398 {
399         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
400
401         if ((!context) || (!context->pdb_methods)) {
402                 DEBUG(0, ("invalid pdb_context specified!\n"));
403                 return ret;
404         }
405
406         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
407                                                              map);
408 }
409
410 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
411                                                    GROUP_MAP *map)
412 {
413         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
414
415         if ((!context) || (!context->pdb_methods)) {
416                 DEBUG(0, ("invalid pdb_context specified!\n"));
417                 return ret;
418         }
419
420         return context->
421                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
422 }
423
424 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
425                                                    DOM_SID sid)
426 {
427         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
428
429         if ((!context) || (!context->pdb_methods)) {
430                 DEBUG(0, ("invalid pdb_context specified!\n"));
431                 return ret;
432         }
433
434         return context->
435                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
436 }
437
438 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
439                                            enum SID_NAME_USE sid_name_use,
440                                            GROUP_MAP **rmap, int *num_entries,
441                                            BOOL unix_only)
442 {
443         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
444
445         if ((!context) || (!context->pdb_methods)) {
446                 DEBUG(0, ("invalid pdb_context specified!\n"));
447                 return ret;
448         }
449
450         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
451                                                         sid_name_use, rmap,
452                                                         num_entries, unix_only);
453 }
454
455 static NTSTATUS context_find_alias(struct pdb_context *context,
456                                    const char *name, DOM_SID *sid)
457 {
458         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
459
460         if ((!context) || (!context->pdb_methods)) {
461                 DEBUG(0, ("invalid pdb_context specified!\n"));
462                 return ret;
463         }
464
465         return context->pdb_methods->find_alias(context->pdb_methods,
466                                                 name, sid);
467 }
468
469 static NTSTATUS context_create_alias(struct pdb_context *context,
470                                      const char *name, uint32 *rid)
471 {
472         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
473
474         if ((!context) || (!context->pdb_methods)) {
475                 DEBUG(0, ("invalid pdb_context specified!\n"));
476                 return ret;
477         }
478
479         return context->pdb_methods->create_alias(context->pdb_methods,
480                                                   name, rid);
481 }
482
483 static NTSTATUS context_delete_alias(struct pdb_context *context,
484                                      const DOM_SID *sid)
485 {
486         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
487
488         if ((!context) || (!context->pdb_methods)) {
489                 DEBUG(0, ("invalid pdb_context specified!\n"));
490                 return ret;
491         }
492
493         return context->pdb_methods->delete_alias(context->pdb_methods, sid);
494 }
495
496 static NTSTATUS context_enum_aliases(struct pdb_context *context,
497                                      const DOM_SID *sid,
498                                      uint32 start_idx, uint32 max_entries,
499                                      uint32 *num_aliases,
500                                      struct acct_info **info)
501 {
502         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
503
504         if ((!context) || (!context->pdb_methods)) {
505                 DEBUG(0, ("invalid pdb_context specified!\n"));
506                 return ret;
507         }
508
509         return context->pdb_methods->enum_aliases(context->pdb_methods,
510                                                   sid, start_idx, max_entries,
511                                                   num_aliases, info);
512 }
513
514 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
515                                       const DOM_SID *sid,
516                                       struct acct_info *info)
517 {
518         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
519
520         if ((!context) || (!context->pdb_methods)) {
521                 DEBUG(0, ("invalid pdb_context specified!\n"));
522                 return ret;
523         }
524
525         return context->pdb_methods->get_aliasinfo(context->pdb_methods,
526                                                    sid, info);
527 }
528
529 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
530                                       const DOM_SID *sid,
531                                       struct acct_info *info)
532 {
533         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
534
535         if ((!context) || (!context->pdb_methods)) {
536                 DEBUG(0, ("invalid pdb_context specified!\n"));
537                 return ret;
538         }
539
540         return context->pdb_methods->set_aliasinfo(context->pdb_methods,
541                                                    sid, info);
542 }
543
544 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
545                                      const DOM_SID *alias,
546                                      const DOM_SID *member)
547 {
548         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
549
550         if ((!context) || (!context->pdb_methods)) {
551                 DEBUG(0, ("invalid pdb_context specified!\n"));
552                 return ret;
553         }
554
555         return context->pdb_methods->add_aliasmem(context->pdb_methods,
556                                                   alias, member);
557 }
558         
559 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
560                                      const DOM_SID *alias,
561                                      const DOM_SID *member)
562 {
563         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
564
565         if ((!context) || (!context->pdb_methods)) {
566                 DEBUG(0, ("invalid pdb_context specified!\n"));
567                 return ret;
568         }
569
570         return context->pdb_methods->del_aliasmem(context->pdb_methods,
571                                                   alias, member);
572 }
573         
574 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
575                                       const DOM_SID *alias, DOM_SID **members,
576                                       int *num)
577 {
578         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
579
580         if ((!context) || (!context->pdb_methods)) {
581                 DEBUG(0, ("invalid pdb_context specified!\n"));
582                 return ret;
583         }
584
585         return context->pdb_methods->enum_aliasmem(context->pdb_methods,
586                                                    alias, members, num);
587 }
588         
589 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
590                                                const DOM_SID *members,
591                                                int num_members,
592                                                DOM_SID **aliases, int *num)
593 {
594         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
595
596         if ((!context) || (!context->pdb_methods)) {
597                 DEBUG(0, ("invalid pdb_context specified!\n"));
598                 return ret;
599         }
600
601         return context->pdb_methods->
602                 enum_alias_memberships(context->pdb_methods, members,
603                                        num_members, aliases, num);
604 }
605         
606 /******************************************************************
607   Free and cleanup a pdb context, any associated data and anything
608   that the attached modules might have associated.
609  *******************************************************************/
610
611 static void free_pdb_context(struct pdb_context **context)
612 {
613         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
614
615         while (pdb_selected){
616                 if(pdb_selected->free_private_data)
617                         pdb_selected->free_private_data(&(pdb_selected->private_data));
618                 pdb_selected = pdb_selected->next;
619         }
620
621         talloc_destroy((*context)->mem_ctx);
622         *context = NULL;
623 }
624
625 /******************************************************************
626   Make a pdb_methods from scratch
627  *******************************************************************/
628
629 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
630 {
631         char *module_name = smb_xstrdup(selected);
632         char *module_location = NULL, *p;
633         struct pdb_init_function_entry *entry;
634         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
635
636         lazy_initialize_passdb();
637
638         p = strchr(module_name, ':');
639
640         if (p) {
641                 *p = 0;
642                 module_location = p+1;
643                 trim_char(module_location, ' ', ' ');
644         }
645
646         trim_char(module_name, ' ', ' ');
647
648
649         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
650
651         entry = pdb_find_backend_entry(module_name);
652         
653         /* Try to find a module that contains this module */
654         if (!entry) { 
655                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
656                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
657                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
658                         SAFE_FREE(module_name);
659                         return NT_STATUS_UNSUCCESSFUL;
660                 }
661         }
662         
663         /* No such backend found */
664         if(!entry) { 
665                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
666                 SAFE_FREE(module_name);
667                 return NT_STATUS_INVALID_PARAMETER;
668         }
669
670         DEBUG(5,("Found pdb backend %s\n", module_name));
671         nt_status = entry->init(context, methods, module_location);
672         if (NT_STATUS_IS_OK(nt_status)) {
673                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
674         } else {
675                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
676         }
677         SAFE_FREE(module_name);
678         return nt_status;
679 }
680
681 /******************************************************************
682   Make a pdb_context from scratch.
683  *******************************************************************/
684
685 static NTSTATUS make_pdb_context(struct pdb_context **context) 
686 {
687         TALLOC_CTX *mem_ctx;
688
689         mem_ctx = talloc_init("pdb_context internal allocation context");
690
691         if (!mem_ctx) {
692                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
693                 return NT_STATUS_NO_MEMORY;
694         }               
695
696         *context = talloc(mem_ctx, sizeof(**context));
697         if (!*context) {
698                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
699                 return NT_STATUS_NO_MEMORY;
700         }
701
702         ZERO_STRUCTP(*context);
703
704         (*context)->mem_ctx = mem_ctx;
705
706         (*context)->pdb_setsampwent = context_setsampwent;
707         (*context)->pdb_endsampwent = context_endsampwent;
708         (*context)->pdb_getsampwent = context_getsampwent;
709         (*context)->pdb_getsampwnam = context_getsampwnam;
710         (*context)->pdb_getsampwsid = context_getsampwsid;
711         (*context)->pdb_add_sam_account = context_add_sam_account;
712         (*context)->pdb_update_sam_account = context_update_sam_account;
713         (*context)->pdb_delete_sam_account = context_delete_sam_account;
714         (*context)->pdb_getgrsid = context_getgrsid;
715         (*context)->pdb_getgrgid = context_getgrgid;
716         (*context)->pdb_getgrnam = context_getgrnam;
717         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
718         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
719         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
720         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
721
722         (*context)->pdb_find_alias = context_find_alias;
723         (*context)->pdb_create_alias = context_create_alias;
724         (*context)->pdb_delete_alias = context_delete_alias;
725         (*context)->pdb_enum_aliases = context_enum_aliases;
726         (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
727         (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
728         (*context)->pdb_add_aliasmem = context_add_aliasmem;
729         (*context)->pdb_del_aliasmem = context_del_aliasmem;
730         (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
731         (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
732
733         (*context)->free_fn = free_pdb_context;
734
735         return NT_STATUS_OK;
736 }
737
738
739 /******************************************************************
740   Make a pdb_context, given an array of strings
741  *******************************************************************/
742
743 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
744 {
745         int i = 0;
746         struct pdb_methods *curmethods, *tmpmethods;
747         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
748         BOOL have_guest = False;
749
750         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
751                 return nt_status;
752         }
753
754         if (!selected) {
755                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
756                 return nt_status;
757         }
758
759         while (selected[i]){
760                 if (strcmp(selected[i], "guest") == 0) {
761                         have_guest = True;
762                 }
763                 /* Try to initialise pdb */
764                 DEBUG(5,("Trying to load: %s\n", selected[i]));
765                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
766                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
767                         free_pdb_context(context);
768                         return nt_status;
769                 }
770                 curmethods->parent = *context;
771                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
772                 i++;
773         }
774
775         if (have_guest)
776                 return NT_STATUS_OK;
777
778         if ( (lp_guestaccount() == NULL) ||
779              (*lp_guestaccount() == '\0') ) {
780                 /* We explicitly don't want guest access. No idea what
781                    else that breaks, but be it that way. */
782                 return NT_STATUS_OK;
783         }
784
785         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
786                                                                *context,
787                                                                "guest"))) {
788                 DEBUG(1, ("Loading guest module failed!\n"));
789                 free_pdb_context(context);
790                 return nt_status;
791         }
792
793         curmethods->parent = *context;
794         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
795         
796         return NT_STATUS_OK;
797 }
798
799 /******************************************************************
800   Make a pdb_context, given a text string.
801  *******************************************************************/
802
803 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
804 {
805         NTSTATUS ret;
806         char **newsel = str_list_make(selected, NULL);
807         ret = make_pdb_context_list(context, (const char **)newsel);
808         str_list_free(&newsel);
809         return ret;
810 }
811
812 /******************************************************************
813  Return an already initialised pdb_context, to facilitate backward 
814  compatibility (see functions below).
815 *******************************************************************/
816
817 static struct pdb_context *pdb_get_static_context(BOOL reload) 
818 {
819         static struct pdb_context *pdb_context = NULL;
820
821         if ((pdb_context) && (reload)) {
822                 pdb_context->free_fn(&pdb_context);
823                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
824                         return NULL;
825                 }
826         }
827
828         if (!pdb_context) {
829                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
830                         return NULL;
831                 }
832         }
833
834         return pdb_context;
835 }
836
837 /******************************************************************
838  Backward compatibility functions for the original passdb interface
839 *******************************************************************/
840
841 BOOL pdb_setsampwent(BOOL update) 
842 {
843         struct pdb_context *pdb_context = pdb_get_static_context(False);
844
845         if (!pdb_context) {
846                 return False;
847         }
848
849         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update));
850 }
851
852 void pdb_endsampwent(void) 
853 {
854         struct pdb_context *pdb_context = pdb_get_static_context(False);
855
856         if (!pdb_context) {
857                 return;
858         }
859
860         pdb_context->pdb_endsampwent(pdb_context);
861 }
862
863 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
864 {
865         struct pdb_context *pdb_context = pdb_get_static_context(False);
866
867         if (!pdb_context) {
868                 return False;
869         }
870
871         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
872 }
873
874 static SAM_ACCOUNT *sam_account_cache = NULL;
875
876 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
877 {
878         struct pdb_context *pdb_context = pdb_get_static_context(False);
879
880         if (!pdb_context) {
881                 return False;
882         }
883
884         if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
885                                                           sam_acct, username)))
886                 return False;
887
888         if (sam_account_cache != NULL) {
889                 pdb_free_sam(&sam_account_cache);
890                 sam_account_cache = NULL;
891         }
892
893         pdb_copy_sam_account(sam_acct, &sam_account_cache);
894         return True;
895 }
896
897 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
898 {
899         struct pdb_context *pdb_context = pdb_get_static_context(False);
900
901         if (!pdb_context) {
902                 return False;
903         }
904
905         if ((sam_account_cache != NULL) &&
906             (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
907                 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
908
909         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
910 }
911
912 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
913 {
914         struct pdb_context *pdb_context = pdb_get_static_context(False);
915
916         if (!pdb_context) {
917                 return False;
918         }
919         
920         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
921 }
922
923 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
924 {
925         struct pdb_context *pdb_context = pdb_get_static_context(False);
926
927         if (!pdb_context) {
928                 return False;
929         }
930
931         if (sam_account_cache != NULL) {
932                 pdb_free_sam(&sam_account_cache);
933                 sam_account_cache = NULL;
934         }
935
936         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
937 }
938
939 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
940 {
941         struct pdb_context *pdb_context = pdb_get_static_context(False);
942
943         if (!pdb_context) {
944                 return False;
945         }
946
947         if (sam_account_cache != NULL) {
948                 pdb_free_sam(&sam_account_cache);
949                 sam_account_cache = NULL;
950         }
951
952         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
953 }
954
955 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
956 {
957         struct pdb_context *pdb_context = pdb_get_static_context(False);
958
959         if (!pdb_context) {
960                 return False;
961         }
962
963         return NT_STATUS_IS_OK(pdb_context->
964                                pdb_getgrsid(pdb_context, map, sid));
965 }
966
967 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
968 {
969         struct pdb_context *pdb_context = pdb_get_static_context(False);
970
971         if (!pdb_context) {
972                 return False;
973         }
974
975         return NT_STATUS_IS_OK(pdb_context->
976                                pdb_getgrgid(pdb_context, map, gid));
977 }
978
979 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
980 {
981         struct pdb_context *pdb_context = pdb_get_static_context(False);
982
983         if (!pdb_context) {
984                 return False;
985         }
986
987         return NT_STATUS_IS_OK(pdb_context->
988                                pdb_getgrnam(pdb_context, map, name));
989 }
990
991 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
992 {
993         struct pdb_context *pdb_context = pdb_get_static_context(False);
994
995         if (!pdb_context) {
996                 return False;
997         }
998
999         return NT_STATUS_IS_OK(pdb_context->
1000                                pdb_add_group_mapping_entry(pdb_context, map));
1001 }
1002
1003 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1004 {
1005         struct pdb_context *pdb_context = pdb_get_static_context(False);
1006
1007         if (!pdb_context) {
1008                 return False;
1009         }
1010
1011         return NT_STATUS_IS_OK(pdb_context->
1012                                pdb_update_group_mapping_entry(pdb_context, map));
1013 }
1014
1015 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1016 {
1017         struct pdb_context *pdb_context = pdb_get_static_context(False);
1018
1019         if (!pdb_context) {
1020                 return False;
1021         }
1022
1023         return NT_STATUS_IS_OK(pdb_context->
1024                                pdb_delete_group_mapping_entry(pdb_context, sid));
1025 }
1026
1027 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
1028                             int *num_entries, BOOL unix_only)
1029 {
1030         struct pdb_context *pdb_context = pdb_get_static_context(False);
1031
1032         if (!pdb_context) {
1033                 return False;
1034         }
1035
1036         return NT_STATUS_IS_OK(pdb_context->
1037                                pdb_enum_group_mapping(pdb_context, sid_name_use,
1038                                                       rmap, num_entries, unix_only));
1039 }
1040
1041 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1042 {
1043         struct pdb_context *pdb_context = pdb_get_static_context(False);
1044
1045         if (!pdb_context) {
1046                 return False;
1047         }
1048
1049         return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1050                                                              name, sid));
1051 }
1052
1053 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1054 {
1055         struct pdb_context *pdb_context = pdb_get_static_context(False);
1056
1057         if (!pdb_context) {
1058                 return NT_STATUS_NOT_IMPLEMENTED;
1059         }
1060
1061         return pdb_context->pdb_create_alias(pdb_context, name, rid);
1062 }
1063
1064 BOOL pdb_delete_alias(const DOM_SID *sid)
1065 {
1066         struct pdb_context *pdb_context = pdb_get_static_context(False);
1067
1068         if (!pdb_context) {
1069                 return False;
1070         }
1071
1072         return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1073                                                              sid));
1074                                                             
1075 }
1076
1077 BOOL pdb_enum_aliases(const DOM_SID *sid, uint32 start_idx, uint32 max_entries,
1078                       uint32 *num_aliases, struct acct_info **info)
1079 {
1080         struct pdb_context *pdb_context = pdb_get_static_context(False);
1081
1082         if (!pdb_context) {
1083                 return False;
1084         }
1085
1086         return NT_STATUS_IS_OK(pdb_context->pdb_enum_aliases(pdb_context, sid,
1087                                                              start_idx,
1088                                                              max_entries,
1089                                                              num_aliases,
1090                                                              info));
1091 }
1092
1093 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1094 {
1095         struct pdb_context *pdb_context = pdb_get_static_context(False);
1096
1097         if (!pdb_context) {
1098                 return False;
1099         }
1100
1101         return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1102                                                               info));
1103 }
1104
1105 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1106 {
1107         struct pdb_context *pdb_context = pdb_get_static_context(False);
1108
1109         if (!pdb_context) {
1110                 return False;
1111         }
1112
1113         return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1114                                                               info));
1115 }
1116
1117 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1118 {
1119         struct pdb_context *pdb_context = pdb_get_static_context(False);
1120
1121         if (!pdb_context) {
1122                 return False;
1123         }
1124
1125         return NT_STATUS_IS_OK(pdb_context->
1126                                pdb_add_aliasmem(pdb_context, alias, member));
1127 }
1128
1129 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1130 {
1131         struct pdb_context *pdb_context = pdb_get_static_context(False);
1132
1133         if (!pdb_context) {
1134                 return False;
1135         }
1136
1137         return NT_STATUS_IS_OK(pdb_context->
1138                                pdb_del_aliasmem(pdb_context, alias, member));
1139 }
1140
1141 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1142                        DOM_SID **members, int *num_members)
1143 {
1144         struct pdb_context *pdb_context = pdb_get_static_context(False);
1145
1146         if (!pdb_context) {
1147                 return False;
1148         }
1149
1150         return NT_STATUS_IS_OK(pdb_context->
1151                                pdb_enum_aliasmem(pdb_context, alias,
1152                                                  members, num_members));
1153 }
1154
1155 BOOL pdb_enum_alias_memberships(const DOM_SID *members, int num_members,
1156                                 DOM_SID **aliases, int *num)
1157 {
1158         struct pdb_context *pdb_context = pdb_get_static_context(False);
1159
1160         if (!pdb_context) {
1161                 return False;
1162         }
1163
1164         return NT_STATUS_IS_OK(pdb_context->
1165                                pdb_enum_alias_memberships(pdb_context, members,
1166                                                           num_members,
1167                                                           aliases, num));
1168 }
1169
1170 /***************************************************************
1171   Initialize the static context (at smbd startup etc). 
1172
1173   If uninitialised, context will auto-init on first use.
1174  ***************************************************************/
1175
1176 BOOL initialize_password_db(BOOL reload)
1177 {       
1178         return (pdb_get_static_context(reload) != NULL);
1179 }
1180
1181
1182 /***************************************************************************
1183   Default implementations of some functions.
1184  ****************************************************************************/
1185
1186 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1187 {
1188         return NT_STATUS_NO_SUCH_USER;
1189 }
1190
1191 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1192 {
1193         return NT_STATUS_NO_SUCH_USER;
1194 }
1195
1196 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1197 {
1198         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1199         return NT_STATUS_NOT_IMPLEMENTED;
1200 }
1201
1202 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1203 {
1204         return NT_STATUS_NOT_IMPLEMENTED;
1205 }
1206
1207 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1208 {
1209         return NT_STATUS_NOT_IMPLEMENTED;
1210 }
1211
1212 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update)
1213 {
1214         return NT_STATUS_NOT_IMPLEMENTED;
1215 }
1216
1217 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1218 {
1219         return NT_STATUS_NOT_IMPLEMENTED;
1220 }
1221
1222 static void pdb_default_endsampwent(struct pdb_methods *methods)
1223 {
1224         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1225 }
1226
1227 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
1228 {
1229         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
1230
1231         if (!*methods) {
1232                 return NT_STATUS_NO_MEMORY;
1233         }
1234
1235         ZERO_STRUCTP(*methods);
1236
1237         (*methods)->setsampwent = pdb_default_setsampwent;
1238         (*methods)->endsampwent = pdb_default_endsampwent;
1239         (*methods)->getsampwent = pdb_default_getsampwent;
1240         (*methods)->getsampwnam = pdb_default_getsampwnam;
1241         (*methods)->getsampwsid = pdb_default_getsampwsid;
1242         (*methods)->add_sam_account = pdb_default_add_sam_account;
1243         (*methods)->update_sam_account = pdb_default_update_sam_account;
1244         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1245
1246         (*methods)->getgrsid = pdb_default_getgrsid;
1247         (*methods)->getgrgid = pdb_default_getgrgid;
1248         (*methods)->getgrnam = pdb_default_getgrnam;
1249         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1250         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1251         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1252         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1253         (*methods)->find_alias = pdb_default_find_alias;
1254         (*methods)->create_alias = pdb_default_create_alias;
1255         (*methods)->delete_alias = pdb_default_delete_alias;
1256         (*methods)->enum_aliases = pdb_default_enum_aliases;
1257         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
1258         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
1259         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
1260         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
1261         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
1262         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
1263
1264         return NT_STATUS_OK;
1265 }