2 Unix SMB/CIFS implementation.
4 test suite for netlogon SamLogon operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_netlogon_c.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "auth/auth.h"
29 #include "lib/crypto/crypto.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/rpc.h"
32 #include "auth/gensec/schannel_proto.h"
33 #include "auth/gensec/gensec.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "param/param.h"
37 #define TEST_MACHINE_NAME "samlogontest"
38 #define TEST_USER_NAME "samlogontestuser"
39 #define TEST_USER_NAME_WRONG_WKS "samlogontest2"
40 #define TEST_USER_NAME_WRONG_TIME "samlogontest3"
51 struct samlogon_state {
54 const char *account_name;
55 const char *account_domain;
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 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 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 !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 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 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 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 !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 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, samlogon_state->iconv_convenience, TEST_MACHINE_NAME, lp_workgroup(global_loadparm));
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->iconv_convenience, lp_netbios_name(global_loadparm), lp_workgroup(global_loadparm));
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);
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 samlogon_state->iconv_convenience,
1164 &unicodepw, samlogon_state->password)) == -1) {
1165 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1169 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1171 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1173 if ((convert_string_talloc(samlogon_state->mem_ctx,
1174 samlogon_state->iconv_convenience,
1176 password, strlen(password)+1,
1177 (void**)&dospw)) == -1) {
1178 DEBUG(0, ("convert_string_talloc failed!\n"));
1182 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1184 nt_status = check_samlogon(samlogon_state,
1186 samlogon_state->parameter_control | MSV1_0_CLEARTEXT_PASSWORD_ALLOWED,
1194 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1195 /* for 'old' passwords, we allow the server to be OK or wrong password */
1196 if (samlogon_state->old_password) {
1199 /* for 'long' passwords, the LM password is invalid */
1200 if (break_which == NO_NT && !lm_good) {
1203 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
1204 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
1205 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
1206 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1207 SAFE_FREE(*error_string);
1208 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1210 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1212 } else if (!NT_STATUS_IS_OK(nt_status)) {
1216 if (break_which == NO_NT && !lm_good) {
1217 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1224 static bool test_plaintext_none_broken(struct samlogon_state *samlogon_state,
1225 char **error_string) {
1226 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1229 static bool test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
1230 char **error_string) {
1231 return test_plaintext(samlogon_state, BREAK_LM, error_string);
1234 static bool test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
1235 char **error_string) {
1236 return test_plaintext(samlogon_state, BREAK_NT, error_string);
1239 static bool test_plaintext_nt_only(struct samlogon_state *samlogon_state,
1240 char **error_string) {
1241 return test_plaintext(samlogon_state, NO_LM, error_string);
1244 static bool test_plaintext_lm_only(struct samlogon_state *samlogon_state,
1245 char **error_string) {
1246 return test_plaintext(samlogon_state, NO_NT, error_string);
1260 - plaintext tests (in challenge-response fields)
1262 check we get the correct session key in each case
1263 check what values we get for the LM session key
1267 static const struct ntlm_tests {
1268 bool (*fn)(struct samlogon_state *, char **);
1272 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", false},
1274 {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", false},
1276 {test_lm, "LM", false},
1277 {test_lm_ntlm, "LM and NTLM", false},
1278 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", false},
1279 {test_ntlm, "NTLM", false},
1280 {test_ntlm_in_lm, "NTLM in LM", false},
1281 {test_ntlm_in_both, "NTLM in both", false},
1282 {test_ntlmv2, "NTLMv2", false},
1283 {test_ntlmv2_no_dom, "NTLMv2 (no domain)", false},
1284 {test_lmv2, "LMv2", false},
1285 {test_lmv2_no_dom, "LMv2 (no domain)", false},
1286 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", false},
1287 {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", false},
1288 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", false},
1290 {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", false},
1292 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", false},
1293 {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", false},
1294 {test_ntlm_lm_broken, "NTLM and LM, LM broken", false},
1295 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", false},
1296 {test_ntlm2, "NTLM2 (NTLMv2 session security)", false},
1297 {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", false},
1298 {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", false},
1299 {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", false},
1300 {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", false},
1301 {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", false},
1302 {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", false},
1303 {test_plaintext_none_broken, "Plaintext", false},
1304 {test_plaintext_lm_broken, "Plaintext LM broken", false},
1305 {test_plaintext_nt_broken, "Plaintext NT broken", false},
1306 {test_plaintext_nt_only, "Plaintext NT only", false},
1307 {test_plaintext_lm_only, "Plaintext LM only", false},
1312 try a netlogon SamLogon
1314 static bool test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1315 struct torture_context *tctx,
1316 struct creds_CredentialState *creds,
1317 const char *comment,
1318 const char *account_domain, const char *account_name,
1319 const char *plain_pass, uint32_t parameter_control,
1320 NTSTATUS expected_error, bool old_password,
1323 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1326 int validation_levels[] = {2,3,6};
1327 int logon_levels[] = { 2, 6 };
1328 int function_levels[] = {
1329 NDR_NETR_LOGONSAMLOGON,
1330 NDR_NETR_LOGONSAMLOGONEX,
1331 NDR_NETR_LOGONSAMLOGONWITHFLAGS };
1332 struct samlogon_state samlogon_state;
1334 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1336 samlogon_state.comment = comment;
1337 samlogon_state.account_name = account_name;
1338 samlogon_state.account_domain = account_domain;
1339 samlogon_state.password = plain_pass;
1340 samlogon_state.p = p;
1341 samlogon_state.creds = creds;
1342 samlogon_state.expected_error = expected_error;
1343 samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1344 samlogon_state.parameter_control = parameter_control;
1345 samlogon_state.old_password = old_password;
1346 samlogon_state.iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
1348 generate_random_buffer(samlogon_state.chall.data, 8);
1349 samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1350 samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
1351 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1352 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1353 samlogon_state.r_flags.in.flags = 0;
1355 samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1356 samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
1357 samlogon_state.r_ex.in.flags = 0;
1359 samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1360 samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
1361 samlogon_state.r.in.credential = &samlogon_state.auth;
1362 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1364 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1365 for (i=0; test_table[i].fn; i++) {
1366 if (n_subtests && (i > n_subtests)) {
1369 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1370 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1371 char *error_string = NULL;
1372 TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1373 samlogon_state.mem_ctx = tmp_ctx;
1374 samlogon_state.function_level = function_levels[f];
1375 samlogon_state.r.in.validation_level = validation_levels[v];
1376 samlogon_state.r.in.logon_level = logon_levels[l];
1377 samlogon_state.r_ex.in.validation_level = validation_levels[v];
1378 samlogon_state.r_ex.in.logon_level = logon_levels[l];
1379 samlogon_state.r_flags.in.validation_level = validation_levels[v];
1380 samlogon_state.r_flags.in.logon_level = logon_levels[l];
1381 if (!test_table[i].fn(&samlogon_state, &error_string)) {
1382 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1383 samlogon_state.comment,
1384 samlogon_state.account_domain,
1385 samlogon_state.account_name,
1386 test_table[i].name, validation_levels[v],
1387 logon_levels[l], function_levels[f]);
1389 if (test_table[i].expect_fail) {
1390 d_printf(" failed (expected, test incomplete): %s\n", error_string);
1392 d_printf(" failed: %s\n", error_string);
1395 SAFE_FREE(error_string);
1397 talloc_free(tmp_ctx);
1402 talloc_free(fn_ctx);
1407 test an ADS style interactive domain logon
1409 bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1410 struct creds_CredentialState *creds,
1411 const char *comment,
1412 const char *workstation_name,
1413 const char *account_domain, const char *account_name,
1414 const char *plain_pass, uint32_t parameter_control,
1415 NTSTATUS expected_error)
1418 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1419 struct netr_LogonSamLogonWithFlags r;
1420 struct netr_Authenticator a, ra;
1421 struct netr_PasswordInfo pinfo;
1427 creds_client_authenticator(creds, &a);
1429 r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1430 r.in.computer_name = TEST_MACHINE_NAME;
1431 r.in.credential = &a;
1432 r.in.return_authenticator = &ra;
1433 r.in.logon_level = 5;
1434 r.in.logon.password = &pinfo;
1435 r.in.validation_level = 6;
1438 pinfo.identity_info.domain_name.string = account_domain;
1439 pinfo.identity_info.parameter_control = parameter_control;
1440 pinfo.identity_info.logon_id_low = 0;
1441 pinfo.identity_info.logon_id_high = 0;
1442 pinfo.identity_info.account_name.string = account_name;
1443 pinfo.identity_info.workstation.string = workstation_name;
1445 if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1446 ZERO_STRUCT(pinfo.lmpassword.hash);
1448 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1450 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1451 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1452 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1454 creds_des_encrypt(creds, &pinfo.lmpassword);
1455 creds_des_encrypt(creds, &pinfo.ntpassword);
1458 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
1460 status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1461 if (!r.out.return_authenticator
1462 || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1463 d_printf("Credential chaining failed\n");
1464 talloc_free(fn_ctx);
1468 talloc_free(fn_ctx);
1470 if (!NT_STATUS_EQUAL(expected_error, status)) {
1471 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1472 account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
1481 bool torture_rpc_samlogon(struct torture_context *torture)
1484 struct dcerpc_pipe *p;
1485 struct dcerpc_binding *b;
1486 struct cli_credentials *machine_credentials;
1487 TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1489 struct test_join *join_ctx = NULL;
1490 struct test_join *user_ctx = NULL, *user_ctx_wrong_wks = NULL, *user_ctx_wrong_time = NULL;
1491 char *user_password, *user_password_wrong_wks, *user_password_wrong_time;
1492 const char *old_user_password;
1493 char *test_machine_account;
1494 const char *userdomain;
1495 struct samr_SetUserInfo s;
1496 union samr_UserInfo u;
1500 unsigned int credential_flags[] = {
1501 NETLOGON_NEG_AUTH2_FLAGS,
1502 NETLOGON_NEG_ARCFOUR,
1503 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1504 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1505 0 /* yes, this is a valid flag, causes the use of DES */
1508 struct creds_CredentialState *creds;
1510 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1511 /* We only need to join as a workstation here, and in future,
1512 * if we wish to test against trusted domains, we must be a
1513 * workstation here */
1514 join_ctx = torture_join_domain(torture, TEST_MACHINE_NAME, ACB_WSTRUST,
1515 &machine_credentials);
1517 d_printf("Failed to join as Workstation\n");
1521 userdomain = torture_setting_string(torture, "userdomain", lp_workgroup(torture->lp_ctx));
1523 user_ctx = torture_create_testuser(torture,
1527 (const char **)&user_password);
1529 d_printf("Failed to create a test user\n");
1533 old_user_password = user_password;
1535 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
1536 TEST_USER_NAME, 16 /* > 14 */, &user_password,
1539 user_ctx_wrong_wks = torture_create_testuser(torture,
1540 TEST_USER_NAME_WRONG_WKS,
1543 (const char **)&user_password_wrong_wks);
1544 if (!user_ctx_wrong_wks) {
1545 d_printf("Failed to create a test user (wrong workstation test)\n");
1550 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_wks);
1554 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS;
1555 u.info21.workstations.string = "not" TEST_MACHINE_NAME;
1557 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks), mem_ctx, &s);
1558 if (!NT_STATUS_IS_OK(status)) {
1559 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status));
1565 = torture_create_testuser(torture, TEST_USER_NAME_WRONG_TIME,
1568 (const char **)&user_password_wrong_time);
1569 if (!user_ctx_wrong_time) {
1570 d_printf("Failed to create a test user (wrong workstation test)\n");
1575 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_time);
1579 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS | SAMR_FIELD_LOGON_HOURS;
1580 u.info21.workstations.string = TEST_MACHINE_NAME;
1581 u.info21.logon_hours.units_per_week = 168;
1582 u.info21.logon_hours.bits = talloc_zero_array(mem_ctx, uint8_t, 168);
1584 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time), mem_ctx, &s);
1585 if (!NT_STATUS_IS_OK(status)) {
1586 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status));
1591 status = torture_rpc_binding(torture, &b);
1592 if (!NT_STATUS_IS_OK(status)) {
1597 /* We have to use schannel, otherwise the SamLogonEx fails
1598 * with INTERNAL_ERROR */
1600 b->flags &= ~DCERPC_AUTH_OPTIONS;
1601 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1603 status = dcerpc_pipe_connect_b(mem_ctx, &p, b,
1604 &ndr_table_netlogon,
1605 machine_credentials, torture->ev, torture->lp_ctx);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1613 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1614 if (!NT_STATUS_IS_OK(status)) {
1622 const char *comment;
1624 const char *username;
1625 const char *password;
1627 NTSTATUS expected_interactive_error;
1628 NTSTATUS expected_network_error;
1629 uint32_t parameter_control;
1630 bool old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1633 .comment = "domain\\user",
1634 .domain = cli_credentials_get_domain(cmdline_credentials),
1635 .username = cli_credentials_get_username(cmdline_credentials),
1636 .password = cli_credentials_get_password(cmdline_credentials),
1637 .network_login = true,
1638 .expected_interactive_error = NT_STATUS_OK,
1639 .expected_network_error = NT_STATUS_OK
1642 .comment = "realm\\user",
1643 .domain = cli_credentials_get_realm(cmdline_credentials),
1644 .username = cli_credentials_get_username(cmdline_credentials),
1645 .password = cli_credentials_get_password(cmdline_credentials),
1646 .network_login = true,
1647 .expected_interactive_error = NT_STATUS_OK,
1648 .expected_network_error = NT_STATUS_OK
1651 .comment = "user@domain",
1653 .username = talloc_asprintf(mem_ctx,
1655 cli_credentials_get_username(cmdline_credentials),
1656 cli_credentials_get_domain(cmdline_credentials)
1658 .password = cli_credentials_get_password(cmdline_credentials),
1659 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1660 .expected_interactive_error = NT_STATUS_OK,
1661 .expected_network_error = NT_STATUS_OK
1664 .comment = "user@realm",
1666 .username = talloc_asprintf(mem_ctx,
1668 cli_credentials_get_username(cmdline_credentials),
1669 cli_credentials_get_realm(cmdline_credentials)
1671 .password = cli_credentials_get_password(cmdline_credentials),
1672 .network_login = true,
1673 .expected_interactive_error = NT_STATUS_OK,
1674 .expected_network_error = NT_STATUS_OK
1677 .comment = "machine domain\\user",
1678 .domain = cli_credentials_get_domain(machine_credentials),
1679 .username = cli_credentials_get_username(machine_credentials),
1680 .password = cli_credentials_get_password(machine_credentials),
1681 .network_login = true,
1682 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1683 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1686 .comment = "machine domain\\user",
1687 .domain = cli_credentials_get_domain(machine_credentials),
1688 .username = cli_credentials_get_username(machine_credentials),
1689 .password = cli_credentials_get_password(machine_credentials),
1690 .network_login = true,
1691 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1692 .expected_network_error = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1695 .comment = "machine realm\\user",
1696 .domain = cli_credentials_get_realm(machine_credentials),
1697 .username = cli_credentials_get_username(machine_credentials),
1698 .password = cli_credentials_get_password(machine_credentials),
1699 .network_login = true,
1700 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1701 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1704 .comment = "machine user@domain",
1706 .username = talloc_asprintf(mem_ctx,
1708 cli_credentials_get_username(machine_credentials),
1709 cli_credentials_get_domain(machine_credentials)
1711 .password = cli_credentials_get_password(machine_credentials),
1712 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1713 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1714 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1717 .comment = "machine user@realm",
1719 .username = talloc_asprintf(mem_ctx,
1721 cli_credentials_get_username(machine_credentials),
1722 cli_credentials_get_realm(machine_credentials)
1724 .password = cli_credentials_get_password(machine_credentials),
1725 .network_login = true,
1726 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1727 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1730 .comment = "test user (long pw): domain\\user",
1731 .domain = userdomain,
1732 .username = TEST_USER_NAME,
1733 .password = user_password,
1734 .network_login = true,
1735 .expected_interactive_error = NT_STATUS_OK,
1736 .expected_network_error = NT_STATUS_OK
1739 .comment = "test user (long pw): user@realm",
1741 .username = talloc_asprintf(mem_ctx,
1744 lp_realm(torture->lp_ctx)),
1745 .password = user_password,
1746 .network_login = true,
1747 .expected_interactive_error = NT_STATUS_OK,
1748 .expected_network_error = NT_STATUS_OK
1751 .comment = "test user (long pw): user@domain",
1753 .username = talloc_asprintf(mem_ctx,
1757 .password = user_password,
1758 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1759 .expected_interactive_error = NT_STATUS_OK,
1760 .expected_network_error = NT_STATUS_OK
1762 /* Oddball, can we use the old password ? */
1764 .comment = "test user: user\\domain OLD PASSWORD",
1765 .domain = userdomain,
1766 .username = TEST_USER_NAME,
1767 .password = old_user_password,
1768 .network_login = true,
1769 .expected_interactive_error = NT_STATUS_WRONG_PASSWORD,
1770 .expected_network_error = NT_STATUS_OK,
1771 .old_password = true
1774 .comment = "test user (wong workstation): domain\\user",
1775 .domain = userdomain,
1776 .username = TEST_USER_NAME_WRONG_WKS,
1777 .password = user_password_wrong_wks,
1778 .network_login = true,
1779 .expected_interactive_error = NT_STATUS_INVALID_WORKSTATION,
1780 .expected_network_error = NT_STATUS_INVALID_WORKSTATION
1784 /* Try all the tests for different username forms */
1785 for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1787 if (!test_InteractiveLogon(p, mem_ctx, creds,
1788 usercreds[ci].comment,
1790 usercreds[ci].domain,
1791 usercreds[ci].username,
1792 usercreds[ci].password,
1793 usercreds[ci].parameter_control,
1794 usercreds[ci].expected_interactive_error)) {
1798 if (usercreds[ci].network_login) {
1799 if (!test_SamLogon(p, mem_ctx, torture, creds,
1800 usercreds[ci].comment,
1801 usercreds[ci].domain,
1802 usercreds[ci].username,
1803 usercreds[ci].password,
1804 usercreds[ci].parameter_control,
1805 usercreds[ci].expected_network_error,
1806 usercreds[ci].old_password,
1813 /* Using the first username form, try the different
1814 * credentials flag setups, on only one of the tests (checks
1815 * session key encryption) */
1817 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1818 /* TODO: Somehow we lost setting up the different credential flags here! */
1820 if (!test_InteractiveLogon(p, mem_ctx, creds,
1821 usercreds[0].comment,
1823 usercreds[0].domain,
1824 usercreds[0].username,
1825 usercreds[0].password,
1826 usercreds[0].parameter_control,
1827 usercreds[0].expected_interactive_error)) {
1831 if (usercreds[0].network_login) {
1832 if (!test_SamLogon(p, mem_ctx, torture, creds,
1833 usercreds[0].comment,
1834 usercreds[0].domain,
1835 usercreds[0].username,
1836 usercreds[0].password,
1837 usercreds[0].parameter_control,
1838 usercreds[0].expected_network_error,
1839 usercreds[0].old_password,
1848 talloc_free(mem_ctx);
1850 torture_leave_domain(join_ctx);
1851 torture_leave_domain(user_ctx);
1852 torture_leave_domain(user_ctx_wrong_wks);
1853 torture_leave_domain(user_ctx_wrong_time);