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