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 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 },
31 { "ldap", pdb_init_ldap },
32 { "nisplus", pdb_init_nisplus },
33 { "unix", pdb_init_unix },
38 static BOOL context_setsampwent(struct pdb_context *context, BOOL update)
40 if ((!context) || (!context->pdb_selected)) {
41 DEBUG(0, ("invalid pdb_context specified!\n"));
45 return context->pdb_selected->setsampwent(context, update);
48 static void context_endsampwent(struct pdb_context *context)
50 if ((!context) || (!context->pdb_selected)) {
51 DEBUG(0, ("invalid pdb_context specified!\n"));
55 context->pdb_selected->endsampwent(context);
58 static BOOL context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
60 if ((!context) || (!context->pdb_selected)) {
61 DEBUG(0, ("invalid pdb_context specified!\n"));
65 return context->pdb_selected->getsampwent(context, user);
68 static BOOL context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
70 if ((!context) || (!context->pdb_selected)) {
71 DEBUG(0, ("invalid pdb_context specified!\n"));
75 return context->pdb_selected->getsampwnam(context, sam_acct, username);
78 static BOOL context_getsampwrid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, uint32 rid)
80 if ((!context) || (!context->pdb_selected)) {
81 DEBUG(0, ("invalid pdb_context specified!\n"));
85 return context->pdb_selected->getsampwrid(context, sam_acct, rid);
88 static BOOL context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
90 if ((!context) || (!context->pdb_selected)) {
91 DEBUG(0, ("invalid pdb_context specified!\n"));
95 /** @todo This is where a 're-read on add' should be done */
97 return context->pdb_selected->add_sam_account(context, sam_acct);
100 static BOOL context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
102 if ((!context) || (!context->pdb_selected)) {
103 DEBUG(0, ("invalid pdb_context specified!\n"));
107 /** @todo This is where a 're-read on update' should be done */
109 return context->pdb_selected->update_sam_account(context, sam_acct);
112 static BOOL context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
114 if ((!context) || (!context->pdb_selected)) {
115 DEBUG(0, ("invalid pdb_context specified!\n"));
119 return context->pdb_selected->delete_sam_account(context, sam_acct);
122 static void free_pdb_context(struct pdb_context **context)
124 if (((*context)->pdb_selected) && ((*context)->pdb_selected->free_private_data)) {
125 (*context)->pdb_selected->free_private_data((*context)->pdb_selected->private_data);
128 talloc_destroy((*context)->mem_ctx);
132 /******************************************************************
133 Make a pdb_context from scratch.
134 *******************************************************************/
136 static NTSTATUS make_pdb_context(struct pdb_context **context)
140 mem_ctx = talloc_init_named("pdb_context internal allocation context");
143 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
144 return NT_STATUS_NO_MEMORY;
147 *context = talloc(mem_ctx, sizeof(**context));
149 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
150 return NT_STATUS_NO_MEMORY;
153 ZERO_STRUCTP(*context);
155 (*context)->mem_ctx = mem_ctx;
157 (*context)->pdb_setsampwent = context_setsampwent;
158 (*context)->pdb_endsampwent = context_endsampwent;
159 (*context)->pdb_getsampwent = context_getsampwent;
160 (*context)->pdb_getsampwnam = context_getsampwnam;
161 (*context)->pdb_getsampwrid = context_getsampwrid;
162 (*context)->pdb_add_sam_account = context_add_sam_account;
163 (*context)->pdb_update_sam_account = context_update_sam_account;
164 (*context)->pdb_delete_sam_account = context_delete_sam_account;
166 (*context)->free_fn = free_pdb_context;
172 /******************************************************************
173 Make a pdb_context, given a text string.
174 *******************************************************************/
176 NTSTATUS make_pdb_context_name(struct pdb_context **context, const char *selected)
178 /* HINT: Don't store 'selected' becouse its often an lp_ string and will 'go away' */
179 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
181 char *module_name = smb_xstrdup(selected);
182 char *module_location = NULL;
185 p = strchr(module_name, ':');
190 module_location = p+1;
192 trim_string(module_location, " ", " ");
195 trim_string(module_name, " ", " ");
197 if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
201 DEBUG(5,("Attempting to find an passdb backend to match %s\n", selected));
202 for (i = 0; builtin_pdb_init_functions[i].name; i++)
204 if (strequal(builtin_pdb_init_functions[i].name, module_name))
206 DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, i));
207 if (NT_STATUS_IS_OK(nt_status
208 = builtin_pdb_init_functions[i].init(*context, &(*context)->pdb_selected, module_location))) {
209 DEBUG(5,("pdb backend %s has a valid init\n", selected));
211 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, get_nt_error_msg(nt_status)));
212 (*context)->pdb_selected = NULL;
218 if (!(*context)->pdb_selected) {
219 DEBUG(0,("failed to select passdb backed!\n"));
220 talloc_destroy((*context)->mem_ctx);
229 /******************************************************************
230 Return an already initilised pdb_context, to facilitate backward
231 compatiablity (see functions below).
232 *******************************************************************/
234 static struct pdb_context *pdb_get_static_context(BOOL reload)
236 static struct pdb_context *pdb_context = NULL;
238 if ((pdb_context) && (reload)) {
239 pdb_context->free_fn(&pdb_context);
240 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
246 if (!NT_STATUS_IS_OK(make_pdb_context_name(&pdb_context, lp_passdb_backend()))) {
254 #if !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM)
256 /******************************************************************
257 Backward compatability functions for the original passdb interface
258 *******************************************************************/
260 BOOL pdb_setsampwent(BOOL update)
262 struct pdb_context *pdb_context = pdb_get_static_context(False);
268 return pdb_context->pdb_setsampwent(pdb_context, update);
271 void pdb_endsampwent(void)
273 struct pdb_context *pdb_context = pdb_get_static_context(False);
279 pdb_context->pdb_endsampwent(pdb_context);
282 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
284 struct pdb_context *pdb_context = pdb_get_static_context(False);
290 return pdb_context->pdb_getsampwent(pdb_context, user);
293 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
295 struct pdb_context *pdb_context = pdb_get_static_context(False);
301 return pdb_context->pdb_getsampwnam(pdb_context, sam_acct, username);
304 BOOL pdb_getsampwrid(SAM_ACCOUNT *sam_acct, uint32 rid)
306 struct pdb_context *pdb_context = pdb_get_static_context(False);
312 return pdb_context->pdb_getsampwrid(pdb_context, sam_acct, rid);
315 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
317 struct pdb_context *pdb_context = pdb_get_static_context(False);
323 return pdb_context->pdb_add_sam_account(pdb_context, sam_acct);
326 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
328 struct pdb_context *pdb_context = pdb_get_static_context(False);
334 return pdb_context->pdb_update_sam_account(pdb_context, sam_acct);
337 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
339 struct pdb_context *pdb_context = pdb_get_static_context(False);
345 return pdb_context->pdb_delete_sam_account(pdb_context, sam_acct);
348 #endif /* !defined(WITH_LDAP_SAM) && !defined(WITH_NISPLUS_SAM) */
350 /***************************************************************
351 Initialize the static context (at smbd startup etc).
353 If uninitialised, context will auto-init on first use.
354 ***************************************************************/
356 BOOL initialize_password_db(BOOL reload)
358 return (pdb_get_static_context(reload) != NULL);
362 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
364 *methods = talloc(mem_ctx, sizeof(struct pdb_methods));
367 return NT_STATUS_NO_MEMORY;
370 ZERO_STRUCTP(*methods);