2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2001-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.
24 #define DBGC_CLASS DBGC_AUTH
26 /****************************************************************************
27 Try to get a challenge out of the various authentication modules.
28 Returns a const char of length 8 bytes.
29 ****************************************************************************/
31 static const uint8_t *get_ntlm_challenge(struct auth_context *auth_context)
33 DATA_BLOB challenge = data_blob(NULL, 0);
34 const char *challenge_set_by = NULL;
35 struct auth_methods *auth_method;
38 if (auth_context->challenge.length) {
39 DEBUG(5, ("get_ntlm_challenge (auth subsystem): returning previous challenge by module %s (normal)\n",
40 auth_context->challenge_set_by));
41 return auth_context->challenge.data;
44 auth_context->challenge_may_be_modified = False;
46 for (auth_method = auth_context->auth_method_list; auth_method; auth_method = auth_method->next) {
47 if (auth_method->get_chal == NULL) {
48 DEBUG(5, ("auth_get_challenge: module %s did not want to specify a challenge\n", auth_method->name));
52 DEBUG(5, ("auth_get_challenge: getting challenge from module %s\n", auth_method->name));
53 if (challenge_set_by != NULL) {
54 DEBUG(1, ("auth_get_challenge: CONFIGURATION ERROR: authentication method %s has already specified a challenge. Challenge by %s ignored.\n",
55 challenge_set_by, auth_method->name));
59 mem_ctx = talloc_init("auth_get_challenge for module %s", auth_method->name);
61 smb_panic("talloc_init() failed!");
64 challenge = auth_method->get_chal(auth_context, &auth_method->private_data, mem_ctx);
65 if (!challenge.length) {
66 DEBUG(3, ("auth_get_challenge: getting challenge from authentication method %s FAILED.\n",
69 DEBUG(5, ("auth_get_challenge: sucessfully got challenge from module %s\n", auth_method->name));
70 auth_context->challenge = challenge;
71 challenge_set_by = auth_method->name;
72 auth_context->challenge_set_method = auth_method;
74 talloc_destroy(mem_ctx);
77 if (!challenge_set_by) {
80 generate_random_buffer(chal, sizeof(chal), False);
81 auth_context->challenge = data_blob_talloc(auth_context->mem_ctx,
84 challenge_set_by = "random";
85 auth_context->challenge_may_be_modified = True;
88 DEBUG(5, ("auth_context challenge created by %s\n", challenge_set_by));
89 DEBUG(5, ("challenge is: \n"));
90 dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
92 SMB_ASSERT(auth_context->challenge.length == 8);
94 auth_context->challenge_set_by=challenge_set_by;
96 return auth_context->challenge.data;
101 * Check user is in correct domain (if required)
103 * @param user Only used to fill in the debug message
105 * @param domain The domain to be verified
107 * @return True if the user can connect with that domain,
111 static BOOL check_domain_match(const char *user, const char *domain)
114 * If we aren't serving to trusted domains, we must make sure that
115 * the validation request comes from an account in the same domain
116 * as the Samba server
119 if (!lp_allow_trusted_domains() &&
120 !(strequal("", domain) ||
121 strequal(lp_workgroup(), domain) ||
122 is_myname(domain))) {
123 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
131 * Check a user's Plaintext, LM or NTLM password.
133 * Check a user's password, as given in the user_info struct and return various
134 * interesting details in the server_info struct.
136 * The return value takes precedence over the contents of the server_info
137 * struct. When the return is other than NT_STATUS_OK the contents
138 * of that structure is undefined.
140 * @param user_info Contains the user supplied components, including the passwords.
141 * Must be created with make_user_info() or one of its wrappers.
143 * @param auth_context Supplies the challenges and some other data.
144 * Must be created with make_auth_context(), and the challenges should be
145 * filled in, either at creation or by calling the challenge geneation
146 * function auth_get_challenge().
148 * @param server_info If successful, contains information about the authentication,
149 * including a SAM_ACCOUNT struct describing the user.
151 * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
155 static NTSTATUS check_ntlm_password(const struct auth_context *auth_context,
156 const struct auth_usersupplied_info *user_info,
157 struct auth_serversupplied_info **server_info)
159 /* if all the modules say 'not for me' this is reasonable */
160 NTSTATUS nt_status = NT_STATUS_NO_SUCH_USER;
161 struct auth_methods *auth_method;
164 if (!user_info || !auth_context || !server_info)
165 return NT_STATUS_LOGON_FAILURE;
167 DEBUG(3, ("check_ntlm_password: Checking password for unmapped user [%s]\\[%s]@[%s] with the new password interface\n",
168 user_info->client_domain.str, user_info->smb_name.str, user_info->wksta_name.str));
170 DEBUG(3, ("check_ntlm_password: mapped user is: [%s]\\[%s]@[%s]\n",
171 user_info->domain.str, user_info->internal_username.str, user_info->wksta_name.str));
173 if (auth_context->challenge.length == 0) {
174 /* get a challenge, if we have not asked for one yet */
175 get_ntlm_challenge(auth_context);
178 if (auth_context->challenge.length != 8) {
179 DEBUG(0, ("check_ntlm_password: Invalid challenge stored for this auth context - cannot continue\n"));
180 return NT_STATUS_LOGON_FAILURE;
183 if (auth_context->challenge_set_by)
184 DEBUG(10, ("check_ntlm_password: auth_context challenge created by %s\n",
185 auth_context->challenge_set_by));
187 DEBUG(10, ("challenge is: \n"));
188 dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
190 #ifdef DEBUG_PASSWORD
191 DEBUG(100, ("user_info has passwords of length %d and %d\n",
192 user_info->lm_resp.length, user_info->nt_resp.length));
193 DEBUG(100, ("lm:\n"));
194 dump_data(100, user_info->lm_resp.data, user_info->lm_resp.length);
195 DEBUG(100, ("nt:\n"));
196 dump_data(100, user_info->nt_resp.data, user_info->nt_resp.length);
199 /* This needs to be sorted: If it doesn't match, what should we do? */
200 if (!check_domain_match(user_info->smb_name.str, user_info->domain.str))
201 return NT_STATUS_LOGON_FAILURE;
203 for (auth_method = auth_context->auth_method_list;auth_method; auth_method = auth_method->next) {
206 mem_ctx = talloc_init("%s authentication for user %s\\%s", auth_method->name,
207 user_info->domain.str, user_info->smb_name.str);
209 result = auth_method->auth(auth_context, auth_method->private_data, mem_ctx, user_info, server_info);
211 /* check if the module did anything */
212 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_NOT_IMPLEMENTED) ) {
213 DEBUG(10,("check_ntlm_password: %s had nothing to say\n", auth_method->name));
214 talloc_destroy(mem_ctx);
220 if (NT_STATUS_IS_OK(nt_status)) {
221 DEBUG(3, ("check_ntlm_password: %s authentication for user [%s] succeeded\n",
222 auth_method->name, user_info->smb_name.str));
224 DEBUG(5, ("check_ntlm_password: %s authentication for user [%s] FAILED with error %s\n",
225 auth_method->name, user_info->smb_name.str, nt_errstr(nt_status)));
228 talloc_destroy(mem_ctx);
230 if ( NT_STATUS_IS_OK(nt_status))
236 if (NT_STATUS_IS_OK(nt_status)) {
237 if (NT_STATUS_IS_OK(nt_status)) {
238 DEBUG((*server_info)->guest ? 5 : 2,
239 ("check_ntlm_password: %sauthentication for user [%s] -> [%s] succeeded\n",
240 (*server_info)->guest ? "guest " : "",
241 user_info->smb_name.str,
242 user_info->internal_username.str));
246 if (!NT_STATUS_IS_OK(nt_status)) {
247 DEBUG(2, ("check_ntlm_password: Authentication for user [%s] -> [%s] FAILED with error %s\n",
248 user_info->smb_name.str, user_info->internal_username.str,
249 nt_errstr(nt_status)));
250 ZERO_STRUCTP(server_info);
255 /***************************************************************************
256 Clear out a auth_context, and destroy the attached TALLOC_CTX
257 ***************************************************************************/
259 void free_auth_context(struct auth_context **auth_context)
261 struct auth_methods *auth_method;
264 /* Free private data of context's authentication methods */
265 for (auth_method = (*auth_context)->auth_method_list; auth_method; auth_method = auth_method->next) {
266 if (auth_method->free_private_data) {
267 auth_method->free_private_data (&auth_method->private_data);
268 auth_method->private_data = NULL;
272 talloc_destroy((*auth_context)->mem_ctx);
273 *auth_context = NULL;
277 /***************************************************************************
278 Make a auth_info struct
279 ***************************************************************************/
281 static NTSTATUS make_auth_context(struct auth_context **auth_context)
285 mem_ctx = talloc_init("authentication context");
287 *auth_context = talloc(mem_ctx, sizeof(**auth_context));
288 if (!*auth_context) {
289 DEBUG(0,("make_auth_context: talloc failed!\n"));
290 talloc_destroy(mem_ctx);
291 return NT_STATUS_NO_MEMORY;
293 ZERO_STRUCTP(*auth_context);
295 (*auth_context)->mem_ctx = mem_ctx;
296 (*auth_context)->check_ntlm_password = check_ntlm_password;
297 (*auth_context)->get_ntlm_challenge = get_ntlm_challenge;
302 /***************************************************************************
303 Make a auth_info struct for the auth subsystem
304 ***************************************************************************/
306 static NTSTATUS make_auth_context_text_list(struct auth_context **auth_context, char **text_list)
308 struct auth_methods *list = NULL;
309 struct auth_methods *t = NULL;
314 DEBUG(2,("make_auth_context_text_list: No auth method list!?\n"));
315 return NT_STATUS_UNSUCCESSFUL;
318 if (!NT_STATUS_IS_OK(nt_status = make_auth_context(auth_context)))
321 for (;*text_list; text_list++) {
322 char *module_name = smb_xstrdup(*text_list);
323 char *module_params = NULL;
325 const struct auth_operations *ops;
327 DEBUG(5,("make_auth_context_text_list: Attempting to find an auth method to match %s\n",
330 p = strchr(module_name, ':');
334 trim_string(module_params, " ", " ");
337 trim_string(module_name, " ", " ");
339 ops = auth_backend_byname(module_name);
341 DEBUG(5,("make_auth_context_text_list: Found auth method %s (at pos %d)\n", *text_list, i));
342 SAFE_FREE(module_name);
346 if (NT_STATUS_IS_OK(ops->init(*auth_context, module_params, &t))) {
347 DEBUG(5,("make_auth_context_text_list: auth method %s has a valid init\n",
349 DLIST_ADD_END(list, t, struct auth_methods *);
351 DEBUG(0,("make_auth_context_text_list: auth method %s did not correctly init\n",
354 SAFE_FREE(module_name);
357 (*auth_context)->auth_method_list = list;
362 /***************************************************************************
363 Make a auth_context struct for the auth subsystem
364 ***************************************************************************/
366 NTSTATUS make_auth_context_subsystem(struct auth_context **auth_context)
368 char **auth_method_list = NULL;
371 if (lp_auth_methods() && !str_list_copy(&auth_method_list, lp_auth_methods())) {
372 return NT_STATUS_NO_MEMORY;
375 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_text_list(auth_context, auth_method_list))) {
376 str_list_free(&auth_method_list);
380 str_list_free(&auth_method_list);
384 /***************************************************************************
385 Make a auth_info struct with a fixed challenge
386 ***************************************************************************/
388 NTSTATUS make_auth_context_fixed(struct auth_context **auth_context, uint8_t chal[8])
391 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(auth_context))) {
395 (*auth_context)->challenge = data_blob_talloc((*auth_context)->mem_ctx, chal, 8);
396 (*auth_context)->challenge_set_by = "fixed";
400 /* the list of currently registered AUTH backends */
402 const struct auth_operations *ops;
404 static int num_backends;
407 register a AUTH backend.
409 The 'name' can be later used by other backends to find the operations
410 structure for this backend.
412 static NTSTATUS auth_register(void *_ops)
414 const struct auth_operations *ops = _ops;
415 struct auth_operations *new_ops;
417 if (auth_backend_byname(ops->name) != NULL) {
418 /* its already registered! */
419 DEBUG(0,("AUTH backend '%s' already registered\n",
421 return NT_STATUS_OBJECT_NAME_COLLISION;
424 backends = Realloc(backends, sizeof(backends[0]) * (num_backends+1));
426 smb_panic("out of memory in auth_register");
429 new_ops = smb_xmemdup(ops, sizeof(*ops));
430 new_ops->name = smb_xstrdup(ops->name);
432 backends[num_backends].ops = new_ops;
436 DEBUG(3,("AUTH backend '%s' registered\n",
443 return the operations structure for a named backend of the specified type
445 const struct auth_operations *auth_backend_byname(const char *name)
449 for (i=0;i<num_backends;i++) {
450 if (strcmp(backends[i].ops->name, name) == 0) {
451 return backends[i].ops;
459 return the AUTH interface version, and the size of some critical types
460 This can be used by backends to either detect compilation errors, or provide
461 multiple implementations for different smbd compilation options in one module
463 const struct auth_critical_sizes *auth_interface_version(void)
465 static const struct auth_critical_sizes critical_sizes = {
466 AUTH_INTERFACE_VERSION,
467 sizeof(struct auth_operations),
468 sizeof(struct auth_methods),
469 sizeof(struct auth_context),
470 sizeof(struct auth_ntlmssp_state),
471 sizeof(struct auth_usersupplied_info),
472 sizeof(struct auth_serversupplied_info),
473 sizeof(struct auth_str),
476 return &critical_sizes;
480 initialise the AUTH subsystem
486 status = register_subsystem("auth", auth_register);
487 if (!NT_STATUS_IS_OK(status)) {
491 /* FIXME: Perhaps panic if a basic backend, such as SAM, fails to initialise? */
494 DEBUG(3,("AUTH subsystem version %d initialised\n", AUTH_INTERFACE_VERSION));