2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004
5 Copyright (C) Gerald Carter 2003
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/crypto/crypto.h"
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 Core of smb password checking routine.
31 ****************************************************************************/
33 static BOOL smb_pwd_check_ntlmv1(TALLOC_CTX *mem_ctx,
34 const DATA_BLOB *nt_response,
35 const uint8_t *part_passwd,
36 const DATA_BLOB *sec_blob,
37 DATA_BLOB *user_sess_key)
39 /* Finish the encryption of part_passwd. */
42 if (part_passwd == NULL) {
43 DEBUG(10,("No password set - DISALLOWING access\n"));
44 /* No password set - always false ! */
48 if (sec_blob->length != 8) {
49 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n",
50 (unsigned long)sec_blob->length));
54 if (nt_response->length != 24) {
55 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n",
56 (unsigned long)nt_response->length));
60 SMBOWFencrypt(part_passwd, sec_blob->data, p24);
61 if (user_sess_key != NULL) {
62 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
63 SMBsesskeygen_ntv1(part_passwd, user_sess_key->data);
68 DEBUG(100,("Part password (P16) was |\n"));
69 dump_data(100, part_passwd, 16);
70 DEBUGADD(100,("Password from client was |\n"));
71 dump_data(100, nt_response->data, nt_response->length);
72 DEBUGADD(100,("Given challenge was |\n"));
73 dump_data(100, sec_blob->data, sec_blob->length);
74 DEBUGADD(100,("Value from encryption was |\n"));
75 dump_data(100, p24, 24);
77 return (memcmp(p24, nt_response->data, 24) == 0);
80 /****************************************************************************
81 Core of smb password checking routine. (NTLMv2, LMv2)
82 Note: The same code works with both NTLMv2 and LMv2.
83 ****************************************************************************/
85 static BOOL smb_pwd_check_ntlmv2(TALLOC_CTX *mem_ctx,
86 const DATA_BLOB *ntv2_response,
87 const uint8_t *part_passwd,
88 const DATA_BLOB *sec_blob,
89 const char *user, const char *domain,
90 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
91 DATA_BLOB *user_sess_key)
93 /* Finish the encryption of part_passwd. */
95 uint8_t value_from_encryption[16];
96 uint8_t client_response[16];
97 DATA_BLOB client_key_data;
99 if (part_passwd == NULL) {
100 DEBUG(10,("No password set - DISALLOWING access\n"));
101 /* No password set - always False */
105 if (sec_blob->length != 8) {
106 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
107 (unsigned long)sec_blob->length));
111 if (ntv2_response->length < 24) {
112 /* We MUST have more than 16 bytes, or the stuff below will go
113 crazy. No known implementation sends less than the 24 bytes
114 for LMv2, let alone NTLMv2. */
115 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
116 (unsigned long)ntv2_response->length));
120 client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16);
122 todo: should we be checking this for anything? We can't for LMv2,
123 but for NTLMv2 it is meant to contain the current time etc.
126 memcpy(client_response, ntv2_response->data, sizeof(client_response));
128 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
132 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
133 if (user_sess_key != NULL) {
134 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
135 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
139 DEBUG(100,("Part password (P16) was |\n"));
140 dump_data(100, part_passwd, 16);
141 DEBUGADD(100,("Password from client was |\n"));
142 dump_data(100, ntv2_response->data, ntv2_response->length);
143 DEBUGADD(100,("Variable data from client was |\n"));
144 dump_data(100, client_key_data.data, client_key_data.length);
145 DEBUGADD(100,("Given challenge was |\n"));
146 dump_data(100, sec_blob->data, sec_blob->length);
147 DEBUGADD(100,("Value from encryption was |\n"));
148 dump_data(100, value_from_encryption, 16);
150 data_blob_clear_free(&client_key_data);
151 return (memcmp(value_from_encryption, client_response, 16) == 0);
155 * Check a challenge-response password against the value of the NT or
158 * @param mem_ctx talloc context
159 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
160 * @param nt_response 'unicode' NT response to the challenge, or unicode password
161 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
162 * @param username internal Samba username, for log messages
163 * @param client_username username the client used
164 * @param client_domain domain name the client used (may be mapped)
165 * @param nt_pw MD4 unicode password from our passdb or similar
166 * @param lm_pw LANMAN ASCII password from our passdb or similar
167 * @param user_sess_key User session key
168 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
171 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
172 const DATA_BLOB *challenge,
173 const DATA_BLOB *lm_response,
174 const DATA_BLOB *nt_response,
175 const DATA_BLOB *lm_interactive_password,
176 const DATA_BLOB *nt_interactive_password,
177 const char *username,
178 const char *client_username,
179 const char *client_domain,
180 const uint8_t *lm_pw, const uint8_t *nt_pw,
181 DATA_BLOB *user_sess_key,
182 DATA_BLOB *lm_sess_key)
184 static const uint8_t zeros[8];
186 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
190 if (nt_interactive_password && nt_interactive_password->length && nt_pw) {
191 if (nt_interactive_password->length != 16) {
192 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_password->length,
194 return NT_STATUS_WRONG_PASSWORD;
197 if (memcmp(nt_interactive_password->data, nt_pw, 16) == 0) {
199 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
200 SMBsesskeygen_ntv1(nt_pw, user_sess_key->data);
204 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
206 return NT_STATUS_WRONG_PASSWORD;
209 } else if (lm_interactive_password && lm_interactive_password->length && lm_pw) {
210 if (lm_interactive_password->length != 16) {
211 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_password->length,
213 return NT_STATUS_WRONG_PASSWORD;
216 if (!lp_lanman_auth()) {
217 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
219 return NT_STATUS_WRONG_PASSWORD;
222 if (memcmp(lm_interactive_password->data, lm_pw, 16) == 0) {
225 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
227 return NT_STATUS_WRONG_PASSWORD;
231 /* Check for cleartext netlogon. Used by Exchange 5.5. */
232 if (challenge->length == sizeof(zeros) &&
233 (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
235 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
237 if (nt_pw && nt_response->length) {
239 mdfour(pwhash, nt_response->data, nt_response->length);
240 if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
243 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
245 return NT_STATUS_WRONG_PASSWORD;
248 } else if (!lp_lanman_auth()) {
249 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
252 } else if (lm_pw && lm_response->length) {
257 memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
258 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
260 /* we *might* need to upper-case the string here */
261 E_P16((const uint8_t *)dospwd, p16);
263 if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
266 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
268 return NT_STATUS_WRONG_PASSWORD;
271 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
272 return NT_STATUS_WRONG_PASSWORD;
276 if (nt_response->length != 0 && nt_response->length < 24) {
277 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
278 (unsigned long)nt_response->length, username));
281 if (nt_response->length >= 24 && nt_pw) {
282 if (nt_response->length > 24) {
283 /* We have the NT MD4 hash challenge available - see if we can
286 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
287 if (smb_pwd_check_ntlmv2(mem_ctx,
297 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
298 if (smb_pwd_check_ntlmv2(mem_ctx,
308 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
309 if (smb_pwd_check_ntlmv2(mem_ctx,
318 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
319 return NT_STATUS_WRONG_PASSWORD;
323 if (lp_ntlm_auth()) {
324 /* We have the NT MD4 hash challenge available - see if we can
325 use it (ie. does it exist in the smbpasswd file).
327 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
328 if (smb_pwd_check_ntlmv1(mem_ctx,
332 /* The LM session key for this response is not very secure,
333 so use it only if we otherwise allow LM authentication */
335 if (lp_lanman_auth() && lm_pw) {
336 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
340 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
342 return NT_STATUS_WRONG_PASSWORD;
345 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
347 /* no return, becouse we might pick up LMv2 in the LM field */
351 if (lm_response->length == 0) {
352 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
354 return NT_STATUS_WRONG_PASSWORD;
357 if (lm_response->length < 24) {
358 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
359 (unsigned long)nt_response->length, username));
360 return NT_STATUS_WRONG_PASSWORD;
363 if (!lp_lanman_auth()) {
364 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
367 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
370 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
371 if (smb_pwd_check_ntlmv1(mem_ctx,
375 /* The session key for this response is still very odd.
376 It not very secure, so use it only if we otherwise
377 allow LM authentication */
379 if (lp_lanman_auth() && lm_pw) {
380 uint8_t first_8_lm_hash[16];
381 memcpy(first_8_lm_hash, lm_pw, 8);
382 memset(first_8_lm_hash + 8, '\0', 8);
383 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
384 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
391 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
392 return NT_STATUS_WRONG_PASSWORD;
395 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
396 - related to Win9X, legacy NAS pass-though authentication
398 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
399 if (smb_pwd_check_ntlmv2(mem_ctx,
409 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain));
410 if (smb_pwd_check_ntlmv2(mem_ctx,
420 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
421 if (smb_pwd_check_ntlmv2(mem_ctx,
431 /* Apparently NT accepts NT responses in the LM field
432 - I think this is related to Win9X pass-though authentication
434 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
435 if (lp_ntlm_auth()) {
436 if (smb_pwd_check_ntlmv1(mem_ctx,
440 /* The session key for this response is still very odd.
441 It not very secure, so use it only if we otherwise
442 allow LM authentication */
444 if (lp_lanman_auth() && lm_pw) {
445 uint8_t first_8_lm_hash[16];
446 memcpy(first_8_lm_hash, lm_pw, 8);
447 memset(first_8_lm_hash + 8, '\0', 8);
448 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
449 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
453 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
455 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
457 return NT_STATUS_WRONG_PASSWORD;