s3/docs: Raise version number up to 3.5.
[Samba/gebeck_regimport.git] / source4 / torture / rpc / schannel.c
blobbc3cbeac3b6cd8e188db6dcb45bec4f8a603f5b2
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/rpc.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "auth/gensec/schannel_proto.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "libcli/security/security.h"
32 #include "system/filesys.h"
33 #include "param/param.h"
34 #include "librpc/rpc/dcerpc_proto.h"
35 #include "auth/gensec/gensec.h"
36 #include "libcli/composite/composite.h"
37 #include "lib/events/events.h"
39 #define TEST_MACHINE_NAME "schannel"
42 try a netlogon SamLogon
44 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx,
45 struct cli_credentials *credentials,
46 struct creds_CredentialState *creds)
48 NTSTATUS status;
49 struct netr_LogonSamLogonEx r;
50 struct netr_NetworkInfo ninfo;
51 union netr_LogonLevel logon;
52 union netr_Validation validation;
53 uint8_t authoritative = 0;
54 uint32_t _flags = 0;
55 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
56 int i;
57 int flags = CLI_CRED_NTLM_AUTH;
58 if (lp_client_lanman_auth(tctx->lp_ctx)) {
59 flags |= CLI_CRED_LANMAN_AUTH;
62 if (lp_client_ntlmv2_auth(tctx->lp_ctx)) {
63 flags |= CLI_CRED_NTLMv2_AUTH;
66 cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
67 &ninfo.identity_info.account_name.string,
68 &ninfo.identity_info.domain_name.string);
70 generate_random_buffer(ninfo.challenge,
71 sizeof(ninfo.challenge));
72 chal = data_blob_const(ninfo.challenge,
73 sizeof(ninfo.challenge));
75 names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
76 cli_credentials_get_domain(credentials));
78 status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
79 &flags,
80 chal,
81 names_blob,
82 &lm_resp, &nt_resp,
83 NULL, NULL);
84 torture_assert_ntstatus_ok(tctx, status,
85 "cli_credentials_get_ntlm_response failed");
87 ninfo.lm.data = lm_resp.data;
88 ninfo.lm.length = lm_resp.length;
90 ninfo.nt.data = nt_resp.data;
91 ninfo.nt.length = nt_resp.length;
93 ninfo.identity_info.parameter_control = 0;
94 ninfo.identity_info.logon_id_low = 0;
95 ninfo.identity_info.logon_id_high = 0;
96 ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
98 logon.network = &ninfo;
100 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
101 r.in.computer_name = cli_credentials_get_workstation(credentials);
102 r.in.logon_level = 2;
103 r.in.logon= &logon;
104 r.in.flags = &_flags;
105 r.out.validation = &validation;
106 r.out.authoritative = &authoritative;
107 r.out.flags = &_flags;
109 torture_comment(tctx,
110 "Testing LogonSamLogonEx with name %s\n",
111 ninfo.identity_info.account_name.string);
113 for (i=2;i<3;i++) {
114 r.in.validation_level = i;
116 status = dcerpc_netr_LogonSamLogonEx(p, tctx, &r);
117 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
120 return true;
124 do some samr ops using the schannel connection
126 static bool test_samr_ops(struct torture_context *tctx,
127 struct dcerpc_pipe *p)
129 NTSTATUS status;
130 struct samr_GetDomPwInfo r;
131 struct samr_PwInfo info;
132 struct samr_Connect connect_r;
133 struct samr_OpenDomain opendom;
134 int i;
135 struct lsa_String name;
136 struct policy_handle handle;
137 struct policy_handle domain_handle;
139 name.string = lp_workgroup(tctx->lp_ctx);
140 r.in.domain_name = &name;
141 r.out.info = &info;
143 connect_r.in.system_name = 0;
144 connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
145 connect_r.out.connect_handle = &handle;
147 printf("Testing Connect and OpenDomain on BUILTIN\n");
149 status = dcerpc_samr_Connect(p, tctx, &connect_r);
150 if (!NT_STATUS_IS_OK(status)) {
151 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
152 printf("Connect failed (expected, schannel mapped to anonymous): %s\n",
153 nt_errstr(status));
154 } else {
155 printf("Connect failed - %s\n", nt_errstr(status));
156 return false;
158 } else {
159 opendom.in.connect_handle = &handle;
160 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
161 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
162 opendom.out.domain_handle = &domain_handle;
164 status = dcerpc_samr_OpenDomain(p, tctx, &opendom);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("OpenDomain failed - %s\n", nt_errstr(status));
167 return false;
171 printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
173 /* do several ops to test credential chaining */
174 for (i=0;i<5;i++) {
175 status = dcerpc_samr_GetDomPwInfo(p, tctx, &r);
176 if (!NT_STATUS_IS_OK(status)) {
177 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
178 printf("GetDomPwInfo op %d failed - %s\n", i, nt_errstr(status));
179 return false;
184 return true;
189 do some lsa ops using the schannel connection
191 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
193 struct lsa_GetUserName r;
194 NTSTATUS status;
195 bool ret = true;
196 struct lsa_String *account_name_p = NULL;
197 struct lsa_String *authority_name_p = NULL;
199 printf("\nTesting GetUserName\n");
201 r.in.system_name = "\\";
202 r.in.account_name = &account_name_p;
203 r.in.authority_name = &authority_name_p;
204 r.out.account_name = &account_name_p;
206 /* do several ops to test credential chaining and various operations */
207 status = dcerpc_lsa_GetUserName(p, tctx, &r);
209 authority_name_p = *r.out.authority_name;
211 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
212 printf("not considering %s to be an error\n", nt_errstr(status));
213 } else if (!NT_STATUS_IS_OK(status)) {
214 printf("GetUserName failed - %s\n", nt_errstr(status));
215 return false;
216 } else {
217 if (!r.out.account_name) {
218 return false;
221 if (strcmp(account_name_p->string, "ANONYMOUS LOGON") != 0) {
222 printf("GetUserName returned wrong user: %s, expected %s\n",
223 account_name_p->string, "ANONYMOUS LOGON");
224 return false;
226 if (!authority_name_p || !authority_name_p->string) {
227 return false;
230 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
231 printf("GetUserName returned wrong user: %s, expected %s\n",
232 authority_name_p->string, "NT AUTHORITY");
233 return false;
236 if (!test_many_LookupSids(p, tctx, NULL)) {
237 printf("LsaLookupSids3 failed!\n");
238 return false;
241 return ret;
246 test a schannel connection with the given flags
248 static bool test_schannel(struct torture_context *tctx,
249 uint16_t acct_flags, uint32_t dcerpc_flags,
250 int i)
252 struct test_join *join_ctx;
253 NTSTATUS status;
254 const char *binding = torture_setting_string(tctx, "binding", NULL);
255 struct dcerpc_binding *b;
256 struct dcerpc_pipe *p = NULL;
257 struct dcerpc_pipe *p_netlogon = NULL;
258 struct dcerpc_pipe *p_netlogon2 = NULL;
259 struct dcerpc_pipe *p_netlogon3 = NULL;
260 struct dcerpc_pipe *p_samr2 = NULL;
261 struct dcerpc_pipe *p_lsa = NULL;
262 struct creds_CredentialState *creds;
263 struct cli_credentials *credentials;
265 join_ctx = torture_join_domain(tctx,
266 talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
267 acct_flags, &credentials);
268 torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
270 status = dcerpc_parse_binding(tctx, binding, &b);
271 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
273 b->flags &= ~DCERPC_AUTH_OPTIONS;
274 b->flags |= dcerpc_flags;
276 status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
277 credentials, tctx->ev, tctx->lp_ctx);
278 torture_assert_ntstatus_ok(tctx, status,
279 "Failed to connect with schannel");
281 torture_assert(tctx, test_samr_ops(tctx, p),
282 "Failed to process schannel secured SAMR ops");
284 /* Also test that when we connect to the netlogon pipe, that
285 * the credentials we setup on the first pipe are valid for
286 * the second */
288 /* Swap the binding details from SAMR to NETLOGON */
289 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
290 torture_assert_ntstatus_ok(tctx, status, "epm map");
292 status = dcerpc_secondary_connection(p, &p_netlogon,
294 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
296 status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon,
297 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
298 DCERPC_AUTH_TYPE_SCHANNEL,
299 dcerpc_auth_level(p->conn),
300 NULL);
302 torture_assert_ntstatus_ok(tctx, status, "bind auth");
304 status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
305 torture_assert_ntstatus_ok(tctx, status, "schannel creds");
307 /* do a couple of logins */
308 torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
309 "Failed to process schannel secured NETLOGON ops");
311 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
312 "Failed to process schannel secured NETLOGON EX ops");
314 /* Swap the binding details from SAMR to LSARPC */
315 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
316 torture_assert_ntstatus_ok(tctx, status, "epm map");
318 status = dcerpc_secondary_connection(p, &p_lsa,
321 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
323 status = dcerpc_bind_auth(p_lsa, &ndr_table_lsarpc,
324 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
325 DCERPC_AUTH_TYPE_SCHANNEL,
326 dcerpc_auth_level(p->conn),
327 NULL);
329 torture_assert_ntstatus_ok(tctx, status, "bind auth");
331 torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
332 "Failed to process schannel secured LSA ops");
334 /* Drop the socket, we want to start from scratch */
335 talloc_free(p);
336 p = NULL;
338 /* Now see what we are still allowed to do */
340 status = dcerpc_parse_binding(tctx, binding, &b);
341 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
343 b->flags &= ~DCERPC_AUTH_OPTIONS;
344 b->flags |= dcerpc_flags;
346 status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
347 credentials, tctx->ev, tctx->lp_ctx);
348 torture_assert_ntstatus_ok(tctx, status,
349 "Failed to connect with schannel");
351 /* do a some SAMR operations. We have *not* done a new serverauthenticate */
352 torture_assert (tctx, test_samr_ops(tctx, p_samr2),
353 "Failed to process schannel secured SAMR ops (on fresh connection)");
355 /* Swap the binding details from SAMR to NETLOGON */
356 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
357 torture_assert_ntstatus_ok(tctx, status, "epm");
359 status = dcerpc_secondary_connection(p_samr2, &p_netlogon2,
361 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
363 /* and now setup an SCHANNEL bind on netlogon */
364 status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
365 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
366 DCERPC_AUTH_TYPE_SCHANNEL,
367 dcerpc_auth_level(p_samr2->conn),
368 NULL);
370 torture_assert_ntstatus_ok(tctx, status, "auth failed");
372 /* Try the schannel-only SamLogonEx operation */
373 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
374 "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
377 /* And the more traditional style, proving that the
378 * credentials chaining state is fully present */
379 torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
380 "Failed to process schannel secured NETLOGON ops (on fresh connection)");
382 /* Drop the socket, we want to start from scratch (again) */
383 talloc_free(p_samr2);
385 /* We don't want schannel for this test */
386 b->flags &= ~DCERPC_AUTH_OPTIONS;
388 status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
389 credentials, tctx->ev, tctx->lp_ctx);
390 torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
392 torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
393 "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
395 /* Required because the previous call will mark the current context as having failed */
396 tctx->last_result = TORTURE_OK;
397 tctx->last_reason = NULL;
399 torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
400 "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
402 torture_leave_domain(tctx, join_ctx);
403 return true;
409 a schannel test suite
411 bool torture_rpc_schannel(struct torture_context *torture)
413 bool ret = true;
414 struct {
415 uint16_t acct_flags;
416 uint32_t dcerpc_flags;
417 } tests[] = {
418 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN},
419 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL},
420 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
421 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
422 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN },
423 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL },
424 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
425 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
427 int i;
429 for (i=0;i<ARRAY_SIZE(tests);i++) {
430 if (!test_schannel(torture,
431 tests[i].acct_flags, tests[i].dcerpc_flags,
432 i)) {
433 torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
434 tests[i].acct_flags, tests[i].dcerpc_flags);
435 ret = false;
439 return ret;
443 test two schannel connections
445 bool torture_rpc_schannel2(struct torture_context *torture)
447 struct test_join *join_ctx;
448 NTSTATUS status;
449 const char *binding = torture_setting_string(torture, "binding", NULL);
450 struct dcerpc_binding *b;
451 struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
452 struct cli_credentials *credentials1, *credentials2;
453 uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
455 join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
456 ACB_WSTRUST, &credentials1);
457 torture_assert(torture, join_ctx != NULL,
458 "Failed to join domain with acct_flags=ACB_WSTRUST");
460 credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
461 credentials1->netlogon_creds = NULL;
462 credentials2->netlogon_creds = NULL;
464 status = dcerpc_parse_binding(torture, binding, &b);
465 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
467 b->flags &= ~DCERPC_AUTH_OPTIONS;
468 b->flags |= dcerpc_flags;
470 printf("Opening first connection\n");
471 status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
472 credentials1, torture->ev, torture->lp_ctx);
473 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
475 torture_comment(torture, "Opening second connection\n");
476 status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
477 credentials2, torture->ev, torture->lp_ctx);
478 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
480 credentials1->netlogon_creds = NULL;
481 credentials2->netlogon_creds = NULL;
483 torture_comment(torture, "Testing logon on pipe1\n");
484 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
485 return false;
487 torture_comment(torture, "Testing logon on pipe2\n");
488 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
489 return false;
491 torture_comment(torture, "Again on pipe1\n");
492 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
493 return false;
495 torture_comment(torture, "Again on pipe2\n");
496 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
497 return false;
499 torture_leave_domain(torture, join_ctx);
500 return true;
503 struct torture_schannel_bench;
505 struct torture_schannel_bench_conn {
506 struct torture_schannel_bench *s;
507 int index;
508 struct cli_credentials *wks_creds;
509 struct dcerpc_pipe *pipe;
510 struct netr_LogonSamLogonEx r;
511 struct netr_NetworkInfo ninfo;
512 TALLOC_CTX *tmp;
513 uint64_t total;
514 uint32_t count;
517 struct torture_schannel_bench {
518 struct torture_context *tctx;
519 bool progress;
520 int timelimit;
521 int nprocs;
522 int nconns;
523 struct torture_schannel_bench_conn *conns;
524 struct test_join *join_ctx1;
525 struct cli_credentials *wks_creds1;
526 struct test_join *join_ctx2;
527 struct cli_credentials *wks_creds2;
528 struct cli_credentials *user1_creds;
529 struct cli_credentials *user2_creds;
530 struct dcerpc_binding *b;
531 NTSTATUS error;
532 uint64_t total;
533 uint32_t count;
534 bool stopped;
537 static void torture_schannel_bench_connected(struct composite_context *c)
539 struct torture_schannel_bench_conn *conn =
540 (struct torture_schannel_bench_conn *)c->async.private_data;
541 struct torture_schannel_bench *s = talloc_get_type(conn->s,
542 struct torture_schannel_bench);
544 s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
545 torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
546 if (NT_STATUS_IS_OK(s->error)) {
547 s->nconns++;
551 static void torture_schannel_bench_recv(struct rpc_request *req);
553 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
555 struct torture_schannel_bench *s = conn->s;
556 NTSTATUS status;
557 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
558 int flags = CLI_CRED_NTLM_AUTH;
559 struct rpc_request *req;
560 struct cli_credentials *user_creds;
562 if (conn->total % 2) {
563 user_creds = s->user1_creds;
564 } else {
565 user_creds = s->user2_creds;
568 if (lp_client_lanman_auth(s->tctx->lp_ctx)) {
569 flags |= CLI_CRED_LANMAN_AUTH;
572 if (lp_client_ntlmv2_auth(s->tctx->lp_ctx)) {
573 flags |= CLI_CRED_NTLMv2_AUTH;
576 talloc_free(conn->tmp);
577 conn->tmp = talloc_new(s);
578 ZERO_STRUCT(conn->ninfo);
579 ZERO_STRUCT(conn->r);
581 cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
582 &conn->ninfo.identity_info.account_name.string,
583 &conn->ninfo.identity_info.domain_name.string);
585 generate_random_buffer(conn->ninfo.challenge,
586 sizeof(conn->ninfo.challenge));
587 chal = data_blob_const(conn->ninfo.challenge,
588 sizeof(conn->ninfo.challenge));
590 names_blob = NTLMv2_generate_names_blob(conn->tmp,
591 cli_credentials_get_workstation(conn->wks_creds),
592 cli_credentials_get_domain(conn->wks_creds));
594 status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
595 &flags,
596 chal,
597 names_blob,
598 &lm_resp, &nt_resp,
599 NULL, NULL);
600 torture_assert_ntstatus_ok(s->tctx, status,
601 "cli_credentials_get_ntlm_response failed");
603 conn->ninfo.lm.data = lm_resp.data;
604 conn->ninfo.lm.length = lm_resp.length;
606 conn->ninfo.nt.data = nt_resp.data;
607 conn->ninfo.nt.length = nt_resp.length;
609 conn->ninfo.identity_info.parameter_control = 0;
610 conn->ninfo.identity_info.logon_id_low = 0;
611 conn->ninfo.identity_info.logon_id_high = 0;
612 conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
614 conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
615 conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
616 conn->r.in.logon_level = 2;
617 conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
618 conn->r.in.logon->network = &conn->ninfo;
619 conn->r.in.flags = talloc(conn->tmp, uint32_t);
620 conn->r.in.validation_level = 2;
621 conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
622 conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
623 conn->r.out.flags = conn->r.in.flags;
625 req = dcerpc_netr_LogonSamLogonEx_send(conn->pipe, conn->tmp, &conn->r);
626 torture_assert(s->tctx, req, "Failed to setup LogonSamLogonEx request");
628 req->async.callback = torture_schannel_bench_recv;
629 req->async.private_data = conn;
631 return true;
634 static void torture_schannel_bench_recv(struct rpc_request *req)
636 bool ret;
637 struct torture_schannel_bench_conn *conn =
638 (struct torture_schannel_bench_conn *)req->async.private_data;
639 struct torture_schannel_bench *s = talloc_get_type(conn->s,
640 struct torture_schannel_bench);
642 s->error = dcerpc_ndr_request_recv(req);
643 if (!NT_STATUS_IS_OK(s->error)) {
644 return;
647 conn->total++;
648 conn->count++;
650 if (s->stopped) {
651 return;
654 ret = torture_schannel_bench_start(conn);
655 if (!ret) {
656 s->error = NT_STATUS_INTERNAL_ERROR;
661 test multiple schannel connection in parallel
663 bool torture_rpc_schannel_bench1(struct torture_context *torture)
665 bool ret = true;
666 NTSTATUS status;
667 const char *binding = torture_setting_string(torture, "binding", NULL);
668 struct torture_schannel_bench *s;
669 struct timeval start;
670 struct timeval end;
671 int i;
672 const char *tmp;
674 s = talloc_zero(torture, struct torture_schannel_bench);
675 s->tctx = torture;
676 s->progress = torture_setting_bool(torture, "progress", true);
677 s->timelimit = torture_setting_int(torture, "timelimit", 10);
678 s->nprocs = torture_setting_int(torture, "nprocs", 4);
679 s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
681 s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
682 cmdline_credentials,
683 sizeof(*s->user1_creds));
684 tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
685 if (tmp) {
686 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
688 s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
689 cmdline_credentials,
690 sizeof(*s->user1_creds));
691 tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
692 if (tmp) {
693 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
696 s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
697 ACB_WSTRUST, &s->wks_creds1);
698 torture_assert(torture, s->join_ctx1 != NULL,
699 "Failed to join domain with acct_flags=ACB_WSTRUST");
700 s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
701 ACB_WSTRUST, &s->wks_creds2);
702 torture_assert(torture, s->join_ctx2 != NULL,
703 "Failed to join domain with acct_flags=ACB_WSTRUST");
705 cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
706 cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
708 for (i=0; i < s->nprocs; i++) {
709 s->conns[i].s = s;
710 s->conns[i].index = i;
711 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
712 s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
713 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
714 memcpy(s->conns[i].wks_creds, s->wks_creds2,
715 talloc_get_size(s->conns[i].wks_creds));
717 s->conns[i].wks_creds->netlogon_creds = NULL;
720 status = dcerpc_parse_binding(s, binding, &s->b);
721 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
722 s->b->flags &= ~DCERPC_AUTH_OPTIONS;
723 s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
725 torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
726 for (i=0; i < s->nprocs; i++) {
727 #if 1
728 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
729 &ndr_table_netlogon,
730 s->conns[i].wks_creds,
731 torture->ev, torture->lp_ctx);
732 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
733 #else
735 * This path doesn't work against windows,
736 * because of windows drops the connections
737 * which haven't reached a session setup yet
739 * The same as the reset on zero vc stuff.
741 struct composite_context *c;
742 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
743 &ndr_table_netlogon,
744 s->conns[i].wks_creds,
745 torture->ev,
746 torture->lp_ctx);
747 torture_assert(torture, c != NULL, "Failed to setup connect");
748 c->async.fn = torture_schannel_bench_connected;
749 c->async.private_data = &s->conns[i];
752 while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
753 int ev_ret = event_loop_once(torture->ev);
754 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
755 #endif
757 torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
760 * Change the workstation password after establishing the netlogon
761 * schannel connections to prove that existing connections are not
762 * affected by a wks pwchange.
766 struct netr_ServerPasswordSet pwset;
767 char *password = generate_random_str(s->join_ctx1, 8);
768 struct creds_CredentialState *creds_state;
769 struct dcerpc_pipe *net_pipe;
770 struct netr_Authenticator credential, return_authenticator;
771 struct samr_Password new_password;
773 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
774 &ndr_table_netlogon,
775 s->wks_creds1,
776 torture->ev, torture->lp_ctx);
778 torture_assert_ntstatus_ok(torture, status,
779 "dcerpc_pipe_connect_b failed");
781 pwset.in.server_name = talloc_asprintf(
782 net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
783 pwset.in.computer_name =
784 cli_credentials_get_workstation(s->wks_creds1);
785 pwset.in.account_name = talloc_asprintf(
786 net_pipe, "%s$", pwset.in.computer_name);
787 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
788 pwset.in.credential = &credential;
789 pwset.in.new_password = &new_password;
790 pwset.out.return_authenticator = &return_authenticator;
792 E_md4hash(password, new_password.hash);
794 creds_state = cli_credentials_get_netlogon_creds(
795 s->wks_creds1);
796 creds_des_encrypt(creds_state, &new_password);
797 creds_client_authenticator(creds_state, &credential);
799 status = dcerpc_netr_ServerPasswordSet(net_pipe, torture, &pwset);
800 torture_assert_ntstatus_ok(torture, status,
801 "ServerPasswordSet failed");
803 if (!creds_client_check(creds_state,
804 &pwset.out.return_authenticator->cred)) {
805 printf("Credential chaining failed\n");
808 cli_credentials_set_password(s->wks_creds1, password,
809 CRED_SPECIFIED);
811 talloc_free(net_pipe);
813 /* Just as a test, connect with the new creds */
815 talloc_free(s->wks_creds1->netlogon_creds);
816 s->wks_creds1->netlogon_creds = NULL;
818 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
819 &ndr_table_netlogon,
820 s->wks_creds1,
821 torture->ev, torture->lp_ctx);
823 torture_assert_ntstatus_ok(torture, status,
824 "dcerpc_pipe_connect_b failed");
826 talloc_free(net_pipe);
829 torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
830 s->nprocs, s->timelimit);
831 for (i=0; i < s->nprocs; i++) {
832 ret = torture_schannel_bench_start(&s->conns[i]);
833 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
836 start = timeval_current();
837 end = timeval_add(&start, s->timelimit, 0);
839 while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
840 int ev_ret = event_loop_once(torture->ev);
841 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
843 torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
844 s->stopped = true;
845 talloc_free(s->conns);
847 for (i=0; i < s->nprocs; i++) {
848 s->total += s->conns[i].total;
851 torture_comment(torture,
852 "Total ops[%llu] (%u ops/s)\n",
853 (unsigned long long)s->total,
854 (unsigned)s->total/s->timelimit);
856 torture_leave_domain(torture, s->join_ctx1);
857 torture_leave_domain(torture, s->join_ctx2);
858 return true;