Renamed get_nt_error_msg() to nt_errstr().
[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       
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 will 'go away' */
181         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
182         int i;
183         char *module_name = smb_xstrdup(selected);
184         char *module_location = NULL;
185         char *p;
186
187         p = strchr(module_name, ':');
188         
189         if (p) {
190                 *p = 0;
191         
192                 module_location = p+1;
193                 
194                 trim_string(module_location, " ", " ");
195         }
196
197         trim_string(module_name, " ", " ");
198
199         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
200                 return nt_status;
201         }
202         
203         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
204         for (i = 0; builtin_pdb_init_functions[i].name; i++)
205         {
206                 if (strequal(builtin_pdb_init_functions[i].name, module_name))
207                 {
208                         DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, i));
209                         if (NT_STATUS_IS_OK(nt_status 
210                                             = builtin_pdb_init_functions[i].init(*context, &(*context)->pdb_selected, module_location))) {
211                                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
212                         } else {
213                                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
214                                 (*context)->pdb_selected = NULL;
215                         }
216                         break;
217                 }
218         }
219     
220         if (!(*context)->pdb_selected) {
221                 DEBUG(0,("failed to select passdb backed!\n"));
222                 talloc_destroy((*context)->mem_ctx);
223                 *context = NULL;
224                 return nt_status;
225         }
226
227         return NT_STATUS_OK;
228 }
229
230
231 /******************************************************************
232  Return an already initilised pdb_context, to facilitate backward 
233  compatiablity (see functions below).
234 *******************************************************************/
235
236 static struct pdb_context *pdb_get_static_context(BOOL reload) 
237 {
238         static struct pdb_context *pdb_context = NULL;
239         
240         if ((pdb_context) && (reload)) {
241                 pdb_context->free_fn(&pdb_context);
242                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
243                         return NULL;
244                 }
245         }
246         
247         if (!pdb_context) {
248                 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
249                         return NULL;
250                 }
251         }
252         
253         return pdb_context;
254 }
255
256 #if !defined(WITH_NISPLUS_SAM)
257
258 /******************************************************************
259  Backward compatability functions for the original passdb interface
260 *******************************************************************/
261
262 BOOL pdb_setsampwent(BOOL update) 
263 {
264         struct pdb_context *pdb_context = pdb_get_static_context(False);
265
266         if (!pdb_context) {
267                 return False;
268         }
269
270         return pdb_context->pdb_setsampwent(pdb_context, update);
271 }
272
273 void pdb_endsampwent(void) 
274 {
275         struct pdb_context *pdb_context = pdb_get_static_context(False);
276
277         if (!pdb_context) {
278                 return;
279         }
280
281         pdb_context->pdb_endsampwent(pdb_context);
282 }
283
284 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
285 {
286         struct pdb_context *pdb_context = pdb_get_static_context(False);
287
288         if (!pdb_context) {
289                 return False;
290         }
291
292         return pdb_context->pdb_getsampwent(pdb_context, user);
293 }
294
295 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
296 {
297         struct pdb_context *pdb_context = pdb_get_static_context(False);
298
299         if (!pdb_context) {
300                 return False;
301         }
302
303         return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
304 }
305
306 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid) 
307 {
308         struct pdb_context *pdb_context = pdb_get_static_context(False);
309
310         if (!pdb_context) {
311                 return False;
312         }
313
314         return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
315 }
316
317 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
318 {
319         struct pdb_context *pdb_context = pdb_get_static_context(False);
320
321         if (!pdb_context) {
322                 return False;
323         }
324
325         return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
326 }
327
328 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
329 {
330         struct pdb_context *pdb_context = pdb_get_static_context(False);
331
332         if (!pdb_context) {
333                 return False;
334         }
335
336         return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
337 }
338
339 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
340 {
341         struct pdb_context *pdb_context = pdb_get_static_context(False);
342         
343         if (!pdb_context) {
344                 return False;
345         }
346         
347         return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
348 }
349
350 #endif /* !defined(WITH_NISPLUS_SAM) */
351
352 /***************************************************************
353  Initialize the static context (at smbd startup etc). 
354
355  If uninitialised, context will auto-init on first use.
356 ***************************************************************/
357
358 BOOL initialize_password_db(BOOL reload)
359 {       
360         return (pdb_get_static_context(reload) != NULL);
361 }
362
363
364 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
365 {
366         *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
367
368         if (!*methods) {
369                 return NT_STATUS_NO_MEMORY;
370         }
371
372         ZERO_STRUCTP(*methods);
373
374         return NT_STATUS_OK;
375 }
376
377
378
379
380
381
382
383