2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
23 /** List of various built-in passdb modules */
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 },
33 { "nisplus", pdb_init_nisplus },
34 { "unix", pdb_init_unix },
36 { "plugin", pdb_init_plugin },
40 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
42 if ((!context) || (!context->pdb_selected)) {
43 DEBUG(0, ("invalid pdb_context specified!\n"));
47 return context->pdb_selected->setsampwent(context, update);
50 static void context_endsampwent(struct pdb_context *context)
52 if ((!context) || (!context->pdb_selected)) {
53 DEBUG(0, ("invalid pdb_context specified!\n"));
57 context->pdb_selected->endsampwent(context);
60 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
62 if ((!context) || (!context->pdb_selected)) {
63 DEBUG(0, ("invalid pdb_context specified!\n"));
67 return context->pdb_selected->getsampwent(context, user);
70 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
72 if ((!context) || (!context->pdb_selected)) {
73 DEBUG(0, ("invalid pdb_context specified!\n"));
77 return context->pdb_selected->getsampwnam(context, sam_acct, username);
80 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
82 if ((!context) || (!context->pdb_selected)) {
83 DEBUG(0, ("invalid pdb_context specified!\n"));
87 return context->pdb_selected->getsampwrid(context, sam_acct, rid);
90 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
92 if ((!context) || (!context->pdb_selected)) {
93 DEBUG(0, ("invalid pdb_context specified!\n"));
97 /** @todo This is where a 're-read on add' should be done */
99 return context->pdb_selected->add_sam_account(context, sam_acct);
102 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
104 if ((!context) || (!context->pdb_selected)) {
105 DEBUG(0, ("invalid pdb_context specified!\n"));
109 /** @todo This is where a 're-read on update' should be done */
111 return context->pdb_selected->update_sam_account(context, sam_acct);
114 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
116 if ((!context) || (!context->pdb_selected)) {
117 DEBUG(0, ("invalid pdb_context specified!\n"));
121 return context->pdb_selected->delete_sam_account(context, sam_acct);
124 static void free_pdb_context(struct pdb_context **context)
126 if (((*context)->pdb_selected) && ((*context)->pdb_selected->free_private_data)) {
127 (*context)->pdb_selected->free_private_data((*context)->pdb_selected->private_data);
130 talloc_destroy((*context)->mem_ctx);
134 /******************************************************************
135 Make a pdb_context from scratch.
136 *******************************************************************/
138 static NTSTATUS make_pdb_context(struct pdb_context **context)
142 mem_ctx = talloc_init_named("pdb_context internal allocation context");
145 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
146 return NT_STATUS_NO_MEMORY;
149 *context = talloc(mem_ctx, sizeof(**context));
151 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
152 return NT_STATUS_NO_MEMORY;
155 ZERO_STRUCTP(*context);
157 (*context)->mem_ctx = mem_ctx;
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;
168 (*context)->free_fn = free_pdb_context;
174 /******************************************************************
175 Make a pdb_context, given a text string.
176 *******************************************************************/
178 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected)
180 /* HINT: Don't store 'selected' becouse its often an lp_ string and will 'go away' */
181 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
183 char *module_name = smb_xstrdup(selected);
184 char *module_location = NULL;
187 p = strchr(module_name, ':');
192 module_location = p+1;
194 trim_string(module_location, " ", " ");
197 trim_string(module_name, " ", " ");
199 if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
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++)
206 if (strequal(builtin_pdb_init_functions[i].name, module_name))
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));
213 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
214 (*context)->pdb_selected = NULL;
220 if (!(*context)->pdb_selected) {
221 DEBUG(0,("failed to select passdb backed!\n"));
222 talloc_destroy((*context)->mem_ctx);
231 /******************************************************************
232 Return an already initilised pdb_context, to facilitate backward
233 compatiablity (see functions below).
234 *******************************************************************/
236 static struct pdb_context *pdb_get_static_context(BOOL reload)
238 static struct pdb_context *pdb_context = NULL;
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()))) {
248 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
256 #if !defined(WITH_NISPLUS_SAM)
258 /******************************************************************
259 Backward compatability functions for the original passdb interface
260 *******************************************************************/
262 BOOL pdb_setsampwent(BOOL update)
264 struct pdb_context *pdb_context = pdb_get_static_context(False);
270 return pdb_context->pdb_setsampwent(pdb_context, update);
273 void pdb_endsampwent(void)
275 struct pdb_context *pdb_context = pdb_get_static_context(False);
281 pdb_context->pdb_endsampwent(pdb_context);
284 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
286 struct pdb_context *pdb_context = pdb_get_static_context(False);
292 return pdb_context->pdb_getsampwent(pdb_context, user);
295 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
297 struct pdb_context *pdb_context = pdb_get_static_context(False);
303 return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
306 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid)
308 struct pdb_context *pdb_context = pdb_get_static_context(False);
314 return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
317 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
319 struct pdb_context *pdb_context = pdb_get_static_context(False);
325 return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
328 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
330 struct pdb_context *pdb_context = pdb_get_static_context(False);
336 return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
339 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
341 struct pdb_context *pdb_context = pdb_get_static_context(False);
347 return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
350 #endif /* !defined(WITH_NISPLUS_SAM) */
352 /***************************************************************
353 Initialize the static context (at smbd startup etc).
355 If uninitialised, context will auto-init on first use.
356 ***************************************************************/
358 BOOL initialize_password_db(BOOL reload)
360 return (pdb_get_static_context(reload) != NULL);
364 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
366 *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
369 return NT_STATUS_NO_MEMORY;
372 ZERO_STRUCTP(*methods);