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
,
50 bool lanman_support_expected
)
55 DATA_BLOB lm_response
= data_blob(NULL
, 24);
56 DATA_BLOB nt_response
= data_blob(NULL
, 24);
57 DATA_BLOB session_key
= data_blob(NULL
, 16);
58 uint8_t authoritative
= 1;
60 uchar user_session_key
[16];
63 DATA_BLOB chall
= get_challenge();
67 ZERO_STRUCT(user_session_key
);
69 flags
|= WBFLAG_PAM_LMKEY
;
70 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
72 SMBencrypt(opt_password
,chall
.data
,lm_response
.data
);
73 E_deshash(opt_password
, lm_hash
);
75 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
77 E_md4hash(opt_password
, nt_hash
);
78 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
80 switch (break_which
) {
84 lm_response
.data
[0]++;
87 nt_response
.data
[0]++;
90 data_blob_free(&lm_response
);
93 data_blob_free(&nt_response
);
97 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
106 &error_string
, NULL
);
108 data_blob_free(&lm_response
);
109 data_blob_free(&nt_response
);
111 if (!NT_STATUS_IS_OK(nt_status
)) {
112 d_printf("%s (0x%x)\n",
114 NT_STATUS_V(nt_status
));
115 SAFE_FREE(error_string
);
116 data_blob_free(&session_key
);
118 return break_which
== BREAK_NT
;
121 /* If we are told the DC is Samba4, expect an LM key of zeros */
122 if (!lanman_support_expected
) {
123 if (!all_zero(lm_key
,
125 DEBUG(1, ("LM Key does not match expectations!\n"));
126 DEBUG(1, ("lm_key:\n"));
127 dump_data(1, lm_key
, 8);
128 DEBUG(1, ("expected: all zeros\n"));
132 if (memcmp(lm_hash
, lm_key
,
133 sizeof(lm_key
)) != 0) {
134 DEBUG(1, ("LM Key does not match expectations!\n"));
135 DEBUG(1, ("lm_key:\n"));
136 dump_data(1, lm_key
, 8);
137 DEBUG(1, ("expected:\n"));
138 dump_data(1, lm_hash
, 8);
143 if (break_which
== NO_NT
) {
144 if (memcmp(lm_hash
, user_session_key
,
146 DEBUG(1, ("NT Session Key does not match expectations (should be LM hash)!\n"));
147 DEBUG(1, ("user_session_key:\n"));
148 dump_data(1, user_session_key
, sizeof(user_session_key
));
149 DEBUG(1, ("expected:\n"));
150 dump_data(1, lm_hash
, sizeof(lm_hash
));
154 if (memcmp(session_key
.data
, user_session_key
,
155 sizeof(user_session_key
)) != 0) {
156 DEBUG(1, ("NT Session Key does not match expectations!\n"));
157 DEBUG(1, ("user_session_key:\n"));
158 dump_data(1, user_session_key
, 16);
159 DEBUG(1, ("expected:\n"));
160 dump_data(1, session_key
.data
, session_key
.length
);
164 data_blob_free(&session_key
);
170 * Test LM authentication, no NT response supplied
173 static bool test_lm(bool lanman_support_expected
)
176 return test_lm_ntlm_broken(NO_NT
, lanman_support_expected
);
180 * Test the NTLM response only, no LM.
183 static bool test_ntlm(bool lanman_support_expected
)
185 return test_lm_ntlm_broken(NO_LM
, lanman_support_expected
);
189 * Test the NTLM response only, but in the LM field.
192 static bool test_ntlm_in_lm(bool lanman_support_expected
)
197 DATA_BLOB nt_response
= data_blob(NULL
, 24);
198 uint8_t authoritative
= 1;
201 uchar user_session_key
[16];
202 DATA_BLOB chall
= get_challenge();
203 char *error_string
= NULL
;
205 ZERO_STRUCT(user_session_key
);
207 flags
|= WBFLAG_PAM_LMKEY
;
208 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
210 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
212 E_deshash(opt_password
, lm_hash
);
214 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
223 &error_string
, NULL
);
225 data_blob_free(&nt_response
);
227 if (!NT_STATUS_IS_OK(nt_status
)) {
228 d_printf("%s (0x%x)\n",
230 NT_STATUS_V(nt_status
));
231 SAFE_FREE(error_string
);
234 SAFE_FREE(error_string
);
236 /* If we are told the DC is Samba4, expect an LM key of zeros */
237 if (!lanman_support_expected
) {
238 if (!all_zero(lm_key
,
240 DEBUG(1, ("LM Key does not match expectations!\n"));
241 DEBUG(1, ("lm_key:\n"));
242 dump_data(1, lm_key
, 8);
243 DEBUG(1, ("expected: all zeros\n"));
246 if (!all_zero(user_session_key
,
247 sizeof(user_session_key
))) {
248 DEBUG(1, ("Session Key (normally first 8 lm hash) does not match expectations!\n"));
249 DEBUG(1, ("user_session_key:\n"));
250 dump_data(1, user_session_key
, 16);
251 DEBUG(1, ("expected all zeros:\n"));
255 if (memcmp(lm_hash
, lm_key
,
256 sizeof(lm_key
)) != 0) {
257 DEBUG(1, ("LM Key does not match expectations!\n"));
258 DEBUG(1, ("lm_key:\n"));
259 dump_data(1, lm_key
, 8);
260 DEBUG(1, ("expected:\n"));
261 dump_data(1, lm_hash
, 8);
264 if (memcmp(lm_hash
, user_session_key
, 8) != 0) {
265 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
266 DEBUG(1, ("user_session_key:\n"));
267 dump_data(1, user_session_key
, 16);
268 DEBUG(1, ("expected:\n"));
269 dump_data(1, lm_hash
, 8);
277 * Test the NTLM response only, but in the both the NT and LM fields.
280 static bool test_ntlm_in_both(bool lanman_support_expected
)
285 DATA_BLOB nt_response
= data_blob(NULL
, 24);
286 DATA_BLOB session_key
= data_blob(NULL
, 16);
287 uint8_t authoritative
= 1;
290 uint8_t user_session_key
[16];
292 DATA_BLOB chall
= get_challenge();
293 char *error_string
= NULL
;
296 ZERO_STRUCT(user_session_key
);
298 flags
|= WBFLAG_PAM_LMKEY
;
299 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
301 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
302 E_md4hash(opt_password
, nt_hash
);
303 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
305 E_deshash(opt_password
, lm_hash
);
307 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
316 &error_string
, NULL
);
318 data_blob_free(&nt_response
);
320 if (!NT_STATUS_IS_OK(nt_status
)) {
321 d_printf("%s (0x%x)\n",
323 NT_STATUS_V(nt_status
));
324 SAFE_FREE(error_string
);
327 SAFE_FREE(error_string
);
329 /* If we are told the DC is Samba4, expect an LM key of zeros */
330 if (!lanman_support_expected
) {
331 if (!all_zero(lm_key
,
333 DEBUG(1, ("LM Key does not match expectations!\n"));
334 DEBUG(1, ("lm_key:\n"));
335 dump_data(1, lm_key
, 8);
336 DEBUG(1, ("expected: all zeros\n"));
340 if (memcmp(lm_hash
, lm_key
,
341 sizeof(lm_key
)) != 0) {
342 DEBUG(1, ("LM Key does not match expectations!\n"));
343 DEBUG(1, ("lm_key:\n"));
344 dump_data(1, lm_key
, 8);
345 DEBUG(1, ("expected:\n"));
346 dump_data(1, lm_hash
, 8);
350 if (memcmp(session_key
.data
, user_session_key
,
351 sizeof(user_session_key
)) != 0) {
352 DEBUG(1, ("NT Session Key does not match expectations!\n"));
353 DEBUG(1, ("user_session_key:\n"));
354 dump_data(1, user_session_key
, 16);
355 DEBUG(1, ("expected:\n"));
356 dump_data(1, session_key
.data
, session_key
.length
);
365 * Test the NTLMv2 and LMv2 responses
368 static bool test_lmv2_ntlmv2_broken(enum ntlm_break break_which
)
373 DATA_BLOB ntlmv2_response
= data_blob_null
;
374 DATA_BLOB lmv2_response
= data_blob_null
;
375 DATA_BLOB ntlmv2_session_key
= data_blob_null
;
376 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(NULL
, get_winbind_netbios_name(), get_winbind_domain());
377 uint8_t authoritative
= 1;
378 uchar user_session_key
[16];
379 DATA_BLOB chall
= get_challenge();
380 char *error_string
= NULL
;
382 ZERO_STRUCT(user_session_key
);
384 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
386 if (!SMBNTLMv2encrypt(NULL
, opt_username
, opt_domain
, opt_password
, &chall
,
388 &lmv2_response
, &ntlmv2_response
, NULL
,
389 &ntlmv2_session_key
)) {
390 data_blob_free(&names_blob
);
393 data_blob_free(&names_blob
);
395 switch (break_which
) {
399 lmv2_response
.data
[0]++;
402 ntlmv2_response
.data
[0]++;
405 data_blob_free(&lmv2_response
);
408 data_blob_free(&ntlmv2_response
);
412 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
421 &error_string
, NULL
);
423 data_blob_free(&lmv2_response
);
424 data_blob_free(&ntlmv2_response
);
426 if (!NT_STATUS_IS_OK(nt_status
)) {
427 d_printf("%s (0x%x)\n",
429 NT_STATUS_V(nt_status
));
430 SAFE_FREE(error_string
);
431 return break_which
== BREAK_NT
;
434 SAFE_FREE(error_string
);
436 if (break_which
!= NO_NT
&& break_which
!= BREAK_NT
&& memcmp(ntlmv2_session_key
.data
, user_session_key
,
437 sizeof(user_session_key
)) != 0) {
438 DEBUG(1, ("USER (NTLMv2) Session Key does not match expectations!\n"));
439 DEBUG(1, ("user_session_key:\n"));
440 dump_data(1, user_session_key
, 16);
441 DEBUG(1, ("expected:\n"));
442 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
446 data_blob_free(&ntlmv2_session_key
);
451 * Test the NTLMv2 and LMv2 responses
454 static bool test_lmv2_ntlmv2(bool lanman_support_expected
)
456 return test_lmv2_ntlmv2_broken(BREAK_NONE
);
460 * Test the LMv2 response only
463 static bool test_lmv2(bool lanman_support_expected
)
465 return test_lmv2_ntlmv2_broken(NO_NT
);
469 * Test the NTLMv2 response only
472 static bool test_ntlmv2(bool lanman_support_expected
)
474 return test_lmv2_ntlmv2_broken(NO_LM
);
477 static bool test_lm_ntlm(bool lanman_support_expected
)
479 return test_lm_ntlm_broken(BREAK_NONE
, lanman_support_expected
);
482 static bool test_ntlm_lm_broken(bool lanman_support_expected
)
484 return test_lm_ntlm_broken(BREAK_LM
, lanman_support_expected
);
487 static bool test_ntlm_ntlm_broken(bool lanman_support_expected
)
489 return test_lm_ntlm_broken(BREAK_NT
, lanman_support_expected
);
492 static bool test_ntlmv2_lmv2_broken(bool lanman_support_expected
)
494 return test_lmv2_ntlmv2_broken(BREAK_LM
);
497 static bool test_ntlmv2_ntlmv2_broken(bool lanman_support_expected
)
499 return test_lmv2_ntlmv2_broken(BREAK_NT
);
502 static bool test_plaintext(enum ntlm_break break_which
)
506 DATA_BLOB nt_response
= data_blob_null
;
507 DATA_BLOB lm_response
= data_blob_null
;
509 smb_ucs2_t
*nt_response_ucs2
;
510 size_t converted_size
;
511 uint8_t authoritative
= 1;
512 uchar user_session_key
[16];
514 static const uchar zeros
[8] = { 0, };
515 DATA_BLOB chall
= data_blob(zeros
, sizeof(zeros
));
518 ZERO_STRUCT(user_session_key
);
520 flags
|= WBFLAG_PAM_LMKEY
;
521 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
523 if (!push_ucs2_talloc(talloc_tos(), &nt_response_ucs2
, opt_password
,
526 DEBUG(0, ("push_ucs2_talloc failed!\n"));
530 nt_response
.data
= (unsigned char *)nt_response_ucs2
;
531 nt_response
.length
= strlen_w(nt_response_ucs2
)*sizeof(smb_ucs2_t
);
533 if ((password
= strupper_talloc(talloc_tos(), opt_password
)) == NULL
) {
534 DEBUG(0, ("strupper_talloc() failed!\n"));
538 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
542 &lm_response
.length
)) {
543 DEBUG(0, ("convert_string_talloc failed!\n"));
547 TALLOC_FREE(password
);
549 switch (break_which
) {
553 lm_response
.data
[0]++;
556 nt_response
.data
[0]++;
559 TALLOC_FREE(lm_response
.data
);
560 lm_response
.length
= 0;
563 TALLOC_FREE(nt_response
.data
);
564 nt_response
.length
= 0;
568 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
573 flags
, MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
,
577 &error_string
, NULL
);
579 TALLOC_FREE(nt_response
.data
);
580 TALLOC_FREE(lm_response
.data
);
581 data_blob_free(&chall
);
583 if (!NT_STATUS_IS_OK(nt_status
)) {
584 d_printf("%s (0x%x)\n",
586 NT_STATUS_V(nt_status
));
587 SAFE_FREE(error_string
);
588 return break_which
== BREAK_NT
;
591 return break_which
!= BREAK_NT
;
594 static bool test_plaintext_none_broken(bool lanman_support_expected
) {
595 return test_plaintext(BREAK_NONE
);
598 static bool test_plaintext_lm_broken(bool lanman_support_expected
) {
599 return test_plaintext(BREAK_LM
);
602 static bool test_plaintext_nt_broken(bool lanman_support_expected
) {
603 return test_plaintext(BREAK_NT
);
606 static bool test_plaintext_nt_only(bool lanman_support_expected
) {
607 return test_plaintext(NO_LM
);
610 static bool test_plaintext_lm_only(bool lanman_support_expected
) {
611 return test_plaintext(NO_NT
);
625 - plaintext tests (in challenge-response fields)
627 check we get the correct session key in each case
628 check what values we get for the LM session key
632 static const struct ntlm_tests
{
633 bool (*fn
)(bool lanman_support_expected
);
644 .name
= "LM and NTLM"
651 .fn
= test_ntlm_in_lm
,
655 .fn
= test_ntlm_in_both
,
656 .name
= "NTLM in both"
663 .fn
= test_lmv2_ntlmv2
,
664 .name
= "NTLMv2 and LMv2"
671 .fn
= test_ntlmv2_lmv2_broken
,
672 .name
= "NTLMv2 and LMv2, LMv2 broken"
675 .fn
= test_ntlmv2_ntlmv2_broken
,
676 .name
= "NTLMv2 and LMv2, NTLMv2 broken"
679 .fn
= test_ntlm_lm_broken
,
680 .name
= "NTLM and LM, LM broken"
683 .fn
= test_ntlm_ntlm_broken
,
684 .name
= "NTLM and LM, NTLM broken"
687 .fn
= test_plaintext_none_broken
,
691 .fn
= test_plaintext_lm_broken
,
692 .name
= "Plaintext LM broken"
695 .fn
= test_plaintext_nt_broken
,
696 .name
= "Plaintext NT broken"
699 .fn
= test_plaintext_nt_only
,
700 .name
= "Plaintext NT only"
703 .fn
= test_plaintext_lm_only
,
704 .name
= "Plaintext LM only",
712 bool diagnose_ntlm_auth(bool lanman_support_expected
)
717 for (i
=0; test_table
[i
].fn
; i
++) {
718 bool test_pass
= test_table
[i
].fn(lanman_support_expected
);
719 if (!lanman_support_expected
720 && test_table
[i
].lanman
) {
722 DBG_ERR("Test %s unexpectedly passed "
723 "(server should have rejected LM)!\n",
727 } else if (!test_pass
) {
728 DBG_ERR("Test %s failed!\n", test_table
[i
].name
);