r38: Fix caching of name->sid lookups
[Samba/gebeck_regimport.git] / source3 / libsmb / ntlm_check.c
bloba7764f9e98639945a1af6a952cd56cd39e03c369
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 #if DEBUG_PASSWORD
67 DEBUG(100,("Part password (P16) was |\n"));
68 dump_data(100, part_passwd, 16);
69 DEBUGADD(100,("Password from client was |\n"));
70 dump_data(100, nt_response->data, nt_response->length);
71 DEBUGADD(100,("Given challenge was |\n"));
72 dump_data(100, sec_blob->data, sec_blob->length);
73 DEBUGADD(100,("Value from encryption was |\n"));
74 dump_data(100, 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;
97 if (part_passwd == NULL) {
98 DEBUG(10,("No password set - DISALLOWING access\n"));
99 /* No password set - always False */
100 return False;
103 if (sec_blob->length != 8) {
104 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
105 (unsigned long)sec_blob->length));
106 return False;
109 if (ntv2_response->length < 24) {
110 /* We MUST have more than 16 bytes, or the stuff below will go
111 crazy. No known implementation sends less than the 24 bytes
112 for LMv2, let alone NTLMv2. */
113 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
114 (unsigned long)ntv2_response->length));
115 return False;
118 client_key_data = data_blob(ntv2_response->data+16, ntv2_response->length-16);
120 todo: should we be checking this for anything? We can't for LMv2,
121 but for NTLMv2 it is meant to contain the current time etc.
124 memcpy(client_response, ntv2_response->data, sizeof(client_response));
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);
131 if (user_sess_key != NULL) {
132 *user_sess_key = data_blob(NULL, 16);
133 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
136 #if DEBUG_PASSWORD
137 DEBUG(100,("Part password (P16) was |\n"));
138 dump_data(100, part_passwd, 16);
139 DEBUGADD(100,("Password from client was |\n"));
140 dump_data(100, ntv2_response->data, ntv2_response->length);
141 DEBUGADD(100,("Variable data from client was |\n"));
142 dump_data(100, client_key_data.data, client_key_data.length);
143 DEBUGADD(100,("Given challenge was |\n"));
144 dump_data(100, sec_blob->data, sec_blob->length);
145 DEBUGADD(100,("Value from encryption was |\n"));
146 dump_data(100, value_from_encryption, 16);
147 #endif
148 data_blob_clear_free(&client_key_data);
149 return (memcmp(value_from_encryption, client_response, 16) == 0);
153 * Check a challenge-response password against the value of the NT or
154 * LM password hash.
156 * @param mem_ctx talloc context
157 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
158 * @param nt_response 'unicode' NT response to the challenge, or unicode password
159 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
160 * @param username internal Samba username, for log messages
161 * @param client_username username the client used
162 * @param client_domain domain name the client used (may be mapped)
163 * @param nt_pw MD4 unicode password from our passdb or similar
164 * @param lm_pw LANMAN ASCII password from our passdb or similar
165 * @param user_sess_key User session key
166 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
169 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
170 const DATA_BLOB *challenge,
171 const DATA_BLOB *lm_response,
172 const DATA_BLOB *nt_response,
173 const DATA_BLOB *lm_interactive_pwd,
174 const DATA_BLOB *nt_interactive_pwd,
175 const char *username,
176 const char *client_username,
177 const char *client_domain,
178 const uint8 *lm_pw, const uint8 *nt_pw,
179 DATA_BLOB *user_sess_key,
180 DATA_BLOB *lm_sess_key)
182 static const unsigned char zeros[8];
183 if (nt_pw == NULL) {
184 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
185 username));
188 if (nt_interactive_pwd && nt_interactive_pwd->length && nt_pw) {
189 if (nt_interactive_pwd->length != 16) {
190 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_pwd->length,
191 username));
192 return NT_STATUS_WRONG_PASSWORD;
195 if (memcmp(nt_interactive_pwd->data, nt_pw, 16) == 0) {
196 if (user_sess_key) {
197 *user_sess_key = data_blob(NULL, 16);
198 SMBsesskeygen_ntv1(nt_pw, NULL, user_sess_key->data);
200 return NT_STATUS_OK;
201 } else {
202 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
203 username));
204 return NT_STATUS_WRONG_PASSWORD;
207 } else if (lm_interactive_pwd && lm_interactive_pwd->length && lm_pw) {
208 if (lm_interactive_pwd->length != 16) {
209 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_pwd->length,
210 username));
211 return NT_STATUS_WRONG_PASSWORD;
214 if (!lp_lanman_auth()) {
215 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
216 username));
217 return NT_STATUS_WRONG_PASSWORD;
220 if (memcmp(lm_interactive_pwd->data, lm_pw, 16) == 0) {
221 return NT_STATUS_OK;
222 } else {
223 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
224 username));
225 return NT_STATUS_WRONG_PASSWORD;
229 /* Check for cleartext netlogon. Used by Exchange 5.5. */
230 if (challenge->length == sizeof(zeros) &&
231 (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
233 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
234 username));
235 if (nt_pw && nt_response->length) {
236 unsigned char pwhash[16];
237 mdfour(pwhash, nt_response->data, nt_response->length);
238 if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
239 return NT_STATUS_OK;
240 } else {
241 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
242 username));
243 return NT_STATUS_WRONG_PASSWORD;
246 } else if (!lp_lanman_auth()) {
247 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
248 username));
250 } else if (lm_pw && lm_response->length) {
251 uchar dospwd[14];
252 uchar p16[16];
253 ZERO_STRUCT(dospwd);
255 memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
256 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
258 /* we *might* need to upper-case the string here */
259 E_P16((const unsigned char *)dospwd, p16);
261 if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
262 return NT_STATUS_OK;
263 } else {
264 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
265 username));
266 return NT_STATUS_WRONG_PASSWORD;
268 } else {
269 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
270 return NT_STATUS_WRONG_PASSWORD;
274 if (nt_response->length != 0 && nt_response->length < 24) {
275 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
276 (unsigned long)nt_response->length, username));
279 if (nt_response->length >= 24 && nt_pw) {
280 if (nt_response->length > 24) {
281 /* We have the NT MD4 hash challenge available - see if we can
282 use it
284 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
285 if (smb_pwd_check_ntlmv2( nt_response,
286 nt_pw, challenge,
287 client_username,
288 client_domain,
289 False,
290 user_sess_key)) {
291 return NT_STATUS_OK;
294 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
295 if (smb_pwd_check_ntlmv2( nt_response,
296 nt_pw, challenge,
297 client_username,
298 client_domain,
299 True,
300 user_sess_key)) {
301 return NT_STATUS_OK;
304 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
305 if (smb_pwd_check_ntlmv2( nt_response,
306 nt_pw, challenge,
307 client_username,
309 False,
310 user_sess_key)) {
311 return NT_STATUS_OK;
312 } else {
313 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
314 return NT_STATUS_WRONG_PASSWORD;
318 if (lp_ntlm_auth()) {
319 /* We have the NT MD4 hash challenge available - see if we can
320 use it (ie. does it exist in the smbpasswd file).
322 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
323 if (smb_pwd_check_ntlmv1(nt_response,
324 nt_pw, challenge,
325 user_sess_key)) {
326 /* The LM session key for this response is not very secure,
327 so use it only if we otherwise allow LM authentication */
329 if (lp_lanman_auth() && lm_pw) {
330 uint8 first_8_lm_hash[16];
331 memcpy(first_8_lm_hash, lm_pw, 8);
332 memset(first_8_lm_hash + 8, '\0', 8);
333 *lm_sess_key = data_blob(first_8_lm_hash, 16);
335 return NT_STATUS_OK;
336 } else {
337 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
338 username));
339 return NT_STATUS_WRONG_PASSWORD;
341 } else {
342 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
343 username));
344 /* no return, becouse we might pick up LMv2 in the LM field */
348 if (lm_response->length == 0) {
349 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
350 username));
351 return NT_STATUS_WRONG_PASSWORD;
354 if (lm_response->length < 24) {
355 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
356 (unsigned long)nt_response->length, username));
357 return NT_STATUS_WRONG_PASSWORD;
360 if (!lp_lanman_auth()) {
361 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
362 username));
363 } else if (!lm_pw) {
364 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
365 username));
366 } else {
367 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
368 if (smb_pwd_check_ntlmv1(lm_response,
369 lm_pw, challenge,
370 NULL)) {
371 uint8 first_8_lm_hash[16];
372 memcpy(first_8_lm_hash, lm_pw, 8);
373 memset(first_8_lm_hash + 8, '\0', 8);
374 *user_sess_key = data_blob(first_8_lm_hash, 16);
375 *lm_sess_key = data_blob(first_8_lm_hash, 16);
376 return NT_STATUS_OK;
380 if (!nt_pw) {
381 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
382 return NT_STATUS_WRONG_PASSWORD;
385 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
386 - related to Win9X, legacy NAS pass-though authentication
388 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
389 if (smb_pwd_check_ntlmv2( lm_response,
390 nt_pw, challenge,
391 client_username,
392 client_domain,
393 False,
394 NULL)) {
395 return NT_STATUS_OK;
398 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain));
399 if (smb_pwd_check_ntlmv2( lm_response,
400 nt_pw, challenge,
401 client_username,
402 client_domain,
403 True,
404 NULL)) {
405 return NT_STATUS_OK;
408 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
409 if (smb_pwd_check_ntlmv2( lm_response,
410 nt_pw, challenge,
411 client_username,
413 False,
414 NULL)) {
415 return NT_STATUS_OK;
418 /* Apparently NT accepts NT responses in the LM field
419 - I think this is related to Win9X pass-though authentication
421 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
422 if (lp_ntlm_auth()) {
423 if (smb_pwd_check_ntlmv1(lm_response,
424 nt_pw, challenge,
425 NULL)) {
426 /* The session key for this response is still very odd.
427 It not very secure, so use it only if we otherwise
428 allow LM authentication */
430 if (lp_lanman_auth() && lm_pw) {
431 uint8 first_8_lm_hash[16];
432 memcpy(first_8_lm_hash, lm_pw, 8);
433 memset(first_8_lm_hash + 8, '\0', 8);
434 *user_sess_key = data_blob(first_8_lm_hash, 16);
435 *lm_sess_key = data_blob(first_8_lm_hash, 16);
437 return NT_STATUS_OK;
439 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
440 } else {
441 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
443 return NT_STATUS_WRONG_PASSWORD;