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 "auth/auth.h"
28 #include "lib/crypto/crypto.h"
30 #define TEST_MACHINE_NAME "samlogontest"
41 struct samlogon_state {
43 const char *account_name;
44 const char *account_domain;
46 struct dcerpc_pipe *p;
48 struct netr_LogonSamLogon r;
49 struct netr_LogonSamLogonWithFlags r_flags;
50 struct netr_Authenticator auth, auth2;
51 struct creds_CredentialState *creds;
57 Authenticate a user with a challenge/response, checking session key
58 and valid authentication types
60 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
61 enum ntlm_break break_which,
63 DATA_BLOB *lm_response,
64 DATA_BLOB *nt_response,
66 uint8_t user_session_key[16],
70 struct netr_LogonSamLogon *r = &samlogon_state->r;
71 struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
72 struct netr_NetworkInfo ninfo;
74 struct netr_SamBaseInfo *base;
76 uint16 validation_level;
78 samlogon_state->r.in.logon.network = &ninfo;
79 samlogon_state->r_flags.in.logon.network = &ninfo;
81 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
82 ninfo.identity_info.parameter_control = 0;
83 ninfo.identity_info.logon_id_low = 0;
84 ninfo.identity_info.logon_id_high = 0;
85 ninfo.identity_info.account_name.string = samlogon_state->account_name;
86 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
88 memcpy(ninfo.challenge, chall->data, 8);
90 switch (break_which) {
94 if (lm_response && lm_response->data) {
95 lm_response->data[0]++;
99 if (nt_response && nt_response->data) {
100 nt_response->data[0]++;
104 if (lm_response && lm_response->data) {
105 lm_response->data[0]++;
107 if (nt_response && nt_response->data) {
108 nt_response->data[0]++;
112 data_blob_free(lm_response);
115 data_blob_free(nt_response);
120 ninfo.nt.data = nt_response->data;
121 ninfo.nt.length = nt_response->length;
123 ninfo.nt.data = NULL;
128 ninfo.lm.data = lm_response->data;
129 ninfo.lm.length = lm_response->length;
131 ninfo.lm.data = NULL;
135 ZERO_STRUCT(samlogon_state->auth2);
136 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
138 switch (samlogon_state->function_level) {
139 case DCERPC_NETR_LOGONSAMLOGON:
140 r->out.return_authenticator = NULL;
141 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
142 if (!r->out.return_authenticator ||
143 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
144 printf("Credential chaining failed\n");
146 if (!NT_STATUS_IS_OK(status)) {
148 *error_string = strdup(nt_errstr(status));
152 validation_level = r->in.validation_level;
153 switch (validation_level) {
155 base = &r->out.validation.sam2->base;
158 base = &r->out.validation.sam3->base;
161 base = &r->out.validation.sam6->base;
165 case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS:
166 r_flags->out.return_authenticator = NULL;
167 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
168 if (!r_flags->out.return_authenticator ||
169 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
170 printf("Credential chaining failed\n");
172 if (!NT_STATUS_IS_OK(status)) {
174 *error_string = strdup(nt_errstr(status));
178 validation_level = r_flags->in.validation_level;
179 switch (validation_level) {
181 base = &r_flags->out.validation.sam2->base;
184 base = &r_flags->out.validation.sam3->base;
187 base = &r_flags->out.validation.sam6->base;
194 if (!NT_STATUS_IS_OK(status)) {
195 /* we cannot check the session key, if the logon failed... */
200 /* find and decyrpt the session keys, return in parameters above */
201 if (validation_level == 6) {
202 /* they aren't encrypted! */
203 if (user_session_key) {
204 memcpy(user_session_key, base->key.key, 16);
207 memcpy(lm_key, base->LMSessKey.key, 8);
209 } else if (samlogon_state->creds->negotiate_flags) {
210 static const char zeros[16];
212 if (memcmp(base->key.key, zeros,
213 sizeof(base->key.key)) != 0) {
214 creds_arcfour_crypt(samlogon_state->creds,
216 sizeof(base->key.key));
219 if (user_session_key) {
220 memcpy(user_session_key, base->key.key, 16);
223 if (memcmp(base->LMSessKey.key, zeros,
224 sizeof(base->LMSessKey.key)) != 0) {
225 creds_arcfour_crypt(samlogon_state->creds,
227 sizeof(base->LMSessKey.key));
231 memcpy(lm_key, base->LMSessKey.key, 8);
234 static const char zeros[16];
236 if (user_session_key) {
237 memcpy(user_session_key, base->key.key, 16);
240 if (memcmp(base->LMSessKey.key, zeros,
241 sizeof(base->LMSessKey.key)) != 0) {
242 creds_des_decrypt_LMKey(samlogon_state->creds,
247 memcpy(lm_key, base->LMSessKey.key, 8);
256 * Test the normal 'LM and NTLM' combination
259 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
264 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
265 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
266 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
269 uint8_t user_session_key[16];
274 ZERO_STRUCT(user_session_key);
276 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
278 ZERO_STRUCT(lm_hash);
280 E_deshash(samlogon_state->password, lm_hash);
283 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
285 E_md4hash(samlogon_state->password, nt_hash);
286 SMBsesskeygen_ntv1(nt_hash, session_key.data);
288 nt_status = check_samlogon(samlogon_state,
290 &samlogon_state->chall,
297 data_blob_free(&lm_response);
299 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
300 /* for 'long' passwords, the LM password is invalid */
301 if (break_which == NO_NT && !lm_good) {
304 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
307 if (!NT_STATUS_IS_OK(nt_status)) {
311 if (break_which == NO_NT && !lm_good) {
312 printf("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
316 if (memcmp(lm_hash, lm_key,
317 sizeof(lm_key)) != 0) {
318 printf("LM Key does not match expectations!\n");
320 dump_data(1, (const char *)lm_key, 8);
321 printf("expected:\n");
322 dump_data(1, (const char *)lm_hash, 8);
326 switch (break_which) {
329 char lm_key_expected[16];
330 memcpy(lm_key_expected, lm_hash, 8);
331 memset(lm_key_expected+8, '\0', 8);
332 if (memcmp(lm_key_expected, user_session_key,
334 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
335 printf("user_session_key:\n");
336 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
337 printf("expected:\n");
338 dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
344 if (memcmp(session_key.data, user_session_key,
345 sizeof(user_session_key)) != 0) {
346 printf("NT Session Key does not match expectations!\n");
347 printf("user_session_key:\n");
348 dump_data(1, (const char *)user_session_key, 16);
349 printf("expected:\n");
350 dump_data(1, (const char *)session_key.data, session_key.length);
358 * Test LM authentication, no NT response supplied
361 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string)
364 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
368 * Test the NTLM response only, no LM.
371 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
373 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
377 * Test the NTLM response only, but in the LM field.
380 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
384 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
388 uint8_t user_session_key[16];
390 ZERO_STRUCT(user_session_key);
392 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
394 E_deshash(samlogon_state->password, lm_hash);
396 nt_status = check_samlogon(samlogon_state,
398 &samlogon_state->chall,
405 if (!NT_STATUS_IS_OK(nt_status)) {
409 if (memcmp(lm_hash, lm_key,
410 sizeof(lm_key)) != 0) {
411 printf("LM Key does not match expectations!\n");
413 dump_data(1, (const char *)lm_key, 8);
414 printf("expected:\n");
415 dump_data(1, (const char *)lm_hash, 8);
418 if (memcmp(lm_hash, user_session_key, 8) != 0) {
419 char lm_key_expected[16];
420 memcpy(lm_key_expected, lm_hash, 8);
421 memset(lm_key_expected+8, '\0', 8);
422 if (memcmp(lm_key_expected, user_session_key,
424 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
425 printf("user_session_key:\n");
426 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
427 printf("expected:\n");
428 dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
436 * Test the NTLM response only, but in the both the NT and LM fields.
439 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
444 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
445 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
449 char user_session_key[16];
453 ZERO_STRUCT(user_session_key);
455 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
457 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
458 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
461 lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
463 ZERO_STRUCT(lm_hash);
466 nt_status = check_samlogon(samlogon_state,
468 &samlogon_state->chall,
475 if (!NT_STATUS_IS_OK(nt_status)) {
479 if (memcmp(lm_hash, lm_key,
480 sizeof(lm_key)) != 0) {
481 printf("LM Key does not match expectations!\n");
483 dump_data(1, lm_key, 8);
484 printf("expected:\n");
485 dump_data(1, lm_hash, 8);
488 if (memcmp(session_key.data, user_session_key,
489 sizeof(user_session_key)) != 0) {
490 printf("NT Session Key does not match expectations!\n");
491 printf("user_session_key:\n");
492 dump_data(1, user_session_key, 16);
493 printf("expected:\n");
494 dump_data(1, (const char *)session_key.data, session_key.length);
503 * Test the NTLMv2 and LMv2 responses
506 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
510 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
511 DATA_BLOB lmv2_response = data_blob(NULL, 0);
512 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
513 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
514 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
516 uint8_t lm_session_key[8];
517 uint8_t user_session_key[16];
519 ZERO_STRUCT(lm_session_key);
520 ZERO_STRUCT(user_session_key);
522 /* TODO - test with various domain cases, and without domain */
523 if (!SMBNTLMv2encrypt(samlogon_state->account_name, samlogon_state->account_domain,
524 samlogon_state->password, &samlogon_state->chall,
526 &lmv2_response, &ntlmv2_response,
527 &lmv2_session_key, &ntlmv2_session_key)) {
528 data_blob_free(&names_blob);
531 data_blob_free(&names_blob);
533 nt_status = check_samlogon(samlogon_state,
535 &samlogon_state->chall,
542 data_blob_free(&lmv2_response);
543 data_blob_free(&ntlmv2_response);
546 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
547 return break_which == BREAK_BOTH;
550 switch (break_which) {
552 if (memcmp(lmv2_session_key.data, user_session_key,
553 sizeof(user_session_key)) != 0) {
554 printf("USER (LMv2) Session Key does not match expectations!\n");
555 printf("user_session_key:\n");
556 dump_data(1, (const char *)user_session_key, 16);
557 printf("expected:\n");
558 dump_data(1, (const char *)lmv2_session_key.data, ntlmv2_session_key.length);
561 if (memcmp(lmv2_session_key.data, lm_session_key,
562 sizeof(lm_session_key)) != 0) {
563 printf("LM (LMv2) Session Key does not match expectations!\n");
564 printf("lm_session_key:\n");
565 dump_data(1, (const char *)lm_session_key, 8);
566 printf("expected:\n");
567 dump_data(1, (const char *)lmv2_session_key.data, 8);
572 if (memcmp(ntlmv2_session_key.data, user_session_key,
573 sizeof(user_session_key)) != 0) {
574 printf("USER (NTLMv2) Session Key does not match expectations!\n");
575 printf("user_session_key:\n");
576 dump_data(1, (const char *)user_session_key, 16);
577 printf("expected:\n");
578 dump_data(1, (const char *)ntlmv2_session_key.data, ntlmv2_session_key.length);
581 if (memcmp(ntlmv2_session_key.data, lm_session_key,
582 sizeof(lm_session_key)) != 0) {
583 printf("LM (NTLMv2) Session Key does not match expectations!\n");
584 printf("lm_session_key:\n");
585 dump_data(1, (const char *)lm_session_key, 8);
586 printf("expected:\n");
587 dump_data(1, (const char *)ntlmv2_session_key.data, 8);
596 * Test the NTLMv2 and LMv2 responses
599 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
601 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, error_string);
605 * Test the LMv2 response only
608 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
610 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, error_string);
614 * Test the NTLMv2 response only
617 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
619 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, error_string);
622 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
624 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
627 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
629 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
632 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
634 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
637 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
639 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
641 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
643 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, error_string);
646 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
648 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, error_string);
651 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
653 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, error_string);
657 * Test the NTLM2 response (extra challenge in LM feild)
659 * This test is the same as the 'break LM' test, but checks that the
660 * server implements NTLM2 session security in the right place
661 * (NETLOGON is the wrong place).
664 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
668 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
669 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
675 uint8_t user_session_key[16];
676 uint8_t expected_user_session_key[16];
677 uint8_t session_nonce_hash[16];
678 uint8_t client_chall[8];
680 struct MD5Context md5_session_nonce_ctx;
681 HMACMD5Context hmac_ctx;
683 ZERO_STRUCT(user_session_key);
685 generate_random_buffer(client_chall, 8);
687 MD5Init(&md5_session_nonce_ctx);
688 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
689 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
690 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
692 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
693 E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
694 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
697 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
699 memcpy(lm_response.data, session_nonce_hash, 8);
700 memset(lm_response.data + 8, 0, 16);
702 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
703 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
704 hmac_md5_update(client_chall, 8, &hmac_ctx);
705 hmac_md5_final(expected_user_session_key, &hmac_ctx);
707 nt_status = check_samlogon(samlogon_state,
709 &samlogon_state->chall,
716 if (!NT_STATUS_IS_OK(nt_status)) {
720 if (memcmp(lm_hash, lm_key,
721 sizeof(lm_key)) != 0) {
722 printf("LM Key does not match expectations!\n");
724 dump_data(1, (const char *)lm_key, 8);
725 printf("expected:\n");
726 dump_data(1, (const char *)lm_hash, 8);
729 if (memcmp(nt_key, user_session_key, 16) != 0) {
730 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
731 printf("user_session_key:\n");
732 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
733 printf("expected:\n");
734 dump_data(1, (const char *)nt_key, sizeof(nt_key));
740 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
743 DATA_BLOB nt_response = data_blob(NULL, 0);
744 DATA_BLOB lm_response = data_blob(NULL, 0);
749 uint8_t user_session_key[16];
751 static const uint8_t zeros[8];
752 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
754 ZERO_STRUCT(user_session_key);
756 if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw,
757 samlogon_state->password)) == -1) {
758 DEBUG(0, ("push_ucs2_allocate failed!\n"));
762 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
764 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
766 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
769 (void**)&dospw)) == -1) {
770 DEBUG(0, ("convert_string_talloc failed!\n"));
774 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
776 nt_status = check_samlogon(samlogon_state,
785 if (!NT_STATUS_IS_OK(nt_status)) {
786 return break_which == BREAK_NT;
792 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
793 char **error_string) {
794 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
797 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
798 char **error_string) {
799 return test_plaintext(samlogon_state, BREAK_LM, error_string);
802 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
803 char **error_string) {
804 return test_plaintext(samlogon_state, BREAK_NT, error_string);
807 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
808 char **error_string) {
809 return test_plaintext(samlogon_state, NO_LM, error_string);
812 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
813 char **error_string) {
814 return test_plaintext(samlogon_state, NO_NT, error_string);
828 - plaintext tests (in challenge-response fields)
830 check we get the correct session key in each case
831 check what values we get for the LM session key
835 static const struct ntlm_tests {
836 BOOL (*fn)(struct samlogon_state *, char **);
840 {test_lm, "LM", False},
841 {test_lm_ntlm, "LM and NTLM", False},
842 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
843 {test_ntlm, "NTLM", False},
844 {test_ntlm_in_lm, "NTLM in LM", False},
845 {test_ntlm_in_both, "NTLM in both", False},
846 {test_ntlmv2, "NTLMv2", False},
847 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
848 {test_lmv2, "LMv2", False},
849 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
850 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
851 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
852 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
853 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
854 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
855 {test_plaintext_none_broken, "Plaintext", True},
856 {test_plaintext_lm_broken, "Plaintext LM broken", True},
857 {test_plaintext_nt_broken, "Plaintext NT broken", True},
858 {test_plaintext_nt_only, "Plaintext NT only", True},
859 {test_plaintext_lm_only, "Plaintext LM only", True},
864 try a netlogon SamLogon
866 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
867 struct creds_CredentialState *creds)
871 int validation_levels[] = {2,3,6};
872 int logon_levels[] = { 2, 6 };
873 int function_levels[] = {
874 DCERPC_NETR_LOGONSAMLOGON,
875 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
876 struct samlogon_state samlogon_state;
878 printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
880 samlogon_state.mem_ctx = mem_ctx;
881 samlogon_state.account_name = lp_parm_string(-1, "torture", "username");
882 samlogon_state.account_domain = lp_parm_string(-1, "torture", "userdomain");
883 samlogon_state.password = lp_parm_string(-1, "torture", "password");
884 samlogon_state.p = p;
885 samlogon_state.creds = creds;
887 samlogon_state.chall = data_blob_talloc(mem_ctx, NULL, 8);
889 generate_random_buffer(samlogon_state.chall.data, 8);
890 samlogon_state.r_flags.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
891 samlogon_state.r_flags.in.workstation = TEST_MACHINE_NAME;
892 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
893 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
894 samlogon_state.r_flags.in.flags = 0;
896 samlogon_state.r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
897 samlogon_state.r.in.workstation = TEST_MACHINE_NAME;
898 samlogon_state.r.in.credential = &samlogon_state.auth;
899 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
901 for (i=0; test_table[i].fn; i++) {
902 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
903 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
904 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
905 char *error_string = NULL;
906 samlogon_state.function_level = function_levels[f];
907 samlogon_state.r.in.validation_level = validation_levels[v];
908 samlogon_state.r.in.logon_level = logon_levels[l];
909 samlogon_state.r_flags.in.validation_level = validation_levels[v];
910 samlogon_state.r_flags.in.logon_level = logon_levels[l];
911 if (!test_table[i].fn(&samlogon_state, &error_string)) {
912 printf("Testing '%s' at validation level %d, logon level %d, function %d: \n",
913 test_table[i].name, validation_levels[v],
914 logon_levels[l], function_levels[f]);
916 if (test_table[i].expect_fail) {
917 printf(" failed (expected, test incomplete): %s\n", error_string);
919 printf(" failed: %s\n", error_string);
922 SAFE_FREE(error_string);
933 test an ADS style interactive domain logon
935 static BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
936 struct creds_CredentialState *creds)
939 struct netr_LogonSamLogonWithFlags r;
940 struct netr_Authenticator a, ra;
941 struct netr_PasswordInfo pinfo;
942 const char *plain_pass;
948 creds_client_authenticator(creds, &a);
950 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
951 r.in.workstation = TEST_MACHINE_NAME;
952 r.in.credential = &a;
953 r.in.return_authenticator = &ra;
954 r.in.logon_level = 5;
955 r.in.logon.password = &pinfo;
956 r.in.validation_level = 6;
959 pinfo.identity_info.domain_name.string = lp_parm_string(-1, "torture", "userdomain");
960 pinfo.identity_info.parameter_control = 0;
961 pinfo.identity_info.logon_id_low = 0;
962 pinfo.identity_info.logon_id_high = 0;
963 pinfo.identity_info.account_name.string = lp_parm_string(-1, "torture", "username");
964 pinfo.identity_info.workstation.string = TEST_MACHINE_NAME;
966 plain_pass = lp_parm_string(-1, "torture", "password");
968 E_deshash(plain_pass, pinfo.lmpassword.hash);
969 E_md4hash(plain_pass, pinfo.ntpassword.hash);
971 if (creds->negotiate_flags) {
972 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
973 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
975 creds_des_encrypt(creds, &pinfo.lmpassword);
976 creds_des_encrypt(creds, &pinfo.ntpassword);
979 printf("Testing netr_LogonSamLogonWithFlags\n");
981 status = dcerpc_netr_LogonSamLogonWithFlags(p, mem_ctx, &r);
982 if (!NT_STATUS_IS_OK(status)) {
983 printf("netr_LogonSamLogonWithFlags - %s\n", nt_errstr(status));
987 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
988 printf("Credential chaining failed\n");
997 BOOL torture_rpc_samlogon(void)
1000 struct dcerpc_pipe *p;
1001 TALLOC_CTX *mem_ctx;
1004 const char *machine_pass;
1006 struct creds_CredentialState creds;
1008 mem_ctx = talloc_init("torture_rpc_netlogon");
1010 join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST,
1013 printf("Failed to join as BDC\n");
1017 status = torture_rpc_connection(&p,
1018 DCERPC_NETLOGON_NAME,
1019 DCERPC_NETLOGON_UUID,
1020 DCERPC_NETLOGON_VERSION);
1021 if (!NT_STATUS_IS_OK(status)) {
1026 if (!test_SetupCredentials(p, mem_ctx,
1027 TEST_MACHINE_NAME, machine_pass, &creds)) {
1031 if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1035 if (!test_SamLogon(p, mem_ctx, &creds)) {
1039 if (!test_SetupCredentials2(p, mem_ctx, NETLOGON_NEG_AUTH2_FLAGS,
1040 TEST_MACHINE_NAME, machine_pass, &creds)) {
1044 if (!test_SamLogon(p, mem_ctx, &creds)) {
1048 if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1052 if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_FLAGS,
1053 TEST_MACHINE_NAME, machine_pass, &creds)) {
1057 if (!test_SamLogon(p, mem_ctx, &creds)) {
1061 if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1065 if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
1066 TEST_MACHINE_NAME, machine_pass, &creds)) {
1070 if (!test_SamLogon(p, mem_ctx, &creds)) {
1074 if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1078 talloc_destroy(mem_ctx);
1080 torture_rpc_close(p);
1082 torture_leave_domain(join_ctx);