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