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-2010
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/gmsa/util.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "dsdb/samdb/ldb_modules/util.h"
34 #include "dsdb/common/util.h"
35 #include "param/param.h"
36 #include "librpc/gen_ndr/ndr_irpc_c.h"
37 #include "librpc/gen_ndr/ndr_winbind_c.h"
38 #include "lib/crypto/gkdi.h"
39 #include "lib/messaging/irpc.h"
40 #include "libcli/auth/libcli_auth.h"
41 #include "libds/common/roles.h"
42 #include "lib/util/tevent_ntstatus.h"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
45 #include "kdc/authn_policy_util.h"
46 #include "kdc/db-glue.h"
49 #define DBGC_CLASS DBGC_AUTH
51 NTSTATUS
auth_sam_init(void);
53 extern const char *user_attrs
[];
54 extern const char *domain_ref_attrs
[];
56 /****************************************************************************
57 Do a specific test for an smb password being correct, given a smb_password and
58 the lanman and NT responses.
59 ****************************************************************************/
60 static NTSTATUS
authsam_password_ok(struct auth4_context
*auth_context
,
62 const struct samr_Password
*nt_pwd
,
63 struct smb_krb5_context
*smb_krb5_context
,
64 const DATA_BLOB
*stored_aes_256_key
,
65 const krb5_data
*salt
,
66 const struct auth_usersupplied_info
*user_info
,
67 DATA_BLOB
*user_sess_key
,
68 DATA_BLOB
*lm_sess_key
)
72 switch (user_info
->password_state
) {
73 case AUTH_PASSWORD_PLAIN
:
75 const struct auth_usersupplied_info
*user_info_temp
;
77 if (nt_pwd
== NULL
&& stored_aes_256_key
!= NULL
&& user_info
->password
.plaintext
!= NULL
) {
80 DATA_BLOB supplied_aes_256_key
;
82 krb5_data cleartext_data
= {
83 .data
= user_info
->password
.plaintext
,
84 .length
= strlen(user_info
->password
.plaintext
)
87 *lm_sess_key
= data_blob_null
;
88 *user_sess_key
= data_blob_null
;
90 krb5_ret
= smb_krb5_create_key_from_string(smb_krb5_context
->krb5_context
,
94 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
97 DBG_ERR("generation of a aes256-cts-hmac-sha1-96 key for password comparison failed: %s\n",
98 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
100 return NT_STATUS_INTERNAL_ERROR
;
103 supplied_aes_256_key
= data_blob_const(KRB5_KEY_DATA(&key
),
104 KRB5_KEY_LENGTH(&key
));
106 pw_equal
= data_blob_equal_const_time(&supplied_aes_256_key
,
109 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &key
);
111 return NT_STATUS_WRONG_PASSWORD
;
116 status
= encrypt_user_info(mem_ctx
, auth_context
,
118 user_info
, &user_info_temp
);
119 if (!NT_STATUS_IS_OK(status
)) {
120 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status
)));
123 user_info
= user_info_temp
;
127 case AUTH_PASSWORD_HASH
:
128 *lm_sess_key
= data_blob(NULL
, 0);
129 *user_sess_key
= data_blob(NULL
, 0);
130 status
= hash_password_check(mem_ctx
,
132 lpcfg_ntlm_auth(auth_context
->lp_ctx
),
134 user_info
->password
.hash
.nt
,
135 user_info
->mapped
.account_name
,
137 NT_STATUS_NOT_OK_RETURN(status
);
140 case AUTH_PASSWORD_RESPONSE
:
141 status
= ntlm_password_check(mem_ctx
,
143 lpcfg_ntlm_auth(auth_context
->lp_ctx
),
144 user_info
->logon_parameters
,
145 &auth_context
->challenge
.data
,
146 &user_info
->password
.response
.lanman
,
147 &user_info
->password
.response
.nt
,
148 user_info
->mapped
.account_name
,
149 user_info
->client
.account_name
,
150 user_info
->client
.domain_name
,
152 user_sess_key
, lm_sess_key
);
153 NT_STATUS_NOT_OK_RETURN(status
);
160 static void auth_sam_trigger_zero_password(TALLOC_CTX
*mem_ctx
,
161 struct imessaging_context
*msg_ctx
,
162 struct tevent_context
*event_ctx
,
163 struct netr_SendToSamBase
*send_to_sam
)
165 struct dcerpc_binding_handle
*irpc_handle
;
166 struct winbind_SendToSam r
;
167 struct tevent_req
*req
;
170 tmp_ctx
= talloc_new(mem_ctx
);
171 if (tmp_ctx
== NULL
) {
175 irpc_handle
= irpc_binding_handle_by_name(tmp_ctx
, msg_ctx
,
178 if (irpc_handle
== NULL
) {
179 DEBUG(1,(__location__
": Unable to get binding handle for winbind\n"));
180 TALLOC_FREE(tmp_ctx
);
184 r
.in
.message
= *send_to_sam
;
187 * This seem to rely on the current IRPC implementation,
188 * which delivers the message in the _send function.
190 * TODO: we need a ONE_WAY IRPC handle and register
191 * a callback and wait for it to be triggered!
193 req
= dcerpc_winbind_SendToSam_r_send(tmp_ctx
,
198 /* we aren't interested in a reply */
200 TALLOC_FREE(tmp_ctx
);
205 send a message to the drepl server telling it to initiate a
206 REPL_SECRET getncchanges extended op to fetch the users secrets
208 static void auth_sam_trigger_repl_secret(TALLOC_CTX
*mem_ctx
,
209 struct imessaging_context
*msg_ctx
,
210 struct tevent_context
*event_ctx
,
211 struct ldb_dn
*user_dn
)
213 struct dcerpc_binding_handle
*irpc_handle
;
214 struct drepl_trigger_repl_secret r
;
215 struct tevent_req
*req
;
218 tmp_ctx
= talloc_new(mem_ctx
);
219 if (tmp_ctx
== NULL
) {
223 irpc_handle
= irpc_binding_handle_by_name(tmp_ctx
, msg_ctx
,
226 if (irpc_handle
== NULL
) {
227 DEBUG(1,(__location__
": Unable to get binding handle for dreplsrv\n"));
228 TALLOC_FREE(tmp_ctx
);
232 r
.in
.user_dn
= ldb_dn_get_linearized(user_dn
);
235 * This seem to rely on the current IRPC implementation,
236 * which delivers the message in the _send function.
238 * TODO: we need a ONE_WAY IRPC handle and register
239 * a callback and wait for it to be triggered!
241 req
= dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx
,
246 /* we aren't interested in a reply */
248 TALLOC_FREE(tmp_ctx
);
251 static const struct samr_Password
*hide_invalid_nthash(const struct samr_Password
*in
)
254 * This is the result of:
256 * E_md4hash("", zero_string_hash.hash);
258 static const struct samr_Password zero_string_hash
= {
260 0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
261 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0,
270 * Skip over any all-zero hashes in the history. No known software
271 * stores these but just to be sure
273 if (all_zero(in
->hash
, sizeof(in
->hash
))) {
278 * This looks odd, but the password_hash module in the past has written
279 * this in the rare situation where (somehow) we didn't have an old NT
280 * hash (one of the old LM-only set paths)
282 * mem_equal_const_time() is used to avoid a timing attack
283 * when comparing secret data in the server with this constant
286 if (mem_equal_const_time(in
->hash
, zero_string_hash
.hash
, 16)) {
294 * Check that a password is OK, and update badPwdCount if required.
297 static NTSTATUS
authsam_password_check_and_record(struct auth4_context
*auth_context
,
299 struct ldb_dn
*domain_dn
,
300 struct ldb_message
*msg
,
301 const struct auth_usersupplied_info
*user_info
,
302 DATA_BLOB
*user_sess_key
,
303 DATA_BLOB
*lm_sess_key
,
307 NTSTATUS auth_status
;
311 struct ldb_context
*sam_ctx
= auth_context
->sam_ctx
;
312 const char * const attrs
[] = { "pwdHistoryLength", NULL
};
313 struct ldb_message
*dom_msg
;
314 struct samr_Password
*nt_pwd
;
315 DATA_BLOB _aes_256_key
= data_blob_null
;
316 DATA_BLOB
*aes_256_key
= NULL
;
317 krb5_data _salt
= { .data
= NULL
, .length
= 0 };
318 krb5_data
*salt
= NULL
;
319 DATA_BLOB salt_data
= data_blob_null
;
320 struct smb_krb5_context
*smb_krb5_context
= NULL
;
321 const struct ldb_val
*sc_val
;
322 uint32_t userAccountControl
= 0;
323 uint32_t current_kvno
= 0;
328 time_ok
= dsdb_gmsa_current_time(sam_ctx
, &now
);
330 return NT_STATUS_INTERNAL_ERROR
;
333 tmp_ctx
= talloc_new(mem_ctx
);
334 if (tmp_ctx
== NULL
) {
335 return NT_STATUS_NO_MEMORY
;
339 * This call does more than what it appears to do, it also
340 * checks for the account lockout.
342 * It is done here so that all parts of Samba that read the
343 * password refuse to even operate on it if the account is
344 * locked out, to avoid mistakes like CVE-2013-4496.
346 nt_status
= samdb_result_passwords(tmp_ctx
, auth_context
->lp_ctx
,
348 if (!NT_STATUS_IS_OK(nt_status
)) {
349 TALLOC_FREE(tmp_ctx
);
353 userAccountControl
= ldb_msg_find_attr_as_uint(msg
,
354 "userAccountControl",
357 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
359 if (nt_pwd
== NULL
&& sc_val
== NULL
) {
360 if (samdb_rodc(auth_context
->sam_ctx
, &am_rodc
) == LDB_SUCCESS
&& am_rodc
) {
362 * we don't have passwords for this
363 * account. We are an RODC, and this account
364 * may be one for which we either are denied
365 * REPL_SECRET replication or we haven't yet
366 * done the replication. We return
367 * NT_STATUS_NOT_IMPLEMENTED which tells the
368 * auth code to try the next authentication
369 * mechanism. We also send a message to our
370 * drepl server to tell it to try and
371 * replicate the secrets for this account.
373 * TODO: Should we only trigger this is detected
374 * there's a chance that the password might be
375 * replicated, we should be able to detect this
376 * based on msDS-NeverRevealGroup.
378 auth_sam_trigger_repl_secret(auth_context
,
379 auth_context
->msg_ctx
,
380 auth_context
->event_ctx
,
382 TALLOC_FREE(tmp_ctx
);
383 return NT_STATUS_NOT_IMPLEMENTED
;
388 * If we don't have an NT password, pull a kerberos key
389 * instead for plaintext.
391 if (nt_pwd
== NULL
&&
393 user_info
->password_state
== AUTH_PASSWORD_PLAIN
)
395 krb5_error_code krb5_ret
;
397 krb5_ret
= smb_krb5_init_context(tmp_ctx
,
398 auth_context
->lp_ctx
,
401 DBG_ERR("Failed to setup krb5_context: %s!\n",
402 error_message(krb5_ret
));
403 return NT_STATUS_INTERNAL_ERROR
;
407 * Get the current salt from the record
410 krb5_ret
= dsdb_extract_aes_256_key(smb_krb5_context
->krb5_context
,
416 ¤t_kvno
, /* kvno_out */
420 aes_256_key
= &_aes_256_key
;
422 _salt
.data
= (char *)salt_data
.data
;
423 _salt
.length
= salt_data
.length
;
428 auth_status
= authsam_password_ok(auth_context
,
435 user_sess_key
, lm_sess_key
);
437 if (NT_STATUS_IS_OK(auth_status
)) {
438 if (user_sess_key
->data
) {
439 talloc_steal(mem_ctx
, user_sess_key
->data
);
441 if (lm_sess_key
->data
) {
442 talloc_steal(mem_ctx
, lm_sess_key
->data
);
444 TALLOC_FREE(tmp_ctx
);
447 *user_sess_key
= data_blob_null
;
448 *lm_sess_key
= data_blob_null
;
450 if (!NT_STATUS_EQUAL(auth_status
, NT_STATUS_WRONG_PASSWORD
)) {
451 TALLOC_FREE(tmp_ctx
);
456 * We only continue if this was a wrong password and we'll
457 * return NT_STATUS_WRONG_PASSWORD in most cases, except for a
458 * (default) 60 min grace period for previous NTLM password
461 /* pull the domain password property attributes */
462 ret
= dsdb_search_one(sam_ctx
, tmp_ctx
, &dom_msg
, domain_dn
, LDB_SCOPE_BASE
,
463 attrs
, 0, "objectClass=domain");
464 if (ret
== LDB_SUCCESS
) {
465 history_len
= ldb_msg_find_attr_as_uint(dom_msg
, "pwdHistoryLength", 0);
466 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
467 DEBUG(3,("Couldn't find domain %s: %s!\n",
468 ldb_dn_get_linearized(domain_dn
),
469 ldb_errstring(sam_ctx
)));
471 DEBUG(3,("error finding domain %s: %s!\n",
472 ldb_dn_get_linearized(domain_dn
),
473 ldb_errstring(sam_ctx
)));
476 for (i
= 1; i
< MIN(history_len
, 3); i
++) {
477 const struct samr_Password
*nt_history_pwd
= NULL
;
479 int allowed_period_mins
;
480 NTTIME allowed_period
;
483 /* Reset these variables back to starting as empty */
488 * Obtain the i'th old password from the NT password
489 * history for this user.
491 * We avoid issues with salts (which are not
492 * recorded for historical AES256 keys) by using the
493 * ntPwdHistory in preference.
495 nt_status
= samdb_result_passwords_from_history(tmp_ctx
,
496 auth_context
->lp_ctx
,
502 * Belts and braces: note that
503 * samdb_result_passwords_from_history() currently
504 * does not fail for missing attributes, it only sets
505 * nt_history_pwd = NULL, so "break" and fall down to
506 * the bad password count update if this happens
508 if (!NT_STATUS_IS_OK(nt_status
)) {
512 nt_history_pwd
= hide_invalid_nthash(nt_history_pwd
);
515 * We don't have an NT hash from the
516 * ntPwdHistory, but we can still perform the
517 * password check with the AES256
520 * However, this is the second preference as
521 * it will fail if the account was renamed
522 * prior to a password change (as we won't
523 * have the correct salt available to
524 * calculate the AES256 key).
527 if (nt_history_pwd
== NULL
&& sc_val
!= NULL
&&
528 user_info
->password_state
== AUTH_PASSWORD_PLAIN
&&
531 krb5_error_code krb5_ret
;
532 const uint32_t request_kvno
= current_kvno
- i
;
535 * Confirm we have a krb5_context set up
537 if (smb_krb5_context
== NULL
) {
539 * We get here if we had a unicodePwd
540 * for the current password, no
541 * ntPwdHistory, a valid previous
542 * Kerberos history AND are processing
545 * This really is a corner case so
546 * favour cleaner code over trying to
547 * allow for an old password. It is
548 * more likely this is just a new
551 * "break" out of the loop and fall down
552 * to the bad password update
558 * Get the current salt from the record
561 krb5_ret
= dsdb_extract_aes_256_key(smb_krb5_context
->krb5_context
,
566 &request_kvno
, /* kvno */
574 aes_256_key
= &_aes_256_key
;
576 _salt
.data
= (char *)salt_data
.data
;
577 _salt
.length
= salt_data
.length
;
580 } else if (nt_history_pwd
== NULL
) {
582 * If we don't find element 'i' in the
583 * ntPwdHistory and can not fall back to the
584 * kerberos hash, we won't find 'i+1' ...
589 auth_status
= authsam_password_ok(auth_context
, tmp_ctx
,
598 if (!NT_STATUS_IS_OK(auth_status
)) {
600 * If this was not a correct password, try the next
601 * one from the history
603 *user_sess_key
= data_blob_null
;
604 *lm_sess_key
= data_blob_null
;
610 * The authentication was OK, but not against
611 * the previous password, which is stored at index 1.
613 * We just return the original wrong password.
614 * This skips the update of the bad pwd count,
615 * because this is almost certainly user error
616 * (or automatic login on a computer using a cached
617 * password from before the password change),
620 TALLOC_FREE(tmp_ctx
);
621 return NT_STATUS_WRONG_PASSWORD
;
624 if (user_info
->flags
& USER_INFO_INTERACTIVE_LOGON
) {
626 * The authentication was OK against the previous password,
627 * but it's not a NTLM network authentication,
628 * LDAP simple bind or something similar.
630 * We just return the original wrong password.
631 * This skips the update of the bad pwd count,
632 * because this is almost certainly user error
633 * (or automatic login on a computer using a cached
634 * password from before the password change),
637 TALLOC_FREE(tmp_ctx
);
638 return NT_STATUS_WRONG_PASSWORD
;
642 * If the password was OK, it's a NTLM network authentication
643 * and it was the previous password.
645 * Now we see if it is within the grace period,
646 * so that we don't break cached sessions on other computers
647 * before the user can lock and unlock their other screens
648 * (resetting their cached password).
652 /* Is the account a Group Managed Service Account? */
653 is_gmsa
= dsdb_account_is_gmsa(sam_ctx
, msg
);
656 * For Group Managed Service Accounts, the previous
657 * password is allowed for five minutes after a password
660 allowed_period_mins
= gkdi_max_clock_skew_mins
;
663 * See http://support.microsoft.com/kb/906305
664 * OldPasswordAllowedPeriod ("old password allowed
665 * period") is specified in minutes. The default is 60.
667 allowed_period_mins
= lpcfg_old_password_allowed_period(
668 auth_context
->lp_ctx
);
671 * NTTIME uses 100ns units
673 allowed_period
= (NTTIME
) allowed_period_mins
*
675 pwdLastSet
= samdb_result_nttime(msg
, "pwdLastSet", 0);
677 if (now
< pwdLastSet
) {
681 * We just return the original wrong password.
682 * This skips the update of the bad pwd count,
683 * because this is almost certainly user error
684 * (or automatic login on a computer using a cached
685 * password from before the password change),
688 TALLOC_FREE(tmp_ctx
);
689 return NT_STATUS_WRONG_PASSWORD
;
692 if ((now
- pwdLastSet
) >= allowed_period
) {
694 * The allowed period is over.
696 * We just return the original wrong password.
697 * This skips the update of the bad pwd count,
698 * because this is almost certainly user error
699 * (or automatic login on a computer using a cached
700 * password from before the password change),
703 TALLOC_FREE(tmp_ctx
);
704 return NT_STATUS_WRONG_PASSWORD
;
708 * We finally allow the authentication with the
709 * previous password within the allowed period.
711 if (user_sess_key
->data
) {
712 talloc_steal(mem_ctx
, user_sess_key
->data
);
714 if (lm_sess_key
->data
) {
715 talloc_steal(mem_ctx
, lm_sess_key
->data
);
718 TALLOC_FREE(tmp_ctx
);
723 * If we are not in the allowed period or match an old password,
724 * we didn't return early. Now update the badPwdCount et al.
726 nt_status
= authsam_update_bad_pwd_count(auth_context
->sam_ctx
,
728 if (!NT_STATUS_IS_OK(nt_status
)) {
730 * We need to return the original
731 * NT_STATUS_WRONG_PASSWORD error, so there isn't
732 * anything more we can do than write something into
735 DEBUG(0, ("Failed to note bad password for user [%s]: %s\n",
736 user_info
->mapped
.account_name
,
737 nt_errstr(nt_status
)));
740 if (samdb_rodc(auth_context
->sam_ctx
, &am_rodc
) == LDB_SUCCESS
&& am_rodc
) {
741 *authoritative
= false;
744 TALLOC_FREE(tmp_ctx
);
746 if (NT_STATUS_IS_OK(nt_status
)) {
747 nt_status
= NT_STATUS_WRONG_PASSWORD
;
752 static NTSTATUS
authsam_check_netlogon_trust(TALLOC_CTX
*mem_ctx
,
753 struct ldb_context
*sam_ctx
,
754 struct loadparm_context
*lp_ctx
,
755 const struct auth_usersupplied_info
*user_info
,
756 const struct auth_user_info_dc
*user_info_dc
,
757 struct authn_audit_info
**server_audit_info_out
)
759 TALLOC_CTX
*tmp_ctx
= NULL
;
761 static const char *authn_policy_silo_attrs
[] = {
762 "msDS-AssignedAuthNPolicy",
763 "msDS-AssignedAuthNPolicySilo",
764 "objectClass", /* used to determine which set of policy
765 * attributes apply. */
769 const struct authn_server_policy
*authn_server_policy
= NULL
;
771 struct dom_sid_buf netlogon_trust_sid_buf
;
772 const char *netlogon_trust_sid_str
= NULL
;
773 struct ldb_dn
*netlogon_trust_dn
= NULL
;
774 struct ldb_message
*netlogon_trust_msg
= NULL
;
778 /* Have we established a secure channel? */
779 if (user_info
->netlogon_trust_account
.secure_channel_type
== SEC_CHAN_NULL
) {
783 if (!authn_policy_silos_and_policies_in_effect(sam_ctx
)) {
788 * We have established a secure channel, and we should have the machine
791 SMB_ASSERT(user_info
->netlogon_trust_account
.sid
!= NULL
);
793 tmp_ctx
= talloc_new(mem_ctx
);
794 if (tmp_ctx
== NULL
) {
795 return NT_STATUS_NO_MEMORY
;
798 netlogon_trust_sid_str
= dom_sid_str_buf(user_info
->netlogon_trust_account
.sid
,
799 &netlogon_trust_sid_buf
);
801 netlogon_trust_dn
= ldb_dn_new_fmt(tmp_ctx
, sam_ctx
,
803 netlogon_trust_sid_str
);
804 if (netlogon_trust_dn
== NULL
) {
805 talloc_free(tmp_ctx
);
806 return NT_STATUS_NO_MEMORY
;
810 * Look up the machine account to see if it has an applicable
811 * authentication policy.
813 ret
= dsdb_search_one(sam_ctx
,
818 authn_policy_silo_attrs
,
822 talloc_free(tmp_ctx
);
823 return dsdb_ldb_err_to_ntstatus(ret
);
826 ret
= authn_policy_server(sam_ctx
,
829 &authn_server_policy
);
831 talloc_free(tmp_ctx
);
832 return NT_STATUS_INTERNAL_ERROR
;
835 if (authn_server_policy
!= NULL
) {
836 struct authn_audit_info
*server_audit_info
= NULL
;
840 * An authentication policy applies to the machine
841 * account. Carry out the access check.
843 status
= authn_policy_authenticate_to_service(tmp_ctx
,
846 AUTHN_POLICY_AUTH_TYPE_NTLM
,
848 NULL
/* device_info */,
850 * It seems that claims go ignored for
851 * SamLogon (see SamLogonTests —
852 * test_samlogon_allowed_to_computer_silo).
854 (struct auth_claims
) {},
856 (struct authn_policy_flags
) {},
858 if (server_audit_info
!= NULL
) {
859 *server_audit_info_out
= talloc_move(mem_ctx
, &server_audit_info
);
861 if (!NT_STATUS_IS_OK(status
)) {
862 talloc_free(tmp_ctx
);
870 static NTSTATUS
authsam_authenticate(struct auth4_context
*auth_context
,
872 struct ldb_dn
*domain_dn
,
873 struct ldb_message
*msg
,
874 const struct auth_usersupplied_info
*user_info
,
875 const struct auth_user_info_dc
*user_info_dc
,
876 DATA_BLOB
*user_sess_key
, DATA_BLOB
*lm_sess_key
,
877 struct authn_audit_info
**client_audit_info_out
,
878 struct authn_audit_info
**server_audit_info_out
,
883 bool interactive
= (user_info
->password_state
== AUTH_PASSWORD_HASH
);
884 uint32_t acct_flags
= samdb_result_acct_flags(msg
, NULL
);
885 struct netr_SendToSamBase
*send_to_sam
= NULL
;
886 const struct authn_ntlm_client_policy
*authn_client_policy
= NULL
;
887 struct ldb_context
*sam_ctx
= auth_context
->sam_ctx
;
888 TALLOC_CTX
*tmp_ctx
= NULL
;
892 time_ok
= dsdb_gmsa_current_time(sam_ctx
, &now
);
894 return NT_STATUS_INTERNAL_ERROR
;
897 tmp_ctx
= talloc_new(mem_ctx
);
899 return NT_STATUS_NO_MEMORY
;
902 /* You can only do an interactive login to normal accounts */
903 if (user_info
->flags
& USER_INFO_INTERACTIVE_LOGON
) {
904 if (!(acct_flags
& ACB_NORMAL
)) {
905 TALLOC_FREE(tmp_ctx
);
906 return NT_STATUS_NO_SUCH_USER
;
908 if (acct_flags
& ACB_SMARTCARD_REQUIRED
) {
909 if (acct_flags
& ACB_DISABLED
) {
910 DEBUG(2,("authsam_authenticate: Account for user '%s' "
912 user_info
->mapped
.account_name
));
913 TALLOC_FREE(tmp_ctx
);
914 return NT_STATUS_ACCOUNT_DISABLED
;
916 DEBUG(2,("authsam_authenticate: Account for user '%s' "
917 "requires interactive smartcard logon.\n",
918 user_info
->mapped
.account_name
));
919 TALLOC_FREE(tmp_ctx
);
920 return NT_STATUS_SMARTCARD_LOGON_REQUIRED
;
924 /* See whether an authentication policy applies to the client. */
925 ret
= authn_policy_ntlm_client(auth_context
->sam_ctx
,
928 &authn_client_policy
);
930 TALLOC_FREE(tmp_ctx
);
931 return NT_STATUS_INTERNAL_ERROR
;
934 nt_status
= authn_policy_ntlm_apply_device_restriction(mem_ctx
,
936 client_audit_info_out
);
937 if (!NT_STATUS_IS_OK(nt_status
)) {
939 * As we didn’t get far enough to check the server policy, only
940 * the client policy will be referenced in the authentication
943 TALLOC_FREE(tmp_ctx
);
947 nt_status
= authsam_password_check_and_record(auth_context
, tmp_ctx
,
950 user_sess_key
, lm_sess_key
,
952 if (!NT_STATUS_IS_OK(nt_status
)) {
953 TALLOC_FREE(tmp_ctx
);
957 nt_status
= authsam_check_netlogon_trust(mem_ctx
,
958 auth_context
->sam_ctx
,
959 auth_context
->lp_ctx
,
962 server_audit_info_out
);
963 if (!NT_STATUS_IS_OK(nt_status
)) {
964 TALLOC_FREE(tmp_ctx
);
968 nt_status
= authsam_account_ok(tmp_ctx
, auth_context
->sam_ctx
,
970 user_info
->logon_parameters
,
973 user_info
->workstation_name
,
974 user_info
->mapped
.account_name
,
976 if (!NT_STATUS_IS_OK(nt_status
)) {
977 TALLOC_FREE(tmp_ctx
);
981 nt_status
= authsam_logon_success_accounting(auth_context
->sam_ctx
,
987 if (send_to_sam
!= NULL
) {
988 auth_sam_trigger_zero_password(tmp_ctx
,
989 auth_context
->msg_ctx
,
990 auth_context
->event_ctx
,
994 if (!NT_STATUS_IS_OK(nt_status
)) {
995 TALLOC_FREE(tmp_ctx
);
999 if (user_sess_key
&& user_sess_key
->data
) {
1000 talloc_steal(mem_ctx
, user_sess_key
->data
);
1002 if (lm_sess_key
&& lm_sess_key
->data
) {
1003 talloc_steal(mem_ctx
, lm_sess_key
->data
);
1006 TALLOC_FREE(tmp_ctx
);
1012 static NTSTATUS
authsam_check_password_internals(struct auth_method_context
*ctx
,
1013 TALLOC_CTX
*mem_ctx
,
1014 const struct auth_usersupplied_info
*user_info
,
1015 struct auth_user_info_dc
**user_info_dc
,
1016 struct authn_audit_info
**client_audit_info_out
,
1017 struct authn_audit_info
**server_audit_info_out
,
1018 bool *authoritative
)
1022 const char *account_name
= user_info
->mapped
.account_name
;
1023 struct ldb_message
*msg
;
1024 struct ldb_dn
*domain_dn
;
1025 DATA_BLOB user_sess_key
, lm_sess_key
;
1026 TALLOC_CTX
*tmp_ctx
;
1027 const char *p
= NULL
;
1028 struct auth_user_info_dc
*reparented
= NULL
;
1029 struct authn_audit_info
*client_audit_info
= NULL
;
1030 struct authn_audit_info
*server_audit_info
= NULL
;
1032 if (ctx
->auth_ctx
->sam_ctx
== NULL
) {
1033 DEBUG(0, ("No SAM available, cannot log in users\n"));
1034 return NT_STATUS_INVALID_SYSTEM_SERVICE
;
1037 if (!account_name
|| !*account_name
) {
1039 return NT_STATUS_NOT_IMPLEMENTED
;
1042 tmp_ctx
= talloc_new(mem_ctx
);
1044 return NT_STATUS_NO_MEMORY
;
1047 domain_dn
= ldb_get_default_basedn(ctx
->auth_ctx
->sam_ctx
);
1048 if (domain_dn
== NULL
) {
1049 talloc_free(tmp_ctx
);
1050 return NT_STATUS_NO_SUCH_DOMAIN
;
1054 * If we have not already mapped this user, then now is a good
1055 * time to do so, before we look it up. We used to do this
1056 * earlier, but in a multi-forest environment we want to do
1057 * this mapping at the final domain.
1059 * However, on the flip side we may have already mapped the
1060 * user if this was an LDAP simple bind, in which case we
1061 * really, really want to get back to exactly the same account
1062 * we got the DN for.
1064 if (!user_info
->cracknames_called
) {
1065 p
= strchr_m(account_name
, '@');
1068 * This is slightly nicer than double-indenting the
1075 const char *nt4_domain
= NULL
;
1076 const char *nt4_account
= NULL
;
1077 bool is_my_domain
= false;
1079 nt_status
= crack_name_to_nt4_name(mem_ctx
,
1080 ctx
->auth_ctx
->sam_ctx
,
1082 * DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON ?
1084 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
1086 &nt4_domain
, &nt4_account
);
1087 if (!NT_STATUS_IS_OK(nt_status
)) {
1088 talloc_free(tmp_ctx
);
1089 return NT_STATUS_NO_SUCH_USER
;
1092 is_my_domain
= lpcfg_is_mydomain(ctx
->auth_ctx
->lp_ctx
, nt4_domain
);
1093 if (!is_my_domain
) {
1095 * This is a user within our forest,
1096 * but in a different domain,
1097 * we're not authoritative
1099 talloc_free(tmp_ctx
);
1100 return NT_STATUS_NOT_IMPLEMENTED
;
1104 * Let's use the NT4 account name for the lookup.
1106 account_name
= nt4_account
;
1109 nt_status
= authsam_search_account(tmp_ctx
, ctx
->auth_ctx
->sam_ctx
, account_name
, domain_dn
, &msg
);
1110 if (!NT_STATUS_IS_OK(nt_status
)) {
1111 talloc_free(tmp_ctx
);
1115 nt_status
= authsam_make_user_info_dc(tmp_ctx
, ctx
->auth_ctx
->sam_ctx
,
1116 lpcfg_netbios_name(ctx
->auth_ctx
->lp_ctx
),
1117 lpcfg_sam_name(ctx
->auth_ctx
->lp_ctx
),
1118 lpcfg_sam_dnsname(ctx
->auth_ctx
->lp_ctx
),
1121 data_blob_null
, data_blob_null
,
1123 if (!NT_STATUS_IS_OK(nt_status
)) {
1124 talloc_free(tmp_ctx
);
1128 result
= dsdb_is_protected_user(ctx
->auth_ctx
->sam_ctx
,
1129 (*user_info_dc
)->sids
,
1130 (*user_info_dc
)->num_sids
);
1132 * We also consider an error result (a negative value) as denying the
1136 talloc_free(tmp_ctx
);
1137 return NT_STATUS_ACCOUNT_RESTRICTION
;
1140 nt_status
= authsam_authenticate(ctx
->auth_ctx
,
1151 if (client_audit_info
!= NULL
) {
1152 *client_audit_info_out
= talloc_move(mem_ctx
, &client_audit_info
);
1154 if (server_audit_info
!= NULL
) {
1155 *server_audit_info_out
= talloc_move(mem_ctx
, &server_audit_info
);
1157 if (!NT_STATUS_IS_OK(nt_status
)) {
1158 talloc_free(tmp_ctx
);
1162 (*user_info_dc
)->user_session_key
= data_blob_talloc(*user_info_dc
,
1164 user_sess_key
.length
);
1165 if (user_sess_key
.data
) {
1166 if ((*user_info_dc
)->user_session_key
.data
== NULL
) {
1167 TALLOC_FREE(tmp_ctx
);
1168 return NT_STATUS_NO_MEMORY
;
1172 (*user_info_dc
)->lm_session_key
= data_blob_talloc(*user_info_dc
,
1174 lm_sess_key
.length
);
1175 if (lm_sess_key
.data
) {
1176 if ((*user_info_dc
)->lm_session_key
.data
== NULL
) {
1177 TALLOC_FREE(tmp_ctx
);
1178 return NT_STATUS_NO_MEMORY
;
1183 * Release our handle to *user_info_dc. {client,server}_audit_info_out,
1184 * if non-NULL, becomes the new parent.
1186 reparented
= talloc_reparent(tmp_ctx
, mem_ctx
, *user_info_dc
);
1187 if (reparented
== NULL
) {
1188 talloc_free(tmp_ctx
);
1189 return NT_STATUS_INTERNAL_ERROR
;
1192 talloc_free(tmp_ctx
);
1194 return NT_STATUS_OK
;
1197 struct authsam_check_password_state
{
1198 struct auth_user_info_dc
*user_info_dc
;
1199 struct authn_audit_info
*client_audit_info
;
1200 struct authn_audit_info
*server_audit_info
;
1204 static struct tevent_req
*authsam_check_password_send(
1205 TALLOC_CTX
*mem_ctx
,
1206 struct tevent_context
*ev
,
1207 struct auth_method_context
*ctx
,
1208 const struct auth_usersupplied_info
*user_info
)
1210 struct tevent_req
*req
= NULL
;
1211 struct authsam_check_password_state
*state
= NULL
;
1214 req
= tevent_req_create(
1215 mem_ctx
, &state
, struct authsam_check_password_state
);
1220 * authsam_check_password_internals() sets this to false in
1221 * the rodc case, otherwise it leaves it untouched. Default to
1222 * "we're authoritative".
1224 state
->authoritative
= true;
1226 status
= authsam_check_password_internals(
1230 &state
->user_info_dc
,
1231 &state
->client_audit_info
,
1232 &state
->server_audit_info
,
1233 &state
->authoritative
);
1234 if (tevent_req_nterror(req
, status
)) {
1235 return tevent_req_post(req
, ev
);
1238 tevent_req_done(req
);
1239 return tevent_req_post(req
, ev
);
1242 static NTSTATUS
authsam_check_password_recv(
1243 struct tevent_req
*req
,
1244 TALLOC_CTX
*mem_ctx
,
1245 struct auth_user_info_dc
**interim_info
,
1246 const struct authn_audit_info
**client_audit_info
,
1247 const struct authn_audit_info
**server_audit_info
,
1248 bool *authoritative
)
1250 struct authsam_check_password_state
*state
= tevent_req_data(
1251 req
, struct authsam_check_password_state
);
1254 *authoritative
= state
->authoritative
;
1256 *client_audit_info
= talloc_reparent(state
, mem_ctx
, state
->client_audit_info
);
1257 state
->client_audit_info
= NULL
;
1259 *server_audit_info
= talloc_reparent(state
, mem_ctx
, state
->server_audit_info
);
1260 state
->server_audit_info
= NULL
;
1262 if (tevent_req_is_nterror(req
, &status
)) {
1263 tevent_req_received(req
);
1267 * Release our handle to state->user_info_dc.
1268 * {client,server}_audit_info, if non-NULL, becomes the new parent.
1270 *interim_info
= talloc_reparent(state
, mem_ctx
, state
->user_info_dc
);
1271 state
->user_info_dc
= NULL
;
1273 tevent_req_received(req
);
1274 return NT_STATUS_OK
;
1277 static NTSTATUS
authsam_ignoredomain_want_check(struct auth_method_context
*ctx
,
1278 TALLOC_CTX
*mem_ctx
,
1279 const struct auth_usersupplied_info
*user_info
)
1281 if (!user_info
->mapped
.account_name
|| !*user_info
->mapped
.account_name
) {
1282 return NT_STATUS_NOT_IMPLEMENTED
;
1285 return NT_STATUS_OK
;
1288 /****************************************************************************
1289 Check SAM security (above) but with a few extra checks.
1290 ****************************************************************************/
1291 static NTSTATUS
authsam_want_check(struct auth_method_context
*ctx
,
1292 TALLOC_CTX
*mem_ctx
,
1293 const struct auth_usersupplied_info
*user_info
)
1295 const char *effective_domain
= user_info
->mapped
.domain_name
;
1296 bool is_local_name
= false;
1297 bool is_my_domain
= false;
1298 const char *p
= NULL
;
1299 struct dsdb_trust_routing_table
*trt
= NULL
;
1300 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
1303 if (!user_info
->mapped
.account_name
|| !*user_info
->mapped
.account_name
) {
1304 return NT_STATUS_NOT_IMPLEMENTED
;
1307 if (effective_domain
== NULL
) {
1308 effective_domain
= "";
1311 is_local_name
= lpcfg_is_myname(ctx
->auth_ctx
->lp_ctx
,
1314 /* check whether or not we service this domain/workgroup name */
1315 switch (lpcfg_server_role(ctx
->auth_ctx
->lp_ctx
)) {
1316 case ROLE_STANDALONE
:
1317 return NT_STATUS_OK
;
1319 case ROLE_DOMAIN_MEMBER
:
1320 if (is_local_name
) {
1321 return NT_STATUS_OK
;
1324 DBG_DEBUG("%s is not one of my local names (DOMAIN_MEMBER)\n",
1326 return NT_STATUS_NOT_IMPLEMENTED
;
1328 case ROLE_ACTIVE_DIRECTORY_DC
:
1333 DBG_ERR("lpcfg_server_role() has an undefined value\n");
1334 return NT_STATUS_INVALID_SERVER_STATE
;
1338 * Now we handle the AD DC case...
1341 is_my_domain
= lpcfg_is_my_domain_or_realm(ctx
->auth_ctx
->lp_ctx
,
1344 return NT_STATUS_OK
;
1347 if (user_info
->cracknames_called
) {
1349 * The caller already did a cracknames call.
1351 DBG_DEBUG("%s is not own domain name (DC)\n",
1353 return NT_STATUS_NOT_IMPLEMENTED
;
1356 if (!strequal(effective_domain
, "")) {
1357 DBG_DEBUG("%s is not own domain name (DC)\n",
1359 return NT_STATUS_NOT_IMPLEMENTED
;
1362 p
= strchr_m(user_info
->mapped
.account_name
, '@');
1365 * An empty to domain name should be handled
1366 * as the local domain name.
1368 return NT_STATUS_OK
;
1371 effective_domain
= p
+ 1;
1372 is_my_domain
= lpcfg_is_my_domain_or_realm(ctx
->auth_ctx
->lp_ctx
,
1375 return NT_STATUS_OK
;
1378 if (strequal(effective_domain
, "")) {
1379 DBG_DEBUG("authsam_check_password: upn without realm (DC)\n");
1380 return NT_STATUS_NOT_IMPLEMENTED
;
1384 * as last option we check the routing table if the
1385 * domain is within our forest.
1387 status
= dsdb_trust_routing_table_load(ctx
->auth_ctx
->sam_ctx
,
1389 if (!NT_STATUS_IS_OK(status
)) {
1390 DBG_ERR("authsam_check_password: dsdb_trust_routing_table_load() %s\n",
1395 tdo
= dsdb_trust_routing_by_name(trt
, effective_domain
);
1397 DBG_DEBUG("%s is not a known TLN (DC)\n",
1400 return NT_STATUS_NOT_IMPLEMENTED
;
1403 if (!(tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
)) {
1404 DBG_DEBUG("%s is not a TLN in our forest (DC)\n",
1407 return NT_STATUS_NOT_IMPLEMENTED
;
1411 * This principal is within our forest.
1412 * we'll later do a crack_name_to_nt4_name()
1413 * to check if it's in our domain.
1416 return NT_STATUS_OK
;
1419 static const struct auth_operations sam_ignoredomain_ops
= {
1420 .name
= "sam_ignoredomain",
1421 .want_check
= authsam_ignoredomain_want_check
,
1422 .check_password_send
= authsam_check_password_send
,
1423 .check_password_recv
= authsam_check_password_recv
,
1426 static const struct auth_operations sam_ops
= {
1428 .want_check
= authsam_want_check
,
1429 .check_password_send
= authsam_check_password_send
,
1430 .check_password_recv
= authsam_check_password_recv
,
1433 _PUBLIC_ NTSTATUS
auth4_sam_init(TALLOC_CTX
*);
1434 _PUBLIC_ NTSTATUS
auth4_sam_init(TALLOC_CTX
*ctx
)
1438 ret
= auth_register(ctx
, &sam_ops
);
1439 if (!NT_STATUS_IS_OK(ret
)) {
1440 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
1444 ret
= auth_register(ctx
, &sam_ignoredomain_ops
);
1445 if (!NT_STATUS_IS_OK(ret
)) {
1446 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));