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 3 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, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_AUTH
28 /****************************************************************************
29 Core of smb password checking routine.
30 ****************************************************************************/
32 static bool smb_pwd_check_ntlmv1(const DATA_BLOB
*nt_response
,
33 const uchar
*part_passwd
,
34 const DATA_BLOB
*sec_blob
,
35 DATA_BLOB
*user_sess_key
)
37 /* Finish the encryption of part_passwd. */
40 if (part_passwd
== NULL
) {
41 DEBUG(10,("No password set - DISALLOWING access\n"));
42 /* No password set - always false ! */
46 if (sec_blob
->length
!= 8) {
47 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n",
48 (unsigned long)sec_blob
->length
));
52 if (nt_response
->length
!= 24) {
53 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n",
54 (unsigned long)nt_response
->length
));
58 SMBOWFencrypt(part_passwd
, sec_blob
->data
, p24
);
59 if (user_sess_key
!= NULL
) {
60 *user_sess_key
= data_blob(NULL
, 16);
61 SMBsesskeygen_ntv1(part_passwd
, NULL
, user_sess_key
->data
);
66 DEBUG(100,("Part password (P16) was |\n"));
67 dump_data(100, part_passwd
, 16);
68 DEBUGADD(100,("Password from client was |\n"));
69 dump_data(100, nt_response
->data
, nt_response
->length
);
70 DEBUGADD(100,("Given challenge was |\n"));
71 dump_data(100, sec_blob
->data
, sec_blob
->length
);
72 DEBUGADD(100,("Value from encryption was |\n"));
73 dump_data(100, p24
, 24);
75 return (memcmp(p24
, nt_response
->data
, 24) == 0);
78 /****************************************************************************
79 Core of smb password checking routine. (NTLMv2, LMv2)
80 Note: The same code works with both NTLMv2 and LMv2.
81 ****************************************************************************/
83 static bool smb_pwd_check_ntlmv2(TALLOC_CTX
*mem_ctx
,
84 const DATA_BLOB
*ntv2_response
,
85 const uint8_t *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. */
93 uint8_t value_from_encryption
[16];
94 uint8_t client_response
[16];
95 DATA_BLOB client_key_data
;
98 if (part_passwd
== NULL
) {
99 DEBUG(10,("No password set - DISALLOWING access\n"));
100 /* No password set - always 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
));
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
));
119 client_key_data
= data_blob_talloc(mem_ctx
, 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
)) {
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
);
138 DEBUG(100,("Part password (P16) was |\n"));
139 dump_data(100, part_passwd
, 16);
140 DEBUGADD(100,("Password from client was |\n"));
141 dump_data(100, ntv2_response
->data
, ntv2_response
->length
);
142 DEBUGADD(100,("Variable data from client was |\n"));
143 dump_data(100, client_key_data
.data
, client_key_data
.length
);
144 DEBUGADD(100,("Given challenge was |\n"));
145 dump_data(100, sec_blob
->data
, sec_blob
->length
);
146 DEBUGADD(100,("Value from encryption was |\n"));
147 dump_data(100, value_from_encryption
, 16);
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
);
157 * Check a challenge-response password against the value of the NT or
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_t *lm_pw
, const uint8_t *nt_pw
,
183 DATA_BLOB
*user_sess_key
,
184 DATA_BLOB
*lm_sess_key
)
186 unsigned char zeros
[8];
191 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
195 if (nt_interactive_pwd
&& nt_interactive_pwd
->length
&& nt_pw
) {
196 if (nt_interactive_pwd
->length
!= 16) {
197 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_pwd
->length
,
199 return NT_STATUS_WRONG_PASSWORD
;
202 if (memcmp(nt_interactive_pwd
->data
, nt_pw
, 16) == 0) {
204 *user_sess_key
= data_blob(NULL
, 16);
205 SMBsesskeygen_ntv1(nt_pw
, NULL
, user_sess_key
->data
);
209 DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
211 return NT_STATUS_WRONG_PASSWORD
;
214 } else if (lm_interactive_pwd
&& lm_interactive_pwd
->length
&& lm_pw
) {
215 if (lm_interactive_pwd
->length
!= 16) {
216 DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_pwd
->length
,
218 return NT_STATUS_WRONG_PASSWORD
;
221 if (!lp_lanman_auth()) {
222 DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
224 return NT_STATUS_WRONG_PASSWORD
;
227 if (memcmp(lm_interactive_pwd
->data
, lm_pw
, 16) == 0) {
230 DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
232 return NT_STATUS_WRONG_PASSWORD
;
236 /* Check for cleartext netlogon. Used by Exchange 5.5. */
237 if (challenge
->length
== sizeof(zeros
) &&
238 (memcmp(challenge
->data
, zeros
, challenge
->length
) == 0 )) {
240 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
242 if (nt_pw
&& nt_response
->length
) {
243 unsigned char pwhash
[16];
244 mdfour(pwhash
, nt_response
->data
, nt_response
->length
);
245 if (memcmp(pwhash
, nt_pw
, sizeof(pwhash
)) == 0) {
248 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
250 return NT_STATUS_WRONG_PASSWORD
;
253 } else if (!lp_lanman_auth()) {
254 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
257 } else if (lm_pw
&& lm_response
->length
) {
262 memcpy(dospwd
, lm_response
->data
, MIN(lm_response
->length
, sizeof(dospwd
)));
263 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
265 /* we *might* need to upper-case the string here */
266 E_P16((const unsigned char *)dospwd
, p16
);
268 if (memcmp(p16
, lm_pw
, sizeof(p16
)) == 0) {
271 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
273 return NT_STATUS_WRONG_PASSWORD
;
276 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username
));
277 return NT_STATUS_WRONG_PASSWORD
;
281 if (nt_response
->length
!= 0 && nt_response
->length
< 24) {
282 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
283 (unsigned long)nt_response
->length
, username
));
286 if (nt_response
->length
>= 24 && nt_pw
) {
287 if (nt_response
->length
> 24) {
288 /* We have the NT MD4 hash challenge available - see if we can
291 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain
));
292 if (smb_pwd_check_ntlmv2(mem_ctx
,
302 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain
));
303 if (smb_pwd_check_ntlmv2(mem_ctx
,
313 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
314 if (smb_pwd_check_ntlmv2(mem_ctx
,
323 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
324 return NT_STATUS_WRONG_PASSWORD
;
328 if (lp_ntlm_auth()) {
329 /* We have the NT MD4 hash challenge available - see if we can
330 use it (ie. does it exist in the smbpasswd file).
332 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
333 if (smb_pwd_check_ntlmv1(nt_response
,
336 /* The LM session key for this response is not very secure,
337 so use it only if we otherwise allow LM authentication */
339 if (lp_lanman_auth() && lm_pw
) {
340 uint8_t first_8_lm_hash
[16];
341 memcpy(first_8_lm_hash
, lm_pw
, 8);
342 memset(first_8_lm_hash
+ 8, '\0', 8);
344 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
349 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
351 return NT_STATUS_WRONG_PASSWORD
;
354 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
356 /* no return, becouse we might pick up LMv2 in the LM field */
360 if (lm_response
->length
== 0) {
361 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
363 return NT_STATUS_WRONG_PASSWORD
;
366 if (lm_response
->length
< 24) {
367 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
368 (unsigned long)nt_response
->length
, username
));
369 return NT_STATUS_WRONG_PASSWORD
;
372 if (!lp_lanman_auth()) {
373 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
376 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
379 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
380 if (smb_pwd_check_ntlmv1(lm_response
,
383 uint8_t first_8_lm_hash
[16];
384 memcpy(first_8_lm_hash
, lm_pw
, 8);
385 memset(first_8_lm_hash
+ 8, '\0', 8);
387 *user_sess_key
= data_blob(first_8_lm_hash
, 16);
391 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
398 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username
));
399 return NT_STATUS_WRONG_PASSWORD
;
402 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
403 - related to Win9X, legacy NAS pass-though authentication
405 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain
));
406 if (smb_pwd_check_ntlmv2(mem_ctx
,
416 DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain
));
417 if (smb_pwd_check_ntlmv2(mem_ctx
,
427 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
428 if (smb_pwd_check_ntlmv2(mem_ctx
,
438 /* Apparently NT accepts NT responses in the LM field
439 - I think this is related to Win9X pass-though authentication
441 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
442 if (lp_ntlm_auth()) {
443 if (smb_pwd_check_ntlmv1(lm_response
,
446 /* The session key for this response is still very odd.
447 It not very secure, so use it only if we otherwise
448 allow LM authentication */
450 if (lp_lanman_auth() && lm_pw
) {
451 uint8_t first_8_lm_hash
[16];
452 memcpy(first_8_lm_hash
, lm_pw
, 8);
453 memset(first_8_lm_hash
+ 8, '\0', 8);
455 *user_sess_key
= data_blob(first_8_lm_hash
, 16);
459 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
464 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username
));
466 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username
));
468 return NT_STATUS_WRONG_PASSWORD
;