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