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());
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(), lp_workgroup());
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, &unicodepw,
1161 samlogon_state->password)) == -1) {
1162 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1166 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1168 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1170 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
1173 (void**)&dospw)) == -1) {
1174 DEBUG(0, ("convert_string_talloc failed!\n"));
1178 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1180 nt_status = check_samlogon(samlogon_state,
1182 samlogon_state->parameter_control | MSV1_0_CLEARTEXT_PASSWORD_ALLOWED,
1190 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1191 /* for 'old' passwords, we allow the server to be OK or wrong password */
1192 if (samlogon_state->old_password) {
1195 /* for 'long' passwords, the LM password is invalid */
1196 if (break_which == NO_NT && !lm_good) {
1199 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
1200 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
1201 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
1202 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1203 SAFE_FREE(*error_string);
1204 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1206 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1208 } else if (!NT_STATUS_IS_OK(nt_status)) {
1212 if (break_which == NO_NT && !lm_good) {
1213 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1220 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
1221 char **error_string) {
1222 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1225 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
1226 char **error_string) {
1227 return test_plaintext(samlogon_state, BREAK_LM, error_string);
1230 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
1231 char **error_string) {
1232 return test_plaintext(samlogon_state, BREAK_NT, error_string);
1235 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
1236 char **error_string) {
1237 return test_plaintext(samlogon_state, NO_LM, error_string);
1240 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
1241 char **error_string) {
1242 return test_plaintext(samlogon_state, NO_NT, error_string);
1256 - plaintext tests (in challenge-response fields)
1258 check we get the correct session key in each case
1259 check what values we get for the LM session key
1263 static const struct ntlm_tests {
1264 BOOL (*fn)(struct samlogon_state *, char **);
1268 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
1270 {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", False},
1272 {test_lm, "LM", False},
1273 {test_lm_ntlm, "LM and NTLM", False},
1274 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
1275 {test_ntlm, "NTLM", False},
1276 {test_ntlm_in_lm, "NTLM in LM", False},
1277 {test_ntlm_in_both, "NTLM in both", False},
1278 {test_ntlmv2, "NTLMv2", False},
1279 {test_ntlmv2_no_dom, "NTLMv2 (no domain)", False},
1280 {test_lmv2, "LMv2", False},
1281 {test_lmv2_no_dom, "LMv2 (no domain)", False},
1282 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
1283 {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", False},
1284 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
1286 {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", False},
1288 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
1289 {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", False},
1290 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
1291 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
1292 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
1293 {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", False},
1294 {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", False},
1295 {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", False},
1296 {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", False},
1297 {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", False},
1298 {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", False},
1299 {test_plaintext_none_broken, "Plaintext", False},
1300 {test_plaintext_lm_broken, "Plaintext LM broken", False},
1301 {test_plaintext_nt_broken, "Plaintext NT broken", False},
1302 {test_plaintext_nt_only, "Plaintext NT only", False},
1303 {test_plaintext_lm_only, "Plaintext LM only", False},
1308 try a netlogon SamLogon
1310 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1311 struct creds_CredentialState *creds,
1312 const char *comment,
1313 const char *account_domain, const char *account_name,
1314 const char *plain_pass, uint32_t parameter_control,
1315 NTSTATUS expected_error, BOOL old_password,
1318 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1321 int validation_levels[] = {2,3,6};
1322 int logon_levels[] = { 2, 6 };
1323 int function_levels[] = {
1324 NDR_NETR_LOGONSAMLOGON,
1325 NDR_NETR_LOGONSAMLOGONEX,
1326 NDR_NETR_LOGONSAMLOGONWITHFLAGS };
1327 struct samlogon_state samlogon_state;
1329 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1331 samlogon_state.comment = comment;
1332 samlogon_state.account_name = account_name;
1333 samlogon_state.account_domain = account_domain;
1334 samlogon_state.password = plain_pass;
1335 samlogon_state.p = p;
1336 samlogon_state.creds = creds;
1337 samlogon_state.expected_error = expected_error;
1338 samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1339 samlogon_state.parameter_control = parameter_control;
1340 samlogon_state.old_password = old_password;
1342 generate_random_buffer(samlogon_state.chall.data, 8);
1343 samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1344 samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
1345 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1346 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1347 samlogon_state.r_flags.in.flags = 0;
1349 samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1350 samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
1351 samlogon_state.r_ex.in.flags = 0;
1353 samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1354 samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
1355 samlogon_state.r.in.credential = &samlogon_state.auth;
1356 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1358 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1359 for (i=0; test_table[i].fn; i++) {
1360 if (n_subtests && (i > n_subtests)) {
1363 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1364 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1365 char *error_string = NULL;
1366 TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1367 samlogon_state.mem_ctx = tmp_ctx;
1368 samlogon_state.function_level = function_levels[f];
1369 samlogon_state.r.in.validation_level = validation_levels[v];
1370 samlogon_state.r.in.logon_level = logon_levels[l];
1371 samlogon_state.r_ex.in.validation_level = validation_levels[v];
1372 samlogon_state.r_ex.in.logon_level = logon_levels[l];
1373 samlogon_state.r_flags.in.validation_level = validation_levels[v];
1374 samlogon_state.r_flags.in.logon_level = logon_levels[l];
1375 if (!test_table[i].fn(&samlogon_state, &error_string)) {
1376 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1377 samlogon_state.comment,
1378 samlogon_state.account_domain,
1379 samlogon_state.account_name,
1380 test_table[i].name, validation_levels[v],
1381 logon_levels[l], function_levels[f]);
1383 if (test_table[i].expect_fail) {
1384 d_printf(" failed (expected, test incomplete): %s\n", error_string);
1386 d_printf(" failed: %s\n", error_string);
1389 SAFE_FREE(error_string);
1391 talloc_free(tmp_ctx);
1396 talloc_free(fn_ctx);
1401 test an ADS style interactive domain logon
1403 BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1404 struct creds_CredentialState *creds,
1405 const char *comment,
1406 const char *workstation_name,
1407 const char *account_domain, const char *account_name,
1408 const char *plain_pass, uint32_t parameter_control,
1409 NTSTATUS expected_error)
1412 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1413 struct netr_LogonSamLogonWithFlags r;
1414 struct netr_Authenticator a, ra;
1415 struct netr_PasswordInfo pinfo;
1421 creds_client_authenticator(creds, &a);
1423 r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1424 r.in.computer_name = TEST_MACHINE_NAME;
1425 r.in.credential = &a;
1426 r.in.return_authenticator = &ra;
1427 r.in.logon_level = 5;
1428 r.in.logon.password = &pinfo;
1429 r.in.validation_level = 6;
1432 pinfo.identity_info.domain_name.string = account_domain;
1433 pinfo.identity_info.parameter_control = parameter_control;
1434 pinfo.identity_info.logon_id_low = 0;
1435 pinfo.identity_info.logon_id_high = 0;
1436 pinfo.identity_info.account_name.string = account_name;
1437 pinfo.identity_info.workstation.string = workstation_name;
1439 if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1440 ZERO_STRUCT(pinfo.lmpassword.hash);
1442 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1444 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1445 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1446 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1448 creds_des_encrypt(creds, &pinfo.lmpassword);
1449 creds_des_encrypt(creds, &pinfo.ntpassword);
1452 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
1454 status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1455 if (!r.out.return_authenticator
1456 || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1457 d_printf("Credential chaining failed\n");
1458 talloc_free(fn_ctx);
1462 talloc_free(fn_ctx);
1464 if (!NT_STATUS_EQUAL(expected_error, status)) {
1465 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1466 account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
1475 BOOL torture_rpc_samlogon(struct torture_context *torture)
1478 struct dcerpc_pipe *p;
1479 struct dcerpc_binding *b;
1480 struct cli_credentials *machine_credentials;
1481 TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1483 struct test_join *join_ctx = NULL;
1484 struct test_join *user_ctx = NULL, *user_ctx_wrong_wks = NULL, *user_ctx_wrong_time = NULL;
1485 char *user_password, *user_password_wrong_wks, *user_password_wrong_time;
1486 const char *old_user_password;
1487 char *test_machine_account;
1488 const char *userdomain;
1489 struct samr_SetUserInfo s;
1490 union samr_UserInfo u;
1494 unsigned int credential_flags[] = {
1495 NETLOGON_NEG_AUTH2_FLAGS,
1496 NETLOGON_NEG_ARCFOUR,
1497 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1498 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1499 0 /* yes, this is a valid flag, causes the use of DES */
1502 struct creds_CredentialState *creds;
1504 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1505 /* We only need to join as a workstation here, and in future,
1506 * if we wish to test against trusted domains, we must be a
1507 * workstation here */
1508 join_ctx = torture_join_domain(TEST_MACHINE_NAME, ACB_WSTRUST,
1509 &machine_credentials);
1511 d_printf("Failed to join as Workstation\n");
1515 userdomain = torture_setting_string(torture, "userdomain", lp_workgroup());
1517 user_ctx = torture_create_testuser(torture,
1521 (const char **)&user_password);
1523 d_printf("Failed to create a test user\n");
1527 old_user_password = user_password;
1529 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
1530 TEST_USER_NAME, 16 /* > 14 */, &user_password,
1533 user_ctx_wrong_wks = torture_create_testuser(torture,
1534 TEST_USER_NAME_WRONG_WKS,
1537 (const char **)&user_password_wrong_wks);
1538 if (!user_ctx_wrong_wks) {
1539 d_printf("Failed to create a test user (wrong workstation test)\n");
1544 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_wks);
1548 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS;
1549 u.info21.workstations.string = "not" TEST_MACHINE_NAME;
1551 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks), mem_ctx, &s);
1552 if (!NT_STATUS_IS_OK(status)) {
1553 printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status));
1559 = torture_create_testuser(torture, TEST_USER_NAME_WRONG_TIME,
1562 (const char **)&user_password_wrong_time);
1563 if (!user_ctx_wrong_time) {
1564 d_printf("Failed to create a test user (wrong workstation test)\n");
1569 s.in.user_handle = torture_join_samr_user_policy(user_ctx_wrong_time);
1573 u.info21.fields_present = SAMR_FIELD_WORKSTATIONS | SAMR_FIELD_LOGON_HOURS;
1574 u.info21.workstations.string = TEST_MACHINE_NAME;
1575 u.info21.logon_hours.units_per_week = 168;
1576 u.info21.logon_hours.bits = talloc_zero_size(mem_ctx, 168);
1578 status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time), mem_ctx, &s);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status));
1585 status = torture_rpc_binding(torture, &b);
1586 if (!NT_STATUS_IS_OK(status)) {
1591 /* We have to use schannel, otherwise the SamLogonEx fails
1592 * with INTERNAL_ERROR */
1594 b->flags &= ~DCERPC_AUTH_OPTIONS;
1595 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1597 status = dcerpc_pipe_connect_b(mem_ctx, &p, b,
1598 &ndr_table_netlogon,
1599 machine_credentials, NULL);
1601 if (!NT_STATUS_IS_OK(status)) {
1602 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1607 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1608 if (!NT_STATUS_IS_OK(status)) {
1616 const char *comment;
1618 const char *username;
1619 const char *password;
1621 NTSTATUS expected_interactive_error;
1622 NTSTATUS expected_network_error;
1623 uint32_t parameter_control;
1624 BOOL old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1627 .comment = "domain\\user",
1628 .domain = cli_credentials_get_domain(cmdline_credentials),
1629 .username = cli_credentials_get_username(cmdline_credentials),
1630 .password = cli_credentials_get_password(cmdline_credentials),
1631 .network_login = True,
1632 .expected_interactive_error = NT_STATUS_OK,
1633 .expected_network_error = NT_STATUS_OK
1636 .comment = "realm\\user",
1637 .domain = cli_credentials_get_realm(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 = "user@domain",
1647 .username = talloc_asprintf(mem_ctx,
1649 cli_credentials_get_username(cmdline_credentials),
1650 cli_credentials_get_domain(cmdline_credentials)
1652 .password = cli_credentials_get_password(cmdline_credentials),
1653 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1654 .expected_interactive_error = NT_STATUS_OK,
1655 .expected_network_error = NT_STATUS_OK
1658 .comment = "user@realm",
1660 .username = talloc_asprintf(mem_ctx,
1662 cli_credentials_get_username(cmdline_credentials),
1663 cli_credentials_get_realm(cmdline_credentials)
1665 .password = cli_credentials_get_password(cmdline_credentials),
1666 .network_login = True,
1667 .expected_interactive_error = NT_STATUS_OK,
1668 .expected_network_error = NT_STATUS_OK
1671 .comment = "machine domain\\user",
1672 .domain = cli_credentials_get_domain(machine_credentials),
1673 .username = cli_credentials_get_username(machine_credentials),
1674 .password = cli_credentials_get_password(machine_credentials),
1675 .network_login = True,
1676 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1677 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
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 .expected_network_error = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1689 .comment = "machine realm\\user",
1690 .domain = cli_credentials_get_realm(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 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1698 .comment = "machine user@domain",
1700 .username = talloc_asprintf(mem_ctx,
1702 cli_credentials_get_username(machine_credentials),
1703 cli_credentials_get_domain(machine_credentials)
1705 .password = cli_credentials_get_password(machine_credentials),
1706 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1707 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1708 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1711 .comment = "machine user@realm",
1713 .username = talloc_asprintf(mem_ctx,
1715 cli_credentials_get_username(machine_credentials),
1716 cli_credentials_get_realm(machine_credentials)
1718 .password = cli_credentials_get_password(machine_credentials),
1719 .network_login = True,
1720 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1721 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1724 .comment = "test user (long pw): domain\\user",
1725 .domain = userdomain,
1726 .username = TEST_USER_NAME,
1727 .password = user_password,
1728 .network_login = True,
1729 .expected_interactive_error = NT_STATUS_OK,
1730 .expected_network_error = NT_STATUS_OK
1733 .comment = "test user (long pw): user@realm",
1735 .username = talloc_asprintf(mem_ctx,
1739 .password = user_password,
1740 .network_login = True,
1741 .expected_interactive_error = NT_STATUS_OK,
1742 .expected_network_error = NT_STATUS_OK
1745 .comment = "test user (long pw): user@domain",
1747 .username = talloc_asprintf(mem_ctx,
1751 .password = user_password,
1752 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1753 .expected_interactive_error = NT_STATUS_OK,
1754 .expected_network_error = NT_STATUS_OK
1756 /* Oddball, can we use the old password ? */
1758 .comment = "test user: user\\domain OLD PASSWORD",
1759 .domain = userdomain,
1760 .username = TEST_USER_NAME,
1761 .password = old_user_password,
1762 .network_login = True,
1763 .expected_interactive_error = NT_STATUS_WRONG_PASSWORD,
1764 .expected_network_error = NT_STATUS_OK,
1765 .old_password = True
1768 .comment = "test user (wong workstation): domain\\user",
1769 .domain = userdomain,
1770 .username = TEST_USER_NAME_WRONG_WKS,
1771 .password = user_password_wrong_wks,
1772 .network_login = True,
1773 .expected_interactive_error = NT_STATUS_INVALID_WORKSTATION,
1774 .expected_network_error = NT_STATUS_INVALID_WORKSTATION
1778 /* Try all the tests for different username forms */
1779 for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1781 if (!test_InteractiveLogon(p, mem_ctx, creds,
1782 usercreds[ci].comment,
1784 usercreds[ci].domain,
1785 usercreds[ci].username,
1786 usercreds[ci].password,
1787 usercreds[ci].parameter_control,
1788 usercreds[ci].expected_interactive_error)) {
1792 if (usercreds[ci].network_login) {
1793 if (!test_SamLogon(p, mem_ctx, creds,
1794 usercreds[ci].comment,
1795 usercreds[ci].domain,
1796 usercreds[ci].username,
1797 usercreds[ci].password,
1798 usercreds[ci].parameter_control,
1799 usercreds[ci].expected_network_error,
1800 usercreds[ci].old_password,
1807 /* Using the first username form, try the different
1808 * credentials flag setups, on only one of the tests (checks
1809 * session key encryption) */
1811 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1812 /* TODO: Somehow we lost setting up the different credential flags here! */
1814 if (!test_InteractiveLogon(p, mem_ctx, creds,
1815 usercreds[0].comment,
1817 usercreds[0].domain,
1818 usercreds[0].username,
1819 usercreds[0].password,
1820 usercreds[0].parameter_control,
1821 usercreds[0].expected_interactive_error)) {
1825 if (usercreds[0].network_login) {
1826 if (!test_SamLogon(p, mem_ctx, creds,
1827 usercreds[0].comment,
1828 usercreds[0].domain,
1829 usercreds[0].username,
1830 usercreds[0].password,
1831 usercreds[0].parameter_control,
1832 usercreds[0].expected_network_error,
1833 usercreds[0].old_password,
1842 talloc_free(mem_ctx);
1844 torture_leave_domain(join_ctx);
1845 torture_leave_domain(user_ctx);
1846 torture_leave_domain(user_ctx_wrong_wks);
1847 torture_leave_domain(user_ctx_wrong_time);