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 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 "lib/crypto/md4.h"
24 #include "librpc/gen_ndr/netlogon.h"
25 #include "libcli/auth/libcli_auth.h"
27 /****************************************************************************
28 Core of smb password checking routine.
29 ****************************************************************************/
31 static bool smb_pwd_check_ntlmv1(TALLOC_CTX
*mem_ctx
,
32 const DATA_BLOB
*nt_response
,
33 const uint8_t *part_passwd
,
34 const DATA_BLOB
*sec_blob
,
35 DATA_BLOB
*user_sess_key
)
37 /* 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 DBG_ERR("incorrect challenge size (%zu)\n", sec_blob
->length
);
53 if (nt_response
->length
!= 24) {
54 DBG_ERR("incorrect password length (%zu)\n",
59 rc
= SMBOWFencrypt(part_passwd
, sec_blob
->data
, p24
);
65 DEBUG(100,("Part password (P16) was |\n"));
66 dump_data(100, part_passwd
, 16);
67 DEBUGADD(100,("Password from client was |\n"));
68 dump_data(100, nt_response
->data
, nt_response
->length
);
69 DEBUGADD(100,("Given challenge was |\n"));
70 dump_data(100, sec_blob
->data
, sec_blob
->length
);
71 DEBUGADD(100,("Value from encryption was |\n"));
72 dump_data(100, p24
, 24);
74 ok
= mem_equal_const_time(p24
, nt_response
->data
, 24);
78 if (user_sess_key
!= NULL
) {
79 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
80 if (user_sess_key
->data
== NULL
) {
81 DBG_ERR("data_blob_talloc failed\n");
84 SMBsesskeygen_ntv1(part_passwd
, user_sess_key
->data
);
89 /****************************************************************************
90 Core of smb password checking routine. (NTLMv2, LMv2)
91 Note: The same code works with both NTLMv2 and LMv2.
92 ****************************************************************************/
94 static bool smb_pwd_check_ntlmv2(TALLOC_CTX
*mem_ctx
,
95 const DATA_BLOB
*ntv2_response
,
96 const uint8_t *part_passwd
,
97 const DATA_BLOB
*sec_blob
,
98 const char *user
, const char *domain
,
99 DATA_BLOB
*user_sess_key
)
101 /* Finish the encryption of part_passwd. */
103 uint8_t value_from_encryption
[16];
104 DATA_BLOB client_key_data
;
108 if (part_passwd
== NULL
) {
109 DEBUG(10,("No password set - DISALLOWING access\n"));
110 /* No password set - always false */
114 if (sec_blob
->length
!= 8) {
115 DBG_ERR("incorrect challenge size (%zu)\n", sec_blob
->length
);
119 if (ntv2_response
->length
< 24) {
120 /* We MUST have more than 16 bytes, or the stuff below will go
121 crazy. No known implementation sends less than the 24 bytes
122 for LMv2, let alone NTLMv2. */
123 DBG_ERR("incorrect password length (%zu)\n",
124 ntv2_response
->length
);
128 client_key_data
= data_blob_talloc(mem_ctx
, ntv2_response
->data
+16, ntv2_response
->length
-16);
130 todo: should we be checking this for anything? We can't for LMv2,
131 but for NTLMv2 it is meant to contain the current time etc.
134 if (!ntv2_owf_gen(part_passwd
, user
, domain
, kr
)) {
138 status
= SMBOWFencrypt_ntv2(kr
,
141 value_from_encryption
);
142 if (!NT_STATUS_IS_OK(status
)) {
147 DEBUG(100,("Part password (P16) was |\n"));
148 dump_data(100, part_passwd
, 16);
149 DEBUGADD(100,("Password from client was |\n"));
150 dump_data(100, ntv2_response
->data
, ntv2_response
->length
);
151 DEBUGADD(100,("Variable data from client was |\n"));
152 dump_data(100, client_key_data
.data
, client_key_data
.length
);
153 DEBUGADD(100,("Given challenge was |\n"));
154 dump_data(100, sec_blob
->data
, sec_blob
->length
);
155 DEBUGADD(100,("Value from encryption was |\n"));
156 dump_data(100, value_from_encryption
, 16);
158 data_blob_clear_free(&client_key_data
);
160 ok
= mem_equal_const_time(value_from_encryption
, ntv2_response
->data
, 16);
164 if (user_sess_key
!= NULL
) {
165 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
166 if (user_sess_key
->data
== NULL
) {
167 DBG_ERR("data_blob_talloc failed\n");
171 status
= SMBsesskeygen_ntv2(
172 kr
, value_from_encryption
, user_sess_key
->data
);
173 if (!NT_STATUS_IS_OK(status
)) {
180 /****************************************************************************
181 Core of smb password checking routine. (NTLMv2, LMv2)
182 Note: The same code works with both NTLMv2 and LMv2.
183 ****************************************************************************/
185 static bool smb_sess_key_ntlmv2(TALLOC_CTX
*mem_ctx
,
186 const DATA_BLOB
*ntv2_response
,
187 const uint8_t *part_passwd
,
188 const DATA_BLOB
*sec_blob
,
189 const char *user
, const char *domain
,
190 DATA_BLOB
*user_sess_key
)
192 /* Finish the encryption of part_passwd. */
194 uint8_t value_from_encryption
[16];
195 DATA_BLOB client_key_data
;
198 if (part_passwd
== NULL
) {
199 DEBUG(10,("No password set - DISALLOWING access\n"));
200 /* No password set - always false */
204 if (sec_blob
->length
!= 8) {
205 DBG_ERR("incorrect challenge size (%zu)\n", sec_blob
->length
);
209 if (ntv2_response
->length
< 24) {
210 /* We MUST have more than 16 bytes, or the stuff below will go
211 crazy. No known implementation sends less than the 24 bytes
212 for LMv2, let alone NTLMv2. */
213 DBG_ERR("incorrect password length (%zu)\n",
214 ntv2_response
->length
);
218 client_key_data
= data_blob_talloc(mem_ctx
, ntv2_response
->data
+16, ntv2_response
->length
-16);
220 if (!ntv2_owf_gen(part_passwd
, user
, domain
, kr
)) {
224 status
= SMBOWFencrypt_ntv2(kr
,
227 value_from_encryption
);
228 if (!NT_STATUS_IS_OK(status
)) {
231 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
232 if (user_sess_key
->data
== NULL
) {
233 DBG_ERR("data_blob_talloc failed\n");
236 status
= SMBsesskeygen_ntv2(kr
,
237 value_from_encryption
,
238 user_sess_key
->data
);
239 if (!NT_STATUS_IS_OK(status
)) {
246 * Compare password hashes against those from the SAM
248 * @param mem_ctx talloc context
249 * @param client_lanman LANMAN password hash, as supplied by the client
250 * @param client_nt NT (MD4) password hash, as supplied by the client
251 * @param username internal Samba username, for log messages
252 * @param client_username username the client used
253 * @param client_domain domain name the client used (may be mapped)
254 * @param stored_lanman LANMAN password hash, as stored on the SAM
255 * @param stored_nt NT (MD4) password hash, as stored on the SAM
256 * @param user_sess_key User session key
257 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
260 NTSTATUS
hash_password_check(TALLOC_CTX
*mem_ctx
,
262 enum ntlm_auth_level ntlm_auth
,
263 const struct samr_Password
*client_lanman
,
264 const struct samr_Password
*client_nt
,
265 const char *username
,
266 const struct samr_Password
*stored_lanman
,
267 const struct samr_Password
*stored_nt
)
269 if (ntlm_auth
== NTLM_AUTH_DISABLED
) {
270 DBG_WARNING("hash_password_check: NTLM authentication not "
271 "permitted by configuration.\n");
272 return NT_STATUS_NTLM_BLOCKED
;
275 if (stored_nt
== NULL
) {
276 DEBUG(3,("hash_password_check: NO NT password stored for user %s.\n",
280 if (client_nt
&& stored_nt
) {
281 if (mem_equal_const_time(client_nt
->hash
, stored_nt
->hash
, sizeof(stored_nt
->hash
))) {
284 DEBUG(3,("hash_password_check: Interactive logon: NT password check failed for user %s\n",
286 return NT_STATUS_WRONG_PASSWORD
;
289 } else if (client_lanman
&& stored_lanman
) {
291 DEBUG(3,("hash_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
293 return NT_STATUS_WRONG_PASSWORD
;
295 if (strchr_m(username
, '@')) {
296 return NT_STATUS_NOT_FOUND
;
299 if (mem_equal_const_time(client_lanman
->hash
, stored_lanman
->hash
, sizeof(stored_lanman
->hash
))) {
302 DEBUG(3,("hash_password_check: Interactive logon: LANMAN password check failed for user %s\n",
304 return NT_STATUS_WRONG_PASSWORD
;
307 if (strchr_m(username
, '@')) {
308 return NT_STATUS_NOT_FOUND
;
310 return NT_STATUS_WRONG_PASSWORD
;
314 * Check a challenge-response password against the value of the NT or
317 * @param mem_ctx talloc context
318 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
319 * @param nt_response 'unicode' NT response to the challenge, or unicode password
320 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
321 * @param username internal Samba username, for log messages
322 * @param client_username username the client used
323 * @param client_domain domain name the client used (may be mapped)
324 * @param stored_lanman LANMAN ASCII password from our passdb or similar
325 * @param stored_nt MD4 unicode password from our passdb or similar
326 * @param user_sess_key User session key
327 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
330 NTSTATUS
ntlm_password_check(TALLOC_CTX
*mem_ctx
,
332 enum ntlm_auth_level ntlm_auth
,
333 uint32_t logon_parameters
,
334 const DATA_BLOB
*challenge
,
335 const DATA_BLOB
*lm_response
,
336 const DATA_BLOB
*nt_response
,
337 const char *username
,
338 const char *client_username
,
339 const char *client_domain
,
340 const struct samr_Password
*stored_lanman
,
341 const struct samr_Password
*stored_nt
,
342 DATA_BLOB
*user_sess_key
,
343 DATA_BLOB
*lm_sess_key
)
345 DATA_BLOB tmp_sess_key
;
346 const char *upper_client_domain
= NULL
;
348 if (ntlm_auth
== NTLM_AUTH_DISABLED
) {
349 DBG_WARNING("ntlm_password_check: NTLM authentication not "
350 "permitted by configuration.\n");
351 return NT_STATUS_NTLM_BLOCKED
;
354 if (client_domain
!= NULL
) {
355 upper_client_domain
= talloc_strdup_upper(mem_ctx
, client_domain
);
356 if (upper_client_domain
== NULL
) {
357 return NT_STATUS_NO_MEMORY
;
361 if (stored_nt
== NULL
) {
362 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
366 *lm_sess_key
= data_blob(NULL
, 0);
367 *user_sess_key
= data_blob(NULL
, 0);
369 /* Check for cleartext netlogon. Used by Exchange 5.5. */
370 if ((logon_parameters
& MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
)
371 && challenge
->length
== 8
372 && (all_zero(challenge
->data
, challenge
->length
))) {
373 struct samr_Password client_nt
;
374 struct samr_Password client_lm
;
375 char *unix_pw
= NULL
;
377 size_t converted_size
= 0;
379 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
381 mdfour(client_nt
.hash
, nt_response
->data
, nt_response
->length
);
383 if (lm_response
->length
&&
384 (convert_string_talloc(mem_ctx
, CH_DOS
, CH_UNIX
,
385 lm_response
->data
, lm_response
->length
,
386 &unix_pw
, &converted_size
))) {
387 if (E_deshash(unix_pw
, client_lm
.hash
)) {
395 return hash_password_check(mem_ctx
,
398 lm_ok
? &client_lm
: NULL
,
399 nt_response
->length
? &client_nt
: NULL
,
401 stored_lanman
, stored_nt
);
404 if (nt_response
->length
!= 0 && nt_response
->length
< 24) {
405 DBG_NOTICE("invalid NT password length (%zu) for user %s\n",
410 if (nt_response
->length
> 24 && stored_nt
) {
411 /* We have the NT MD4 hash challenge available - see if we can
414 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n",
415 client_domain
? client_domain
: "<NULL>"));
416 if (smb_pwd_check_ntlmv2(mem_ctx
,
418 stored_nt
->hash
, challenge
,
422 if (user_sess_key
->length
) {
423 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
428 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n",
429 upper_client_domain
? upper_client_domain
: "<NULL>"));
430 if (smb_pwd_check_ntlmv2(mem_ctx
,
432 stored_nt
->hash
, challenge
,
436 if (user_sess_key
->length
) {
437 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
442 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
443 if (smb_pwd_check_ntlmv2(mem_ctx
,
445 stored_nt
->hash
, challenge
,
449 if (user_sess_key
->length
) {
450 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
454 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
456 } else if (nt_response
->length
== 24 && stored_nt
) {
457 if (ntlm_auth
== NTLM_AUTH_ON
458 || (ntlm_auth
== NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY
&& (logon_parameters
& MSV1_0_ALLOW_MSVCHAPV2
))) {
459 /* We have the NT MD4 hash challenge available - see if we can
460 use it (ie. does it exist in the smbpasswd file).
462 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
463 if (smb_pwd_check_ntlmv1(mem_ctx
,
465 stored_nt
->hash
, challenge
,
467 /* The LM session key for this response is not very secure,
468 so use it only if we otherwise allow LM authentication */
470 if (lanman_auth
&& stored_lanman
) {
471 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, MIN(8, user_sess_key
->length
));
475 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
477 return NT_STATUS_WRONG_PASSWORD
;
480 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
482 /* no return, because we might pick up LMv2 in the LM field */
486 if (lm_response
->length
== 0) {
487 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
489 return NT_STATUS_WRONG_PASSWORD
;
492 if (lm_response
->length
< 24) {
493 DBG_NOTICE("invalid LanMan password length (%zu) for "
495 nt_response
->length
, username
);
496 return NT_STATUS_WRONG_PASSWORD
;
500 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
502 } else if (!stored_lanman
) {
503 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
505 } else if (strchr_m(username
, '@')) {
506 DEBUG(3,("ntlm_password_check: NO LanMan password allowed for username@realm logins (user: %s)\n",
509 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
510 if (smb_pwd_check_ntlmv1(mem_ctx
,
512 stored_lanman
->hash
, challenge
,
514 /* The session key for this response is still very odd.
515 It not very secure, so use it only if we otherwise
516 allow LM authentication */
518 if (lanman_auth
&& stored_lanman
) {
519 uint8_t first_8_lm_hash
[16];
520 memcpy(first_8_lm_hash
, stored_lanman
->hash
, 8);
521 memset(first_8_lm_hash
+ 8, '\0', 8);
522 *user_sess_key
= data_blob_talloc(mem_ctx
, first_8_lm_hash
, 16);
523 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, 8);
530 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username
));
531 return NT_STATUS_WRONG_PASSWORD
;
534 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
535 - related to Win9X, legacy NAS pass-though authentication
537 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n",
538 client_domain
? client_domain
: "<NULL>"));
539 if (smb_pwd_check_ntlmv2(mem_ctx
,
541 stored_nt
->hash
, challenge
,
545 if (nt_response
->length
> 24) {
546 /* If NTLMv2 authentication has preceded us
547 * (even if it failed), then use the session
548 * key from that. See the RPC-SAMLOGON
550 smb_sess_key_ntlmv2(mem_ctx
,
552 stored_nt
->hash
, challenge
,
557 /* Otherwise, use the LMv2 session key */
558 *user_sess_key
= tmp_sess_key
;
560 if (user_sess_key
->length
) {
561 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
566 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n",
567 upper_client_domain
? upper_client_domain
: "<NULL>"));
568 if (smb_pwd_check_ntlmv2(mem_ctx
,
570 stored_nt
->hash
, challenge
,
574 if (nt_response
->length
> 24) {
575 /* If NTLMv2 authentication has preceded us
576 * (even if it failed), then use the session
577 * key from that. See the RPC-SAMLOGON
579 smb_sess_key_ntlmv2(mem_ctx
,
581 stored_nt
->hash
, challenge
,
586 /* Otherwise, use the LMv2 session key */
587 *user_sess_key
= tmp_sess_key
;
589 if (user_sess_key
->length
) {
590 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
595 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
596 if (smb_pwd_check_ntlmv2(mem_ctx
,
598 stored_nt
->hash
, challenge
,
602 if (nt_response
->length
> 24) {
603 /* If NTLMv2 authentication has preceded us
604 * (even if it failed), then use the session
605 * key from that. See the RPC-SAMLOGON
607 smb_sess_key_ntlmv2(mem_ctx
,
609 stored_nt
->hash
, challenge
,
614 /* Otherwise, use the LMv2 session key */
615 *user_sess_key
= tmp_sess_key
;
617 if (user_sess_key
->length
) {
618 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
623 /* Apparently NT accepts NT responses in the LM field
624 - I think this is related to Win9X pass-though authentication
626 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
627 if (ntlm_auth
== NTLM_AUTH_ON
) {
628 if (smb_pwd_check_ntlmv1(mem_ctx
,
630 stored_nt
->hash
, challenge
,
632 /* The session key for this response is still very odd.
633 It not very secure, so use it only if we otherwise
634 allow LM authentication */
636 if (lanman_auth
&& stored_lanman
) {
637 uint8_t first_8_lm_hash
[16];
638 memcpy(first_8_lm_hash
, stored_lanman
->hash
, 8);
639 memset(first_8_lm_hash
+ 8, '\0', 8);
640 *user_sess_key
= data_blob_talloc(mem_ctx
, first_8_lm_hash
, 16);
641 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, 8);
645 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username
));
647 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username
));
650 /* Try and match error codes */
651 if (strchr_m(username
, '@')) {
652 return NT_STATUS_NOT_FOUND
;
654 return NT_STATUS_WRONG_PASSWORD
;