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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "../lib/tsocket/tsocket.h"
24 #include "param/param.h"
25 #include "../lib/messaging/messaging.h"
28 #define DBGC_CLASS DBGC_AUTH
32 static struct auth_init_function_entry *auth_backends = NULL;
34 static struct auth_init_function_entry *auth_find_backend_entry(const char *name);
36 NTSTATUS smb_register_auth(int version, const char *name, auth_init_function init)
38 struct auth_init_function_entry *entry = NULL;
40 if (version != AUTH_INTERFACE_VERSION) {
41 DEBUG(0,("Can't register auth_method!\n"
42 "You tried to register an auth module with AUTH_INTERFACE_VERSION %d, while this version of samba uses %d\n",
43 version,AUTH_INTERFACE_VERSION));
44 return NT_STATUS_OBJECT_TYPE_MISMATCH;
48 return NT_STATUS_INVALID_PARAMETER;
51 DEBUG(5,("Attempting to register auth backend %s\n", name));
53 if (auth_find_backend_entry(name)) {
54 DEBUG(0,("There already is an auth method registered with the name %s!\n", name));
55 return NT_STATUS_OBJECT_NAME_COLLISION;
58 entry = SMB_XMALLOC_P(struct auth_init_function_entry);
59 entry->name = smb_xstrdup(name);
62 DLIST_ADD(auth_backends, entry);
63 DEBUG(5,("Successfully added auth method '%s'\n", name));
67 static struct auth_init_function_entry *auth_find_backend_entry(const char *name)
69 struct auth_init_function_entry *entry = auth_backends;
72 if (strcmp(entry->name, name)==0) return entry;
79 /****************************************************************************
80 Try to get a challenge out of the various authentication modules.
81 Returns a const char of length 8 bytes.
82 ****************************************************************************/
84 NTSTATUS auth_get_ntlm_challenge(struct auth_context *auth_context,
87 if (auth_context->challenge.length) {
88 DEBUG(5, ("get_ntlm_challenge (auth subsystem): returning previous challenge by module %s (normal)\n",
89 auth_context->challenge_set_by));
90 memcpy(chal, auth_context->challenge.data, 8);
94 auth_context->challenge = data_blob_talloc(auth_context, NULL, 8);
95 if (auth_context->challenge.data == NULL) {
96 DBG_WARNING("data_blob_talloc failed\n");
97 return NT_STATUS_NO_MEMORY;
99 generate_random_buffer(
100 auth_context->challenge.data, auth_context->challenge.length);
102 auth_context->challenge_set_by = "random";
104 memcpy(chal, auth_context->challenge.data, 8);
110 * Check user is in correct domain (if required)
112 * @param user Only used to fill in the debug message
114 * @param domain The domain to be verified
116 * @return True if the user can connect with that domain,
120 static bool check_domain_match(const char *user, const char *domain)
123 * If we aren't serving to trusted domains, we must make sure that
124 * the validation request comes from an account in the same domain
125 * as the Samba server
128 if (!lp_allow_trusted_domains() &&
129 !(strequal("", domain) ||
130 strequal(lp_workgroup(), domain) ||
131 is_myname(domain))) {
132 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
140 * Check a user's Plaintext, LM or NTLM password.
142 * Check a user's password, as given in the user_info struct and return various
143 * interesting details in the server_info struct.
145 * This function does NOT need to be in a become_root()/unbecome_root() pair
146 * as it makes the calls itself when needed.
148 * The return value takes precedence over the contents of the server_info
149 * struct. When the return is other than NT_STATUS_OK the contents
150 * of that structure is undefined.
152 * @param user_info Contains the user supplied components, including the passwords.
153 * Must be created with make_user_info() or one of its wrappers.
155 * @param auth_context Supplies the challenges and some other data.
156 * Must be created with make_auth_context(), and the challenges should be
157 * filled in, either at creation or by calling the challenge geneation
158 * function auth_get_challenge().
160 * @param pserver_info If successful, contains information about the authentication,
161 * including a struct samu struct describing the user.
163 * @param pauthoritative Indicates if the result should be treated as final
166 * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
169 NTSTATUS auth_check_ntlm_password(TALLOC_CTX *mem_ctx,
170 const struct auth_context *auth_context,
171 const struct auth_usersupplied_info *user_info,
172 struct auth_serversupplied_info **pserver_info,
173 uint8_t *pauthoritative)
176 const char *auth_method_name = "";
177 /* if all the modules say 'not for me' this is reasonable */
178 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
179 const char *unix_username;
180 struct auth_methods *auth_method;
181 struct auth_serversupplied_info *server_info = NULL;
182 struct dom_sid sid = {0};
183 struct imessaging_context *msg_ctx = NULL;
184 struct loadparm_context *lp_ctx = NULL;
186 if (user_info == NULL || auth_context == NULL || pserver_info == NULL) {
187 return NT_STATUS_LOGON_FAILURE;
190 frame = talloc_stackframe();
192 if (lp_auth_event_notification()) {
193 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
194 msg_ctx = imessaging_client_init(
195 frame, lp_ctx, global_event_context());
200 DEBUG(3, ("check_ntlm_password: Checking password for unmapped user [%s]\\[%s]@[%s] with the new password interface\n",
201 user_info->client.domain_name, user_info->client.account_name, user_info->workstation_name));
203 DEBUG(3, ("check_ntlm_password: mapped user is: [%s]\\[%s]@[%s]\n",
204 user_info->mapped.domain_name, user_info->mapped.account_name, user_info->workstation_name));
206 if (auth_context->challenge.length != 8) {
207 DEBUG(0, ("check_ntlm_password: Invalid challenge stored for this auth context - cannot continue\n"));
208 nt_status = NT_STATUS_LOGON_FAILURE;
212 if (auth_context->challenge_set_by)
213 DEBUG(10, ("check_ntlm_password: auth_context challenge created by %s\n",
214 auth_context->challenge_set_by));
216 DEBUG(10, ("challenge is: \n"));
217 dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
219 #ifdef DEBUG_PASSWORD
220 DEBUG(100, ("user_info has passwords of length %d and %d\n",
221 (int)user_info->password.response.lanman.length, (int)user_info->password.response.nt.length));
222 DEBUG(100, ("lm:\n"));
223 dump_data(100, user_info->password.response.lanman.data, user_info->password.response.lanman.length);
224 DEBUG(100, ("nt:\n"));
225 dump_data(100, user_info->password.response.nt.data, user_info->password.response.nt.length);
228 /* This needs to be sorted: If it doesn't match, what should we do? */
229 if (!check_domain_match(user_info->client.account_name,
230 user_info->mapped.domain_name)) {
231 nt_status = NT_STATUS_LOGON_FAILURE;
235 for (auth_method = auth_context->auth_method_list;auth_method; auth_method = auth_method->next) {
237 auth_method_name = auth_method->name;
239 nt_status = auth_method->auth(auth_context,
240 auth_method->private_data,
245 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOT_IMPLEMENTED)) {
249 DBG_DEBUG("%s had nothing to say\n", auth_method->name);
252 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOT_IMPLEMENTED)) {
254 nt_status = NT_STATUS_NO_SUCH_USER;
257 if (!NT_STATUS_IS_OK(nt_status)) {
258 DBG_INFO("%s authentication for user [%s] FAILED with "
259 "error %s, authoritative=%u\n",
261 user_info->client.account_name,
262 nt_errstr(nt_status),
267 DBG_NOTICE("%s authentication for user [%s] succeeded\n",
268 auth_method_name, user_info->client.account_name);
270 unix_username = server_info->unix_name;
272 /* We skip doing this step if the caller asked us not to */
273 if (!(user_info->flags & USER_INFO_INFO3_AND_NO_AUTHZ)
274 && !(server_info->guest)) {
277 if (tsocket_address_is_inet(user_info->remote_host, "ip")) {
278 rhost = tsocket_address_inet_addr_string(
279 user_info->remote_host, talloc_tos());
281 nt_status = NT_STATUS_NO_MEMORY;
288 /* We might not be root if we are an RPC call */
290 nt_status = smb_pam_accountcheck(unix_username, rhost);
293 if (NT_STATUS_IS_OK(nt_status)) {
294 DEBUG(5, ("check_ntlm_password: PAM Account for user [%s] "
295 "succeeded\n", unix_username));
297 DEBUG(3, ("check_ntlm_password: PAM Account for user [%s] "
298 "FAILED with error %s\n",
299 unix_username, nt_errstr(nt_status)));
303 if (!NT_STATUS_IS_OK(nt_status)) {
307 nt_status = get_user_sid_info3_and_extra(server_info->info3,
310 if (!NT_STATUS_IS_OK(nt_status)) {
311 sid = (struct dom_sid) {0};
314 log_authentication_event(msg_ctx,
316 &auth_context->start_time,
319 server_info->info3->base.logon_domain.string,
320 server_info->info3->base.account_name.string,
323 DEBUG(server_info->guest ? 5 : 2,
324 ("check_ntlm_password: %sauthentication for user "
325 "[%s] -> [%s] -> [%s] succeeded\n",
326 server_info->guest ? "guest " : "",
327 user_info->client.account_name,
328 user_info->mapped.account_name,
331 *pserver_info = talloc_move(mem_ctx, &server_info);
338 /* failed authentication; check for guest lapping */
341 * Please try not to change this string, it is probably in use
342 * in audit logging tools
344 DEBUG(2, ("check_ntlm_password: Authentication for user "
345 "[%s] -> [%s] FAILED with error %s, authoritative=%u\n",
346 user_info->client.account_name, user_info->mapped.account_name,
347 nt_errstr(nt_status), *pauthoritative));
349 log_authentication_event(msg_ctx,
351 &auth_context->start_time,
358 ZERO_STRUCTP(pserver_info);
365 /***************************************************************************
366 Clear out a auth_context, and destroy the attached TALLOC_CTX
367 ***************************************************************************/
369 static int auth_context_destructor(void *ptr)
371 struct auth_context *ctx = talloc_get_type(ptr, struct auth_context);
372 struct auth_methods *am;
375 /* Free private data of context's authentication methods */
376 for (am = ctx->auth_method_list; am; am = am->next) {
377 TALLOC_FREE(am->private_data);
383 /***************************************************************************
384 Make a auth_info struct
385 ***************************************************************************/
387 static NTSTATUS make_auth_context(TALLOC_CTX *mem_ctx,
388 struct auth_context **auth_context)
390 struct auth_context *ctx;
392 ctx = talloc_zero(mem_ctx, struct auth_context);
394 DEBUG(0,("make_auth_context: talloc failed!\n"));
395 return NT_STATUS_NO_MEMORY;
398 ctx->start_time = timeval_current();
400 talloc_set_destructor((TALLOC_CTX *)ctx, auth_context_destructor);
406 bool load_auth_module(struct auth_context *auth_context,
408 struct auth_methods **ret)
410 static bool initialised_static_modules = False;
412 struct auth_init_function_entry *entry;
413 char *module_name = smb_xstrdup(module);
414 char *module_params = NULL;
418 /* Initialise static modules if not done so yet */
419 if(!initialised_static_modules) {
420 static_init_auth(NULL);
421 initialised_static_modules = True;
424 DEBUG(5,("load_auth_module: Attempting to find an auth method to match %s\n",
427 p = strchr(module_name, ':');
431 trim_char(module_params, ' ', ' ');
434 trim_char(module_name, ' ', ' ');
436 entry = auth_find_backend_entry(module_name);
439 if (NT_STATUS_IS_OK(smb_probe_module("auth", module_name))) {
440 entry = auth_find_backend_entry(module_name);
445 if (!NT_STATUS_IS_OK(entry->init(auth_context, module_params, ret))) {
446 DEBUG(0,("load_auth_module: auth method %s did not correctly init\n",
449 DEBUG(5,("load_auth_module: auth method %s has a valid init\n",
454 DEBUG(0,("load_auth_module: can't find auth method %s!\n", module_name));
457 SAFE_FREE(module_name);
461 /***************************************************************************
462 Make a auth_info struct for the auth subsystem
463 ***************************************************************************/
465 static NTSTATUS make_auth_context_text_list(TALLOC_CTX *mem_ctx,
466 struct auth_context **auth_context,
469 struct auth_methods *list = NULL;
470 struct auth_methods *t, *method = NULL;
474 DEBUG(2,("make_auth_context_text_list: No auth method list!?\n"));
475 return NT_STATUS_UNSUCCESSFUL;
478 nt_status = make_auth_context(mem_ctx, auth_context);
480 if (!NT_STATUS_IS_OK(nt_status)) {
484 for (;*text_list; text_list++) {
485 if (load_auth_module(*auth_context, *text_list, &t)) {
486 DLIST_ADD_END(list, t);
490 (*auth_context)->auth_method_list = list;
492 /* Look for the first module to provide a prepare_gensec and
493 * make_auth4_context hook, and set that if provided */
494 for (method = (*auth_context)->auth_method_list; method; method = method->next) {
495 if (method->prepare_gensec && method->make_auth4_context) {
496 (*auth_context)->prepare_gensec = method->prepare_gensec;
497 (*auth_context)->make_auth4_context = method->make_auth4_context;
504 static NTSTATUS make_auth_context_specific(TALLOC_CTX *mem_ctx,
505 struct auth_context **auth_context,
511 method_list = str_list_make_v3(talloc_tos(), methods, NULL);
512 if (method_list == NULL) {
513 return NT_STATUS_NO_MEMORY;
516 status = make_auth_context_text_list(
517 mem_ctx, auth_context, method_list);
519 TALLOC_FREE(method_list);
524 /***************************************************************************
525 Make a auth_context struct for the auth subsystem
526 ***************************************************************************/
528 NTSTATUS make_auth3_context_for_ntlm(TALLOC_CTX *mem_ctx,
529 struct auth_context **auth_context)
531 const char *methods = NULL;
533 switch (lp_server_role()) {
534 case ROLE_ACTIVE_DIRECTORY_DC:
535 DEBUG(5,("Making default auth method list for server role = "
536 "'active directory domain controller'\n"));
539 case ROLE_DOMAIN_MEMBER:
540 DEBUG(5,("Making default auth method list for server role = 'domain member'\n"));
541 methods = "anonymous sam winbind sam_ignoredomain";
543 case ROLE_DOMAIN_BDC:
544 case ROLE_DOMAIN_PDC:
545 DEBUG(5,("Making default auth method list for DC\n"));
546 methods = "anonymous sam winbind sam_ignoredomain";
548 case ROLE_STANDALONE:
549 DEBUG(5,("Making default auth method list for server role = 'standalone server', encrypt passwords = yes\n"));
550 if (lp_encrypt_passwords()) {
551 methods = "anonymous sam_ignoredomain";
553 DEBUG(5,("Making default auth method list for server role = 'standalone server', encrypt passwords = no\n"));
554 methods = "anonymous unix";
558 DEBUG(5,("Unknown auth method!\n"));
559 return NT_STATUS_UNSUCCESSFUL;
562 return make_auth_context_specific(mem_ctx, auth_context, methods);
565 NTSTATUS make_auth3_context_for_netlogon(TALLOC_CTX *mem_ctx,
566 struct auth_context **auth_context)
568 const char *methods = NULL;
570 switch (lp_server_role()) {
571 case ROLE_DOMAIN_BDC:
572 case ROLE_DOMAIN_PDC:
573 methods = "sam_netlogon3 winbind";
577 DBG_ERR("Invalid server role!\n");
578 return NT_STATUS_INVALID_SERVER_STATE;
581 return make_auth_context_specific(mem_ctx, auth_context, methods);
584 NTSTATUS make_auth3_context_for_winbind(TALLOC_CTX *mem_ctx,
585 struct auth_context **auth_context)
587 const char *methods = NULL;
589 switch (lp_server_role()) {
590 case ROLE_STANDALONE:
591 case ROLE_DOMAIN_MEMBER:
592 case ROLE_DOMAIN_BDC:
593 case ROLE_DOMAIN_PDC:
596 case ROLE_ACTIVE_DIRECTORY_DC:
597 methods = "samba4:sam";
600 DEBUG(5,("Unknown auth method!\n"));
601 return NT_STATUS_UNSUCCESSFUL;
604 return make_auth_context_specific(mem_ctx, auth_context, methods);
607 bool auth3_context_set_challenge(struct auth_context *ctx, uint8_t chal[8],
608 const char *challenge_set_by)
610 ctx->challenge = data_blob_talloc(ctx, chal, 8);
611 if (ctx->challenge.data == NULL) {
614 ctx->challenge_set_by = talloc_strdup(ctx, challenge_set_by);
615 if (ctx->challenge_set_by == NULL) {