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 "../lib/crypto/crypto.h"
29 #include "lib/cmdline/popt_common.h"
30 #include "torture/rpc/rpc.h"
31 #include "auth/gensec/gensec.h"
32 #include "libcli/auth/libcli_auth.h"
33 #include "param/param.h"
35 #define TEST_MACHINE_NAME "samlogontest"
36 #define TEST_USER_NAME "samlogontestuser"
37 #define TEST_USER_NAME_WRONG_WKS "samlogontest2"
38 #define TEST_USER_NAME_WRONG_TIME "samlogontest3"
49 struct samlogon_state
{
52 const char *account_name
;
53 const char *account_domain
;
54 const char *netbios_name
;
56 const char *workgroup
;
57 struct dcerpc_pipe
*p
;
59 uint32_t parameter_control
;
60 struct netr_LogonSamLogon r
;
61 struct netr_LogonSamLogonEx r_ex
;
62 struct netr_LogonSamLogonWithFlags r_flags
;
63 struct netr_Authenticator auth
, auth2
;
64 struct netlogon_creds_CredentialState
*creds
;
65 NTSTATUS expected_error
;
66 bool old_password
; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
68 struct smb_iconv_convenience
*iconv_convenience
;
72 Authenticate a user with a challenge/response, checking session key
73 and valid authentication types
75 static NTSTATUS
check_samlogon(struct samlogon_state
*samlogon_state
,
76 enum ntlm_break break_which
,
77 uint32_t parameter_control
,
79 DATA_BLOB
*lm_response
,
80 DATA_BLOB
*nt_response
,
82 uint8_t user_session_key
[16],
86 struct netr_LogonSamLogon
*r
= &samlogon_state
->r
;
87 struct netr_LogonSamLogonEx
*r_ex
= &samlogon_state
->r_ex
;
88 struct netr_LogonSamLogonWithFlags
*r_flags
= &samlogon_state
->r_flags
;
89 struct netr_NetworkInfo ninfo
;
90 struct netr_SamBaseInfo
*base
= NULL
;
91 uint16_t validation_level
= 0;
93 samlogon_state
->r
.in
.logon
->network
= &ninfo
;
94 samlogon_state
->r_ex
.in
.logon
->network
= &ninfo
;
95 samlogon_state
->r_flags
.in
.logon
->network
= &ninfo
;
97 ninfo
.identity_info
.domain_name
.string
= samlogon_state
->account_domain
;
98 ninfo
.identity_info
.parameter_control
= parameter_control
;
99 ninfo
.identity_info
.logon_id_low
= 0;
100 ninfo
.identity_info
.logon_id_high
= 0;
101 ninfo
.identity_info
.account_name
.string
= samlogon_state
->account_name
;
102 ninfo
.identity_info
.workstation
.string
= TEST_MACHINE_NAME
;
104 memcpy(ninfo
.challenge
, chall
->data
, 8);
106 switch (break_which
) {
110 if (lm_response
&& lm_response
->data
) {
111 lm_response
->data
[0]++;
115 if (nt_response
&& nt_response
->data
) {
116 nt_response
->data
[0]++;
120 if (lm_response
&& lm_response
->data
) {
121 lm_response
->data
[0]++;
123 if (nt_response
&& nt_response
->data
) {
124 nt_response
->data
[0]++;
128 data_blob_free(lm_response
);
131 data_blob_free(nt_response
);
136 ninfo
.nt
.data
= nt_response
->data
;
137 ninfo
.nt
.length
= nt_response
->length
;
139 ninfo
.nt
.data
= NULL
;
144 ninfo
.lm
.data
= lm_response
->data
;
145 ninfo
.lm
.length
= lm_response
->length
;
147 ninfo
.lm
.data
= NULL
;
151 switch (samlogon_state
->function_level
) {
152 case NDR_NETR_LOGONSAMLOGON
:
153 ZERO_STRUCT(samlogon_state
->auth2
);
154 netlogon_creds_client_authenticator(samlogon_state
->creds
, &samlogon_state
->auth
);
156 r
->out
.return_authenticator
= NULL
;
157 status
= dcerpc_netr_LogonSamLogon_r(samlogon_state
->p
->binding_handle
,
158 samlogon_state
->mem_ctx
, r
);
159 if (!r
->out
.return_authenticator
||
160 !netlogon_creds_client_check(samlogon_state
->creds
, &r
->out
.return_authenticator
->cred
)) {
161 d_printf("Credential chaining failed\n");
163 if (!NT_STATUS_IS_OK(status
)) {
165 *error_string
= strdup(nt_errstr(status
));
170 validation_level
= r
->in
.validation_level
;
172 netlogon_creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r
->out
.validation
);
174 switch (validation_level
) {
176 base
= &r
->out
.validation
->sam2
->base
;
179 base
= &r
->out
.validation
->sam3
->base
;
182 base
= &r
->out
.validation
->sam6
->base
;
186 case NDR_NETR_LOGONSAMLOGONEX
:
187 status
= dcerpc_netr_LogonSamLogonEx_r(samlogon_state
->p
->binding_handle
,
188 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 netlogon_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 netlogon_creds_client_authenticator(samlogon_state
->creds
, &samlogon_state
->auth
);
216 r_flags
->out
.return_authenticator
= NULL
;
217 status
= dcerpc_netr_LogonSamLogonWithFlags_r(samlogon_state
->p
->binding_handle
,
218 samlogon_state
->mem_ctx
, r_flags
);
219 if (!r_flags
->out
.return_authenticator
||
220 !netlogon_creds_client_check(samlogon_state
->creds
, &r_flags
->out
.return_authenticator
->cred
)) {
221 d_printf("Credential chaining failed\n");
223 if (!NT_STATUS_IS_OK(status
)) {
225 *error_string
= strdup(nt_errstr(status
));
230 validation_level
= r_flags
->in
.validation_level
;
232 netlogon_creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r_flags
->out
.validation
);
234 switch (validation_level
) {
236 base
= &r_flags
->out
.validation
->sam2
->base
;
239 base
= &r_flags
->out
.validation
->sam3
->base
;
242 base
= &r_flags
->out
.validation
->sam6
->base
;
248 return NT_STATUS_INVALID_PARAMETER
;
252 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
253 return NT_STATUS_INVALID_PARAMETER
;
256 if (user_session_key
) {
257 memcpy(user_session_key
, base
->key
.key
, 16);
260 memcpy(lm_key
, base
->LMSessKey
.key
, 8);
268 * Test the normal 'LM and NTLM' combination
271 static bool test_lm_ntlm_broken(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
276 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
277 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
278 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
281 uint8_t user_session_key
[16];
286 ZERO_STRUCT(user_session_key
);
288 lm_good
= SMBencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, lm_response
.data
);
290 ZERO_STRUCT(lm_hash
);
292 E_deshash(samlogon_state
->password
, lm_hash
);
295 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
297 E_md4hash(samlogon_state
->password
, nt_hash
);
298 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
300 nt_status
= check_samlogon(samlogon_state
,
302 samlogon_state
->parameter_control
,
303 &samlogon_state
->chall
,
310 data_blob_free(&lm_response
);
312 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
313 /* for 'long' passwords, the LM password is invalid */
314 if (break_which
== NO_NT
&& !lm_good
) {
317 /* for 'old' passwords, we allow the server to be OK or wrong password */
318 if (samlogon_state
->old_password
) {
321 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
322 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
323 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
324 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
325 SAFE_FREE(*error_string
);
326 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
328 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
330 } else if (!NT_STATUS_IS_OK(nt_status
)) {
334 if (break_which
== NO_NT
&& !lm_good
) {
335 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
339 if (memcmp(lm_hash
, lm_key
,
340 sizeof(lm_key
)) != 0) {
341 d_printf("LM Key does not match expectations!\n");
342 d_printf("lm_key:\n");
343 dump_data(1, lm_key
, 8);
344 d_printf("expected:\n");
345 dump_data(1, lm_hash
, 8);
349 switch (break_which
) {
352 uint8_t lm_key_expected
[16];
353 memcpy(lm_key_expected
, lm_hash
, 8);
354 memset(lm_key_expected
+8, '\0', 8);
355 if (memcmp(lm_key_expected
, user_session_key
,
357 *error_string
= strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
358 d_printf("user_session_key:\n");
359 dump_data(1, user_session_key
, sizeof(user_session_key
));
360 d_printf("expected:\n");
361 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
367 if (memcmp(session_key
.data
, user_session_key
,
368 sizeof(user_session_key
)) != 0) {
369 *error_string
= strdup("NT Session Key does not match expectations!\n");
370 d_printf("user_session_key:\n");
371 dump_data(1, user_session_key
, 16);
372 d_printf("expected:\n");
373 dump_data(1, session_key
.data
, session_key
.length
);
381 * Test LM authentication, no NT response supplied
384 static bool test_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
387 return test_lm_ntlm_broken(samlogon_state
, NO_NT
, error_string
);
391 * Test the NTLM response only, no LM.
394 static bool test_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
396 return test_lm_ntlm_broken(samlogon_state
, NO_LM
, error_string
);
400 * Test the NTLM response only, but in the LM field.
403 static bool test_ntlm_in_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
408 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
409 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
413 uint8_t user_session_key
[16];
417 ZERO_STRUCT(user_session_key
);
419 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
421 E_md4hash(samlogon_state
->password
, nt_hash
);
422 SMBsesskeygen_ntv1(nt_hash
,
425 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
427 ZERO_STRUCT(lm_hash
);
429 nt_status
= check_samlogon(samlogon_state
,
431 samlogon_state
->parameter_control
,
432 &samlogon_state
->chall
,
439 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
440 /* for 'old' passwords, we allow the server to be OK or wrong password */
441 if (samlogon_state
->old_password
) {
445 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
446 SAFE_FREE(*error_string
);
447 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
449 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
451 } else if (!NT_STATUS_IS_OK(nt_status
)) {
456 if (memcmp(lm_hash
, lm_key
,
457 sizeof(lm_key
)) != 0) {
458 d_printf("LM Key does not match expectations!\n");
459 d_printf("lm_key:\n");
460 dump_data(1, lm_key
, 8);
461 d_printf("expected:\n");
462 dump_data(1, lm_hash
, 8);
467 if (memcmp(session_key
.data
, lm_key
,
468 sizeof(lm_key
)) != 0) {
469 d_printf("LM Key does not match expectations (first 8 session key)!\n");
470 d_printf("lm_key:\n");
471 dump_data(1, lm_key
, 8);
472 d_printf("expected:\n");
473 dump_data(1, session_key
.data
, 8);
478 if (lm_good
&& memcmp(lm_hash
, user_session_key
, 8) != 0) {
479 uint8_t lm_key_expected
[16];
480 memcpy(lm_key_expected
, lm_hash
, 8);
481 memset(lm_key_expected
+8, '\0', 8);
482 if (memcmp(lm_key_expected
, user_session_key
,
484 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
485 d_printf("user_session_key:\n");
486 dump_data(1, user_session_key
, sizeof(user_session_key
));
487 d_printf("expected:\n");
488 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
496 * Test the NTLM response only, but in the both the NT and LM fields.
499 static bool test_ntlm_in_both(struct samlogon_state
*samlogon_state
, char **error_string
)
504 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
505 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
509 uint8_t user_session_key
[16];
513 ZERO_STRUCT(user_session_key
);
515 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
517 E_md4hash(samlogon_state
->password
, nt_hash
);
518 SMBsesskeygen_ntv1(nt_hash
,
521 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
523 ZERO_STRUCT(lm_hash
);
526 nt_status
= check_samlogon(samlogon_state
,
528 samlogon_state
->parameter_control
,
529 &samlogon_state
->chall
,
536 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
537 /* for 'old' passwords, we allow the server to be OK or wrong password */
538 if (samlogon_state
->old_password
) {
542 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
543 SAFE_FREE(*error_string
);
544 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
546 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
548 } else if (!NT_STATUS_IS_OK(nt_status
)) {
552 if (!NT_STATUS_IS_OK(nt_status
)) {
556 if (memcmp(lm_hash
, lm_key
,
557 sizeof(lm_key
)) != 0) {
558 d_printf("LM Key does not match expectations!\n");
559 d_printf("lm_key:\n");
560 dump_data(1, lm_key
, 8);
561 d_printf("expected:\n");
562 dump_data(1, lm_hash
, 8);
565 if (memcmp(session_key
.data
, user_session_key
,
566 sizeof(user_session_key
)) != 0) {
567 d_printf("NT Session Key does not match expectations!\n");
568 d_printf("user_session_key:\n");
569 dump_data(1, user_session_key
, 16);
570 d_printf("expected:\n");
571 dump_data(1, session_key
.data
, session_key
.length
);
580 * Test the NTLMv2 and LMv2 responses
588 static bool test_lmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
,
589 enum ntlm_break break_which
,
590 enum ntlmv2_domain ntlmv2_domain
,
595 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
596 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
597 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
598 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
599 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, TEST_MACHINE_NAME
, samlogon_state
->workgroup
);
601 uint8_t lm_session_key
[8];
602 uint8_t user_session_key
[16];
604 ZERO_STRUCT(lm_session_key
);
605 ZERO_STRUCT(user_session_key
);
607 switch (ntlmv2_domain
) {
609 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
610 samlogon_state
->account_name
, samlogon_state
->account_domain
,
611 samlogon_state
->password
, &samlogon_state
->chall
,
613 &lmv2_response
, &ntlmv2_response
,
614 &lmv2_session_key
, &ntlmv2_session_key
)) {
615 data_blob_free(&names_blob
);
620 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
621 samlogon_state
->account_name
, "",
622 samlogon_state
->password
, &samlogon_state
->chall
,
624 &lmv2_response
, &ntlmv2_response
,
625 &lmv2_session_key
, &ntlmv2_session_key
)) {
626 data_blob_free(&names_blob
);
631 data_blob_free(&names_blob
);
633 nt_status
= check_samlogon(samlogon_state
,
635 samlogon_state
->parameter_control
,
636 &samlogon_state
->chall
,
643 data_blob_free(&lmv2_response
);
644 data_blob_free(&ntlmv2_response
);
647 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
648 /* for 'old' passwords, we allow the server to be OK or wrong password */
649 if (samlogon_state
->old_password
) {
652 return break_which
== BREAK_BOTH
;
653 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
654 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
655 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
656 SAFE_FREE(*error_string
);
657 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
659 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
661 } else if (!NT_STATUS_IS_OK(nt_status
)) {
666 switch (break_which
) {
668 if (memcmp(lmv2_session_key
.data
, user_session_key
,
669 sizeof(user_session_key
)) != 0) {
670 d_printf("USER (LMv2) Session Key does not match expectations!\n");
671 d_printf("user_session_key:\n");
672 dump_data(1, user_session_key
, 16);
673 d_printf("expected:\n");
674 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
677 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
678 sizeof(lm_session_key
)) != 0) {
679 d_printf("LM (LMv2) Session Key does not match expectations!\n");
680 d_printf("lm_session_key:\n");
681 dump_data(1, lm_session_key
, 8);
682 d_printf("expected:\n");
683 dump_data(1, lmv2_session_key
.data
, 8);
688 if (memcmp(ntlmv2_session_key
.data
, user_session_key
,
689 sizeof(user_session_key
)) != 0) {
690 if (memcmp(lmv2_session_key
.data
, user_session_key
,
691 sizeof(user_session_key
)) == 0) {
692 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
693 d_printf("user_session_key:\n");
694 dump_data(1, user_session_key
, 16);
695 d_printf("expected:\n");
696 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
700 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
701 d_printf("user_session_key:\n");
702 dump_data(1, user_session_key
, 16);
703 d_printf("expected:\n");
704 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
708 if (memcmp(ntlmv2_session_key
.data
, lm_session_key
,
709 sizeof(lm_session_key
)) != 0) {
710 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
711 sizeof(lm_session_key
)) == 0) {
712 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
713 d_printf("user_session_key:\n");
714 dump_data(1, lm_session_key
, 8);
715 d_printf("expected:\n");
716 dump_data(1, ntlmv2_session_key
.data
, 8);
719 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
720 d_printf("lm_session_key:\n");
721 dump_data(1, lm_session_key
, 8);
722 d_printf("expected:\n");
723 dump_data(1, ntlmv2_session_key
.data
, 8);
733 * Test the NTLM and LMv2 responses
736 static bool test_lmv2_ntlm_broken(struct samlogon_state
*samlogon_state
,
737 enum ntlm_break break_which
,
738 enum ntlmv2_domain ntlmv2_domain
,
743 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
744 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
745 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
746 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
747 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, samlogon_state
->netbios_name
, samlogon_state
->workgroup
);
749 DATA_BLOB ntlm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
750 DATA_BLOB ntlm_session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
754 uint8_t lm_session_key
[8];
755 uint8_t user_session_key
[16];
758 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
760 E_md4hash(samlogon_state
->password
, nt_hash
);
761 SMBsesskeygen_ntv1(nt_hash
,
762 ntlm_session_key
.data
);
764 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
766 ZERO_STRUCT(lm_hash
);
769 ZERO_STRUCT(lm_session_key
);
770 ZERO_STRUCT(user_session_key
);
772 switch (ntlmv2_domain
) {
774 /* TODO - test with various domain cases, and without domain */
775 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
776 samlogon_state
->account_name
, samlogon_state
->account_domain
,
777 samlogon_state
->password
, &samlogon_state
->chall
,
779 &lmv2_response
, &ntlmv2_response
,
780 &lmv2_session_key
, &ntlmv2_session_key
)) {
781 data_blob_free(&names_blob
);
786 /* TODO - test with various domain cases, and without domain */
787 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
788 samlogon_state
->account_name
, "",
789 samlogon_state
->password
, &samlogon_state
->chall
,
791 &lmv2_response
, &ntlmv2_response
,
792 &lmv2_session_key
, &ntlmv2_session_key
)) {
793 data_blob_free(&names_blob
);
799 data_blob_free(&names_blob
);
801 nt_status
= check_samlogon(samlogon_state
,
803 samlogon_state
->parameter_control
,
804 &samlogon_state
->chall
,
811 data_blob_free(&lmv2_response
);
812 data_blob_free(&ntlmv2_response
);
815 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
816 /* for 'old' passwords, we allow the server to be OK or wrong password */
817 if (samlogon_state
->old_password
) {
820 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
821 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
822 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
823 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
824 SAFE_FREE(*error_string
);
825 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
827 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
829 } else if (!NT_STATUS_IS_OK(nt_status
)) {
833 switch (break_which
) {
835 if (memcmp(lmv2_session_key
.data
, user_session_key
,
836 sizeof(user_session_key
)) != 0) {
837 d_printf("USER (LMv2) Session Key does not match expectations!\n");
838 d_printf("user_session_key:\n");
839 dump_data(1, user_session_key
, 16);
840 d_printf("expected:\n");
841 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
844 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
845 sizeof(lm_session_key
)) != 0) {
846 d_printf("LM (LMv2) Session Key does not match expectations!\n");
847 d_printf("lm_session_key:\n");
848 dump_data(1, lm_session_key
, 8);
849 d_printf("expected:\n");
850 dump_data(1, lmv2_session_key
.data
, 8);
855 if (memcmp(ntlm_session_key
.data
, user_session_key
,
856 sizeof(user_session_key
)) != 0) {
857 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
858 d_printf("user_session_key:\n");
859 dump_data(1, user_session_key
, 16);
860 d_printf("expected:\n");
861 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
865 if (memcmp(lm_hash
, lm_session_key
,
866 sizeof(lm_session_key
)) != 0) {
867 d_printf("LM Session Key does not match expectations!\n");
868 d_printf("lm_session_key:\n");
869 dump_data(1, lm_session_key
, 8);
870 d_printf("expected:\n");
871 dump_data(1, lm_hash
, 8);
875 static const uint8_t zeros
[8];
876 if (memcmp(zeros
, lm_session_key
,
877 sizeof(lm_session_key
)) != 0) {
878 d_printf("LM Session Key does not match expectations (zeros)!\n");
879 d_printf("lm_session_key:\n");
880 dump_data(1, lm_session_key
, 8);
881 d_printf("expected:\n");
882 dump_data(1, zeros
, 8);
888 if (memcmp(ntlm_session_key
.data
, user_session_key
,
889 sizeof(user_session_key
)) != 0) {
890 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
891 d_printf("user_session_key:\n");
892 dump_data(1, user_session_key
, 16);
893 d_printf("expected:\n");
894 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
897 if (memcmp(ntlm_session_key
.data
, lm_session_key
,
898 sizeof(lm_session_key
)) != 0) {
899 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
900 d_printf("lm_session_key:\n");
901 dump_data(1, lm_session_key
, 8);
902 d_printf("expected:\n");
903 dump_data(1, ntlm_session_key
.data
, 8);
912 * Test the NTLMv2 and LMv2 responses
915 static bool test_lmv2_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
917 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, UPPER_DOMAIN
, error_string
);
921 static bool test_lmv2_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
923 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, NO_DOMAIN
, error_string
);
928 * Test the LMv2 response only
931 static bool test_lmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
933 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, UPPER_DOMAIN
, error_string
);
936 static bool test_lmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
938 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, NO_DOMAIN
, error_string
);
942 * Test the NTLMv2 response only
945 static bool test_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
947 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, UPPER_DOMAIN
, error_string
);
950 static bool test_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
952 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, NO_DOMAIN
, error_string
);
955 static bool test_lm_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
957 return test_lm_ntlm_broken(samlogon_state
, BREAK_NONE
, error_string
);
960 static bool test_ntlm_lm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
962 return test_lm_ntlm_broken(samlogon_state
, BREAK_LM
, error_string
);
965 static bool test_ntlm_ntlm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
967 return test_lm_ntlm_broken(samlogon_state
, BREAK_NT
, error_string
);
970 static bool test_lm_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
972 return test_lm_ntlm_broken(samlogon_state
, BREAK_BOTH
, error_string
);
974 static bool test_ntlmv2_lmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
976 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
979 static bool test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
981 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
984 static bool test_ntlmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
986 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
990 static bool test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
992 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
996 static bool test_ntlmv2_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
998 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
1001 static bool test_ntlmv2_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1003 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1006 static bool test_lmv2_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
1008 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
1011 static bool test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1013 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1016 static bool test_lmv2_ntlm_break_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
1018 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
1021 static bool test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1023 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
1026 static bool test_lmv2_ntlm_break_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
1028 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
1031 static bool test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1033 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
1037 * Test the NTLM2 response (extra challenge in LM feild)
1039 * This test is the same as the 'break LM' test, but checks that the
1040 * server implements NTLM2 session security in the right place
1041 * (NETLOGON is the wrong place).
1044 static bool test_ntlm2(struct samlogon_state
*samlogon_state
, char **error_string
)
1048 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1049 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1053 uint8_t nt_hash
[16];
1054 uint8_t lm_hash
[16];
1056 uint8_t user_session_key
[16];
1057 uint8_t expected_user_session_key
[16];
1058 uint8_t session_nonce_hash
[16];
1059 uint8_t client_chall
[8];
1061 struct MD5Context md5_session_nonce_ctx
;
1062 HMACMD5Context hmac_ctx
;
1064 ZERO_STRUCT(user_session_key
);
1065 ZERO_STRUCT(lm_key
);
1066 generate_random_buffer(client_chall
, 8);
1068 MD5Init(&md5_session_nonce_ctx
);
1069 MD5Update(&md5_session_nonce_ctx
, samlogon_state
->chall
.data
, 8);
1070 MD5Update(&md5_session_nonce_ctx
, client_chall
, 8);
1071 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
1073 E_md4hash(samlogon_state
->password
, (uint8_t *)nt_hash
);
1074 lm_good
= E_deshash(samlogon_state
->password
, (uint8_t *)lm_hash
);
1075 SMBsesskeygen_ntv1((const uint8_t *)nt_hash
,
1078 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
1080 memcpy(lm_response
.data
, session_nonce_hash
, 8);
1081 memset(lm_response
.data
+ 8, 0, 16);
1083 hmac_md5_init_rfc2104(nt_key
, 16, &hmac_ctx
);
1084 hmac_md5_update(samlogon_state
->chall
.data
, 8, &hmac_ctx
);
1085 hmac_md5_update(client_chall
, 8, &hmac_ctx
);
1086 hmac_md5_final(expected_user_session_key
, &hmac_ctx
);
1088 nt_status
= check_samlogon(samlogon_state
,
1090 samlogon_state
->parameter_control
,
1091 &samlogon_state
->chall
,
1098 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1099 /* for 'old' passwords, we allow the server to be OK or wrong password */
1100 if (samlogon_state
->old_password
) {
1104 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1105 SAFE_FREE(*error_string
);
1106 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1108 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1110 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1115 if (memcmp(lm_hash
, lm_key
,
1116 sizeof(lm_key
)) != 0) {
1117 d_printf("LM Key does not match expectations!\n");
1118 d_printf("lm_key:\n");
1119 dump_data(1, lm_key
, 8);
1120 d_printf("expected:\n");
1121 dump_data(1, lm_hash
, 8);
1125 static const uint8_t zeros
[8];
1126 if (memcmp(zeros
, lm_key
,
1127 sizeof(lm_key
)) != 0) {
1128 d_printf("LM Session Key does not match expectations (zeros)!\n");
1129 d_printf("lm_key:\n");
1130 dump_data(1, lm_key
, 8);
1131 d_printf("expected:\n");
1132 dump_data(1, zeros
, 8);
1136 if (memcmp(nt_key
, user_session_key
, 16) != 0) {
1137 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1138 d_printf("user_session_key:\n");
1139 dump_data(1, user_session_key
, sizeof(user_session_key
));
1140 d_printf("expected:\n");
1141 dump_data(1, nt_key
, sizeof(nt_key
));
1147 static bool test_plaintext(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
1150 DATA_BLOB nt_response
= data_blob(NULL
, 0);
1151 DATA_BLOB lm_response
= data_blob(NULL
, 0);
1154 smb_ucs2_t
*unicodepw
;
1156 uint8_t user_session_key
[16];
1158 uint8_t lm_hash
[16];
1159 static const uint8_t zeros
[8];
1160 DATA_BLOB chall
= data_blob_talloc(samlogon_state
->mem_ctx
, zeros
, sizeof(zeros
));
1161 bool lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
1163 ZERO_STRUCT(user_session_key
);
1165 if (!push_ucs2_talloc(samlogon_state
->mem_ctx
,
1166 &unicodepw
, samlogon_state
->password
, NULL
)) {
1167 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1171 nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, unicodepw
, strlen_m(samlogon_state
->password
)*2);
1173 password
= strupper_talloc(samlogon_state
->mem_ctx
, samlogon_state
->password
);
1175 if (!convert_string_talloc_convenience(samlogon_state
->mem_ctx
,
1176 samlogon_state
->iconv_convenience
,
1178 password
, strlen(password
)+1,
1179 (void**)&dospw
, NULL
, false)) {
1180 DEBUG(0, ("convert_string_talloc failed!\n"));
1184 lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, dospw
, strlen(dospw
));
1186 nt_status
= check_samlogon(samlogon_state
,
1188 samlogon_state
->parameter_control
| MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
,
1196 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1197 /* for 'old' passwords, we allow the server to be OK or wrong password */
1198 if (samlogon_state
->old_password
) {
1201 /* for 'long' passwords, the LM password is invalid */
1202 if (break_which
== NO_NT
&& !lm_good
) {
1205 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
1206 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
1207 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
1208 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1209 SAFE_FREE(*error_string
);
1210 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1212 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1214 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1218 if (break_which
== NO_NT
&& !lm_good
) {
1219 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1226 static bool test_plaintext_none_broken(struct samlogon_state
*samlogon_state
,
1227 char **error_string
) {
1228 return test_plaintext(samlogon_state
, BREAK_NONE
, error_string
);
1231 static bool test_plaintext_lm_broken(struct samlogon_state
*samlogon_state
,
1232 char **error_string
) {
1233 return test_plaintext(samlogon_state
, BREAK_LM
, error_string
);
1236 static bool test_plaintext_nt_broken(struct samlogon_state
*samlogon_state
,
1237 char **error_string
) {
1238 return test_plaintext(samlogon_state
, BREAK_NT
, error_string
);
1241 static bool test_plaintext_nt_only(struct samlogon_state
*samlogon_state
,
1242 char **error_string
) {
1243 return test_plaintext(samlogon_state
, NO_LM
, error_string
);
1246 static bool test_plaintext_lm_only(struct samlogon_state
*samlogon_state
,
1247 char **error_string
) {
1248 return test_plaintext(samlogon_state
, NO_NT
, error_string
);
1262 - plaintext tests (in challenge-response fields)
1264 check we get the correct session key in each case
1265 check what values we get for the LM session key
1269 static const struct ntlm_tests
{
1270 bool (*fn
)(struct samlogon_state
*, char **);
1274 {test_lmv2_ntlmv2
, "NTLMv2 and LMv2", false},
1276 {test_lmv2_ntlmv2_no_dom
, "NTLMv2 and LMv2 (no domain)", false},
1278 {test_lm
, "LM", false},
1279 {test_lm_ntlm
, "LM and NTLM", false},
1280 {test_lm_ntlm_both_broken
, "LM and NTLM, both broken", false},
1281 {test_ntlm
, "NTLM", false},
1282 {test_ntlm_in_lm
, "NTLM in LM", false},
1283 {test_ntlm_in_both
, "NTLM in both", false},
1284 {test_ntlmv2
, "NTLMv2", false},
1285 {test_ntlmv2_no_dom
, "NTLMv2 (no domain)", false},
1286 {test_lmv2
, "LMv2", false},
1287 {test_lmv2_no_dom
, "LMv2 (no domain)", false},
1288 {test_ntlmv2_lmv2_broken
, "NTLMv2 and LMv2, LMv2 broken", false},
1289 {test_ntlmv2_lmv2_broken_no_dom
, "NTLMv2 and LMv2, LMv2 broken (no domain)", false},
1290 {test_ntlmv2_ntlmv2_broken
, "NTLMv2 and LMv2, NTLMv2 broken", false},
1292 {test_ntlmv2_ntlmv2_broken_no_dom
, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", false},
1294 {test_ntlmv2_both_broken
, "NTLMv2 and LMv2, both broken", false},
1295 {test_ntlmv2_both_broken_no_dom
, "NTLMv2 and LMv2, both broken (no domain)", false},
1296 {test_ntlm_lm_broken
, "NTLM and LM, LM broken", false},
1297 {test_ntlm_ntlm_broken
, "NTLM and LM, NTLM broken", false},
1298 {test_ntlm2
, "NTLM2 (NTLMv2 session security)", false},
1299 {test_lmv2_ntlm_both_broken
, "LMv2 and NTLM, both broken", false},
1300 {test_lmv2_ntlm_both_broken_no_dom
, "LMv2 and NTLM, both broken (no domain)", false},
1301 {test_lmv2_ntlm_break_ntlm
, "LMv2 and NTLM, NTLM broken", false},
1302 {test_lmv2_ntlm_break_ntlm_no_dom
, "LMv2 and NTLM, NTLM broken (no domain)", false},
1303 {test_lmv2_ntlm_break_lm
, "LMv2 and NTLM, LMv2 broken", false},
1304 {test_lmv2_ntlm_break_lm_no_dom
, "LMv2 and NTLM, LMv2 broken (no domain)", false},
1305 {test_plaintext_none_broken
, "Plaintext", false},
1306 {test_plaintext_lm_broken
, "Plaintext LM broken", false},
1307 {test_plaintext_nt_broken
, "Plaintext NT broken", false},
1308 {test_plaintext_nt_only
, "Plaintext NT only", false},
1309 {test_plaintext_lm_only
, "Plaintext LM only", false},
1314 try a netlogon SamLogon
1316 static bool test_SamLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1317 struct torture_context
*tctx
,
1318 struct netlogon_creds_CredentialState
*creds
,
1319 const char *comment
,
1320 const char *account_domain
, const char *account_name
,
1321 const char *plain_pass
, uint32_t parameter_control
,
1322 NTSTATUS expected_error
, bool old_password
,
1325 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_SamLogon function-level context");
1328 int validation_levels
[] = {2,3,6};
1329 int logon_levels
[] = { 2, 6 };
1330 int function_levels
[] = {
1331 NDR_NETR_LOGONSAMLOGON
,
1332 NDR_NETR_LOGONSAMLOGONEX
,
1333 NDR_NETR_LOGONSAMLOGONWITHFLAGS
};
1334 struct samlogon_state samlogon_state
;
1336 union netr_LogonLevel logon
;
1337 union netr_Validation validation
;
1338 uint8_t authoritative
= 0;
1343 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1345 samlogon_state
.comment
= comment
;
1346 samlogon_state
.account_name
= account_name
;
1347 samlogon_state
.account_domain
= account_domain
;
1348 samlogon_state
.password
= plain_pass
;
1349 samlogon_state
.workgroup
= lp_workgroup(tctx
->lp_ctx
);
1350 samlogon_state
.netbios_name
= lp_netbios_name(tctx
->lp_ctx
);
1351 samlogon_state
.p
= p
;
1352 samlogon_state
.creds
= creds
;
1353 samlogon_state
.expected_error
= expected_error
;
1354 samlogon_state
.chall
= data_blob_talloc(fn_ctx
, NULL
, 8);
1355 samlogon_state
.parameter_control
= parameter_control
;
1356 samlogon_state
.old_password
= old_password
;
1357 samlogon_state
.iconv_convenience
= lp_iconv_convenience(tctx
->lp_ctx
);
1359 generate_random_buffer(samlogon_state
.chall
.data
, 8);
1360 samlogon_state
.r_flags
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1361 samlogon_state
.r_flags
.in
.computer_name
= TEST_MACHINE_NAME
;
1362 samlogon_state
.r_flags
.in
.credential
= &samlogon_state
.auth
;
1363 samlogon_state
.r_flags
.in
.return_authenticator
= &samlogon_state
.auth2
;
1364 samlogon_state
.r_flags
.in
.flags
= &flags
;
1365 samlogon_state
.r_flags
.in
.logon
= &logon
;
1366 samlogon_state
.r_flags
.out
.validation
= &validation
;
1367 samlogon_state
.r_flags
.out
.authoritative
= &authoritative
;
1368 samlogon_state
.r_flags
.out
.flags
= &flags
;
1370 samlogon_state
.r_ex
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1371 samlogon_state
.r_ex
.in
.computer_name
= TEST_MACHINE_NAME
;
1372 samlogon_state
.r_ex
.in
.flags
= &flags
;
1373 samlogon_state
.r_ex
.in
.logon
= &logon
;
1374 samlogon_state
.r_ex
.out
.validation
= &validation
;
1375 samlogon_state
.r_ex
.out
.authoritative
= &authoritative
;
1376 samlogon_state
.r_ex
.out
.flags
= &flags
;
1378 samlogon_state
.r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1379 samlogon_state
.r
.in
.computer_name
= TEST_MACHINE_NAME
;
1380 samlogon_state
.r
.in
.credential
= &samlogon_state
.auth
;
1381 samlogon_state
.r
.in
.return_authenticator
= &samlogon_state
.auth2
;
1382 samlogon_state
.r
.in
.logon
= &logon
;
1383 samlogon_state
.r
.out
.validation
= &validation
;
1384 samlogon_state
.r
.out
.authoritative
= &authoritative
;
1387 for (f
=0;f
<ARRAY_SIZE(function_levels
);f
++) {
1388 for (i
=0; test_table
[i
].fn
; i
++) {
1389 if (n_subtests
&& (i
> n_subtests
)) {
1392 for (v
=0;v
<ARRAY_SIZE(validation_levels
);v
++) {
1393 for (l
=0;l
<ARRAY_SIZE(logon_levels
);l
++) {
1394 char *error_string
= NULL
;
1395 TALLOC_CTX
*tmp_ctx
= talloc_named(fn_ctx
, 0, "test_SamLogon inner loop");
1396 samlogon_state
.mem_ctx
= tmp_ctx
;
1397 samlogon_state
.function_level
= function_levels
[f
];
1398 samlogon_state
.r
.in
.validation_level
= validation_levels
[v
];
1399 samlogon_state
.r
.in
.logon_level
= logon_levels
[l
];
1400 samlogon_state
.r_ex
.in
.validation_level
= validation_levels
[v
];
1401 samlogon_state
.r_ex
.in
.logon_level
= logon_levels
[l
];
1402 samlogon_state
.r_flags
.in
.validation_level
= validation_levels
[v
];
1403 samlogon_state
.r_flags
.in
.logon_level
= logon_levels
[l
];
1404 if (!test_table
[i
].fn(&samlogon_state
, &error_string
)) {
1405 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1406 samlogon_state
.comment
,
1407 samlogon_state
.account_domain
,
1408 samlogon_state
.account_name
,
1409 test_table
[i
].name
, validation_levels
[v
],
1410 logon_levels
[l
], function_levels
[f
]);
1412 if (test_table
[i
].expect_fail
) {
1413 d_printf(" failed (expected, test incomplete): %s\n", error_string
);
1415 d_printf(" failed: %s\n", error_string
);
1418 SAFE_FREE(error_string
);
1420 talloc_free(tmp_ctx
);
1425 talloc_free(fn_ctx
);
1430 test an ADS style interactive domain logon
1432 bool test_InteractiveLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1433 struct netlogon_creds_CredentialState
*creds
,
1434 const char *comment
,
1435 const char *workstation_name
,
1436 const char *account_domain
, const char *account_name
,
1437 const char *plain_pass
, uint32_t parameter_control
,
1438 NTSTATUS expected_error
)
1441 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_InteractiveLogon function-level context");
1442 struct netr_LogonSamLogonWithFlags r
;
1443 struct netr_Authenticator a
, ra
;
1444 struct netr_PasswordInfo pinfo
;
1447 union netr_LogonLevel logon
;
1448 union netr_Validation validation
;
1449 uint8_t authoritative
= 0;
1450 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
1457 ZERO_STRUCT(validation
);
1459 netlogon_creds_client_authenticator(creds
, &a
);
1461 logon
.password
= &pinfo
;
1463 r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1464 r
.in
.computer_name
= TEST_MACHINE_NAME
;
1465 r
.in
.credential
= &a
;
1466 r
.in
.return_authenticator
= &ra
;
1467 r
.in
.logon_level
= 5;
1468 r
.in
.logon
= &logon
;
1469 r
.in
.validation_level
= 6;
1470 r
.in
.flags
= &flags
;
1471 r
.out
.validation
= &validation
;
1472 r
.out
.authoritative
= &authoritative
;
1473 r
.out
.flags
= &flags
;
1475 pinfo
.identity_info
.domain_name
.string
= account_domain
;
1476 pinfo
.identity_info
.parameter_control
= parameter_control
;
1477 pinfo
.identity_info
.logon_id_low
= 0;
1478 pinfo
.identity_info
.logon_id_high
= 0;
1479 pinfo
.identity_info
.account_name
.string
= account_name
;
1480 pinfo
.identity_info
.workstation
.string
= workstation_name
;
1482 if (!E_deshash(plain_pass
, pinfo
.lmpassword
.hash
)) {
1483 ZERO_STRUCT(pinfo
.lmpassword
.hash
);
1485 E_md4hash(plain_pass
, pinfo
.ntpassword
.hash
);
1487 if (creds
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
1488 netlogon_creds_arcfour_crypt(creds
, pinfo
.lmpassword
.hash
, 16);
1489 netlogon_creds_arcfour_crypt(creds
, pinfo
.ntpassword
.hash
, 16);
1491 netlogon_creds_des_encrypt(creds
, &pinfo
.lmpassword
);
1492 netlogon_creds_des_encrypt(creds
, &pinfo
.ntpassword
);
1495 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment
);
1497 status
= dcerpc_netr_LogonSamLogonWithFlags_r(b
, fn_ctx
, &r
);
1498 if (!r
.out
.return_authenticator
1499 || !netlogon_creds_client_check(creds
, &r
.out
.return_authenticator
->cred
)) {
1500 d_printf("Credential chaining failed\n");
1501 talloc_free(fn_ctx
);
1505 talloc_free(fn_ctx
);
1507 if (!NT_STATUS_EQUAL(expected_error
, status
)) {
1508 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1509 account_domain
, account_name
, nt_errstr(expected_error
), nt_errstr(status
));
1518 bool torture_rpc_samlogon(struct torture_context
*torture
)
1521 struct dcerpc_pipe
*p
;
1522 struct dcerpc_binding
*b
;
1523 struct cli_credentials
*machine_credentials
;
1524 TALLOC_CTX
*mem_ctx
= talloc_init("torture_rpc_netlogon");
1526 struct test_join
*join_ctx
= NULL
;
1527 struct test_join
*user_ctx
= NULL
, *user_ctx_wrong_wks
= NULL
, *user_ctx_wrong_time
= NULL
;
1528 char *user_password
, *user_password_wrong_wks
, *user_password_wrong_time
;
1529 const char *old_user_password
;
1530 char *test_machine_account
;
1531 const char *userdomain
;
1532 struct samr_SetUserInfo s
;
1533 union samr_UserInfo u
;
1537 unsigned int credential_flags
[] = {
1538 NETLOGON_NEG_AUTH2_FLAGS
,
1539 NETLOGON_NEG_ARCFOUR
,
1540 NETLOGON_NEG_ARCFOUR
| NETLOGON_NEG_128BIT
,
1541 NETLOGON_NEG_AUTH2_ADS_FLAGS
,
1542 0 /* yes, this is a valid flag, causes the use of DES */
1545 struct netlogon_creds_CredentialState
*creds
;
1546 struct dcerpc_pipe
*tmp_p
= NULL
;
1548 test_machine_account
= talloc_asprintf(mem_ctx
, "%s$", TEST_MACHINE_NAME
);
1549 /* We only need to join as a workstation here, and in future,
1550 * if we wish to test against trusted domains, we must be a
1551 * workstation here */
1552 join_ctx
= torture_join_domain(torture
, TEST_MACHINE_NAME
, ACB_WSTRUST
,
1553 &machine_credentials
);
1555 d_printf("Failed to join as Workstation\n");
1559 userdomain
= torture_setting_string(torture
, "userdomain", lp_workgroup(torture
->lp_ctx
));
1561 user_ctx
= torture_create_testuser(torture
,
1565 (const char **)&user_password
);
1567 d_printf("Failed to create a test user\n");
1571 old_user_password
= user_password
;
1573 tmp_p
= torture_join_samr_pipe(user_ctx
);
1574 test_ChangePasswordUser3(tmp_p
, torture
,
1575 TEST_USER_NAME
, 16 /* > 14 */, &user_password
,
1578 user_ctx_wrong_wks
= torture_create_testuser(torture
,
1579 TEST_USER_NAME_WRONG_WKS
,
1582 (const char **)&user_password_wrong_wks
);
1583 if (!user_ctx_wrong_wks
) {
1584 d_printf("Failed to create a test user (wrong workstation test)\n");
1589 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_wks
);
1593 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
;
1594 u
.info21
.workstations
.string
= "not" TEST_MACHINE_NAME
;
1596 tmp_p
= torture_join_samr_pipe(user_ctx_wrong_wks
);
1597 status
= dcerpc_samr_SetUserInfo_r(tmp_p
->binding_handle
, mem_ctx
, &s
);
1598 if (!NT_STATUS_IS_OK(status
)) {
1599 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status
));
1605 = torture_create_testuser(torture
, TEST_USER_NAME_WRONG_TIME
,
1608 (const char **)&user_password_wrong_time
);
1609 if (!user_ctx_wrong_time
) {
1610 d_printf("Failed to create a test user (wrong workstation test)\n");
1615 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_time
);
1619 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
| SAMR_FIELD_LOGON_HOURS
;
1620 u
.info21
.workstations
.string
= TEST_MACHINE_NAME
;
1621 u
.info21
.logon_hours
.units_per_week
= 168;
1622 u
.info21
.logon_hours
.bits
= talloc_zero_array(mem_ctx
, uint8_t, 168);
1624 tmp_p
= torture_join_samr_pipe(user_ctx_wrong_time
);
1625 status
= dcerpc_samr_SetUserInfo_r(tmp_p
->binding_handle
, mem_ctx
, &s
);
1626 if (!NT_STATUS_IS_OK(status
)) {
1627 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status
));
1632 status
= torture_rpc_binding(torture
, &b
);
1633 if (!NT_STATUS_IS_OK(status
)) {
1638 /* We have to use schannel, otherwise the SamLogonEx fails
1639 * with INTERNAL_ERROR */
1641 b
->flags
&= ~DCERPC_AUTH_OPTIONS
;
1642 b
->flags
|= DCERPC_SCHANNEL
| DCERPC_SIGN
| DCERPC_SCHANNEL_128
;
1644 status
= dcerpc_pipe_connect_b(mem_ctx
, &p
, b
,
1645 &ndr_table_netlogon
,
1646 machine_credentials
, torture
->ev
, torture
->lp_ctx
);
1648 if (!NT_STATUS_IS_OK(status
)) {
1649 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status
));
1654 status
= dcerpc_schannel_creds(p
->conn
->security_state
.generic_state
, mem_ctx
, &creds
);
1655 if (!NT_STATUS_IS_OK(status
)) {
1663 const char *comment
;
1665 const char *username
;
1666 const char *password
;
1668 NTSTATUS expected_interactive_error
;
1669 NTSTATUS expected_network_error
;
1670 uint32_t parameter_control
;
1671 bool old_password
; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1674 .comment
= "domain\\user",
1675 .domain
= cli_credentials_get_domain(cmdline_credentials
),
1676 .username
= cli_credentials_get_username(cmdline_credentials
),
1677 .password
= cli_credentials_get_password(cmdline_credentials
),
1678 .network_login
= true,
1679 .expected_interactive_error
= NT_STATUS_OK
,
1680 .expected_network_error
= NT_STATUS_OK
1683 .comment
= "realm\\user",
1684 .domain
= cli_credentials_get_realm(cmdline_credentials
),
1685 .username
= cli_credentials_get_username(cmdline_credentials
),
1686 .password
= cli_credentials_get_password(cmdline_credentials
),
1687 .network_login
= true,
1688 .expected_interactive_error
= NT_STATUS_OK
,
1689 .expected_network_error
= NT_STATUS_OK
1692 .comment
= "user@domain",
1694 .username
= talloc_asprintf(mem_ctx
,
1696 cli_credentials_get_username(cmdline_credentials
),
1697 cli_credentials_get_domain(cmdline_credentials
)
1699 .password
= cli_credentials_get_password(cmdline_credentials
),
1700 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1701 .expected_interactive_error
= NT_STATUS_OK
,
1702 .expected_network_error
= NT_STATUS_OK
1705 .comment
= "user@realm",
1707 .username
= talloc_asprintf(mem_ctx
,
1709 cli_credentials_get_username(cmdline_credentials
),
1710 cli_credentials_get_realm(cmdline_credentials
)
1712 .password
= cli_credentials_get_password(cmdline_credentials
),
1713 .network_login
= true,
1714 .expected_interactive_error
= NT_STATUS_OK
,
1715 .expected_network_error
= NT_STATUS_OK
1718 .comment
= "machine domain\\user",
1719 .domain
= cli_credentials_get_domain(machine_credentials
),
1720 .username
= cli_credentials_get_username(machine_credentials
),
1721 .password
= cli_credentials_get_password(machine_credentials
),
1722 .network_login
= true,
1723 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1724 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1727 .comment
= "machine domain\\user",
1728 .domain
= cli_credentials_get_domain(machine_credentials
),
1729 .username
= cli_credentials_get_username(machine_credentials
),
1730 .password
= cli_credentials_get_password(machine_credentials
),
1731 .network_login
= true,
1732 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1733 .expected_network_error
= NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1736 .comment
= "machine realm\\user",
1737 .domain
= cli_credentials_get_realm(machine_credentials
),
1738 .username
= cli_credentials_get_username(machine_credentials
),
1739 .password
= cli_credentials_get_password(machine_credentials
),
1740 .network_login
= true,
1741 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1742 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1745 .comment
= "machine user@domain",
1747 .username
= talloc_asprintf(mem_ctx
,
1749 cli_credentials_get_username(machine_credentials
),
1750 cli_credentials_get_domain(machine_credentials
)
1752 .password
= cli_credentials_get_password(machine_credentials
),
1753 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1754 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1755 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1758 .comment
= "machine user@realm",
1760 .username
= talloc_asprintf(mem_ctx
,
1762 cli_credentials_get_username(machine_credentials
),
1763 cli_credentials_get_realm(machine_credentials
)
1765 .password
= cli_credentials_get_password(machine_credentials
),
1766 .network_login
= true,
1767 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1768 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1771 .comment
= "test user (long pw): domain\\user",
1772 .domain
= userdomain
,
1773 .username
= TEST_USER_NAME
,
1774 .password
= user_password
,
1775 .network_login
= true,
1776 .expected_interactive_error
= NT_STATUS_OK
,
1777 .expected_network_error
= NT_STATUS_OK
1780 .comment
= "test user (long pw): user@realm",
1782 .username
= talloc_asprintf(mem_ctx
,
1785 lp_realm(torture
->lp_ctx
)),
1786 .password
= user_password
,
1787 .network_login
= true,
1788 .expected_interactive_error
= NT_STATUS_OK
,
1789 .expected_network_error
= NT_STATUS_OK
1792 .comment
= "test user (long pw): user@domain",
1794 .username
= talloc_asprintf(mem_ctx
,
1798 .password
= user_password
,
1799 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1800 .expected_interactive_error
= NT_STATUS_OK
,
1801 .expected_network_error
= NT_STATUS_OK
1803 /* Oddball, can we use the old password ? */
1805 .comment
= "test user: user\\domain OLD PASSWORD",
1806 .domain
= userdomain
,
1807 .username
= TEST_USER_NAME
,
1808 .password
= old_user_password
,
1809 .network_login
= true,
1810 .expected_interactive_error
= NT_STATUS_WRONG_PASSWORD
,
1811 .expected_network_error
= NT_STATUS_OK
,
1812 .old_password
= true
1815 .comment
= "test user (wong workstation): domain\\user",
1816 .domain
= userdomain
,
1817 .username
= TEST_USER_NAME_WRONG_WKS
,
1818 .password
= user_password_wrong_wks
,
1819 .network_login
= true,
1820 .expected_interactive_error
= NT_STATUS_INVALID_WORKSTATION
,
1821 .expected_network_error
= NT_STATUS_INVALID_WORKSTATION
1825 /* Try all the tests for different username forms */
1826 for (ci
= 0; ci
< ARRAY_SIZE(usercreds
); ci
++) {
1828 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1829 usercreds
[ci
].comment
,
1831 usercreds
[ci
].domain
,
1832 usercreds
[ci
].username
,
1833 usercreds
[ci
].password
,
1834 usercreds
[ci
].parameter_control
,
1835 usercreds
[ci
].expected_interactive_error
)) {
1839 if (usercreds
[ci
].network_login
) {
1840 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1841 usercreds
[ci
].comment
,
1842 usercreds
[ci
].domain
,
1843 usercreds
[ci
].username
,
1844 usercreds
[ci
].password
,
1845 usercreds
[ci
].parameter_control
,
1846 usercreds
[ci
].expected_network_error
,
1847 usercreds
[ci
].old_password
,
1854 /* Using the first username form, try the different
1855 * credentials flag setups, on only one of the tests (checks
1856 * session key encryption) */
1858 for (i
=0; i
< ARRAY_SIZE(credential_flags
); i
++) {
1859 /* TODO: Somehow we lost setting up the different credential flags here! */
1861 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1862 usercreds
[0].comment
,
1864 usercreds
[0].domain
,
1865 usercreds
[0].username
,
1866 usercreds
[0].password
,
1867 usercreds
[0].parameter_control
,
1868 usercreds
[0].expected_interactive_error
)) {
1872 if (usercreds
[0].network_login
) {
1873 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1874 usercreds
[0].comment
,
1875 usercreds
[0].domain
,
1876 usercreds
[0].username
,
1877 usercreds
[0].password
,
1878 usercreds
[0].parameter_control
,
1879 usercreds
[0].expected_network_error
,
1880 usercreds
[0].old_password
,
1889 talloc_free(mem_ctx
);
1891 torture_leave_domain(torture
, join_ctx
);
1892 torture_leave_domain(torture
, user_ctx
);
1893 torture_leave_domain(torture
, user_ctx_wrong_wks
);
1894 torture_leave_domain(torture
, user_ctx_wrong_time
);