r23063: Make sure to invalidate the ccache when we set a
[Samba.git] / source / auth / ntlm_check.c
blob20c5854098e58ea66599993bf316a1fea44366d7
1 /*
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.
23 #include "includes.h"
24 #include "lib/crypto/crypto.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "libcli/auth/libcli_auth.h"
28 /****************************************************************************
29 Core of smb password checking routine.
30 ****************************************************************************/
32 static BOOL smb_pwd_check_ntlmv1(TALLOC_CTX *mem_ctx,
33 const DATA_BLOB *nt_response,
34 const uint8_t *part_passwd,
35 const DATA_BLOB *sec_blob,
36 DATA_BLOB *user_sess_key)
38 /* Finish the encryption of part_passwd. */
39 uint8_t p24[24];
41 if (part_passwd == NULL) {
42 DEBUG(10,("No password set - DISALLOWING access\n"));
43 /* No password set - always false ! */
44 return 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));
50 return False;
53 if (nt_response->length != 24) {
54 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n",
55 (unsigned long)nt_response->length));
56 return False;
59 SMBOWFencrypt(part_passwd, sec_blob->data, p24);
61 #if DEBUG_PASSWORD
62 DEBUG(100,("Part password (P16) was |\n"));
63 dump_data(100, part_passwd, 16);
64 DEBUGADD(100,("Password from client was |\n"));
65 dump_data(100, nt_response->data, nt_response->length);
66 DEBUGADD(100,("Given challenge was |\n"));
67 dump_data(100, sec_blob->data, sec_blob->length);
68 DEBUGADD(100,("Value from encryption was |\n"));
69 dump_data(100, p24, 24);
70 #endif
71 if (memcmp(p24, nt_response->data, 24) == 0) {
72 if (user_sess_key != NULL) {
73 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
74 SMBsesskeygen_ntv1(part_passwd, user_sess_key->data);
76 return True;
78 return False;
81 /****************************************************************************
82 Core of smb password checking routine. (NTLMv2, LMv2)
83 Note: The same code works with both NTLMv2 and LMv2.
84 ****************************************************************************/
86 static BOOL smb_pwd_check_ntlmv2(TALLOC_CTX *mem_ctx,
87 const DATA_BLOB *ntv2_response,
88 const uint8_t *part_passwd,
89 const DATA_BLOB *sec_blob,
90 const char *user, const char *domain,
91 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
92 DATA_BLOB *user_sess_key)
94 /* Finish the encryption of part_passwd. */
95 uint8_t kr[16];
96 uint8_t value_from_encryption[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 */
102 return 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));
108 return False;
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));
117 return False;
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 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
127 return False;
130 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
132 #if DEBUG_PASSWORD
133 DEBUG(100,("Part password (P16) was |\n"));
134 dump_data(100, part_passwd, 16);
135 DEBUGADD(100,("Password from client was |\n"));
136 dump_data(100, ntv2_response->data, ntv2_response->length);
137 DEBUGADD(100,("Variable data from client was |\n"));
138 dump_data(100, client_key_data.data, client_key_data.length);
139 DEBUGADD(100,("Given challenge was |\n"));
140 dump_data(100, sec_blob->data, sec_blob->length);
141 DEBUGADD(100,("Value from encryption was |\n"));
142 dump_data(100, value_from_encryption, 16);
143 #endif
144 data_blob_clear_free(&client_key_data);
145 if (memcmp(value_from_encryption, ntv2_response->data, 16) == 0) {
146 if (user_sess_key != NULL) {
147 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
148 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
150 return True;
152 return False;
155 /****************************************************************************
156 Core of smb password checking routine. (NTLMv2, LMv2)
157 Note: The same code works with both NTLMv2 and LMv2.
158 ****************************************************************************/
160 static BOOL smb_sess_key_ntlmv2(TALLOC_CTX *mem_ctx,
161 const DATA_BLOB *ntv2_response,
162 const uint8_t *part_passwd,
163 const DATA_BLOB *sec_blob,
164 const char *user, const char *domain,
165 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
166 DATA_BLOB *user_sess_key)
168 /* Finish the encryption of part_passwd. */
169 uint8_t kr[16];
170 uint8_t value_from_encryption[16];
171 DATA_BLOB client_key_data;
173 if (part_passwd == NULL) {
174 DEBUG(10,("No password set - DISALLOWING access\n"));
175 /* No password set - always False */
176 return False;
179 if (sec_blob->length != 8) {
180 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect challenge size (%lu)\n",
181 (unsigned long)sec_blob->length));
182 return False;
185 if (ntv2_response->length < 24) {
186 /* We MUST have more than 16 bytes, or the stuff below will go
187 crazy. No known implementation sends less than the 24 bytes
188 for LMv2, let alone NTLMv2. */
189 DEBUG(0, ("smb_sess_key_ntlmv2: incorrect password length (%lu)\n",
190 (unsigned long)ntv2_response->length));
191 return False;
194 client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16);
196 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
197 return False;
200 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
201 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
202 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
203 return True;
207 * Compare password hashes against those from the SAM
209 * @param mem_ctx talloc context
210 * @param client_lanman LANMAN password hash, as supplied by the client
211 * @param client_nt NT (MD4) password hash, as supplied by the client
212 * @param username internal Samba username, for log messages
213 * @param client_username username the client used
214 * @param client_domain domain name the client used (may be mapped)
215 * @param stored_lanman LANMAN password hash, as stored on the SAM
216 * @param stored_nt NT (MD4) password hash, as stored on the SAM
217 * @param user_sess_key User session key
218 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
221 NTSTATUS hash_password_check(TALLOC_CTX *mem_ctx,
222 const struct samr_Password *client_lanman,
223 const struct samr_Password *client_nt,
224 const char *username,
225 const struct samr_Password *stored_lanman,
226 const struct samr_Password *stored_nt)
228 if (stored_nt == NULL) {
229 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
230 username));
233 if (client_nt && stored_nt) {
234 if (memcmp(client_nt->hash, stored_nt->hash, sizeof(stored_nt->hash)) == 0) {
235 return NT_STATUS_OK;
236 } else {
237 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
238 username));
239 return NT_STATUS_WRONG_PASSWORD;
242 } else if (client_lanman && stored_lanman) {
243 if (!lp_lanman_auth()) {
244 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
245 username));
246 return NT_STATUS_WRONG_PASSWORD;
248 if (strchr_m(username, '@')) {
249 return NT_STATUS_NOT_FOUND;
252 if (memcmp(client_lanman->hash, stored_lanman->hash, sizeof(stored_lanman->hash)) == 0) {
253 return NT_STATUS_OK;
254 } else {
255 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
256 username));
257 return NT_STATUS_WRONG_PASSWORD;
260 if (strchr_m(username, '@')) {
261 return NT_STATUS_NOT_FOUND;
263 return NT_STATUS_WRONG_PASSWORD;
267 * Check a challenge-response password against the value of the NT or
268 * LM password hash.
270 * @param mem_ctx talloc context
271 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
272 * @param nt_response 'unicode' NT response to the challenge, or unicode password
273 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
274 * @param username internal Samba username, for log messages
275 * @param client_username username the client used
276 * @param client_domain domain name the client used (may be mapped)
277 * @param stored_lanman LANMAN ASCII password from our passdb or similar
278 * @param stored_nt MD4 unicode password from our passdb or similar
279 * @param user_sess_key User session key
280 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
283 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
284 uint32_t logon_parameters,
285 const DATA_BLOB *challenge,
286 const DATA_BLOB *lm_response,
287 const DATA_BLOB *nt_response,
288 const char *username,
289 const char *client_username,
290 const char *client_domain,
291 const struct samr_Password *stored_lanman,
292 const struct samr_Password *stored_nt,
293 DATA_BLOB *user_sess_key,
294 DATA_BLOB *lm_sess_key)
296 static const uint8_t zeros[8];
297 DATA_BLOB tmp_sess_key;
299 if (stored_nt == NULL) {
300 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
301 username));
304 *lm_sess_key = data_blob(NULL, 0);
305 *user_sess_key = data_blob(NULL, 0);
307 /* Check for cleartext netlogon. Used by Exchange 5.5. */
308 if ((logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_ALLOWED)
309 && challenge->length == sizeof(zeros)
310 && (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
311 struct samr_Password client_nt;
312 struct samr_Password client_lm;
313 char *unix_pw = NULL;
314 BOOL lm_ok;
316 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
317 username));
318 mdfour(client_nt.hash, nt_response->data, nt_response->length);
320 if (lm_response->length &&
321 (convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX,
322 lm_response->data, lm_response->length,
323 (void **)&unix_pw) != -1)) {
324 if (E_deshash(unix_pw, client_lm.hash)) {
325 lm_ok = True;
326 } else {
327 lm_ok = False;
329 } else {
330 lm_ok = False;
332 return hash_password_check(mem_ctx,
333 lm_ok ? &client_lm : NULL,
334 nt_response->length ? &client_nt : NULL,
335 username,
336 stored_lanman, stored_nt);
339 if (nt_response->length != 0 && nt_response->length < 24) {
340 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
341 (unsigned long)nt_response->length, username));
344 if (nt_response->length > 24 && stored_nt) {
345 /* We have the NT MD4 hash challenge available - see if we can
346 use it
348 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
349 if (smb_pwd_check_ntlmv2(mem_ctx,
350 nt_response,
351 stored_nt->hash, challenge,
352 client_username,
353 client_domain,
354 False,
355 user_sess_key)) {
356 *lm_sess_key = *user_sess_key;
357 if (user_sess_key->length) {
358 lm_sess_key->length = 8;
360 return NT_STATUS_OK;
363 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
364 if (smb_pwd_check_ntlmv2(mem_ctx,
365 nt_response,
366 stored_nt->hash, challenge,
367 client_username,
368 client_domain,
369 True,
370 user_sess_key)) {
371 *lm_sess_key = *user_sess_key;
372 if (user_sess_key->length) {
373 lm_sess_key->length = 8;
375 return NT_STATUS_OK;
378 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
379 if (smb_pwd_check_ntlmv2(mem_ctx,
380 nt_response,
381 stored_nt->hash, challenge,
382 client_username,
384 False,
385 user_sess_key)) {
386 *lm_sess_key = *user_sess_key;
387 if (user_sess_key->length) {
388 lm_sess_key->length = 8;
390 return NT_STATUS_OK;
391 } else {
392 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
394 } else if (nt_response->length == 24 && stored_nt) {
395 if (lp_ntlm_auth()) {
396 /* We have the NT MD4 hash challenge available - see if we can
397 use it (ie. does it exist in the smbpasswd file).
399 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
400 if (smb_pwd_check_ntlmv1(mem_ctx,
401 nt_response,
402 stored_nt->hash, challenge,
403 user_sess_key)) {
404 /* The LM session key for this response is not very secure,
405 so use it only if we otherwise allow LM authentication */
407 if (lp_lanman_auth() && stored_lanman) {
408 *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8);
410 return NT_STATUS_OK;
411 } else {
412 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
413 username));
414 return NT_STATUS_WRONG_PASSWORD;
416 } else {
417 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
418 username));
419 /* no return, becouse we might pick up LMv2 in the LM field */
423 if (lm_response->length == 0) {
424 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
425 username));
426 return NT_STATUS_WRONG_PASSWORD;
429 if (lm_response->length < 24) {
430 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
431 (unsigned long)nt_response->length, username));
432 return NT_STATUS_WRONG_PASSWORD;
435 if (!lp_lanman_auth()) {
436 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
437 username));
438 } else if (!stored_lanman) {
439 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
440 username));
441 } else if (strchr_m(username, '@')) {
442 DEBUG(3,("ntlm_password_check: NO LanMan password allowed for username@realm logins (user: %s)\n",
443 username));
444 } else {
445 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
446 if (smb_pwd_check_ntlmv1(mem_ctx,
447 lm_response,
448 stored_lanman->hash, challenge,
449 NULL)) {
450 /* The session key for this response is still very odd.
451 It not very secure, so use it only if we otherwise
452 allow LM authentication */
454 if (lp_lanman_auth() && stored_lanman) {
455 uint8_t first_8_lm_hash[16];
456 memcpy(first_8_lm_hash, stored_lanman->hash, 8);
457 memset(first_8_lm_hash + 8, '\0', 8);
458 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
459 *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8);
461 return NT_STATUS_OK;
465 if (!stored_nt) {
466 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
467 return NT_STATUS_WRONG_PASSWORD;
470 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
471 - related to Win9X, legacy NAS pass-though authentication
473 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
474 if (smb_pwd_check_ntlmv2(mem_ctx,
475 lm_response,
476 stored_nt->hash, challenge,
477 client_username,
478 client_domain,
479 False,
480 &tmp_sess_key)) {
481 if (nt_response->length > 24) {
482 /* If NTLMv2 authentication has preceeded us
483 * (even if it failed), then use the session
484 * key from that. See the RPC-SAMLOGON
485 * torture test */
486 smb_sess_key_ntlmv2(mem_ctx,
487 nt_response,
488 stored_nt->hash, challenge,
489 client_username,
490 client_domain,
491 False,
492 user_sess_key);
493 } else {
494 /* Otherwise, use the LMv2 session key */
495 *user_sess_key = tmp_sess_key;
497 *lm_sess_key = *user_sess_key;
498 if (user_sess_key->length) {
499 lm_sess_key->length = 8;
501 return NT_STATUS_OK;
504 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain));
505 if (smb_pwd_check_ntlmv2(mem_ctx,
506 lm_response,
507 stored_nt->hash, challenge,
508 client_username,
509 client_domain,
510 True,
511 &tmp_sess_key)) {
512 if (nt_response->length > 24) {
513 /* If NTLMv2 authentication has preceeded us
514 * (even if it failed), then use the session
515 * key from that. See the RPC-SAMLOGON
516 * torture test */
517 smb_sess_key_ntlmv2(mem_ctx,
518 nt_response,
519 stored_nt->hash, challenge,
520 client_username,
521 client_domain,
522 True,
523 user_sess_key);
524 } else {
525 /* Otherwise, use the LMv2 session key */
526 *user_sess_key = tmp_sess_key;
528 *lm_sess_key = *user_sess_key;
529 if (user_sess_key->length) {
530 lm_sess_key->length = 8;
532 return NT_STATUS_OK;
535 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
536 if (smb_pwd_check_ntlmv2(mem_ctx,
537 lm_response,
538 stored_nt->hash, challenge,
539 client_username,
541 False,
542 &tmp_sess_key)) {
543 if (nt_response->length > 24) {
544 /* If NTLMv2 authentication has preceeded us
545 * (even if it failed), then use the session
546 * key from that. See the RPC-SAMLOGON
547 * torture test */
548 smb_sess_key_ntlmv2(mem_ctx,
549 nt_response,
550 stored_nt->hash, challenge,
551 client_username,
553 False,
554 user_sess_key);
555 } else {
556 /* Otherwise, use the LMv2 session key */
557 *user_sess_key = tmp_sess_key;
559 *lm_sess_key = *user_sess_key;
560 if (user_sess_key->length) {
561 lm_sess_key->length = 8;
563 return NT_STATUS_OK;
566 /* Apparently NT accepts NT responses in the LM field
567 - I think this is related to Win9X pass-though authentication
569 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
570 if (lp_ntlm_auth()) {
571 if (smb_pwd_check_ntlmv1(mem_ctx,
572 lm_response,
573 stored_nt->hash, challenge,
574 NULL)) {
575 /* The session key for this response is still very odd.
576 It not very secure, so use it only if we otherwise
577 allow LM authentication */
579 if (lp_lanman_auth() && stored_lanman) {
580 uint8_t first_8_lm_hash[16];
581 memcpy(first_8_lm_hash, stored_lanman->hash, 8);
582 memset(first_8_lm_hash + 8, '\0', 8);
583 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
584 *lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8);
586 return NT_STATUS_OK;
588 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
589 } else {
590 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
593 /* Try and match error codes */
594 if (strchr_m(username, '@')) {
595 return NT_STATUS_NOT_FOUND;
597 return NT_STATUS_WRONG_PASSWORD;