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"
28 #define DBGC_CLASS DBGC_WINBIND
39 Authenticate a user with a challenge/response, checking session key
40 and valid authentication types
44 * Test the normal 'LM and NTLM' combination
47 static bool test_lm_ntlm_broken(enum ntlm_break break_which
)
52 DATA_BLOB lm_response
= data_blob(NULL
, 24);
53 DATA_BLOB nt_response
= data_blob(NULL
, 24);
54 DATA_BLOB session_key
= data_blob(NULL
, 16);
57 uchar user_session_key
[16];
60 DATA_BLOB chall
= get_challenge();
64 ZERO_STRUCT(user_session_key
);
66 flags
|= WBFLAG_PAM_LMKEY
;
67 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
69 SMBencrypt(opt_password
,chall
.data
,lm_response
.data
);
70 E_deshash(opt_password
, lm_hash
);
72 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
74 E_md4hash(opt_password
, nt_hash
);
75 SMBsesskeygen_ntv1(nt_hash
, NULL
, session_key
.data
);
77 switch (break_which
) {
81 lm_response
.data
[0]++;
84 nt_response
.data
[0]++;
87 data_blob_free(&lm_response
);
90 data_blob_free(&nt_response
);
94 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
102 &error_string
, NULL
);
104 data_blob_free(&lm_response
);
106 if (!NT_STATUS_IS_OK(nt_status
)) {
107 d_printf("%s (0x%x)\n",
109 NT_STATUS_V(nt_status
));
110 SAFE_FREE(error_string
);
111 return break_which
== BREAK_NT
;
114 if (memcmp(lm_hash
, lm_key
,
115 sizeof(lm_key
)) != 0) {
116 DEBUG(1, ("LM Key does not match expectations!\n"));
117 DEBUG(1, ("lm_key:\n"));
118 dump_data(1, lm_key
, 8);
119 DEBUG(1, ("expected:\n"));
120 dump_data(1, lm_hash
, 8);
124 if (break_which
== NO_NT
) {
125 if (memcmp(lm_hash
, user_session_key
,
127 DEBUG(1, ("NT Session Key does not match expectations (should be LM hash)!\n"));
128 DEBUG(1, ("user_session_key:\n"));
129 dump_data(1, user_session_key
, sizeof(user_session_key
));
130 DEBUG(1, ("expected:\n"));
131 dump_data(1, lm_hash
, sizeof(lm_hash
));
135 if (memcmp(session_key
.data
, user_session_key
,
136 sizeof(user_session_key
)) != 0) {
137 DEBUG(1, ("NT Session Key does not match expectations!\n"));
138 DEBUG(1, ("user_session_key:\n"));
139 dump_data(1, user_session_key
, 16);
140 DEBUG(1, ("expected:\n"));
141 dump_data(1, session_key
.data
, session_key
.length
);
149 * Test LM authentication, no NT response supplied
152 static bool test_lm(void)
155 return test_lm_ntlm_broken(NO_NT
);
159 * Test the NTLM response only, no LM.
162 static bool test_ntlm(void)
164 return test_lm_ntlm_broken(NO_LM
);
168 * Test the NTLM response only, but in the LM field.
171 static bool test_ntlm_in_lm(void)
176 DATA_BLOB nt_response
= data_blob(NULL
, 24);
180 uchar user_session_key
[16];
181 DATA_BLOB chall
= get_challenge();
184 ZERO_STRUCT(user_session_key
);
186 flags
|= WBFLAG_PAM_LMKEY
;
187 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
189 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
191 E_deshash(opt_password
, lm_hash
);
193 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
201 &error_string
, NULL
);
203 data_blob_free(&nt_response
);
205 if (!NT_STATUS_IS_OK(nt_status
)) {
206 d_printf("%s (0x%x)\n",
208 NT_STATUS_V(nt_status
));
209 SAFE_FREE(error_string
);
213 if (memcmp(lm_hash
, lm_key
,
214 sizeof(lm_key
)) != 0) {
215 DEBUG(1, ("LM Key does not match expectations!\n"));
216 DEBUG(1, ("lm_key:\n"));
217 dump_data(1, lm_key
, 8);
218 DEBUG(1, ("expected:\n"));
219 dump_data(1, lm_hash
, 8);
222 if (memcmp(lm_hash
, user_session_key
, 8) != 0) {
223 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
224 DEBUG(1, ("user_session_key:\n"));
225 dump_data(1, user_session_key
, 16);
226 DEBUG(1, ("expected:\n"));
227 dump_data(1, lm_hash
, 8);
234 * Test the NTLM response only, but in the both the NT and LM fields.
237 static bool test_ntlm_in_both(void)
242 DATA_BLOB nt_response
= data_blob(NULL
, 24);
243 DATA_BLOB session_key
= data_blob(NULL
, 16);
247 uint8 user_session_key
[16];
249 DATA_BLOB chall
= get_challenge();
253 ZERO_STRUCT(user_session_key
);
255 flags
|= WBFLAG_PAM_LMKEY
;
256 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
258 SMBNTencrypt(opt_password
,chall
.data
,nt_response
.data
);
259 E_md4hash(opt_password
, nt_hash
);
260 SMBsesskeygen_ntv1(nt_hash
, NULL
, session_key
.data
);
262 E_deshash(opt_password
, lm_hash
);
264 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
272 &error_string
, NULL
);
274 data_blob_free(&nt_response
);
276 if (!NT_STATUS_IS_OK(nt_status
)) {
277 d_printf("%s (0x%x)\n",
279 NT_STATUS_V(nt_status
));
280 SAFE_FREE(error_string
);
284 if (memcmp(lm_hash
, lm_key
,
285 sizeof(lm_key
)) != 0) {
286 DEBUG(1, ("LM Key does not match expectations!\n"));
287 DEBUG(1, ("lm_key:\n"));
288 dump_data(1, lm_key
, 8);
289 DEBUG(1, ("expected:\n"));
290 dump_data(1, lm_hash
, 8);
293 if (memcmp(session_key
.data
, user_session_key
,
294 sizeof(user_session_key
)) != 0) {
295 DEBUG(1, ("NT Session Key does not match expectations!\n"));
296 DEBUG(1, ("user_session_key:\n"));
297 dump_data(1, user_session_key
, 16);
298 DEBUG(1, ("expected:\n"));
299 dump_data(1, session_key
.data
, session_key
.length
);
308 * Test the NTLMv2 and LMv2 responses
311 static bool test_lmv2_ntlmv2_broken(enum ntlm_break break_which
)
316 DATA_BLOB ntlmv2_response
= data_blob_null
;
317 DATA_BLOB lmv2_response
= data_blob_null
;
318 DATA_BLOB ntlmv2_session_key
= data_blob_null
;
319 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
321 uchar user_session_key
[16];
322 DATA_BLOB chall
= get_challenge();
325 ZERO_STRUCT(user_session_key
);
327 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
329 if (!SMBNTLMv2encrypt(opt_username
, opt_domain
, opt_password
, &chall
,
331 &lmv2_response
, &ntlmv2_response
,
332 &ntlmv2_session_key
)) {
333 data_blob_free(&names_blob
);
336 data_blob_free(&names_blob
);
338 switch (break_which
) {
342 lmv2_response
.data
[0]++;
345 ntlmv2_response
.data
[0]++;
348 data_blob_free(&lmv2_response
);
351 data_blob_free(&ntlmv2_response
);
355 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
363 &error_string
, NULL
);
365 data_blob_free(&lmv2_response
);
366 data_blob_free(&ntlmv2_response
);
368 if (!NT_STATUS_IS_OK(nt_status
)) {
369 d_printf("%s (0x%x)\n",
371 NT_STATUS_V(nt_status
));
372 SAFE_FREE(error_string
);
373 return break_which
== BREAK_NT
;
376 if (break_which
!= NO_NT
&& break_which
!= BREAK_NT
&& memcmp(ntlmv2_session_key
.data
, user_session_key
,
377 sizeof(user_session_key
)) != 0) {
378 DEBUG(1, ("USER (NTLMv2) Session Key does not match expectations!\n"));
379 DEBUG(1, ("user_session_key:\n"));
380 dump_data(1, user_session_key
, 16);
381 DEBUG(1, ("expected:\n"));
382 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
389 * Test the NTLMv2 and LMv2 responses
392 static bool test_lmv2_ntlmv2(void)
394 return test_lmv2_ntlmv2_broken(BREAK_NONE
);
398 * Test the LMv2 response only
401 static bool test_lmv2(void)
403 return test_lmv2_ntlmv2_broken(NO_NT
);
407 * Test the NTLMv2 response only
410 static bool test_ntlmv2(void)
412 return test_lmv2_ntlmv2_broken(NO_LM
);
415 static bool test_lm_ntlm(void)
417 return test_lm_ntlm_broken(BREAK_NONE
);
420 static bool test_ntlm_lm_broken(void)
422 return test_lm_ntlm_broken(BREAK_LM
);
425 static bool test_ntlm_ntlm_broken(void)
427 return test_lm_ntlm_broken(BREAK_NT
);
430 static bool test_ntlmv2_lmv2_broken(void)
432 return test_lmv2_ntlmv2_broken(BREAK_LM
);
435 static bool test_ntlmv2_ntlmv2_broken(void)
437 return test_lmv2_ntlmv2_broken(BREAK_NT
);
440 static bool test_plaintext(enum ntlm_break break_which
)
444 DATA_BLOB nt_response
= data_blob_null
;
445 DATA_BLOB lm_response
= data_blob_null
;
447 smb_ucs2_t
*nt_response_ucs2
;
449 uchar user_session_key
[16];
451 static const uchar zeros
[8] = { 0, };
452 DATA_BLOB chall
= data_blob(zeros
, sizeof(zeros
));
455 ZERO_STRUCT(user_session_key
);
457 flags
|= WBFLAG_PAM_LMKEY
;
458 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
460 if ((push_ucs2_allocate(&nt_response_ucs2
, opt_password
)) == -1) {
461 DEBUG(0, ("push_ucs2_allocate failed!\n"));
465 nt_response
.data
= (unsigned char *)nt_response_ucs2
;
466 nt_response
.length
= strlen_w(nt_response_ucs2
)*sizeof(smb_ucs2_t
);
468 if ((password
= strdup_upper(opt_password
)) == NULL
) {
469 DEBUG(0, ("strdup_upper failed!\n"));
473 if (!convert_string_allocate(NULL
, CH_UNIX
,
477 &lm_response
.length
, True
)) {
478 DEBUG(0, ("convert_string_allocate failed!\n"));
484 switch (break_which
) {
488 lm_response
.data
[0]++;
491 nt_response
.data
[0]++;
494 SAFE_FREE(lm_response
.data
);
495 lm_response
.length
= 0;
498 SAFE_FREE(nt_response
.data
);
499 nt_response
.length
= 0;
503 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
511 &error_string
, NULL
);
513 SAFE_FREE(nt_response
.data
);
514 SAFE_FREE(lm_response
.data
);
515 data_blob_free(&chall
);
517 if (!NT_STATUS_IS_OK(nt_status
)) {
518 d_printf("%s (0x%x)\n",
520 NT_STATUS_V(nt_status
));
521 SAFE_FREE(error_string
);
522 return break_which
== BREAK_NT
;
525 return break_which
!= BREAK_NT
;
528 static bool test_plaintext_none_broken(void) {
529 return test_plaintext(BREAK_NONE
);
532 static bool test_plaintext_lm_broken(void) {
533 return test_plaintext(BREAK_LM
);
536 static bool test_plaintext_nt_broken(void) {
537 return test_plaintext(BREAK_NT
);
540 static bool test_plaintext_nt_only(void) {
541 return test_plaintext(NO_LM
);
544 static bool test_plaintext_lm_only(void) {
545 return test_plaintext(NO_NT
);
559 - plaintext tests (in challenge-response feilds)
561 check we get the correct session key in each case
562 check what values we get for the LM session key
566 static const struct ntlm_tests
{
571 {test_lm_ntlm
, "LM and NTLM"},
573 {test_ntlm_in_lm
, "NTLM in LM"},
574 {test_ntlm_in_both
, "NTLM in both"},
575 {test_ntlmv2
, "NTLMv2"},
576 {test_lmv2_ntlmv2
, "NTLMv2 and LMv2"},
578 {test_ntlmv2_lmv2_broken
, "NTLMv2 and LMv2, LMv2 broken"},
579 {test_ntlmv2_ntlmv2_broken
, "NTLMv2 and LMv2, NTLMv2 broken"},
580 {test_ntlm_lm_broken
, "NTLM and LM, LM broken"},
581 {test_ntlm_ntlm_broken
, "NTLM and LM, NTLM broken"},
582 {test_plaintext_none_broken
, "Plaintext"},
583 {test_plaintext_lm_broken
, "Plaintext LM broken"},
584 {test_plaintext_nt_broken
, "Plaintext NT broken"},
585 {test_plaintext_nt_only
, "Plaintext NT only"},
586 {test_plaintext_lm_only
, "Plaintext LM only"},
590 bool diagnose_ntlm_auth(void)
595 for (i
=0; test_table
[i
].fn
; i
++) {
596 if (!test_table
[i
].fn()) {
597 DEBUG(1, ("Test %s failed!\n", test_table
[i
].name
));