2 Unix SMB/CIFS implementation.
4 test suite for netlogon SamLogon operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_netlogon_c.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "auth/auth.h"
29 #include "lib/crypto/crypto.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/rpc.h"
32 #include "auth/gensec/schannel_proto.h"
33 #include "libcli/auth/libcli_auth.h"
34 #include "param/param.h"
36 #define TEST_MACHINE_NAME "samlogontest"
37 #define TEST_USER_NAME "samlogontestuser"
38 #define TEST_USER_NAME_WRONG_WKS "samlogontest2"
39 #define TEST_USER_NAME_WRONG_TIME "samlogontest3"
50 struct samlogon_state {
53 const char *account_name;
54 const char *account_domain;
56 struct dcerpc_pipe *p;
58 uint32_t parameter_control;
59 struct netr_LogonSamLogon r;
60 struct netr_LogonSamLogonEx r_ex;
61 struct netr_LogonSamLogonWithFlags r_flags;
62 struct netr_Authenticator auth, auth2;
63 struct creds_CredentialState *creds;
64 NTSTATUS expected_error;
65 bool old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
70 Authenticate a user with a challenge/response, checking session key
71 and valid authentication types
73 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
74 enum ntlm_break break_which,
75 uint32_t parameter_control,
77 DATA_BLOB *lm_response,
78 DATA_BLOB *nt_response,
80 uint8_t user_session_key[16],
84 struct netr_LogonSamLogon *r = &samlogon_state->r;
85 struct netr_LogonSamLogonEx *r_ex = &samlogon_state->r_ex;
86 struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
87 struct netr_NetworkInfo ninfo;
88 struct netr_SamBaseInfo *base = NULL;
89 uint16_t validation_level = 0;
91 samlogon_state->r.in.logon.network = &ninfo;
92 samlogon_state->r_ex.in.logon.network = &ninfo;
93 samlogon_state->r_flags.in.logon.network = &ninfo;
95 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
96 ninfo.identity_info.parameter_control = parameter_control;
97 ninfo.identity_info.logon_id_low = 0;
98 ninfo.identity_info.logon_id_high = 0;
99 ninfo.identity_info.account_name.string = samlogon_state->account_name;
100 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
102 memcpy(ninfo.challenge, chall->data, 8);
104 switch (break_which) {
108 if (lm_response && lm_response->data) {
109 lm_response->data[0]++;
113 if (nt_response && nt_response->data) {
114 nt_response->data[0]++;
118 if (lm_response && lm_response->data) {
119 lm_response->data[0]++;
121 if (nt_response && nt_response->data) {
122 nt_response->data[0]++;
126 data_blob_free(lm_response);
129 data_blob_free(nt_response);
134 ninfo.nt.data = nt_response->data;
135 ninfo.nt.length = nt_response->length;
137 ninfo.nt.data = NULL;
142 ninfo.lm.data = lm_response->data;
143 ninfo.lm.length = lm_response->length;
145 ninfo.lm.data = NULL;
149 switch (samlogon_state->function_level) {
150 case NDR_NETR_LOGONSAMLOGON:
151 ZERO_STRUCT(samlogon_state->auth2);
152 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
154 r->out.return_authenticator = NULL;
155 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
156 if (!r->out.return_authenticator ||
157 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
158 d_printf("Credential chaining failed\n");
160 if (!NT_STATUS_IS_OK(status)) {
162 *error_string = strdup(nt_errstr(status));
167 validation_level = r->in.validation_level;
169 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r->out.validation);
171 switch (validation_level) {
173 base = &r->out.validation.sam2->base;
176 base = &r->out.validation.sam3->base;
179 base = &r->out.validation.sam6->base;
183 case NDR_NETR_LOGONSAMLOGONEX:
184 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
185 if (!NT_STATUS_IS_OK(status)) {
187 *error_string = strdup(nt_errstr(status));
192 validation_level = r_ex->in.validation_level;
194 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_ex->out.validation);
196 switch (validation_level) {
198 base = &r_ex->out.validation.sam2->base;
201 base = &r_ex->out.validation.sam3->base;
204 base = &r_ex->out.validation.sam6->base;
208 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
209 ZERO_STRUCT(samlogon_state->auth2);
210 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
212 r_flags->out.return_authenticator = NULL;
213 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
214 if (!r_flags->out.return_authenticator ||
215 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
216 d_printf("Credential chaining failed\n");
218 if (!NT_STATUS_IS_OK(status)) {
220 *error_string = strdup(nt_errstr(status));
225 validation_level = r_flags->in.validation_level;
227 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_flags->out.validation);
229 switch (validation_level) {
231 base = &r_flags->out.validation.sam2->base;
234 base = &r_flags->out.validation.sam3->base;
237 base = &r_flags->out.validation.sam6->base;
243 return NT_STATUS_INVALID_PARAMETER;
247 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
248 return NT_STATUS_INVALID_PARAMETER;
251 if (user_session_key) {
252 memcpy(user_session_key, base->key.key, 16);
255 memcpy(lm_key, base->LMSessKey.key, 8);
263 * Test the normal 'LM and NTLM' combination
266 static bool test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
271 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
272 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
273 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
276 uint8_t user_session_key[16];
281 ZERO_STRUCT(user_session_key);
283 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
285 ZERO_STRUCT(lm_hash);
287 E_deshash(samlogon_state->password, lm_hash);
290 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
292 E_md4hash(samlogon_state->password, nt_hash);
293 SMBsesskeygen_ntv1(nt_hash, session_key.data);
295 nt_status = check_samlogon(samlogon_state,
297 samlogon_state->parameter_control,
298 &samlogon_state->chall,
305 data_blob_free(&lm_response);
307 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
308 /* for 'long' passwords, the LM password is invalid */
309 if (break_which == NO_NT && !lm_good) {
312 /* for 'old' passwords, we allow the server to be OK or wrong password */
313 if (samlogon_state->old_password) {
316 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
317 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
318 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
319 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
320 SAFE_FREE(*error_string);
321 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
323 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
325 } else if (!NT_STATUS_IS_OK(nt_status)) {
329 if (break_which == NO_NT && !lm_good) {
330 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
334 if (memcmp(lm_hash, lm_key,
335 sizeof(lm_key)) != 0) {
336 d_printf("LM Key does not match expectations!\n");
337 d_printf("lm_key:\n");
338 dump_data(1, lm_key, 8);
339 d_printf("expected:\n");
340 dump_data(1, lm_hash, 8);
344 switch (break_which) {
347 uint8_t lm_key_expected[16];
348 memcpy(lm_key_expected, lm_hash, 8);
349 memset(lm_key_expected+8, '\0', 8);
350 if (memcmp(lm_key_expected, user_session_key,
352 *error_string = strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
353 d_printf("user_session_key:\n");
354 dump_data(1, user_session_key, sizeof(user_session_key));
355 d_printf("expected:\n");
356 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
362 if (memcmp(session_key.data, user_session_key,
363 sizeof(user_session_key)) != 0) {
364 *error_string = strdup("NT Session Key does not match expectations!\n");
365 d_printf("user_session_key:\n");
366 dump_data(1, user_session_key, 16);
367 d_printf("expected:\n");
368 dump_data(1, session_key.data, session_key.length);
376 * Test LM authentication, no NT response supplied
379 static bool test_lm(struct samlogon_state *samlogon_state, char **error_string)
382 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
386 * Test the NTLM response only, no LM.
389 static bool test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
391 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
395 * Test the NTLM response only, but in the LM field.
398 static bool test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
403 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
404 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
408 uint8_t user_session_key[16];
412 ZERO_STRUCT(user_session_key);
414 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
416 E_md4hash(samlogon_state->password, nt_hash);
417 SMBsesskeygen_ntv1(nt_hash,
420 lm_good = E_deshash(samlogon_state->password, lm_hash);
422 ZERO_STRUCT(lm_hash);
424 nt_status = check_samlogon(samlogon_state,
426 samlogon_state->parameter_control,
427 &samlogon_state->chall,
434 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
435 /* for 'old' passwords, we allow the server to be OK or wrong password */
436 if (samlogon_state->old_password) {
440 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
441 SAFE_FREE(*error_string);
442 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
444 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
446 } else if (!NT_STATUS_IS_OK(nt_status)) {
451 if (memcmp(lm_hash, lm_key,
452 sizeof(lm_key)) != 0) {
453 d_printf("LM Key does not match expectations!\n");
454 d_printf("lm_key:\n");
455 dump_data(1, lm_key, 8);
456 d_printf("expected:\n");
457 dump_data(1, lm_hash, 8);
462 if (memcmp(session_key.data, lm_key,
463 sizeof(lm_key)) != 0) {
464 d_printf("LM Key does not match expectations (first 8 session key)!\n");
465 d_printf("lm_key:\n");
466 dump_data(1, lm_key, 8);
467 d_printf("expected:\n");
468 dump_data(1, session_key.data, 8);
473 if (lm_good && memcmp(lm_hash, user_session_key, 8) != 0) {
474 uint8_t lm_key_expected[16];
475 memcpy(lm_key_expected, lm_hash, 8);
476 memset(lm_key_expected+8, '\0', 8);
477 if (memcmp(lm_key_expected, user_session_key,
479 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
480 d_printf("user_session_key:\n");
481 dump_data(1, user_session_key, sizeof(user_session_key));
482 d_printf("expected:\n");
483 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
491 * Test the NTLM response only, but in the both the NT and LM fields.
494 static bool test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
499 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
500 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
504 uint8_t user_session_key[16];
508 ZERO_STRUCT(user_session_key);
510 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
512 E_md4hash(samlogon_state->password, nt_hash);
513 SMBsesskeygen_ntv1(nt_hash,
516 lm_good = E_deshash(samlogon_state->password, lm_hash);
518 ZERO_STRUCT(lm_hash);
521 nt_status = check_samlogon(samlogon_state,
523 samlogon_state->parameter_control,
524 &samlogon_state->chall,
531 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
532 /* for 'old' passwords, we allow the server to be OK or wrong password */
533 if (samlogon_state->old_password) {
537 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
538 SAFE_FREE(*error_string);
539 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
541 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
543 } else if (!NT_STATUS_IS_OK(nt_status)) {
547 if (!NT_STATUS_IS_OK(nt_status)) {
551 if (memcmp(lm_hash, lm_key,
552 sizeof(lm_key)) != 0) {
553 d_printf("LM Key does not match expectations!\n");
554 d_printf("lm_key:\n");
555 dump_data(1, lm_key, 8);
556 d_printf("expected:\n");
557 dump_data(1, lm_hash, 8);
560 if (memcmp(session_key.data, user_session_key,
561 sizeof(user_session_key)) != 0) {
562 d_printf("NT Session Key does not match expectations!\n");
563 d_printf("user_session_key:\n");
564 dump_data(1, user_session_key, 16);
565 d_printf("expected:\n");
566 dump_data(1, session_key.data, session_key.length);
575 * Test the NTLMv2 and LMv2 responses
583 static bool test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state,
584 enum ntlm_break break_which,
585 enum ntlmv2_domain ntlmv2_domain,
590 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
591 DATA_BLOB lmv2_response = data_blob(NULL, 0);
592 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
593 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
594 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, TEST_MACHINE_NAME, lp_workgroup(global_loadparm));
596 uint8_t lm_session_key[8];
597 uint8_t user_session_key[16];
599 ZERO_STRUCT(lm_session_key);
600 ZERO_STRUCT(user_session_key);
602 switch (ntlmv2_domain) {
604 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
605 samlogon_state->account_name, samlogon_state->account_domain,
606 samlogon_state->password, &samlogon_state->chall,
608 &lmv2_response, &ntlmv2_response,
609 &lmv2_session_key, &ntlmv2_session_key)) {
610 data_blob_free(&names_blob);
615 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
616 samlogon_state->account_name, "",
617 samlogon_state->password, &samlogon_state->chall,
619 &lmv2_response, &ntlmv2_response,
620 &lmv2_session_key, &ntlmv2_session_key)) {
621 data_blob_free(&names_blob);
626 data_blob_free(&names_blob);
628 nt_status = check_samlogon(samlogon_state,
630 samlogon_state->parameter_control,
631 &samlogon_state->chall,
638 data_blob_free(&lmv2_response);
639 data_blob_free(&ntlmv2_response);
642 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
643 /* for 'old' passwords, we allow the server to be OK or wrong password */
644 if (samlogon_state->old_password) {
647 return break_which == BREAK_BOTH;
648 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
649 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
650 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
651 SAFE_FREE(*error_string);
652 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
654 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
656 } else if (!NT_STATUS_IS_OK(nt_status)) {
661 switch (break_which) {
663 if (memcmp(lmv2_session_key.data, user_session_key,
664 sizeof(user_session_key)) != 0) {
665 d_printf("USER (LMv2) Session Key does not match expectations!\n");
666 d_printf("user_session_key:\n");
667 dump_data(1, user_session_key, 16);
668 d_printf("expected:\n");
669 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
672 if (memcmp(lmv2_session_key.data, lm_session_key,
673 sizeof(lm_session_key)) != 0) {
674 d_printf("LM (LMv2) Session Key does not match expectations!\n");
675 d_printf("lm_session_key:\n");
676 dump_data(1, lm_session_key, 8);
677 d_printf("expected:\n");
678 dump_data(1, lmv2_session_key.data, 8);
683 if (memcmp(ntlmv2_session_key.data, user_session_key,
684 sizeof(user_session_key)) != 0) {
685 if (memcmp(lmv2_session_key.data, user_session_key,
686 sizeof(user_session_key)) == 0) {
687 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
688 d_printf("user_session_key:\n");
689 dump_data(1, user_session_key, 16);
690 d_printf("expected:\n");
691 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
695 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
696 d_printf("user_session_key:\n");
697 dump_data(1, user_session_key, 16);
698 d_printf("expected:\n");
699 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
703 if (memcmp(ntlmv2_session_key.data, lm_session_key,
704 sizeof(lm_session_key)) != 0) {
705 if (memcmp(lmv2_session_key.data, lm_session_key,
706 sizeof(lm_session_key)) == 0) {
707 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
708 d_printf("user_session_key:\n");
709 dump_data(1, lm_session_key, 8);
710 d_printf("expected:\n");
711 dump_data(1, ntlmv2_session_key.data, 8);
714 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
715 d_printf("lm_session_key:\n");
716 dump_data(1, lm_session_key, 8);
717 d_printf("expected:\n");
718 dump_data(1, ntlmv2_session_key.data, 8);
728 * Test the NTLM and LMv2 responses
731 static bool test_lmv2_ntlm_broken(struct samlogon_state *samlogon_state,
732 enum ntlm_break break_which,
733 enum ntlmv2_domain ntlmv2_domain,
738 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
739 DATA_BLOB lmv2_response = data_blob(NULL, 0);
740 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
741 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
742 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(global_loadparm), lp_workgroup(global_loadparm));
744 DATA_BLOB ntlm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
745 DATA_BLOB ntlm_session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
749 uint8_t lm_session_key[8];
750 uint8_t user_session_key[16];
753 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
755 E_md4hash(samlogon_state->password, nt_hash);
756 SMBsesskeygen_ntv1(nt_hash,
757 ntlm_session_key.data);
759 lm_good = E_deshash(samlogon_state->password, lm_hash);
761 ZERO_STRUCT(lm_hash);
764 ZERO_STRUCT(lm_session_key);
765 ZERO_STRUCT(user_session_key);
767 switch (ntlmv2_domain) {
769 /* TODO - test with various domain cases, and without domain */
770 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
771 samlogon_state->account_name, samlogon_state->account_domain,
772 samlogon_state->password, &samlogon_state->chall,
774 &lmv2_response, &ntlmv2_response,
775 &lmv2_session_key, &ntlmv2_session_key)) {
776 data_blob_free(&names_blob);
781 /* TODO - test with various domain cases, and without domain */
782 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
783 samlogon_state->account_name, "",
784 samlogon_state->password, &samlogon_state->chall,
786 &lmv2_response, &ntlmv2_response,
787 &lmv2_session_key, &ntlmv2_session_key)) {
788 data_blob_free(&names_blob);
794 data_blob_free(&names_blob);
796 nt_status = check_samlogon(samlogon_state,
798 samlogon_state->parameter_control,
799 &samlogon_state->chall,
806 data_blob_free(&lmv2_response);
807 data_blob_free(&ntlmv2_response);
810 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
811 /* for 'old' passwords, we allow the server to be OK or wrong password */
812 if (samlogon_state->old_password) {
815 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
816 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
817 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
818 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
819 SAFE_FREE(*error_string);
820 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
822 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
824 } else if (!NT_STATUS_IS_OK(nt_status)) {
828 switch (break_which) {
830 if (memcmp(lmv2_session_key.data, user_session_key,
831 sizeof(user_session_key)) != 0) {
832 d_printf("USER (LMv2) Session Key does not match expectations!\n");
833 d_printf("user_session_key:\n");
834 dump_data(1, user_session_key, 16);
835 d_printf("expected:\n");
836 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
839 if (memcmp(lmv2_session_key.data, lm_session_key,
840 sizeof(lm_session_key)) != 0) {
841 d_printf("LM (LMv2) Session Key does not match expectations!\n");
842 d_printf("lm_session_key:\n");
843 dump_data(1, lm_session_key, 8);
844 d_printf("expected:\n");
845 dump_data(1, lmv2_session_key.data, 8);
850 if (memcmp(ntlm_session_key.data, user_session_key,
851 sizeof(user_session_key)) != 0) {
852 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
853 d_printf("user_session_key:\n");
854 dump_data(1, user_session_key, 16);
855 d_printf("expected:\n");
856 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
860 if (memcmp(lm_hash, lm_session_key,
861 sizeof(lm_session_key)) != 0) {
862 d_printf("LM Session Key does not match expectations!\n");
863 d_printf("lm_session_key:\n");
864 dump_data(1, lm_session_key, 8);
865 d_printf("expected:\n");
866 dump_data(1, lm_hash, 8);
870 static const uint8_t zeros[8];
871 if (memcmp(zeros, lm_session_key,
872 sizeof(lm_session_key)) != 0) {
873 d_printf("LM Session Key does not match expectations (zeros)!\n");
874 d_printf("lm_session_key:\n");
875 dump_data(1, lm_session_key, 8);
876 d_printf("expected:\n");
877 dump_data(1, zeros, 8);
883 if (memcmp(ntlm_session_key.data, user_session_key,
884 sizeof(user_session_key)) != 0) {
885 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
886 d_printf("user_session_key:\n");
887 dump_data(1, user_session_key, 16);
888 d_printf("expected:\n");
889 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
892 if (memcmp(ntlm_session_key.data, lm_session_key,
893 sizeof(lm_session_key)) != 0) {
894 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
895 d_printf("lm_session_key:\n");
896 dump_data(1, lm_session_key, 8);
897 d_printf("expected:\n");
898 dump_data(1, ntlm_session_key.data, 8);
907 * Test the NTLMv2 and LMv2 responses
910 static bool test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
912 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, UPPER_DOMAIN, error_string);
916 static bool test_lmv2_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
918 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, NO_DOMAIN, error_string);
923 * Test the LMv2 response only
926 static bool test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
928 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, UPPER_DOMAIN, error_string);
931 static bool test_lmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
933 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, NO_DOMAIN, error_string);
937 * Test the NTLMv2 response only
940 static bool test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
942 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, UPPER_DOMAIN, error_string);
945 static bool test_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
947 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, NO_DOMAIN, error_string);
950 static bool test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
952 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
955 static bool test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
957 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
960 static bool test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
962 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
965 static bool test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
967 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
969 static bool test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
971 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
974 static bool test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
976 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
979 static bool test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
981 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
985 static bool test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
987 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
991 static bool test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
993 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
996 static bool test_ntlmv2_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
998 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
1001 static bool test_lmv2_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
1003 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
1006 static bool test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1008 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
1011 static bool test_lmv2_ntlm_break_ntlm(struct samlogon_state *samlogon_state, char **error_string)
1013 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
1016 static bool test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1018 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
1021 static bool test_lmv2_ntlm_break_lm(struct samlogon_state *samlogon_state, char **error_string)
1023 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
1026 static bool test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1028 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
1032 * Test the NTLM2 response (extra challenge in LM feild)
1034 * This test is the same as the 'break LM' test, but checks that the
1035 * server implements NTLM2 session security in the right place
1036 * (NETLOGON is the wrong place).
1039 static bool test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
1043 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1044 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1048 uint8_t nt_hash[16];
1049 uint8_t lm_hash[16];
1051 uint8_t user_session_key[16];
1052 uint8_t expected_user_session_key[16];
1053 uint8_t session_nonce_hash[16];
1054 uint8_t client_chall[8];
1056 struct MD5Context md5_session_nonce_ctx;
1057 HMACMD5Context hmac_ctx;
1059 ZERO_STRUCT(user_session_key);
1060 ZERO_STRUCT(lm_key);
1061 generate_random_buffer(client_chall, 8);
1063 MD5Init(&md5_session_nonce_ctx);
1064 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
1065 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
1066 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1068 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
1069 lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
1070 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
1073 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
1075 memcpy(lm_response.data, session_nonce_hash, 8);
1076 memset(lm_response.data + 8, 0, 16);
1078 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
1079 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
1080 hmac_md5_update(client_chall, 8, &hmac_ctx);
1081 hmac_md5_final(expected_user_session_key, &hmac_ctx);
1083 nt_status = check_samlogon(samlogon_state,
1085 samlogon_state->parameter_control,
1086 &samlogon_state->chall,
1093 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1094 /* for 'old' passwords, we allow the server to be OK or wrong password */
1095 if (samlogon_state->old_password) {
1099 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1100 SAFE_FREE(*error_string);
1101 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1103 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1105 } else if (!NT_STATUS_IS_OK(nt_status)) {
1110 if (memcmp(lm_hash, lm_key,
1111 sizeof(lm_key)) != 0) {
1112 d_printf("LM Key does not match expectations!\n");
1113 d_printf("lm_key:\n");
1114 dump_data(1, lm_key, 8);
1115 d_printf("expected:\n");
1116 dump_data(1, lm_hash, 8);
1120 static const uint8_t zeros[8];
1121 if (memcmp(zeros, lm_key,
1122 sizeof(lm_key)) != 0) {
1123 d_printf("LM Session Key does not match expectations (zeros)!\n");
1124 d_printf("lm_key:\n");
1125 dump_data(1, lm_key, 8);
1126 d_printf("expected:\n");
1127 dump_data(1, zeros, 8);
1131 if (memcmp(nt_key, user_session_key, 16) != 0) {
1132 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1133 d_printf("user_session_key:\n");
1134 dump_data(1, user_session_key, sizeof(user_session_key));
1135 d_printf("expected:\n");
1136 dump_data(1, nt_key, sizeof(nt_key));
1142 static bool test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
1145 DATA_BLOB nt_response = data_blob(NULL, 0);
1146 DATA_BLOB lm_response = data_blob(NULL, 0);
1151 uint8_t user_session_key[16];
1153 uint8_t lm_hash[16];
1154 static const uint8_t zeros[8];
1155 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
1156 bool lm_good = E_deshash(samlogon_state->password, lm_hash);
1158 ZERO_STRUCT(user_session_key);
1160 if ((push_ucs2_talloc(samlogon_state->mem_ctx,
1161 lp_iconv_convenience(global_loadparm),
1162 &unicodepw, samlogon_state->password)) == -1) {
1163 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1167 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1169 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1171 if ((convert_string_talloc(samlogon_state->mem_ctx,
1172 lp_iconv_convenience(global_loadparm),
1174 password, strlen(password)+1,
1175 (void**)&dospw)) == -1) {
1176 DEBUG(0, ("convert_string_talloc failed!\n"));
1180 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1182 nt_status = check_samlogon(samlogon_state,
1184 samlogon_state->parameter_control | MSV1_0_CLEARTEXT_PASSWORD_ALLOWED,
1192 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1193 /* for 'old' passwords, we allow the server to be OK or wrong password */
1194 if (samlogon_state->old_password) {
1197 /* for 'long' passwords, the LM password is invalid */
1198 if (break_which == NO_NT && !lm_good) {
1201 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
1202 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
1203 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
1204 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1205 SAFE_FREE(*error_string);
1206 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1208 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1210 } else if (!NT_STATUS_IS_OK(nt_status)) {
1214 if (break_which == NO_NT && !lm_good) {
1215 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1222 static bool test_plaintext_none_broken(struct samlogon_state *samlogon_state,
1223 char **error_string) {
1224 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1227 static bool test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
1228 char **error_string) {
1229 return test_plaintext(samlogon_state, BREAK_LM, error_string);
1232 static bool test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
1233 char **error_string) {
1234 return test_plaintext(samlogon_state, BREAK_NT, error_string);
1237 static bool test_plaintext_nt_only(struct samlogon_state *samlogon_state,
1238 char **error_string) {
1239 return test_plaintext(samlogon_state, NO_LM, error_string);
1242 static bool test_plaintext_lm_only(struct samlogon_state *samlogon_state,
1243 char **error_string) {
1244 return test_plaintext(samlogon_state, NO_NT, error_string);
1258 - plaintext tests (in challenge-response fields)
1260 check we get the correct session key in each case
1261 check what values we get for the LM session key
1265 static const struct ntlm_tests {
1266 bool (*fn)(struct samlogon_state *, char **);
1270 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", false},
1272 {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", false},
1274 {test_lm, "LM", false},
1275 {test_lm_ntlm, "LM and NTLM", false},
1276 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", false},
1277 {test_ntlm, "NTLM", false},
1278 {test_ntlm_in_lm, "NTLM in LM", false},
1279 {test_ntlm_in_both, "NTLM in both", false},
1280 {test_ntlmv2, "NTLMv2", false},
1281 {test_ntlmv2_no_dom, "NTLMv2 (no domain)", false},
1282 {test_lmv2, "LMv2", false},
1283 {test_lmv2_no_dom, "LMv2 (no domain)", false},
1284 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", false},
1285 {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", false},
1286 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", false},
1288 {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", false},
1290 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", false},
1291 {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", false},
1292 {test_ntlm_lm_broken, "NTLM and LM, LM broken", false},
1293 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", false},
1294 {test_ntlm2, "NTLM2 (NTLMv2 session security)", false},
1295 {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", false},
1296 {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", false},
1297 {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", false},
1298 {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", false},
1299 {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", false},
1300 {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", false},
1301 {test_plaintext_none_broken, "Plaintext", false},
1302 {test_plaintext_lm_broken, "Plaintext LM broken", false},
1303 {test_plaintext_nt_broken, "Plaintext NT broken", false},
1304 {test_plaintext_nt_only, "Plaintext NT only", false},
1305 {test_plaintext_lm_only, "Plaintext LM only", false},
1310 try a netlogon SamLogon
1312 static bool test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1313 struct creds_CredentialState *creds,
1314 const char *comment,
1315 const char *account_domain, const char *account_name,
1316 const char *plain_pass, uint32_t parameter_control,
1317 NTSTATUS expected_error, bool old_password,
1320 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1323 int validation_levels[] = {2,3,6};
1324 int logon_levels[] = { 2, 6 };
1325 int function_levels[] = {
1326 NDR_NETR_LOGONSAMLOGON,
1327 NDR_NETR_LOGONSAMLOGONEX,
1328 NDR_NETR_LOGONSAMLOGONWITHFLAGS };
1329 struct samlogon_state samlogon_state;
1331 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1333 samlogon_state.comment = comment;
1334 samlogon_state.account_name = account_name;
1335 samlogon_state.account_domain = account_domain;
1336 samlogon_state.password = plain_pass;
1337 samlogon_state.p = p;
1338 samlogon_state.creds = creds;
1339 samlogon_state.expected_error = expected_error;
1340 samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1341 samlogon_state.parameter_control = parameter_control;
1342 samlogon_state.old_password = old_password;
1344 generate_random_buffer(samlogon_state.chall.data, 8);
1345 samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1346 samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
1347 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1348 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1349 samlogon_state.r_flags.in.flags = 0;
1351 samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1352 samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
1353 samlogon_state.r_ex.in.flags = 0;
1355 samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1356 samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
1357 samlogon_state.r.in.credential = &samlogon_state.auth;
1358 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1360 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1361 for (i=0; test_table[i].fn; i++) {
1362 if (n_subtests && (i > n_subtests)) {
1365 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1366 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1367 char *error_string = NULL;
1368 TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1369 samlogon_state.mem_ctx = tmp_ctx;
1370 samlogon_state.function_level = function_levels[f];
1371 samlogon_state.r.in.validation_level = validation_levels[v];
1372 samlogon_state.r.in.logon_level = logon_levels[l];
1373 samlogon_state.r_ex.in.validation_level = validation_levels[v];
1374 samlogon_state.r_ex.in.logon_level = logon_levels[l];
1375 samlogon_state.r_flags.in.validation_level = validation_levels[v];
1376 samlogon_state.r_flags.in.logon_level = logon_levels[l];
1377 if (!test_table[i].fn(&samlogon_state, &error_string)) {
1378 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1379 samlogon_state.comment,
1380 samlogon_state.account_domain,
1381 samlogon_state.account_name,
1382 test_table[i].name, validation_levels[v],
1383 logon_levels[l], function_levels[f]);
1385 if (test_table[i].expect_fail) {
1386 d_printf(" failed (expected, test incomplete): %s\n", error_string);
1388 d_printf(" failed: %s\n", error_string);
1391 SAFE_FREE(error_string);
1393 talloc_free(tmp_ctx);
1398 talloc_free(fn_ctx);
1403 test an ADS style interactive domain logon
1405 bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1406 struct creds_CredentialState *creds,
1407 const char *comment,
1408 const char *workstation_name,
1409 const char *account_domain, const char *account_name,
1410 const char *plain_pass, uint32_t parameter_control,
1411 NTSTATUS expected_error)
1414 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1415 struct netr_LogonSamLogonWithFlags r;
1416 struct netr_Authenticator a, ra;
1417 struct netr_PasswordInfo pinfo;
1423 creds_client_authenticator(creds, &a);
1425 r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1426 r.in.computer_name = TEST_MACHINE_NAME;
1427 r.in.credential = &a;
1428 r.in.return_authenticator = &ra;
1429 r.in.logon_level = 5;
1430 r.in.logon.password = &pinfo;
1431 r.in.validation_level = 6;
1434 pinfo.identity_info.domain_name.string = account_domain;
1435 pinfo.identity_info.parameter_control = parameter_control;
1436 pinfo.identity_info.logon_id_low = 0;
1437 pinfo.identity_info.logon_id_high = 0;
1438 pinfo.identity_info.account_name.string = account_name;
1439 pinfo.identity_info.workstation.string = workstation_name;
1441 if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1442 ZERO_STRUCT(pinfo.lmpassword.hash);
1444 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1446 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1447 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1448 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1450 creds_des_encrypt(creds, &pinfo.lmpassword);
1451 creds_des_encrypt(creds, &pinfo.ntpassword);
1454 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
1456 status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1457 if (!r.out.return_authenticator
1458 || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1459 d_printf("Credential chaining failed\n");
1460 talloc_free(fn_ctx);
1464 talloc_free(fn_ctx);
1466 if (!NT_STATUS_EQUAL(expected_error, status)) {
1467 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1468 account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
1477 bool torture_rpc_samlogon(struct torture_context *torture)
1480 struct dcerpc_pipe *p;
1481 struct dcerpc_binding *b;
1482 struct cli_credentials *machine_credentials;
1483 TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1485 struct test_join *join_ctx = NULL;
1486 struct test_join *user_ctx = NULL, *user_ctx_wrong_wks = NULL, *user_ctx_wrong_time = NULL;
1487 char *user_password, *user_password_wrong_wks, *user_password_wrong_time;
1488 const char *old_user_password;
1489 char *test_machine_account;
1490 const char *userdomain;
1491 struct samr_SetUserInfo s;
1492 union samr_UserInfo u;
1496 unsigned int credential_flags[] = {
1497 NETLOGON_NEG_AUTH2_FLAGS,
1498 NETLOGON_NEG_ARCFOUR,
1499 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1500 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1501 0 /* yes, this is a valid flag, causes the use of DES */
1504 struct creds_CredentialState *creds;
1506 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1507 /* We only need to join as a workstation here, and in future,
1508 * if we wish to test against trusted domains, we must be a
1509 * workstation here */
1510 join_ctx = torture_join_domain(torture, TEST_MACHINE_NAME, ACB_WSTRUST,
1511 &machine_credentials);
1513 d_printf("Failed to join as Workstation\n");
1517 userdomain = torture_setting_string(torture, "userdomain", lp_workgroup(torture->lp_ctx));
1519 user_ctx = torture_create_testuser(torture,
1523 (const char **)&user_password);
1525 d_printf("Failed to create a test user\n");
1529 old_user_password = user_password;
1531 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
1532 TEST_USER_NAME, 16 /* > 14 */, &user_password,
1535 user_ctx_wrong_wks = torture_create_testuser(torture,
1536 TEST_USER_NAME_WRONG_WKS,
1539 (const char **)&user_password_wrong_wks);
1540 if (!user_ctx_wrong_wks) {
1541 d_printf("Failed to create a test user (wrong workstation test)\n");
1546 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_wks);
1550 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS;
1551 u.info21.workstations.string = "not" TEST_MACHINE_NAME;
1553 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks), mem_ctx, &s);
1554 if (!NT_STATUS_IS_OK(status)) {
1555 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status));
1561 = torture_create_testuser(torture, TEST_USER_NAME_WRONG_TIME,
1564 (const char **)&user_password_wrong_time);
1565 if (!user_ctx_wrong_time) {
1566 d_printf("Failed to create a test user (wrong workstation test)\n");
1571 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_time);
1575 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS | SAMR_FIELD_LOGON_HOURS;
1576 u.info21.workstations.string = TEST_MACHINE_NAME;
1577 u.info21.logon_hours.units_per_week = 168;
1578 u.info21.logon_hours.bits = talloc_zero_array(mem_ctx, uint8_t, 168);
1580 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time), mem_ctx, &s);
1581 if (!NT_STATUS_IS_OK(status)) {
1582 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status));
1587 status = torture_rpc_binding(torture, &b);
1588 if (!NT_STATUS_IS_OK(status)) {
1593 /* We have to use schannel, otherwise the SamLogonEx fails
1594 * with INTERNAL_ERROR */
1596 b->flags &= ~DCERPC_AUTH_OPTIONS;
1597 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1599 status = dcerpc_pipe_connect_b(mem_ctx, &p, b,
1600 &ndr_table_netlogon,
1601 machine_credentials, NULL, torture->lp_ctx);
1603 if (!NT_STATUS_IS_OK(status)) {
1604 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1609 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1610 if (!NT_STATUS_IS_OK(status)) {
1618 const char *comment;
1620 const char *username;
1621 const char *password;
1623 NTSTATUS expected_interactive_error;
1624 NTSTATUS expected_network_error;
1625 uint32_t parameter_control;
1626 bool old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1629 .comment = "domain\\user",
1630 .domain = cli_credentials_get_domain(cmdline_credentials),
1631 .username = cli_credentials_get_username(cmdline_credentials),
1632 .password = cli_credentials_get_password(cmdline_credentials),
1633 .network_login = true,
1634 .expected_interactive_error = NT_STATUS_OK,
1635 .expected_network_error = NT_STATUS_OK
1638 .comment = "realm\\user",
1639 .domain = cli_credentials_get_realm(cmdline_credentials),
1640 .username = cli_credentials_get_username(cmdline_credentials),
1641 .password = cli_credentials_get_password(cmdline_credentials),
1642 .network_login = true,
1643 .expected_interactive_error = NT_STATUS_OK,
1644 .expected_network_error = NT_STATUS_OK
1647 .comment = "user@domain",
1649 .username = talloc_asprintf(mem_ctx,
1651 cli_credentials_get_username(cmdline_credentials),
1652 cli_credentials_get_domain(cmdline_credentials)
1654 .password = cli_credentials_get_password(cmdline_credentials),
1655 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1656 .expected_interactive_error = NT_STATUS_OK,
1657 .expected_network_error = NT_STATUS_OK
1660 .comment = "user@realm",
1662 .username = talloc_asprintf(mem_ctx,
1664 cli_credentials_get_username(cmdline_credentials),
1665 cli_credentials_get_realm(cmdline_credentials)
1667 .password = cli_credentials_get_password(cmdline_credentials),
1668 .network_login = true,
1669 .expected_interactive_error = NT_STATUS_OK,
1670 .expected_network_error = NT_STATUS_OK
1673 .comment = "machine domain\\user",
1674 .domain = cli_credentials_get_domain(machine_credentials),
1675 .username = cli_credentials_get_username(machine_credentials),
1676 .password = cli_credentials_get_password(machine_credentials),
1677 .network_login = true,
1678 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1679 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1682 .comment = "machine domain\\user",
1683 .domain = cli_credentials_get_domain(machine_credentials),
1684 .username = cli_credentials_get_username(machine_credentials),
1685 .password = cli_credentials_get_password(machine_credentials),
1686 .network_login = true,
1687 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1688 .expected_network_error = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1691 .comment = "machine realm\\user",
1692 .domain = cli_credentials_get_realm(machine_credentials),
1693 .username = cli_credentials_get_username(machine_credentials),
1694 .password = cli_credentials_get_password(machine_credentials),
1695 .network_login = true,
1696 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1697 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1700 .comment = "machine user@domain",
1702 .username = talloc_asprintf(mem_ctx,
1704 cli_credentials_get_username(machine_credentials),
1705 cli_credentials_get_domain(machine_credentials)
1707 .password = cli_credentials_get_password(machine_credentials),
1708 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1709 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1710 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1713 .comment = "machine user@realm",
1715 .username = talloc_asprintf(mem_ctx,
1717 cli_credentials_get_username(machine_credentials),
1718 cli_credentials_get_realm(machine_credentials)
1720 .password = cli_credentials_get_password(machine_credentials),
1721 .network_login = true,
1722 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1723 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1726 .comment = "test user (long pw): domain\\user",
1727 .domain = userdomain,
1728 .username = TEST_USER_NAME,
1729 .password = user_password,
1730 .network_login = true,
1731 .expected_interactive_error = NT_STATUS_OK,
1732 .expected_network_error = NT_STATUS_OK
1735 .comment = "test user (long pw): user@realm",
1737 .username = talloc_asprintf(mem_ctx,
1740 lp_realm(torture->lp_ctx)),
1741 .password = user_password,
1742 .network_login = true,
1743 .expected_interactive_error = NT_STATUS_OK,
1744 .expected_network_error = NT_STATUS_OK
1747 .comment = "test user (long pw): user@domain",
1749 .username = talloc_asprintf(mem_ctx,
1753 .password = user_password,
1754 .network_login = false, /* works for some things, but not NTLMv2. Odd */
1755 .expected_interactive_error = NT_STATUS_OK,
1756 .expected_network_error = NT_STATUS_OK
1758 /* Oddball, can we use the old password ? */
1760 .comment = "test user: user\\domain OLD PASSWORD",
1761 .domain = userdomain,
1762 .username = TEST_USER_NAME,
1763 .password = old_user_password,
1764 .network_login = true,
1765 .expected_interactive_error = NT_STATUS_WRONG_PASSWORD,
1766 .expected_network_error = NT_STATUS_OK,
1767 .old_password = true
1770 .comment = "test user (wong workstation): domain\\user",
1771 .domain = userdomain,
1772 .username = TEST_USER_NAME_WRONG_WKS,
1773 .password = user_password_wrong_wks,
1774 .network_login = true,
1775 .expected_interactive_error = NT_STATUS_INVALID_WORKSTATION,
1776 .expected_network_error = NT_STATUS_INVALID_WORKSTATION
1780 /* Try all the tests for different username forms */
1781 for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1783 if (!test_InteractiveLogon(p, mem_ctx, creds,
1784 usercreds[ci].comment,
1786 usercreds[ci].domain,
1787 usercreds[ci].username,
1788 usercreds[ci].password,
1789 usercreds[ci].parameter_control,
1790 usercreds[ci].expected_interactive_error)) {
1794 if (usercreds[ci].network_login) {
1795 if (!test_SamLogon(p, mem_ctx, creds,
1796 usercreds[ci].comment,
1797 usercreds[ci].domain,
1798 usercreds[ci].username,
1799 usercreds[ci].password,
1800 usercreds[ci].parameter_control,
1801 usercreds[ci].expected_network_error,
1802 usercreds[ci].old_password,
1809 /* Using the first username form, try the different
1810 * credentials flag setups, on only one of the tests (checks
1811 * session key encryption) */
1813 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1814 /* TODO: Somehow we lost setting up the different credential flags here! */
1816 if (!test_InteractiveLogon(p, mem_ctx, creds,
1817 usercreds[0].comment,
1819 usercreds[0].domain,
1820 usercreds[0].username,
1821 usercreds[0].password,
1822 usercreds[0].parameter_control,
1823 usercreds[0].expected_interactive_error)) {
1827 if (usercreds[0].network_login) {
1828 if (!test_SamLogon(p, mem_ctx, creds,
1829 usercreds[0].comment,
1830 usercreds[0].domain,
1831 usercreds[0].username,
1832 usercreds[0].password,
1833 usercreds[0].parameter_control,
1834 usercreds[0].expected_network_error,
1835 usercreds[0].old_password,
1844 talloc_free(mem_ctx);
1846 torture_leave_domain(join_ctx);
1847 torture_leave_domain(user_ctx);
1848 torture_leave_domain(user_ctx_wrong_wks);
1849 torture_leave_domain(user_ctx_wrong_time);