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
182 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
184 char *module_name = smb_xstrdup(selected);
185 char *module_location = NULL;
188 p = strchr(module_name, ':');
193 module_location = p+1;
195 trim_string(module_location, " ", " ");
198 trim_string(module_name, " ", " ");
200 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",
204 selected, module_name));
206 for (i = 0; builtin_pdb_init_functions[i].name; i++) {
207 if (strequal(builtin_pdb_init_functions[i].name,
210 DEBUG(5,("Found pdb backend %s (at pos %d)\n",
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));
216 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
217 (*context)->pdb_selected = NULL;
223 if (!(*context)->pdb_selected) {
224 DEBUG(0,("failed to select passdb backed!\n"));
225 talloc_destroy((*context)->mem_ctx);
230 nt_status = NT_STATUS_OK;
233 SAFE_FREE(module_name);
239 /******************************************************************
240 Return an already initialised pdb_context, to facilitate backward
241 compatibility (see functions below).
242 *******************************************************************/
244 static struct pdb_context *pdb_get_static_context(BOOL reload)
246 static struct pdb_context *pdb_context = NULL;
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()))) {
256 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
264 #if !defined(WITH_NISPLUS_SAM)
266 /******************************************************************
267 Backward compatibility functions for the original passdb interface
268 *******************************************************************/
270 BOOL pdb_setsampwent(BOOL update)
272 struct pdb_context *pdb_context = pdb_get_static_context(False);
278 return pdb_context->pdb_setsampwent(pdb_context, update);
281 void pdb_endsampwent(void)
283 struct pdb_context *pdb_context = pdb_get_static_context(False);
289 pdb_context->pdb_endsampwent(pdb_context);
292 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
294 struct pdb_context *pdb_context = pdb_get_static_context(False);
300 return pdb_context->pdb_getsampwent(pdb_context, user);
303 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
305 struct pdb_context *pdb_context = pdb_get_static_context(False);
311 return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
314 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid)
316 struct pdb_context *pdb_context = pdb_get_static_context(False);
322 return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
325 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
327 struct pdb_context *pdb_context = pdb_get_static_context(False);
333 return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
336 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
338 struct pdb_context *pdb_context = pdb_get_static_context(False);
344 return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
347 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
349 struct pdb_context *pdb_context = pdb_get_static_context(False);
355 return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
358 #endif /* !defined(WITH_NISPLUS_SAM) */
360 /***************************************************************
361 Initialize the static context (at smbd startup etc).
363 If uninitialised, context will auto-init on first use.
364 ***************************************************************/
366 BOOL initialize_password_db(BOOL reload)
368 return (pdb_get_static_context(reload) != NULL);
372 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
374 *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
377 return NT_STATUS_NO_MEMORY;
380 ZERO_STRUCTP(*methods);