5264c0d060acb1c097b4b47626711d29996a828a
[jpeach/samba.git] / source / passdb / pdb_interface.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Password and authentication handling
5    Copyright (C) Andrew Bartlett                    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 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 #if 0
32         { "ldap", pdb_init_ldap },
33         { "nisplus", pdb_init_nisplus },        
34         { "unix", pdb_init_unix },
35 #endif
36         { NULL, NULL}
37 };
38
39 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
40 {
41         if ((!context) || (!context->pdb_selected)) {
42                 DEBUG(0, ("invalid pdb_context specified!\n"));
43                 return False;
44         }
45         
46         return context->pdb_selected->setsampwent(context, update);
47 }
48
49 static void context_endsampwent(struct pdb_context *context)
50 {
51         if ((!context) || (!context->pdb_selected)) {
52                 DEBUG(0, ("invalid pdb_context specified!\n"));
53                 return;
54         }
55         
56         context->pdb_selected->endsampwent(context);
57 }
58
59 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
60 {
61         if ((!context) || (!context->pdb_selected)) {
62                 DEBUG(0, ("invalid pdb_context specified!\n"));
63                 return False;
64         }
65         
66         return context->pdb_selected->getsampwent(context, user);
67 }
68
69 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
70 {
71         if ((!context) || (!context->pdb_selected)) {
72                 DEBUG(0, ("invalid pdb_context specified!\n"));
73                 return False;
74         }
75         
76         return context->pdb_selected->getsampwnam(context, sam_acct, username);
77 }
78
79 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
80 {
81         if ((!context) || (!context->pdb_selected)) {
82                 DEBUG(0, ("invalid pdb_context specified!\n"));
83                 return False;
84         }
85         
86         return context->pdb_selected->getsampwrid(context, sam_acct, rid);
87 }
88
89 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
90 {
91         if ((!context) || (!context->pdb_selected)) {
92                 DEBUG(0, ("invalid pdb_context specified!\n"));
93                 return False;
94         }
95         
96         /** @todo  This is where a 're-read on add' should be done */
97   
98         return context->pdb_selected->add_sam_account(context, sam_acct);
99 }
100
101 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
102 {
103         if ((!context) || (!context->pdb_selected)) {
104                 DEBUG(0, ("invalid pdb_context specified!\n"));
105                 return False;
106         }
107         
108         /** @todo  This is where a 're-read on update' should be done */
109         
110         return context->pdb_selected->update_sam_account(context, sam_acct);
111 }
112
113 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
114 {
115         if ((!context) || (!context->pdb_selected)) {
116                 DEBUG(0, ("invalid pdb_context specified!\n"));
117                 return False;
118         }
119         
120         return context->pdb_selected->delete_sam_account(context, sam_acct);
121 }
122
123 static void free_pdb_context(struct pdb_context **context) 
124 {
125         if (((*context)->pdb_selected) && ((*context)->pdb_selected->free_private_data)) {
126                 (*context)->pdb_selected->free_private_data((*context)->pdb_selected->private_data);
127         }
128
129         talloc_destroy((*context)->mem_ctx);
130         *context = NULL;
131 }
132
133 /******************************************************************
134  Make a pdb_context from scratch.
135 *******************************************************************/
136
137 static NTSTATUS make_pdb_context(struct pdb_context **context) 
138 {
139         TALLOC_CTX *mem_ctx;
140         
141         mem_ctx = talloc_init_named("pdb_context internal allocation context");
142
143         if (!mem_ctx) {
144                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
145                 return NT_STATUS_NO_MEMORY;
146         }               
147
148         *context = talloc(mem_ctx, sizeof(**context));
149         if (!*context) {
150                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         ZERO_STRUCTP(*context);
155
156         (*context)->mem_ctx = mem_ctx;
157
158         (*context)->pdb_setsampwent = context_setsampwent;
159         (*context)->pdb_endsampwent = context_endsampwent;
160         (*context)->pdb_getsampwent = context_getsampwent;
161         (*context)->pdb_getsampwnam = context_getsampwnam;
162         (*context)->pdb_getsampwrid = context_getsampwrid;
163         (*context)->pdb_add_sam_account = context_add_sam_account;
164         (*context)->pdb_update_sam_account = context_update_sam_account;
165         (*context)->pdb_delete_sam_account = context_delete_sam_account;
166
167         (*context)->free_fn = free_pdb_context;
168         
169         return NT_STATUS_OK;
170 }
171
172
173 /******************************************************************
174  Make a pdb_context, given a text string.
175 *******************************************************************/
176
177 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected) 
178 {
179         /* HINT: Don't store 'selected' becouse its often an lp_ string and will 'go away' */
180         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
181         int i;
182         char *module_name = smb_xstrdup(selected);
183         char *module_location = NULL;
184         char *p;
185
186         p = strchr(module_name, ':');
187         
188         if (p) {
189                 *p = 0;
190         
191                 module_location = p+1;
192                 
193                 trim_string(module_location, " ", " ");
194         }
195
196         trim_string(module_name, " ", " ");
197
198         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
199                 return nt_status;
200         }
201         
202         DEBUG(5,("Attempting to find an passdb backend to match %s\n", selected));
203         for (i = 0; builtin_pdb_init_functions[i].name; i++)
204         {
205                 if (strequal(builtin_pdb_init_functions[i].name, module_name))
206                 {
207                         DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, i));
208                         if (NT_STATUS_IS_OK(nt_status 
209                                             = builtin_pdb_init_functions[i].init(*context, &(*context)->pdb_selected, module_location))) {
210                                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
211                         } else {
212                                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, get_nt_error_msg(nt_status)));
213                                 (*context)->pdb_selected = NULL;
214                         }
215                         break;
216                 }
217         }
218         
219         if (!(*context)->pdb_selected) {
220                 DEBUG(0,("failed to select passdb backed!\n"));
221                 talloc_destroy((*context)->mem_ctx);
222                 *context = NULL;
223                 return nt_status;
224         }
225
226         return NT_STATUS_OK;
227 }
228
229
230 /******************************************************************
231  Return an already initilised pdb_context, to facilitate backward 
232  compatiablity (see functions below).
233 *******************************************************************/
234
235 static struct pdb_context *pdb_get_static_context(BOOL reload) 
236 {
237         static struct pdb_context *pdb_context = NULL;
238         
239         if ((pdb_context) && (reload)) {
240                 pdb_context->free_fn(&pdb_context);
241                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
242                         return NULL;
243                 }
244         }
245         
246         if (!pdb_context) {
247                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
248                         return NULL;
249                 }
250         }
251         
252         return pdb_context;
253 }
254
255 #if !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM)
256
257 /******************************************************************
258  Backward compatability functions for the original passdb interface
259 *******************************************************************/
260
261 BOOL pdb_setsampwent(BOOL update) 
262 {
263         struct pdb_context *pdb_context = pdb_get_static_context(False);
264
265         if (!pdb_context) {
266                 return False;
267         }
268
269         return pdb_context->pdb_setsampwent(pdb_context, update);
270 }
271
272 void pdb_endsampwent(void) 
273 {
274         struct pdb_context *pdb_context = pdb_get_static_context(False);
275
276         if (!pdb_context) {
277                 return;
278         }
279
280         pdb_context->pdb_endsampwent(pdb_context);
281 }
282
283 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
284 {
285         struct pdb_context *pdb_context = pdb_get_static_context(False);
286
287         if (!pdb_context) {
288                 return False;
289         }
290
291         return pdb_context->pdb_getsampwent(pdb_context, user);
292 }
293
294 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
295 {
296         struct pdb_context *pdb_context = pdb_get_static_context(False);
297
298         if (!pdb_context) {
299                 return False;
300         }
301
302         return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
303 }
304
305 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid) 
306 {
307         struct pdb_context *pdb_context = pdb_get_static_context(False);
308
309         if (!pdb_context) {
310                 return False;
311         }
312
313         return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
314 }
315
316 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
317 {
318         struct pdb_context *pdb_context = pdb_get_static_context(False);
319
320         if (!pdb_context) {
321                 return False;
322         }
323
324         return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
325 }
326
327 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
328 {
329         struct pdb_context *pdb_context = pdb_get_static_context(False);
330
331         if (!pdb_context) {
332                 return False;
333         }
334
335         return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
336 }
337
338 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
339 {
340         struct pdb_context *pdb_context = pdb_get_static_context(False);
341         
342         if (!pdb_context) {
343                 return False;
344         }
345         
346         return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
347 }
348
349 #endif /* !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM) */
350
351 /***************************************************************
352  Initialize the static context (at smbd startup etc). 
353
354  If uninitialised, context will auto-init on first use.
355 ***************************************************************/
356
357 BOOL initialize_password_db(BOOL reload)
358 {       
359         return (pdb_get_static_context(reload) != NULL);
360 }
361
362
363 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
364 {
365         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
366
367         if (!*methods) {
368                 return NT_STATUS_NO_MEMORY;
369         }
370
371         ZERO_STRUCTP(*methods);
372
373         return NT_STATUS_OK;
374 }
375
376
377
378
379
380
381
382