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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/ndr_netlogon.h"
27 #include "librpc/gen_ndr/ndr_netlogon_c.h"
28 #include "auth/auth.h"
29 #include "lib/crypto/crypto.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/rpc.h"
32 #include "auth/gensec/schannel_proto.h"
33 #include "libcli/auth/libcli_auth.h"
35 #define TEST_MACHINE_NAME "samlogontest"
36 #define TEST_USER_NAME "samlogontestuser"
47 struct samlogon_state {
50 const char *account_name;
51 const char *account_domain;
53 struct dcerpc_pipe *p;
55 uint32_t parameter_control;
56 struct netr_LogonSamLogon r;
57 struct netr_LogonSamLogonEx r_ex;
58 struct netr_LogonSamLogonWithFlags r_flags;
59 struct netr_Authenticator auth, auth2;
60 struct creds_CredentialState *creds;
61 NTSTATUS expected_error;
62 BOOL old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
67 Authenticate a user with a challenge/response, checking session key
68 and valid authentication types
70 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
71 enum ntlm_break break_which,
72 uint32_t parameter_control,
74 DATA_BLOB *lm_response,
75 DATA_BLOB *nt_response,
77 uint8_t user_session_key[16],
81 struct netr_LogonSamLogon *r = &samlogon_state->r;
82 struct netr_LogonSamLogonEx *r_ex = &samlogon_state->r_ex;
83 struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
84 struct netr_NetworkInfo ninfo;
85 struct netr_SamBaseInfo *base = NULL;
86 uint16_t validation_level = 0;
88 samlogon_state->r.in.logon.network = &ninfo;
89 samlogon_state->r_ex.in.logon.network = &ninfo;
90 samlogon_state->r_flags.in.logon.network = &ninfo;
92 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
93 ninfo.identity_info.parameter_control = parameter_control;
94 ninfo.identity_info.logon_id_low = 0;
95 ninfo.identity_info.logon_id_high = 0;
96 ninfo.identity_info.account_name.string = samlogon_state->account_name;
97 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
99 memcpy(ninfo.challenge, chall->data, 8);
101 switch (break_which) {
105 if (lm_response && lm_response->data) {
106 lm_response->data[0]++;
110 if (nt_response && nt_response->data) {
111 nt_response->data[0]++;
115 if (lm_response && lm_response->data) {
116 lm_response->data[0]++;
118 if (nt_response && nt_response->data) {
119 nt_response->data[0]++;
123 data_blob_free(lm_response);
126 data_blob_free(nt_response);
131 ninfo.nt.data = nt_response->data;
132 ninfo.nt.length = nt_response->length;
134 ninfo.nt.data = NULL;
139 ninfo.lm.data = lm_response->data;
140 ninfo.lm.length = lm_response->length;
142 ninfo.lm.data = NULL;
146 switch (samlogon_state->function_level) {
147 case DCERPC_NETR_LOGONSAMLOGON:
148 ZERO_STRUCT(samlogon_state->auth2);
149 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
151 r->out.return_authenticator = NULL;
152 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
153 if (!r->out.return_authenticator ||
154 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
155 d_printf("Credential chaining failed\n");
157 if (!NT_STATUS_IS_OK(status)) {
159 *error_string = strdup(nt_errstr(status));
164 validation_level = r->in.validation_level;
166 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r->out.validation);
168 switch (validation_level) {
170 base = &r->out.validation.sam2->base;
173 base = &r->out.validation.sam3->base;
176 base = &r->out.validation.sam6->base;
180 case DCERPC_NETR_LOGONSAMLOGONEX:
181 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
182 if (!NT_STATUS_IS_OK(status)) {
184 *error_string = strdup(nt_errstr(status));
189 validation_level = r_ex->in.validation_level;
191 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_ex->out.validation);
193 switch (validation_level) {
195 base = &r_ex->out.validation.sam2->base;
198 base = &r_ex->out.validation.sam3->base;
201 base = &r_ex->out.validation.sam6->base;
205 case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS:
206 ZERO_STRUCT(samlogon_state->auth2);
207 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
209 r_flags->out.return_authenticator = NULL;
210 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
211 if (!r_flags->out.return_authenticator ||
212 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
213 d_printf("Credential chaining failed\n");
215 if (!NT_STATUS_IS_OK(status)) {
217 *error_string = strdup(nt_errstr(status));
222 validation_level = r_flags->in.validation_level;
224 creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_flags->out.validation);
226 switch (validation_level) {
228 base = &r_flags->out.validation.sam2->base;
231 base = &r_flags->out.validation.sam3->base;
234 base = &r_flags->out.validation.sam6->base;
240 return NT_STATUS_INVALID_PARAMETER;
244 d_printf("No user info returned from 'successful' SamLogon*() call!\n");
245 return NT_STATUS_INVALID_PARAMETER;
248 if (user_session_key) {
249 memcpy(user_session_key, base->key.key, 16);
252 memcpy(lm_key, base->LMSessKey.key, 8);
260 * Test the normal 'LM and NTLM' combination
263 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
268 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
269 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
270 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
273 uint8_t user_session_key[16];
278 ZERO_STRUCT(user_session_key);
280 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
282 ZERO_STRUCT(lm_hash);
284 E_deshash(samlogon_state->password, lm_hash);
287 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
289 E_md4hash(samlogon_state->password, nt_hash);
290 SMBsesskeygen_ntv1(nt_hash, session_key.data);
292 nt_status = check_samlogon(samlogon_state,
294 samlogon_state->parameter_control,
295 &samlogon_state->chall,
302 data_blob_free(&lm_response);
304 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
305 /* for 'long' passwords, the LM password is invalid */
306 if (break_which == NO_NT && !lm_good) {
309 /* for 'old' passwords, we allow the server to be OK or wrong password */
310 if (samlogon_state->old_password) {
313 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
314 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
315 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
316 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
317 SAFE_FREE(*error_string);
318 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
320 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
322 } else if (!NT_STATUS_IS_OK(nt_status)) {
326 if (break_which == NO_NT && !lm_good) {
327 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
331 if (memcmp(lm_hash, lm_key,
332 sizeof(lm_key)) != 0) {
333 d_printf("LM Key does not match expectations!\n");
334 d_printf("lm_key:\n");
335 dump_data(1, lm_key, 8);
336 d_printf("expected:\n");
337 dump_data(1, lm_hash, 8);
341 switch (break_which) {
344 uint8_t lm_key_expected[16];
345 memcpy(lm_key_expected, lm_hash, 8);
346 memset(lm_key_expected+8, '\0', 8);
347 if (memcmp(lm_key_expected, user_session_key,
349 *error_string = strdup("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
350 d_printf("user_session_key:\n");
351 dump_data(1, user_session_key, sizeof(user_session_key));
352 d_printf("expected:\n");
353 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
359 if (memcmp(session_key.data, user_session_key,
360 sizeof(user_session_key)) != 0) {
361 *error_string = strdup("NT Session Key does not match expectations!\n");
362 d_printf("user_session_key:\n");
363 dump_data(1, user_session_key, 16);
364 d_printf("expected:\n");
365 dump_data(1, session_key.data, session_key.length);
373 * Test LM authentication, no NT response supplied
376 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string)
379 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
383 * Test the NTLM response only, no LM.
386 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
388 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
392 * Test the NTLM response only, but in the LM field.
395 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
400 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
401 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
405 uint8_t user_session_key[16];
409 ZERO_STRUCT(user_session_key);
411 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
413 E_md4hash(samlogon_state->password, nt_hash);
414 SMBsesskeygen_ntv1(nt_hash,
417 lm_good = E_deshash(samlogon_state->password, lm_hash);
419 ZERO_STRUCT(lm_hash);
421 nt_status = check_samlogon(samlogon_state,
423 samlogon_state->parameter_control,
424 &samlogon_state->chall,
431 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
432 /* for 'old' passwords, we allow the server to be OK or wrong password */
433 if (samlogon_state->old_password) {
437 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
438 SAFE_FREE(*error_string);
439 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
441 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
443 } else if (!NT_STATUS_IS_OK(nt_status)) {
448 if (memcmp(lm_hash, lm_key,
449 sizeof(lm_key)) != 0) {
450 d_printf("LM Key does not match expectations!\n");
451 d_printf("lm_key:\n");
452 dump_data(1, lm_key, 8);
453 d_printf("expected:\n");
454 dump_data(1, lm_hash, 8);
459 if (memcmp(session_key.data, lm_key,
460 sizeof(lm_key)) != 0) {
461 d_printf("LM Key does not match expectations (first 8 session key)!\n");
462 d_printf("lm_key:\n");
463 dump_data(1, lm_key, 8);
464 d_printf("expected:\n");
465 dump_data(1, session_key.data, 8);
470 if (lm_good && memcmp(lm_hash, user_session_key, 8) != 0) {
471 uint8_t lm_key_expected[16];
472 memcpy(lm_key_expected, lm_hash, 8);
473 memset(lm_key_expected+8, '\0', 8);
474 if (memcmp(lm_key_expected, user_session_key,
476 d_printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
477 d_printf("user_session_key:\n");
478 dump_data(1, user_session_key, sizeof(user_session_key));
479 d_printf("expected:\n");
480 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
488 * Test the NTLM response only, but in the both the NT and LM fields.
491 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
496 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
497 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
501 uint8_t user_session_key[16];
505 ZERO_STRUCT(user_session_key);
507 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
509 E_md4hash(samlogon_state->password, nt_hash);
510 SMBsesskeygen_ntv1(nt_hash,
513 lm_good = E_deshash(samlogon_state->password, lm_hash);
515 ZERO_STRUCT(lm_hash);
518 nt_status = check_samlogon(samlogon_state,
520 samlogon_state->parameter_control,
521 &samlogon_state->chall,
528 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
529 /* for 'old' passwords, we allow the server to be OK or wrong password */
530 if (samlogon_state->old_password) {
534 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
535 SAFE_FREE(*error_string);
536 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
538 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
540 } else if (!NT_STATUS_IS_OK(nt_status)) {
544 if (!NT_STATUS_IS_OK(nt_status)) {
548 if (memcmp(lm_hash, lm_key,
549 sizeof(lm_key)) != 0) {
550 d_printf("LM Key does not match expectations!\n");
551 d_printf("lm_key:\n");
552 dump_data(1, lm_key, 8);
553 d_printf("expected:\n");
554 dump_data(1, lm_hash, 8);
557 if (memcmp(session_key.data, user_session_key,
558 sizeof(user_session_key)) != 0) {
559 d_printf("NT Session Key does not match expectations!\n");
560 d_printf("user_session_key:\n");
561 dump_data(1, user_session_key, 16);
562 d_printf("expected:\n");
563 dump_data(1, session_key.data, session_key.length);
572 * Test the NTLMv2 and LMv2 responses
580 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state,
581 enum ntlm_break break_which,
582 enum ntlmv2_domain ntlmv2_domain,
587 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
588 DATA_BLOB lmv2_response = data_blob(NULL, 0);
589 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
590 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
591 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, TEST_MACHINE_NAME, lp_workgroup());
593 uint8_t lm_session_key[8];
594 uint8_t user_session_key[16];
596 ZERO_STRUCT(lm_session_key);
597 ZERO_STRUCT(user_session_key);
599 switch (ntlmv2_domain) {
601 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
602 samlogon_state->account_name, samlogon_state->account_domain,
603 samlogon_state->password, &samlogon_state->chall,
605 &lmv2_response, &ntlmv2_response,
606 &lmv2_session_key, &ntlmv2_session_key)) {
607 data_blob_free(&names_blob);
612 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
613 samlogon_state->account_name, "",
614 samlogon_state->password, &samlogon_state->chall,
616 &lmv2_response, &ntlmv2_response,
617 &lmv2_session_key, &ntlmv2_session_key)) {
618 data_blob_free(&names_blob);
623 data_blob_free(&names_blob);
625 nt_status = check_samlogon(samlogon_state,
627 samlogon_state->parameter_control,
628 &samlogon_state->chall,
635 data_blob_free(&lmv2_response);
636 data_blob_free(&ntlmv2_response);
639 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
640 /* for 'old' passwords, we allow the server to be OK or wrong password */
641 if (samlogon_state->old_password) {
644 return break_which == BREAK_BOTH;
645 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
646 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
647 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
648 SAFE_FREE(*error_string);
649 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
651 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
653 } else if (!NT_STATUS_IS_OK(nt_status)) {
658 switch (break_which) {
660 if (memcmp(lmv2_session_key.data, user_session_key,
661 sizeof(user_session_key)) != 0) {
662 d_printf("USER (LMv2) Session Key does not match expectations!\n");
663 d_printf("user_session_key:\n");
664 dump_data(1, user_session_key, 16);
665 d_printf("expected:\n");
666 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
669 if (memcmp(lmv2_session_key.data, lm_session_key,
670 sizeof(lm_session_key)) != 0) {
671 d_printf("LM (LMv2) Session Key does not match expectations!\n");
672 d_printf("lm_session_key:\n");
673 dump_data(1, lm_session_key, 8);
674 d_printf("expected:\n");
675 dump_data(1, lmv2_session_key.data, 8);
680 if (memcmp(ntlmv2_session_key.data, user_session_key,
681 sizeof(user_session_key)) != 0) {
682 if (memcmp(lmv2_session_key.data, user_session_key,
683 sizeof(user_session_key)) == 0) {
684 d_printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
685 d_printf("user_session_key:\n");
686 dump_data(1, user_session_key, 16);
687 d_printf("expected:\n");
688 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
692 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
693 d_printf("user_session_key:\n");
694 dump_data(1, user_session_key, 16);
695 d_printf("expected:\n");
696 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
700 if (memcmp(ntlmv2_session_key.data, lm_session_key,
701 sizeof(lm_session_key)) != 0) {
702 if (memcmp(lmv2_session_key.data, lm_session_key,
703 sizeof(lm_session_key)) == 0) {
704 d_printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
705 d_printf("user_session_key:\n");
706 dump_data(1, lm_session_key, 8);
707 d_printf("expected:\n");
708 dump_data(1, ntlmv2_session_key.data, 8);
711 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
712 d_printf("lm_session_key:\n");
713 dump_data(1, lm_session_key, 8);
714 d_printf("expected:\n");
715 dump_data(1, ntlmv2_session_key.data, 8);
725 * Test the NTLM and LMv2 responses
728 static BOOL test_lmv2_ntlm_broken(struct samlogon_state *samlogon_state,
729 enum ntlm_break break_which,
730 enum ntlmv2_domain ntlmv2_domain,
735 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
736 DATA_BLOB lmv2_response = data_blob(NULL, 0);
737 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
738 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
739 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
741 DATA_BLOB ntlm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
742 DATA_BLOB ntlm_session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
746 uint8_t lm_session_key[8];
747 uint8_t user_session_key[16];
750 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
752 E_md4hash(samlogon_state->password, nt_hash);
753 SMBsesskeygen_ntv1(nt_hash,
754 ntlm_session_key.data);
756 lm_good = E_deshash(samlogon_state->password, lm_hash);
758 ZERO_STRUCT(lm_hash);
761 ZERO_STRUCT(lm_session_key);
762 ZERO_STRUCT(user_session_key);
764 switch (ntlmv2_domain) {
766 /* TODO - test with various domain cases, and without domain */
767 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
768 samlogon_state->account_name, samlogon_state->account_domain,
769 samlogon_state->password, &samlogon_state->chall,
771 &lmv2_response, &ntlmv2_response,
772 &lmv2_session_key, &ntlmv2_session_key)) {
773 data_blob_free(&names_blob);
778 /* TODO - test with various domain cases, and without domain */
779 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx,
780 samlogon_state->account_name, "",
781 samlogon_state->password, &samlogon_state->chall,
783 &lmv2_response, &ntlmv2_response,
784 &lmv2_session_key, &ntlmv2_session_key)) {
785 data_blob_free(&names_blob);
791 data_blob_free(&names_blob);
793 nt_status = check_samlogon(samlogon_state,
795 samlogon_state->parameter_control,
796 &samlogon_state->chall,
803 data_blob_free(&lmv2_response);
804 data_blob_free(&ntlmv2_response);
807 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
808 /* for 'old' passwords, we allow the server to be OK or wrong password */
809 if (samlogon_state->old_password) {
812 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
813 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
814 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
815 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
816 SAFE_FREE(*error_string);
817 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
819 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
821 } else if (!NT_STATUS_IS_OK(nt_status)) {
825 switch (break_which) {
827 if (memcmp(lmv2_session_key.data, user_session_key,
828 sizeof(user_session_key)) != 0) {
829 d_printf("USER (LMv2) Session Key does not match expectations!\n");
830 d_printf("user_session_key:\n");
831 dump_data(1, user_session_key, 16);
832 d_printf("expected:\n");
833 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
836 if (memcmp(lmv2_session_key.data, lm_session_key,
837 sizeof(lm_session_key)) != 0) {
838 d_printf("LM (LMv2) Session Key does not match expectations!\n");
839 d_printf("lm_session_key:\n");
840 dump_data(1, lm_session_key, 8);
841 d_printf("expected:\n");
842 dump_data(1, lmv2_session_key.data, 8);
847 if (memcmp(ntlm_session_key.data, user_session_key,
848 sizeof(user_session_key)) != 0) {
849 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
850 d_printf("user_session_key:\n");
851 dump_data(1, user_session_key, 16);
852 d_printf("expected:\n");
853 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
857 if (memcmp(lm_hash, lm_session_key,
858 sizeof(lm_session_key)) != 0) {
859 d_printf("LM Session Key does not match expectations!\n");
860 d_printf("lm_session_key:\n");
861 dump_data(1, lm_session_key, 8);
862 d_printf("expected:\n");
863 dump_data(1, lm_hash, 8);
867 static const uint8_t zeros[8];
868 if (memcmp(zeros, lm_session_key,
869 sizeof(lm_session_key)) != 0) {
870 d_printf("LM Session Key does not match expectations (zeros)!\n");
871 d_printf("lm_session_key:\n");
872 dump_data(1, lm_session_key, 8);
873 d_printf("expected:\n");
874 dump_data(1, zeros, 8);
880 if (memcmp(ntlm_session_key.data, user_session_key,
881 sizeof(user_session_key)) != 0) {
882 d_printf("USER (NTLMv2) Session Key does not match expectations!\n");
883 d_printf("user_session_key:\n");
884 dump_data(1, user_session_key, 16);
885 d_printf("expected:\n");
886 dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
889 if (memcmp(ntlm_session_key.data, lm_session_key,
890 sizeof(lm_session_key)) != 0) {
891 d_printf("LM (NTLMv2) Session Key does not match expectations!\n");
892 d_printf("lm_session_key:\n");
893 dump_data(1, lm_session_key, 8);
894 d_printf("expected:\n");
895 dump_data(1, ntlm_session_key.data, 8);
904 * Test the NTLMv2 and LMv2 responses
907 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
909 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, UPPER_DOMAIN, error_string);
913 static BOOL test_lmv2_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
915 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, NO_DOMAIN, error_string);
920 * Test the LMv2 response only
923 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
925 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, UPPER_DOMAIN, error_string);
928 static BOOL test_lmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
930 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, NO_DOMAIN, error_string);
934 * Test the NTLMv2 response only
937 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
939 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, UPPER_DOMAIN, error_string);
942 static BOOL test_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string)
944 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, NO_DOMAIN, error_string);
947 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
949 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
952 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
954 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
957 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
959 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
962 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
964 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
966 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
968 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
971 static BOOL test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
973 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
976 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
978 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
982 static BOOL test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
984 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
988 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
990 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
993 static BOOL test_ntlmv2_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
995 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
998 static BOOL test_lmv2_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
1000 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
1003 static BOOL test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1005 return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
1008 static BOOL test_lmv2_ntlm_break_ntlm(struct samlogon_state *samlogon_state, char **error_string)
1010 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
1013 static BOOL test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1015 return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
1018 static BOOL test_lmv2_ntlm_break_lm(struct samlogon_state *samlogon_state, char **error_string)
1020 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
1023 static BOOL test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state *samlogon_state, char **error_string)
1025 return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
1029 * Test the NTLM2 response (extra challenge in LM feild)
1031 * This test is the same as the 'break LM' test, but checks that the
1032 * server implements NTLM2 session security in the right place
1033 * (NETLOGON is the wrong place).
1036 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
1040 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1041 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
1045 uint8_t nt_hash[16];
1046 uint8_t lm_hash[16];
1048 uint8_t user_session_key[16];
1049 uint8_t expected_user_session_key[16];
1050 uint8_t session_nonce_hash[16];
1051 uint8_t client_chall[8];
1053 struct MD5Context md5_session_nonce_ctx;
1054 HMACMD5Context hmac_ctx;
1056 ZERO_STRUCT(user_session_key);
1057 ZERO_STRUCT(lm_key);
1058 generate_random_buffer(client_chall, 8);
1060 MD5Init(&md5_session_nonce_ctx);
1061 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
1062 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
1063 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1065 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
1066 lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
1067 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
1070 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
1072 memcpy(lm_response.data, session_nonce_hash, 8);
1073 memset(lm_response.data + 8, 0, 16);
1075 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
1076 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
1077 hmac_md5_update(client_chall, 8, &hmac_ctx);
1078 hmac_md5_final(expected_user_session_key, &hmac_ctx);
1080 nt_status = check_samlogon(samlogon_state,
1082 samlogon_state->parameter_control,
1083 &samlogon_state->chall,
1090 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1091 /* for 'old' passwords, we allow the server to be OK or wrong password */
1092 if (samlogon_state->old_password) {
1096 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1097 SAFE_FREE(*error_string);
1098 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1100 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1102 } else if (!NT_STATUS_IS_OK(nt_status)) {
1107 if (memcmp(lm_hash, lm_key,
1108 sizeof(lm_key)) != 0) {
1109 d_printf("LM Key does not match expectations!\n");
1110 d_printf("lm_key:\n");
1111 dump_data(1, lm_key, 8);
1112 d_printf("expected:\n");
1113 dump_data(1, lm_hash, 8);
1117 static const uint8_t zeros[8];
1118 if (memcmp(zeros, lm_key,
1119 sizeof(lm_key)) != 0) {
1120 d_printf("LM Session Key does not match expectations (zeros)!\n");
1121 d_printf("lm_key:\n");
1122 dump_data(1, lm_key, 8);
1123 d_printf("expected:\n");
1124 dump_data(1, zeros, 8);
1128 if (memcmp(nt_key, user_session_key, 16) != 0) {
1129 d_printf("NT Session Key does not match expectations (should be NT Key)!\n");
1130 d_printf("user_session_key:\n");
1131 dump_data(1, user_session_key, sizeof(user_session_key));
1132 d_printf("expected:\n");
1133 dump_data(1, nt_key, sizeof(nt_key));
1139 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
1142 DATA_BLOB nt_response = data_blob(NULL, 0);
1143 DATA_BLOB lm_response = data_blob(NULL, 0);
1148 uint8_t user_session_key[16];
1150 uint8_t lm_hash[16];
1151 static const uint8_t zeros[8];
1152 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
1153 BOOL lm_good = E_deshash(samlogon_state->password, lm_hash);
1155 ZERO_STRUCT(user_session_key);
1157 if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw,
1158 samlogon_state->password)) == -1) {
1159 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1163 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1165 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1167 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
1170 (void**)&dospw)) == -1) {
1171 DEBUG(0, ("convert_string_talloc failed!\n"));
1175 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1177 nt_status = check_samlogon(samlogon_state,
1179 samlogon_state->parameter_control | MSV1_0_CLEARTEXT_PASSWORD_ALLOWED,
1187 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
1188 /* for 'old' passwords, we allow the server to be OK or wrong password */
1189 if (samlogon_state->old_password) {
1192 /* for 'long' passwords, the LM password is invalid */
1193 if (break_which == NO_NT && !lm_good) {
1196 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
1197 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, nt_status) && strchr_m(samlogon_state->account_name, '@')) {
1198 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH) || (break_which == NO_NT));
1199 } else if (!NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status)) {
1200 SAFE_FREE(*error_string);
1201 asprintf(error_string, "Expected error: %s, got %s", nt_errstr(samlogon_state->expected_error), nt_errstr(nt_status));
1203 } else if (NT_STATUS_EQUAL(samlogon_state->expected_error, nt_status) && !NT_STATUS_IS_OK(nt_status)) {
1205 } else if (!NT_STATUS_IS_OK(nt_status)) {
1209 if (break_which == NO_NT && !lm_good) {
1210 *error_string = strdup("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
1217 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
1218 char **error_string) {
1219 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1222 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
1223 char **error_string) {
1224 return test_plaintext(samlogon_state, BREAK_LM, error_string);
1227 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
1228 char **error_string) {
1229 return test_plaintext(samlogon_state, BREAK_NT, error_string);
1232 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
1233 char **error_string) {
1234 return test_plaintext(samlogon_state, NO_LM, error_string);
1237 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
1238 char **error_string) {
1239 return test_plaintext(samlogon_state, NO_NT, error_string);
1253 - plaintext tests (in challenge-response fields)
1255 check we get the correct session key in each case
1256 check what values we get for the LM session key
1260 static const struct ntlm_tests {
1261 BOOL (*fn)(struct samlogon_state *, char **);
1265 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
1267 {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", False},
1269 {test_lm, "LM", False},
1270 {test_lm_ntlm, "LM and NTLM", False},
1271 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
1272 {test_ntlm, "NTLM", False},
1273 {test_ntlm_in_lm, "NTLM in LM", False},
1274 {test_ntlm_in_both, "NTLM in both", False},
1275 {test_ntlmv2, "NTLMv2", False},
1276 {test_ntlmv2_no_dom, "NTLMv2 (no domain)", False},
1277 {test_lmv2, "LMv2", False},
1278 {test_lmv2_no_dom, "LMv2 (no domain)", False},
1279 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
1280 {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", False},
1281 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
1283 {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", False},
1285 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
1286 {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", False},
1287 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
1288 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
1289 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
1290 {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", False},
1291 {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", False},
1292 {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", False},
1293 {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", False},
1294 {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", False},
1295 {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", False},
1296 {test_plaintext_none_broken, "Plaintext", False},
1297 {test_plaintext_lm_broken, "Plaintext LM broken", False},
1298 {test_plaintext_nt_broken, "Plaintext NT broken", False},
1299 {test_plaintext_nt_only, "Plaintext NT only", False},
1300 {test_plaintext_lm_only, "Plaintext LM only", False},
1305 try a netlogon SamLogon
1307 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1308 struct creds_CredentialState *creds,
1309 const char *comment,
1310 const char *account_domain, const char *account_name,
1311 const char *plain_pass, uint32_t parameter_control,
1312 NTSTATUS expected_error, BOOL old_password,
1315 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1318 int validation_levels[] = {2,3,6};
1319 int logon_levels[] = { 2, 6 };
1320 int function_levels[] = {
1321 DCERPC_NETR_LOGONSAMLOGON,
1322 DCERPC_NETR_LOGONSAMLOGONEX,
1323 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
1324 struct samlogon_state samlogon_state;
1326 d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1328 samlogon_state.comment = comment;
1329 samlogon_state.account_name = account_name;
1330 samlogon_state.account_domain = account_domain;
1331 samlogon_state.password = plain_pass;
1332 samlogon_state.p = p;
1333 samlogon_state.creds = creds;
1334 samlogon_state.expected_error = expected_error;
1335 samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1336 samlogon_state.parameter_control = parameter_control;
1337 samlogon_state.old_password = old_password;
1339 generate_random_buffer(samlogon_state.chall.data, 8);
1340 samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1341 samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
1342 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1343 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1344 samlogon_state.r_flags.in.flags = 0;
1346 samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1347 samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
1348 samlogon_state.r_ex.in.flags = 0;
1350 samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1351 samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
1352 samlogon_state.r.in.credential = &samlogon_state.auth;
1353 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1355 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1356 for (i=0; test_table[i].fn; i++) {
1357 if (n_subtests && (i > n_subtests)) {
1360 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1361 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1362 char *error_string = NULL;
1363 TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1364 samlogon_state.mem_ctx = tmp_ctx;
1365 samlogon_state.function_level = function_levels[f];
1366 samlogon_state.r.in.validation_level = validation_levels[v];
1367 samlogon_state.r.in.logon_level = logon_levels[l];
1368 samlogon_state.r_ex.in.validation_level = validation_levels[v];
1369 samlogon_state.r_ex.in.logon_level = logon_levels[l];
1370 samlogon_state.r_flags.in.validation_level = validation_levels[v];
1371 samlogon_state.r_flags.in.logon_level = logon_levels[l];
1372 if (!test_table[i].fn(&samlogon_state, &error_string)) {
1373 d_printf("Testing '%s' [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n",
1374 samlogon_state.comment,
1375 samlogon_state.account_domain,
1376 samlogon_state.account_name,
1377 test_table[i].name, validation_levels[v],
1378 logon_levels[l], function_levels[f]);
1380 if (test_table[i].expect_fail) {
1381 d_printf(" failed (expected, test incomplete): %s\n", error_string);
1383 d_printf(" failed: %s\n", error_string);
1386 SAFE_FREE(error_string);
1388 talloc_free(tmp_ctx);
1393 talloc_free(fn_ctx);
1398 test an ADS style interactive domain logon
1400 BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1401 struct creds_CredentialState *creds,
1402 const char *comment,
1403 const char *workstation_name,
1404 const char *account_domain, const char *account_name,
1405 const char *plain_pass, uint32_t parameter_control,
1406 NTSTATUS expected_error)
1409 TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1410 struct netr_LogonSamLogonWithFlags r;
1411 struct netr_Authenticator a, ra;
1412 struct netr_PasswordInfo pinfo;
1418 creds_client_authenticator(creds, &a);
1420 r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1421 r.in.computer_name = TEST_MACHINE_NAME;
1422 r.in.credential = &a;
1423 r.in.return_authenticator = &ra;
1424 r.in.logon_level = 5;
1425 r.in.logon.password = &pinfo;
1426 r.in.validation_level = 6;
1429 pinfo.identity_info.domain_name.string = account_domain;
1430 pinfo.identity_info.parameter_control = parameter_control;
1431 pinfo.identity_info.logon_id_low = 0;
1432 pinfo.identity_info.logon_id_high = 0;
1433 pinfo.identity_info.account_name.string = account_name;
1434 pinfo.identity_info.workstation.string = workstation_name;
1436 if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1437 ZERO_STRUCT(pinfo.lmpassword.hash);
1439 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1441 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1442 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1443 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1445 creds_des_encrypt(creds, &pinfo.lmpassword);
1446 creds_des_encrypt(creds, &pinfo.ntpassword);
1449 d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
1451 status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1452 if (!r.out.return_authenticator
1453 || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1454 d_printf("Credential chaining failed\n");
1455 talloc_free(fn_ctx);
1459 talloc_free(fn_ctx);
1461 if (!NT_STATUS_EQUAL(expected_error, status)) {
1462 d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n",
1463 account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
1472 BOOL torture_rpc_samlogon(struct torture_context *torture)
1475 struct dcerpc_pipe *p;
1476 struct dcerpc_binding *b;
1477 struct cli_credentials *machine_credentials;
1478 TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1480 struct test_join *join_ctx;
1481 struct test_join *user_ctx;
1482 char *user_password;
1483 const char *old_user_password;
1484 char *test_machine_account;
1485 const char *binding = torture_setting_string(torture, "binding", NULL);
1486 const char *userdomain;
1490 unsigned int credential_flags[] = {
1491 NETLOGON_NEG_AUTH2_FLAGS,
1492 NETLOGON_NEG_ARCFOUR,
1493 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1494 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1495 0 /* yes, this is a valid flag, causes the use of DES */
1498 struct creds_CredentialState *creds;
1500 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1501 /* We only need to join as a workstation here, and in future,
1502 * if we wish to test against trusted domains, we must be a
1503 * workstation here */
1504 join_ctx = torture_join_domain(TEST_MACHINE_NAME, ACB_WSTRUST,
1505 &machine_credentials);
1507 d_printf("Failed to join as Workstation\n");
1511 userdomain = torture_setting_string(torture, "userdomain", lp_workgroup());
1513 user_ctx = torture_create_testuser(TEST_USER_NAME,
1516 (const char **)&user_password);
1518 d_printf("Failed to join as Workstation\n");
1522 old_user_password = user_password;
1524 test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
1525 TEST_USER_NAME, 16 /* > 14 */, &user_password,
1528 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1529 if (!NT_STATUS_IS_OK(status)) {
1530 d_printf("Bad binding string %s\n", binding);
1535 /* We have to use schannel, otherwise the SamLogonEx fails
1536 * with INTERNAL_ERROR */
1538 b->flags &= ~DCERPC_AUTH_OPTIONS;
1539 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1541 status = dcerpc_pipe_connect_b(mem_ctx, &p, b,
1542 &dcerpc_table_netlogon,
1543 machine_credentials, NULL);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1551 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1552 if (!NT_STATUS_IS_OK(status)) {
1560 const char *comment;
1562 const char *username;
1563 const char *password;
1565 NTSTATUS expected_interactive_error;
1566 NTSTATUS expected_network_error;
1567 uint32_t parameter_control;
1568 BOOL old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
1571 .comment = "domain\\user",
1572 .domain = cli_credentials_get_domain(cmdline_credentials),
1573 .username = cli_credentials_get_username(cmdline_credentials),
1574 .password = cli_credentials_get_password(cmdline_credentials),
1575 .network_login = True,
1576 .expected_interactive_error = NT_STATUS_OK,
1577 .expected_network_error = NT_STATUS_OK
1580 .comment = "realm\\user",
1581 .domain = cli_credentials_get_realm(cmdline_credentials),
1582 .username = cli_credentials_get_username(cmdline_credentials),
1583 .password = cli_credentials_get_password(cmdline_credentials),
1584 .network_login = True,
1585 .expected_interactive_error = NT_STATUS_OK,
1586 .expected_network_error = NT_STATUS_OK
1589 .comment = "user@domain",
1591 .username = talloc_asprintf(mem_ctx,
1593 cli_credentials_get_username(cmdline_credentials),
1594 cli_credentials_get_domain(cmdline_credentials)
1596 .password = cli_credentials_get_password(cmdline_credentials),
1597 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1598 .expected_interactive_error = NT_STATUS_OK,
1599 .expected_network_error = NT_STATUS_OK
1602 .comment = "user@realm",
1604 .username = talloc_asprintf(mem_ctx,
1606 cli_credentials_get_username(cmdline_credentials),
1607 cli_credentials_get_realm(cmdline_credentials)
1609 .password = cli_credentials_get_password(cmdline_credentials),
1610 .network_login = True,
1611 .expected_interactive_error = NT_STATUS_OK,
1612 .expected_network_error = NT_STATUS_OK
1615 .comment = "machine domain\\user",
1616 .domain = cli_credentials_get_domain(machine_credentials),
1617 .username = cli_credentials_get_username(machine_credentials),
1618 .password = cli_credentials_get_password(machine_credentials),
1619 .network_login = True,
1620 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1621 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1624 .comment = "machine domain\\user",
1625 .domain = cli_credentials_get_domain(machine_credentials),
1626 .username = cli_credentials_get_username(machine_credentials),
1627 .password = cli_credentials_get_password(machine_credentials),
1628 .network_login = True,
1629 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1630 .expected_network_error = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
1633 .comment = "machine realm\\user",
1634 .domain = cli_credentials_get_realm(machine_credentials),
1635 .username = cli_credentials_get_username(machine_credentials),
1636 .password = cli_credentials_get_password(machine_credentials),
1637 .network_login = True,
1638 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1639 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1642 .comment = "machine user@domain",
1644 .username = talloc_asprintf(mem_ctx,
1646 cli_credentials_get_username(machine_credentials),
1647 cli_credentials_get_domain(machine_credentials)
1649 .password = cli_credentials_get_password(machine_credentials),
1650 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1651 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1652 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1655 .comment = "machine user@realm",
1657 .username = talloc_asprintf(mem_ctx,
1659 cli_credentials_get_username(machine_credentials),
1660 cli_credentials_get_realm(machine_credentials)
1662 .password = cli_credentials_get_password(machine_credentials),
1663 .network_login = True,
1664 .expected_interactive_error = NT_STATUS_NO_SUCH_USER,
1665 .parameter_control = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
1668 .comment = "test user (long pw): domain\\user",
1669 .domain = userdomain,
1670 .username = TEST_USER_NAME,
1671 .password = user_password,
1672 .network_login = True,
1673 .expected_interactive_error = NT_STATUS_OK,
1674 .expected_network_error = NT_STATUS_OK
1677 .comment = "test user (long pw): user@realm",
1679 .username = talloc_asprintf(mem_ctx,
1683 .password = user_password,
1684 .network_login = True,
1685 .expected_interactive_error = NT_STATUS_OK,
1686 .expected_network_error = NT_STATUS_OK
1689 .comment = "test user (long pw): user@domain",
1691 .username = talloc_asprintf(mem_ctx,
1695 .password = user_password,
1696 .network_login = False, /* works for some things, but not NTLMv2. Odd */
1697 .expected_interactive_error = NT_STATUS_OK,
1698 .expected_network_error = NT_STATUS_OK
1700 /* Oddball, can we use the old password ? */
1702 .comment = "test user: user\\domain OLD PASSWORD",
1703 .domain = userdomain,
1704 .username = TEST_USER_NAME,
1705 .password = old_user_password,
1706 .network_login = True,
1707 .expected_interactive_error = NT_STATUS_WRONG_PASSWORD,
1708 .expected_network_error = NT_STATUS_OK,
1709 .old_password = True
1713 /* Try all the tests for different username forms */
1714 for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1716 if (!test_InteractiveLogon(p, mem_ctx, creds,
1717 usercreds[ci].comment,
1719 usercreds[ci].domain,
1720 usercreds[ci].username,
1721 usercreds[ci].password,
1722 usercreds[ci].parameter_control,
1723 usercreds[ci].expected_interactive_error)) {
1727 if (usercreds[ci].network_login) {
1728 if (!test_SamLogon(p, mem_ctx, creds,
1729 usercreds[ci].comment,
1730 usercreds[ci].domain,
1731 usercreds[ci].username,
1732 usercreds[ci].password,
1733 usercreds[ci].parameter_control,
1734 usercreds[ci].expected_network_error,
1735 usercreds[ci].old_password,
1742 /* Using the first username form, try the different
1743 * credentials flag setups, on only one of the tests (checks
1744 * session key encryption) */
1746 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1747 /* TODO: Somehow we lost setting up the different credential flags here! */
1749 if (!test_InteractiveLogon(p, mem_ctx, creds,
1750 usercreds[0].comment,
1752 usercreds[0].domain,
1753 usercreds[0].username,
1754 usercreds[0].password,
1755 usercreds[0].parameter_control,
1756 usercreds[0].expected_interactive_error)) {
1760 if (usercreds[0].network_login) {
1761 if (!test_SamLogon(p, mem_ctx, creds,
1762 usercreds[0].comment,
1763 usercreds[0].domain,
1764 usercreds[0].username,
1765 usercreds[0].password,
1766 usercreds[0].parameter_control,
1767 usercreds[0].expected_network_error,
1768 usercreds[0].old_password,
1777 talloc_free(mem_ctx);
1779 torture_leave_domain(join_ctx);
1780 torture_leave_domain(user_ctx);