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);
63 DEBUG(100,("Part password (P16) was |\n"));
64 dump_data(100, part_passwd, 16);
65 DEBUGADD(100,("Password from client was |\n"));
66 dump_data(100, nt_response->data, nt_response->length);
67 DEBUGADD(100,("Given challenge was |\n"));
68 dump_data(100, sec_blob->data, sec_blob->length);
69 DEBUGADD(100,("Value from encryption was |\n"));
70 dump_data(100, p24, 24);
72 if (memcmp(p24, nt_response->data, 24) == 0) {
73 if (user_sess_key != NULL) {
74 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
75 SMBsesskeygen_ntv1(part_passwd, user_sess_key->data);
82 /****************************************************************************
83 Core of smb password checking routine. (NTLMv2, LMv2)
84 Note: The same code works with both NTLMv2 and LMv2.
85 ****************************************************************************/
87 static BOOL smb_pwd_check_ntlmv2(TALLOC_CTX *mem_ctx,
88 const DATA_BLOB *ntv2_response,
89 const uint8_t *part_passwd,
90 const DATA_BLOB *sec_blob,
91 const char *user, const char *domain,
92 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
93 DATA_BLOB *user_sess_key)
95 /* Finish the encryption of part_passwd. */
97 uint8_t value_from_encryption[16];
98 DATA_BLOB client_key_data;
100 if (part_passwd == NULL) {
101 DEBUG(10,("No password set - DISALLOWING access\n"));
102 /* No password set - always False */
106 if (sec_blob->length != 8) {
107 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
108 (unsigned long)sec_blob->length));
112 if (ntv2_response->length < 24) {
113 /* We MUST have more than 16 bytes, or the stuff below will go
114 crazy. No known implementation sends less than the 24 bytes
115 for LMv2, let alone NTLMv2. */
116 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
117 (unsigned long)ntv2_response->length));
121 client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16);
123 todo: should we be checking this for anything? We can't for LMv2,
124 but for NTLMv2 it is meant to contain the current time etc.
127 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
131 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
134 DEBUG(100,("Part password (P16) was |\n"));
135 dump_data(100, part_passwd, 16);
136 DEBUGADD(100,("Password from client was |\n"));
137 dump_data(100, ntv2_response->data, ntv2_response->length);
138 DEBUGADD(100,("Variable data from client was |\n"));
139 dump_data(100, client_key_data.data, client_key_data.length);
140 DEBUGADD(100,("Given challenge was |\n"));
141 dump_data(100, sec_blob->data, sec_blob->length);
142 DEBUGADD(100,("Value from encryption was |\n"));
143 dump_data(100, value_from_encryption, 16);
145 data_blob_clear_free(&client_key_data);
146 if (memcmp(value_from_encryption, ntv2_response->data, 16) == 0) {
147 if (user_sess_key != NULL) {
148 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
149 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
156 /****************************************************************************
157 Core of smb password checking routine. (NTLMv2, LMv2)
158 Note: The same code works with both NTLMv2 and LMv2.
159 ****************************************************************************/
161 static BOOL smb_sess_key_ntlmv2(TALLOC_CTX *mem_ctx,
162 const DATA_BLOB *ntv2_response,
163 const uint8_t *part_passwd,
164 const DATA_BLOB *sec_blob,
165 const char *user, const char *domain,
166 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
167 DATA_BLOB *user_sess_key)
169 /* Finish the encryption of part_passwd. */
171 uint8_t value_from_encryption[16];
172 DATA_BLOB client_key_data;
174 if (part_passwd == NULL) {
175 DEBUG(10,("No password set - DISALLOWING access\n"));
176 /* No password set - always False */
180 if (sec_blob->length != 8) {
181 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect challenge size (%lu)\n",
182 (unsigned long)sec_blob->length));
186 if (ntv2_response->length < 24) {
187 /* We MUST have more than 16 bytes, or the stuff below will go
188 crazy. No known implementation sends less than the 24 bytes
189 for LMv2, let alone NTLMv2. */
190 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect password length (%lu)\n",
191 (unsigned long)ntv2_response->length));
195 client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16);
197 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
201 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
202 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
203 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
208 * Check a challenge-response password against the value of the NT or
211 * @param mem_ctx talloc context
212 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
213 * @param nt_response 'unicode' NT response to the challenge, or unicode password
214 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
215 * @param username internal Samba username, for log messages
216 * @param client_username username the client used
217 * @param client_domain domain name the client used (may be mapped)
218 * @param nt_pw MD4 unicode password from our passdb or similar
219 * @param lm_pw LANMAN ASCII password from our passdb or similar
220 * @param user_sess_key User session key
221 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
224 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
225 const DATA_BLOB *challenge,
226 const DATA_BLOB *lm_response,
227 const DATA_BLOB *nt_response,
228 const DATA_BLOB *lm_interactive_password,
229 const DATA_BLOB *nt_interactive_password,
230 const char *username,
231 const char *client_username,
232 const char *client_domain,
233 const uint8_t *lm_pw, const uint8_t *nt_pw,
234 DATA_BLOB *user_sess_key,
235 DATA_BLOB *lm_sess_key)
237 static const uint8_t zeros[8];
238 DATA_BLOB tmp_sess_key;
241 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
246 *lm_sess_key = data_blob(NULL, 0);
249 *user_sess_key = data_blob(NULL, 0);
252 if (nt_interactive_password && nt_interactive_password->length && nt_pw) {
253 if (nt_interactive_password->length != 16) {
254 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_password->length,
256 return NT_STATUS_WRONG_PASSWORD;
259 if (memcmp(nt_interactive_password->data, nt_pw, 16) == 0) {
261 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
262 SMBsesskeygen_ntv1(nt_pw, user_sess_key->data);
266 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
268 return NT_STATUS_WRONG_PASSWORD;
271 } else if (lm_interactive_password && lm_interactive_password->length && lm_pw) {
272 if (lm_interactive_password->length != 16) {
273 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_password->length,
275 return NT_STATUS_WRONG_PASSWORD;
278 if (!lp_lanman_auth()) {
279 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
281 return NT_STATUS_WRONG_PASSWORD;
284 if (memcmp(lm_interactive_password->data, lm_pw, 16) == 0) {
287 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
289 return NT_STATUS_WRONG_PASSWORD;
293 /* Check for cleartext netlogon. Used by Exchange 5.5. */
294 if (challenge->length == sizeof(zeros) &&
295 (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
297 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
299 if (nt_pw && nt_response->length) {
301 mdfour(pwhash, nt_response->data, nt_response->length);
302 if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
305 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
307 return NT_STATUS_WRONG_PASSWORD;
310 } else if (!lp_lanman_auth()) {
311 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
314 } else if (lm_pw && lm_response->length) {
319 memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
320 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
322 /* we *might* need to upper-case the string here */
323 E_P16((const uint8_t *)dospwd, p16);
325 if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
328 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
330 return NT_STATUS_WRONG_PASSWORD;
333 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
334 return NT_STATUS_WRONG_PASSWORD;
338 if (nt_response->length != 0 && nt_response->length < 24) {
339 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
340 (unsigned long)nt_response->length, username));
343 if (nt_response->length > 24 && nt_pw) {
344 /* We have the NT MD4 hash challenge available - see if we can
347 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
348 if (smb_pwd_check_ntlmv2(mem_ctx,
356 *lm_sess_key = *user_sess_key;
357 if (user_sess_key->length) {
358 lm_sess_key->length = 8;
364 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
365 if (smb_pwd_check_ntlmv2(mem_ctx,
373 *lm_sess_key = *user_sess_key;
374 if (user_sess_key->length) {
375 lm_sess_key->length = 8;
381 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
382 if (smb_pwd_check_ntlmv2(mem_ctx,
390 *lm_sess_key = *user_sess_key;
391 if (user_sess_key->length) {
392 lm_sess_key->length = 8;
397 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
399 } else if (nt_response->length == 24 && nt_pw) {
400 if (lp_ntlm_auth()) {
401 /* We have the NT MD4 hash challenge available - see if we can
402 use it (ie. does it exist in the smbpasswd file).
404 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
405 if (smb_pwd_check_ntlmv1(mem_ctx,
409 /* The LM session key for this response is not very secure,
410 so use it only if we otherwise allow LM authentication */
412 if (lp_lanman_auth() && lm_pw) {
413 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
417 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
419 return NT_STATUS_WRONG_PASSWORD;
422 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
424 /* no return, becouse we might pick up LMv2 in the LM field */
428 if (lm_response->length == 0) {
429 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
431 return NT_STATUS_WRONG_PASSWORD;
434 if (lm_response->length < 24) {
435 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
436 (unsigned long)nt_response->length, username));
437 return NT_STATUS_WRONG_PASSWORD;
440 if (!lp_lanman_auth()) {
441 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
444 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
447 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
448 if (smb_pwd_check_ntlmv1(mem_ctx,
452 /* The session key for this response is still very odd.
453 It not very secure, so use it only if we otherwise
454 allow LM authentication */
456 if (lp_lanman_auth() && lm_pw) {
457 uint8_t first_8_lm_hash[16];
458 memcpy(first_8_lm_hash, lm_pw, 8);
459 memset(first_8_lm_hash + 8, '\0', 8);
460 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
461 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
468 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
469 return NT_STATUS_WRONG_PASSWORD;
472 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
473 - related to Win9X, legacy NAS pass-though authentication
475 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
476 if (smb_pwd_check_ntlmv2(mem_ctx,
483 if (nt_response->length > 24) {
484 /* If NTLMv2 authentication has preceeded us
485 * (even if it failed), then use the session
486 * key from that. See the RPC-SAMLOGON
488 smb_sess_key_ntlmv2(mem_ctx,
495 } else if (user_sess_key) {
496 /* Otherwise, use the LMv2 session key */
497 *user_sess_key = tmp_sess_key;
499 if (user_sess_key && lm_sess_key) {
500 *lm_sess_key = *user_sess_key;
501 if (user_sess_key->length) {
502 lm_sess_key->length = 8;
508 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain));
509 if (smb_pwd_check_ntlmv2(mem_ctx,
516 if (nt_response->length > 24) {
517 /* If NTLMv2 authentication has preceeded us
518 * (even if it failed), then use the session
519 * key from that. See the RPC-SAMLOGON
521 smb_sess_key_ntlmv2(mem_ctx,
528 } else if (user_sess_key) {
529 /* Otherwise, use the LMv2 session key */
530 *user_sess_key = tmp_sess_key;
532 if (user_sess_key && lm_sess_key) {
533 *lm_sess_key = *user_sess_key;
534 if (user_sess_key->length) {
535 lm_sess_key->length = 8;
541 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
542 if (smb_pwd_check_ntlmv2(mem_ctx,
549 if (nt_response->length > 24) {
550 /* If NTLMv2 authentication has preceeded us
551 * (even if it failed), then use the session
552 * key from that. See the RPC-SAMLOGON
554 smb_sess_key_ntlmv2(mem_ctx,
561 } else if (user_sess_key) {
562 /* Otherwise, use the LMv2 session key */
563 *user_sess_key = tmp_sess_key;
565 if (user_sess_key && lm_sess_key) {
566 *lm_sess_key = *user_sess_key;
567 if (user_sess_key->length) {
568 lm_sess_key->length = 8;
574 /* Apparently NT accepts NT responses in the LM field
575 - I think this is related to Win9X pass-though authentication
577 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
578 if (lp_ntlm_auth()) {
579 if (smb_pwd_check_ntlmv1(mem_ctx,
583 /* The session key for this response is still very odd.
584 It not very secure, so use it only if we otherwise
585 allow LM authentication */
587 if (lp_lanman_auth() && lm_pw) {
588 uint8_t first_8_lm_hash[16];
589 memcpy(first_8_lm_hash, lm_pw, 8);
590 memset(first_8_lm_hash + 8, '\0', 8);
591 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
592 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
596 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
598 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
600 return NT_STATUS_WRONG_PASSWORD;