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.
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. */
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 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
);
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);
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 DATA_BLOB
*user_sess_key
)
90 /* Finish the encryption of part_passwd. */
92 uchar value_from_encryption
[16];
93 uchar client_response
[16];
94 DATA_BLOB client_key_data
;
96 if (part_passwd
== NULL
) {
97 DEBUG(10,("No password set - DISALLOWING access\n"));
98 /* No password set - always False */
102 if (sec_blob
->length
!= 8) {
103 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n",
104 (unsigned long)sec_blob
->length
));
108 if (ntv2_response
->length
< 24) {
109 /* We MUST have more than 16 bytes, or the stuff below will go
110 crazy. No known implementation sends less than the 24 bytes
111 for LMv2, let alone NTLMv2. */
112 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
113 (unsigned long)ntv2_response
->length
));
117 client_key_data
= data_blob(ntv2_response
->data
+16, ntv2_response
->length
-16);
119 todo: should we be checking this for anything? We can't for LMv2,
120 but for NTLMv2 it is meant to contain the current time etc.
123 memcpy(client_response
, ntv2_response
->data
, sizeof(client_response
));
125 if (!ntv2_owf_gen(part_passwd
, user
, domain
, kr
)) {
129 SMBOWFencrypt_ntv2(kr
, sec_blob
, &client_key_data
, value_from_encryption
);
130 if (user_sess_key
!= NULL
) {
131 *user_sess_key
= data_blob(NULL
, 16);
132 SMBsesskeygen_ntv2(kr
, value_from_encryption
, user_sess_key
->data
);
136 DEBUG(100,("Part password (P16) was |\n"));
137 dump_data(100, part_passwd
, 16);
138 DEBUGADD(100,("Password from client was |\n"));
139 dump_data(100, ntv2_response
->data
, ntv2_response
->length
);
140 DEBUGADD(100,("Variable data from client was |\n"));
141 dump_data(100, client_key_data
.data
, client_key_data
.length
);
142 DEBUGADD(100,("Given challenge was |\n"));
143 dump_data(100, sec_blob
->data
, sec_blob
->length
);
144 DEBUGADD(100,("Value from encryption was |\n"));
145 dump_data(100, value_from_encryption
, 16);
147 data_blob_clear_free(&client_key_data
);
148 return (memcmp(value_from_encryption
, client_response
, 16) == 0);
152 * Check a challenge-response password against the value of the NT or
155 * @param mem_ctx talloc context
156 * @param challenge 8-byte challenge. If all zero, forces plaintext comparison
157 * @param nt_response 'unicode' NT response to the challenge, or unicode password
158 * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
159 * @param username internal Samba username, for log messages
160 * @param client_username username the client used
161 * @param client_domain domain name the client used (may be mapped)
162 * @param nt_pw MD4 unicode password from our passdb or similar
163 * @param lm_pw LANMAN ASCII password from our passdb or similar
164 * @param user_sess_key User session key
165 * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
168 NTSTATUS
ntlm_password_check(TALLOC_CTX
*mem_ctx
,
169 const DATA_BLOB
*challenge
,
170 const DATA_BLOB
*lm_response
,
171 const DATA_BLOB
*nt_response
,
172 const char *username
,
173 const char *client_username
,
174 const char *client_domain
,
175 const uint8
*lm_pw
, const uint8
*nt_pw
,
176 DATA_BLOB
*user_sess_key
,
177 DATA_BLOB
*lm_sess_key
)
179 static const unsigned char zeros
[8];
181 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n",
185 /* Check for cleartext netlogon. Used by Exchange 5.5. */
186 if (challenge
->length
== sizeof(zeros
) &&
187 (memcmp(challenge
->data
, zeros
, challenge
->length
) == 0 )) {
189 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
191 if (nt_pw
&& nt_response
->length
) {
192 unsigned char pwhash
[16];
193 mdfour(pwhash
, nt_response
->data
, nt_response
->length
);
194 if (memcmp(pwhash
, nt_pw
, sizeof(pwhash
)) == 0) {
197 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
199 return NT_STATUS_WRONG_PASSWORD
;
202 } else if (!lp_lanman_auth()) {
203 DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
206 } else if (lm_pw
&& lm_response
->length
) {
211 memcpy(dospwd
, lm_response
->data
, MIN(lm_response
->length
, sizeof(dospwd
)));
212 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
214 /* we *might* need to upper-case the string here */
215 E_P16((const unsigned char *)dospwd
, p16
);
217 if (memcmp(p16
, lm_pw
, sizeof(p16
)) == 0) {
220 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
222 return NT_STATUS_WRONG_PASSWORD
;
225 DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username
));
226 return NT_STATUS_WRONG_PASSWORD
;
230 if (nt_response
->length
!= 0 && nt_response
->length
< 24) {
231 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n",
232 (unsigned long)nt_response
->length
, username
));
235 if (nt_response
->length
>= 24 && nt_pw
) {
236 if (nt_response
->length
> 24) {
237 /* We have the NT MD4 hash challenge available - see if we can
238 use it (ie. does it exist in the smbpasswd file).
240 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain
));
241 if (smb_pwd_check_ntlmv2( nt_response
,
249 DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
250 if (smb_pwd_check_ntlmv2( nt_response
,
257 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
258 return NT_STATUS_WRONG_PASSWORD
;
262 if (lp_ntlm_auth()) {
263 /* We have the NT MD4 hash challenge available - see if we can
264 use it (ie. does it exist in the smbpasswd file).
266 DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
267 if (smb_pwd_check_ntlmv1(nt_response
,
270 /* The LM session key for this response is not very secure,
271 so use it only if we otherwise allow LM authentication */
273 if (lp_lanman_auth() && lm_pw
) {
274 uint8 first_8_lm_hash
[16];
275 memcpy(first_8_lm_hash
, lm_pw
, 8);
276 memset(first_8_lm_hash
+ 8, '\0', 8);
277 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
281 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
283 return NT_STATUS_WRONG_PASSWORD
;
286 DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
288 /* no return, becouse we might pick up LMv2 in the LM field */
292 if (lm_response
->length
== 0) {
293 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
295 return NT_STATUS_WRONG_PASSWORD
;
298 if (lm_response
->length
< 24) {
299 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n",
300 (unsigned long)nt_response
->length
, username
));
301 return NT_STATUS_WRONG_PASSWORD
;
304 if (!lp_lanman_auth()) {
305 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
308 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
311 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
312 if (smb_pwd_check_ntlmv1(lm_response
,
315 uint8 first_8_lm_hash
[16];
316 memcpy(first_8_lm_hash
, lm_pw
, 8);
317 memset(first_8_lm_hash
+ 8, '\0', 8);
318 *user_sess_key
= data_blob(first_8_lm_hash
, 16);
319 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
325 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username
));
326 return NT_STATUS_WRONG_PASSWORD
;
329 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
330 - related to Win9X, legacy NAS pass-though authentication
332 DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain
));
333 if (smb_pwd_check_ntlmv2( lm_response
,
341 DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
342 if (smb_pwd_check_ntlmv2( lm_response
,
350 /* Apparently NT accepts NT responses in the LM field
351 - I think this is related to Win9X pass-though authentication
353 DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
354 if (lp_ntlm_auth()) {
355 if (smb_pwd_check_ntlmv1(lm_response
,
358 /* The session key for this response is still very odd.
359 It not very secure, so use it only if we otherwise
360 allow LM authentication */
362 if (lp_lanman_auth() && lm_pw
) {
363 uint8 first_8_lm_hash
[16];
364 memcpy(first_8_lm_hash
, lm_pw
, 8);
365 memset(first_8_lm_hash
+ 8, '\0', 8);
366 *user_sess_key
= data_blob(first_8_lm_hash
, 16);
367 *lm_sess_key
= data_blob(first_8_lm_hash
, 16);
371 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username
));
373 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username
));
375 return NT_STATUS_WRONG_PASSWORD
;