s4:torture/rpc: use generate_random_password()
[Samba/nascimento.git] / source4 / torture / rpc / schannel.c
blob2f9d60ad305f074d9271d4fca47853bdb4b067b1
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 "../libcli/auth/schannel.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 netlogon_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 /* FIXME: gd */
225 if (!torture_setting_bool(tctx, "samba3", false)) {
226 return false;
229 if (!authority_name_p || !authority_name_p->string) {
230 return false;
233 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
234 printf("GetUserName returned wrong user: %s, expected %s\n",
235 authority_name_p->string, "NT AUTHORITY");
236 /* FIXME: gd */
237 if (!torture_setting_bool(tctx, "samba3", false)) {
238 return false;
242 if (!test_many_LookupSids(p, tctx, NULL)) {
243 printf("LsaLookupSids3 failed!\n");
244 return false;
247 return ret;
252 test a schannel connection with the given flags
254 static bool test_schannel(struct torture_context *tctx,
255 uint16_t acct_flags, uint32_t dcerpc_flags,
256 int i)
258 struct test_join *join_ctx;
259 NTSTATUS status;
260 const char *binding = torture_setting_string(tctx, "binding", NULL);
261 struct dcerpc_binding *b;
262 struct dcerpc_pipe *p = NULL;
263 struct dcerpc_pipe *p_netlogon = NULL;
264 struct dcerpc_pipe *p_netlogon2 = NULL;
265 struct dcerpc_pipe *p_netlogon3 = NULL;
266 struct dcerpc_pipe *p_samr2 = NULL;
267 struct dcerpc_pipe *p_lsa = NULL;
268 struct netlogon_creds_CredentialState *creds;
269 struct cli_credentials *credentials;
271 join_ctx = torture_join_domain(tctx,
272 talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
273 acct_flags, &credentials);
274 torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
276 status = dcerpc_parse_binding(tctx, binding, &b);
277 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
279 b->flags &= ~DCERPC_AUTH_OPTIONS;
280 b->flags |= dcerpc_flags;
282 status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
283 credentials, tctx->ev, tctx->lp_ctx);
284 torture_assert_ntstatus_ok(tctx, status,
285 "Failed to connect with schannel");
287 torture_assert(tctx, test_samr_ops(tctx, p),
288 "Failed to process schannel secured SAMR ops");
290 /* Also test that when we connect to the netlogon pipe, that
291 * the credentials we setup on the first pipe are valid for
292 * the second */
294 /* Swap the binding details from SAMR to NETLOGON */
295 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
296 torture_assert_ntstatus_ok(tctx, status, "epm map");
298 status = dcerpc_secondary_connection(p, &p_netlogon,
300 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
302 status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon,
303 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
304 DCERPC_AUTH_TYPE_SCHANNEL,
305 dcerpc_auth_level(p->conn),
306 NULL);
308 torture_assert_ntstatus_ok(tctx, status, "bind auth");
310 status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
311 torture_assert_ntstatus_ok(tctx, status, "schannel creds");
313 /* do a couple of logins */
314 torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
315 "Failed to process schannel secured NETLOGON ops");
317 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
318 "Failed to process schannel secured NETLOGON EX ops");
320 /* Swap the binding details from SAMR to LSARPC */
321 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
322 torture_assert_ntstatus_ok(tctx, status, "epm map");
324 status = dcerpc_secondary_connection(p, &p_lsa,
327 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
329 status = dcerpc_bind_auth(p_lsa, &ndr_table_lsarpc,
330 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
331 DCERPC_AUTH_TYPE_SCHANNEL,
332 dcerpc_auth_level(p->conn),
333 NULL);
335 torture_assert_ntstatus_ok(tctx, status, "bind auth");
337 torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
338 "Failed to process schannel secured LSA ops");
340 /* Drop the socket, we want to start from scratch */
341 talloc_free(p);
342 p = NULL;
344 /* Now see what we are still allowed to do */
346 status = dcerpc_parse_binding(tctx, binding, &b);
347 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
349 b->flags &= ~DCERPC_AUTH_OPTIONS;
350 b->flags |= dcerpc_flags;
352 status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
353 credentials, tctx->ev, tctx->lp_ctx);
354 torture_assert_ntstatus_ok(tctx, status,
355 "Failed to connect with schannel");
357 /* do a some SAMR operations. We have *not* done a new serverauthenticate */
358 torture_assert (tctx, test_samr_ops(tctx, p_samr2),
359 "Failed to process schannel secured SAMR ops (on fresh connection)");
361 /* Swap the binding details from SAMR to NETLOGON */
362 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
363 torture_assert_ntstatus_ok(tctx, status, "epm");
365 status = dcerpc_secondary_connection(p_samr2, &p_netlogon2,
367 torture_assert_ntstatus_ok(tctx, status, "seconday connection");
369 /* and now setup an SCHANNEL bind on netlogon */
370 status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
371 credentials, lp_gensec_settings(tctx, tctx->lp_ctx),
372 DCERPC_AUTH_TYPE_SCHANNEL,
373 dcerpc_auth_level(p_samr2->conn),
374 NULL);
376 torture_assert_ntstatus_ok(tctx, status, "auth failed");
378 /* Try the schannel-only SamLogonEx operation */
379 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
380 "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
383 /* And the more traditional style, proving that the
384 * credentials chaining state is fully present */
385 torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
386 "Failed to process schannel secured NETLOGON ops (on fresh connection)");
388 /* Drop the socket, we want to start from scratch (again) */
389 talloc_free(p_samr2);
391 /* We don't want schannel for this test */
392 b->flags &= ~DCERPC_AUTH_OPTIONS;
394 status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
395 credentials, tctx->ev, tctx->lp_ctx);
396 torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
398 torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
399 "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
401 /* Required because the previous call will mark the current context as having failed */
402 tctx->last_result = TORTURE_OK;
403 tctx->last_reason = NULL;
405 torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
406 "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
408 torture_leave_domain(tctx, join_ctx);
409 return true;
415 a schannel test suite
417 bool torture_rpc_schannel(struct torture_context *torture)
419 bool ret = true;
420 struct {
421 uint16_t acct_flags;
422 uint32_t dcerpc_flags;
423 } tests[] = {
424 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN},
425 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL},
426 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
427 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
428 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN },
429 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL },
430 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
431 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
433 int i;
435 for (i=0;i<ARRAY_SIZE(tests);i++) {
436 if (!test_schannel(torture,
437 tests[i].acct_flags, tests[i].dcerpc_flags,
438 i)) {
439 torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
440 tests[i].acct_flags, tests[i].dcerpc_flags);
441 ret = false;
445 return ret;
449 test two schannel connections
451 bool torture_rpc_schannel2(struct torture_context *torture)
453 struct test_join *join_ctx;
454 NTSTATUS status;
455 const char *binding = torture_setting_string(torture, "binding", NULL);
456 struct dcerpc_binding *b;
457 struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
458 struct cli_credentials *credentials1, *credentials2;
459 uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
461 join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
462 ACB_WSTRUST, &credentials1);
463 torture_assert(torture, join_ctx != NULL,
464 "Failed to join domain with acct_flags=ACB_WSTRUST");
466 credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
467 credentials1->netlogon_creds = NULL;
468 credentials2->netlogon_creds = NULL;
470 status = dcerpc_parse_binding(torture, binding, &b);
471 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
473 b->flags &= ~DCERPC_AUTH_OPTIONS;
474 b->flags |= dcerpc_flags;
476 printf("Opening first connection\n");
477 status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
478 credentials1, torture->ev, torture->lp_ctx);
479 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
481 torture_comment(torture, "Opening second connection\n");
482 status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
483 credentials2, torture->ev, torture->lp_ctx);
484 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
486 credentials1->netlogon_creds = NULL;
487 credentials2->netlogon_creds = NULL;
489 torture_comment(torture, "Testing logon on pipe1\n");
490 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
491 return false;
493 torture_comment(torture, "Testing logon on pipe2\n");
494 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
495 return false;
497 torture_comment(torture, "Again on pipe1\n");
498 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
499 return false;
501 torture_comment(torture, "Again on pipe2\n");
502 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
503 return false;
505 torture_leave_domain(torture, join_ctx);
506 return true;
509 struct torture_schannel_bench;
511 struct torture_schannel_bench_conn {
512 struct torture_schannel_bench *s;
513 int index;
514 struct cli_credentials *wks_creds;
515 struct dcerpc_pipe *pipe;
516 struct netr_LogonSamLogonEx r;
517 struct netr_NetworkInfo ninfo;
518 TALLOC_CTX *tmp;
519 uint64_t total;
520 uint32_t count;
523 struct torture_schannel_bench {
524 struct torture_context *tctx;
525 bool progress;
526 int timelimit;
527 int nprocs;
528 int nconns;
529 struct torture_schannel_bench_conn *conns;
530 struct test_join *join_ctx1;
531 struct cli_credentials *wks_creds1;
532 struct test_join *join_ctx2;
533 struct cli_credentials *wks_creds2;
534 struct cli_credentials *user1_creds;
535 struct cli_credentials *user2_creds;
536 struct dcerpc_binding *b;
537 NTSTATUS error;
538 uint64_t total;
539 uint32_t count;
540 bool stopped;
543 static void torture_schannel_bench_connected(struct composite_context *c)
545 struct torture_schannel_bench_conn *conn =
546 (struct torture_schannel_bench_conn *)c->async.private_data;
547 struct torture_schannel_bench *s = talloc_get_type(conn->s,
548 struct torture_schannel_bench);
550 s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
551 torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
552 if (NT_STATUS_IS_OK(s->error)) {
553 s->nconns++;
557 static void torture_schannel_bench_recv(struct rpc_request *req);
559 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
561 struct torture_schannel_bench *s = conn->s;
562 NTSTATUS status;
563 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
564 int flags = CLI_CRED_NTLM_AUTH;
565 struct rpc_request *req;
566 struct cli_credentials *user_creds;
568 if (conn->total % 2) {
569 user_creds = s->user1_creds;
570 } else {
571 user_creds = s->user2_creds;
574 if (lp_client_lanman_auth(s->tctx->lp_ctx)) {
575 flags |= CLI_CRED_LANMAN_AUTH;
578 if (lp_client_ntlmv2_auth(s->tctx->lp_ctx)) {
579 flags |= CLI_CRED_NTLMv2_AUTH;
582 talloc_free(conn->tmp);
583 conn->tmp = talloc_new(s);
584 ZERO_STRUCT(conn->ninfo);
585 ZERO_STRUCT(conn->r);
587 cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
588 &conn->ninfo.identity_info.account_name.string,
589 &conn->ninfo.identity_info.domain_name.string);
591 generate_random_buffer(conn->ninfo.challenge,
592 sizeof(conn->ninfo.challenge));
593 chal = data_blob_const(conn->ninfo.challenge,
594 sizeof(conn->ninfo.challenge));
596 names_blob = NTLMv2_generate_names_blob(conn->tmp,
597 cli_credentials_get_workstation(conn->wks_creds),
598 cli_credentials_get_domain(conn->wks_creds));
600 status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
601 &flags,
602 chal,
603 names_blob,
604 &lm_resp, &nt_resp,
605 NULL, NULL);
606 torture_assert_ntstatus_ok(s->tctx, status,
607 "cli_credentials_get_ntlm_response failed");
609 conn->ninfo.lm.data = lm_resp.data;
610 conn->ninfo.lm.length = lm_resp.length;
612 conn->ninfo.nt.data = nt_resp.data;
613 conn->ninfo.nt.length = nt_resp.length;
615 conn->ninfo.identity_info.parameter_control = 0;
616 conn->ninfo.identity_info.logon_id_low = 0;
617 conn->ninfo.identity_info.logon_id_high = 0;
618 conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
620 conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
621 conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
622 conn->r.in.logon_level = 2;
623 conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
624 conn->r.in.logon->network = &conn->ninfo;
625 conn->r.in.flags = talloc(conn->tmp, uint32_t);
626 conn->r.in.validation_level = 2;
627 conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
628 conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
629 conn->r.out.flags = conn->r.in.flags;
631 req = dcerpc_netr_LogonSamLogonEx_send(conn->pipe, conn->tmp, &conn->r);
632 torture_assert(s->tctx, req, "Failed to setup LogonSamLogonEx request");
634 req->async.callback = torture_schannel_bench_recv;
635 req->async.private_data = conn;
637 return true;
640 static void torture_schannel_bench_recv(struct rpc_request *req)
642 bool ret;
643 struct torture_schannel_bench_conn *conn =
644 (struct torture_schannel_bench_conn *)req->async.private_data;
645 struct torture_schannel_bench *s = talloc_get_type(conn->s,
646 struct torture_schannel_bench);
648 s->error = dcerpc_ndr_request_recv(req);
649 if (!NT_STATUS_IS_OK(s->error)) {
650 return;
653 conn->total++;
654 conn->count++;
656 if (s->stopped) {
657 return;
660 ret = torture_schannel_bench_start(conn);
661 if (!ret) {
662 s->error = NT_STATUS_INTERNAL_ERROR;
667 test multiple schannel connection in parallel
669 bool torture_rpc_schannel_bench1(struct torture_context *torture)
671 bool ret = true;
672 NTSTATUS status;
673 const char *binding = torture_setting_string(torture, "binding", NULL);
674 struct torture_schannel_bench *s;
675 struct timeval start;
676 struct timeval end;
677 int i;
678 const char *tmp;
680 s = talloc_zero(torture, struct torture_schannel_bench);
681 s->tctx = torture;
682 s->progress = torture_setting_bool(torture, "progress", true);
683 s->timelimit = torture_setting_int(torture, "timelimit", 10);
684 s->nprocs = torture_setting_int(torture, "nprocs", 4);
685 s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
687 s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
688 cmdline_credentials,
689 sizeof(*s->user1_creds));
690 tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
691 if (tmp) {
692 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
694 s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
695 cmdline_credentials,
696 sizeof(*s->user1_creds));
697 tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
698 if (tmp) {
699 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
702 s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
703 ACB_WSTRUST, &s->wks_creds1);
704 torture_assert(torture, s->join_ctx1 != NULL,
705 "Failed to join domain with acct_flags=ACB_WSTRUST");
706 s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
707 ACB_WSTRUST, &s->wks_creds2);
708 torture_assert(torture, s->join_ctx2 != NULL,
709 "Failed to join domain with acct_flags=ACB_WSTRUST");
711 cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
712 cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
714 for (i=0; i < s->nprocs; i++) {
715 s->conns[i].s = s;
716 s->conns[i].index = i;
717 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
718 s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
719 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
720 memcpy(s->conns[i].wks_creds, s->wks_creds2,
721 talloc_get_size(s->conns[i].wks_creds));
723 s->conns[i].wks_creds->netlogon_creds = NULL;
726 status = dcerpc_parse_binding(s, binding, &s->b);
727 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
728 s->b->flags &= ~DCERPC_AUTH_OPTIONS;
729 s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
731 torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
732 for (i=0; i < s->nprocs; i++) {
733 #if 1
734 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
735 &ndr_table_netlogon,
736 s->conns[i].wks_creds,
737 torture->ev, torture->lp_ctx);
738 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
739 #else
741 * This path doesn't work against windows,
742 * because of windows drops the connections
743 * which haven't reached a session setup yet
745 * The same as the reset on zero vc stuff.
747 struct composite_context *c;
748 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
749 &ndr_table_netlogon,
750 s->conns[i].wks_creds,
751 torture->ev,
752 torture->lp_ctx);
753 torture_assert(torture, c != NULL, "Failed to setup connect");
754 c->async.fn = torture_schannel_bench_connected;
755 c->async.private_data = &s->conns[i];
758 while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
759 int ev_ret = event_loop_once(torture->ev);
760 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
761 #endif
763 torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
766 * Change the workstation password after establishing the netlogon
767 * schannel connections to prove that existing connections are not
768 * affected by a wks pwchange.
772 struct netr_ServerPasswordSet pwset;
773 char *password = generate_random_password(s->join_ctx1, 8, 255);
774 struct netlogon_creds_CredentialState *creds_state;
775 struct dcerpc_pipe *net_pipe;
776 struct netr_Authenticator credential, return_authenticator;
777 struct samr_Password new_password;
779 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
780 &ndr_table_netlogon,
781 s->wks_creds1,
782 torture->ev, torture->lp_ctx);
784 torture_assert_ntstatus_ok(torture, status,
785 "dcerpc_pipe_connect_b failed");
787 pwset.in.server_name = talloc_asprintf(
788 net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
789 pwset.in.computer_name =
790 cli_credentials_get_workstation(s->wks_creds1);
791 pwset.in.account_name = talloc_asprintf(
792 net_pipe, "%s$", pwset.in.computer_name);
793 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
794 pwset.in.credential = &credential;
795 pwset.in.new_password = &new_password;
796 pwset.out.return_authenticator = &return_authenticator;
798 E_md4hash(password, new_password.hash);
800 creds_state = cli_credentials_get_netlogon_creds(
801 s->wks_creds1);
802 netlogon_creds_des_encrypt(creds_state, &new_password);
803 netlogon_creds_client_authenticator(creds_state, &credential);
805 status = dcerpc_netr_ServerPasswordSet(net_pipe, torture, &pwset);
806 torture_assert_ntstatus_ok(torture, status,
807 "ServerPasswordSet failed");
809 if (!netlogon_creds_client_check(creds_state,
810 &pwset.out.return_authenticator->cred)) {
811 printf("Credential chaining failed\n");
814 cli_credentials_set_password(s->wks_creds1, password,
815 CRED_SPECIFIED);
817 talloc_free(net_pipe);
819 /* Just as a test, connect with the new creds */
821 talloc_free(s->wks_creds1->netlogon_creds);
822 s->wks_creds1->netlogon_creds = NULL;
824 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
825 &ndr_table_netlogon,
826 s->wks_creds1,
827 torture->ev, torture->lp_ctx);
829 torture_assert_ntstatus_ok(torture, status,
830 "dcerpc_pipe_connect_b failed");
832 talloc_free(net_pipe);
835 torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
836 s->nprocs, s->timelimit);
837 for (i=0; i < s->nprocs; i++) {
838 ret = torture_schannel_bench_start(&s->conns[i]);
839 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
842 start = timeval_current();
843 end = timeval_add(&start, s->timelimit, 0);
845 while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
846 int ev_ret = event_loop_once(torture->ev);
847 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
849 torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
850 s->stopped = true;
851 talloc_free(s->conns);
853 for (i=0; i < s->nprocs; i++) {
854 s->total += s->conns[i].total;
857 torture_comment(torture,
858 "Total ops[%llu] (%u ops/s)\n",
859 (unsigned long long)s->total,
860 (unsigned)s->total/s->timelimit);
862 torture_leave_domain(torture, s->join_ctx1);
863 torture_leave_domain(torture, s->join_ctx2);
864 return true;