NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx, bool lanman_auth, bool ntlm_auth, uint32_t logon_parameters, const DATA_BLOB *challenge, const DATA_BLOB *lm_response, const DATA_BLOB *nt_response, const char *username, const char *client_username, const char *client_domain, const struct samr_Password *stored_lanman, const struct samr_Password *stored_nt, DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key) { const static uint8_t zeros[8]; DATA_BLOB tmp_sess_key; const char *upper_client_domain = NULL; if (client_domain != NULL) { upper_client_domain = talloc_strdup_upper(mem_ctx, client_domain); if (upper_client_domain == NULL) { return NT_STATUS_NO_MEMORY; } } if (stored_nt == NULL) { DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n", username)); } *lm_sess_key = data_blob(NULL, 0); *user_sess_key = data_blob(NULL, 0); /* Check for cleartext netlogon. Used by Exchange 5.5. */ if ((logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_ALLOWED) && challenge->length == sizeof(zeros) && (memcmp(challenge->data, zeros, challenge->length) == 0 )) { struct samr_Password client_nt; struct samr_Password client_lm; char *unix_pw = NULL; bool lm_ok; size_t converted_size = 0; DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n", username)); mdfour(client_nt.hash, nt_response->data, nt_response->length); if (lm_response->length && (convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX, lm_response->data, lm_response->length, (void *)&unix_pw, &converted_size))) { if (E_deshash(unix_pw, client_lm.hash)) { lm_ok = true; } else { lm_ok = false; } } else { lm_ok = false; } return hash_password_check(mem_ctx, lanman_auth, lm_ok ? &client_lm : NULL, nt_response->length ? &client_nt : NULL, username, stored_lanman, stored_nt); } if (nt_response->length != 0 && nt_response->length < 24) { DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n", (unsigned long)nt_response->length, username)); } if (nt_response->length > 24 && stored_nt) { /* We have the NT MD4 hash challenge available - see if we can use it */ DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain ? client_domain : "<NULL>")); if (smb_pwd_check_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, client_domain, user_sess_key)) { if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", upper_client_domain ? upper_client_domain : "<NULL>")); if (smb_pwd_check_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, upper_client_domain, user_sess_key)) { if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n")); if (smb_pwd_check_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, "", user_sess_key)) { if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n")); } } else if (nt_response->length == 24 && stored_nt) { if (ntlm_auth) { /* We have the NT MD4 hash challenge available - see if we can use it (ie. does it exist in the smbpasswd file). */ DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n")); if (smb_pwd_check_ntlmv1(mem_ctx, nt_response, stored_nt->hash, challenge, user_sess_key)) { /* The LM session key for this response is not very secure, so use it only if we otherwise allow LM authentication */ if (lanman_auth && stored_lanman) { *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else { DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n", username)); /* no return, because we might pick up LMv2 in the LM field */ } } if (lm_response->length == 0) { DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } if (lm_response->length < 24) { DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n", (unsigned long)nt_response->length, username)); return NT_STATUS_WRONG_PASSWORD; } if (!lanman_auth) { DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n", username)); } else if (!stored_lanman) { DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n", username)); } else if (strchr_m(username, '@')) { DEBUG(3,("ntlm_password_check: NO LanMan password allowed for username@realm logins (user: %s)\n", username)); } else { DEBUG(4,("ntlm_password_check: Checking LM password\n")); if (smb_pwd_check_ntlmv1(mem_ctx, lm_response, stored_lanman->hash, challenge, NULL)) { /* The session key for this response is still very odd. It not very secure, so use it only if we otherwise allow LM authentication */ if (lanman_auth && stored_lanman) { uint8_t first_8_lm_hash[16]; memcpy(first_8_lm_hash, stored_lanman->hash, 8); memset(first_8_lm_hash + 8, '\0', 8); *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16); *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8); } return NT_STATUS_OK; } } if (!stored_nt) { DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username)); return NT_STATUS_WRONG_PASSWORD; } /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes. - related to Win9X, legacy NAS pass-though authentication */ DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain ? client_domain : "<NULL>")); if (smb_pwd_check_ntlmv2(mem_ctx, lm_response, stored_nt->hash, challenge, client_username, client_domain, &tmp_sess_key)) { if (nt_response->length > 24) { /* If NTLMv2 authentication has preceded us * (even if it failed), then use the session * key from that. See the RPC-SAMLOGON * torture test */ smb_sess_key_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, client_domain, user_sess_key); } else { /* Otherwise, use the LMv2 session key */ *user_sess_key = tmp_sess_key; } if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", upper_client_domain ? upper_client_domain : "<NULL>")); if (smb_pwd_check_ntlmv2(mem_ctx, lm_response, stored_nt->hash, challenge, client_username, upper_client_domain, &tmp_sess_key)) { if (nt_response->length > 24) { /* If NTLMv2 authentication has preceded us * (even if it failed), then use the session * key from that. See the RPC-SAMLOGON * torture test */ smb_sess_key_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, upper_client_domain, user_sess_key); } else { /* Otherwise, use the LMv2 session key */ *user_sess_key = tmp_sess_key; } if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n")); if (smb_pwd_check_ntlmv2(mem_ctx, lm_response, stored_nt->hash, challenge, client_username, "", &tmp_sess_key)) { if (nt_response->length > 24) { /* If NTLMv2 authentication has preceded us * (even if it failed), then use the session * key from that. See the RPC-SAMLOGON * torture test */ smb_sess_key_ntlmv2(mem_ctx, nt_response, stored_nt->hash, challenge, client_username, "", user_sess_key); } else { /* Otherwise, use the LMv2 session key */ *user_sess_key = tmp_sess_key; } if (user_sess_key->length) { *lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length)); } return NT_STATUS_OK; } /* Apparently NT accepts NT responses in the LM field - I think this is related to Win9X pass-though authentication */ DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n")); if (ntlm_auth) { if (smb_pwd_check_ntlmv1(mem_ctx, lm_response, stored_nt->hash, challenge, NULL)) { /* The session key for this response is still very odd. It not very secure, so use it only if we otherwise allow LM authentication */ if (lanman_auth && stored_lanman) { uint8_t first_8_lm_hash[16]; memcpy(first_8_lm_hash, stored_lanman->hash, 8); memset(first_8_lm_hash + 8, '\0', 8); *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16); *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8); } return NT_STATUS_OK; } DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username)); } else { DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username)); } /* Try and match error codes */ if (strchr_m(username, '@')) { return NT_STATUS_NOT_FOUND; } return NT_STATUS_WRONG_PASSWORD; }
NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx, const DATA_BLOB *challenge, const DATA_BLOB *lm_response, const DATA_BLOB *nt_response, const DATA_BLOB *lm_interactive_pwd, const DATA_BLOB *nt_interactive_pwd, const char *username, const char *client_username, const char *client_domain, const uint8 *lm_pw, const uint8 *nt_pw, DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key) { static const unsigned char zeros[8]; if (nt_pw == NULL) { DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n", username)); } if (nt_interactive_pwd && nt_interactive_pwd->length && nt_pw) { if (nt_interactive_pwd->length != 16) { DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_pwd->length, username)); return NT_STATUS_WRONG_PASSWORD; } if (memcmp(nt_interactive_pwd->data, nt_pw, 16) == 0) { if (user_sess_key) { *user_sess_key = data_blob(NULL, 16); SMBsesskeygen_ntv1(nt_pw, NULL, user_sess_key->data); } return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else if (lm_interactive_pwd && lm_interactive_pwd->length && lm_pw) { if (lm_interactive_pwd->length != 16) { DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_pwd->length, username)); return NT_STATUS_WRONG_PASSWORD; } if (!lp_lanman_auth()) { DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n", username)); return NT_STATUS_WRONG_PASSWORD; } if (memcmp(lm_interactive_pwd->data, lm_pw, 16) == 0) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } /* Check for cleartext netlogon. Used by Exchange 5.5. */ if (challenge->length == sizeof(zeros) && (memcmp(challenge->data, zeros, challenge->length) == 0 )) { DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n", username)); if (nt_pw && nt_response->length) { unsigned char pwhash[16]; mdfour(pwhash, nt_response->data, nt_response->length); if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else if (!lp_lanman_auth()) { DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n", username)); } else if (lm_pw && lm_response->length) { uchar dospwd[14]; uchar p16[16]; ZERO_STRUCT(dospwd); memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd))); /* Only the fisrt 14 chars are considered, password need not be null terminated. */ /* we *might* need to upper-case the string here */ E_P16((const unsigned char *)dospwd, p16); if (memcmp(p16, lm_pw, sizeof(p16)) == 0) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else { DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username)); return NT_STATUS_WRONG_PASSWORD; } } if (nt_response->length != 0 && nt_response->length < 24) { DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n", (unsigned long)nt_response->length, username)); } if (nt_response->length >= 24 && nt_pw) { if (nt_response->length > 24) { /* We have the NT MD4 hash challenge available - see if we can use it */ DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain)); if (smb_pwd_check_ntlmv2( nt_response, nt_pw, challenge, client_username, client_domain, False, user_sess_key)) { return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain)); if (smb_pwd_check_ntlmv2( nt_response, nt_pw, challenge, client_username, client_domain, True, user_sess_key)) { return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n")); if (smb_pwd_check_ntlmv2( nt_response, nt_pw, challenge, client_username, "", False, user_sess_key)) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n")); return NT_STATUS_WRONG_PASSWORD; } } if (lp_ntlm_auth()) { /* We have the NT MD4 hash challenge available - see if we can use it (ie. does it exist in the smbpasswd file). */ DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n")); if (smb_pwd_check_ntlmv1(nt_response, nt_pw, challenge, user_sess_key)) { /* The LM session key for this response is not very secure, so use it only if we otherwise allow LM authentication */ if (lp_lanman_auth() && lm_pw) { uint8 first_8_lm_hash[16]; memcpy(first_8_lm_hash, lm_pw, 8); memset(first_8_lm_hash + 8, '\0', 8); if (lm_sess_key) { *lm_sess_key = data_blob(first_8_lm_hash, 16); } } return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else { DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n", username)); /* no return, becouse we might pick up LMv2 in the LM field */ } } if (lm_response->length == 0) { DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } if (lm_response->length < 24) { DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n", (unsigned long)nt_response->length, username)); return NT_STATUS_WRONG_PASSWORD; } if (!lp_lanman_auth()) { DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n", username)); } else if (!lm_pw) { DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n", username)); } else { DEBUG(4,("ntlm_password_check: Checking LM password\n")); if (smb_pwd_check_ntlmv1(lm_response, lm_pw, challenge, NULL)) { uint8 first_8_lm_hash[16]; memcpy(first_8_lm_hash, lm_pw, 8); memset(first_8_lm_hash + 8, '\0', 8); if (user_sess_key) { *user_sess_key = data_blob(first_8_lm_hash, 16); } if (lm_sess_key) { *lm_sess_key = data_blob(first_8_lm_hash, 16); } return NT_STATUS_OK; } } if (!nt_pw) { DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username)); return NT_STATUS_WRONG_PASSWORD; } /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes. - related to Win9X, legacy NAS pass-though authentication */ DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain)); if (smb_pwd_check_ntlmv2( lm_response, nt_pw, challenge, client_username, client_domain, False, NULL)) { return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain)); if (smb_pwd_check_ntlmv2( lm_response, nt_pw, challenge, client_username, client_domain, True, NULL)) { return NT_STATUS_OK; } DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n")); if (smb_pwd_check_ntlmv2( lm_response, nt_pw, challenge, client_username, "", False, NULL)) { return NT_STATUS_OK; } /* Apparently NT accepts NT responses in the LM field - I think this is related to Win9X pass-though authentication */ DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n")); if (lp_ntlm_auth()) { if (smb_pwd_check_ntlmv1(lm_response, nt_pw, challenge, NULL)) { /* The session key for this response is still very odd. It not very secure, so use it only if we otherwise allow LM authentication */ if (lp_lanman_auth() && lm_pw) { uint8 first_8_lm_hash[16]; memcpy(first_8_lm_hash, lm_pw, 8); memset(first_8_lm_hash + 8, '\0', 8); if (user_sess_key) { *user_sess_key = data_blob(first_8_lm_hash, 16); } if (lm_sess_key) { *lm_sess_key = data_blob(first_8_lm_hash, 16); } } return NT_STATUS_OK; } DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username)); } else { DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username)); } return NT_STATUS_WRONG_PASSWORD; }