2 Unix SMB/CIFS implementation.
4 test suite for netlogon SamLogon operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
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 "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_netlogon_c.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "auth/auth.h"
29 #include "../lib/crypto/crypto.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/rpc.h"
32 #include "auth/gensec/schannel_proto.h"
33 #include "auth/gensec/gensec.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "param/param.h"
37 #define TEST_MACHINE_NAME "samlogontest"
38 #define TEST_USER_NAME "samlogontestuser"
39 #define TEST_USER_NAME_WRONG_WKS "samlogontest2"
40 #define TEST_USER_NAME_WRONG_TIME "samlogontest3"
51 struct samlogon_state
{
54 const char *account_name
;
55 const char *account_domain
;
56 const char *netbios_name
;
58 const char *workgroup
;
59 struct dcerpc_pipe
*p
;
61 uint32_t parameter_control
;
62 struct netr_LogonSamLogon r
;
63 struct netr_LogonSamLogonEx r_ex
;
64 struct netr_LogonSamLogonWithFlags r_flags
;
65 struct netr_Authenticator auth
, auth2
;
66 struct creds_CredentialState
*creds
;
67 NTSTATUS expected_error
;
68 bool old_password
; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
70 struct smb_iconv_convenience
*iconv_convenience
;
74 Authenticate a user with a challenge/response, checking session key
75 and valid authentication types
77 static NTSTATUS
check_samlogon(struct samlogon_state
*samlogon_state
,
78 enum ntlm_break break_which
,
79 uint32_t parameter_control
,
81 DATA_BLOB
*lm_response
,
82 DATA_BLOB
*nt_response
,
84 uint8_t user_session_key
[16],
88 struct netr_LogonSamLogon
*r
= &samlogon_state
->r
;
89 struct netr_LogonSamLogonEx
*r_ex
= &samlogon_state
->r_ex
;
90 struct netr_LogonSamLogonWithFlags
*r_flags
= &samlogon_state
->r_flags
;
91 struct netr_NetworkInfo ninfo
;
92 struct netr_SamBaseInfo
*base
= NULL
;
93 uint16_t validation_level
= 0;
95 samlogon_state
->r
.in
.logon
->network
= &ninfo
;
96 samlogon_state
->r_ex
.in
.logon
->network
= &ninfo
;
97 samlogon_state
->r_flags
.in
.logon
->network
= &ninfo
;
99 ninfo
.identity_info
.domain_name
.string
= samlogon_state
->account_domain
;
100 ninfo
.identity_info
.parameter_control
= parameter_control
;
101 ninfo
.identity_info
.logon_id_low
= 0;
102 ninfo
.identity_info
.logon_id_high
= 0;
103 ninfo
.identity_info
.account_name
.string
= samlogon_state
->account_name
;
104 ninfo
.identity_info
.workstation
.string
= TEST_MACHINE_NAME
;
106 memcpy(ninfo
.challenge
, chall
->data
, 8);
108 switch (break_which
) {
112 if (lm_response
&& lm_response
->data
) {
113 lm_response
->data
[0]++;
117 if (nt_response
&& nt_response
->data
) {
118 nt_response
->data
[0]++;
122 if (lm_response
&& lm_response
->data
) {
123 lm_response
->data
[0]++;
125 if (nt_response
&& nt_response
->data
) {
126 nt_response
->data
[0]++;
130 data_blob_free(lm_response
);
133 data_blob_free(nt_response
);
138 ninfo
.nt
.data
= nt_response
->data
;
139 ninfo
.nt
.length
= nt_response
->length
;
141 ninfo
.nt
.data
= NULL
;
146 ninfo
.lm
.data
= lm_response
->data
;
147 ninfo
.lm
.length
= lm_response
->length
;
149 ninfo
.lm
.data
= NULL
;
153 switch (samlogon_state
->function_level
) {
154 case NDR_NETR_LOGONSAMLOGON
:
155 ZERO_STRUCT(samlogon_state
->auth2
);
156 creds_client_authenticator(samlogon_state
->creds
, &samlogon_state
->auth
);
158 r
->out
.return_authenticator
= NULL
;
159 status
= dcerpc_netr_LogonSamLogon(samlogon_state
->p
, samlogon_state
->mem_ctx
, r
);
160 if (!r
->out
.return_authenticator
||
161 !creds_client_check(samlogon_state
->creds
, &r
->out
.return_authenticator
->cred
)) {
162 d_printf("Credential chaining failed\n");
164 if (!NT_STATUS_IS_OK(status
)) {
166 *error_string
= strdup(nt_errstr(status
));
171 validation_level
= r
->in
.validation_level
;
173 creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r
->out
.validation
);
175 switch (validation_level
) {
177 base
= &r
->out
.validation
->sam2
->base
;
180 base
= &r
->out
.validation
->sam3
->base
;
183 base
= &r
->out
.validation
->sam6
->base
;
187 case NDR_NETR_LOGONSAMLOGONEX
:
188 status
= dcerpc_netr_LogonSamLogonEx(samlogon_state
->p
, samlogon_state
->mem_ctx
, r_ex
);
189 if (!NT_STATUS_IS_OK(status
)) {
191 *error_string
= strdup(nt_errstr(status
));
196 validation_level
= r_ex
->in
.validation_level
;
198 creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r_ex
->out
.validation
);
200 switch (validation_level
) {
202 base
= &r_ex
->out
.validation
->sam2
->base
;
205 base
= &r_ex
->out
.validation
->sam3
->base
;
208 base
= &r_ex
->out
.validation
->sam6
->base
;
212 case NDR_NETR_LOGONSAMLOGONWITHFLAGS
:
213 ZERO_STRUCT(samlogon_state
->auth2
);
214 creds_client_authenticator(samlogon_state
->creds
, &samlogon_state
->auth
);
216 r_flags
->out
.return_authenticator
= NULL
;
217 status
= dcerpc_netr_LogonSamLogonWithFlags(samlogon_state
->p
, samlogon_state
->mem_ctx
, r_flags
);
218 if (!r_flags
->out
.return_authenticator
||
219 !creds_client_check(samlogon_state
->creds
, &r_flags
->out
.return_authenticator
->cred
)) {
220 d_printf("Credential chaining failed\n");
222 if (!NT_STATUS_IS_OK(status
)) {
224 *error_string
= strdup(nt_errstr(status
));
229 validation_level
= r_flags
->in
.validation_level
;
231 creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r_flags
->out
.validation
);
233 switch (validation_level
) {
235 base
= &r_flags
->out
.validation
->sam2
->base
;
238 base
= &r_flags
->out
.validation
->sam3
->base
;
241 base
= &r_flags
->out
.validation
->sam6
->base
;
247 return NT_STATUS_INVALID_PARAMETER
;
251 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
252 return NT_STATUS_INVALID_PARAMETER
;
255 if (user_session_key
) {
256 memcpy(user_session_key
, base
->key
.key
, 16);
259 memcpy(lm_key
, base
->LMSessKey
.key
, 8);
267 * Test the normal 'LM and NTLM' combination
270 static bool test_lm_ntlm_broken(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
275 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
276 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
277 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
280 uint8_t user_session_key
[16];
285 ZERO_STRUCT(user_session_key
);
287 lm_good
= SMBencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, lm_response
.data
);
289 ZERO_STRUCT(lm_hash
);
291 E_deshash(samlogon_state
->password
, lm_hash
);
294 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
296 E_md4hash(samlogon_state
->password
, nt_hash
);
297 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
299 nt_status
= check_samlogon(samlogon_state
,
301 samlogon_state
->parameter_control
,
302 &samlogon_state
->chall
,
309 data_blob_free(&lm_response
);
311 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
312 /* for 'long' passwords, the LM password is invalid */
313 if (break_which
== NO_NT
&& !lm_good
) {
316 /* for 'old' passwords, we allow the server to be OK or wrong password */
317 if (samlogon_state
->old_password
) {
320 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
321 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
322 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
323 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
324 SAFE_FREE(*error_string
);
325 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
327 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
329 } else if (!NT_STATUS_IS_OK(nt_status
)) {
333 if (break_which
== NO_NT
&& !lm_good
) {
334 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
338 if (memcmp(lm_hash
, lm_key
,
339 sizeof(lm_key
)) != 0) {
340 d_printf("LM Key does not match expectations!\n");
341 d_printf("lm_key:\n");
342 dump_data(1, lm_key
, 8);
343 d_printf("expected:\n");
344 dump_data(1, lm_hash
, 8);
348 switch (break_which
) {
351 uint8_t lm_key_expected
[16];
352 memcpy(lm_key_expected
, lm_hash
, 8);
353 memset(lm_key_expected
+8, '\0', 8);
354 if (memcmp(lm_key_expected
, user_session_key
,
356 *error_string
= strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
357 d_printf("user_session_key:\n");
358 dump_data(1, user_session_key
, sizeof(user_session_key
));
359 d_printf("expected:\n");
360 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
366 if (memcmp(session_key
.data
, user_session_key
,
367 sizeof(user_session_key
)) != 0) {
368 *error_string
= strdup("NT Session Key does not match expectations!\n");
369 d_printf("user_session_key:\n");
370 dump_data(1, user_session_key
, 16);
371 d_printf("expected:\n");
372 dump_data(1, session_key
.data
, session_key
.length
);
380 * Test LM authentication, no NT response supplied
383 static bool test_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
386 return test_lm_ntlm_broken(samlogon_state
, NO_NT
, error_string
);
390 * Test the NTLM response only, no LM.
393 static bool test_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
395 return test_lm_ntlm_broken(samlogon_state
, NO_LM
, error_string
);
399 * Test the NTLM response only, but in the LM field.
402 static bool test_ntlm_in_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
407 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
408 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
412 uint8_t user_session_key
[16];
416 ZERO_STRUCT(user_session_key
);
418 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
420 E_md4hash(samlogon_state
->password
, nt_hash
);
421 SMBsesskeygen_ntv1(nt_hash
,
424 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
426 ZERO_STRUCT(lm_hash
);
428 nt_status
= check_samlogon(samlogon_state
,
430 samlogon_state
->parameter_control
,
431 &samlogon_state
->chall
,
438 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
439 /* for 'old' passwords, we allow the server to be OK or wrong password */
440 if (samlogon_state
->old_password
) {
444 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
445 SAFE_FREE(*error_string
);
446 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
448 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
450 } else if (!NT_STATUS_IS_OK(nt_status
)) {
455 if (memcmp(lm_hash
, lm_key
,
456 sizeof(lm_key
)) != 0) {
457 d_printf("LM Key does not match expectations!\n");
458 d_printf("lm_key:\n");
459 dump_data(1, lm_key
, 8);
460 d_printf("expected:\n");
461 dump_data(1, lm_hash
, 8);
466 if (memcmp(session_key
.data
, lm_key
,
467 sizeof(lm_key
)) != 0) {
468 d_printf("LM Key does not match expectations (first 8 session key)!\n");
469 d_printf("lm_key:\n");
470 dump_data(1, lm_key
, 8);
471 d_printf("expected:\n");
472 dump_data(1, session_key
.data
, 8);
477 if (lm_good
&& memcmp(lm_hash
, user_session_key
, 8) != 0) {
478 uint8_t lm_key_expected
[16];
479 memcpy(lm_key_expected
, lm_hash
, 8);
480 memset(lm_key_expected
+8, '\0', 8);
481 if (memcmp(lm_key_expected
, user_session_key
,
483 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
484 d_printf("user_session_key:\n");
485 dump_data(1, user_session_key
, sizeof(user_session_key
));
486 d_printf("expected:\n");
487 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
495 * Test the NTLM response only, but in the both the NT and LM fields.
498 static bool test_ntlm_in_both(struct samlogon_state
*samlogon_state
, char **error_string
)
503 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
504 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
508 uint8_t user_session_key
[16];
512 ZERO_STRUCT(user_session_key
);
514 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
516 E_md4hash(samlogon_state
->password
, nt_hash
);
517 SMBsesskeygen_ntv1(nt_hash
,
520 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
522 ZERO_STRUCT(lm_hash
);
525 nt_status
= check_samlogon(samlogon_state
,
527 samlogon_state
->parameter_control
,
528 &samlogon_state
->chall
,
535 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
536 /* for 'old' passwords, we allow the server to be OK or wrong password */
537 if (samlogon_state
->old_password
) {
541 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
542 SAFE_FREE(*error_string
);
543 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
545 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
547 } else if (!NT_STATUS_IS_OK(nt_status
)) {
551 if (!NT_STATUS_IS_OK(nt_status
)) {
555 if (memcmp(lm_hash
, lm_key
,
556 sizeof(lm_key
)) != 0) {
557 d_printf("LM Key does not match expectations!\n");
558 d_printf("lm_key:\n");
559 dump_data(1, lm_key
, 8);
560 d_printf("expected:\n");
561 dump_data(1, lm_hash
, 8);
564 if (memcmp(session_key
.data
, user_session_key
,
565 sizeof(user_session_key
)) != 0) {
566 d_printf("NT Session Key does not match expectations!\n");
567 d_printf("user_session_key:\n");
568 dump_data(1, user_session_key
, 16);
569 d_printf("expected:\n");
570 dump_data(1, session_key
.data
, session_key
.length
);
579 * Test the NTLMv2 and LMv2 responses
587 static bool test_lmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
,
588 enum ntlm_break break_which
,
589 enum ntlmv2_domain ntlmv2_domain
,
594 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
595 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
596 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
597 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
598 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, TEST_MACHINE_NAME
, samlogon_state
->workgroup
);
600 uint8_t lm_session_key
[8];
601 uint8_t user_session_key
[16];
603 ZERO_STRUCT(lm_session_key
);
604 ZERO_STRUCT(user_session_key
);
606 switch (ntlmv2_domain
) {
608 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
609 samlogon_state
->account_name
, samlogon_state
->account_domain
,
610 samlogon_state
->password
, &samlogon_state
->chall
,
612 &lmv2_response
, &ntlmv2_response
,
613 &lmv2_session_key
, &ntlmv2_session_key
)) {
614 data_blob_free(&names_blob
);
619 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
620 samlogon_state
->account_name
, "",
621 samlogon_state
->password
, &samlogon_state
->chall
,
623 &lmv2_response
, &ntlmv2_response
,
624 &lmv2_session_key
, &ntlmv2_session_key
)) {
625 data_blob_free(&names_blob
);
630 data_blob_free(&names_blob
);
632 nt_status
= check_samlogon(samlogon_state
,
634 samlogon_state
->parameter_control
,
635 &samlogon_state
->chall
,
642 data_blob_free(&lmv2_response
);
643 data_blob_free(&ntlmv2_response
);
646 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
647 /* for 'old' passwords, we allow the server to be OK or wrong password */
648 if (samlogon_state
->old_password
) {
651 return break_which
== BREAK_BOTH
;
652 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
653 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
654 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
655 SAFE_FREE(*error_string
);
656 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
658 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
660 } else if (!NT_STATUS_IS_OK(nt_status
)) {
665 switch (break_which
) {
667 if (memcmp(lmv2_session_key
.data
, user_session_key
,
668 sizeof(user_session_key
)) != 0) {
669 d_printf("USER (LMv2) Session Key does not match expectations!\n");
670 d_printf("user_session_key:\n");
671 dump_data(1, user_session_key
, 16);
672 d_printf("expected:\n");
673 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
676 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
677 sizeof(lm_session_key
)) != 0) {
678 d_printf("LM (LMv2) Session Key does not match expectations!\n");
679 d_printf("lm_session_key:\n");
680 dump_data(1, lm_session_key
, 8);
681 d_printf("expected:\n");
682 dump_data(1, lmv2_session_key
.data
, 8);
687 if (memcmp(ntlmv2_session_key
.data
, user_session_key
,
688 sizeof(user_session_key
)) != 0) {
689 if (memcmp(lmv2_session_key
.data
, user_session_key
,
690 sizeof(user_session_key
)) == 0) {
691 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
692 d_printf("user_session_key:\n");
693 dump_data(1, user_session_key
, 16);
694 d_printf("expected:\n");
695 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
699 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
700 d_printf("user_session_key:\n");
701 dump_data(1, user_session_key
, 16);
702 d_printf("expected:\n");
703 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
707 if (memcmp(ntlmv2_session_key
.data
, lm_session_key
,
708 sizeof(lm_session_key
)) != 0) {
709 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
710 sizeof(lm_session_key
)) == 0) {
711 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
712 d_printf("user_session_key:\n");
713 dump_data(1, lm_session_key
, 8);
714 d_printf("expected:\n");
715 dump_data(1, ntlmv2_session_key
.data
, 8);
718 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
719 d_printf("lm_session_key:\n");
720 dump_data(1, lm_session_key
, 8);
721 d_printf("expected:\n");
722 dump_data(1, ntlmv2_session_key
.data
, 8);
732 * Test the NTLM and LMv2 responses
735 static bool test_lmv2_ntlm_broken(struct samlogon_state
*samlogon_state
,
736 enum ntlm_break break_which
,
737 enum ntlmv2_domain ntlmv2_domain
,
742 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
743 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
744 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
745 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
746 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, samlogon_state
->netbios_name
, samlogon_state
->workgroup
);
748 DATA_BLOB ntlm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
749 DATA_BLOB ntlm_session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
753 uint8_t lm_session_key
[8];
754 uint8_t user_session_key
[16];
757 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
759 E_md4hash(samlogon_state
->password
, nt_hash
);
760 SMBsesskeygen_ntv1(nt_hash
,
761 ntlm_session_key
.data
);
763 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
765 ZERO_STRUCT(lm_hash
);
768 ZERO_STRUCT(lm_session_key
);
769 ZERO_STRUCT(user_session_key
);
771 switch (ntlmv2_domain
) {
773 /* TODO - test with various domain cases, and without domain */
774 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
775 samlogon_state
->account_name
, samlogon_state
->account_domain
,
776 samlogon_state
->password
, &samlogon_state
->chall
,
778 &lmv2_response
, &ntlmv2_response
,
779 &lmv2_session_key
, &ntlmv2_session_key
)) {
780 data_blob_free(&names_blob
);
785 /* TODO - test with various domain cases, and without domain */
786 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
787 samlogon_state
->account_name
, "",
788 samlogon_state
->password
, &samlogon_state
->chall
,
790 &lmv2_response
, &ntlmv2_response
,
791 &lmv2_session_key
, &ntlmv2_session_key
)) {
792 data_blob_free(&names_blob
);
798 data_blob_free(&names_blob
);
800 nt_status
= check_samlogon(samlogon_state
,
802 samlogon_state
->parameter_control
,
803 &samlogon_state
->chall
,
810 data_blob_free(&lmv2_response
);
811 data_blob_free(&ntlmv2_response
);
814 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
815 /* for 'old' passwords, we allow the server to be OK or wrong password */
816 if (samlogon_state
->old_password
) {
819 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
820 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
821 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
822 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
823 SAFE_FREE(*error_string
);
824 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
826 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
828 } else if (!NT_STATUS_IS_OK(nt_status
)) {
832 switch (break_which
) {
834 if (memcmp(lmv2_session_key
.data
, user_session_key
,
835 sizeof(user_session_key
)) != 0) {
836 d_printf("USER (LMv2) Session Key does not match expectations!\n");
837 d_printf("user_session_key:\n");
838 dump_data(1, user_session_key
, 16);
839 d_printf("expected:\n");
840 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
843 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
844 sizeof(lm_session_key
)) != 0) {
845 d_printf("LM (LMv2) Session Key does not match expectations!\n");
846 d_printf("lm_session_key:\n");
847 dump_data(1, lm_session_key
, 8);
848 d_printf("expected:\n");
849 dump_data(1, lmv2_session_key
.data
, 8);
854 if (memcmp(ntlm_session_key
.data
, user_session_key
,
855 sizeof(user_session_key
)) != 0) {
856 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
857 d_printf("user_session_key:\n");
858 dump_data(1, user_session_key
, 16);
859 d_printf("expected:\n");
860 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
864 if (memcmp(lm_hash
, lm_session_key
,
865 sizeof(lm_session_key
)) != 0) {
866 d_printf("LM Session Key does not match expectations!\n");
867 d_printf("lm_session_key:\n");
868 dump_data(1, lm_session_key
, 8);
869 d_printf("expected:\n");
870 dump_data(1, lm_hash
, 8);
874 static const uint8_t zeros
[8];
875 if (memcmp(zeros
, lm_session_key
,
876 sizeof(lm_session_key
)) != 0) {
877 d_printf("LM Session Key does not match expectations (zeros)!\n");
878 d_printf("lm_session_key:\n");
879 dump_data(1, lm_session_key
, 8);
880 d_printf("expected:\n");
881 dump_data(1, zeros
, 8);
887 if (memcmp(ntlm_session_key
.data
, user_session_key
,
888 sizeof(user_session_key
)) != 0) {
889 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
890 d_printf("user_session_key:\n");
891 dump_data(1, user_session_key
, 16);
892 d_printf("expected:\n");
893 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
896 if (memcmp(ntlm_session_key
.data
, lm_session_key
,
897 sizeof(lm_session_key
)) != 0) {
898 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
899 d_printf("lm_session_key:\n");
900 dump_data(1, lm_session_key
, 8);
901 d_printf("expected:\n");
902 dump_data(1, ntlm_session_key
.data
, 8);
911 * Test the NTLMv2 and LMv2 responses
914 static bool test_lmv2_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
916 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, UPPER_DOMAIN
, error_string
);
920 static bool test_lmv2_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
922 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, NO_DOMAIN
, error_string
);
927 * Test the LMv2 response only
930 static bool test_lmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
932 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, UPPER_DOMAIN
, error_string
);
935 static bool test_lmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
937 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, NO_DOMAIN
, error_string
);
941 * Test the NTLMv2 response only
944 static bool test_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
946 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, UPPER_DOMAIN
, error_string
);
949 static bool test_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
951 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, NO_DOMAIN
, error_string
);
954 static bool test_lm_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
956 return test_lm_ntlm_broken(samlogon_state
, BREAK_NONE
, error_string
);
959 static bool test_ntlm_lm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
961 return test_lm_ntlm_broken(samlogon_state
, BREAK_LM
, error_string
);
964 static bool test_ntlm_ntlm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
966 return test_lm_ntlm_broken(samlogon_state
, BREAK_NT
, error_string
);
969 static bool test_lm_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
971 return test_lm_ntlm_broken(samlogon_state
, BREAK_BOTH
, error_string
);
973 static bool test_ntlmv2_lmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
975 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
978 static bool test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
980 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
983 static bool test_ntlmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
985 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
989 static bool test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
991 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
995 static bool test_ntlmv2_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
997 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
1000 static bool test_ntlmv2_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1002 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1005 static bool test_lmv2_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
1007 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
1010 static bool test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1012 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1015 static bool test_lmv2_ntlm_break_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
1017 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
1020 static bool test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1022 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
1025 static bool test_lmv2_ntlm_break_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
1027 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
1030 static bool test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1032 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
1036 * Test the NTLM2 response (extra challenge in LM feild)
1038 * This test is the same as the 'break LM' test, but checks that the
1039 * server implements NTLM2 session security in the right place
1040 * (NETLOGON is the wrong place).
1043 static bool test_ntlm2(struct samlogon_state
*samlogon_state
, char **error_string
)
1047 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1048 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1052 uint8_t nt_hash
[16];
1053 uint8_t lm_hash
[16];
1055 uint8_t user_session_key
[16];
1056 uint8_t expected_user_session_key
[16];
1057 uint8_t session_nonce_hash
[16];
1058 uint8_t client_chall
[8];
1060 struct MD5Context md5_session_nonce_ctx
;
1061 HMACMD5Context hmac_ctx
;
1063 ZERO_STRUCT(user_session_key
);
1064 ZERO_STRUCT(lm_key
);
1065 generate_random_buffer(client_chall
, 8);
1067 MD5Init(&md5_session_nonce_ctx
);
1068 MD5Update(&md5_session_nonce_ctx
, samlogon_state
->chall
.data
, 8);
1069 MD5Update(&md5_session_nonce_ctx
, client_chall
, 8);
1070 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
1072 E_md4hash(samlogon_state
->password
, (uint8_t *)nt_hash
);
1073 lm_good
= E_deshash(samlogon_state
->password
, (uint8_t *)lm_hash
);
1074 SMBsesskeygen_ntv1((const uint8_t *)nt_hash
,
1077 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
1079 memcpy(lm_response
.data
, session_nonce_hash
, 8);
1080 memset(lm_response
.data
+ 8, 0, 16);
1082 hmac_md5_init_rfc2104(nt_key
, 16, &hmac_ctx
);
1083 hmac_md5_update(samlogon_state
->chall
.data
, 8, &hmac_ctx
);
1084 hmac_md5_update(client_chall
, 8, &hmac_ctx
);
1085 hmac_md5_final(expected_user_session_key
, &hmac_ctx
);
1087 nt_status
= check_samlogon(samlogon_state
,
1089 samlogon_state
->parameter_control
,
1090 &samlogon_state
->chall
,
1097 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1098 /* for 'old' passwords, we allow the server to be OK or wrong password */
1099 if (samlogon_state
->old_password
) {
1103 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1104 SAFE_FREE(*error_string
);
1105 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1107 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1109 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1114 if (memcmp(lm_hash
, lm_key
,
1115 sizeof(lm_key
)) != 0) {
1116 d_printf("LM Key does not match expectations!\n");
1117 d_printf("lm_key:\n");
1118 dump_data(1, lm_key
, 8);
1119 d_printf("expected:\n");
1120 dump_data(1, lm_hash
, 8);
1124 static const uint8_t zeros
[8];
1125 if (memcmp(zeros
, lm_key
,
1126 sizeof(lm_key
)) != 0) {
1127 d_printf("LM Session Key does not match expectations (zeros)!\n");
1128 d_printf("lm_key:\n");
1129 dump_data(1, lm_key
, 8);
1130 d_printf("expected:\n");
1131 dump_data(1, zeros
, 8);
1135 if (memcmp(nt_key
, user_session_key
, 16) != 0) {
1136 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1137 d_printf("user_session_key:\n");
1138 dump_data(1, user_session_key
, sizeof(user_session_key
));
1139 d_printf("expected:\n");
1140 dump_data(1, nt_key
, sizeof(nt_key
));
1146 static bool test_plaintext(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
1149 DATA_BLOB nt_response
= data_blob(NULL
, 0);
1150 DATA_BLOB lm_response
= data_blob(NULL
, 0);
1153 smb_ucs2_t
*unicodepw
;
1155 uint8_t user_session_key
[16];
1157 uint8_t lm_hash
[16];
1158 static const uint8_t zeros
[8];
1159 DATA_BLOB chall
= data_blob_talloc(samlogon_state
->mem_ctx
, zeros
, sizeof(zeros
));
1160 bool lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
1162 ZERO_STRUCT(user_session_key
);
1164 if (!push_ucs2_talloc(samlogon_state
->mem_ctx
,
1165 &unicodepw
, samlogon_state
->password
, NULL
)) {
1166 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1170 nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, unicodepw
, strlen_m(samlogon_state
->password
)*2);
1172 password
= strupper_talloc(samlogon_state
->mem_ctx
, samlogon_state
->password
);
1174 if (!convert_string_talloc_convenience(samlogon_state
->mem_ctx
,
1175 samlogon_state
->iconv_convenience
,
1177 password
, strlen(password
)+1,
1178 (void**)&dospw
, NULL
, false)) {
1179 DEBUG(0, ("convert_string_talloc failed!\n"));
1183 lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, dospw
, strlen(dospw
));
1185 nt_status
= check_samlogon(samlogon_state
,
1187 samlogon_state
->parameter_control
| MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
,
1195 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1196 /* for 'old' passwords, we allow the server to be OK or wrong password */
1197 if (samlogon_state
->old_password
) {
1200 /* for 'long' passwords, the LM password is invalid */
1201 if (break_which
== NO_NT
&& !lm_good
) {
1204 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
1205 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
1206 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
1207 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1208 SAFE_FREE(*error_string
);
1209 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1211 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1213 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1217 if (break_which
== NO_NT
&& !lm_good
) {
1218 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1225 static bool test_plaintext_none_broken(struct samlogon_state
*samlogon_state
,
1226 char **error_string
) {
1227 return test_plaintext(samlogon_state
, BREAK_NONE
, error_string
);
1230 static bool test_plaintext_lm_broken(struct samlogon_state
*samlogon_state
,
1231 char **error_string
) {
1232 return test_plaintext(samlogon_state
, BREAK_LM
, error_string
);
1235 static bool test_plaintext_nt_broken(struct samlogon_state
*samlogon_state
,
1236 char **error_string
) {
1237 return test_plaintext(samlogon_state
, BREAK_NT
, error_string
);
1240 static bool test_plaintext_nt_only(struct samlogon_state
*samlogon_state
,
1241 char **error_string
) {
1242 return test_plaintext(samlogon_state
, NO_LM
, error_string
);
1245 static bool test_plaintext_lm_only(struct samlogon_state
*samlogon_state
,
1246 char **error_string
) {
1247 return test_plaintext(samlogon_state
, NO_NT
, error_string
);
1261 - plaintext tests (in challenge-response fields)
1263 check we get the correct session key in each case
1264 check what values we get for the LM session key
1268 static const struct ntlm_tests
{
1269 bool (*fn
)(struct samlogon_state
*, char **);
1273 {test_lmv2_ntlmv2
, "NTLMv2 and LMv2", false},
1275 {test_lmv2_ntlmv2_no_dom
, "NTLMv2 and LMv2 (no domain)", false},
1277 {test_lm
, "LM", false},
1278 {test_lm_ntlm
, "LM and NTLM", false},
1279 {test_lm_ntlm_both_broken
, "LM and NTLM, both broken", false},
1280 {test_ntlm
, "NTLM", false},
1281 {test_ntlm_in_lm
, "NTLM in LM", false},
1282 {test_ntlm_in_both
, "NTLM in both", false},
1283 {test_ntlmv2
, "NTLMv2", false},
1284 {test_ntlmv2_no_dom
, "NTLMv2 (no domain)", false},
1285 {test_lmv2
, "LMv2", false},
1286 {test_lmv2_no_dom
, "LMv2 (no domain)", false},
1287 {test_ntlmv2_lmv2_broken
, "NTLMv2 and LMv2, LMv2 broken", false},
1288 {test_ntlmv2_lmv2_broken_no_dom
, "NTLMv2 and LMv2, LMv2 broken (no domain)", false},
1289 {test_ntlmv2_ntlmv2_broken
, "NTLMv2 and LMv2, NTLMv2 broken", false},
1291 {test_ntlmv2_ntlmv2_broken_no_dom
, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", false},
1293 {test_ntlmv2_both_broken
, "NTLMv2 and LMv2, both broken", false},
1294 {test_ntlmv2_both_broken_no_dom
, "NTLMv2 and LMv2, both broken (no domain)", false},
1295 {test_ntlm_lm_broken
, "NTLM and LM, LM broken", false},
1296 {test_ntlm_ntlm_broken
, "NTLM and LM, NTLM broken", false},
1297 {test_ntlm2
, "NTLM2 (NTLMv2 session security)", false},
1298 {test_lmv2_ntlm_both_broken
, "LMv2 and NTLM, both broken", false},
1299 {test_lmv2_ntlm_both_broken_no_dom
, "LMv2 and NTLM, both broken (no domain)", false},
1300 {test_lmv2_ntlm_break_ntlm
, "LMv2 and NTLM, NTLM broken", false},
1301 {test_lmv2_ntlm_break_ntlm_no_dom
, "LMv2 and NTLM, NTLM broken (no domain)", false},
1302 {test_lmv2_ntlm_break_lm
, "LMv2 and NTLM, LMv2 broken", false},
1303 {test_lmv2_ntlm_break_lm_no_dom
, "LMv2 and NTLM, LMv2 broken (no domain)", false},
1304 {test_plaintext_none_broken
, "Plaintext", false},
1305 {test_plaintext_lm_broken
, "Plaintext LM broken", false},
1306 {test_plaintext_nt_broken
, "Plaintext NT broken", false},
1307 {test_plaintext_nt_only
, "Plaintext NT only", false},
1308 {test_plaintext_lm_only
, "Plaintext LM only", false},
1313 try a netlogon SamLogon
1315 static bool test_SamLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1316 struct torture_context
*tctx
,
1317 struct creds_CredentialState
*creds
,
1318 const char *comment
,
1319 const char *account_domain
, const char *account_name
,
1320 const char *plain_pass
, uint32_t parameter_control
,
1321 NTSTATUS expected_error
, bool old_password
,
1324 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_SamLogon function-level context");
1327 int validation_levels
[] = {2,3,6};
1328 int logon_levels
[] = { 2, 6 };
1329 int function_levels
[] = {
1330 NDR_NETR_LOGONSAMLOGON
,
1331 NDR_NETR_LOGONSAMLOGONEX
,
1332 NDR_NETR_LOGONSAMLOGONWITHFLAGS
};
1333 struct samlogon_state samlogon_state
;
1335 union netr_LogonLevel logon
;
1336 union netr_Validation validation
;
1337 uint8_t authoritative
= 0;
1342 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1344 samlogon_state
.comment
= comment
;
1345 samlogon_state
.account_name
= account_name
;
1346 samlogon_state
.account_domain
= account_domain
;
1347 samlogon_state
.password
= plain_pass
;
1348 samlogon_state
.workgroup
= lp_workgroup(tctx
->lp_ctx
);
1349 samlogon_state
.netbios_name
= lp_netbios_name(tctx
->lp_ctx
);
1350 samlogon_state
.p
= p
;
1351 samlogon_state
.creds
= creds
;
1352 samlogon_state
.expected_error
= expected_error
;
1353 samlogon_state
.chall
= data_blob_talloc(fn_ctx
, NULL
, 8);
1354 samlogon_state
.parameter_control
= parameter_control
;
1355 samlogon_state
.old_password
= old_password
;
1356 samlogon_state
.iconv_convenience
= lp_iconv_convenience(tctx
->lp_ctx
);
1358 generate_random_buffer(samlogon_state
.chall
.data
, 8);
1359 samlogon_state
.r_flags
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1360 samlogon_state
.r_flags
.in
.computer_name
= TEST_MACHINE_NAME
;
1361 samlogon_state
.r_flags
.in
.credential
= &samlogon_state
.auth
;
1362 samlogon_state
.r_flags
.in
.return_authenticator
= &samlogon_state
.auth2
;
1363 samlogon_state
.r_flags
.in
.flags
= &flags
;
1364 samlogon_state
.r_flags
.in
.logon
= &logon
;
1365 samlogon_state
.r_flags
.out
.validation
= &validation
;
1366 samlogon_state
.r_flags
.out
.authoritative
= &authoritative
;
1367 samlogon_state
.r_flags
.out
.flags
= &flags
;
1369 samlogon_state
.r_ex
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1370 samlogon_state
.r_ex
.in
.computer_name
= TEST_MACHINE_NAME
;
1371 samlogon_state
.r_ex
.in
.flags
= &flags
;
1372 samlogon_state
.r_ex
.in
.logon
= &logon
;
1373 samlogon_state
.r_ex
.out
.validation
= &validation
;
1374 samlogon_state
.r_ex
.out
.authoritative
= &authoritative
;
1375 samlogon_state
.r_ex
.out
.flags
= &flags
;
1377 samlogon_state
.r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1378 samlogon_state
.r
.in
.computer_name
= TEST_MACHINE_NAME
;
1379 samlogon_state
.r
.in
.credential
= &samlogon_state
.auth
;
1380 samlogon_state
.r
.in
.return_authenticator
= &samlogon_state
.auth2
;
1381 samlogon_state
.r
.in
.logon
= &logon
;
1382 samlogon_state
.r
.out
.validation
= &validation
;
1383 samlogon_state
.r
.out
.authoritative
= &authoritative
;
1386 for (f
=0;f
<ARRAY_SIZE(function_levels
);f
++) {
1387 for (i
=0; test_table
[i
].fn
; i
++) {
1388 if (n_subtests
&& (i
> n_subtests
)) {
1391 for (v
=0;v
<ARRAY_SIZE(validation_levels
);v
++) {
1392 for (l
=0;l
<ARRAY_SIZE(logon_levels
);l
++) {
1393 char *error_string
= NULL
;
1394 TALLOC_CTX
*tmp_ctx
= talloc_named(fn_ctx
, 0, "test_SamLogon inner loop");
1395 samlogon_state
.mem_ctx
= tmp_ctx
;
1396 samlogon_state
.function_level
= function_levels
[f
];
1397 samlogon_state
.r
.in
.validation_level
= validation_levels
[v
];
1398 samlogon_state
.r
.in
.logon_level
= logon_levels
[l
];
1399 samlogon_state
.r_ex
.in
.validation_level
= validation_levels
[v
];
1400 samlogon_state
.r_ex
.in
.logon_level
= logon_levels
[l
];
1401 samlogon_state
.r_flags
.in
.validation_level
= validation_levels
[v
];
1402 samlogon_state
.r_flags
.in
.logon_level
= logon_levels
[l
];
1403 if (!test_table
[i
].fn(&samlogon_state
, &error_string
)) {
1404 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1405 samlogon_state
.comment
,
1406 samlogon_state
.account_domain
,
1407 samlogon_state
.account_name
,
1408 test_table
[i
].name
, validation_levels
[v
],
1409 logon_levels
[l
], function_levels
[f
]);
1411 if (test_table
[i
].expect_fail
) {
1412 d_printf(" failed (expected, test incomplete): %s\n", error_string
);
1414 d_printf(" failed: %s\n", error_string
);
1417 SAFE_FREE(error_string
);
1419 talloc_free(tmp_ctx
);
1424 talloc_free(fn_ctx
);
1429 test an ADS style interactive domain logon
1431 bool test_InteractiveLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1432 struct creds_CredentialState
*creds
,
1433 const char *comment
,
1434 const char *workstation_name
,
1435 const char *account_domain
, const char *account_name
,
1436 const char *plain_pass
, uint32_t parameter_control
,
1437 NTSTATUS expected_error
)
1440 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_InteractiveLogon function-level context");
1441 struct netr_LogonSamLogonWithFlags r
;
1442 struct netr_Authenticator a
, ra
;
1443 struct netr_PasswordInfo pinfo
;
1446 union netr_LogonLevel logon
;
1447 union netr_Validation validation
;
1448 uint8_t authoritative
= 0;
1455 ZERO_STRUCT(validation
);
1457 creds_client_authenticator(creds
, &a
);
1459 logon
.password
= &pinfo
;
1461 r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1462 r
.in
.computer_name
= TEST_MACHINE_NAME
;
1463 r
.in
.credential
= &a
;
1464 r
.in
.return_authenticator
= &ra
;
1465 r
.in
.logon_level
= 5;
1466 r
.in
.logon
= &logon
;
1467 r
.in
.validation_level
= 6;
1468 r
.in
.flags
= &flags
;
1469 r
.out
.validation
= &validation
;
1470 r
.out
.authoritative
= &authoritative
;
1471 r
.out
.flags
= &flags
;
1473 pinfo
.identity_info
.domain_name
.string
= account_domain
;
1474 pinfo
.identity_info
.parameter_control
= parameter_control
;
1475 pinfo
.identity_info
.logon_id_low
= 0;
1476 pinfo
.identity_info
.logon_id_high
= 0;
1477 pinfo
.identity_info
.account_name
.string
= account_name
;
1478 pinfo
.identity_info
.workstation
.string
= workstation_name
;
1480 if (!E_deshash(plain_pass
, pinfo
.lmpassword
.hash
)) {
1481 ZERO_STRUCT(pinfo
.lmpassword
.hash
);
1483 E_md4hash(plain_pass
, pinfo
.ntpassword
.hash
);
1485 if (creds
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
1486 creds_arcfour_crypt(creds
, pinfo
.lmpassword
.hash
, 16);
1487 creds_arcfour_crypt(creds
, pinfo
.ntpassword
.hash
, 16);
1489 creds_des_encrypt(creds
, &pinfo
.lmpassword
);
1490 creds_des_encrypt(creds
, &pinfo
.ntpassword
);
1493 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment
);
1495 status
= dcerpc_netr_LogonSamLogonWithFlags(p
, fn_ctx
, &r
);
1496 if (!r
.out
.return_authenticator
1497 || !creds_client_check(creds
, &r
.out
.return_authenticator
->cred
)) {
1498 d_printf("Credential chaining failed\n");
1499 talloc_free(fn_ctx
);
1503 talloc_free(fn_ctx
);
1505 if (!NT_STATUS_EQUAL(expected_error
, status
)) {
1506 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1507 account_domain
, account_name
, nt_errstr(expected_error
), nt_errstr(status
));
1516 bool torture_rpc_samlogon(struct torture_context
*torture
)
1519 struct dcerpc_pipe
*p
;
1520 struct dcerpc_binding
*b
;
1521 struct cli_credentials
*machine_credentials
;
1522 TALLOC_CTX
*mem_ctx
= talloc_init("torture_rpc_netlogon");
1524 struct test_join
*join_ctx
= NULL
;
1525 struct test_join
*user_ctx
= NULL
, *user_ctx_wrong_wks
= NULL
, *user_ctx_wrong_time
= NULL
;
1526 char *user_password
, *user_password_wrong_wks
, *user_password_wrong_time
;
1527 const char *old_user_password
;
1528 char *test_machine_account
;
1529 const char *userdomain
;
1530 struct samr_SetUserInfo s
;
1531 union samr_UserInfo u
;
1535 unsigned int credential_flags
[] = {
1536 NETLOGON_NEG_AUTH2_FLAGS
,
1537 NETLOGON_NEG_ARCFOUR
,
1538 NETLOGON_NEG_ARCFOUR
| NETLOGON_NEG_128BIT
,
1539 NETLOGON_NEG_AUTH2_ADS_FLAGS
,
1540 0 /* yes, this is a valid flag, causes the use of DES */
1543 struct creds_CredentialState
*creds
;
1545 test_machine_account
= talloc_asprintf(mem_ctx
, "%s$", TEST_MACHINE_NAME
);
1546 /* We only need to join as a workstation here, and in future,
1547 * if we wish to test against trusted domains, we must be a
1548 * workstation here */
1549 join_ctx
= torture_join_domain(torture
, TEST_MACHINE_NAME
, ACB_WSTRUST
,
1550 &machine_credentials
);
1552 d_printf("Failed to join as Workstation\n");
1556 userdomain
= torture_setting_string(torture
, "userdomain", lp_workgroup(torture
->lp_ctx
));
1558 user_ctx
= torture_create_testuser(torture
,
1562 (const char **)&user_password
);
1564 d_printf("Failed to create a test user\n");
1568 old_user_password
= user_password
;
1570 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx
), torture
,
1571 TEST_USER_NAME
, 16 /* > 14 */, &user_password
,
1574 user_ctx_wrong_wks
= torture_create_testuser(torture
,
1575 TEST_USER_NAME_WRONG_WKS
,
1578 (const char **)&user_password_wrong_wks
);
1579 if (!user_ctx_wrong_wks
) {
1580 d_printf("Failed to create a test user (wrong workstation test)\n");
1585 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_wks
);
1589 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
;
1590 u
.info21
.workstations
.string
= "not" TEST_MACHINE_NAME
;
1592 status
= dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks
), mem_ctx
, &s
);
1593 if (!NT_STATUS_IS_OK(status
)) {
1594 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status
));
1600 = torture_create_testuser(torture
, TEST_USER_NAME_WRONG_TIME
,
1603 (const char **)&user_password_wrong_time
);
1604 if (!user_ctx_wrong_time
) {
1605 d_printf("Failed to create a test user (wrong workstation test)\n");
1610 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_time
);
1614 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
| SAMR_FIELD_LOGON_HOURS
;
1615 u
.info21
.workstations
.string
= TEST_MACHINE_NAME
;
1616 u
.info21
.logon_hours
.units_per_week
= 168;
1617 u
.info21
.logon_hours
.bits
= talloc_zero_array(mem_ctx
, uint8_t, 168);
1619 status
= dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time
), mem_ctx
, &s
);
1620 if (!NT_STATUS_IS_OK(status
)) {
1621 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status
));
1626 status
= torture_rpc_binding(torture
, &b
);
1627 if (!NT_STATUS_IS_OK(status
)) {
1632 /* We have to use schannel, otherwise the SamLogonEx fails
1633 * with INTERNAL_ERROR */
1635 b
->flags
&= ~DCERPC_AUTH_OPTIONS
;
1636 b
->flags
|= DCERPC_SCHANNEL
| DCERPC_SIGN
| DCERPC_SCHANNEL_128
;
1638 status
= dcerpc_pipe_connect_b(mem_ctx
, &p
, b
,
1639 &ndr_table_netlogon
,
1640 machine_credentials
, torture
->ev
, torture
->lp_ctx
);
1642 if (!NT_STATUS_IS_OK(status
)) {
1643 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status
));
1648 status
= dcerpc_schannel_creds(p
->conn
->security_state
.generic_state
, mem_ctx
, &creds
);
1649 if (!NT_STATUS_IS_OK(status
)) {
1657 const char *comment
;
1659 const char *username
;
1660 const char *password
;
1662 NTSTATUS expected_interactive_error
;
1663 NTSTATUS expected_network_error
;
1664 uint32_t parameter_control
;
1665 bool old_password
; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1668 .comment
= "domain\\user",
1669 .domain
= cli_credentials_get_domain(cmdline_credentials
),
1670 .username
= cli_credentials_get_username(cmdline_credentials
),
1671 .password
= cli_credentials_get_password(cmdline_credentials
),
1672 .network_login
= true,
1673 .expected_interactive_error
= NT_STATUS_OK
,
1674 .expected_network_error
= NT_STATUS_OK
1677 .comment
= "realm\\user",
1678 .domain
= cli_credentials_get_realm(cmdline_credentials
),
1679 .username
= cli_credentials_get_username(cmdline_credentials
),
1680 .password
= cli_credentials_get_password(cmdline_credentials
),
1681 .network_login
= true,
1682 .expected_interactive_error
= NT_STATUS_OK
,
1683 .expected_network_error
= NT_STATUS_OK
1686 .comment
= "user@domain",
1688 .username
= talloc_asprintf(mem_ctx
,
1690 cli_credentials_get_username(cmdline_credentials
),
1691 cli_credentials_get_domain(cmdline_credentials
)
1693 .password
= cli_credentials_get_password(cmdline_credentials
),
1694 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1695 .expected_interactive_error
= NT_STATUS_OK
,
1696 .expected_network_error
= NT_STATUS_OK
1699 .comment
= "user@realm",
1701 .username
= talloc_asprintf(mem_ctx
,
1703 cli_credentials_get_username(cmdline_credentials
),
1704 cli_credentials_get_realm(cmdline_credentials
)
1706 .password
= cli_credentials_get_password(cmdline_credentials
),
1707 .network_login
= true,
1708 .expected_interactive_error
= NT_STATUS_OK
,
1709 .expected_network_error
= NT_STATUS_OK
1712 .comment
= "machine domain\\user",
1713 .domain
= cli_credentials_get_domain(machine_credentials
),
1714 .username
= cli_credentials_get_username(machine_credentials
),
1715 .password
= cli_credentials_get_password(machine_credentials
),
1716 .network_login
= true,
1717 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1718 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1721 .comment
= "machine domain\\user",
1722 .domain
= cli_credentials_get_domain(machine_credentials
),
1723 .username
= cli_credentials_get_username(machine_credentials
),
1724 .password
= cli_credentials_get_password(machine_credentials
),
1725 .network_login
= true,
1726 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1727 .expected_network_error
= NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1730 .comment
= "machine realm\\user",
1731 .domain
= cli_credentials_get_realm(machine_credentials
),
1732 .username
= cli_credentials_get_username(machine_credentials
),
1733 .password
= cli_credentials_get_password(machine_credentials
),
1734 .network_login
= true,
1735 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1736 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1739 .comment
= "machine user@domain",
1741 .username
= talloc_asprintf(mem_ctx
,
1743 cli_credentials_get_username(machine_credentials
),
1744 cli_credentials_get_domain(machine_credentials
)
1746 .password
= cli_credentials_get_password(machine_credentials
),
1747 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1748 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1749 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1752 .comment
= "machine user@realm",
1754 .username
= talloc_asprintf(mem_ctx
,
1756 cli_credentials_get_username(machine_credentials
),
1757 cli_credentials_get_realm(machine_credentials
)
1759 .password
= cli_credentials_get_password(machine_credentials
),
1760 .network_login
= true,
1761 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1762 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1765 .comment
= "test user (long pw): domain\\user",
1766 .domain
= userdomain
,
1767 .username
= TEST_USER_NAME
,
1768 .password
= user_password
,
1769 .network_login
= true,
1770 .expected_interactive_error
= NT_STATUS_OK
,
1771 .expected_network_error
= NT_STATUS_OK
1774 .comment
= "test user (long pw): user@realm",
1776 .username
= talloc_asprintf(mem_ctx
,
1779 lp_realm(torture
->lp_ctx
)),
1780 .password
= user_password
,
1781 .network_login
= true,
1782 .expected_interactive_error
= NT_STATUS_OK
,
1783 .expected_network_error
= NT_STATUS_OK
1786 .comment
= "test user (long pw): user@domain",
1788 .username
= talloc_asprintf(mem_ctx
,
1792 .password
= user_password
,
1793 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1794 .expected_interactive_error
= NT_STATUS_OK
,
1795 .expected_network_error
= NT_STATUS_OK
1797 /* Oddball, can we use the old password ? */
1799 .comment
= "test user: user\\domain OLD PASSWORD",
1800 .domain
= userdomain
,
1801 .username
= TEST_USER_NAME
,
1802 .password
= old_user_password
,
1803 .network_login
= true,
1804 .expected_interactive_error
= NT_STATUS_WRONG_PASSWORD
,
1805 .expected_network_error
= NT_STATUS_OK
,
1806 .old_password
= true
1809 .comment
= "test user (wong workstation): domain\\user",
1810 .domain
= userdomain
,
1811 .username
= TEST_USER_NAME_WRONG_WKS
,
1812 .password
= user_password_wrong_wks
,
1813 .network_login
= true,
1814 .expected_interactive_error
= NT_STATUS_INVALID_WORKSTATION
,
1815 .expected_network_error
= NT_STATUS_INVALID_WORKSTATION
1819 /* Try all the tests for different username forms */
1820 for (ci
= 0; ci
< ARRAY_SIZE(usercreds
); ci
++) {
1822 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1823 usercreds
[ci
].comment
,
1825 usercreds
[ci
].domain
,
1826 usercreds
[ci
].username
,
1827 usercreds
[ci
].password
,
1828 usercreds
[ci
].parameter_control
,
1829 usercreds
[ci
].expected_interactive_error
)) {
1833 if (usercreds
[ci
].network_login
) {
1834 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1835 usercreds
[ci
].comment
,
1836 usercreds
[ci
].domain
,
1837 usercreds
[ci
].username
,
1838 usercreds
[ci
].password
,
1839 usercreds
[ci
].parameter_control
,
1840 usercreds
[ci
].expected_network_error
,
1841 usercreds
[ci
].old_password
,
1848 /* Using the first username form, try the different
1849 * credentials flag setups, on only one of the tests (checks
1850 * session key encryption) */
1852 for (i
=0; i
< ARRAY_SIZE(credential_flags
); i
++) {
1853 /* TODO: Somehow we lost setting up the different credential flags here! */
1855 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1856 usercreds
[0].comment
,
1858 usercreds
[0].domain
,
1859 usercreds
[0].username
,
1860 usercreds
[0].password
,
1861 usercreds
[0].parameter_control
,
1862 usercreds
[0].expected_interactive_error
)) {
1866 if (usercreds
[0].network_login
) {
1867 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1868 usercreds
[0].comment
,
1869 usercreds
[0].domain
,
1870 usercreds
[0].username
,
1871 usercreds
[0].password
,
1872 usercreds
[0].parameter_control
,
1873 usercreds
[0].expected_network_error
,
1874 usercreds
[0].old_password
,
1883 talloc_free(mem_ctx
);
1885 torture_leave_domain(torture
, join_ctx
);
1886 torture_leave_domain(torture
, user_ctx
);
1887 torture_leave_domain(torture
, user_ctx_wrong_wks
);
1888 torture_leave_domain(torture
, user_ctx_wrong_time
);