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(samlogon_state
->p
, samlogon_state
->mem_ctx
, r
);
158 if (!r
->out
.return_authenticator
||
159 !netlogon_creds_client_check(samlogon_state
->creds
, &r
->out
.return_authenticator
->cred
)) {
160 d_printf("Credential chaining failed\n");
162 if (!NT_STATUS_IS_OK(status
)) {
164 *error_string
= strdup(nt_errstr(status
));
169 validation_level
= r
->in
.validation_level
;
171 netlogon_creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r
->out
.validation
);
173 switch (validation_level
) {
175 base
= &r
->out
.validation
->sam2
->base
;
178 base
= &r
->out
.validation
->sam3
->base
;
181 base
= &r
->out
.validation
->sam6
->base
;
185 case NDR_NETR_LOGONSAMLOGONEX
:
186 status
= dcerpc_netr_LogonSamLogonEx(samlogon_state
->p
, samlogon_state
->mem_ctx
, r_ex
);
187 if (!NT_STATUS_IS_OK(status
)) {
189 *error_string
= strdup(nt_errstr(status
));
194 validation_level
= r_ex
->in
.validation_level
;
196 netlogon_creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r_ex
->out
.validation
);
198 switch (validation_level
) {
200 base
= &r_ex
->out
.validation
->sam2
->base
;
203 base
= &r_ex
->out
.validation
->sam3
->base
;
206 base
= &r_ex
->out
.validation
->sam6
->base
;
210 case NDR_NETR_LOGONSAMLOGONWITHFLAGS
:
211 ZERO_STRUCT(samlogon_state
->auth2
);
212 netlogon_creds_client_authenticator(samlogon_state
->creds
, &samlogon_state
->auth
);
214 r_flags
->out
.return_authenticator
= NULL
;
215 status
= dcerpc_netr_LogonSamLogonWithFlags(samlogon_state
->p
, samlogon_state
->mem_ctx
, r_flags
);
216 if (!r_flags
->out
.return_authenticator
||
217 !netlogon_creds_client_check(samlogon_state
->creds
, &r_flags
->out
.return_authenticator
->cred
)) {
218 d_printf("Credential chaining failed\n");
220 if (!NT_STATUS_IS_OK(status
)) {
222 *error_string
= strdup(nt_errstr(status
));
227 validation_level
= r_flags
->in
.validation_level
;
229 netlogon_creds_decrypt_samlogon(samlogon_state
->creds
, validation_level
, r_flags
->out
.validation
);
231 switch (validation_level
) {
233 base
= &r_flags
->out
.validation
->sam2
->base
;
236 base
= &r_flags
->out
.validation
->sam3
->base
;
239 base
= &r_flags
->out
.validation
->sam6
->base
;
245 return NT_STATUS_INVALID_PARAMETER
;
249 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
250 return NT_STATUS_INVALID_PARAMETER
;
253 if (user_session_key
) {
254 memcpy(user_session_key
, base
->key
.key
, 16);
257 memcpy(lm_key
, base
->LMSessKey
.key
, 8);
265 * Test the normal 'LM and NTLM' combination
268 static bool test_lm_ntlm_broken(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
273 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
274 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
275 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
278 uint8_t user_session_key
[16];
283 ZERO_STRUCT(user_session_key
);
285 lm_good
= SMBencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, lm_response
.data
);
287 ZERO_STRUCT(lm_hash
);
289 E_deshash(samlogon_state
->password
, lm_hash
);
292 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
294 E_md4hash(samlogon_state
->password
, nt_hash
);
295 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
297 nt_status
= check_samlogon(samlogon_state
,
299 samlogon_state
->parameter_control
,
300 &samlogon_state
->chall
,
307 data_blob_free(&lm_response
);
309 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
310 /* for 'long' passwords, the LM password is invalid */
311 if (break_which
== NO_NT
&& !lm_good
) {
314 /* for 'old' passwords, we allow the server to be OK or wrong password */
315 if (samlogon_state
->old_password
) {
318 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
319 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
320 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
321 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
322 SAFE_FREE(*error_string
);
323 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
325 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
327 } else if (!NT_STATUS_IS_OK(nt_status
)) {
331 if (break_which
== NO_NT
&& !lm_good
) {
332 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
336 if (memcmp(lm_hash
, lm_key
,
337 sizeof(lm_key
)) != 0) {
338 d_printf("LM Key does not match expectations!\n");
339 d_printf("lm_key:\n");
340 dump_data(1, lm_key
, 8);
341 d_printf("expected:\n");
342 dump_data(1, lm_hash
, 8);
346 switch (break_which
) {
349 uint8_t lm_key_expected
[16];
350 memcpy(lm_key_expected
, lm_hash
, 8);
351 memset(lm_key_expected
+8, '\0', 8);
352 if (memcmp(lm_key_expected
, user_session_key
,
354 *error_string
= strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
355 d_printf("user_session_key:\n");
356 dump_data(1, user_session_key
, sizeof(user_session_key
));
357 d_printf("expected:\n");
358 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
364 if (memcmp(session_key
.data
, user_session_key
,
365 sizeof(user_session_key
)) != 0) {
366 *error_string
= strdup("NT Session Key does not match expectations!\n");
367 d_printf("user_session_key:\n");
368 dump_data(1, user_session_key
, 16);
369 d_printf("expected:\n");
370 dump_data(1, session_key
.data
, session_key
.length
);
378 * Test LM authentication, no NT response supplied
381 static bool test_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
384 return test_lm_ntlm_broken(samlogon_state
, NO_NT
, error_string
);
388 * Test the NTLM response only, no LM.
391 static bool test_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
393 return test_lm_ntlm_broken(samlogon_state
, NO_LM
, error_string
);
397 * Test the NTLM response only, but in the LM field.
400 static bool test_ntlm_in_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
405 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
406 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
410 uint8_t user_session_key
[16];
414 ZERO_STRUCT(user_session_key
);
416 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
418 E_md4hash(samlogon_state
->password
, nt_hash
);
419 SMBsesskeygen_ntv1(nt_hash
,
422 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
424 ZERO_STRUCT(lm_hash
);
426 nt_status
= check_samlogon(samlogon_state
,
428 samlogon_state
->parameter_control
,
429 &samlogon_state
->chall
,
436 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
437 /* for 'old' passwords, we allow the server to be OK or wrong password */
438 if (samlogon_state
->old_password
) {
442 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
443 SAFE_FREE(*error_string
);
444 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
446 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
448 } else if (!NT_STATUS_IS_OK(nt_status
)) {
453 if (memcmp(lm_hash
, lm_key
,
454 sizeof(lm_key
)) != 0) {
455 d_printf("LM Key does not match expectations!\n");
456 d_printf("lm_key:\n");
457 dump_data(1, lm_key
, 8);
458 d_printf("expected:\n");
459 dump_data(1, lm_hash
, 8);
464 if (memcmp(session_key
.data
, lm_key
,
465 sizeof(lm_key
)) != 0) {
466 d_printf("LM Key does not match expectations (first 8 session key)!\n");
467 d_printf("lm_key:\n");
468 dump_data(1, lm_key
, 8);
469 d_printf("expected:\n");
470 dump_data(1, session_key
.data
, 8);
475 if (lm_good
&& memcmp(lm_hash
, user_session_key
, 8) != 0) {
476 uint8_t lm_key_expected
[16];
477 memcpy(lm_key_expected
, lm_hash
, 8);
478 memset(lm_key_expected
+8, '\0', 8);
479 if (memcmp(lm_key_expected
, user_session_key
,
481 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
482 d_printf("user_session_key:\n");
483 dump_data(1, user_session_key
, sizeof(user_session_key
));
484 d_printf("expected:\n");
485 dump_data(1, lm_key_expected
, sizeof(lm_key_expected
));
493 * Test the NTLM response only, but in the both the NT and LM fields.
496 static bool test_ntlm_in_both(struct samlogon_state
*samlogon_state
, char **error_string
)
501 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
502 DATA_BLOB session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
506 uint8_t user_session_key
[16];
510 ZERO_STRUCT(user_session_key
);
512 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
514 E_md4hash(samlogon_state
->password
, nt_hash
);
515 SMBsesskeygen_ntv1(nt_hash
,
518 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
520 ZERO_STRUCT(lm_hash
);
523 nt_status
= check_samlogon(samlogon_state
,
525 samlogon_state
->parameter_control
,
526 &samlogon_state
->chall
,
533 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
534 /* for 'old' passwords, we allow the server to be OK or wrong password */
535 if (samlogon_state
->old_password
) {
539 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
540 SAFE_FREE(*error_string
);
541 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
543 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
545 } else if (!NT_STATUS_IS_OK(nt_status
)) {
549 if (!NT_STATUS_IS_OK(nt_status
)) {
553 if (memcmp(lm_hash
, lm_key
,
554 sizeof(lm_key
)) != 0) {
555 d_printf("LM Key does not match expectations!\n");
556 d_printf("lm_key:\n");
557 dump_data(1, lm_key
, 8);
558 d_printf("expected:\n");
559 dump_data(1, lm_hash
, 8);
562 if (memcmp(session_key
.data
, user_session_key
,
563 sizeof(user_session_key
)) != 0) {
564 d_printf("NT Session Key does not match expectations!\n");
565 d_printf("user_session_key:\n");
566 dump_data(1, user_session_key
, 16);
567 d_printf("expected:\n");
568 dump_data(1, session_key
.data
, session_key
.length
);
577 * Test the NTLMv2 and LMv2 responses
585 static bool test_lmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
,
586 enum ntlm_break break_which
,
587 enum ntlmv2_domain ntlmv2_domain
,
592 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
593 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
594 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
595 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
596 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, TEST_MACHINE_NAME
, samlogon_state
->workgroup
);
598 uint8_t lm_session_key
[8];
599 uint8_t user_session_key
[16];
601 ZERO_STRUCT(lm_session_key
);
602 ZERO_STRUCT(user_session_key
);
604 switch (ntlmv2_domain
) {
606 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
607 samlogon_state
->account_name
, samlogon_state
->account_domain
,
608 samlogon_state
->password
, &samlogon_state
->chall
,
610 &lmv2_response
, &ntlmv2_response
,
611 &lmv2_session_key
, &ntlmv2_session_key
)) {
612 data_blob_free(&names_blob
);
617 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
618 samlogon_state
->account_name
, "",
619 samlogon_state
->password
, &samlogon_state
->chall
,
621 &lmv2_response
, &ntlmv2_response
,
622 &lmv2_session_key
, &ntlmv2_session_key
)) {
623 data_blob_free(&names_blob
);
628 data_blob_free(&names_blob
);
630 nt_status
= check_samlogon(samlogon_state
,
632 samlogon_state
->parameter_control
,
633 &samlogon_state
->chall
,
640 data_blob_free(&lmv2_response
);
641 data_blob_free(&ntlmv2_response
);
644 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
645 /* for 'old' passwords, we allow the server to be OK or wrong password */
646 if (samlogon_state
->old_password
) {
649 return break_which
== BREAK_BOTH
;
650 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
651 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
652 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
653 SAFE_FREE(*error_string
);
654 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
656 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
658 } else if (!NT_STATUS_IS_OK(nt_status
)) {
663 switch (break_which
) {
665 if (memcmp(lmv2_session_key
.data
, user_session_key
,
666 sizeof(user_session_key
)) != 0) {
667 d_printf("USER (LMv2) Session Key does not match expectations!\n");
668 d_printf("user_session_key:\n");
669 dump_data(1, user_session_key
, 16);
670 d_printf("expected:\n");
671 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
674 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
675 sizeof(lm_session_key
)) != 0) {
676 d_printf("LM (LMv2) Session Key does not match expectations!\n");
677 d_printf("lm_session_key:\n");
678 dump_data(1, lm_session_key
, 8);
679 d_printf("expected:\n");
680 dump_data(1, lmv2_session_key
.data
, 8);
685 if (memcmp(ntlmv2_session_key
.data
, user_session_key
,
686 sizeof(user_session_key
)) != 0) {
687 if (memcmp(lmv2_session_key
.data
, user_session_key
,
688 sizeof(user_session_key
)) == 0) {
689 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
690 d_printf("user_session_key:\n");
691 dump_data(1, user_session_key
, 16);
692 d_printf("expected:\n");
693 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
697 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
698 d_printf("user_session_key:\n");
699 dump_data(1, user_session_key
, 16);
700 d_printf("expected:\n");
701 dump_data(1, ntlmv2_session_key
.data
, ntlmv2_session_key
.length
);
705 if (memcmp(ntlmv2_session_key
.data
, lm_session_key
,
706 sizeof(lm_session_key
)) != 0) {
707 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
708 sizeof(lm_session_key
)) == 0) {
709 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
710 d_printf("user_session_key:\n");
711 dump_data(1, lm_session_key
, 8);
712 d_printf("expected:\n");
713 dump_data(1, ntlmv2_session_key
.data
, 8);
716 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
717 d_printf("lm_session_key:\n");
718 dump_data(1, lm_session_key
, 8);
719 d_printf("expected:\n");
720 dump_data(1, ntlmv2_session_key
.data
, 8);
730 * Test the NTLM and LMv2 responses
733 static bool test_lmv2_ntlm_broken(struct samlogon_state
*samlogon_state
,
734 enum ntlm_break break_which
,
735 enum ntlmv2_domain ntlmv2_domain
,
740 DATA_BLOB ntlmv2_response
= data_blob(NULL
, 0);
741 DATA_BLOB lmv2_response
= data_blob(NULL
, 0);
742 DATA_BLOB lmv2_session_key
= data_blob(NULL
, 0);
743 DATA_BLOB ntlmv2_session_key
= data_blob(NULL
, 0);
744 DATA_BLOB names_blob
= NTLMv2_generate_names_blob(samlogon_state
->mem_ctx
, samlogon_state
->netbios_name
, samlogon_state
->workgroup
);
746 DATA_BLOB ntlm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
747 DATA_BLOB ntlm_session_key
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 16);
751 uint8_t lm_session_key
[8];
752 uint8_t user_session_key
[16];
755 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
,
757 E_md4hash(samlogon_state
->password
, nt_hash
);
758 SMBsesskeygen_ntv1(nt_hash
,
759 ntlm_session_key
.data
);
761 lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
763 ZERO_STRUCT(lm_hash
);
766 ZERO_STRUCT(lm_session_key
);
767 ZERO_STRUCT(user_session_key
);
769 switch (ntlmv2_domain
) {
771 /* TODO - test with various domain cases, and without domain */
772 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
773 samlogon_state
->account_name
, samlogon_state
->account_domain
,
774 samlogon_state
->password
, &samlogon_state
->chall
,
776 &lmv2_response
, &ntlmv2_response
,
777 &lmv2_session_key
, &ntlmv2_session_key
)) {
778 data_blob_free(&names_blob
);
783 /* TODO - test with various domain cases, and without domain */
784 if (!SMBNTLMv2encrypt(samlogon_state
->mem_ctx
,
785 samlogon_state
->account_name
, "",
786 samlogon_state
->password
, &samlogon_state
->chall
,
788 &lmv2_response
, &ntlmv2_response
,
789 &lmv2_session_key
, &ntlmv2_session_key
)) {
790 data_blob_free(&names_blob
);
796 data_blob_free(&names_blob
);
798 nt_status
= check_samlogon(samlogon_state
,
800 samlogon_state
->parameter_control
,
801 &samlogon_state
->chall
,
808 data_blob_free(&lmv2_response
);
809 data_blob_free(&ntlmv2_response
);
812 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
813 /* for 'old' passwords, we allow the server to be OK or wrong password */
814 if (samlogon_state
->old_password
) {
817 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
818 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
819 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
820 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
821 SAFE_FREE(*error_string
);
822 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
824 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
826 } else if (!NT_STATUS_IS_OK(nt_status
)) {
830 switch (break_which
) {
832 if (memcmp(lmv2_session_key
.data
, user_session_key
,
833 sizeof(user_session_key
)) != 0) {
834 d_printf("USER (LMv2) Session Key does not match expectations!\n");
835 d_printf("user_session_key:\n");
836 dump_data(1, user_session_key
, 16);
837 d_printf("expected:\n");
838 dump_data(1, lmv2_session_key
.data
, ntlmv2_session_key
.length
);
841 if (memcmp(lmv2_session_key
.data
, lm_session_key
,
842 sizeof(lm_session_key
)) != 0) {
843 d_printf("LM (LMv2) Session Key does not match expectations!\n");
844 d_printf("lm_session_key:\n");
845 dump_data(1, lm_session_key
, 8);
846 d_printf("expected:\n");
847 dump_data(1, lmv2_session_key
.data
, 8);
852 if (memcmp(ntlm_session_key
.data
, user_session_key
,
853 sizeof(user_session_key
)) != 0) {
854 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
855 d_printf("user_session_key:\n");
856 dump_data(1, user_session_key
, 16);
857 d_printf("expected:\n");
858 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
862 if (memcmp(lm_hash
, lm_session_key
,
863 sizeof(lm_session_key
)) != 0) {
864 d_printf("LM Session Key does not match expectations!\n");
865 d_printf("lm_session_key:\n");
866 dump_data(1, lm_session_key
, 8);
867 d_printf("expected:\n");
868 dump_data(1, lm_hash
, 8);
872 static const uint8_t zeros
[8];
873 if (memcmp(zeros
, lm_session_key
,
874 sizeof(lm_session_key
)) != 0) {
875 d_printf("LM Session Key does not match expectations (zeros)!\n");
876 d_printf("lm_session_key:\n");
877 dump_data(1, lm_session_key
, 8);
878 d_printf("expected:\n");
879 dump_data(1, zeros
, 8);
885 if (memcmp(ntlm_session_key
.data
, user_session_key
,
886 sizeof(user_session_key
)) != 0) {
887 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
888 d_printf("user_session_key:\n");
889 dump_data(1, user_session_key
, 16);
890 d_printf("expected:\n");
891 dump_data(1, ntlm_session_key
.data
, ntlm_session_key
.length
);
894 if (memcmp(ntlm_session_key
.data
, lm_session_key
,
895 sizeof(lm_session_key
)) != 0) {
896 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
897 d_printf("lm_session_key:\n");
898 dump_data(1, lm_session_key
, 8);
899 d_printf("expected:\n");
900 dump_data(1, ntlm_session_key
.data
, 8);
909 * Test the NTLMv2 and LMv2 responses
912 static bool test_lmv2_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
914 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, UPPER_DOMAIN
, error_string
);
918 static bool test_lmv2_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
920 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NONE
, NO_DOMAIN
, error_string
);
925 * Test the LMv2 response only
928 static bool test_lmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
930 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, UPPER_DOMAIN
, error_string
);
933 static bool test_lmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
935 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_NT
, NO_DOMAIN
, error_string
);
939 * Test the NTLMv2 response only
942 static bool test_ntlmv2(struct samlogon_state
*samlogon_state
, char **error_string
)
944 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, UPPER_DOMAIN
, error_string
);
947 static bool test_ntlmv2_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
949 return test_lmv2_ntlmv2_broken(samlogon_state
, NO_LM
, NO_DOMAIN
, error_string
);
952 static bool test_lm_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
954 return test_lm_ntlm_broken(samlogon_state
, BREAK_NONE
, error_string
);
957 static bool test_ntlm_lm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
959 return test_lm_ntlm_broken(samlogon_state
, BREAK_LM
, error_string
);
962 static bool test_ntlm_ntlm_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
964 return test_lm_ntlm_broken(samlogon_state
, BREAK_NT
, error_string
);
967 static bool test_lm_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
969 return test_lm_ntlm_broken(samlogon_state
, BREAK_BOTH
, error_string
);
971 static bool test_ntlmv2_lmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
973 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
976 static bool test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
978 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
981 static bool test_ntlmv2_ntlmv2_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
983 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
987 static bool test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
989 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
993 static bool test_ntlmv2_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
995 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
998 static bool test_ntlmv2_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1000 return test_lmv2_ntlmv2_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1003 static bool test_lmv2_ntlm_both_broken(struct samlogon_state
*samlogon_state
, char **error_string
)
1005 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, UPPER_DOMAIN
, error_string
);
1008 static bool test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1010 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_BOTH
, NO_DOMAIN
, error_string
);
1013 static bool test_lmv2_ntlm_break_ntlm(struct samlogon_state
*samlogon_state
, char **error_string
)
1015 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, UPPER_DOMAIN
, error_string
);
1018 static bool test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1020 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_NT
, NO_DOMAIN
, error_string
);
1023 static bool test_lmv2_ntlm_break_lm(struct samlogon_state
*samlogon_state
, char **error_string
)
1025 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, UPPER_DOMAIN
, error_string
);
1028 static bool test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state
*samlogon_state
, char **error_string
)
1030 return test_lmv2_ntlm_broken(samlogon_state
, BREAK_LM
, NO_DOMAIN
, error_string
);
1034 * Test the NTLM2 response (extra challenge in LM feild)
1036 * This test is the same as the 'break LM' test, but checks that the
1037 * server implements NTLM2 session security in the right place
1038 * (NETLOGON is the wrong place).
1041 static bool test_ntlm2(struct samlogon_state
*samlogon_state
, char **error_string
)
1045 DATA_BLOB lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1046 DATA_BLOB nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, NULL
, 24);
1050 uint8_t nt_hash
[16];
1051 uint8_t lm_hash
[16];
1053 uint8_t user_session_key
[16];
1054 uint8_t expected_user_session_key
[16];
1055 uint8_t session_nonce_hash
[16];
1056 uint8_t client_chall
[8];
1058 struct MD5Context md5_session_nonce_ctx
;
1059 HMACMD5Context hmac_ctx
;
1061 ZERO_STRUCT(user_session_key
);
1062 ZERO_STRUCT(lm_key
);
1063 generate_random_buffer(client_chall
, 8);
1065 MD5Init(&md5_session_nonce_ctx
);
1066 MD5Update(&md5_session_nonce_ctx
, samlogon_state
->chall
.data
, 8);
1067 MD5Update(&md5_session_nonce_ctx
, client_chall
, 8);
1068 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
1070 E_md4hash(samlogon_state
->password
, (uint8_t *)nt_hash
);
1071 lm_good
= E_deshash(samlogon_state
->password
, (uint8_t *)lm_hash
);
1072 SMBsesskeygen_ntv1((const uint8_t *)nt_hash
,
1075 SMBNTencrypt(samlogon_state
->password
, samlogon_state
->chall
.data
, nt_response
.data
);
1077 memcpy(lm_response
.data
, session_nonce_hash
, 8);
1078 memset(lm_response
.data
+ 8, 0, 16);
1080 hmac_md5_init_rfc2104(nt_key
, 16, &hmac_ctx
);
1081 hmac_md5_update(samlogon_state
->chall
.data
, 8, &hmac_ctx
);
1082 hmac_md5_update(client_chall
, 8, &hmac_ctx
);
1083 hmac_md5_final(expected_user_session_key
, &hmac_ctx
);
1085 nt_status
= check_samlogon(samlogon_state
,
1087 samlogon_state
->parameter_control
,
1088 &samlogon_state
->chall
,
1095 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1096 /* for 'old' passwords, we allow the server to be OK or wrong password */
1097 if (samlogon_state
->old_password
) {
1101 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1102 SAFE_FREE(*error_string
);
1103 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1105 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1107 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1112 if (memcmp(lm_hash
, lm_key
,
1113 sizeof(lm_key
)) != 0) {
1114 d_printf("LM Key does not match expectations!\n");
1115 d_printf("lm_key:\n");
1116 dump_data(1, lm_key
, 8);
1117 d_printf("expected:\n");
1118 dump_data(1, lm_hash
, 8);
1122 static const uint8_t zeros
[8];
1123 if (memcmp(zeros
, lm_key
,
1124 sizeof(lm_key
)) != 0) {
1125 d_printf("LM Session Key does not match expectations (zeros)!\n");
1126 d_printf("lm_key:\n");
1127 dump_data(1, lm_key
, 8);
1128 d_printf("expected:\n");
1129 dump_data(1, zeros
, 8);
1133 if (memcmp(nt_key
, user_session_key
, 16) != 0) {
1134 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1135 d_printf("user_session_key:\n");
1136 dump_data(1, user_session_key
, sizeof(user_session_key
));
1137 d_printf("expected:\n");
1138 dump_data(1, nt_key
, sizeof(nt_key
));
1144 static bool test_plaintext(struct samlogon_state
*samlogon_state
, enum ntlm_break break_which
, char **error_string
)
1147 DATA_BLOB nt_response
= data_blob(NULL
, 0);
1148 DATA_BLOB lm_response
= data_blob(NULL
, 0);
1151 smb_ucs2_t
*unicodepw
;
1153 uint8_t user_session_key
[16];
1155 uint8_t lm_hash
[16];
1156 static const uint8_t zeros
[8];
1157 DATA_BLOB chall
= data_blob_talloc(samlogon_state
->mem_ctx
, zeros
, sizeof(zeros
));
1158 bool lm_good
= E_deshash(samlogon_state
->password
, lm_hash
);
1160 ZERO_STRUCT(user_session_key
);
1162 if (!push_ucs2_talloc(samlogon_state
->mem_ctx
,
1163 &unicodepw
, samlogon_state
->password
, NULL
)) {
1164 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1168 nt_response
= data_blob_talloc(samlogon_state
->mem_ctx
, unicodepw
, strlen_m(samlogon_state
->password
)*2);
1170 password
= strupper_talloc(samlogon_state
->mem_ctx
, samlogon_state
->password
);
1172 if (!convert_string_talloc_convenience(samlogon_state
->mem_ctx
,
1173 samlogon_state
->iconv_convenience
,
1175 password
, strlen(password
)+1,
1176 (void**)&dospw
, NULL
, false)) {
1177 DEBUG(0, ("convert_string_talloc failed!\n"));
1181 lm_response
= data_blob_talloc(samlogon_state
->mem_ctx
, dospw
, strlen(dospw
));
1183 nt_status
= check_samlogon(samlogon_state
,
1185 samlogon_state
->parameter_control
| MSV1_0_CLEARTEXT_PASSWORD_ALLOWED
,
1193 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD
, nt_status
)) {
1194 /* for 'old' passwords, we allow the server to be OK or wrong password */
1195 if (samlogon_state
->old_password
) {
1198 /* for 'long' passwords, the LM password is invalid */
1199 if (break_which
== NO_NT
&& !lm_good
) {
1202 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
));
1203 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND
, nt_status
) && strchr_m(samlogon_state
->account_name
, '@')) {
1204 return ((break_which
== BREAK_NT
) || (break_which
== BREAK_BOTH
) || (break_which
== NO_NT
));
1205 } else if (!NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
)) {
1206 SAFE_FREE(*error_string
);
1207 asprintf(error_string
, "Expected error: %s, got %s", nt_errstr(samlogon_state
->expected_error
), nt_errstr(nt_status
));
1209 } else if (NT_STATUS_EQUAL(samlogon_state
->expected_error
, nt_status
) && !NT_STATUS_IS_OK(nt_status
)) {
1211 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1215 if (break_which
== NO_NT
&& !lm_good
) {
1216 *error_string
= strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1223 static bool test_plaintext_none_broken(struct samlogon_state
*samlogon_state
,
1224 char **error_string
) {
1225 return test_plaintext(samlogon_state
, BREAK_NONE
, error_string
);
1228 static bool test_plaintext_lm_broken(struct samlogon_state
*samlogon_state
,
1229 char **error_string
) {
1230 return test_plaintext(samlogon_state
, BREAK_LM
, error_string
);
1233 static bool test_plaintext_nt_broken(struct samlogon_state
*samlogon_state
,
1234 char **error_string
) {
1235 return test_plaintext(samlogon_state
, BREAK_NT
, error_string
);
1238 static bool test_plaintext_nt_only(struct samlogon_state
*samlogon_state
,
1239 char **error_string
) {
1240 return test_plaintext(samlogon_state
, NO_LM
, error_string
);
1243 static bool test_plaintext_lm_only(struct samlogon_state
*samlogon_state
,
1244 char **error_string
) {
1245 return test_plaintext(samlogon_state
, NO_NT
, error_string
);
1259 - plaintext tests (in challenge-response fields)
1261 check we get the correct session key in each case
1262 check what values we get for the LM session key
1266 static const struct ntlm_tests
{
1267 bool (*fn
)(struct samlogon_state
*, char **);
1271 {test_lmv2_ntlmv2
, "NTLMv2 and LMv2", false},
1273 {test_lmv2_ntlmv2_no_dom
, "NTLMv2 and LMv2 (no domain)", false},
1275 {test_lm
, "LM", false},
1276 {test_lm_ntlm
, "LM and NTLM", false},
1277 {test_lm_ntlm_both_broken
, "LM and NTLM, both broken", false},
1278 {test_ntlm
, "NTLM", false},
1279 {test_ntlm_in_lm
, "NTLM in LM", false},
1280 {test_ntlm_in_both
, "NTLM in both", false},
1281 {test_ntlmv2
, "NTLMv2", false},
1282 {test_ntlmv2_no_dom
, "NTLMv2 (no domain)", false},
1283 {test_lmv2
, "LMv2", false},
1284 {test_lmv2_no_dom
, "LMv2 (no domain)", false},
1285 {test_ntlmv2_lmv2_broken
, "NTLMv2 and LMv2, LMv2 broken", false},
1286 {test_ntlmv2_lmv2_broken_no_dom
, "NTLMv2 and LMv2, LMv2 broken (no domain)", false},
1287 {test_ntlmv2_ntlmv2_broken
, "NTLMv2 and LMv2, NTLMv2 broken", false},
1289 {test_ntlmv2_ntlmv2_broken_no_dom
, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", false},
1291 {test_ntlmv2_both_broken
, "NTLMv2 and LMv2, both broken", false},
1292 {test_ntlmv2_both_broken_no_dom
, "NTLMv2 and LMv2, both broken (no domain)", false},
1293 {test_ntlm_lm_broken
, "NTLM and LM, LM broken", false},
1294 {test_ntlm_ntlm_broken
, "NTLM and LM, NTLM broken", false},
1295 {test_ntlm2
, "NTLM2 (NTLMv2 session security)", false},
1296 {test_lmv2_ntlm_both_broken
, "LMv2 and NTLM, both broken", false},
1297 {test_lmv2_ntlm_both_broken_no_dom
, "LMv2 and NTLM, both broken (no domain)", false},
1298 {test_lmv2_ntlm_break_ntlm
, "LMv2 and NTLM, NTLM broken", false},
1299 {test_lmv2_ntlm_break_ntlm_no_dom
, "LMv2 and NTLM, NTLM broken (no domain)", false},
1300 {test_lmv2_ntlm_break_lm
, "LMv2 and NTLM, LMv2 broken", false},
1301 {test_lmv2_ntlm_break_lm_no_dom
, "LMv2 and NTLM, LMv2 broken (no domain)", false},
1302 {test_plaintext_none_broken
, "Plaintext", false},
1303 {test_plaintext_lm_broken
, "Plaintext LM broken", false},
1304 {test_plaintext_nt_broken
, "Plaintext NT broken", false},
1305 {test_plaintext_nt_only
, "Plaintext NT only", false},
1306 {test_plaintext_lm_only
, "Plaintext LM only", false},
1311 try a netlogon SamLogon
1313 static bool test_SamLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1314 struct torture_context
*tctx
,
1315 struct netlogon_creds_CredentialState
*creds
,
1316 const char *comment
,
1317 const char *account_domain
, const char *account_name
,
1318 const char *plain_pass
, uint32_t parameter_control
,
1319 NTSTATUS expected_error
, bool old_password
,
1322 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_SamLogon function-level context");
1325 int validation_levels
[] = {2,3,6};
1326 int logon_levels
[] = { 2, 6 };
1327 int function_levels
[] = {
1328 NDR_NETR_LOGONSAMLOGON
,
1329 NDR_NETR_LOGONSAMLOGONEX
,
1330 NDR_NETR_LOGONSAMLOGONWITHFLAGS
};
1331 struct samlogon_state samlogon_state
;
1333 union netr_LogonLevel logon
;
1334 union netr_Validation validation
;
1335 uint8_t authoritative
= 0;
1340 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1342 samlogon_state
.comment
= comment
;
1343 samlogon_state
.account_name
= account_name
;
1344 samlogon_state
.account_domain
= account_domain
;
1345 samlogon_state
.password
= plain_pass
;
1346 samlogon_state
.workgroup
= lp_workgroup(tctx
->lp_ctx
);
1347 samlogon_state
.netbios_name
= lp_netbios_name(tctx
->lp_ctx
);
1348 samlogon_state
.p
= p
;
1349 samlogon_state
.creds
= creds
;
1350 samlogon_state
.expected_error
= expected_error
;
1351 samlogon_state
.chall
= data_blob_talloc(fn_ctx
, NULL
, 8);
1352 samlogon_state
.parameter_control
= parameter_control
;
1353 samlogon_state
.old_password
= old_password
;
1354 samlogon_state
.iconv_convenience
= lp_iconv_convenience(tctx
->lp_ctx
);
1356 generate_random_buffer(samlogon_state
.chall
.data
, 8);
1357 samlogon_state
.r_flags
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1358 samlogon_state
.r_flags
.in
.computer_name
= TEST_MACHINE_NAME
;
1359 samlogon_state
.r_flags
.in
.credential
= &samlogon_state
.auth
;
1360 samlogon_state
.r_flags
.in
.return_authenticator
= &samlogon_state
.auth2
;
1361 samlogon_state
.r_flags
.in
.flags
= &flags
;
1362 samlogon_state
.r_flags
.in
.logon
= &logon
;
1363 samlogon_state
.r_flags
.out
.validation
= &validation
;
1364 samlogon_state
.r_flags
.out
.authoritative
= &authoritative
;
1365 samlogon_state
.r_flags
.out
.flags
= &flags
;
1367 samlogon_state
.r_ex
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1368 samlogon_state
.r_ex
.in
.computer_name
= TEST_MACHINE_NAME
;
1369 samlogon_state
.r_ex
.in
.flags
= &flags
;
1370 samlogon_state
.r_ex
.in
.logon
= &logon
;
1371 samlogon_state
.r_ex
.out
.validation
= &validation
;
1372 samlogon_state
.r_ex
.out
.authoritative
= &authoritative
;
1373 samlogon_state
.r_ex
.out
.flags
= &flags
;
1375 samlogon_state
.r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1376 samlogon_state
.r
.in
.computer_name
= TEST_MACHINE_NAME
;
1377 samlogon_state
.r
.in
.credential
= &samlogon_state
.auth
;
1378 samlogon_state
.r
.in
.return_authenticator
= &samlogon_state
.auth2
;
1379 samlogon_state
.r
.in
.logon
= &logon
;
1380 samlogon_state
.r
.out
.validation
= &validation
;
1381 samlogon_state
.r
.out
.authoritative
= &authoritative
;
1384 for (f
=0;f
<ARRAY_SIZE(function_levels
);f
++) {
1385 for (i
=0; test_table
[i
].fn
; i
++) {
1386 if (n_subtests
&& (i
> n_subtests
)) {
1389 for (v
=0;v
<ARRAY_SIZE(validation_levels
);v
++) {
1390 for (l
=0;l
<ARRAY_SIZE(logon_levels
);l
++) {
1391 char *error_string
= NULL
;
1392 TALLOC_CTX
*tmp_ctx
= talloc_named(fn_ctx
, 0, "test_SamLogon inner loop");
1393 samlogon_state
.mem_ctx
= tmp_ctx
;
1394 samlogon_state
.function_level
= function_levels
[f
];
1395 samlogon_state
.r
.in
.validation_level
= validation_levels
[v
];
1396 samlogon_state
.r
.in
.logon_level
= logon_levels
[l
];
1397 samlogon_state
.r_ex
.in
.validation_level
= validation_levels
[v
];
1398 samlogon_state
.r_ex
.in
.logon_level
= logon_levels
[l
];
1399 samlogon_state
.r_flags
.in
.validation_level
= validation_levels
[v
];
1400 samlogon_state
.r_flags
.in
.logon_level
= logon_levels
[l
];
1401 if (!test_table
[i
].fn(&samlogon_state
, &error_string
)) {
1402 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1403 samlogon_state
.comment
,
1404 samlogon_state
.account_domain
,
1405 samlogon_state
.account_name
,
1406 test_table
[i
].name
, validation_levels
[v
],
1407 logon_levels
[l
], function_levels
[f
]);
1409 if (test_table
[i
].expect_fail
) {
1410 d_printf(" failed (expected, test incomplete): %s\n", error_string
);
1412 d_printf(" failed: %s\n", error_string
);
1415 SAFE_FREE(error_string
);
1417 talloc_free(tmp_ctx
);
1422 talloc_free(fn_ctx
);
1427 test an ADS style interactive domain logon
1429 bool test_InteractiveLogon(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1430 struct netlogon_creds_CredentialState
*creds
,
1431 const char *comment
,
1432 const char *workstation_name
,
1433 const char *account_domain
, const char *account_name
,
1434 const char *plain_pass
, uint32_t parameter_control
,
1435 NTSTATUS expected_error
)
1438 TALLOC_CTX
*fn_ctx
= talloc_named(mem_ctx
, 0, "test_InteractiveLogon function-level context");
1439 struct netr_LogonSamLogonWithFlags r
;
1440 struct netr_Authenticator a
, ra
;
1441 struct netr_PasswordInfo pinfo
;
1444 union netr_LogonLevel logon
;
1445 union netr_Validation validation
;
1446 uint8_t authoritative
= 0;
1453 ZERO_STRUCT(validation
);
1455 netlogon_creds_client_authenticator(creds
, &a
);
1457 logon
.password
= &pinfo
;
1459 r
.in
.server_name
= talloc_asprintf(fn_ctx
, "\\\\%s", dcerpc_server_name(p
));
1460 r
.in
.computer_name
= TEST_MACHINE_NAME
;
1461 r
.in
.credential
= &a
;
1462 r
.in
.return_authenticator
= &ra
;
1463 r
.in
.logon_level
= 5;
1464 r
.in
.logon
= &logon
;
1465 r
.in
.validation_level
= 6;
1466 r
.in
.flags
= &flags
;
1467 r
.out
.validation
= &validation
;
1468 r
.out
.authoritative
= &authoritative
;
1469 r
.out
.flags
= &flags
;
1471 pinfo
.identity_info
.domain_name
.string
= account_domain
;
1472 pinfo
.identity_info
.parameter_control
= parameter_control
;
1473 pinfo
.identity_info
.logon_id_low
= 0;
1474 pinfo
.identity_info
.logon_id_high
= 0;
1475 pinfo
.identity_info
.account_name
.string
= account_name
;
1476 pinfo
.identity_info
.workstation
.string
= workstation_name
;
1478 if (!E_deshash(plain_pass
, pinfo
.lmpassword
.hash
)) {
1479 ZERO_STRUCT(pinfo
.lmpassword
.hash
);
1481 E_md4hash(plain_pass
, pinfo
.ntpassword
.hash
);
1483 if (creds
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
1484 netlogon_creds_arcfour_crypt(creds
, pinfo
.lmpassword
.hash
, 16);
1485 netlogon_creds_arcfour_crypt(creds
, pinfo
.ntpassword
.hash
, 16);
1487 netlogon_creds_des_encrypt(creds
, &pinfo
.lmpassword
);
1488 netlogon_creds_des_encrypt(creds
, &pinfo
.ntpassword
);
1491 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment
);
1493 status
= dcerpc_netr_LogonSamLogonWithFlags(p
, fn_ctx
, &r
);
1494 if (!r
.out
.return_authenticator
1495 || !netlogon_creds_client_check(creds
, &r
.out
.return_authenticator
->cred
)) {
1496 d_printf("Credential chaining failed\n");
1497 talloc_free(fn_ctx
);
1501 talloc_free(fn_ctx
);
1503 if (!NT_STATUS_EQUAL(expected_error
, status
)) {
1504 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1505 account_domain
, account_name
, nt_errstr(expected_error
), nt_errstr(status
));
1514 bool torture_rpc_samlogon(struct torture_context
*torture
)
1517 struct dcerpc_pipe
*p
;
1518 struct dcerpc_binding
*b
;
1519 struct cli_credentials
*machine_credentials
;
1520 TALLOC_CTX
*mem_ctx
= talloc_init("torture_rpc_netlogon");
1522 struct test_join
*join_ctx
= NULL
;
1523 struct test_join
*user_ctx
= NULL
, *user_ctx_wrong_wks
= NULL
, *user_ctx_wrong_time
= NULL
;
1524 char *user_password
, *user_password_wrong_wks
, *user_password_wrong_time
;
1525 const char *old_user_password
;
1526 char *test_machine_account
;
1527 const char *userdomain
;
1528 struct samr_SetUserInfo s
;
1529 union samr_UserInfo u
;
1533 unsigned int credential_flags
[] = {
1534 NETLOGON_NEG_AUTH2_FLAGS
,
1535 NETLOGON_NEG_ARCFOUR
,
1536 NETLOGON_NEG_ARCFOUR
| NETLOGON_NEG_128BIT
,
1537 NETLOGON_NEG_AUTH2_ADS_FLAGS
,
1538 0 /* yes, this is a valid flag, causes the use of DES */
1541 struct netlogon_creds_CredentialState
*creds
;
1543 test_machine_account
= talloc_asprintf(mem_ctx
, "%s$", TEST_MACHINE_NAME
);
1544 /* We only need to join as a workstation here, and in future,
1545 * if we wish to test against trusted domains, we must be a
1546 * workstation here */
1547 join_ctx
= torture_join_domain(torture
, TEST_MACHINE_NAME
, ACB_WSTRUST
,
1548 &machine_credentials
);
1550 d_printf("Failed to join as Workstation\n");
1554 userdomain
= torture_setting_string(torture
, "userdomain", lp_workgroup(torture
->lp_ctx
));
1556 user_ctx
= torture_create_testuser(torture
,
1560 (const char **)&user_password
);
1562 d_printf("Failed to create a test user\n");
1566 old_user_password
= user_password
;
1568 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx
), torture
,
1569 TEST_USER_NAME
, 16 /* > 14 */, &user_password
,
1572 user_ctx_wrong_wks
= torture_create_testuser(torture
,
1573 TEST_USER_NAME_WRONG_WKS
,
1576 (const char **)&user_password_wrong_wks
);
1577 if (!user_ctx_wrong_wks
) {
1578 d_printf("Failed to create a test user (wrong workstation test)\n");
1583 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_wks
);
1587 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
;
1588 u
.info21
.workstations
.string
= "not" TEST_MACHINE_NAME
;
1590 status
= dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks
), mem_ctx
, &s
);
1591 if (!NT_STATUS_IS_OK(status
)) {
1592 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status
));
1598 = torture_create_testuser(torture
, TEST_USER_NAME_WRONG_TIME
,
1601 (const char **)&user_password_wrong_time
);
1602 if (!user_ctx_wrong_time
) {
1603 d_printf("Failed to create a test user (wrong workstation test)\n");
1608 s
.in
.user_handle
= torture_join_samr_user_policy(user_ctx_wrong_time
);
1612 u
.info21
.fields_present
= SAMR_FIELD_WORKSTATIONS
| SAMR_FIELD_LOGON_HOURS
;
1613 u
.info21
.workstations
.string
= TEST_MACHINE_NAME
;
1614 u
.info21
.logon_hours
.units_per_week
= 168;
1615 u
.info21
.logon_hours
.bits
= talloc_zero_array(mem_ctx
, uint8_t, 168);
1617 status
= dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time
), mem_ctx
, &s
);
1618 if (!NT_STATUS_IS_OK(status
)) {
1619 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status
));
1624 status
= torture_rpc_binding(torture
, &b
);
1625 if (!NT_STATUS_IS_OK(status
)) {
1630 /* We have to use schannel, otherwise the SamLogonEx fails
1631 * with INTERNAL_ERROR */
1633 b
->flags
&= ~DCERPC_AUTH_OPTIONS
;
1634 b
->flags
|= DCERPC_SCHANNEL
| DCERPC_SIGN
| DCERPC_SCHANNEL_128
;
1636 status
= dcerpc_pipe_connect_b(mem_ctx
, &p
, b
,
1637 &ndr_table_netlogon
,
1638 machine_credentials
, torture
->ev
, torture
->lp_ctx
);
1640 if (!NT_STATUS_IS_OK(status
)) {
1641 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status
));
1646 status
= dcerpc_schannel_creds(p
->conn
->security_state
.generic_state
, mem_ctx
, &creds
);
1647 if (!NT_STATUS_IS_OK(status
)) {
1655 const char *comment
;
1657 const char *username
;
1658 const char *password
;
1660 NTSTATUS expected_interactive_error
;
1661 NTSTATUS expected_network_error
;
1662 uint32_t parameter_control
;
1663 bool old_password
; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1666 .comment
= "domain\\user",
1667 .domain
= cli_credentials_get_domain(cmdline_credentials
),
1668 .username
= cli_credentials_get_username(cmdline_credentials
),
1669 .password
= cli_credentials_get_password(cmdline_credentials
),
1670 .network_login
= true,
1671 .expected_interactive_error
= NT_STATUS_OK
,
1672 .expected_network_error
= NT_STATUS_OK
1675 .comment
= "realm\\user",
1676 .domain
= cli_credentials_get_realm(cmdline_credentials
),
1677 .username
= cli_credentials_get_username(cmdline_credentials
),
1678 .password
= cli_credentials_get_password(cmdline_credentials
),
1679 .network_login
= true,
1680 .expected_interactive_error
= NT_STATUS_OK
,
1681 .expected_network_error
= NT_STATUS_OK
1684 .comment
= "user@domain",
1686 .username
= talloc_asprintf(mem_ctx
,
1688 cli_credentials_get_username(cmdline_credentials
),
1689 cli_credentials_get_domain(cmdline_credentials
)
1691 .password
= cli_credentials_get_password(cmdline_credentials
),
1692 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1693 .expected_interactive_error
= NT_STATUS_OK
,
1694 .expected_network_error
= NT_STATUS_OK
1697 .comment
= "user@realm",
1699 .username
= talloc_asprintf(mem_ctx
,
1701 cli_credentials_get_username(cmdline_credentials
),
1702 cli_credentials_get_realm(cmdline_credentials
)
1704 .password
= cli_credentials_get_password(cmdline_credentials
),
1705 .network_login
= true,
1706 .expected_interactive_error
= NT_STATUS_OK
,
1707 .expected_network_error
= NT_STATUS_OK
1710 .comment
= "machine domain\\user",
1711 .domain
= cli_credentials_get_domain(machine_credentials
),
1712 .username
= cli_credentials_get_username(machine_credentials
),
1713 .password
= cli_credentials_get_password(machine_credentials
),
1714 .network_login
= true,
1715 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1716 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1719 .comment
= "machine domain\\user",
1720 .domain
= cli_credentials_get_domain(machine_credentials
),
1721 .username
= cli_credentials_get_username(machine_credentials
),
1722 .password
= cli_credentials_get_password(machine_credentials
),
1723 .network_login
= true,
1724 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1725 .expected_network_error
= NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1728 .comment
= "machine realm\\user",
1729 .domain
= cli_credentials_get_realm(machine_credentials
),
1730 .username
= cli_credentials_get_username(machine_credentials
),
1731 .password
= cli_credentials_get_password(machine_credentials
),
1732 .network_login
= true,
1733 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1734 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1737 .comment
= "machine user@domain",
1739 .username
= talloc_asprintf(mem_ctx
,
1741 cli_credentials_get_username(machine_credentials
),
1742 cli_credentials_get_domain(machine_credentials
)
1744 .password
= cli_credentials_get_password(machine_credentials
),
1745 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1746 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1747 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1750 .comment
= "machine user@realm",
1752 .username
= talloc_asprintf(mem_ctx
,
1754 cli_credentials_get_username(machine_credentials
),
1755 cli_credentials_get_realm(machine_credentials
)
1757 .password
= cli_credentials_get_password(machine_credentials
),
1758 .network_login
= true,
1759 .expected_interactive_error
= NT_STATUS_NO_SUCH_USER
,
1760 .parameter_control
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1763 .comment
= "test user (long pw): domain\\user",
1764 .domain
= userdomain
,
1765 .username
= TEST_USER_NAME
,
1766 .password
= user_password
,
1767 .network_login
= true,
1768 .expected_interactive_error
= NT_STATUS_OK
,
1769 .expected_network_error
= NT_STATUS_OK
1772 .comment
= "test user (long pw): user@realm",
1774 .username
= talloc_asprintf(mem_ctx
,
1777 lp_realm(torture
->lp_ctx
)),
1778 .password
= user_password
,
1779 .network_login
= true,
1780 .expected_interactive_error
= NT_STATUS_OK
,
1781 .expected_network_error
= NT_STATUS_OK
1784 .comment
= "test user (long pw): user@domain",
1786 .username
= talloc_asprintf(mem_ctx
,
1790 .password
= user_password
,
1791 .network_login
= false, /* works for some things, but not NTLMv2. Odd */
1792 .expected_interactive_error
= NT_STATUS_OK
,
1793 .expected_network_error
= NT_STATUS_OK
1795 /* Oddball, can we use the old password ? */
1797 .comment
= "test user: user\\domain OLD PASSWORD",
1798 .domain
= userdomain
,
1799 .username
= TEST_USER_NAME
,
1800 .password
= old_user_password
,
1801 .network_login
= true,
1802 .expected_interactive_error
= NT_STATUS_WRONG_PASSWORD
,
1803 .expected_network_error
= NT_STATUS_OK
,
1804 .old_password
= true
1807 .comment
= "test user (wong workstation): domain\\user",
1808 .domain
= userdomain
,
1809 .username
= TEST_USER_NAME_WRONG_WKS
,
1810 .password
= user_password_wrong_wks
,
1811 .network_login
= true,
1812 .expected_interactive_error
= NT_STATUS_INVALID_WORKSTATION
,
1813 .expected_network_error
= NT_STATUS_INVALID_WORKSTATION
1817 /* Try all the tests for different username forms */
1818 for (ci
= 0; ci
< ARRAY_SIZE(usercreds
); ci
++) {
1820 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1821 usercreds
[ci
].comment
,
1823 usercreds
[ci
].domain
,
1824 usercreds
[ci
].username
,
1825 usercreds
[ci
].password
,
1826 usercreds
[ci
].parameter_control
,
1827 usercreds
[ci
].expected_interactive_error
)) {
1831 if (usercreds
[ci
].network_login
) {
1832 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1833 usercreds
[ci
].comment
,
1834 usercreds
[ci
].domain
,
1835 usercreds
[ci
].username
,
1836 usercreds
[ci
].password
,
1837 usercreds
[ci
].parameter_control
,
1838 usercreds
[ci
].expected_network_error
,
1839 usercreds
[ci
].old_password
,
1846 /* Using the first username form, try the different
1847 * credentials flag setups, on only one of the tests (checks
1848 * session key encryption) */
1850 for (i
=0; i
< ARRAY_SIZE(credential_flags
); i
++) {
1851 /* TODO: Somehow we lost setting up the different credential flags here! */
1853 if (!test_InteractiveLogon(p
, mem_ctx
, creds
,
1854 usercreds
[0].comment
,
1856 usercreds
[0].domain
,
1857 usercreds
[0].username
,
1858 usercreds
[0].password
,
1859 usercreds
[0].parameter_control
,
1860 usercreds
[0].expected_interactive_error
)) {
1864 if (usercreds
[0].network_login
) {
1865 if (!test_SamLogon(p
, mem_ctx
, torture
, creds
,
1866 usercreds
[0].comment
,
1867 usercreds
[0].domain
,
1868 usercreds
[0].username
,
1869 usercreds
[0].password
,
1870 usercreds
[0].parameter_control
,
1871 usercreds
[0].expected_network_error
,
1872 usercreds
[0].old_password
,
1881 talloc_free(mem_ctx
);
1883 torture_leave_domain(torture
, join_ctx
);
1884 torture_leave_domain(torture
, user_ctx
);
1885 torture_leave_domain(torture
, user_ctx_wrong_wks
);
1886 torture_leave_domain(torture
, user_ctx_wrong_time
);