2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "utils/ntlm_auth.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "nsswitch/winbind_client.h"
30 #define DBGC_CLASS DBGC_WINBIND
41 Authenticate a user with a challenge/response, checking session key
42 and valid authentication types
46 * Test the normal 'LM and NTLM' combination
49 static bool test_lm_ntlm_broken(enum ntlm_break break_which
)
54 DATA_BLOB lm_response
= data_blob(NULL
, 24);
55 DATA_BLOB nt_response
= data_blob(NULL
, 24);
56 DATA_BLOB session_key
= data_blob(NULL
, 16);
59 uchar user_session_key
[16];
62 DATA_BLOB chall
= get_challenge();
66 ZERO_STRUCT(user_session_key
);
68 flags
|= WBFLAG_PAM_LMKEY
;
69 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
71 SMBencrypt(opt_password
,chall
.data
,lm_response
.data
);
72 E_deshash(opt_password
, lm_hash
);
74 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
76 E_md4hash(opt_password
, nt_hash
);
77 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
79 switch (break_which
) {
83 lm_response
.data
[0]++;
86 nt_response
.data
[0]++;
89 data_blob_free(&lm_response
);
92 data_blob_free(&nt_response
);
96 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
104 &error_string
, NULL
);
106 data_blob_free(&lm_response
);
108 if (!NT_STATUS_IS_OK(nt_status
)) {
109 d_printf("%s (0x%x)\n",
111 NT_STATUS_V(nt_status
));
112 SAFE_FREE(error_string
);
113 return break_which
== BREAK_NT
;
116 if (memcmp(lm_hash
, lm_key
,
117 sizeof(lm_key
)) != 0) {
118 DEBUG(1, ("LM Key does not match expectations!\n"));
119 DEBUG(1, ("lm_key:\n"));
120 dump_data(1, lm_key
, 8);
121 DEBUG(1, ("expected:\n"));
122 dump_data(1, lm_hash
, 8);
126 if (break_which
== NO_NT
) {
127 if (memcmp(lm_hash
, user_session_key
,
129 DEBUG(1, ("NT Session Key does not match expectations (should be LM hash)!\n"));
130 DEBUG(1, ("user_session_key:\n"));
131 dump_data(1, user_session_key
, sizeof(user_session_key
));
132 DEBUG(1, ("expected:\n"));
133 dump_data(1, lm_hash
, sizeof(lm_hash
));
137 if (memcmp(session_key
.data
, user_session_key
,
138 sizeof(user_session_key
)) != 0) {
139 DEBUG(1, ("NT Session Key does not match expectations!\n"));
140 DEBUG(1, ("user_session_key:\n"));
141 dump_data(1, user_session_key
, 16);
142 DEBUG(1, ("expected:\n"));
143 dump_data(1, session_key
.data
, session_key
.length
);
151 * Test LM authentication, no NT response supplied
154 static bool test_lm(void)
157 return test_lm_ntlm_broken(NO_NT
);
161 * Test the NTLM response only, no LM.
164 static bool test_ntlm(void)
166 return test_lm_ntlm_broken(NO_LM
);
170 * Test the NTLM response only, but in the LM field.
173 static bool test_ntlm_in_lm(void)
178 DATA_BLOB nt_response
= data_blob(NULL
, 24);
182 uchar user_session_key
[16];
183 DATA_BLOB chall
= get_challenge();
186 ZERO_STRUCT(user_session_key
);
188 flags
|= WBFLAG_PAM_LMKEY
;
189 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
191 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
193 E_deshash(opt_password
, lm_hash
);
195 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
203 &error_string
, NULL
);
205 data_blob_free(&nt_response
);
207 if (!NT_STATUS_IS_OK(nt_status
)) {
208 d_printf("%s (0x%x)\n",
210 NT_STATUS_V(nt_status
));
211 SAFE_FREE(error_string
);
215 if (memcmp(lm_hash
, lm_key
,
216 sizeof(lm_key
)) != 0) {
217 DEBUG(1, ("LM Key does not match expectations!\n"));
218 DEBUG(1, ("lm_key:\n"));
219 dump_data(1, lm_key
, 8);
220 DEBUG(1, ("expected:\n"));
221 dump_data(1, lm_hash
, 8);
224 if (memcmp(lm_hash
, user_session_key
, 8) != 0) {
225 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
226 DEBUG(1, ("user_session_key:\n"));
227 dump_data(1, user_session_key
, 16);
228 DEBUG(1, ("expected:\n"));
229 dump_data(1, lm_hash
, 8);
236 * Test the NTLM response only, but in the both the NT and LM fields.
239 static bool test_ntlm_in_both(void)
244 DATA_BLOB nt_response
= data_blob(NULL
, 24);
245 DATA_BLOB session_key
= data_blob(NULL
, 16);
249 uint8 user_session_key
[16];
251 DATA_BLOB chall
= get_challenge();
255 ZERO_STRUCT(user_session_key
);
257 flags
|= WBFLAG_PAM_LMKEY
;
258 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
260 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
261 E_md4hash(opt_password
, nt_hash
);
262 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
264 E_deshash(opt_password
, lm_hash
);
266 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
274 &error_string
, NULL
);
276 data_blob_free(&nt_response
);
278 if (!NT_STATUS_IS_OK(nt_status
)) {
279 d_printf("%s (0x%x)\n",
281 NT_STATUS_V(nt_status
));
282 SAFE_FREE(error_string
);
286 if (memcmp(lm_hash
, lm_key
,
287 sizeof(lm_key
)) != 0) {
288 DEBUG(1, ("LM Key does not match expectations!\n"));
289 DEBUG(1, ("lm_key:\n"));
290 dump_data(1, lm_key
, 8);
291 DEBUG(1, ("expected:\n"));
292 dump_data(1, lm_hash
, 8);
295 if (memcmp(session_key
.data
, user_session_key
,
296 sizeof(user_session_key
)) != 0) {
297 DEBUG(1, ("NT Session Key does not match expectations!\n"));
298 DEBUG(1, ("user_session_key:\n"));
299 dump_data(1, user_session_key
, 16);
300 DEBUG(1, ("expected:\n"));
301 dump_data(1, session_key
.data
, session_key
.length
);
310 * Test the NTLMv2 and LMv2 responses
313 static bool test_lmv2_ntlmv2_broken(enum ntlm_break break_which
)
318 DATA_BLOB ntlmv2_response
= data_blob_null
;
319 DATA_BLOB lmv2_response
= data_blob_null
;
320 DATA_BLOB ntlmv2_session_key
= data_blob_null
;
321 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(NULL
, get_winbind_netbios_name(), get_winbind_domain());
323 uchar user_session_key
[16];
324 DATA_BLOB chall
= get_challenge();
327 ZERO_STRUCT(user_session_key
);
329 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
331 if (!SMBNTLMv2encrypt(NULL
, opt_username
, opt_domain
, opt_password
, &chall
,
333 &lmv2_response
, &ntlmv2_response
, NULL
,
334 &ntlmv2_session_key
)) {
335 data_blob_free(&names_blob
);
338 data_blob_free(&names_blob
);
340 switch (break_which
) {
344 lmv2_response
.data
[0]++;
347 ntlmv2_response
.data
[0]++;
350 data_blob_free(&lmv2_response
);
353 data_blob_free(&ntlmv2_response
);
357 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
365 &error_string
, NULL
);
367 data_blob_free(&lmv2_response
);
368 data_blob_free(&ntlmv2_response
);
370 if (!NT_STATUS_IS_OK(nt_status
)) {
371 d_printf("%s (0x%x)\n",
373 NT_STATUS_V(nt_status
));
374 SAFE_FREE(error_string
);
375 return break_which
== BREAK_NT
;
378 if (break_which
!= NO_NT
&& break_which
!= BREAK_NT
&& memcmp(ntlmv2_session_key
.data
, user_session_key
,
379 sizeof(user_session_key
)) != 0) {
380 DEBUG(1, ("USER (NTLMv2) Session Key does not match expectations!\n"));
381 DEBUG(1, ("user_session_key:\n"));
382 dump_data(1, user_session_key
, 16);
383 DEBUG(1, ("expected:\n"));
384 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
391 * Test the NTLMv2 and LMv2 responses
394 static bool test_lmv2_ntlmv2(void)
396 return test_lmv2_ntlmv2_broken(BREAK_NONE
);
400 * Test the LMv2 response only
403 static bool test_lmv2(void)
405 return test_lmv2_ntlmv2_broken(NO_NT
);
409 * Test the NTLMv2 response only
412 static bool test_ntlmv2(void)
414 return test_lmv2_ntlmv2_broken(NO_LM
);
417 static bool test_lm_ntlm(void)
419 return test_lm_ntlm_broken(BREAK_NONE
);
422 static bool test_ntlm_lm_broken(void)
424 return test_lm_ntlm_broken(BREAK_LM
);
427 static bool test_ntlm_ntlm_broken(void)
429 return test_lm_ntlm_broken(BREAK_NT
);
432 static bool test_ntlmv2_lmv2_broken(void)
434 return test_lmv2_ntlmv2_broken(BREAK_LM
);
437 static bool test_ntlmv2_ntlmv2_broken(void)
439 return test_lmv2_ntlmv2_broken(BREAK_NT
);
442 static bool test_plaintext(enum ntlm_break break_which
)
446 DATA_BLOB nt_response
= data_blob_null
;
447 DATA_BLOB lm_response
= data_blob_null
;
449 smb_ucs2_t
*nt_response_ucs2
;
450 size_t converted_size
;
452 uchar user_session_key
[16];
454 static const uchar zeros
[8] = { 0, };
455 DATA_BLOB chall
= data_blob(zeros
, sizeof(zeros
));
458 ZERO_STRUCT(user_session_key
);
460 flags
|= WBFLAG_PAM_LMKEY
;
461 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
463 if (!push_ucs2_talloc(talloc_tos(), &nt_response_ucs2
, opt_password
,
466 DEBUG(0, ("push_ucs2_talloc failed!\n"));
470 nt_response
.data
= (unsigned char *)nt_response_ucs2
;
471 nt_response
.length
= strlen_w(nt_response_ucs2
)*sizeof(smb_ucs2_t
);
473 if ((password
= strupper_talloc(talloc_tos(), opt_password
)) == NULL
) {
474 DEBUG(0, ("strupper_talloc() failed!\n"));
478 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
482 &lm_response
.length
)) {
483 DEBUG(0, ("convert_string_talloc failed!\n"));
487 TALLOC_FREE(password
);
489 switch (break_which
) {
493 lm_response
.data
[0]++;
496 nt_response
.data
[0]++;
499 TALLOC_FREE(lm_response
.data
);
500 lm_response
.length
= 0;
503 TALLOC_FREE(nt_response
.data
);
504 nt_response
.length
= 0;
508 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
513 flags
, MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
,
516 &error_string
, NULL
);
518 TALLOC_FREE(nt_response
.data
);
519 TALLOC_FREE(lm_response
.data
);
520 data_blob_free(&chall
);
522 if (!NT_STATUS_IS_OK(nt_status
)) {
523 d_printf("%s (0x%x)\n",
525 NT_STATUS_V(nt_status
));
526 SAFE_FREE(error_string
);
527 return break_which
== BREAK_NT
;
530 return break_which
!= BREAK_NT
;
533 static bool test_plaintext_none_broken(void) {
534 return test_plaintext(BREAK_NONE
);
537 static bool test_plaintext_lm_broken(void) {
538 return test_plaintext(BREAK_LM
);
541 static bool test_plaintext_nt_broken(void) {
542 return test_plaintext(BREAK_NT
);
545 static bool test_plaintext_nt_only(void) {
546 return test_plaintext(NO_LM
);
549 static bool test_plaintext_lm_only(void) {
550 return test_plaintext(NO_NT
);
564 - plaintext tests (in challenge-response feilds)
566 check we get the correct session key in each case
567 check what values we get for the LM session key
571 static const struct ntlm_tests
{
576 {test_lm_ntlm
, "LM and NTLM"},
578 {test_ntlm_in_lm
, "NTLM in LM"},
579 {test_ntlm_in_both
, "NTLM in both"},
580 {test_ntlmv2
, "NTLMv2"},
581 {test_lmv2_ntlmv2
, "NTLMv2 and LMv2"},
583 {test_ntlmv2_lmv2_broken
, "NTLMv2 and LMv2, LMv2 broken"},
584 {test_ntlmv2_ntlmv2_broken
, "NTLMv2 and LMv2, NTLMv2 broken"},
585 {test_ntlm_lm_broken
, "NTLM and LM, LM broken"},
586 {test_ntlm_ntlm_broken
, "NTLM and LM, NTLM broken"},
587 {test_plaintext_none_broken
, "Plaintext"},
588 {test_plaintext_lm_broken
, "Plaintext LM broken"},
589 {test_plaintext_nt_broken
, "Plaintext NT broken"},
590 {test_plaintext_nt_only
, "Plaintext NT only"},
591 {test_plaintext_lm_only
, "Plaintext LM only"},
595 bool diagnose_ntlm_auth(void)
600 for (i
=0; test_table
[i
].fn
; i
++) {
601 if (!test_table
[i
].fn()) {
602 DEBUG(1, ("Test %s failed!\n", test_table
[i
].name
));