Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[sfrench/samba-autobuild/.git] / source4 / torture / rpc / schannel.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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/>.
20 */
21
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"
38
39 #define TEST_MACHINE_NAME "schannel"
40
41 /*
42   try a netlogon SamLogon
43 */
44 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx, 
45                           struct cli_credentials *credentials, 
46                           struct creds_CredentialState *creds)
47 {
48         NTSTATUS status;
49         struct netr_LogonSamLogonEx r;
50         struct netr_NetworkInfo ninfo;
51         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
52         int i;
53         int flags = CLI_CRED_NTLM_AUTH;
54         if (lp_client_lanman_auth(tctx->lp_ctx)) {
55                 flags |= CLI_CRED_LANMAN_AUTH;
56         }
57
58         if (lp_client_ntlmv2_auth(tctx->lp_ctx)) {
59                 flags |= CLI_CRED_NTLMv2_AUTH;
60         }
61
62         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx, 
63                                                  &ninfo.identity_info.account_name.string,
64                                                  &ninfo.identity_info.domain_name.string);
65         
66         generate_random_buffer(ninfo.challenge, 
67                                sizeof(ninfo.challenge));
68         chal = data_blob_const(ninfo.challenge, 
69                                sizeof(ninfo.challenge));
70
71         names_blob = NTLMv2_generate_names_blob(tctx, lp_iconv_convenience(tctx->lp_ctx), cli_credentials_get_workstation(credentials), 
72                                                 cli_credentials_get_domain(credentials));
73
74         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx, 
75                                                    &flags, 
76                                                    chal,
77                                                    names_blob,
78                                                    &lm_resp, &nt_resp,
79                                                    NULL, NULL);
80         torture_assert_ntstatus_ok(tctx, status, 
81                                    "cli_credentials_get_ntlm_response failed");
82
83         ninfo.lm.data = lm_resp.data;
84         ninfo.lm.length = lm_resp.length;
85
86         ninfo.nt.data = nt_resp.data;
87         ninfo.nt.length = nt_resp.length;
88
89         ninfo.identity_info.parameter_control = 0;
90         ninfo.identity_info.logon_id_low = 0;
91         ninfo.identity_info.logon_id_high = 0;
92         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
93
94         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
95         r.in.computer_name = cli_credentials_get_workstation(credentials);
96         r.in.logon_level = 2;
97         r.in.logon.network = &ninfo;
98         r.in.flags = 0;
99
100         torture_comment(tctx, 
101                         "Testing LogonSamLogonEx with name %s\n", 
102                         ninfo.identity_info.account_name.string);
103         
104         for (i=2;i<3;i++) {
105                 r.in.validation_level = i;
106                 
107                 status = dcerpc_netr_LogonSamLogonEx(p, tctx, &r);
108                 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
109         }
110
111         return true;
112 }
113
114 /*
115   do some samr ops using the schannel connection
116  */
117 static bool test_samr_ops(struct torture_context *tctx,
118                           struct dcerpc_pipe *p)
119 {
120         NTSTATUS status;
121         struct samr_GetDomPwInfo r;
122         struct samr_Connect connect;
123         struct samr_OpenDomain opendom;
124         int i;
125         struct lsa_String name;
126         struct policy_handle handle;
127         struct policy_handle domain_handle;
128
129         name.string = lp_workgroup(tctx->lp_ctx);
130         r.in.domain_name = &name;
131
132         connect.in.system_name = 0;
133         connect.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
134         connect.out.connect_handle = &handle;
135         
136         printf("Testing Connect and OpenDomain on BUILTIN\n");
137
138         status = dcerpc_samr_Connect(p, tctx, &connect);
139         if (!NT_STATUS_IS_OK(status)) {
140                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
141                         printf("Connect failed (expected, schannel mapped to anonymous): %s\n",
142                                nt_errstr(status));
143                 } else {
144                         printf("Connect failed - %s\n", nt_errstr(status));
145                         return false;
146                 }
147         } else {
148                 opendom.in.connect_handle = &handle;
149                 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
150                 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
151                 opendom.out.domain_handle = &domain_handle;
152                 
153                 status = dcerpc_samr_OpenDomain(p, tctx, &opendom);
154                 if (!NT_STATUS_IS_OK(status)) {
155                         printf("OpenDomain failed - %s\n", nt_errstr(status));
156                         return false;
157                 }
158         }
159
160         printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
161         
162         /* do several ops to test credential chaining */
163         for (i=0;i<5;i++) {
164                 status = dcerpc_samr_GetDomPwInfo(p, tctx, &r);
165                 if (!NT_STATUS_IS_OK(status)) {
166                         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
167                                 printf("GetDomPwInfo op %d failed - %s\n", i, nt_errstr(status));
168                                 return false;
169                         }
170                 }
171         }
172
173         return true;
174 }
175
176
177 /*
178   do some lsa ops using the schannel connection
179  */
180 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
181 {
182         struct lsa_GetUserName r;
183         NTSTATUS status;
184         bool ret = true;
185         struct lsa_StringPointer authority_name_p;
186
187         printf("\nTesting GetUserName\n");
188
189         r.in.system_name = "\\";        
190         r.in.account_name = NULL;       
191         r.in.authority_name = &authority_name_p;
192         authority_name_p.string = NULL;
193
194         /* do several ops to test credential chaining and various operations */
195         status = dcerpc_lsa_GetUserName(p, tctx, &r);
196         
197         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
198                 printf("not considering %s to be an error\n", nt_errstr(status));
199         } else if (!NT_STATUS_IS_OK(status)) {
200                 printf("GetUserName failed - %s\n", nt_errstr(status));
201                 return false;
202         } else {
203                 if (!r.out.account_name) {
204                         return false;
205                 }
206                 
207                 if (strcmp(r.out.account_name->string, "ANONYMOUS LOGON") != 0) {
208                         printf("GetUserName returned wrong user: %s, expected %s\n",
209                                r.out.account_name->string, "ANONYMOUS LOGON");
210                         return false;
211                 }
212                 if (!r.out.authority_name || !r.out.authority_name->string) {
213                         return false;
214                 }
215                 
216                 if (strcmp(r.out.authority_name->string->string, "NT AUTHORITY") != 0) {
217                         printf("GetUserName returned wrong user: %s, expected %s\n",
218                                r.out.authority_name->string->string, "NT AUTHORITY");
219                         return false;
220                 }
221         }
222         if (!test_many_LookupSids(p, tctx, NULL)) {
223                 printf("LsaLookupSids3 failed!\n");
224                 return false;
225         }
226
227         return ret;
228 }
229
230
231 /*
232   test a schannel connection with the given flags
233  */
234 static bool test_schannel(struct torture_context *tctx,
235                           uint16_t acct_flags, uint32_t dcerpc_flags,
236                           int i)
237 {
238         struct test_join *join_ctx;
239         NTSTATUS status;
240         const char *binding = torture_setting_string(tctx, "binding", NULL);
241         struct dcerpc_binding *b;
242         struct dcerpc_pipe *p = NULL;
243         struct dcerpc_pipe *p_netlogon = NULL;
244         struct dcerpc_pipe *p_netlogon2 = NULL;
245         struct dcerpc_pipe *p_netlogon3 = NULL;
246         struct dcerpc_pipe *p_samr2 = NULL;
247         struct dcerpc_pipe *p_lsa = NULL;
248         struct creds_CredentialState *creds;
249         struct cli_credentials *credentials;
250
251         join_ctx = torture_join_domain(tctx, 
252                                        talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i), 
253                                        acct_flags, &credentials);
254         torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
255
256         status = dcerpc_parse_binding(tctx, binding, &b);
257         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
258
259         b->flags &= ~DCERPC_AUTH_OPTIONS;
260         b->flags |= dcerpc_flags;
261
262         status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
263                                        credentials, tctx->ev, tctx->lp_ctx);
264         torture_assert_ntstatus_ok(tctx, status, 
265                 "Failed to connect with schannel");
266
267         torture_assert(tctx, test_samr_ops(tctx, p), 
268                        "Failed to process schannel secured SAMR ops");
269
270         /* Also test that when we connect to the netlogon pipe, that
271          * the credentials we setup on the first pipe are valid for
272          * the second */
273
274         /* Swap the binding details from SAMR to NETLOGON */
275         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
276         torture_assert_ntstatus_ok(tctx, status, "epm map");
277
278         status = dcerpc_secondary_connection(p, &p_netlogon, 
279                                              b);
280         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
281
282         status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon, 
283                                   credentials, tctx->lp_ctx,
284                                   DCERPC_AUTH_TYPE_SCHANNEL,
285                                   dcerpc_auth_level(p->conn),
286                                   NULL);
287
288         torture_assert_ntstatus_ok(tctx, status, "bind auth");
289
290         status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
291         torture_assert_ntstatus_ok(tctx, status, "schannel creds");
292
293         /* do a couple of logins */
294         torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
295                 "Failed to process schannel secured NETLOGON ops");
296
297         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
298                 "Failed to process schannel secured NETLOGON EX ops");
299
300         /* Swap the binding details from SAMR to LSARPC */
301         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
302         torture_assert_ntstatus_ok(tctx, status, "epm map");
303
304         status = dcerpc_secondary_connection(p, &p_lsa, 
305                                              b);
306
307         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
308
309         status = dcerpc_bind_auth(p_lsa, &ndr_table_lsarpc,
310                                   credentials, tctx->lp_ctx,
311                                   DCERPC_AUTH_TYPE_SCHANNEL,
312                                   dcerpc_auth_level(p->conn),
313                                   NULL);
314
315         torture_assert_ntstatus_ok(tctx, status, "bind auth");
316
317         torture_assert(tctx, test_lsa_ops(tctx, p_lsa), 
318                 "Failed to process schannel secured LSA ops");
319
320         /* Drop the socket, we want to start from scratch */
321         talloc_free(p);
322         p = NULL;
323
324         /* Now see what we are still allowed to do */
325         
326         status = dcerpc_parse_binding(tctx, binding, &b);
327         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
328
329         b->flags &= ~DCERPC_AUTH_OPTIONS;
330         b->flags |= dcerpc_flags;
331
332         status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
333                                        credentials, tctx->ev, tctx->lp_ctx);
334         torture_assert_ntstatus_ok(tctx, status, 
335                 "Failed to connect with schannel");
336
337         /* do a some SAMR operations.  We have *not* done a new serverauthenticate */
338         torture_assert (tctx, test_samr_ops(tctx, p_samr2), 
339                         "Failed to process schannel secured SAMR ops (on fresh connection)");
340
341         /* Swap the binding details from SAMR to NETLOGON */
342         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
343         torture_assert_ntstatus_ok(tctx, status, "epm");
344
345         status = dcerpc_secondary_connection(p_samr2, &p_netlogon2, 
346                                              b);
347         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
348
349         /* and now setup an SCHANNEL bind on netlogon */
350         status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
351                                   credentials, tctx->lp_ctx,
352                                   DCERPC_AUTH_TYPE_SCHANNEL,
353                                   dcerpc_auth_level(p_samr2->conn),
354                                   NULL);
355
356         torture_assert_ntstatus_ok(tctx, status, "auth failed");
357         
358         /* Try the schannel-only SamLogonEx operation */
359         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds), 
360                        "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
361                 
362
363         /* And the more traditional style, proving that the
364          * credentials chaining state is fully present */
365         torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
366                              "Failed to process schannel secured NETLOGON ops (on fresh connection)");
367
368         /* Drop the socket, we want to start from scratch (again) */
369         talloc_free(p_samr2);
370
371         /* We don't want schannel for this test */
372         b->flags &= ~DCERPC_AUTH_OPTIONS;
373
374         status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
375                                        credentials, tctx->ev, tctx->lp_ctx);
376         torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
377
378         torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
379                         "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
380
381         /* Required because the previous call will mark the current context as having failed */
382         tctx->last_result = TORTURE_OK;
383         tctx->last_reason = NULL;
384
385         torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
386                         "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
387
388         torture_leave_domain(join_ctx);
389         return true;
390 }
391
392
393
394 /*
395   a schannel test suite
396  */
397 bool torture_rpc_schannel(struct torture_context *torture)
398 {
399         bool ret = true;
400         struct {
401                 uint16_t acct_flags;
402                 uint32_t dcerpc_flags;
403         } tests[] = {
404                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN},
405                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL},
406                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
407                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
408                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN },
409                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL },
410                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
411                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
412         };
413         int i;
414
415         for (i=0;i<ARRAY_SIZE(tests);i++) {
416                 if (!test_schannel(torture, 
417                                    tests[i].acct_flags, tests[i].dcerpc_flags,
418                                    i)) {
419                         torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
420                                tests[i].acct_flags, tests[i].dcerpc_flags);
421                         ret = false;
422                 }
423         }
424
425         return ret;
426 }
427
428 /*
429   test two schannel connections
430  */
431 bool torture_rpc_schannel2(struct torture_context *torture)
432 {
433         struct test_join *join_ctx;
434         NTSTATUS status;
435         const char *binding = torture_setting_string(torture, "binding", NULL);
436         struct dcerpc_binding *b;
437         struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
438         struct cli_credentials *credentials1, *credentials2;
439         uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
440
441         join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME), 
442                                        ACB_WSTRUST, &credentials1);
443         torture_assert(torture, join_ctx != NULL, 
444                        "Failed to join domain with acct_flags=ACB_WSTRUST");
445
446         credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
447         credentials1->netlogon_creds = NULL;
448         credentials2->netlogon_creds = NULL;
449
450         status = dcerpc_parse_binding(torture, binding, &b);
451         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
452
453         b->flags &= ~DCERPC_AUTH_OPTIONS;
454         b->flags |= dcerpc_flags;
455
456         printf("Opening first connection\n");
457         status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
458                                        credentials1, torture->ev, torture->lp_ctx);
459         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
460
461         torture_comment(torture, "Opening second connection\n");
462         status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
463                                        credentials2, torture->ev, torture->lp_ctx);
464         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
465
466         credentials1->netlogon_creds = NULL;
467         credentials2->netlogon_creds = NULL;
468
469         torture_comment(torture, "Testing logon on pipe1\n");
470         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
471                 return false;
472
473         torture_comment(torture, "Testing logon on pipe2\n");
474         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
475                 return false;
476
477         torture_comment(torture, "Again on pipe1\n");
478         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
479                 return false;
480
481         torture_comment(torture, "Again on pipe2\n");
482         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
483                 return false;
484
485         torture_leave_domain(join_ctx);
486         return true;
487 }
488
489 struct torture_schannel_bench;
490
491 struct torture_schannel_bench_conn {
492         struct torture_schannel_bench *s;
493         int index;
494         struct cli_credentials *wks_creds;
495         struct dcerpc_pipe *pipe;
496         struct netr_LogonSamLogonEx r;
497         struct netr_NetworkInfo ninfo;
498         TALLOC_CTX *tmp;
499         uint64_t total;
500         uint32_t count;
501 };
502
503 struct torture_schannel_bench {
504         struct torture_context *tctx;
505         bool progress;
506         int timelimit;
507         int nprocs;
508         int nconns;
509         struct torture_schannel_bench_conn *conns;
510         struct test_join *join_ctx1;
511         struct cli_credentials *wks_creds1;
512         struct test_join *join_ctx2;
513         struct cli_credentials *wks_creds2;
514         struct cli_credentials *user1_creds;
515         struct cli_credentials *user2_creds;
516         struct dcerpc_binding *b;
517         NTSTATUS error;
518         uint64_t total;
519         uint32_t count;
520         bool stopped;
521 };
522
523 static void torture_schannel_bench_connected(struct composite_context *c)
524 {
525         struct torture_schannel_bench_conn *conn =
526                 (struct torture_schannel_bench_conn *)c->async.private_data;
527         struct torture_schannel_bench *s = talloc_get_type(conn->s,
528                                            struct torture_schannel_bench);
529
530         s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
531         torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
532         if (NT_STATUS_IS_OK(s->error)) {
533                 s->nconns++;
534         }
535 }
536
537 static void torture_schannel_bench_recv(struct rpc_request *req);
538
539 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
540 {
541         struct torture_schannel_bench *s = conn->s;
542         NTSTATUS status;
543         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
544         int flags = CLI_CRED_NTLM_AUTH;
545         struct rpc_request *req;
546         struct cli_credentials *user_creds;
547
548         if (conn->total % 2) {
549                 user_creds = s->user1_creds;
550         } else {
551                 user_creds = s->user2_creds;
552         }
553
554         if (lp_client_lanman_auth(s->tctx->lp_ctx)) {
555                 flags |= CLI_CRED_LANMAN_AUTH;
556         }
557
558         if (lp_client_ntlmv2_auth(s->tctx->lp_ctx)) {
559                 flags |= CLI_CRED_NTLMv2_AUTH;
560         }
561
562         talloc_free(conn->tmp);
563         conn->tmp = talloc_new(s);
564         ZERO_STRUCT(conn->ninfo);
565         ZERO_STRUCT(conn->r);
566
567         cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
568                                                  &conn->ninfo.identity_info.account_name.string,
569                                                  &conn->ninfo.identity_info.domain_name.string);
570
571         generate_random_buffer(conn->ninfo.challenge,
572                                sizeof(conn->ninfo.challenge));
573         chal = data_blob_const(conn->ninfo.challenge,
574                                sizeof(conn->ninfo.challenge));
575
576         names_blob = NTLMv2_generate_names_blob(conn->tmp, lp_iconv_convenience(s->tctx->lp_ctx),
577                                                 cli_credentials_get_workstation(conn->wks_creds),
578                                                 cli_credentials_get_domain(conn->wks_creds));
579
580         status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
581                                                    &flags,
582                                                    chal,
583                                                    names_blob,
584                                                    &lm_resp, &nt_resp,
585                                                    NULL, NULL);
586         torture_assert_ntstatus_ok(s->tctx, status,
587                                    "cli_credentials_get_ntlm_response failed");
588
589         conn->ninfo.lm.data = lm_resp.data;
590         conn->ninfo.lm.length = lm_resp.length;
591
592         conn->ninfo.nt.data = nt_resp.data;
593         conn->ninfo.nt.length = nt_resp.length;
594
595         conn->ninfo.identity_info.parameter_control = 0;
596         conn->ninfo.identity_info.logon_id_low = 0;
597         conn->ninfo.identity_info.logon_id_high = 0;
598         conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
599
600         conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
601         conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
602         conn->r.in.logon_level = 2;
603         conn->r.in.logon.network = &conn->ninfo;
604         conn->r.in.flags = 0;
605         conn->r.in.validation_level = 2;
606
607         req = dcerpc_netr_LogonSamLogonEx_send(conn->pipe, conn->tmp, &conn->r);
608         torture_assert(s->tctx, req, "Failed to setup LogonSamLogonEx request");
609
610         req->async.callback = torture_schannel_bench_recv;
611         req->async.private_data = conn;
612
613         return true;
614 }
615
616 static void torture_schannel_bench_recv(struct rpc_request *req)
617 {
618         bool ret;
619         struct torture_schannel_bench_conn *conn =
620                 (struct torture_schannel_bench_conn *)req->async.private_data;
621         struct torture_schannel_bench *s = talloc_get_type(conn->s,
622                                            struct torture_schannel_bench);
623
624         s->error = dcerpc_ndr_request_recv(req);
625         if (!NT_STATUS_IS_OK(s->error)) {
626                 return;
627         }
628
629         conn->total++;
630         conn->count++;
631
632         if (s->stopped) {
633                 return;
634         }
635
636         ret = torture_schannel_bench_start(conn);
637         if (!ret) {
638                 s->error = NT_STATUS_INTERNAL_ERROR;
639         }
640 }
641
642 /*
643   test multiple schannel connection in parallel
644  */
645 bool torture_rpc_schannel_bench1(struct torture_context *torture)
646 {
647         bool ret = true;
648         NTSTATUS status;
649         const char *binding = torture_setting_string(torture, "binding", NULL);
650         struct torture_schannel_bench *s;
651         struct timeval start;
652         struct timeval end;
653         int i;
654         const char *tmp;
655
656         s = talloc_zero(torture, struct torture_schannel_bench);
657         s->tctx = torture;
658         s->progress = torture_setting_bool(torture, "progress", true);
659         s->timelimit = torture_setting_int(torture, "timelimit", 10);
660         s->nprocs = torture_setting_int(torture, "nprocs", 4);
661         s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
662
663         s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
664                                                                  cmdline_credentials,
665                                                                  sizeof(*s->user1_creds));
666         tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
667         if (tmp) {
668                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
669         }
670         s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
671                                                                  cmdline_credentials,
672                                                                  sizeof(*s->user1_creds));
673         tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
674         if (tmp) {
675                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
676         }
677
678         s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
679                                            ACB_WSTRUST, &s->wks_creds1);
680         torture_assert(torture, s->join_ctx1 != NULL,
681                        "Failed to join domain with acct_flags=ACB_WSTRUST");
682         s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
683                                            ACB_WSTRUST, &s->wks_creds2);
684         torture_assert(torture, s->join_ctx2 != NULL,
685                        "Failed to join domain with acct_flags=ACB_WSTRUST");
686
687         cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
688         cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
689
690         for (i=0; i < s->nprocs; i++) {
691                 s->conns[i].s = s;
692                 s->conns[i].index = i;
693                 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
694                         s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
695                 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
696                         memcpy(s->conns[i].wks_creds, s->wks_creds2,
697                                talloc_get_size(s->conns[i].wks_creds));
698                 }
699                 s->conns[i].wks_creds->netlogon_creds = NULL;
700         }
701
702         status = dcerpc_parse_binding(s, binding, &s->b);
703         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
704         s->b->flags &= ~DCERPC_AUTH_OPTIONS;
705         s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
706
707         torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
708         for (i=0; i < s->nprocs; i++) {
709 #if 1
710                 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
711                                                  &ndr_table_netlogon,
712                                                  s->conns[i].wks_creds,
713                                                  torture->ev, torture->lp_ctx);
714                 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
715 #else
716                 /*
717                  * This path doesn't work against windows,
718                  * because of windows drops the connections
719                  * which haven't reached a session setup yet
720                  *
721                  * The same as the reset on zero vc stuff.
722                  */
723                 struct composite_context *c;
724                 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
725                                                &ndr_table_netlogon,
726                                                s->conns[i].wks_creds,
727                                                torture->ev,
728                                                torture->lp_ctx);
729                 torture_assert(torture, c != NULL, "Failed to setup connect");
730                 c->async.fn = torture_schannel_bench_connected;
731                 c->async.private_data = &s->conns[i];
732         }
733
734         while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
735                 int ev_ret = event_loop_once(torture->ev);
736                 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
737 #endif
738         }
739         torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
740
741         torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
742                         s->nprocs, s->timelimit);
743         for (i=0; i < s->nprocs; i++) {
744                 ret = torture_schannel_bench_start(&s->conns[i]);
745                 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
746         }
747
748         start = timeval_current();
749         end = timeval_add(&start, s->timelimit, 0);
750
751         while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
752                 int ev_ret = event_loop_once(torture->ev);
753                 torture_assert(torture, ev_ret == 0, "event_loop_once failed");
754         }
755         torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
756         s->stopped = true;
757         talloc_free(s->conns);
758
759         for (i=0; i < s->nprocs; i++) {
760                 s->total += s->conns[i].total;
761         }
762
763         torture_comment(torture,
764                         "Total ops[%llu] (%u ops/s)\n",
765                         (unsigned long long)s->total,
766                         (unsigned)s->total/s->timelimit);
767
768         torture_leave_domain(s->join_ctx1);
769         torture_leave_domain(s->join_ctx2);
770         return true;
771 }