This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[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       
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /** List of various built-in passdb modules */
24
25 const struct pdb_init_function_entry builtin_pdb_init_functions[] = {
26         { "smbpasswd", pdb_init_smbpasswd },
27         { "smbpasswd_nua", pdb_init_smbpasswd_nua },
28         { "tdbsam", pdb_init_tdbsam },
29         { "tdbsam_nua", pdb_init_tdbsam_nua },
30         { "ldapsam", pdb_init_ldapsam },
31         { "ldapsam_nua", pdb_init_ldapsam_nua },
32 #if 0
33         { "nisplus", pdb_init_nisplus },        
34         { "unix", pdb_init_unix },
35 #endif
36         { "plugin", pdb_init_plugin },
37         { NULL, NULL}
38 };
39
40 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
41 {
42         if ((!context) || (!context->pdb_selected)) {
43                 DEBUG(0, ("invalid pdb_context specified!\n"));
44                 return False;
45         }
46         
47         return context->pdb_selected->setsampwent(context, update);
48 }
49
50 static void context_endsampwent(struct pdb_context *context)
51 {
52         if ((!context) || (!context->pdb_selected)) {
53                 DEBUG(0, ("invalid pdb_context specified!\n"));
54                 return;
55         }
56         
57         context->pdb_selected->endsampwent(context);
58 }
59
60 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
61 {
62         if ((!context) || (!context->pdb_selected)) {
63                 DEBUG(0, ("invalid pdb_context specified!\n"));
64                 return False;
65         }
66         
67         return context->pdb_selected->getsampwent(context, user);
68 }
69
70 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
71 {
72         if ((!context) || (!context->pdb_selected)) {
73                 DEBUG(0, ("invalid pdb_context specified!\n"));
74                 return False;
75         }
76         
77         return context->pdb_selected->getsampwnam(context, sam_acct, username);
78 }
79
80 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
81 {
82         if ((!context) || (!context->pdb_selected)) {
83                 DEBUG(0, ("invalid pdb_context specified!\n"));
84                 return False;
85         }
86         
87         return context->pdb_selected->getsampwrid(context, sam_acct, rid);
88 }
89
90 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
91 {
92         if ((!context) || (!context->pdb_selected)) {
93                 DEBUG(0, ("invalid pdb_context specified!\n"));
94                 return False;
95         }
96         
97         /** @todo  This is where a 're-read on add' should be done */
98   
99         return context->pdb_selected->add_sam_account(context, sam_acct);
100 }
101
102 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
103 {
104         if ((!context) || (!context->pdb_selected)) {
105                 DEBUG(0, ("invalid pdb_context specified!\n"));
106                 return False;
107         }
108         
109         /** @todo  This is where a 're-read on update' should be done */
110         
111         return context->pdb_selected->update_sam_account(context, sam_acct);
112 }
113
114 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
115 {
116         if ((!context) || (!context->pdb_selected)) {
117                 DEBUG(0, ("invalid pdb_context specified!\n"));
118                 return False;
119         }
120         
121         return context->pdb_selected->delete_sam_account(context, sam_acct);
122 }
123
124 static void free_pdb_context(struct pdb_context **context)
125 {
126         if (((*context)->pdb_selected) && ((*context)->pdb_selected->free_private_data)) {
127                 (*context)->pdb_selected->free_private_data((*context)->pdb_selected->private_data);
128         }
129
130         talloc_destroy((*context)->mem_ctx);
131         *context = NULL;
132 }
133
134 /******************************************************************
135  Make a pdb_context from scratch.
136 *******************************************************************/
137
138 static NTSTATUS make_pdb_context(struct pdb_context **context) 
139 {
140         TALLOC_CTX *mem_ctx;
141         
142         mem_ctx = talloc_init_named("pdb_context internal allocation context");
143
144         if (!mem_ctx) {
145                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
146                 return NT_STATUS_NO_MEMORY;
147         }               
148
149         *context = talloc(mem_ctx, sizeof(**context));
150         if (!*context) {
151                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
152                 return NT_STATUS_NO_MEMORY;
153         }
154
155         ZERO_STRUCTP(*context);
156
157         (*context)->mem_ctx = mem_ctx;
158
159         (*context)->pdb_setsampwent = context_setsampwent;
160         (*context)->pdb_endsampwent = context_endsampwent;
161         (*context)->pdb_getsampwent = context_getsampwent;
162         (*context)->pdb_getsampwnam = context_getsampwnam;
163         (*context)->pdb_getsampwrid = context_getsampwrid;
164         (*context)->pdb_add_sam_account = context_add_sam_account;
165         (*context)->pdb_update_sam_account = context_update_sam_account;
166         (*context)->pdb_delete_sam_account = context_delete_sam_account;
167
168         (*context)->free_fn = free_pdb_context;
169         
170         return NT_STATUS_OK;
171 }
172
173
174 /******************************************************************
175  Make a pdb_context, given a text string.
176 *******************************************************************/
177
178 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected) 
179 {
180         /* HINT: Don't store 'selected' becouse its often an lp_ string and
181            will 'go away' */
182         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
183         int i;
184         char *module_name = smb_xstrdup(selected);
185         char *module_location = NULL;
186         char *p;
187
188         p = strchr(module_name, ':');
189         
190         if (p) {
191                 *p = 0;
192         
193                 module_location = p+1;
194                 
195                 trim_string(module_location, " ", " ");
196         }
197
198         trim_string(module_name, " ", " ");
199
200         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context)))
201                 goto done;
202         
203         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", 
204                  selected, module_name));
205
206         for (i = 0; builtin_pdb_init_functions[i].name; i++) {
207                 if (strequal(builtin_pdb_init_functions[i].name, 
208                              module_name)) {
209
210                         DEBUG(5,("Found pdb backend %s (at pos %d)\n", 
211                                  module_name, i));
212
213                         if (NT_STATUS_IS_OK(nt_status = builtin_pdb_init_functions[i].init(*context, &(*context)->pdb_selected, module_location))) {
214                                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
215                         } else {
216                                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
217                                 (*context)->pdb_selected = NULL;
218                         }
219                         break;
220                 }
221         }
222     
223         if (!(*context)->pdb_selected) {
224                 DEBUG(0,("failed to select passdb backed!\n"));
225                 talloc_destroy((*context)->mem_ctx);
226                 *context = NULL;
227                 goto done;
228         }
229
230         nt_status = NT_STATUS_OK;
231
232  done:
233         SAFE_FREE(module_name);
234
235         return nt_status;
236 }
237
238
239 /******************************************************************
240  Return an already initialised pdb_context, to facilitate backward 
241  compatibility (see functions below).
242 *******************************************************************/
243
244 static struct pdb_context *pdb_get_static_context(BOOL reload) 
245 {
246         static struct pdb_context *pdb_context = NULL;
247         
248         if ((pdb_context) && (reload)) {
249                 pdb_context->free_fn(&pdb_context);
250                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
251                         return NULL;
252                 }
253         }
254         
255         if (!pdb_context) {
256                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
257                         return NULL;
258                 }
259         }
260         
261         return pdb_context;
262 }
263
264 #if !defined(WITH_NISPLUS_SAM)
265
266 /******************************************************************
267  Backward compatibility functions for the original passdb interface
268 *******************************************************************/
269
270 BOOL pdb_setsampwent(BOOL update) 
271 {
272         struct pdb_context *pdb_context = pdb_get_static_context(False);
273
274         if (!pdb_context) {
275                 return False;
276         }
277
278         return pdb_context->pdb_setsampwent(pdb_context, update);
279 }
280
281 void pdb_endsampwent(void) 
282 {
283         struct pdb_context *pdb_context = pdb_get_static_context(False);
284
285         if (!pdb_context) {
286                 return;
287         }
288
289         pdb_context->pdb_endsampwent(pdb_context);
290 }
291
292 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
293 {
294         struct pdb_context *pdb_context = pdb_get_static_context(False);
295
296         if (!pdb_context) {
297                 return False;
298         }
299
300         return pdb_context->pdb_getsampwent(pdb_context, user);
301 }
302
303 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
304 {
305         struct pdb_context *pdb_context = pdb_get_static_context(False);
306
307         if (!pdb_context) {
308                 return False;
309         }
310
311         return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
312 }
313
314 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid) 
315 {
316         struct pdb_context *pdb_context = pdb_get_static_context(False);
317
318         if (!pdb_context) {
319                 return False;
320         }
321
322         return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
323 }
324
325 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
326 {
327         struct pdb_context *pdb_context = pdb_get_static_context(False);
328
329         if (!pdb_context) {
330                 return False;
331         }
332
333         return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
334 }
335
336 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
337 {
338         struct pdb_context *pdb_context = pdb_get_static_context(False);
339
340         if (!pdb_context) {
341                 return False;
342         }
343
344         return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
345 }
346
347 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
348 {
349         struct pdb_context *pdb_context = pdb_get_static_context(False);
350         
351         if (!pdb_context) {
352                 return False;
353         }
354         
355         return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
356 }
357
358 #endif /* !defined(WITH_NISPLUS_SAM) */
359
360 /***************************************************************
361  Initialize the static context (at smbd startup etc). 
362
363  If uninitialised, context will auto-init on first use.
364 ***************************************************************/
365
366 BOOL initialize_password_db(BOOL reload)
367 {       
368         return (pdb_get_static_context(reload) != NULL);
369 }
370
371
372 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
373 {
374         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
375
376         if (!*methods) {
377                 return NT_STATUS_NO_MEMORY;
378         }
379
380         ZERO_STRUCTP(*methods);
381
382         return NT_STATUS_OK;
383 }
384
385
386
387
388
389
390
391