libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source4 / torture / rpc / schannel.c
blobe0862d246764191f211dab47f93447e45a43382e
1 /*
2 Unix SMB/CIFS implementation.
4 test suite for schannel operations
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_netlogon_c.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "auth/credentials/credentials.h"
27 #include "torture/rpc/torture_rpc.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "auth/gensec/schannel.h"
30 #include "../libcli/auth/schannel.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "libcli/security/security.h"
33 #include "system/filesys.h"
34 #include "param/param.h"
35 #include "librpc/rpc/dcerpc_proto.h"
36 #include "auth/gensec/gensec.h"
37 #include "libcli/composite/composite.h"
38 #include "lib/events/events.h"
40 #define TEST_MACHINE_NAME "schannel"
43 try a netlogon SamLogon
45 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx,
46 struct cli_credentials *credentials,
47 struct netlogon_creds_CredentialState *creds)
49 NTSTATUS status;
50 struct netr_LogonSamLogonEx r;
51 struct netr_NetworkInfo ninfo;
52 union netr_LogonLevel logon;
53 union netr_Validation validation;
54 uint8_t authoritative = 0;
55 uint32_t _flags = 0;
56 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
57 int i;
58 int flags = CLI_CRED_NTLM_AUTH;
59 struct dcerpc_binding_handle *b = p->binding_handle;
61 struct netr_UserSessionKey key;
62 struct netr_LMSessionKey LMSessKey;
63 uint32_t validation_levels[] = { 2, 3 };
64 struct netr_SamBaseInfo *base;
65 const char *crypto_alg = "";
66 bool can_do_validation_6 = true;
68 if (lpcfg_client_lanman_auth(tctx->lp_ctx)) {
69 flags |= CLI_CRED_LANMAN_AUTH;
72 if (lpcfg_client_ntlmv2_auth(tctx->lp_ctx)) {
73 flags |= CLI_CRED_NTLMv2_AUTH;
76 cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
77 &ninfo.identity_info.account_name.string,
78 &ninfo.identity_info.domain_name.string);
80 generate_random_buffer(ninfo.challenge,
81 sizeof(ninfo.challenge));
82 chal = data_blob_const(ninfo.challenge,
83 sizeof(ninfo.challenge));
85 names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
86 cli_credentials_get_domain(credentials));
88 status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
89 &flags,
90 chal,
91 names_blob,
92 &lm_resp, &nt_resp,
93 NULL, NULL);
94 torture_assert_ntstatus_ok(tctx, status,
95 "cli_credentials_get_ntlm_response failed");
97 ninfo.lm.data = lm_resp.data;
98 ninfo.lm.length = lm_resp.length;
100 ninfo.nt.data = nt_resp.data;
101 ninfo.nt.length = nt_resp.length;
103 ninfo.identity_info.parameter_control = 0;
104 ninfo.identity_info.logon_id_low = 0;
105 ninfo.identity_info.logon_id_high = 0;
106 ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
108 logon.network = &ninfo;
110 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
111 r.in.computer_name = cli_credentials_get_workstation(credentials);
112 r.in.logon_level = NetlogonNetworkInformation;
113 r.in.logon= &logon;
114 r.in.flags = &_flags;
115 r.out.validation = &validation;
116 r.out.authoritative = &authoritative;
117 r.out.flags = &_flags;
120 - retrieve level6
121 - save usrsession and lmsession key
122 - retrieve level 2
123 - calculate, compare
124 - retrieve level 3
125 - calculate, compare
128 if (creds) {
129 if (creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
130 crypto_alg = "AES";
131 } else if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
132 crypto_alg = "ARCFOUR";
136 r.in.validation_level = 6;
138 torture_comment(tctx,
139 "Testing LogonSamLogonEx with name %s using %s and validation_level: %d\n",
140 ninfo.identity_info.account_name.string, crypto_alg,
141 r.in.validation_level);
143 torture_assert_ntstatus_ok(tctx,
144 dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
145 "LogonSamLogonEx failed");
146 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_INVALID_INFO_CLASS)) {
147 can_do_validation_6 = false;
148 } else {
149 torture_assert_ntstatus_ok(tctx, r.out.result,
150 "LogonSamLogonEx failed");
152 key = r.out.validation->sam6->base.key;
153 LMSessKey = r.out.validation->sam6->base.LMSessKey;
155 DEBUG(1,("unencrypted session keys from validation_level 6:\n"));
156 dump_data(1, r.out.validation->sam6->base.key.key, 16);
157 dump_data(1, r.out.validation->sam6->base.LMSessKey.key, 8);
160 for (i=0; i < ARRAY_SIZE(validation_levels); i++) {
162 r.in.validation_level = validation_levels[i];
164 torture_comment(tctx,
165 "Testing LogonSamLogonEx with name %s using %s and validation_level: %d\n",
166 ninfo.identity_info.account_name.string, crypto_alg,
167 r.in.validation_level);
169 torture_assert_ntstatus_ok(tctx,
170 dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
171 "LogonSamLogonEx failed");
172 torture_assert_ntstatus_ok(tctx, r.out.result,
173 "LogonSamLogonEx failed");
175 if (creds == NULL) {
176 /* when this test is called without creds no point in
177 * testing the session keys */
178 continue;
181 switch (validation_levels[i]) {
182 case 2:
183 base = &r.out.validation->sam2->base;
184 break;
185 case 3:
186 base = &r.out.validation->sam3->base;
187 break;
188 default:
189 break;
192 DEBUG(1,("encrypted keys validation_level %d:\n",
193 validation_levels[i]));
194 dump_data(1, base->key.key, 16);
195 dump_data(1, base->LMSessKey.key, 8);
197 if (creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
198 netlogon_creds_aes_decrypt(creds, base->key.key, 16);
199 netlogon_creds_aes_decrypt(creds, base->LMSessKey.key, 8);
200 } else if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
201 netlogon_creds_arcfour_crypt(creds, base->key.key, 16);
202 netlogon_creds_arcfour_crypt(creds, base->LMSessKey.key, 8);
205 DEBUG(1,("decryped keys validation_level %d\n",
206 validation_levels[i]));
208 dump_data(1, base->key.key, 16);
209 dump_data(1, base->LMSessKey.key, 8);
211 if (!can_do_validation_6) {
212 /* we cant compare against unencrypted keys */
213 continue;
216 torture_assert_mem_equal(tctx,
217 base->key.key,
218 key.key,
220 "unexpected user session key\n");
221 torture_assert_mem_equal(tctx,
222 base->LMSessKey.key,
223 LMSessKey.key,
225 "unexpected LM session key\n");
228 return true;
232 do some samr ops using the schannel connection
234 static bool test_samr_ops(struct torture_context *tctx,
235 struct dcerpc_binding_handle *b)
237 struct samr_GetDomPwInfo r;
238 struct samr_PwInfo info;
239 struct samr_Connect connect_r;
240 struct samr_OpenDomain opendom;
241 int i;
242 struct lsa_String name;
243 struct policy_handle handle;
244 struct policy_handle domain_handle;
246 name.string = lpcfg_workgroup(tctx->lp_ctx);
247 r.in.domain_name = &name;
248 r.out.info = &info;
250 connect_r.in.system_name = 0;
251 connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
252 connect_r.out.connect_handle = &handle;
254 torture_comment(tctx, "Testing Connect and OpenDomain on BUILTIN\n");
256 torture_assert_ntstatus_ok(tctx, dcerpc_samr_Connect_r(b, tctx, &connect_r),
257 "Connect failed");
258 if (!NT_STATUS_IS_OK(connect_r.out.result)) {
259 if (NT_STATUS_EQUAL(connect_r.out.result, NT_STATUS_ACCESS_DENIED)) {
260 torture_comment(tctx, "Connect failed (expected, schannel mapped to anonymous): %s\n",
261 nt_errstr(connect_r.out.result));
262 } else {
263 torture_comment(tctx, "Connect failed - %s\n", nt_errstr(connect_r.out.result));
264 return false;
266 } else {
267 opendom.in.connect_handle = &handle;
268 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
269 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
270 opendom.out.domain_handle = &domain_handle;
272 torture_assert_ntstatus_ok(tctx, dcerpc_samr_OpenDomain_r(b, tctx, &opendom),
273 "OpenDomain failed");
274 if (!NT_STATUS_IS_OK(opendom.out.result)) {
275 torture_comment(tctx, "OpenDomain failed - %s\n", nt_errstr(opendom.out.result));
276 return false;
280 torture_comment(tctx, "Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
282 /* do several ops to test credential chaining */
283 for (i=0;i<5;i++) {
284 torture_assert_ntstatus_ok(tctx, dcerpc_samr_GetDomPwInfo_r(b, tctx, &r),
285 "GetDomPwInfo failed");
286 if (!NT_STATUS_IS_OK(r.out.result)) {
287 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED)) {
288 torture_comment(tctx, "GetDomPwInfo op %d failed - %s\n", i, nt_errstr(r.out.result));
289 return false;
294 return true;
299 do some lsa ops using the schannel connection
301 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
303 struct lsa_GetUserName r;
304 bool ret = true;
305 struct lsa_String *account_name_p = NULL;
306 struct lsa_String *authority_name_p = NULL;
307 struct dcerpc_binding_handle *b = p->binding_handle;
309 torture_comment(tctx, "\nTesting GetUserName\n");
311 r.in.system_name = "\\";
312 r.in.account_name = &account_name_p;
313 r.in.authority_name = &authority_name_p;
314 r.out.account_name = &account_name_p;
316 /* do several ops to test credential chaining and various operations */
317 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
318 "lsa_GetUserName failed");
320 authority_name_p = *r.out.authority_name;
322 if (!NT_STATUS_IS_OK(r.out.result)) {
323 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(r.out.result));
324 return false;
325 } else {
326 if (!r.out.account_name) {
327 return false;
330 if (strcmp(account_name_p->string, "ANONYMOUS LOGON") != 0) {
331 torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
332 account_name_p->string, "ANONYMOUS LOGON");
333 /* FIXME: gd */
334 if (!torture_setting_bool(tctx, "samba3", false)) {
335 return false;
338 if (!authority_name_p || !authority_name_p->string) {
339 return false;
342 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
343 torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
344 authority_name_p->string, "NT AUTHORITY");
345 /* FIXME: gd */
346 if (!torture_setting_bool(tctx, "samba3", false)) {
347 return false;
352 return ret;
357 test a schannel connection with the given flags
359 static bool test_schannel(struct torture_context *tctx,
360 uint16_t acct_flags, uint32_t dcerpc_flags,
361 int i)
363 struct test_join *join_ctx;
364 NTSTATUS status;
365 const char *binding = torture_setting_string(tctx, "binding", NULL);
366 struct dcerpc_binding *b;
367 struct dcerpc_pipe *p = NULL;
368 struct dcerpc_pipe *p_netlogon = NULL;
369 struct dcerpc_pipe *p_netlogon2 = NULL;
370 struct dcerpc_pipe *p_netlogon3 = NULL;
371 struct dcerpc_pipe *p_samr2 = NULL;
372 struct dcerpc_pipe *p_lsa = NULL;
373 struct netlogon_creds_CredentialState *creds;
374 struct cli_credentials *credentials;
375 enum dcerpc_transport_t transport;
377 join_ctx = torture_join_domain(tctx,
378 talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
379 acct_flags, &credentials);
380 torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
382 status = dcerpc_parse_binding(tctx, binding, &b);
383 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
385 b->flags &= ~DCERPC_AUTH_OPTIONS;
386 b->flags |= dcerpc_flags;
388 status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
389 credentials, tctx->ev, tctx->lp_ctx);
390 torture_assert_ntstatus_ok(tctx, status,
391 "Failed to connect to samr with schannel");
393 torture_assert(tctx, test_samr_ops(tctx, p->binding_handle),
394 "Failed to process schannel secured SAMR ops");
396 /* Also test that when we connect to the netlogon pipe, that
397 * the credentials we setup on the first pipe are valid for
398 * the second */
400 /* Swap the binding details from SAMR to NETLOGON */
401 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
402 torture_assert_ntstatus_ok(tctx, status, "epm map");
404 status = dcerpc_secondary_connection(p, &p_netlogon,
406 torture_assert_ntstatus_ok(tctx, status, "secondary connection");
408 status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon,
409 credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
410 DCERPC_AUTH_TYPE_SCHANNEL,
411 dcerpc_auth_level(p->conn),
412 NULL);
414 torture_assert_ntstatus_ok(tctx, status, "bind auth");
416 status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
417 torture_assert_ntstatus_ok(tctx, status, "schannel creds");
419 /* checks the capabilities */
420 torture_assert(tctx, test_netlogon_capabilities(p_netlogon, tctx, credentials, creds),
421 "Failed to process schannel secured capability ops (on fresh connection)");
423 /* do a couple of logins */
424 torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
425 "Failed to process schannel secured NETLOGON ops");
427 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
428 "Failed to process schannel secured NETLOGON EX ops");
430 /* we *MUST* use ncacn_np for openpolicy etc. */
431 transport = b->transport;
432 b->transport = NCACN_NP;
434 /* Swap the binding details from SAMR to LSARPC */
435 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
436 torture_assert_ntstatus_ok(tctx, status, "epm map");
438 torture_assert_ntstatus_ok(tctx,
439 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
440 credentials, tctx->ev, tctx->lp_ctx),
441 "failed to connect lsarpc with schannel");
443 torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
444 "Failed to process schannel secured LSA ops");
446 talloc_free(p_lsa);
447 p_lsa = NULL;
449 b->transport = transport;
451 /* we *MUST* use ncacn_ip_tcp for lookupsids3/lookupnames4 */
452 transport = b->transport;
453 b->transport = NCACN_IP_TCP;
455 torture_assert_ntstatus_ok(tctx,
456 dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx),
457 "failed to call epm map");
459 torture_assert_ntstatus_ok(tctx,
460 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
461 credentials, tctx->ev, tctx->lp_ctx),
462 "failed to connect lsarpc with schannel");
464 torture_assert(tctx,
465 test_many_LookupSids(p_lsa, tctx, NULL),
466 "LsaLookupSids3 failed!\n");
468 b->transport = transport;
470 /* Drop the socket, we want to start from scratch */
471 talloc_free(p);
472 p = NULL;
474 /* Now see what we are still allowed to do */
476 status = dcerpc_parse_binding(tctx, binding, &b);
477 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
479 b->flags &= ~DCERPC_AUTH_OPTIONS;
480 b->flags |= dcerpc_flags;
482 status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
483 credentials, tctx->ev, tctx->lp_ctx);
484 torture_assert_ntstatus_ok(tctx, status,
485 "Failed to connect with schannel");
487 /* do a some SAMR operations. We have *not* done a new serverauthenticate */
488 torture_assert (tctx, test_samr_ops(tctx, p_samr2->binding_handle),
489 "Failed to process schannel secured SAMR ops (on fresh connection)");
491 /* Swap the binding details from SAMR to NETLOGON */
492 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
493 torture_assert_ntstatus_ok(tctx, status, "epm");
495 status = dcerpc_secondary_connection(p_samr2, &p_netlogon2,
497 torture_assert_ntstatus_ok(tctx, status, "secondary connection");
499 /* and now setup an SCHANNEL bind on netlogon */
500 status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
501 credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
502 DCERPC_AUTH_TYPE_SCHANNEL,
503 dcerpc_auth_level(p_samr2->conn),
504 NULL);
506 torture_assert_ntstatus_ok(tctx, status, "auth failed");
508 /* checks the capabilities */
509 torture_assert(tctx, test_netlogon_capabilities(p_netlogon2, tctx, credentials, creds),
510 "Failed to process schannel secured capability ops (on fresh connection)");
512 /* Try the schannel-only SamLogonEx operation */
513 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
514 "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
517 /* And the more traditional style, proving that the
518 * credentials chaining state is fully present */
519 torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
520 "Failed to process schannel secured NETLOGON ops (on fresh connection)");
522 /* Drop the socket, we want to start from scratch (again) */
523 talloc_free(p_samr2);
525 /* We don't want schannel for this test */
526 b->flags &= ~DCERPC_AUTH_OPTIONS;
528 status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
529 credentials, tctx->ev, tctx->lp_ctx);
530 torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
532 torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
533 "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
535 /* Required because the previous call will mark the current context as having failed */
536 tctx->last_result = TORTURE_OK;
537 tctx->last_reason = NULL;
539 torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
540 "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
542 torture_leave_domain(tctx, join_ctx);
543 return true;
549 a schannel test suite
551 bool torture_rpc_schannel(struct torture_context *torture)
553 bool ret = true;
554 struct {
555 uint16_t acct_flags;
556 uint32_t dcerpc_flags;
557 } tests[] = {
558 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
559 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
560 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
561 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
562 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AES},
563 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AES },
564 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
565 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
566 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
567 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
568 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AES },
569 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AES }
571 int i;
573 for (i=0;i<ARRAY_SIZE(tests);i++) {
574 torture_comment(torture, "Testing with acct_flags=0x%x dcerpc_flags=0x%x \n",
575 tests[i].acct_flags, tests[i].dcerpc_flags);
577 if (!test_schannel(torture,
578 tests[i].acct_flags, tests[i].dcerpc_flags,
579 i)) {
580 torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
581 tests[i].acct_flags, tests[i].dcerpc_flags);
582 ret = false;
586 return ret;
590 test two schannel connections
592 bool torture_rpc_schannel2(struct torture_context *torture)
594 struct test_join *join_ctx;
595 NTSTATUS status;
596 const char *binding = torture_setting_string(torture, "binding", NULL);
597 struct dcerpc_binding *b;
598 struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
599 struct cli_credentials *credentials1, *credentials2;
600 uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
602 join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
603 ACB_WSTRUST, &credentials1);
604 torture_assert(torture, join_ctx != NULL,
605 "Failed to join domain with acct_flags=ACB_WSTRUST");
607 credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
608 credentials1->netlogon_creds = NULL;
609 credentials2->netlogon_creds = NULL;
611 status = dcerpc_parse_binding(torture, binding, &b);
612 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
614 b->flags &= ~DCERPC_AUTH_OPTIONS;
615 b->flags |= dcerpc_flags;
617 torture_comment(torture, "Opening first connection\n");
618 status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
619 credentials1, torture->ev, torture->lp_ctx);
620 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
622 torture_comment(torture, "Opening second connection\n");
623 status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
624 credentials2, torture->ev, torture->lp_ctx);
625 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
627 credentials1->netlogon_creds = NULL;
628 credentials2->netlogon_creds = NULL;
630 torture_comment(torture, "Testing logon on pipe1\n");
631 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
632 return false;
634 torture_comment(torture, "Testing logon on pipe2\n");
635 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
636 return false;
638 torture_comment(torture, "Again on pipe1\n");
639 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
640 return false;
642 torture_comment(torture, "Again on pipe2\n");
643 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
644 return false;
646 torture_leave_domain(torture, join_ctx);
647 return true;
650 struct torture_schannel_bench;
652 struct torture_schannel_bench_conn {
653 struct torture_schannel_bench *s;
654 int index;
655 struct cli_credentials *wks_creds;
656 struct dcerpc_pipe *pipe;
657 struct netr_LogonSamLogonEx r;
658 struct netr_NetworkInfo ninfo;
659 TALLOC_CTX *tmp;
660 uint64_t total;
661 uint32_t count;
664 struct torture_schannel_bench {
665 struct torture_context *tctx;
666 bool progress;
667 int timelimit;
668 int nprocs;
669 int nconns;
670 struct torture_schannel_bench_conn *conns;
671 struct test_join *join_ctx1;
672 struct cli_credentials *wks_creds1;
673 struct test_join *join_ctx2;
674 struct cli_credentials *wks_creds2;
675 struct cli_credentials *user1_creds;
676 struct cli_credentials *user2_creds;
677 struct dcerpc_binding *b;
678 NTSTATUS error;
679 uint64_t total;
680 uint32_t count;
681 bool stopped;
684 static void torture_schannel_bench_connected(struct composite_context *c)
686 struct torture_schannel_bench_conn *conn =
687 (struct torture_schannel_bench_conn *)c->async.private_data;
688 struct torture_schannel_bench *s = talloc_get_type(conn->s,
689 struct torture_schannel_bench);
691 s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
692 torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
693 if (NT_STATUS_IS_OK(s->error)) {
694 s->nconns++;
698 static void torture_schannel_bench_recv(struct tevent_req *subreq);
700 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
702 struct torture_schannel_bench *s = conn->s;
703 NTSTATUS status;
704 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
705 int flags = CLI_CRED_NTLM_AUTH;
706 struct tevent_req *subreq;
707 struct cli_credentials *user_creds;
709 if (conn->total % 2) {
710 user_creds = s->user1_creds;
711 } else {
712 user_creds = s->user2_creds;
715 if (lpcfg_client_lanman_auth(s->tctx->lp_ctx)) {
716 flags |= CLI_CRED_LANMAN_AUTH;
719 if (lpcfg_client_ntlmv2_auth(s->tctx->lp_ctx)) {
720 flags |= CLI_CRED_NTLMv2_AUTH;
723 talloc_free(conn->tmp);
724 conn->tmp = talloc_new(s);
725 ZERO_STRUCT(conn->ninfo);
726 ZERO_STRUCT(conn->r);
728 cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
729 &conn->ninfo.identity_info.account_name.string,
730 &conn->ninfo.identity_info.domain_name.string);
732 generate_random_buffer(conn->ninfo.challenge,
733 sizeof(conn->ninfo.challenge));
734 chal = data_blob_const(conn->ninfo.challenge,
735 sizeof(conn->ninfo.challenge));
737 names_blob = NTLMv2_generate_names_blob(conn->tmp,
738 cli_credentials_get_workstation(conn->wks_creds),
739 cli_credentials_get_domain(conn->wks_creds));
741 status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
742 &flags,
743 chal,
744 names_blob,
745 &lm_resp, &nt_resp,
746 NULL, NULL);
747 torture_assert_ntstatus_ok(s->tctx, status,
748 "cli_credentials_get_ntlm_response failed");
750 conn->ninfo.lm.data = lm_resp.data;
751 conn->ninfo.lm.length = lm_resp.length;
753 conn->ninfo.nt.data = nt_resp.data;
754 conn->ninfo.nt.length = nt_resp.length;
756 conn->ninfo.identity_info.parameter_control = 0;
757 conn->ninfo.identity_info.logon_id_low = 0;
758 conn->ninfo.identity_info.logon_id_high = 0;
759 conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
761 conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
762 conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
763 conn->r.in.logon_level = NetlogonNetworkInformation;
764 conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
765 conn->r.in.logon->network = &conn->ninfo;
766 conn->r.in.flags = talloc(conn->tmp, uint32_t);
767 conn->r.in.validation_level = 2;
768 conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
769 conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
770 conn->r.out.flags = conn->r.in.flags;
772 subreq = dcerpc_netr_LogonSamLogonEx_r_send(s, s->tctx->ev,
773 conn->pipe->binding_handle,
774 &conn->r);
775 torture_assert(s->tctx, subreq, "Failed to setup LogonSamLogonEx request");
777 tevent_req_set_callback(subreq, torture_schannel_bench_recv, conn);
779 return true;
782 static void torture_schannel_bench_recv(struct tevent_req *subreq)
784 bool ret;
785 struct torture_schannel_bench_conn *conn =
786 (struct torture_schannel_bench_conn *)tevent_req_callback_data_void(subreq);
787 struct torture_schannel_bench *s = talloc_get_type(conn->s,
788 struct torture_schannel_bench);
790 s->error = dcerpc_netr_LogonSamLogonEx_r_recv(subreq, subreq);
791 TALLOC_FREE(subreq);
792 if (!NT_STATUS_IS_OK(s->error)) {
793 return;
796 conn->total++;
797 conn->count++;
799 if (s->stopped) {
800 return;
803 ret = torture_schannel_bench_start(conn);
804 if (!ret) {
805 s->error = NT_STATUS_INTERNAL_ERROR;
810 test multiple schannel connection in parallel
812 bool torture_rpc_schannel_bench1(struct torture_context *torture)
814 bool ret = true;
815 NTSTATUS status;
816 const char *binding = torture_setting_string(torture, "binding", NULL);
817 struct torture_schannel_bench *s;
818 struct timeval start;
819 struct timeval end;
820 int i;
821 const char *tmp;
823 s = talloc_zero(torture, struct torture_schannel_bench);
824 s->tctx = torture;
825 s->progress = torture_setting_bool(torture, "progress", true);
826 s->timelimit = torture_setting_int(torture, "timelimit", 10);
827 s->nprocs = torture_setting_int(torture, "nprocs", 4);
828 s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
830 s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
831 cmdline_credentials,
832 sizeof(*s->user1_creds));
833 tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
834 if (tmp) {
835 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
837 s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
838 cmdline_credentials,
839 sizeof(*s->user1_creds));
840 tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
841 if (tmp) {
842 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
845 s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
846 ACB_WSTRUST, &s->wks_creds1);
847 torture_assert(torture, s->join_ctx1 != NULL,
848 "Failed to join domain with acct_flags=ACB_WSTRUST");
849 s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
850 ACB_WSTRUST, &s->wks_creds2);
851 torture_assert(torture, s->join_ctx2 != NULL,
852 "Failed to join domain with acct_flags=ACB_WSTRUST");
854 cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
855 cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
857 for (i=0; i < s->nprocs; i++) {
858 s->conns[i].s = s;
859 s->conns[i].index = i;
860 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
861 s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
862 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
863 memcpy(s->conns[i].wks_creds, s->wks_creds2,
864 talloc_get_size(s->conns[i].wks_creds));
866 s->conns[i].wks_creds->netlogon_creds = NULL;
869 status = dcerpc_parse_binding(s, binding, &s->b);
870 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
871 s->b->flags &= ~DCERPC_AUTH_OPTIONS;
872 s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
874 torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
875 for (i=0; i < s->nprocs; i++) {
876 #if 1
877 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
878 &ndr_table_netlogon,
879 s->conns[i].wks_creds,
880 torture->ev, torture->lp_ctx);
881 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
882 #else
884 * This path doesn't work against windows,
885 * because of windows drops the connections
886 * which haven't reached a session setup yet
888 * The same as the reset on zero vc stuff.
890 struct composite_context *c;
891 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
892 &ndr_table_netlogon,
893 s->conns[i].wks_creds,
894 torture->ev,
895 torture->lp_ctx);
896 torture_assert(torture, c != NULL, "Failed to setup connect");
897 c->async.fn = torture_schannel_bench_connected;
898 c->async.private_data = &s->conns[i];
901 while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
902 int ev_ret = tevent_loop_once(torture->ev);
903 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
904 #endif
906 torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
909 * Change the workstation password after establishing the netlogon
910 * schannel connections to prove that existing connections are not
911 * affected by a wks pwchange.
915 struct netr_ServerPasswordSet pwset;
916 char *password = generate_random_password(s->join_ctx1, 8, 255);
917 struct netlogon_creds_CredentialState *creds_state;
918 struct dcerpc_pipe *net_pipe;
919 struct netr_Authenticator credential, return_authenticator;
920 struct samr_Password new_password;
922 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
923 &ndr_table_netlogon,
924 s->wks_creds1,
925 torture->ev, torture->lp_ctx);
927 torture_assert_ntstatus_ok(torture, status,
928 "dcerpc_pipe_connect_b failed");
930 pwset.in.server_name = talloc_asprintf(
931 net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
932 pwset.in.computer_name =
933 cli_credentials_get_workstation(s->wks_creds1);
934 pwset.in.account_name = talloc_asprintf(
935 net_pipe, "%s$", pwset.in.computer_name);
936 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
937 pwset.in.credential = &credential;
938 pwset.in.new_password = &new_password;
939 pwset.out.return_authenticator = &return_authenticator;
941 E_md4hash(password, new_password.hash);
943 creds_state = cli_credentials_get_netlogon_creds(
944 s->wks_creds1);
945 netlogon_creds_des_encrypt(creds_state, &new_password);
946 netlogon_creds_client_authenticator(creds_state, &credential);
948 torture_assert_ntstatus_ok(torture, dcerpc_netr_ServerPasswordSet_r(net_pipe->binding_handle, torture, &pwset),
949 "ServerPasswordSet failed");
950 torture_assert_ntstatus_ok(torture, pwset.out.result,
951 "ServerPasswordSet failed");
953 if (!netlogon_creds_client_check(creds_state,
954 &pwset.out.return_authenticator->cred)) {
955 torture_comment(torture, "Credential chaining failed\n");
958 cli_credentials_set_password(s->wks_creds1, password,
959 CRED_SPECIFIED);
961 talloc_free(net_pipe);
963 /* Just as a test, connect with the new creds */
965 talloc_free(s->wks_creds1->netlogon_creds);
966 s->wks_creds1->netlogon_creds = NULL;
968 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
969 &ndr_table_netlogon,
970 s->wks_creds1,
971 torture->ev, torture->lp_ctx);
973 torture_assert_ntstatus_ok(torture, status,
974 "dcerpc_pipe_connect_b failed");
976 talloc_free(net_pipe);
979 torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
980 s->nprocs, s->timelimit);
981 for (i=0; i < s->nprocs; i++) {
982 ret = torture_schannel_bench_start(&s->conns[i]);
983 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
986 start = timeval_current();
987 end = timeval_add(&start, s->timelimit, 0);
989 while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
990 int ev_ret = tevent_loop_once(torture->ev);
991 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
993 torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
994 s->stopped = true;
995 talloc_free(s->conns);
997 for (i=0; i < s->nprocs; i++) {
998 s->total += s->conns[i].total;
1001 torture_comment(torture,
1002 "Total ops[%llu] (%u ops/s)\n",
1003 (unsigned long long)s->total,
1004 (unsigned)s->total/s->timelimit);
1006 torture_leave_domain(torture, s->join_ctx1);
1007 torture_leave_domain(torture, s->join_ctx2);
1008 return true;