torture: confirm printer driver file removal
[Samba/vl.git] / source4 / torture / rpc / schannel.c
blob30b8a1d1aeb838b103b51f32366d42c9a71a4b56
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 if (lpcfg_client_lanman_auth(tctx->lp_ctx)) {
62 flags |= CLI_CRED_LANMAN_AUTH;
65 if (lpcfg_client_ntlmv2_auth(tctx->lp_ctx)) {
66 flags |= CLI_CRED_NTLMv2_AUTH;
69 cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
70 &ninfo.identity_info.account_name.string,
71 &ninfo.identity_info.domain_name.string);
73 generate_random_buffer(ninfo.challenge,
74 sizeof(ninfo.challenge));
75 chal = data_blob_const(ninfo.challenge,
76 sizeof(ninfo.challenge));
78 names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
79 cli_credentials_get_domain(credentials));
81 status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
82 &flags,
83 chal,
84 names_blob,
85 &lm_resp, &nt_resp,
86 NULL, NULL);
87 torture_assert_ntstatus_ok(tctx, status,
88 "cli_credentials_get_ntlm_response failed");
90 ninfo.lm.data = lm_resp.data;
91 ninfo.lm.length = lm_resp.length;
93 ninfo.nt.data = nt_resp.data;
94 ninfo.nt.length = nt_resp.length;
96 ninfo.identity_info.parameter_control = 0;
97 ninfo.identity_info.logon_id_low = 0;
98 ninfo.identity_info.logon_id_high = 0;
99 ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
101 logon.network = &ninfo;
103 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
104 r.in.computer_name = cli_credentials_get_workstation(credentials);
105 r.in.logon_level = 2;
106 r.in.logon= &logon;
107 r.in.flags = &_flags;
108 r.out.validation = &validation;
109 r.out.authoritative = &authoritative;
110 r.out.flags = &_flags;
112 torture_comment(tctx,
113 "Testing LogonSamLogonEx with name %s\n",
114 ninfo.identity_info.account_name.string);
116 for (i=2;i<3;i++) {
117 r.in.validation_level = i;
119 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
120 "LogonSamLogon failed");
121 torture_assert_ntstatus_ok(tctx, r.out.result, "LogonSamLogon failed");
124 return true;
128 do some samr ops using the schannel connection
130 static bool test_samr_ops(struct torture_context *tctx,
131 struct dcerpc_binding_handle *b)
133 struct samr_GetDomPwInfo r;
134 struct samr_PwInfo info;
135 struct samr_Connect connect_r;
136 struct samr_OpenDomain opendom;
137 int i;
138 struct lsa_String name;
139 struct policy_handle handle;
140 struct policy_handle domain_handle;
142 name.string = lpcfg_workgroup(tctx->lp_ctx);
143 r.in.domain_name = &name;
144 r.out.info = &info;
146 connect_r.in.system_name = 0;
147 connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
148 connect_r.out.connect_handle = &handle;
150 torture_comment(tctx, "Testing Connect and OpenDomain on BUILTIN\n");
152 torture_assert_ntstatus_ok(tctx, dcerpc_samr_Connect_r(b, tctx, &connect_r),
153 "Connect failed");
154 if (!NT_STATUS_IS_OK(connect_r.out.result)) {
155 if (NT_STATUS_EQUAL(connect_r.out.result, NT_STATUS_ACCESS_DENIED)) {
156 torture_comment(tctx, "Connect failed (expected, schannel mapped to anonymous): %s\n",
157 nt_errstr(connect_r.out.result));
158 } else {
159 torture_comment(tctx, "Connect failed - %s\n", nt_errstr(connect_r.out.result));
160 return false;
162 } else {
163 opendom.in.connect_handle = &handle;
164 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
165 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
166 opendom.out.domain_handle = &domain_handle;
168 torture_assert_ntstatus_ok(tctx, dcerpc_samr_OpenDomain_r(b, tctx, &opendom),
169 "OpenDomain failed");
170 if (!NT_STATUS_IS_OK(opendom.out.result)) {
171 torture_comment(tctx, "OpenDomain failed - %s\n", nt_errstr(opendom.out.result));
172 return false;
176 torture_comment(tctx, "Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
178 /* do several ops to test credential chaining */
179 for (i=0;i<5;i++) {
180 torture_assert_ntstatus_ok(tctx, dcerpc_samr_GetDomPwInfo_r(b, tctx, &r),
181 "GetDomPwInfo failed");
182 if (!NT_STATUS_IS_OK(r.out.result)) {
183 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED)) {
184 torture_comment(tctx, "GetDomPwInfo op %d failed - %s\n", i, nt_errstr(r.out.result));
185 return false;
190 return true;
195 do some lsa ops using the schannel connection
197 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
199 struct lsa_GetUserName r;
200 bool ret = true;
201 struct lsa_String *account_name_p = NULL;
202 struct lsa_String *authority_name_p = NULL;
203 struct dcerpc_binding_handle *b = p->binding_handle;
205 torture_comment(tctx, "\nTesting GetUserName\n");
207 r.in.system_name = "\\";
208 r.in.account_name = &account_name_p;
209 r.in.authority_name = &authority_name_p;
210 r.out.account_name = &account_name_p;
212 /* do several ops to test credential chaining and various operations */
213 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
214 "lsa_GetUserName failed");
216 authority_name_p = *r.out.authority_name;
218 if (!NT_STATUS_IS_OK(r.out.result)) {
219 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(r.out.result));
220 return false;
221 } else {
222 if (!r.out.account_name) {
223 return false;
226 if (strcmp(account_name_p->string, "ANONYMOUS LOGON") != 0) {
227 torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
228 account_name_p->string, "ANONYMOUS LOGON");
229 /* FIXME: gd */
230 if (!torture_setting_bool(tctx, "samba3", false)) {
231 return false;
234 if (!authority_name_p || !authority_name_p->string) {
235 return false;
238 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
239 torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
240 authority_name_p->string, "NT AUTHORITY");
241 /* FIXME: gd */
242 if (!torture_setting_bool(tctx, "samba3", false)) {
243 return false;
248 return ret;
253 test a schannel connection with the given flags
255 static bool test_schannel(struct torture_context *tctx,
256 uint16_t acct_flags, uint32_t dcerpc_flags,
257 int i)
259 struct test_join *join_ctx;
260 NTSTATUS status;
261 const char *binding = torture_setting_string(tctx, "binding", NULL);
262 struct dcerpc_binding *b;
263 struct dcerpc_pipe *p = NULL;
264 struct dcerpc_pipe *p_netlogon = NULL;
265 struct dcerpc_pipe *p_netlogon2 = NULL;
266 struct dcerpc_pipe *p_netlogon3 = NULL;
267 struct dcerpc_pipe *p_samr2 = NULL;
268 struct dcerpc_pipe *p_lsa = NULL;
269 struct netlogon_creds_CredentialState *creds;
270 struct cli_credentials *credentials;
271 enum dcerpc_transport_t transport;
273 join_ctx = torture_join_domain(tctx,
274 talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
275 acct_flags, &credentials);
276 torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
278 status = dcerpc_parse_binding(tctx, binding, &b);
279 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
281 b->flags &= ~DCERPC_AUTH_OPTIONS;
282 b->flags |= dcerpc_flags;
284 status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
285 credentials, tctx->ev, tctx->lp_ctx);
286 torture_assert_ntstatus_ok(tctx, status,
287 "Failed to connect to samr with schannel");
289 torture_assert(tctx, test_samr_ops(tctx, p->binding_handle),
290 "Failed to process schannel secured SAMR ops");
292 /* Also test that when we connect to the netlogon pipe, that
293 * the credentials we setup on the first pipe are valid for
294 * the second */
296 /* Swap the binding details from SAMR to NETLOGON */
297 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
298 torture_assert_ntstatus_ok(tctx, status, "epm map");
300 status = dcerpc_secondary_connection(p, &p_netlogon,
302 torture_assert_ntstatus_ok(tctx, status, "secondary connection");
304 status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon,
305 credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
306 DCERPC_AUTH_TYPE_SCHANNEL,
307 dcerpc_auth_level(p->conn),
308 NULL);
310 torture_assert_ntstatus_ok(tctx, status, "bind auth");
312 status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
313 torture_assert_ntstatus_ok(tctx, status, "schannel creds");
315 /* checks the capabilities */
316 torture_assert(tctx, test_netlogon_capabilities(p_netlogon, tctx, credentials, creds),
317 "Failed to process schannel secured capability ops (on fresh connection)");
319 /* do a couple of logins */
320 torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
321 "Failed to process schannel secured NETLOGON ops");
323 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
324 "Failed to process schannel secured NETLOGON EX ops");
326 /* Swap the binding details from SAMR to LSARPC */
327 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
328 torture_assert_ntstatus_ok(tctx, status, "epm map");
330 torture_assert_ntstatus_ok(tctx,
331 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
332 credentials, tctx->ev, tctx->lp_ctx),
333 "failed to connect lsarpc with schannel");
335 torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
336 "Failed to process schannel secured LSA ops");
338 talloc_free(p_lsa);
339 p_lsa = NULL;
341 /* we *MUST* use ncacn_ip_tcp for lookupsids3/lookupnames4 */
342 transport = b->transport;
343 b->transport = NCACN_IP_TCP;
345 torture_assert_ntstatus_ok(tctx,
346 dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx),
347 "failed to call epm map");
349 torture_assert_ntstatus_ok(tctx,
350 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
351 credentials, tctx->ev, tctx->lp_ctx),
352 "failed to connect lsarpc with schannel");
354 torture_assert(tctx,
355 test_many_LookupSids(p_lsa, tctx, NULL),
356 "LsaLookupSids3 failed!\n");
358 b->transport = transport;
360 /* Drop the socket, we want to start from scratch */
361 talloc_free(p);
362 p = NULL;
364 /* Now see what we are still allowed to do */
366 status = dcerpc_parse_binding(tctx, binding, &b);
367 torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
369 b->flags &= ~DCERPC_AUTH_OPTIONS;
370 b->flags |= dcerpc_flags;
372 status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
373 credentials, tctx->ev, tctx->lp_ctx);
374 torture_assert_ntstatus_ok(tctx, status,
375 "Failed to connect with schannel");
377 /* do a some SAMR operations. We have *not* done a new serverauthenticate */
378 torture_assert (tctx, test_samr_ops(tctx, p_samr2->binding_handle),
379 "Failed to process schannel secured SAMR ops (on fresh connection)");
381 /* Swap the binding details from SAMR to NETLOGON */
382 status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
383 torture_assert_ntstatus_ok(tctx, status, "epm");
385 status = dcerpc_secondary_connection(p_samr2, &p_netlogon2,
387 torture_assert_ntstatus_ok(tctx, status, "secondary connection");
389 /* and now setup an SCHANNEL bind on netlogon */
390 status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
391 credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
392 DCERPC_AUTH_TYPE_SCHANNEL,
393 dcerpc_auth_level(p_samr2->conn),
394 NULL);
396 torture_assert_ntstatus_ok(tctx, status, "auth failed");
398 /* checks the capabilities */
399 torture_assert(tctx, test_netlogon_capabilities(p_netlogon2, tctx, credentials, creds),
400 "Failed to process schannel secured capability ops (on fresh connection)");
402 /* Try the schannel-only SamLogonEx operation */
403 torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
404 "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
407 /* And the more traditional style, proving that the
408 * credentials chaining state is fully present */
409 torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
410 "Failed to process schannel secured NETLOGON ops (on fresh connection)");
412 /* Drop the socket, we want to start from scratch (again) */
413 talloc_free(p_samr2);
415 /* We don't want schannel for this test */
416 b->flags &= ~DCERPC_AUTH_OPTIONS;
418 status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
419 credentials, tctx->ev, tctx->lp_ctx);
420 torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
422 torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
423 "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
425 /* Required because the previous call will mark the current context as having failed */
426 tctx->last_result = TORTURE_OK;
427 tctx->last_reason = NULL;
429 torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
430 "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
432 torture_leave_domain(tctx, join_ctx);
433 return true;
439 a schannel test suite
441 bool torture_rpc_schannel(struct torture_context *torture)
443 bool ret = true;
444 struct {
445 uint16_t acct_flags;
446 uint32_t dcerpc_flags;
447 } tests[] = {
448 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
449 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
450 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
451 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
452 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
453 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
454 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
455 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
457 int i;
459 for (i=0;i<ARRAY_SIZE(tests);i++) {
460 torture_comment(torture, "Testing with acct_flags=0x%x dcerpc_flags=0x%x \n",
461 tests[i].acct_flags, tests[i].dcerpc_flags);
463 if (!test_schannel(torture,
464 tests[i].acct_flags, tests[i].dcerpc_flags,
465 i)) {
466 torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
467 tests[i].acct_flags, tests[i].dcerpc_flags);
468 ret = false;
472 return ret;
476 test two schannel connections
478 bool torture_rpc_schannel2(struct torture_context *torture)
480 struct test_join *join_ctx;
481 NTSTATUS status;
482 const char *binding = torture_setting_string(torture, "binding", NULL);
483 struct dcerpc_binding *b;
484 struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
485 struct cli_credentials *credentials1, *credentials2;
486 uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
488 join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
489 ACB_WSTRUST, &credentials1);
490 torture_assert(torture, join_ctx != NULL,
491 "Failed to join domain with acct_flags=ACB_WSTRUST");
493 credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
494 credentials1->netlogon_creds = NULL;
495 credentials2->netlogon_creds = NULL;
497 status = dcerpc_parse_binding(torture, binding, &b);
498 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
500 b->flags &= ~DCERPC_AUTH_OPTIONS;
501 b->flags |= dcerpc_flags;
503 torture_comment(torture, "Opening first connection\n");
504 status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
505 credentials1, torture->ev, torture->lp_ctx);
506 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
508 torture_comment(torture, "Opening second connection\n");
509 status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
510 credentials2, torture->ev, torture->lp_ctx);
511 torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
513 credentials1->netlogon_creds = NULL;
514 credentials2->netlogon_creds = NULL;
516 torture_comment(torture, "Testing logon on pipe1\n");
517 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
518 return false;
520 torture_comment(torture, "Testing logon on pipe2\n");
521 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
522 return false;
524 torture_comment(torture, "Again on pipe1\n");
525 if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
526 return false;
528 torture_comment(torture, "Again on pipe2\n");
529 if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
530 return false;
532 torture_leave_domain(torture, join_ctx);
533 return true;
536 struct torture_schannel_bench;
538 struct torture_schannel_bench_conn {
539 struct torture_schannel_bench *s;
540 int index;
541 struct cli_credentials *wks_creds;
542 struct dcerpc_pipe *pipe;
543 struct netr_LogonSamLogonEx r;
544 struct netr_NetworkInfo ninfo;
545 TALLOC_CTX *tmp;
546 uint64_t total;
547 uint32_t count;
550 struct torture_schannel_bench {
551 struct torture_context *tctx;
552 bool progress;
553 int timelimit;
554 int nprocs;
555 int nconns;
556 struct torture_schannel_bench_conn *conns;
557 struct test_join *join_ctx1;
558 struct cli_credentials *wks_creds1;
559 struct test_join *join_ctx2;
560 struct cli_credentials *wks_creds2;
561 struct cli_credentials *user1_creds;
562 struct cli_credentials *user2_creds;
563 struct dcerpc_binding *b;
564 NTSTATUS error;
565 uint64_t total;
566 uint32_t count;
567 bool stopped;
570 static void torture_schannel_bench_connected(struct composite_context *c)
572 struct torture_schannel_bench_conn *conn =
573 (struct torture_schannel_bench_conn *)c->async.private_data;
574 struct torture_schannel_bench *s = talloc_get_type(conn->s,
575 struct torture_schannel_bench);
577 s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
578 torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
579 if (NT_STATUS_IS_OK(s->error)) {
580 s->nconns++;
584 static void torture_schannel_bench_recv(struct tevent_req *subreq);
586 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
588 struct torture_schannel_bench *s = conn->s;
589 NTSTATUS status;
590 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
591 int flags = CLI_CRED_NTLM_AUTH;
592 struct tevent_req *subreq;
593 struct cli_credentials *user_creds;
595 if (conn->total % 2) {
596 user_creds = s->user1_creds;
597 } else {
598 user_creds = s->user2_creds;
601 if (lpcfg_client_lanman_auth(s->tctx->lp_ctx)) {
602 flags |= CLI_CRED_LANMAN_AUTH;
605 if (lpcfg_client_ntlmv2_auth(s->tctx->lp_ctx)) {
606 flags |= CLI_CRED_NTLMv2_AUTH;
609 talloc_free(conn->tmp);
610 conn->tmp = talloc_new(s);
611 ZERO_STRUCT(conn->ninfo);
612 ZERO_STRUCT(conn->r);
614 cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
615 &conn->ninfo.identity_info.account_name.string,
616 &conn->ninfo.identity_info.domain_name.string);
618 generate_random_buffer(conn->ninfo.challenge,
619 sizeof(conn->ninfo.challenge));
620 chal = data_blob_const(conn->ninfo.challenge,
621 sizeof(conn->ninfo.challenge));
623 names_blob = NTLMv2_generate_names_blob(conn->tmp,
624 cli_credentials_get_workstation(conn->wks_creds),
625 cli_credentials_get_domain(conn->wks_creds));
627 status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
628 &flags,
629 chal,
630 names_blob,
631 &lm_resp, &nt_resp,
632 NULL, NULL);
633 torture_assert_ntstatus_ok(s->tctx, status,
634 "cli_credentials_get_ntlm_response failed");
636 conn->ninfo.lm.data = lm_resp.data;
637 conn->ninfo.lm.length = lm_resp.length;
639 conn->ninfo.nt.data = nt_resp.data;
640 conn->ninfo.nt.length = nt_resp.length;
642 conn->ninfo.identity_info.parameter_control = 0;
643 conn->ninfo.identity_info.logon_id_low = 0;
644 conn->ninfo.identity_info.logon_id_high = 0;
645 conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
647 conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
648 conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
649 conn->r.in.logon_level = 2;
650 conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
651 conn->r.in.logon->network = &conn->ninfo;
652 conn->r.in.flags = talloc(conn->tmp, uint32_t);
653 conn->r.in.validation_level = 2;
654 conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
655 conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
656 conn->r.out.flags = conn->r.in.flags;
658 subreq = dcerpc_netr_LogonSamLogonEx_r_send(s, s->tctx->ev,
659 conn->pipe->binding_handle,
660 &conn->r);
661 torture_assert(s->tctx, subreq, "Failed to setup LogonSamLogonEx request");
663 tevent_req_set_callback(subreq, torture_schannel_bench_recv, conn);
665 return true;
668 static void torture_schannel_bench_recv(struct tevent_req *subreq)
670 bool ret;
671 struct torture_schannel_bench_conn *conn =
672 (struct torture_schannel_bench_conn *)tevent_req_callback_data_void(subreq);
673 struct torture_schannel_bench *s = talloc_get_type(conn->s,
674 struct torture_schannel_bench);
676 s->error = dcerpc_netr_LogonSamLogonEx_r_recv(subreq, subreq);
677 TALLOC_FREE(subreq);
678 if (!NT_STATUS_IS_OK(s->error)) {
679 return;
682 conn->total++;
683 conn->count++;
685 if (s->stopped) {
686 return;
689 ret = torture_schannel_bench_start(conn);
690 if (!ret) {
691 s->error = NT_STATUS_INTERNAL_ERROR;
696 test multiple schannel connection in parallel
698 bool torture_rpc_schannel_bench1(struct torture_context *torture)
700 bool ret = true;
701 NTSTATUS status;
702 const char *binding = torture_setting_string(torture, "binding", NULL);
703 struct torture_schannel_bench *s;
704 struct timeval start;
705 struct timeval end;
706 int i;
707 const char *tmp;
709 s = talloc_zero(torture, struct torture_schannel_bench);
710 s->tctx = torture;
711 s->progress = torture_setting_bool(torture, "progress", true);
712 s->timelimit = torture_setting_int(torture, "timelimit", 10);
713 s->nprocs = torture_setting_int(torture, "nprocs", 4);
714 s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
716 s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
717 cmdline_credentials,
718 sizeof(*s->user1_creds));
719 tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
720 if (tmp) {
721 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
723 s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
724 cmdline_credentials,
725 sizeof(*s->user1_creds));
726 tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
727 if (tmp) {
728 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
731 s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
732 ACB_WSTRUST, &s->wks_creds1);
733 torture_assert(torture, s->join_ctx1 != NULL,
734 "Failed to join domain with acct_flags=ACB_WSTRUST");
735 s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
736 ACB_WSTRUST, &s->wks_creds2);
737 torture_assert(torture, s->join_ctx2 != NULL,
738 "Failed to join domain with acct_flags=ACB_WSTRUST");
740 cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
741 cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
743 for (i=0; i < s->nprocs; i++) {
744 s->conns[i].s = s;
745 s->conns[i].index = i;
746 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
747 s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
748 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
749 memcpy(s->conns[i].wks_creds, s->wks_creds2,
750 talloc_get_size(s->conns[i].wks_creds));
752 s->conns[i].wks_creds->netlogon_creds = NULL;
755 status = dcerpc_parse_binding(s, binding, &s->b);
756 torture_assert_ntstatus_ok(torture, status, "Bad binding string");
757 s->b->flags &= ~DCERPC_AUTH_OPTIONS;
758 s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
760 torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
761 for (i=0; i < s->nprocs; i++) {
762 #if 1
763 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
764 &ndr_table_netlogon,
765 s->conns[i].wks_creds,
766 torture->ev, torture->lp_ctx);
767 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
768 #else
770 * This path doesn't work against windows,
771 * because of windows drops the connections
772 * which haven't reached a session setup yet
774 * The same as the reset on zero vc stuff.
776 struct composite_context *c;
777 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
778 &ndr_table_netlogon,
779 s->conns[i].wks_creds,
780 torture->ev,
781 torture->lp_ctx);
782 torture_assert(torture, c != NULL, "Failed to setup connect");
783 c->async.fn = torture_schannel_bench_connected;
784 c->async.private_data = &s->conns[i];
787 while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
788 int ev_ret = tevent_loop_once(torture->ev);
789 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
790 #endif
792 torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
795 * Change the workstation password after establishing the netlogon
796 * schannel connections to prove that existing connections are not
797 * affected by a wks pwchange.
801 struct netr_ServerPasswordSet pwset;
802 char *password = generate_random_password(s->join_ctx1, 8, 255);
803 struct netlogon_creds_CredentialState *creds_state;
804 struct dcerpc_pipe *net_pipe;
805 struct netr_Authenticator credential, return_authenticator;
806 struct samr_Password new_password;
808 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
809 &ndr_table_netlogon,
810 s->wks_creds1,
811 torture->ev, torture->lp_ctx);
813 torture_assert_ntstatus_ok(torture, status,
814 "dcerpc_pipe_connect_b failed");
816 pwset.in.server_name = talloc_asprintf(
817 net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
818 pwset.in.computer_name =
819 cli_credentials_get_workstation(s->wks_creds1);
820 pwset.in.account_name = talloc_asprintf(
821 net_pipe, "%s$", pwset.in.computer_name);
822 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
823 pwset.in.credential = &credential;
824 pwset.in.new_password = &new_password;
825 pwset.out.return_authenticator = &return_authenticator;
827 E_md4hash(password, new_password.hash);
829 creds_state = cli_credentials_get_netlogon_creds(
830 s->wks_creds1);
831 netlogon_creds_des_encrypt(creds_state, &new_password);
832 netlogon_creds_client_authenticator(creds_state, &credential);
834 torture_assert_ntstatus_ok(torture, dcerpc_netr_ServerPasswordSet_r(net_pipe->binding_handle, torture, &pwset),
835 "ServerPasswordSet failed");
836 torture_assert_ntstatus_ok(torture, pwset.out.result,
837 "ServerPasswordSet failed");
839 if (!netlogon_creds_client_check(creds_state,
840 &pwset.out.return_authenticator->cred)) {
841 torture_comment(torture, "Credential chaining failed\n");
844 cli_credentials_set_password(s->wks_creds1, password,
845 CRED_SPECIFIED);
847 talloc_free(net_pipe);
849 /* Just as a test, connect with the new creds */
851 talloc_free(s->wks_creds1->netlogon_creds);
852 s->wks_creds1->netlogon_creds = NULL;
854 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
855 &ndr_table_netlogon,
856 s->wks_creds1,
857 torture->ev, torture->lp_ctx);
859 torture_assert_ntstatus_ok(torture, status,
860 "dcerpc_pipe_connect_b failed");
862 talloc_free(net_pipe);
865 torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
866 s->nprocs, s->timelimit);
867 for (i=0; i < s->nprocs; i++) {
868 ret = torture_schannel_bench_start(&s->conns[i]);
869 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
872 start = timeval_current();
873 end = timeval_add(&start, s->timelimit, 0);
875 while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
876 int ev_ret = tevent_loop_once(torture->ev);
877 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
879 torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
880 s->stopped = true;
881 talloc_free(s->conns);
883 for (i=0; i < s->nprocs; i++) {
884 s->total += s->conns[i].total;
887 torture_comment(torture,
888 "Total ops[%llu] (%u ops/s)\n",
889 (unsigned long long)s->total,
890 (unsigned)s->total/s->timelimit);
892 torture_leave_domain(torture, s->join_ctx1);
893 torture_leave_domain(torture, s->join_ctx2);
894 return true;