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. */
41 if (part_passwd
== NULL
) {
42 DEBUG(10,("No password set - DISALLOWING access\n"));
43 /* No password set - always false ! */
47 if (sec_blob
->length
!= 8) {
48 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n",
49 (unsigned long)sec_blob
->length
));
53 if (nt_response
->length
!= 24) {
54 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n",
55 (unsigned long)nt_response
->length
));
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 if (memcmp(p24
, nt_response
->data
, 24) == 0) {
75 if (user_sess_key
!= NULL
) {
76 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
77 SMBsesskeygen_ntv1(part_passwd
, user_sess_key
->data
);
84 /****************************************************************************
85 Core of smb password checking routine. (NTLMv2, LMv2)
86 Note: The same code works with both NTLMv2 and LMv2.
87 ****************************************************************************/
89 static bool smb_pwd_check_ntlmv2(TALLOC_CTX
*mem_ctx
,
90 const DATA_BLOB
*ntv2_response
,
91 const uint8_t *part_passwd
,
92 const DATA_BLOB
*sec_blob
,
93 const char *user
, const char *domain
,
94 DATA_BLOB
*user_sess_key
)
96 /* Finish the encryption of part_passwd. */
98 uint8_t value_from_encryption
[16];
99 DATA_BLOB client_key_data
;
102 if (part_passwd
== NULL
) {
103 DEBUG(10,("No password set - DISALLOWING access\n"));
104 /* No password set - always false */
108 if (sec_blob
->length
!= 8) {
109 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
110 (unsigned long)sec_blob
->length
));
114 if (ntv2_response
->length
< 24) {
115 /* We MUST have more than 16 bytes, or the stuff below will go
116 crazy. No known implementation sends less than the 24 bytes
117 for LMv2, let alone NTLMv2. */
118 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
119 (unsigned long)ntv2_response
->length
));
123 client_key_data
= data_blob_talloc(mem_ctx
, ntv2_response
->data
+16, ntv2_response
->length
-16);
125 todo: should we be checking this for anything? We can't for LMv2,
126 but for NTLMv2 it is meant to contain the current time etc.
129 if (!ntv2_owf_gen(part_passwd
, user
, domain
, kr
)) {
133 status
= SMBOWFencrypt_ntv2(kr
,
136 value_from_encryption
);
137 if (!NT_STATUS_IS_OK(status
)) {
142 DEBUG(100,("Part password (P16) was |\n"));
143 dump_data(100, part_passwd
, 16);
144 DEBUGADD(100,("Password from client was |\n"));
145 dump_data(100, ntv2_response
->data
, ntv2_response
->length
);
146 DEBUGADD(100,("Variable data from client was |\n"));
147 dump_data(100, client_key_data
.data
, client_key_data
.length
);
148 DEBUGADD(100,("Given challenge was |\n"));
149 dump_data(100, sec_blob
->data
, sec_blob
->length
);
150 DEBUGADD(100,("Value from encryption was |\n"));
151 dump_data(100, value_from_encryption
, 16);
153 data_blob_clear_free(&client_key_data
);
154 if (memcmp(value_from_encryption
, ntv2_response
->data
, 16) == 0) {
155 if (user_sess_key
!= NULL
) {
156 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
157 if (user_sess_key
->data
== NULL
) {
158 DBG_ERR("data_blob_talloc failed\n");
162 status
= SMBsesskeygen_ntv2(kr
,
163 value_from_encryption
,
164 user_sess_key
->data
);
165 if (!NT_STATUS_IS_OK(status
)) {
174 /****************************************************************************
175 Core of smb password checking routine. (NTLMv2, LMv2)
176 Note: The same code works with both NTLMv2 and LMv2.
177 ****************************************************************************/
179 static bool smb_sess_key_ntlmv2(TALLOC_CTX
*mem_ctx
,
180 const DATA_BLOB
*ntv2_response
,
181 const uint8_t *part_passwd
,
182 const DATA_BLOB
*sec_blob
,
183 const char *user
, const char *domain
,
184 DATA_BLOB
*user_sess_key
)
186 /* Finish the encryption of part_passwd. */
188 uint8_t value_from_encryption
[16];
189 DATA_BLOB client_key_data
;
192 if (part_passwd
== NULL
) {
193 DEBUG(10,("No password set - DISALLOWING access\n"));
194 /* No password set - always false */
198 if (sec_blob
->length
!= 8) {
199 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect challenge size (%lu)\n",
200 (unsigned long)sec_blob
->length
));
204 if (ntv2_response
->length
< 24) {
205 /* We MUST have more than 16 bytes, or the stuff below will go
206 crazy. No known implementation sends less than the 24 bytes
207 for LMv2, let alone NTLMv2. */
208 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect password length (%lu)\n",
209 (unsigned long)ntv2_response
->length
));
213 client_key_data
= data_blob_talloc(mem_ctx
, ntv2_response
->data
+16, ntv2_response
->length
-16);
215 if (!ntv2_owf_gen(part_passwd
, user
, domain
, kr
)) {
219 status
= SMBOWFencrypt_ntv2(kr
,
222 value_from_encryption
);
223 if (!NT_STATUS_IS_OK(status
)) {
226 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
227 status
= SMBsesskeygen_ntv2(kr
,
228 value_from_encryption
,
229 user_sess_key
->data
);
230 if (!NT_STATUS_IS_OK(status
)) {
237 * Compare password hashes against those from the SAM
239 * @param mem_ctx talloc context
240 * @param client_lanman LANMAN password hash, as supplied by the client
241 * @param client_nt NT (MD4) password hash, as supplied by the client
242 * @param username internal Samba username, for log messages
243 * @param client_username username the client used
244 * @param client_domain domain name the client used (may be mapped)
245 * @param stored_lanman LANMAN password hash, as stored on the SAM
246 * @param stored_nt NT (MD4) password hash, as stored on the SAM
247 * @param user_sess_key User session key
248 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
251 NTSTATUS
hash_password_check(TALLOC_CTX
*mem_ctx
,
253 const struct samr_Password
*client_lanman
,
254 const struct samr_Password
*client_nt
,
255 const char *username
,
256 const struct samr_Password
*stored_lanman
,
257 const struct samr_Password
*stored_nt
)
259 if (stored_nt
== NULL
) {
260 DEBUG(3,("hash_password_check: NO NT password stored for user %s.\n",
264 if (client_nt
&& stored_nt
) {
265 if (memcmp(client_nt
->hash
, stored_nt
->hash
, sizeof(stored_nt
->hash
)) == 0) {
268 DEBUG(3,("hash_password_check: Interactive logon: NT password check failed for user %s\n",
270 return NT_STATUS_WRONG_PASSWORD
;
273 } else if (client_lanman
&& stored_lanman
) {
275 DEBUG(3,("hash_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
277 return NT_STATUS_WRONG_PASSWORD
;
279 if (strchr_m(username
, '@')) {
280 return NT_STATUS_NOT_FOUND
;
283 if (memcmp(client_lanman
->hash
, stored_lanman
->hash
, sizeof(stored_lanman
->hash
)) == 0) {
286 DEBUG(3,("hash_password_check: Interactive logon: LANMAN password check failed for user %s\n",
288 return NT_STATUS_WRONG_PASSWORD
;
291 if (strchr_m(username
, '@')) {
292 return NT_STATUS_NOT_FOUND
;
294 return NT_STATUS_WRONG_PASSWORD
;
298 * Check a challenge-response password against the value of the NT or
301 * @param mem_ctx talloc context
302 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
303 * @param nt_response 'unicode' NT response to the challenge, or unicode password
304 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
305 * @param username internal Samba username, for log messages
306 * @param client_username username the client used
307 * @param client_domain domain name the client used (may be mapped)
308 * @param stored_lanman LANMAN ASCII password from our passdb or similar
309 * @param stored_nt MD4 unicode password from our passdb or similar
310 * @param user_sess_key User session key
311 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
314 NTSTATUS
ntlm_password_check(TALLOC_CTX
*mem_ctx
,
316 enum ntlm_auth_level ntlm_auth
,
317 uint32_t logon_parameters
,
318 const DATA_BLOB
*challenge
,
319 const DATA_BLOB
*lm_response
,
320 const DATA_BLOB
*nt_response
,
321 const char *username
,
322 const char *client_username
,
323 const char *client_domain
,
324 const struct samr_Password
*stored_lanman
,
325 const struct samr_Password
*stored_nt
,
326 DATA_BLOB
*user_sess_key
,
327 DATA_BLOB
*lm_sess_key
)
329 DATA_BLOB tmp_sess_key
;
330 const char *upper_client_domain
= NULL
;
332 if (ntlm_auth
== NTLM_AUTH_DISABLED
) {
333 DBG_WARNING("ntlm_password_check: NTLM authentication not "
334 "permitted by configuration.\n");
335 return NT_STATUS_NTLM_BLOCKED
;
338 if (client_domain
!= NULL
) {
339 upper_client_domain
= talloc_strdup_upper(mem_ctx
, client_domain
);
340 if (upper_client_domain
== NULL
) {
341 return NT_STATUS_NO_MEMORY
;
345 if (stored_nt
== NULL
) {
346 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
350 *lm_sess_key
= data_blob(NULL
, 0);
351 *user_sess_key
= data_blob(NULL
, 0);
353 /* Check for cleartext netlogon. Used by Exchange 5.5. */
354 if ((logon_parameters
& MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
)
355 && challenge
->length
== 8
356 && (all_zero(challenge
->data
, challenge
->length
))) {
357 struct samr_Password client_nt
;
358 struct samr_Password client_lm
;
359 char *unix_pw
= NULL
;
361 size_t converted_size
= 0;
363 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
365 mdfour(client_nt
.hash
, nt_response
->data
, nt_response
->length
);
367 if (lm_response
->length
&&
368 (convert_string_talloc(mem_ctx
, CH_DOS
, CH_UNIX
,
369 lm_response
->data
, lm_response
->length
,
370 (void *)&unix_pw
, &converted_size
))) {
371 if (E_deshash(unix_pw
, client_lm
.hash
)) {
379 return hash_password_check(mem_ctx
,
381 lm_ok
? &client_lm
: NULL
,
382 nt_response
->length
? &client_nt
: NULL
,
384 stored_lanman
, stored_nt
);
387 if (nt_response
->length
!= 0 && nt_response
->length
< 24) {
388 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
389 (unsigned long)nt_response
->length
, username
));
392 if (nt_response
->length
> 24 && stored_nt
) {
393 /* We have the NT MD4 hash challenge available - see if we can
396 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n",
397 client_domain
? client_domain
: "<NULL>"));
398 if (smb_pwd_check_ntlmv2(mem_ctx
,
400 stored_nt
->hash
, challenge
,
404 if (user_sess_key
->length
) {
405 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
410 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n",
411 upper_client_domain
? upper_client_domain
: "<NULL>"));
412 if (smb_pwd_check_ntlmv2(mem_ctx
,
414 stored_nt
->hash
, challenge
,
418 if (user_sess_key
->length
) {
419 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
424 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
425 if (smb_pwd_check_ntlmv2(mem_ctx
,
427 stored_nt
->hash
, challenge
,
431 if (user_sess_key
->length
) {
432 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
436 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
438 } else if (nt_response
->length
== 24 && stored_nt
) {
439 if (ntlm_auth
== NTLM_AUTH_ON
440 || (ntlm_auth
== NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY
&& (logon_parameters
& MSV1_0_ALLOW_MSVCHAPV2
))) {
441 /* We have the NT MD4 hash challenge available - see if we can
442 use it (ie. does it exist in the smbpasswd file).
444 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
445 if (smb_pwd_check_ntlmv1(mem_ctx
,
447 stored_nt
->hash
, challenge
,
449 /* The LM session key for this response is not very secure,
450 so use it only if we otherwise allow LM authentication */
452 if (lanman_auth
&& stored_lanman
) {
453 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, MIN(8, user_sess_key
->length
));
457 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
459 return NT_STATUS_WRONG_PASSWORD
;
462 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
464 /* no return, because we might pick up LMv2 in the LM field */
468 if (lm_response
->length
== 0) {
469 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
471 return NT_STATUS_WRONG_PASSWORD
;
474 if (lm_response
->length
< 24) {
475 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
476 (unsigned long)nt_response
->length
, username
));
477 return NT_STATUS_WRONG_PASSWORD
;
481 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
483 } else if (!stored_lanman
) {
484 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
486 } else if (strchr_m(username
, '@')) {
487 DEBUG(3,("ntlm_password_check: NO LanMan password allowed for username@realm logins (user: %s)\n",
490 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
491 if (smb_pwd_check_ntlmv1(mem_ctx
,
493 stored_lanman
->hash
, challenge
,
495 /* The session key for this response is still very odd.
496 It not very secure, so use it only if we otherwise
497 allow LM authentication */
499 if (lanman_auth
&& stored_lanman
) {
500 uint8_t first_8_lm_hash
[16];
501 memcpy(first_8_lm_hash
, stored_lanman
->hash
, 8);
502 memset(first_8_lm_hash
+ 8, '\0', 8);
503 *user_sess_key
= data_blob_talloc(mem_ctx
, first_8_lm_hash
, 16);
504 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, 8);
511 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username
));
512 return NT_STATUS_WRONG_PASSWORD
;
515 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
516 - related to Win9X, legacy NAS pass-though authentication
518 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n",
519 client_domain
? client_domain
: "<NULL>"));
520 if (smb_pwd_check_ntlmv2(mem_ctx
,
522 stored_nt
->hash
, challenge
,
526 if (nt_response
->length
> 24) {
527 /* If NTLMv2 authentication has preceded us
528 * (even if it failed), then use the session
529 * key from that. See the RPC-SAMLOGON
531 smb_sess_key_ntlmv2(mem_ctx
,
533 stored_nt
->hash
, challenge
,
538 /* Otherwise, use the LMv2 session key */
539 *user_sess_key
= tmp_sess_key
;
541 if (user_sess_key
->length
) {
542 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
547 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n",
548 upper_client_domain
? upper_client_domain
: "<NULL>"));
549 if (smb_pwd_check_ntlmv2(mem_ctx
,
551 stored_nt
->hash
, challenge
,
555 if (nt_response
->length
> 24) {
556 /* If NTLMv2 authentication has preceded us
557 * (even if it failed), then use the session
558 * key from that. See the RPC-SAMLOGON
560 smb_sess_key_ntlmv2(mem_ctx
,
562 stored_nt
->hash
, challenge
,
567 /* Otherwise, use the LMv2 session key */
568 *user_sess_key
= tmp_sess_key
;
570 if (user_sess_key
->length
) {
571 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
576 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
577 if (smb_pwd_check_ntlmv2(mem_ctx
,
579 stored_nt
->hash
, challenge
,
583 if (nt_response
->length
> 24) {
584 /* If NTLMv2 authentication has preceded us
585 * (even if it failed), then use the session
586 * key from that. See the RPC-SAMLOGON
588 smb_sess_key_ntlmv2(mem_ctx
,
590 stored_nt
->hash
, challenge
,
595 /* Otherwise, use the LMv2 session key */
596 *user_sess_key
= tmp_sess_key
;
598 if (user_sess_key
->length
) {
599 *lm_sess_key
= data_blob_talloc(mem_ctx
, user_sess_key
->data
, MIN(8, user_sess_key
->length
));
604 /* Apparently NT accepts NT responses in the LM field
605 - I think this is related to Win9X pass-though authentication
607 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
608 if (ntlm_auth
== NTLM_AUTH_ON
) {
609 if (smb_pwd_check_ntlmv1(mem_ctx
,
611 stored_nt
->hash
, challenge
,
613 /* The session key for this response is still very odd.
614 It not very secure, so use it only if we otherwise
615 allow LM authentication */
617 if (lanman_auth
&& stored_lanman
) {
618 uint8_t first_8_lm_hash
[16];
619 memcpy(first_8_lm_hash
, stored_lanman
->hash
, 8);
620 memset(first_8_lm_hash
+ 8, '\0', 8);
621 *user_sess_key
= data_blob_talloc(mem_ctx
, first_8_lm_hash
, 16);
622 *lm_sess_key
= data_blob_talloc(mem_ctx
, stored_lanman
->hash
, 8);
626 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username
));
628 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username
));
631 /* Try and match error codes */
632 if (strchr_m(username
, '@')) {
633 return NT_STATUS_NOT_FOUND
;
635 return NT_STATUS_WRONG_PASSWORD
;