2 Unix SMB/Netbios implementation.
4 Password and authentication handling
5 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
24 /** List of various built-in passdb modules */
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 },
32 { "ldap", pdb_init_ldap },
33 { "nisplus", pdb_init_nisplus },
34 { "unix", pdb_init_unix },
39 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
41 if ((!context) || (!context->pdb_selected)) {
42 DEBUG(0, ("invalid pdb_context specified!\n"));
46 return context->pdb_selected->setsampwent(context, update);
49 static void context_endsampwent(struct pdb_context *context)
51 if ((!context) || (!context->pdb_selected)) {
52 DEBUG(0, ("invalid pdb_context specified!\n"));
56 context->pdb_selected->endsampwent(context);
59 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
61 if ((!context) || (!context->pdb_selected)) {
62 DEBUG(0, ("invalid pdb_context specified!\n"));
66 return context->pdb_selected->getsampwent(context, user);
69 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
71 if ((!context) || (!context->pdb_selected)) {
72 DEBUG(0, ("invalid pdb_context specified!\n"));
76 return context->pdb_selected->getsampwnam(context, sam_acct, username);
79 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
81 if ((!context) || (!context->pdb_selected)) {
82 DEBUG(0, ("invalid pdb_context specified!\n"));
86 return context->pdb_selected->getsampwrid(context, sam_acct, rid);
89 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
91 if ((!context) || (!context->pdb_selected)) {
92 DEBUG(0, ("invalid pdb_context specified!\n"));
96 /** @todo This is where a 're-read on add' should be done */
98 return context->pdb_selected->add_sam_account(context, sam_acct);
101 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
103 if ((!context) || (!context->pdb_selected)) {
104 DEBUG(0, ("invalid pdb_context specified!\n"));
108 /** @todo This is where a 're-read on update' should be done */
110 return context->pdb_selected->update_sam_account(context, sam_acct);
113 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
115 if ((!context) || (!context->pdb_selected)) {
116 DEBUG(0, ("invalid pdb_context specified!\n"));
120 return context->pdb_selected->delete_sam_account(context, sam_acct);
123 static void free_pdb_context(struct pdb_context **context)
125 if (((*context)->pdb_selected) && ((*context)->pdb_selected->free_private_data)) {
126 (*context)->pdb_selected->free_private_data((*context)->pdb_selected->private_data);
129 talloc_destroy((*context)->mem_ctx);
133 /******************************************************************
134 Make a pdb_context from scratch.
135 *******************************************************************/
137 static NTSTATUS make_pdb_context(struct pdb_context **context)
141 mem_ctx = talloc_init_named("pdb_context internal allocation context");
144 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
145 return NT_STATUS_NO_MEMORY;
148 *context = talloc(mem_ctx, sizeof(**context));
150 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
151 return NT_STATUS_NO_MEMORY;
154 ZERO_STRUCTP(*context);
156 (*context)->mem_ctx = mem_ctx;
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;
167 (*context)->free_fn = free_pdb_context;
173 /******************************************************************
174 Make a pdb_context, given a text string.
175 *******************************************************************/
177 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected)
179 /* HINT: Don't store 'selected' becouse its often an lp_ string and will 'go away' */
180 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
182 char *module_name = smb_xstrdup(selected);
183 char *module_location = NULL;
186 p = strchr(module_name, ':');
191 module_location = p+1;
193 trim_string(module_location, " ", " ");
196 trim_string(module_name, " ", " ");
198 if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
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++)
205 if (strequal(builtin_pdb_init_functions[i].name, module_name))
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));
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;
219 if (!(*context)->pdb_selected) {
220 DEBUG(0,("failed to select passdb backed!\n"));
221 talloc_destroy((*context)->mem_ctx);
230 /******************************************************************
231 Return an already initilised pdb_context, to facilitate backward
232 compatiablity (see functions below).
233 *******************************************************************/
235 static struct pdb_context *pdb_get_static_context(BOOL reload)
237 static struct pdb_context *pdb_context = NULL;
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()))) {
247 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
255 #if !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM)
257 /******************************************************************
258 Backward compatability functions for the original passdb interface
259 *******************************************************************/
261 BOOL pdb_setsampwent(BOOL update)
263 struct pdb_context *pdb_context = pdb_get_static_context(False);
269 return pdb_context->pdb_setsampwent(pdb_context, update);
272 void pdb_endsampwent(void)
274 struct pdb_context *pdb_context = pdb_get_static_context(False);
280 pdb_context->pdb_endsampwent(pdb_context);
283 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
285 struct pdb_context *pdb_context = pdb_get_static_context(False);
291 return pdb_context->pdb_getsampwent(pdb_context, user);
294 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
296 struct pdb_context *pdb_context = pdb_get_static_context(False);
302 return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
305 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid)
307 struct pdb_context *pdb_context = pdb_get_static_context(False);
313 return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
316 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
318 struct pdb_context *pdb_context = pdb_get_static_context(False);
324 return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
327 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
329 struct pdb_context *pdb_context = pdb_get_static_context(False);
335 return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
338 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
340 struct pdb_context *pdb_context = pdb_get_static_context(False);
346 return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
349 #endif /* !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM) */
351 /***************************************************************
352 Initialize the static context (at smbd startup etc).
354 If uninitialised, context will auto-init on first use.
355 ***************************************************************/
357 BOOL initialize_password_db(BOOL reload)
359 return (pdb_get_static_context(reload) != NULL);
363 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
365 *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
368 return NT_STATUS_NO_MEMORY;
371 ZERO_STRUCTP(*methods);