2 Unix SMB/CIFS implementation.
4 test suite for netlogon SamLogon operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_netlogon_c.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "auth/auth.h"
29 #include "lib/crypto/crypto.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/rpc.h"
32 #include "auth/gensec/schannel_proto.h"
33 #include "libcli/auth/libcli_auth.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;
55 struct dcerpc_pipe *p;
57 uint32_t parameter_control;
58 struct netr_LogonSamLogon r;
59 struct netr_LogonSamLogonEx r_ex;
60 struct netr_LogonSamLogonWithFlags r_flags;
61 struct netr_Authenticator auth, auth2;
62 struct creds_CredentialState *creds;
63 NTSTATUS expected_error;
64 BOOL old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
69 Authenticate a user with a challenge/response, checking session key
70 and valid authentication types
72 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
73 enum ntlm_break break_which,
74 uint32_t parameter_control,
76 DATA_BLOB *lm_response,
77 DATA_BLOB *nt_response,
79 uint8_t user_session_key[16],
83 struct netr_LogonSamLogon *r = &samlogon_state->r;
84 struct netr_LogonSamLogonEx *r_ex = &samlogon_state->r_ex;
85 struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
86 struct netr_NetworkInfo ninfo;
87 struct netr_SamBaseInfo *base = NULL;
88 uint16_t validation_level = 0;
90 samlogon_state->r.in.logon.network = &ninfo;
91 samlogon_state->r_ex.in.logon.network = &ninfo;
92 samlogon_state->r_flags.in.logon.network = &ninfo;
94 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
95 ninfo.identity_info.parameter_control = parameter_control;
96 ninfo.identity_info.logon_id_low = 0;
97 ninfo.identity_info.logon_id_high = 0;
98 ninfo.identity_info.account_name.string = samlogon_state->account_name;
99 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
101 memcpy(ninfo.challenge, chall->data, 8);
103 switch (break_which) {
107 if (lm_response && lm_response->data) {
108 lm_response->data[0]++;
112 if (nt_response && nt_response->data) {
113 nt_response->data[0]++;
117 if (lm_response && lm_response->data) {
118 lm_response->data[0]++;
120 if (nt_response && nt_response->data) {
121 nt_response->data[0]++;
125 data_blob_free(lm_response);
128 data_blob_free(nt_response);
133 ninfo.nt.data = nt_response->data;
134 ninfo.nt.length = nt_response->length;
136 ninfo.nt.data = NULL;
141 ninfo.lm.data = lm_response->data;
142 ninfo.lm.length = lm_response->length;
144 ninfo.lm.data = NULL;
148 switch (samlogon_state->function_level) {
149 case NDR_NETR_LOGONSAMLOGON:
150 ZERO_STRUCT(samlogon_state->auth2);
151 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
153 r->out.return_authenticator = NULL;
154 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
155 if (!r->out.return_authenticator ||
156 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
157 d_printf("Credential chaining failed\n");
159 if (!NT_STATUS_IS_OK(status)) {
161 *error_string = strdup(nt_errstr(status));
166 validation_level = r->in.validation_level;
168 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r->out.validation);
170 switch (validation_level) {
172 base = &r->out.validation.sam2->base;
175 base = &r->out.validation.sam3->base;
178 base = &r->out.validation.sam6->base;
182 case NDR_NETR_LOGONSAMLOGONEX:
183 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
184 if (!NT_STATUS_IS_OK(status)) {
186 *error_string = strdup(nt_errstr(status));
191 validation_level = r_ex->in.validation_level;
193 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_ex->out.validation);
195 switch (validation_level) {
197 base = &r_ex->out.validation.sam2->base;
200 base = &r_ex->out.validation.sam3->base;
203 base = &r_ex->out.validation.sam6->base;
207 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
208 ZERO_STRUCT(samlogon_state->auth2);
209 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
211 r_flags->out.return_authenticator = NULL;
212 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
213 if (!r_flags->out.return_authenticator ||
214 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
215 d_printf("Credential chaining failed\n");
217 if (!NT_STATUS_IS_OK(status)) {
219 *error_string = strdup(nt_errstr(status));
224 validation_level = r_flags->in.validation_level;
226 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_flags->out.validation);
228 switch (validation_level) {
230 base = &r_flags->out.validation.sam2->base;
233 base = &r_flags->out.validation.sam3->base;
236 base = &r_flags->out.validation.sam6->base;
242 return NT_STATUS_INVALID_PARAMETER;
246 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
247 return NT_STATUS_INVALID_PARAMETER;
250 if (user_session_key) {
251 memcpy(user_session_key, base->key.key, 16);
254 memcpy(lm_key, base->LMSessKey.key, 8);
262 * Test the normal 'LM and NTLM' combination
265 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
270 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
271 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
272 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
275 uint8_t user_session_key[16];
280 ZERO_STRUCT(user_session_key);
282 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
284 ZERO_STRUCT(lm_hash);
286 E_deshash(samlogon_state->password, lm_hash);
289 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
291 E_md4hash(samlogon_state->password, nt_hash);
292 SMBsesskeygen_ntv1(nt_hash, session_key.data);
294 nt_status = check_samlogon(samlogon_state,
296 samlogon_state->parameter_control,
297 &samlogon_state->chall,
304 data_blob_free(&lm_response);
306 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
307 /* for 'long' passwords, the LM password is invalid */
308 if (break_which == NO_NT && !lm_good) {
311 /* for 'old' passwords, we allow the server to be OK or wrong password */
312 if (samlogon_state->old_password) {
315 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
316 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
317 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
318 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
319 SAFE_FREE(*error_string);
320 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
322 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
324 } else if (!NT_STATUS_IS_OK(nt_status)) {
328 if (break_which == NO_NT && !lm_good) {
329 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
333 if (memcmp(lm_hash, lm_key,
334 sizeof(lm_key)) != 0) {
335 d_printf("LM Key does not match expectations!\n");
336 d_printf("lm_key:\n");
337 dump_data(1, lm_key, 8);
338 d_printf("expected:\n");
339 dump_data(1, lm_hash, 8);
343 switch (break_which) {
346 uint8_t lm_key_expected[16];
347 memcpy(lm_key_expected, lm_hash, 8);
348 memset(lm_key_expected+8, '\0', 8);
349 if (memcmp(lm_key_expected, user_session_key,
351 *error_string = strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
352 d_printf("user_session_key:\n");
353 dump_data(1, user_session_key, sizeof(user_session_key));
354 d_printf("expected:\n");
355 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
361 if (memcmp(session_key.data, user_session_key,
362 sizeof(user_session_key)) != 0) {
363 *error_string = strdup("NT Session Key does not match expectations!\n");
364 d_printf("user_session_key:\n");
365 dump_data(1, user_session_key, 16);
366 d_printf("expected:\n");
367 dump_data(1, session_key.data, session_key.length);
375 * Test LM authentication, no NT response supplied
378 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string)
381 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
385 * Test the NTLM response only, no LM.
388 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
390 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
394 * Test the NTLM response only, but in the LM field.
397 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
402 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
403 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
407 uint8_t user_session_key[16];
411 ZERO_STRUCT(user_session_key);
413 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
415 E_md4hash(samlogon_state->password, nt_hash);
416 SMBsesskeygen_ntv1(nt_hash,
419 lm_good = E_deshash(samlogon_state->password, lm_hash);
421 ZERO_STRUCT(lm_hash);
423 nt_status = check_samlogon(samlogon_state,
425 samlogon_state->parameter_control,
426 &samlogon_state->chall,
433 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
434 /* for 'old' passwords, we allow the server to be OK or wrong password */
435 if (samlogon_state->old_password) {
439 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
440 SAFE_FREE(*error_string);
441 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
443 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
445 } else if (!NT_STATUS_IS_OK(nt_status)) {
450 if (memcmp(lm_hash, lm_key,
451 sizeof(lm_key)) != 0) {
452 d_printf("LM Key does not match expectations!\n");
453 d_printf("lm_key:\n");
454 dump_data(1, lm_key, 8);
455 d_printf("expected:\n");
456 dump_data(1, lm_hash, 8);
461 if (memcmp(session_key.data, lm_key,
462 sizeof(lm_key)) != 0) {
463 d_printf("LM Key does not match expectations (first 8 session key)!\n");
464 d_printf("lm_key:\n");
465 dump_data(1, lm_key, 8);
466 d_printf("expected:\n");
467 dump_data(1, session_key.data, 8);
472 if (lm_good && memcmp(lm_hash, user_session_key, 8) != 0) {
473 uint8_t lm_key_expected[16];
474 memcpy(lm_key_expected, lm_hash, 8);
475 memset(lm_key_expected+8, '\0', 8);
476 if (memcmp(lm_key_expected, user_session_key,
478 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
479 d_printf("user_session_key:\n");
480 dump_data(1, user_session_key, sizeof(user_session_key));
481 d_printf("expected:\n");
482 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
490 * Test the NTLM response only, but in the both the NT and LM fields.
493 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
498 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
499 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
503 uint8_t user_session_key[16];
507 ZERO_STRUCT(user_session_key);
509 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
511 E_md4hash(samlogon_state->password, nt_hash);
512 SMBsesskeygen_ntv1(nt_hash,
515 lm_good = E_deshash(samlogon_state->password, lm_hash);
517 ZERO_STRUCT(lm_hash);
520 nt_status = check_samlogon(samlogon_state,
522 samlogon_state->parameter_control,
523 &samlogon_state->chall,
530 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
531 /* for 'old' passwords, we allow the server to be OK or wrong password */
532 if (samlogon_state->old_password) {
536 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
537 SAFE_FREE(*error_string);
538 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
540 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
542 } else if (!NT_STATUS_IS_OK(nt_status)) {
546 if (!NT_STATUS_IS_OK(nt_status)) {
550 if (memcmp(lm_hash, lm_key,
551 sizeof(lm_key)) != 0) {
552 d_printf("LM Key does not match expectations!\n");
553 d_printf("lm_key:\n");
554 dump_data(1, lm_key, 8);
555 d_printf("expected:\n");
556 dump_data(1, lm_hash, 8);
559 if (memcmp(session_key.data, user_session_key,
560 sizeof(user_session_key)) != 0) {
561 d_printf("NT Session Key does not match expectations!\n");
562 d_printf("user_session_key:\n");
563 dump_data(1, user_session_key, 16);
564 d_printf("expected:\n");
565 dump_data(1, session_key.data, session_key.length);
574 * Test the NTLMv2 and LMv2 responses
582 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state,
583 enum ntlm_break break_which,
584 enum ntlmv2_domain ntlmv2_domain,
589 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
590 DATA_BLOB lmv2_response = data_blob(NULL, 0);
591 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
592 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
593 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, TEST_MACHINE_NAME, lp_workgroup());
595 uint8_t lm_session_key[8];
596 uint8_t user_session_key[16];
598 ZERO_STRUCT(lm_session_key);
599 ZERO_STRUCT(user_session_key);
601 switch (ntlmv2_domain) {
603 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
604 samlogon_state->account_name, samlogon_state->account_domain,
605 samlogon_state->password, &samlogon_state->chall,
607 &lmv2_response, &ntlmv2_response,
608 &lmv2_session_key, &ntlmv2_session_key)) {
609 data_blob_free(&names_blob);
614 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
615 samlogon_state->account_name, "",
616 samlogon_state->password, &samlogon_state->chall,
618 &lmv2_response, &ntlmv2_response,
619 &lmv2_session_key, &ntlmv2_session_key)) {
620 data_blob_free(&names_blob);
625 data_blob_free(&names_blob);
627 nt_status = check_samlogon(samlogon_state,
629 samlogon_state->parameter_control,
630 &samlogon_state->chall,
637 data_blob_free(&lmv2_response);
638 data_blob_free(&ntlmv2_response);
641 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
642 /* for 'old' passwords, we allow the server to be OK or wrong password */
643 if (samlogon_state->old_password) {
646 return break_which == BREAK_BOTH;
647 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
648 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
649 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
650 SAFE_FREE(*error_string);
651 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
653 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
655 } else if (!NT_STATUS_IS_OK(nt_status)) {
660 switch (break_which) {
662 if (memcmp(lmv2_session_key.data, user_session_key,
663 sizeof(user_session_key)) != 0) {
664 d_printf("USER (LMv2) Session Key does not match expectations!\n");
665 d_printf("user_session_key:\n");
666 dump_data(1, user_session_key, 16);
667 d_printf("expected:\n");
668 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
671 if (memcmp(lmv2_session_key.data, lm_session_key,
672 sizeof(lm_session_key)) != 0) {
673 d_printf("LM (LMv2) Session Key does not match expectations!\n");
674 d_printf("lm_session_key:\n");
675 dump_data(1, lm_session_key, 8);
676 d_printf("expected:\n");
677 dump_data(1, lmv2_session_key.data, 8);
682 if (memcmp(ntlmv2_session_key.data, user_session_key,
683 sizeof(user_session_key)) != 0) {
684 if (memcmp(lmv2_session_key.data, user_session_key,
685 sizeof(user_session_key)) == 0) {
686 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
687 d_printf("user_session_key:\n");
688 dump_data(1, user_session_key, 16);
689 d_printf("expected:\n");
690 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
694 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
695 d_printf("user_session_key:\n");
696 dump_data(1, user_session_key, 16);
697 d_printf("expected:\n");
698 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
702 if (memcmp(ntlmv2_session_key.data, lm_session_key,
703 sizeof(lm_session_key)) != 0) {
704 if (memcmp(lmv2_session_key.data, lm_session_key,
705 sizeof(lm_session_key)) == 0) {
706 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
707 d_printf("user_session_key:\n");
708 dump_data(1, lm_session_key, 8);
709 d_printf("expected:\n");
710 dump_data(1, ntlmv2_session_key.data, 8);
713 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
714 d_printf("lm_session_key:\n");
715 dump_data(1, lm_session_key, 8);
716 d_printf("expected:\n");
717 dump_data(1, ntlmv2_session_key.data, 8);
727 * Test the NTLM and LMv2 responses
730 static BOOL test_lmv2_ntlm_broken(struct samlogon_state *samlogon_state,
731 enum ntlm_break break_which,
732 enum ntlmv2_domain ntlmv2_domain,
737 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
738 DATA_BLOB lmv2_response = data_blob(NULL, 0);
739 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
740 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
741 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
743 DATA_BLOB ntlm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
744 DATA_BLOB ntlm_session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
748 uint8_t lm_session_key[8];
749 uint8_t user_session_key[16];
752 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
754 E_md4hash(samlogon_state->password, nt_hash);
755 SMBsesskeygen_ntv1(nt_hash,
756 ntlm_session_key.data);
758 lm_good = E_deshash(samlogon_state->password, lm_hash);
760 ZERO_STRUCT(lm_hash);
763 ZERO_STRUCT(lm_session_key);
764 ZERO_STRUCT(user_session_key);
766 switch (ntlmv2_domain) {
768 /* TODO - test with various domain cases, and without domain */
769 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
770 samlogon_state->account_name, samlogon_state->account_domain,
771 samlogon_state->password, &samlogon_state->chall,
773 &lmv2_response, &ntlmv2_response,
774 &lmv2_session_key, &ntlmv2_session_key)) {
775 data_blob_free(&names_blob);
780 /* TODO - test with various domain cases, and without domain */
781 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
782 samlogon_state->account_name, "",
783 samlogon_state->password, &samlogon_state->chall,
785 &lmv2_response, &ntlmv2_response,
786 &lmv2_session_key, &ntlmv2_session_key)) {
787 data_blob_free(&names_blob);
793 data_blob_free(&names_blob);
795 nt_status = check_samlogon(samlogon_state,
797 samlogon_state->parameter_control,
798 &samlogon_state->chall,
805 data_blob_free(&lmv2_response);
806 data_blob_free(&ntlmv2_response);
809 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
810 /* for 'old' passwords, we allow the server to be OK or wrong password */
811 if (samlogon_state->old_password) {
814 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
815 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
816 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
817 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
818 SAFE_FREE(*error_string);
819 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
821 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
823 } else if (!NT_STATUS_IS_OK(nt_status)) {
827 switch (break_which) {
829 if (memcmp(lmv2_session_key.data, user_session_key,
830 sizeof(user_session_key)) != 0) {
831 d_printf("USER (LMv2) Session Key does not match expectations!\n");
832 d_printf("user_session_key:\n");
833 dump_data(1, user_session_key, 16);
834 d_printf("expected:\n");
835 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
838 if (memcmp(lmv2_session_key.data, lm_session_key,
839 sizeof(lm_session_key)) != 0) {
840 d_printf("LM (LMv2) Session Key does not match expectations!\n");
841 d_printf("lm_session_key:\n");
842 dump_data(1, lm_session_key, 8);
843 d_printf("expected:\n");
844 dump_data(1, lmv2_session_key.data, 8);
849 if (memcmp(ntlm_session_key.data, user_session_key,
850 sizeof(user_session_key)) != 0) {
851 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
852 d_printf("user_session_key:\n");
853 dump_data(1, user_session_key, 16);
854 d_printf("expected:\n");
855 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
859 if (memcmp(lm_hash, lm_session_key,
860 sizeof(lm_session_key)) != 0) {
861 d_printf("LM Session Key does not match expectations!\n");
862 d_printf("lm_session_key:\n");
863 dump_data(1, lm_session_key, 8);
864 d_printf("expected:\n");
865 dump_data(1, lm_hash, 8);
869 static const uint8_t zeros[8];
870 if (memcmp(zeros, lm_session_key,
871 sizeof(lm_session_key)) != 0) {
872 d_printf("LM Session Key does not match expectations (zeros)!\n");
873 d_printf("lm_session_key:\n");
874 dump_data(1, lm_session_key, 8);
875 d_printf("expected:\n");
876 dump_data(1, zeros, 8);
882 if (memcmp(ntlm_session_key.data, user_session_key,
883 sizeof(user_session_key)) != 0) {
884 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
885 d_printf("user_session_key:\n");
886 dump_data(1, user_session_key, 16);
887 d_printf("expected:\n");
888 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
891 if (memcmp(ntlm_session_key.data, lm_session_key,
892 sizeof(lm_session_key)) != 0) {
893 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
894 d_printf("lm_session_key:\n");
895 dump_data(1, lm_session_key, 8);
896 d_printf("expected:\n");
897 dump_data(1, ntlm_session_key.data, 8);
906 * Test the NTLMv2 and LMv2 responses
909 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
911 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, UPPER_DOMAIN, error_string);
915 static BOOL test_lmv2_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
917 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, NO_DOMAIN, error_string);
922 * Test the LMv2 response only
925 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
927 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, UPPER_DOMAIN, error_string);
930 static BOOL test_lmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
932 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, NO_DOMAIN, error_string);
936 * Test the NTLMv2 response only
939 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
941 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, UPPER_DOMAIN, error_string);
944 static BOOL test_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
946 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, NO_DOMAIN, error_string);
949 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
951 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
954 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
956 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
959 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
961 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
964 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
966 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
968 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
970 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
973 static BOOL test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
975 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
978 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
980 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
984 static BOOL test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
986 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
990 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
992 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
995 static BOOL test_ntlmv2_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
997 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
1000 static BOOL test_lmv2_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
1002 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
1005 static BOOL test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1007 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
1010 static BOOL test_lmv2_ntlm_break_ntlm(struct samlogon_state *samlogon_state, char **error_string)
1012 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
1015 static BOOL test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1017 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
1020 static BOOL test_lmv2_ntlm_break_lm(struct samlogon_state *samlogon_state, char **error_string)
1022 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
1025 static BOOL test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1027 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
1031 * Test the NTLM2 response (extra challenge in LM feild)
1033 * This test is the same as the 'break LM' test, but checks that the
1034 * server implements NTLM2 session security in the right place
1035 * (NETLOGON is the wrong place).
1038 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
1042 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1043 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1047 uint8_t nt_hash[16];
1048 uint8_t lm_hash[16];
1050 uint8_t user_session_key[16];
1051 uint8_t expected_user_session_key[16];
1052 uint8_t session_nonce_hash[16];
1053 uint8_t client_chall[8];
1055 struct MD5Context md5_session_nonce_ctx;
1056 HMACMD5Context hmac_ctx;
1058 ZERO_STRUCT(user_session_key);
1059 ZERO_STRUCT(lm_key);
1060 generate_random_buffer(client_chall, 8);
1062 MD5Init(&md5_session_nonce_ctx);
1063 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
1064 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
1065 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1067 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
1068 lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
1069 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
1072 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
1074 memcpy(lm_response.data, session_nonce_hash, 8);
1075 memset(lm_response.data + 8, 0, 16);
1077 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
1078 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
1079 hmac_md5_update(client_chall, 8, &hmac_ctx);
1080 hmac_md5_final(expected_user_session_key, &hmac_ctx);
1082 nt_status = check_samlogon(samlogon_state,
1084 samlogon_state->parameter_control,
1085 &samlogon_state->chall,
1092 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1093 /* for 'old' passwords, we allow the server to be OK or wrong password */
1094 if (samlogon_state->old_password) {
1098 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1099 SAFE_FREE(*error_string);
1100 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1102 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1104 } else if (!NT_STATUS_IS_OK(nt_status)) {
1109 if (memcmp(lm_hash, lm_key,
1110 sizeof(lm_key)) != 0) {
1111 d_printf("LM Key does not match expectations!\n");
1112 d_printf("lm_key:\n");
1113 dump_data(1, lm_key, 8);
1114 d_printf("expected:\n");
1115 dump_data(1, lm_hash, 8);
1119 static const uint8_t zeros[8];
1120 if (memcmp(zeros, lm_key,
1121 sizeof(lm_key)) != 0) {
1122 d_printf("LM Session Key does not match expectations (zeros)!\n");
1123 d_printf("lm_key:\n");
1124 dump_data(1, lm_key, 8);
1125 d_printf("expected:\n");
1126 dump_data(1, zeros, 8);
1130 if (memcmp(nt_key, user_session_key, 16) != 0) {
1131 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1132 d_printf("user_session_key:\n");
1133 dump_data(1, user_session_key, sizeof(user_session_key));
1134 d_printf("expected:\n");
1135 dump_data(1, nt_key, sizeof(nt_key));
1141 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
1144 DATA_BLOB nt_response = data_blob(NULL, 0);
1145 DATA_BLOB lm_response = data_blob(NULL, 0);
1150 uint8_t user_session_key[16];
1152 uint8_t lm_hash[16];
1153 static const uint8_t zeros[8];
1154 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
1155 BOOL lm_good = E_deshash(samlogon_state->password, lm_hash);
1157 ZERO_STRUCT(user_session_key);
1159 if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw,
1160 samlogon_state->password)) == -1) {
1161 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1165 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1167 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1169 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
1172 (void**)&dospw)) == -1) {
1173 DEBUG(0, ("convert_string_talloc failed!\n"));
1177 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1179 nt_status = check_samlogon(samlogon_state,
1181 samlogon_state->parameter_control | MSV1_0_CLEARTEXT_PASSWORD_ALLOWED,
1189 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1190 /* for 'old' passwords, we allow the server to be OK or wrong password */
1191 if (samlogon_state->old_password) {
1194 /* for 'long' passwords, the LM password is invalid */
1195 if (break_which == NO_NT && !lm_good) {
1198 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
1199 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
1200 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
1201 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1202 SAFE_FREE(*error_string);
1203 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1205 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1207 } else if (!NT_STATUS_IS_OK(nt_status)) {
1211 if (break_which == NO_NT && !lm_good) {
1212 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1219 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
1220 char **error_string) {
1221 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1224 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
1225 char **error_string) {
1226 return test_plaintext(samlogon_state, BREAK_LM, error_string);
1229 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
1230 char **error_string) {
1231 return test_plaintext(samlogon_state, BREAK_NT, error_string);
1234 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
1235 char **error_string) {
1236 return test_plaintext(samlogon_state, NO_LM, error_string);
1239 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
1240 char **error_string) {
1241 return test_plaintext(samlogon_state, NO_NT, error_string);
1255 - plaintext tests (in challenge-response fields)
1257 check we get the correct session key in each case
1258 check what values we get for the LM session key
1262 static const struct ntlm_tests {
1263 BOOL (*fn)(struct samlogon_state *, char **);
1267 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
1269 {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", False},
1271 {test_lm, "LM", False},
1272 {test_lm_ntlm, "LM and NTLM", False},
1273 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
1274 {test_ntlm, "NTLM", False},
1275 {test_ntlm_in_lm, "NTLM in LM", False},
1276 {test_ntlm_in_both, "NTLM in both", False},
1277 {test_ntlmv2, "NTLMv2", False},
1278 {test_ntlmv2_no_dom, "NTLMv2 (no domain)", False},
1279 {test_lmv2, "LMv2", False},
1280 {test_lmv2_no_dom, "LMv2 (no domain)", False},
1281 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
1282 {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", False},
1283 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
1285 {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", False},
1287 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
1288 {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", False},
1289 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
1290 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
1291 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
1292 {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", False},
1293 {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", False},
1294 {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", False},
1295 {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", False},
1296 {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", False},
1297 {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", False},
1298 {test_plaintext_none_broken, "Plaintext", False},
1299 {test_plaintext_lm_broken, "Plaintext LM broken", False},
1300 {test_plaintext_nt_broken, "Plaintext NT broken", False},
1301 {test_plaintext_nt_only, "Plaintext NT only", False},
1302 {test_plaintext_lm_only, "Plaintext LM only", False},
1307 try a netlogon SamLogon
1309 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1310 struct creds_CredentialState *creds,
1311 const char *comment,
1312 const char *account_domain, const char *account_name,
1313 const char *plain_pass, uint32_t parameter_control,
1314 NTSTATUS expected_error, BOOL old_password,
1317 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1320 int validation_levels[] = {2,3,6};
1321 int logon_levels[] = { 2, 6 };
1322 int function_levels[] = {
1323 NDR_NETR_LOGONSAMLOGON,
1324 NDR_NETR_LOGONSAMLOGONEX,
1325 NDR_NETR_LOGONSAMLOGONWITHFLAGS };
1326 struct samlogon_state samlogon_state;
1328 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1330 samlogon_state.comment = comment;
1331 samlogon_state.account_name = account_name;
1332 samlogon_state.account_domain = account_domain;
1333 samlogon_state.password = plain_pass;
1334 samlogon_state.p = p;
1335 samlogon_state.creds = creds;
1336 samlogon_state.expected_error = expected_error;
1337 samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1338 samlogon_state.parameter_control = parameter_control;
1339 samlogon_state.old_password = old_password;
1341 generate_random_buffer(samlogon_state.chall.data, 8);
1342 samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1343 samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
1344 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1345 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1346 samlogon_state.r_flags.in.flags = 0;
1348 samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1349 samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
1350 samlogon_state.r_ex.in.flags = 0;
1352 samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1353 samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
1354 samlogon_state.r.in.credential = &samlogon_state.auth;
1355 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1357 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1358 for (i=0; test_table[i].fn; i++) {
1359 if (n_subtests && (i > n_subtests)) {
1362 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1363 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1364 char *error_string = NULL;
1365 TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1366 samlogon_state.mem_ctx = tmp_ctx;
1367 samlogon_state.function_level = function_levels[f];
1368 samlogon_state.r.in.validation_level = validation_levels[v];
1369 samlogon_state.r.in.logon_level = logon_levels[l];
1370 samlogon_state.r_ex.in.validation_level = validation_levels[v];
1371 samlogon_state.r_ex.in.logon_level = logon_levels[l];
1372 samlogon_state.r_flags.in.validation_level = validation_levels[v];
1373 samlogon_state.r_flags.in.logon_level = logon_levels[l];
1374 if (!test_table[i].fn(&samlogon_state, &error_string)) {
1375 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1376 samlogon_state.comment,
1377 samlogon_state.account_domain,
1378 samlogon_state.account_name,
1379 test_table[i].name, validation_levels[v],
1380 logon_levels[l], function_levels[f]);
1382 if (test_table[i].expect_fail) {
1383 d_printf(" failed (expected, test incomplete): %s\n", error_string);
1385 d_printf(" failed: %s\n", error_string);
1388 SAFE_FREE(error_string);
1390 talloc_free(tmp_ctx);
1395 talloc_free(fn_ctx);
1400 test an ADS style interactive domain logon
1402 BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1403 struct creds_CredentialState *creds,
1404 const char *comment,
1405 const char *workstation_name,
1406 const char *account_domain, const char *account_name,
1407 const char *plain_pass, uint32_t parameter_control,
1408 NTSTATUS expected_error)
1411 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1412 struct netr_LogonSamLogonWithFlags r;
1413 struct netr_Authenticator a, ra;
1414 struct netr_PasswordInfo pinfo;
1420 creds_client_authenticator(creds, &a);
1422 r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1423 r.in.computer_name = TEST_MACHINE_NAME;
1424 r.in.credential = &a;
1425 r.in.return_authenticator = &ra;
1426 r.in.logon_level = 5;
1427 r.in.logon.password = &pinfo;
1428 r.in.validation_level = 6;
1431 pinfo.identity_info.domain_name.string = account_domain;
1432 pinfo.identity_info.parameter_control = parameter_control;
1433 pinfo.identity_info.logon_id_low = 0;
1434 pinfo.identity_info.logon_id_high = 0;
1435 pinfo.identity_info.account_name.string = account_name;
1436 pinfo.identity_info.workstation.string = workstation_name;
1438 if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1439 ZERO_STRUCT(pinfo.lmpassword.hash);
1441 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1443 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1444 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1445 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1447 creds_des_encrypt(creds, &pinfo.lmpassword);
1448 creds_des_encrypt(creds, &pinfo.ntpassword);
1451 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
1453 status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1454 if (!r.out.return_authenticator
1455 || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1456 d_printf("Credential chaining failed\n");
1457 talloc_free(fn_ctx);
1461 talloc_free(fn_ctx);
1463 if (!NT_STATUS_EQUAL(expected_error, status)) {
1464 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1465 account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
1474 BOOL torture_rpc_samlogon(struct torture_context *torture)
1477 struct dcerpc_pipe *p;
1478 struct dcerpc_binding *b;
1479 struct cli_credentials *machine_credentials;
1480 TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1482 struct test_join *join_ctx = NULL;
1483 struct test_join *user_ctx = NULL, *user_ctx_wrong_wks = NULL, *user_ctx_wrong_time = NULL;
1484 char *user_password, *user_password_wrong_wks, *user_password_wrong_time;
1485 const char *old_user_password;
1486 char *test_machine_account;
1487 const char *binding = torture_setting_string(torture, "binding", NULL);
1488 const char *userdomain;
1489 struct samr_SetUserInfo s;
1490 union samr_UserInfo u;
1494 unsigned int credential_flags[] = {
1495 NETLOGON_NEG_AUTH2_FLAGS,
1496 NETLOGON_NEG_ARCFOUR,
1497 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1498 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1499 0 /* yes, this is a valid flag, causes the use of DES */
1502 struct creds_CredentialState *creds;
1504 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1505 /* We only need to join as a workstation here, and in future,
1506 * if we wish to test against trusted domains, we must be a
1507 * workstation here */
1508 join_ctx = torture_join_domain(TEST_MACHINE_NAME, ACB_WSTRUST,
1509 &machine_credentials);
1511 d_printf("Failed to join as Workstation\n");
1515 userdomain = torture_setting_string(torture, "userdomain", lp_workgroup());
1517 user_ctx = torture_create_testuser(TEST_USER_NAME,
1520 (const char **)&user_password);
1522 d_printf("Failed to create a test user\n");
1526 old_user_password = user_password;
1528 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
1529 TEST_USER_NAME, 16 /* > 14 */, &user_password,
1532 user_ctx_wrong_wks = torture_create_testuser(TEST_USER_NAME_WRONG_WKS,
1535 (const char **)&user_password_wrong_wks);
1536 if (!user_ctx_wrong_wks) {
1537 d_printf("Failed to create a test user (wrong workstation test)\n");
1542 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_wks);
1546 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS;
1547 u.info21.workstations.string = "not" TEST_MACHINE_NAME;
1549 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks), mem_ctx, &s);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status));
1557 = torture_create_testuser(TEST_USER_NAME_WRONG_TIME,
1560 (const char **)&user_password_wrong_time);
1561 if (!user_ctx_wrong_time) {
1562 d_printf("Failed to create a test user (wrong workstation test)\n");
1567 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_time);
1571 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS | SAMR_FIELD_LOGON_HOURS;
1572 u.info21.workstations.string = TEST_MACHINE_NAME;
1573 u.info21.logon_hours.units_per_week = 168;
1574 u.info21.logon_hours.bits = talloc_zero_size(mem_ctx, 168);
1576 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time), mem_ctx, &s);
1577 if (!NT_STATUS_IS_OK(status)) {
1578 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status));
1583 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 d_printf("Bad binding string %s\n", binding);
1590 /* We have to use schannel, otherwise the SamLogonEx fails
1591 * with INTERNAL_ERROR */
1593 b->flags &= ~DCERPC_AUTH_OPTIONS;
1594 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1596 status = dcerpc_pipe_connect_b(mem_ctx, &p, b,
1597 &ndr_table_netlogon,
1598 machine_credentials, NULL);
1600 if (!NT_STATUS_IS_OK(status)) {
1601 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1606 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1607 if (!NT_STATUS_IS_OK(status)) {
1615 const char *comment;
1617 const char *username;
1618 const char *password;
1620 NTSTATUS expected_interactive_error;
1621 NTSTATUS expected_network_error;
1622 uint32_t parameter_control;
1623 BOOL old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1626 .comment = "domain\\user",
1627 .domain = cli_credentials_get_domain(cmdline_credentials),
1628 .username = cli_credentials_get_username(cmdline_credentials),
1629 .password = cli_credentials_get_password(cmdline_credentials),
1630 .network_login = True,
1631 .expected_interactive_error = NT_STATUS_OK,
1632 .expected_network_error = NT_STATUS_OK
1635 .comment = "realm\\user",
1636 .domain = cli_credentials_get_realm(cmdline_credentials),
1637 .username = cli_credentials_get_username(cmdline_credentials),
1638 .password = cli_credentials_get_password(cmdline_credentials),
1639 .network_login = True,
1640 .expected_interactive_error = NT_STATUS_OK,
1641 .expected_network_error = NT_STATUS_OK
1644 .comment = "user@domain",
1646 .username = talloc_asprintf(mem_ctx,
1648 cli_credentials_get_username(cmdline_credentials),
1649 cli_credentials_get_domain(cmdline_credentials)
1651 .password = cli_credentials_get_password(cmdline_credentials),
1652 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1653 .expected_interactive_error = NT_STATUS_OK,
1654 .expected_network_error = NT_STATUS_OK
1657 .comment = "user@realm",
1659 .username = talloc_asprintf(mem_ctx,
1661 cli_credentials_get_username(cmdline_credentials),
1662 cli_credentials_get_realm(cmdline_credentials)
1664 .password = cli_credentials_get_password(cmdline_credentials),
1665 .network_login = True,
1666 .expected_interactive_error = NT_STATUS_OK,
1667 .expected_network_error = NT_STATUS_OK
1670 .comment = "machine domain\\user",
1671 .domain = cli_credentials_get_domain(machine_credentials),
1672 .username = cli_credentials_get_username(machine_credentials),
1673 .password = cli_credentials_get_password(machine_credentials),
1674 .network_login = True,
1675 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1676 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1679 .comment = "machine domain\\user",
1680 .domain = cli_credentials_get_domain(machine_credentials),
1681 .username = cli_credentials_get_username(machine_credentials),
1682 .password = cli_credentials_get_password(machine_credentials),
1683 .network_login = True,
1684 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1685 .expected_network_error = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1688 .comment = "machine realm\\user",
1689 .domain = cli_credentials_get_realm(machine_credentials),
1690 .username = cli_credentials_get_username(machine_credentials),
1691 .password = cli_credentials_get_password(machine_credentials),
1692 .network_login = True,
1693 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1694 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1697 .comment = "machine user@domain",
1699 .username = talloc_asprintf(mem_ctx,
1701 cli_credentials_get_username(machine_credentials),
1702 cli_credentials_get_domain(machine_credentials)
1704 .password = cli_credentials_get_password(machine_credentials),
1705 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1706 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1707 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1710 .comment = "machine user@realm",
1712 .username = talloc_asprintf(mem_ctx,
1714 cli_credentials_get_username(machine_credentials),
1715 cli_credentials_get_realm(machine_credentials)
1717 .password = cli_credentials_get_password(machine_credentials),
1718 .network_login = True,
1719 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1720 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1723 .comment = "test user (long pw): domain\\user",
1724 .domain = userdomain,
1725 .username = TEST_USER_NAME,
1726 .password = user_password,
1727 .network_login = True,
1728 .expected_interactive_error = NT_STATUS_OK,
1729 .expected_network_error = NT_STATUS_OK
1732 .comment = "test user (long pw): user@realm",
1734 .username = talloc_asprintf(mem_ctx,
1738 .password = user_password,
1739 .network_login = True,
1740 .expected_interactive_error = NT_STATUS_OK,
1741 .expected_network_error = NT_STATUS_OK
1744 .comment = "test user (long pw): user@domain",
1746 .username = talloc_asprintf(mem_ctx,
1750 .password = user_password,
1751 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1752 .expected_interactive_error = NT_STATUS_OK,
1753 .expected_network_error = NT_STATUS_OK
1755 /* Oddball, can we use the old password ? */
1757 .comment = "test user: user\\domain OLD PASSWORD",
1758 .domain = userdomain,
1759 .username = TEST_USER_NAME,
1760 .password = old_user_password,
1761 .network_login = True,
1762 .expected_interactive_error = NT_STATUS_WRONG_PASSWORD,
1763 .expected_network_error = NT_STATUS_OK,
1764 .old_password = True
1767 .comment = "test user (wong workstation): domain\\user",
1768 .domain = userdomain,
1769 .username = TEST_USER_NAME_WRONG_WKS,
1770 .password = user_password_wrong_wks,
1771 .network_login = True,
1772 .expected_interactive_error = NT_STATUS_INVALID_WORKSTATION,
1773 .expected_network_error = NT_STATUS_INVALID_WORKSTATION
1777 /* Try all the tests for different username forms */
1778 for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1780 if (!test_InteractiveLogon(p, mem_ctx, creds,
1781 usercreds[ci].comment,
1783 usercreds[ci].domain,
1784 usercreds[ci].username,
1785 usercreds[ci].password,
1786 usercreds[ci].parameter_control,
1787 usercreds[ci].expected_interactive_error)) {
1791 if (usercreds[ci].network_login) {
1792 if (!test_SamLogon(p, mem_ctx, creds,
1793 usercreds[ci].comment,
1794 usercreds[ci].domain,
1795 usercreds[ci].username,
1796 usercreds[ci].password,
1797 usercreds[ci].parameter_control,
1798 usercreds[ci].expected_network_error,
1799 usercreds[ci].old_password,
1806 /* Using the first username form, try the different
1807 * credentials flag setups, on only one of the tests (checks
1808 * session key encryption) */
1810 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1811 /* TODO: Somehow we lost setting up the different credential flags here! */
1813 if (!test_InteractiveLogon(p, mem_ctx, creds,
1814 usercreds[0].comment,
1816 usercreds[0].domain,
1817 usercreds[0].username,
1818 usercreds[0].password,
1819 usercreds[0].parameter_control,
1820 usercreds[0].expected_interactive_error)) {
1824 if (usercreds[0].network_login) {
1825 if (!test_SamLogon(p, mem_ctx, creds,
1826 usercreds[0].comment,
1827 usercreds[0].domain,
1828 usercreds[0].username,
1829 usercreds[0].password,
1830 usercreds[0].parameter_control,
1831 usercreds[0].expected_network_error,
1832 usercreds[0].old_password,
1841 talloc_free(mem_ctx);
1843 torture_leave_domain(join_ctx);
1844 torture_leave_domain(user_ctx);
1845 torture_leave_domain(user_ctx_wrong_wks);
1846 torture_leave_domain(user_ctx_wrong_time);