This is the 'multiple pdb backends' patch from ctrlsoft, aka Jelmer Vernooij
[kai/samba.git] / source3 / passdb / pdb_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Jelmer Vernooij                        2002
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /** List of various built-in passdb modules */
25
26 const struct pdb_init_function_entry builtin_pdb_init_functions[] = {
27         { "smbpasswd", pdb_init_smbpasswd },
28         { "smbpasswd_nua", pdb_init_smbpasswd_nua },
29         { "tdbsam", pdb_init_tdbsam },
30         { "tdbsam_nua", pdb_init_tdbsam_nua },
31         { "ldapsam", pdb_init_ldapsam },
32         { "ldapsam_nua", pdb_init_ldapsam_nua },
33         { "plugin", pdb_init_plugin },
34         { NULL, NULL}
35 };
36
37 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
38 {
39         if ((!context) || (!context->pdb_methods) || (!context->pdb_methods->setsampwent)) {
40                 DEBUG(0, ("invalid pdb_context specified!\n"));
41                 return False;
42         }
43
44         context->pwent_methods = context->pdb_methods;
45         
46         while(!(context->pwent_methods->setsampwent(context->pwent_methods, update))){
47                 context->pwent_methods = context->pwent_methods->next;
48                 if(context->pwent_methods == NULL)return False;
49         }
50         return True;
51 }
52
53 static void context_endsampwent(struct pdb_context *context)
54 {
55         if ((!context)){
56                 DEBUG(0, ("invalid pdb_context specified!\n"));
57                 return;
58         }
59
60         if(context->pwent_methods && context->pwent_methods->endsampwent)
61                 context->pwent_methods->endsampwent(context->pwent_methods);
62
63         /* So we won't get strange data when calling getsampwent now */
64         context->pwent_methods = NULL;
65 }
66
67 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
68 {
69         if ((!context) || (!context->pwent_methods)) {
70                 DEBUG(0, ("invalid pdb_context specified!\n"));
71                 return False;
72         }
73         /* Loop until we find something useful */
74         while((!context->pwent_methods->getsampwent) || 
75                   context->pwent_methods->getsampwent(context->pwent_methods, user) == False){
76
77                 if(context->pwent_methods->endsampwent)
78                         context->pwent_methods->endsampwent(context->pwent_methods);
79
80                 context->pwent_methods = context->pwent_methods->next;
81
82                 /* All methods are checked now. There are no more entries */
83                 if(context->pwent_methods == NULL)return False;
84         
85                 if(!context->pwent_methods->setsampwent){
86                         DEBUG(0, ("invalid context->pwent_methods->setsampwent\n"));
87                         return False;
88                 }
89
90                 context->pwent_methods->setsampwent(context->pwent_methods, False);
91         }
92         user->methods = context->pwent_methods;
93         return True;
94 }
95
96 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
97 {
98         struct pdb_methods *curmethods;
99         if ((!context)) {
100                 DEBUG(0, ("invalid pdb_context specified!\n"));
101                 return False;
102         }
103         curmethods = context->pdb_methods;
104         while(curmethods){
105                 if(curmethods->getsampwnam && curmethods->getsampwnam(curmethods, sam_acct, username) == True){
106                         sam_acct->methods = curmethods;
107                         return True;
108                 }
109                 curmethods = curmethods->next;
110         }
111
112         return False;
113 }
114
115 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
116 {
117         struct pdb_methods *curmethods;
118         if ((!context)) {
119                 DEBUG(0, ("invalid pdb_context specified!\n"));
120                 return False;
121         }
122         
123         curmethods = context->pdb_methods;
124
125         while(curmethods){
126                 if(curmethods->getsampwrid && curmethods->getsampwrid(curmethods, sam_acct, rid) == True){
127                         sam_acct->methods = curmethods;
128                         return True;
129                 }
130                 curmethods = curmethods->next;
131         }
132
133         return False;
134 }
135
136 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
137 {
138         if ((!context) || (!context->pdb_methods) || (!context->pdb_methods->add_sam_account)) {
139                 DEBUG(0, ("invalid pdb_context specified!\n"));
140                 return False;
141         }
142
143         /** @todo  This is where a 're-read on add' should be done */
144         /* We now add a new account to the first database listed. 
145          * Should we? */
146
147         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
148 }
149
150 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
151 {
152         if (!context) {
153                 DEBUG(0, ("invalid pdb_context specified!\n"));
154                 return False;
155         }
156
157         if(!sam_acct || !sam_acct->methods){
158                 DEBUG(0, ("invalid sam_acct specified\n"));
159                 return False;
160         }
161
162         if(!sam_acct->methods->update_sam_account){
163                 DEBUG(0, ("invalid sam_acct->methods\n"));
164                 return False;
165         }
166
167         /** @todo  This is where a 're-read on update' should be done */
168
169         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
170 }
171
172 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
173 {
174         struct pdb_methods *pdb_selected;
175         if (!context) {
176                 DEBUG(0, ("invalid pdb_context specified!\n"));
177                 return False;
178         }
179
180         if(!sam_acct->methods){
181                 pdb_selected = context->pdb_methods;
182                 /* There's no passdb backend specified for this account.
183                  * Try to delete it in every passdb available */
184                 while(pdb_selected){
185                         if(pdb_selected->delete_sam_account && pdb_selected->delete_sam_account(pdb_selected, sam_acct)){
186                                 return True;
187                         }
188                         pdb_selected = pdb_selected->next;
189                 }
190                 return False;
191         }
192
193         if(!sam_acct->methods->delete_sam_account){
194                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
195                 return False;
196         }
197         
198         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
199 }
200
201 static void free_pdb_context(struct pdb_context **context)
202 {
203         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
204
205         while(pdb_selected){
206                 if(pdb_selected->free_private_data)
207                         pdb_selected->free_private_data(pdb_selected->private_data);
208                 pdb_selected = pdb_selected->next;
209         }
210
211         talloc_destroy((*context)->mem_ctx);
212         *context = NULL;
213 }
214
215 /******************************************************************
216   Make a pdb_methods from scratch
217  *******************************************************************/
218
219 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
220 {
221         char *module_name = smb_xstrdup(selected);
222         char *module_location = NULL, *p;
223         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
224         int i;
225
226         p = strchr(module_name, ':');
227
228         if (p) {
229                 *p = 0;
230                 module_location = p+1;
231                 trim_string(module_location, " ", " ");
232         }
233
234         trim_string(module_name, " ", " ");
235
236         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
237         for (i = 0; builtin_pdb_init_functions[i].name; i++)
238         {
239                 if (strequal(builtin_pdb_init_functions[i].name, module_name))
240                 {
241                         DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, i));
242                         if (NT_STATUS_IS_OK(nt_status 
243                                                                 = builtin_pdb_init_functions[i].init(context, methods, module_location))) {
244                                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
245                         } else {
246                                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
247                         }
248                         break;
249                 }
250         }
251
252         if (!*methods) {
253                 DEBUG(0,("failed to select passdb backed!\n"));
254                 return nt_status;
255         }
256         return NT_STATUS_OK;
257 }
258
259 /******************************************************************
260   Make a pdb_context from scratch.
261  *******************************************************************/
262
263 static NTSTATUS make_pdb_context(struct pdb_context **context) 
264 {
265         TALLOC_CTX *mem_ctx;
266
267         mem_ctx = talloc_init_named("pdb_context internal allocation context");
268
269         if (!mem_ctx) {
270                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
271                 return NT_STATUS_NO_MEMORY;
272         }               
273
274         *context = talloc(mem_ctx, sizeof(**context));
275         if (!*context) {
276                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
277                 return NT_STATUS_NO_MEMORY;
278         }
279
280         ZERO_STRUCTP(*context);
281
282         (*context)->mem_ctx = mem_ctx;
283
284         (*context)->pdb_setsampwent = context_setsampwent;
285         (*context)->pdb_endsampwent = context_endsampwent;
286         (*context)->pdb_getsampwent = context_getsampwent;
287         (*context)->pdb_getsampwnam = context_getsampwnam;
288         (*context)->pdb_getsampwrid = context_getsampwrid;
289         (*context)->pdb_add_sam_account = context_add_sam_account;
290         (*context)->pdb_update_sam_account = context_update_sam_account;
291         (*context)->pdb_delete_sam_account = context_delete_sam_account;
292
293         (*context)->pdb_methods = NULL;
294         (*context)->pwent_methods = NULL;
295
296         (*context)->free_fn = free_pdb_context;
297
298         return NT_STATUS_OK;
299 }
300
301
302 /******************************************************************
303   Make a pdb_context, given a text string.
304  *******************************************************************/
305
306 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected) 
307 {
308         /* HINT: Don't store 'selected' becouse its often an lp_ string and will 'go away' */
309         char *conf = smb_xstrdup(selected);
310         char *confcur = conf, *confnext;
311         struct pdb_methods *curmethods, *tmpmethods;
312         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
313
314         if(!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))){
315                 return nt_status;
316         }
317
318         while(confcur){
319                 if(strchr(confcur, ' ')){
320                         confnext = strchr(confcur,' ');
321                         *confnext = '\0';
322                         confnext++;
323                 }else confnext = NULL;
324
325                 /* Try to initialise pdb */
326                 DEBUG(5,("Trying to load: %s\n", confcur));
327                 if(!NT_STATUS_IS_OK(make_pdb_methods_name(&curmethods, *context, confcur))){
328                         DEBUG(5, ("Loading %s failed!\n", confcur));
329                         SAFE_FREE(curmethods);
330                         continue;
331                 }
332                 curmethods->parent = *context;
333                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
334
335                 if(!confnext)break;
336                 confcur = confnext;
337         }
338         SAFE_FREE(conf);
339
340         nt_status = NT_STATUS_OK;
341
342         return nt_status;
343 }
344
345 /******************************************************************
346  Return an already initialised pdb_context, to facilitate backward 
347  compatibility (see functions below).
348 *******************************************************************/
349
350 static struct pdb_context *pdb_get_static_context(BOOL reload) 
351 {
352         static struct pdb_context *pdb_context = NULL;
353
354         if ((pdb_context) && (reload)) {
355                 pdb_context->free_fn(&pdb_context);
356                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
357                         return NULL;
358                 }
359         }
360
361         if (!pdb_context) {
362                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
363                         return NULL;
364                 }
365         }
366
367         return pdb_context;
368 }
369
370 #if !defined(WITH_NISPLUS_SAM)
371
372 /******************************************************************
373  Backward compatibility functions for the original passdb interface
374 *******************************************************************/
375
376 BOOL pdb_setsampwent(BOOL update) 
377 {
378         struct pdb_context *pdb_context = pdb_get_static_context(False);
379
380         if (!pdb_context) {
381                 return False;
382         }
383
384         return pdb_context->pdb_setsampwent(pdb_context, update);
385 }
386
387 void pdb_endsampwent(void) 
388 {
389         struct pdb_context *pdb_context = pdb_get_static_context(False);
390
391         if (!pdb_context) {
392                 return;
393         }
394
395         pdb_context->pdb_endsampwent(pdb_context);
396 }
397
398 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
399 {
400         struct pdb_context *pdb_context = pdb_get_static_context(False);
401
402         if (!pdb_context) {
403                 return False;
404         }
405
406         return pdb_context->pdb_getsampwent(pdb_context, user);
407 }
408
409 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
410 {
411         struct pdb_context *pdb_context = pdb_get_static_context(False);
412
413         if (!pdb_context) {
414                 return False;
415         }
416
417         return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
418 }
419
420 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid) 
421 {
422         struct pdb_context *pdb_context = pdb_get_static_context(False);
423
424         if (!pdb_context) {
425                 return False;
426         }
427
428         return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
429 }
430
431 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
432 {
433         struct pdb_context *pdb_context = pdb_get_static_context(False);
434
435         if (!pdb_context) {
436                 return False;
437         }
438
439         return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
440 }
441
442 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
443 {
444         struct pdb_context *pdb_context = pdb_get_static_context(False);
445
446         if (!pdb_context) {
447                 return False;
448         }
449
450         return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
451 }
452
453 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
454 {
455         struct pdb_context *pdb_context = pdb_get_static_context(False);
456
457         if (!pdb_context) {
458                 return False;
459         }
460
461         return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
462 }
463
464 #endif /* !defined(WITH_NISPLUS_SAM) */
465
466 /***************************************************************
467   Initialize the static context (at smbd startup etc). 
468
469   If uninitialised, context will auto-init on first use.
470  ***************************************************************/
471
472 BOOL initialize_password_db(BOOL reload)
473 {       
474         return (pdb_get_static_context(reload) != NULL);
475 }
476
477
478 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
479 {
480         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
481
482         if (!*methods) {
483                 return NT_STATUS_NO_MEMORY;
484         }
485
486         ZERO_STRUCTP(*methods);
487
488         return NT_STATUS_OK;
489 }