r14683: Get rid of hardcoded output file. With no arg, print to stdout,
[Samba.git] / source / libsmb / ntlm_check.c
blob212bc19767de331e0525ed00637ae16317427677
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6 Copyright (C) Andrew Bartlett 2001-2003
7 Copyright (C) Gerald Carter 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 Core of smb password checking routine.
31 ****************************************************************************/
33 static BOOL smb_pwd_check_ntlmv1(const DATA_BLOB *nt_response,
34 const uchar *part_passwd,
35 const DATA_BLOB *sec_blob,
36 DATA_BLOB *user_sess_key)
38 /* Finish the encryption of part_passwd. */
39 uchar 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);
60 if (user_sess_key != NULL) {
61 *user_sess_key = data_blob(NULL, 16);
62 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key->data);
66 #ifdef DEBUG_PASSWORD
67 DEBUG(100,("Part password (P16) was |\n"));
68 dump_data(100, (const char *)part_passwd, 16);
69 DEBUGADD(100,("Password from client was |\n"));
70 dump_data(100, (const char *)nt_response->data, nt_response->length);
71 DEBUGADD(100,("Given challenge was |\n"));
72 dump_data(100, (const char *)sec_blob->data, sec_blob->length);
73 DEBUGADD(100,("Value from encryption was |\n"));
74 dump_data(100, (const char *)p24, 24);
75 #endif
76 return (memcmp(p24, nt_response->data, 24) == 0);
79 /****************************************************************************
80 Core of smb password checking routine. (NTLMv2, LMv2)
81 Note: The same code works with both NTLMv2 and LMv2.
82 ****************************************************************************/
84 static BOOL smb_pwd_check_ntlmv2(const DATA_BLOB *ntv2_response,
85 const uchar *part_passwd,
86 const DATA_BLOB *sec_blob,
87 const char *user, const char *domain,
88 BOOL upper_case_domain, /* should the domain be transformed into upper case? */
89 DATA_BLOB *user_sess_key)
91 /* Finish the encryption of part_passwd. */
92 uchar kr[16];
93 uchar value_from_encryption[16];
94 uchar client_response[16];
95 DATA_BLOB client_key_data;
96 BOOL res;
98 if (part_passwd == NULL) {
99 DEBUG(10,("No password set - DISALLOWING access\n"));
100 /* No password set - always False */
101 return False;
104 if (sec_blob->length != 8) {
105 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
106 (unsigned long)sec_blob->length));
107 return False;
110 if (ntv2_response->length < 24) {
111 /* We MUST have more than 16 bytes, or the stuff below will go
112 crazy. No known implementation sends less than the 24 bytes
113 for LMv2, let alone NTLMv2. */
114 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
115 (unsigned long)ntv2_response->length));
116 return False;
119 client_key_data = data_blob(ntv2_response->data+16, ntv2_response->length-16);
121 todo: should we be checking this for anything? We can't for LMv2,
122 but for NTLMv2 it is meant to contain the current time etc.
125 memcpy(client_response, ntv2_response->data, sizeof(client_response));
127 if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
128 return False;
131 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
132 if (user_sess_key != NULL) {
133 *user_sess_key = data_blob(NULL, 16);
134 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
137 #if DEBUG_PASSWORD
138 DEBUG(100,("Part password (P16) was |\n"));
139 dump_data(100, (const char *)part_passwd, 16);
140 DEBUGADD(100,("Password from client was |\n"));
141 dump_data(100, (const char *)ntv2_response->data, ntv2_response->length);
142 DEBUGADD(100,("Variable data from client was |\n"));
143 dump_data(100, (const char *)client_key_data.data, client_key_data.length);
144 DEBUGADD(100,("Given challenge was |\n"));
145 dump_data(100, (const char *)sec_blob->data, sec_blob->length);
146 DEBUGADD(100,("Value from encryption was |\n"));
147 dump_data(100, (const char *)value_from_encryption, 16);
148 #endif
149 data_blob_clear_free(&client_key_data);
150 res = (memcmp(value_from_encryption, client_response, 16) == 0);
151 if ((!res) && (user_sess_key != NULL))
152 data_blob_clear_free(user_sess_key);
153 return res;
157 * Check a challenge-response password against the value of the NT or
158 * LM password hash.
160 * @param mem_ctx talloc context
161 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
162 * @param nt_response 'unicode' NT response to the challenge, or unicode password
163 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
164 * @param username internal Samba username, for log messages
165 * @param client_username username the client used
166 * @param client_domain domain name the client used (may be mapped)
167 * @param nt_pw MD4 unicode password from our passdb or similar
168 * @param lm_pw LANMAN ASCII password from our passdb or similar
169 * @param user_sess_key User session key
170 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
173 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
174 const DATA_BLOB *challenge,
175 const DATA_BLOB *lm_response,
176 const DATA_BLOB *nt_response,
177 const DATA_BLOB *lm_interactive_pwd,
178 const DATA_BLOB *nt_interactive_pwd,
179 const char *username,
180 const char *client_username,
181 const char *client_domain,
182 const uint8 *lm_pw, const uint8 *nt_pw,
183 DATA_BLOB *user_sess_key,
184 DATA_BLOB *lm_sess_key)
186 static const unsigned char zeros[8];
187 if (nt_pw == NULL) {
188 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
189 username));
192 if (nt_interactive_pwd && nt_interactive_pwd->length && nt_pw) {
193 if (nt_interactive_pwd->length != 16) {
194 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_pwd->length,
195 username));
196 return NT_STATUS_WRONG_PASSWORD;
199 if (memcmp(nt_interactive_pwd->data, nt_pw, 16) == 0) {
200 if (user_sess_key) {
201 *user_sess_key = data_blob(NULL, 16);
202 SMBsesskeygen_ntv1(nt_pw, NULL, user_sess_key->data);
204 return NT_STATUS_OK;
205 } else {
206 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
207 username));
208 return NT_STATUS_WRONG_PASSWORD;
211 } else if (lm_interactive_pwd && lm_interactive_pwd->length && lm_pw) {
212 if (lm_interactive_pwd->length != 16) {
213 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_pwd->length,
214 username));
215 return NT_STATUS_WRONG_PASSWORD;
218 if (!lp_lanman_auth()) {
219 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
220 username));
221 return NT_STATUS_WRONG_PASSWORD;
224 if (memcmp(lm_interactive_pwd->data, lm_pw, 16) == 0) {
225 return NT_STATUS_OK;
226 } else {
227 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
228 username));
229 return NT_STATUS_WRONG_PASSWORD;
233 /* Check for cleartext netlogon. Used by Exchange 5.5. */
234 if (challenge->length == sizeof(zeros) &&
235 (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
237 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
238 username));
239 if (nt_pw && nt_response->length) {
240 unsigned char pwhash[16];
241 mdfour(pwhash, nt_response->data, nt_response->length);
242 if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
243 return NT_STATUS_OK;
244 } else {
245 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
246 username));
247 return NT_STATUS_WRONG_PASSWORD;
250 } else if (!lp_lanman_auth()) {
251 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
252 username));
254 } else if (lm_pw && lm_response->length) {
255 uchar dospwd[14];
256 uchar p16[16];
257 ZERO_STRUCT(dospwd);
259 memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
260 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
262 /* we *might* need to upper-case the string here */
263 E_P16((const unsigned char *)dospwd, p16);
265 if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
266 return NT_STATUS_OK;
267 } else {
268 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
269 username));
270 return NT_STATUS_WRONG_PASSWORD;
272 } else {
273 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
274 return NT_STATUS_WRONG_PASSWORD;
278 if (nt_response->length != 0 && nt_response->length < 24) {
279 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
280 (unsigned long)nt_response->length, username));
283 if (nt_response->length >= 24 && nt_pw) {
284 if (nt_response->length > 24) {
285 /* We have the NT MD4 hash challenge available - see if we can
286 use it
288 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
289 if (smb_pwd_check_ntlmv2( nt_response,
290 nt_pw, challenge,
291 client_username,
292 client_domain,
293 False,
294 user_sess_key)) {
295 return NT_STATUS_OK;
298 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
299 if (smb_pwd_check_ntlmv2( nt_response,
300 nt_pw, challenge,
301 client_username,
302 client_domain,
303 True,
304 user_sess_key)) {
305 return NT_STATUS_OK;
308 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
309 if (smb_pwd_check_ntlmv2( nt_response,
310 nt_pw, challenge,
311 client_username,
313 False,
314 user_sess_key)) {
315 return NT_STATUS_OK;
316 } else {
317 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
318 return NT_STATUS_WRONG_PASSWORD;
322 if (lp_ntlm_auth()) {
323 /* We have the NT MD4 hash challenge available - see if we can
324 use it (ie. does it exist in the smbpasswd file).
326 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
327 if (smb_pwd_check_ntlmv1(nt_response,
328 nt_pw, challenge,
329 user_sess_key)) {
330 /* The LM session key for this response is not very secure,
331 so use it only if we otherwise allow LM authentication */
333 if (lp_lanman_auth() && lm_pw) {
334 uint8 first_8_lm_hash[16];
335 memcpy(first_8_lm_hash, lm_pw, 8);
336 memset(first_8_lm_hash + 8, '\0', 8);
337 if (lm_sess_key) {
338 *lm_sess_key = data_blob(first_8_lm_hash, 16);
341 return NT_STATUS_OK;
342 } else {
343 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
344 username));
345 return NT_STATUS_WRONG_PASSWORD;
347 } else {
348 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
349 username));
350 /* no return, becouse we might pick up LMv2 in the LM field */
354 if (lm_response->length == 0) {
355 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
356 username));
357 return NT_STATUS_WRONG_PASSWORD;
360 if (lm_response->length < 24) {
361 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
362 (unsigned long)nt_response->length, username));
363 return NT_STATUS_WRONG_PASSWORD;
366 if (!lp_lanman_auth()) {
367 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
368 username));
369 } else if (!lm_pw) {
370 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
371 username));
372 } else {
373 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
374 if (smb_pwd_check_ntlmv1(lm_response,
375 lm_pw, challenge,
376 NULL)) {
377 uint8 first_8_lm_hash[16];
378 memcpy(first_8_lm_hash, lm_pw, 8);
379 memset(first_8_lm_hash + 8, '\0', 8);
380 if (user_sess_key) {
381 *user_sess_key = data_blob(first_8_lm_hash, 16);
384 if (lm_sess_key) {
385 *lm_sess_key = data_blob(first_8_lm_hash, 16);
387 return NT_STATUS_OK;
391 if (!nt_pw) {
392 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
393 return NT_STATUS_WRONG_PASSWORD;
396 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
397 - related to Win9X, legacy NAS pass-though authentication
399 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
400 if (smb_pwd_check_ntlmv2( lm_response,
401 nt_pw, challenge,
402 client_username,
403 client_domain,
404 False,
405 NULL)) {
406 return NT_STATUS_OK;
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( lm_response,
411 nt_pw, challenge,
412 client_username,
413 client_domain,
414 True,
415 NULL)) {
416 return NT_STATUS_OK;
419 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
420 if (smb_pwd_check_ntlmv2( lm_response,
421 nt_pw, challenge,
422 client_username,
424 False,
425 NULL)) {
426 return NT_STATUS_OK;
429 /* Apparently NT accepts NT responses in the LM field
430 - I think this is related to Win9X pass-though authentication
432 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
433 if (lp_ntlm_auth()) {
434 if (smb_pwd_check_ntlmv1(lm_response,
435 nt_pw, challenge,
436 NULL)) {
437 /* The session key for this response is still very odd.
438 It not very secure, so use it only if we otherwise
439 allow LM authentication */
441 if (lp_lanman_auth() && lm_pw) {
442 uint8 first_8_lm_hash[16];
443 memcpy(first_8_lm_hash, lm_pw, 8);
444 memset(first_8_lm_hash + 8, '\0', 8);
445 if (user_sess_key) {
446 *user_sess_key = data_blob(first_8_lm_hash, 16);
449 if (lm_sess_key) {
450 *lm_sess_key = data_blob(first_8_lm_hash, 16);
453 return NT_STATUS_OK;
455 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
456 } else {
457 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
459 return NT_STATUS_WRONG_PASSWORD;