2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2009
5 Copyright (C) Gerald Carter 2003
6 Copyright (C) Stefan Metzmacher 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/time.h"
25 #include "libcli/ldap/ldap_ndr.h"
26 #include "libcli/security/security.h"
27 #include "auth/auth.h"
28 #include "../libcli/auth/ntlm_check.h"
29 #include "auth/ntlm/auth_proto.h"
30 #include "auth/auth_sam.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "dsdb/common/util.h"
33 #include "param/param.h"
34 #include "librpc/gen_ndr/ndr_irpc_c.h"
35 #include "lib/messaging/irpc.h"
36 #include "libcli/auth/libcli_auth.h"
37 #include "libds/common/roles.h"
39 NTSTATUS auth_sam_init(void);
41 extern const char *user_attrs[];
42 extern const char *domain_ref_attrs[];
44 /****************************************************************************
45 Look for the specified user in the sam, return ldb result structures
46 ****************************************************************************/
48 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
49 const char *account_name,
50 struct ldb_dn *domain_dn,
51 struct ldb_message **ret_msg)
55 /* pull the user attributes */
56 ret = dsdb_search_one(sam_ctx, mem_ctx, ret_msg, domain_dn, LDB_SCOPE_SUBTREE,
58 DSDB_SEARCH_SHOW_EXTENDED_DN,
59 "(&(sAMAccountName=%s)(objectclass=user))",
60 ldb_binary_encode_string(mem_ctx, account_name));
61 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
62 DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n",
63 account_name, ldb_dn_get_linearized(domain_dn)));
64 return NT_STATUS_NO_SUCH_USER;
66 if (ret != LDB_SUCCESS) {
67 return NT_STATUS_INTERNAL_DB_CORRUPTION;
73 /****************************************************************************
74 Do a specific test for an smb password being correct, given a smb_password and
75 the lanman and NT responses.
76 ****************************************************************************/
77 static NTSTATUS authsam_password_ok(struct auth4_context *auth_context,
80 const struct samr_Password *lm_pwd,
81 const struct samr_Password *nt_pwd,
82 const struct auth_usersupplied_info *user_info,
83 DATA_BLOB *user_sess_key,
84 DATA_BLOB *lm_sess_key)
88 switch (user_info->password_state) {
89 case AUTH_PASSWORD_PLAIN:
91 const struct auth_usersupplied_info *user_info_temp;
92 status = encrypt_user_info(mem_ctx, auth_context,
94 user_info, &user_info_temp);
95 if (!NT_STATUS_IS_OK(status)) {
96 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
99 user_info = user_info_temp;
103 case AUTH_PASSWORD_HASH:
104 *lm_sess_key = data_blob(NULL, 0);
105 *user_sess_key = data_blob(NULL, 0);
106 status = hash_password_check(mem_ctx,
107 lpcfg_lanman_auth(auth_context->lp_ctx),
108 user_info->password.hash.lanman,
109 user_info->password.hash.nt,
110 user_info->mapped.account_name,
112 NT_STATUS_NOT_OK_RETURN(status);
115 case AUTH_PASSWORD_RESPONSE:
116 status = ntlm_password_check(mem_ctx,
117 lpcfg_lanman_auth(auth_context->lp_ctx),
118 lpcfg_ntlm_auth(auth_context->lp_ctx),
119 user_info->logon_parameters,
120 &auth_context->challenge.data,
121 &user_info->password.response.lanman,
122 &user_info->password.response.nt,
123 user_info->mapped.account_name,
124 user_info->client.account_name,
125 user_info->client.domain_name,
127 user_sess_key, lm_sess_key);
128 NT_STATUS_NOT_OK_RETURN(status);
137 send a message to the drepl server telling it to initiate a
138 REPL_SECRET getncchanges extended op to fetch the users secrets
140 static void auth_sam_trigger_repl_secret(struct auth4_context *auth_context,
141 struct ldb_dn *user_dn)
143 struct dcerpc_binding_handle *irpc_handle;
144 struct drepl_trigger_repl_secret r;
145 struct tevent_req *req;
148 tmp_ctx = talloc_new(auth_context);
149 if (tmp_ctx == NULL) {
153 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, auth_context->msg_ctx,
156 if (irpc_handle == NULL) {
157 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
158 TALLOC_FREE(tmp_ctx);
162 r.in.user_dn = ldb_dn_get_linearized(user_dn);
165 * This seem to rely on the current IRPC implementation,
166 * which delivers the message in the _send function.
168 * TODO: we need a ONE_WAY IRPC handle and register
169 * a callback and wait for it to be triggered!
171 req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
172 auth_context->event_ctx,
176 /* we aren't interested in a reply */
178 TALLOC_FREE(tmp_ctx);
183 * Check that a password is OK, and update badPwdCount if required.
186 static NTSTATUS authsam_password_check_and_record(struct auth4_context *auth_context,
188 struct ldb_dn *domain_dn,
189 struct ldb_message *msg,
191 const struct auth_usersupplied_info *user_info,
192 DATA_BLOB *user_sess_key,
193 DATA_BLOB *lm_sess_key)
196 NTSTATUS auth_status;
200 struct ldb_context *sam_ctx = auth_context->sam_ctx;
201 const char * const attrs[] = { "pwdHistoryLength", NULL };
202 struct ldb_message *dom_msg;
203 struct samr_Password *lm_pwd;
204 struct samr_Password *nt_pwd;
206 tmp_ctx = talloc_new(mem_ctx);
207 if (tmp_ctx == NULL) {
208 return NT_STATUS_NO_MEMORY;
212 * This call does more than what it appears to do, it also
213 * checks for the account lockout.
215 * It is done here so that all parts of Samba that read the
216 * password refuse to even operate on it if the account is
217 * locked out, to avoid mistakes like CVE-2013-4496.
219 nt_status = samdb_result_passwords(tmp_ctx, auth_context->lp_ctx,
220 msg, &lm_pwd, &nt_pwd);
221 if (!NT_STATUS_IS_OK(nt_status)) {
222 TALLOC_FREE(tmp_ctx);
226 if (lm_pwd == NULL && nt_pwd == NULL) {
228 if (samdb_rodc(auth_context->sam_ctx, &am_rodc) == LDB_SUCCESS && am_rodc) {
230 * we don't have passwords for this
231 * account. We are an RODC, and this account
232 * may be one for which we either are denied
233 * REPL_SECRET replication or we haven't yet
234 * done the replication. We return
235 * NT_STATUS_NOT_IMPLEMENTED which tells the
236 * auth code to try the next authentication
237 * mechanism. We also send a message to our
238 * drepl server to tell it to try and
239 * replicate the secrets for this account.
241 auth_sam_trigger_repl_secret(auth_context, msg->dn);
242 TALLOC_FREE(tmp_ctx);
243 return NT_STATUS_NOT_IMPLEMENTED;
247 auth_status = authsam_password_ok(auth_context, tmp_ctx,
251 user_sess_key, lm_sess_key);
252 if (NT_STATUS_IS_OK(auth_status)) {
253 if (user_sess_key->data) {
254 talloc_steal(mem_ctx, user_sess_key->data);
256 if (lm_sess_key->data) {
257 talloc_steal(mem_ctx, lm_sess_key->data);
259 TALLOC_FREE(tmp_ctx);
262 *user_sess_key = data_blob_null;
263 *lm_sess_key = data_blob_null;
265 if (!NT_STATUS_EQUAL(auth_status, NT_STATUS_WRONG_PASSWORD)) {
266 TALLOC_FREE(tmp_ctx);
271 * We only continue if this was a wrong password
272 * and we'll always return NT_STATUS_WRONG_PASSWORD
273 * no matter what error happens.
276 /* pull the domain password property attributes */
277 ret = dsdb_search_one(sam_ctx, tmp_ctx, &dom_msg, domain_dn, LDB_SCOPE_BASE,
278 attrs, 0, "objectClass=domain");
279 if (ret == LDB_SUCCESS) {
280 history_len = ldb_msg_find_attr_as_uint(dom_msg, "pwdHistoryLength", 0);
281 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
282 DEBUG(3,("Couldn't find domain %s: %s!\n",
283 ldb_dn_get_linearized(domain_dn),
284 ldb_errstring(sam_ctx)));
286 DEBUG(3,("error finding domain %s: %s!\n",
287 ldb_dn_get_linearized(domain_dn),
288 ldb_errstring(sam_ctx)));
291 for (i = 1; i < MIN(history_len, 3); i++) {
292 static const struct samr_Password zero_hash;
293 struct samr_Password zero_string_hash;
294 struct samr_Password zero_string_des_hash;
295 struct samr_Password *nt_history_pwd = NULL;
296 struct samr_Password *lm_history_pwd = NULL;
298 struct timeval tv_now;
300 int allowed_period_mins;
301 NTTIME allowed_period;
303 nt_status = samdb_result_passwords_from_history(tmp_ctx,
304 auth_context->lp_ctx,
308 if (!NT_STATUS_IS_OK(nt_status)) {
310 * If we don't find element 'i' we won't find
317 * We choose to avoid any issues
318 * around different LM and NT history
319 * lengths by only checking the NT
322 if (nt_history_pwd == NULL) {
324 * If we don't find element 'i' we won't find
330 /* Skip over all-zero hashes in the history */
331 if (memcmp(nt_history_pwd->hash, zero_hash.hash,
332 sizeof(zero_hash.hash)) == 0) {
337 * This looks odd, but the password_hash module writes this in if
338 * (somehow) we didn't have an old NT hash
341 E_md4hash("", zero_string_hash.hash);
342 if (memcmp(nt_history_pwd->hash, zero_string_hash.hash, 16) == 0) {
346 E_deshash("", zero_string_des_hash.hash);
347 if (!lm_history_pwd || memcmp(lm_history_pwd->hash, zero_string_des_hash.hash, 16) == 0) {
348 lm_history_pwd = NULL;
351 auth_status = authsam_password_ok(auth_context, tmp_ctx,
358 if (!NT_STATUS_IS_OK(auth_status)) {
360 * If this was not a correct password, try the next
361 * one from the history
363 *user_sess_key = data_blob_null;
364 *lm_sess_key = data_blob_null;
370 * The authentication was OK, but not against
371 * the previous password, which is stored at index 1.
373 * We just return the original wrong password.
374 * This skips the update of the bad pwd count,
375 * because this is almost certainly user error
376 * (or automatic login on a computer using a cached
377 * password from before the password change),
380 TALLOC_FREE(tmp_ctx);
381 return NT_STATUS_WRONG_PASSWORD;
384 if (user_info->password_state != AUTH_PASSWORD_RESPONSE) {
386 * The authentication was OK against the previous password,
387 * but it's not a NTLM network authentication.
389 * We just return the original wrong password.
390 * This skips the update of the bad pwd count,
391 * because this is almost certainly user error
392 * (or automatic login on a computer using a cached
393 * password from before the password change),
396 TALLOC_FREE(tmp_ctx);
397 return NT_STATUS_WRONG_PASSWORD;
401 * If the password was OK, it's a NTLM network authentication
402 * and it was the previous password.
404 * Now we see if it is within the grace period,
405 * so that we don't break cached sessions on other computers
406 * before the user can lock and unlock their other screens
407 * (resetting their cached password).
409 * See http://support.microsoft.com/kb/906305
410 * OldPasswordAllowedPeriod ("old password allowed period")
411 * is specified in minutes. The default is 60.
413 allowed_period_mins = lpcfg_old_password_allowed_period(auth_context->lp_ctx);
415 * NTTIME uses 100ns units
417 allowed_period = allowed_period_mins * 60 * 1000*1000*10;
418 pwdLastSet = samdb_result_nttime(msg, "pwdLastSet", 0);
419 tv_now = timeval_current();
420 now = timeval_to_nttime(&tv_now);
422 if (now < pwdLastSet) {
426 * We just return the original wrong password.
427 * This skips the update of the bad pwd count,
428 * because this is almost certainly user error
429 * (or automatic login on a computer using a cached
430 * password from before the password change),
433 TALLOC_FREE(tmp_ctx);
434 return NT_STATUS_WRONG_PASSWORD;
437 if ((now - pwdLastSet) >= allowed_period) {
439 * The allowed period is over.
441 * We just return the original wrong password.
442 * This skips the update of the bad pwd count,
443 * because this is almost certainly user error
444 * (or automatic login on a computer using a cached
445 * password from before the password change),
448 TALLOC_FREE(tmp_ctx);
449 return NT_STATUS_WRONG_PASSWORD;
453 * We finally allow the authentication with the
454 * previous password within the allowed period.
456 if (user_sess_key->data) {
457 talloc_steal(mem_ctx, user_sess_key->data);
459 if (lm_sess_key->data) {
460 talloc_steal(mem_ctx, lm_sess_key->data);
463 TALLOC_FREE(tmp_ctx);
468 * If we are not in the allowed period or match an old password,
469 * we didn't return early. Now update the badPwdCount et al.
471 nt_status = authsam_update_bad_pwd_count(auth_context->sam_ctx,
473 if (!NT_STATUS_IS_OK(nt_status)) {
475 * We need to return the original
476 * NT_STATUS_WRONG_PASSWORD error, so there isn't
477 * anything more we can do than write something into
480 DEBUG(0, ("Failed to note bad password for user [%s]: %s\n",
481 user_info->mapped.account_name,
482 nt_errstr(nt_status)));
485 TALLOC_FREE(tmp_ctx);
486 return NT_STATUS_WRONG_PASSWORD;
489 static NTSTATUS authsam_authenticate(struct auth4_context *auth_context,
490 TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
491 struct ldb_dn *domain_dn,
492 struct ldb_message *msg,
493 const struct auth_usersupplied_info *user_info,
494 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
497 bool interactive = (user_info->password_state == AUTH_PASSWORD_HASH);
498 uint16_t acct_flags = samdb_result_acct_flags(msg, NULL);
499 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
501 return NT_STATUS_NO_MEMORY;
504 /* You can only do an interactive login to normal accounts */
505 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
506 if (!(acct_flags & ACB_NORMAL)) {
507 TALLOC_FREE(tmp_ctx);
508 return NT_STATUS_NO_SUCH_USER;
512 nt_status = authsam_password_check_and_record(auth_context, tmp_ctx,
513 domain_dn, msg, acct_flags,
515 user_sess_key, lm_sess_key);
516 if (!NT_STATUS_IS_OK(nt_status)) {
517 TALLOC_FREE(tmp_ctx);
521 nt_status = authsam_account_ok(tmp_ctx, auth_context->sam_ctx,
522 user_info->logon_parameters,
525 user_info->workstation_name,
526 user_info->mapped.account_name,
528 if (!NT_STATUS_IS_OK(nt_status)) {
529 TALLOC_FREE(tmp_ctx);
533 nt_status = authsam_logon_success_accounting(auth_context->sam_ctx,
536 if (!NT_STATUS_IS_OK(nt_status)) {
537 TALLOC_FREE(tmp_ctx);
541 if (user_sess_key && user_sess_key->data) {
542 talloc_steal(mem_ctx, user_sess_key->data);
544 if (lm_sess_key && lm_sess_key->data) {
545 talloc_steal(mem_ctx, lm_sess_key->data);
548 TALLOC_FREE(tmp_ctx);
554 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
556 const struct auth_usersupplied_info *user_info,
557 struct auth_user_info_dc **user_info_dc)
560 const char *account_name = user_info->mapped.account_name;
561 struct ldb_message *msg;
562 struct ldb_dn *domain_dn;
563 DATA_BLOB user_sess_key, lm_sess_key;
566 if (ctx->auth_ctx->sam_ctx == NULL) {
567 DEBUG(0, ("No SAM available, cannot log in users\n"));
568 return NT_STATUS_INVALID_SYSTEM_SERVICE;
571 if (!account_name || !*account_name) {
573 return NT_STATUS_NOT_IMPLEMENTED;
576 tmp_ctx = talloc_new(mem_ctx);
578 return NT_STATUS_NO_MEMORY;
581 domain_dn = ldb_get_default_basedn(ctx->auth_ctx->sam_ctx);
582 if (domain_dn == NULL) {
583 talloc_free(tmp_ctx);
584 return NT_STATUS_NO_SUCH_DOMAIN;
587 nt_status = authsam_search_account(tmp_ctx, ctx->auth_ctx->sam_ctx, account_name, domain_dn, &msg);
588 if (!NT_STATUS_IS_OK(nt_status)) {
589 talloc_free(tmp_ctx);
593 nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, ctx->auth_ctx->sam_ctx, domain_dn, msg, user_info,
594 &user_sess_key, &lm_sess_key);
595 if (!NT_STATUS_IS_OK(nt_status)) {
596 talloc_free(tmp_ctx);
600 nt_status = authsam_make_user_info_dc(tmp_ctx, ctx->auth_ctx->sam_ctx, lpcfg_netbios_name(ctx->auth_ctx->lp_ctx),
601 lpcfg_sam_name(ctx->auth_ctx->lp_ctx),
604 user_sess_key, lm_sess_key,
606 if (!NT_STATUS_IS_OK(nt_status)) {
607 talloc_free(tmp_ctx);
611 talloc_steal(mem_ctx, *user_info_dc);
612 talloc_free(tmp_ctx);
617 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
619 const struct auth_usersupplied_info *user_info)
621 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
622 return NT_STATUS_NOT_IMPLEMENTED;
628 /****************************************************************************
629 Check SAM security (above) but with a few extra checks.
630 ****************************************************************************/
631 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
633 const struct auth_usersupplied_info *user_info)
635 bool is_local_name, is_my_domain;
637 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
638 return NT_STATUS_NOT_IMPLEMENTED;
641 is_local_name = lpcfg_is_myname(ctx->auth_ctx->lp_ctx,
642 user_info->mapped.domain_name);
643 is_my_domain = lpcfg_is_mydomain(ctx->auth_ctx->lp_ctx,
644 user_info->mapped.domain_name);
646 /* check whether or not we service this domain/workgroup name */
647 switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
648 case ROLE_STANDALONE:
651 case ROLE_DOMAIN_MEMBER:
652 if (!is_local_name) {
653 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
654 user_info->mapped.domain_name));
655 return NT_STATUS_NOT_IMPLEMENTED;
659 case ROLE_ACTIVE_DIRECTORY_DC:
660 if (!is_local_name && !is_my_domain) {
661 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
662 user_info->mapped.domain_name));
663 return NT_STATUS_NOT_IMPLEMENTED;
668 DEBUG(6,("authsam_check_password: lpcfg_server_role() has an undefined value\n"));
669 return NT_STATUS_NOT_IMPLEMENTED;
673 /* Wrapper for the auth subsystem pointer */
674 static NTSTATUS authsam_get_user_info_dc_principal_wrapper(TALLOC_CTX *mem_ctx,
675 struct auth4_context *auth_context,
676 const char *principal,
677 struct ldb_dn *user_dn,
678 struct auth_user_info_dc **user_info_dc)
680 return authsam_get_user_info_dc_principal(mem_ctx, auth_context->lp_ctx, auth_context->sam_ctx,
681 principal, user_dn, user_info_dc);
683 static const struct auth_operations sam_ignoredomain_ops = {
684 .name = "sam_ignoredomain",
685 .want_check = authsam_ignoredomain_want_check,
686 .check_password = authsam_check_password_internals,
687 .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
688 .flags = AUTH_METHOD_LOCAL_SAM
691 static const struct auth_operations sam_ops = {
693 .want_check = authsam_want_check,
694 .check_password = authsam_check_password_internals,
695 .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
696 .flags = AUTH_METHOD_LOCAL_SAM
699 _PUBLIC_ NTSTATUS auth4_sam_init(void);
700 _PUBLIC_ NTSTATUS auth4_sam_init(void)
704 ret = auth_register(&sam_ops);
705 if (!NT_STATUS_IS_OK(ret)) {
706 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
710 ret = auth_register(&sam_ignoredomain_ops);
711 if (!NT_STATUS_IS_OK(ret)) {
712 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));