s4: fix various warnings (not "const" related ones)
[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 "../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"
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 netlogon_creds_CredentialState *creds)
47 {
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;
60         }
61
62         if (lp_client_ntlmv2_auth(tctx->lp_ctx)) {
63                 flags |= CLI_CRED_NTLMv2_AUTH;
64         }
65
66         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx, 
67                                                  &ninfo.identity_info.account_name.string,
68                                                  &ninfo.identity_info.domain_name.string);
69         
70         generate_random_buffer(ninfo.challenge, 
71                                sizeof(ninfo.challenge));
72         chal = data_blob_const(ninfo.challenge, 
73                                sizeof(ninfo.challenge));
74
75         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials), 
76                                                 cli_credentials_get_domain(credentials));
77
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");
86
87         ninfo.lm.data = lm_resp.data;
88         ninfo.lm.length = lm_resp.length;
89
90         ninfo.nt.data = nt_resp.data;
91         ninfo.nt.length = nt_resp.length;
92
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);
97
98         logon.network = &ninfo;
99
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;
108
109         torture_comment(tctx, 
110                         "Testing LogonSamLogonEx with name %s\n", 
111                         ninfo.identity_info.account_name.string);
112         
113         for (i=2;i<3;i++) {
114                 r.in.validation_level = i;
115                 
116                 status = dcerpc_netr_LogonSamLogonEx(p, tctx, &r);
117                 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
118         }
119
120         return true;
121 }
122
123 /*
124   do some samr ops using the schannel connection
125  */
126 static bool test_samr_ops(struct torture_context *tctx,
127                           struct dcerpc_pipe *p)
128 {
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;
138
139         name.string = lp_workgroup(tctx->lp_ctx);
140         r.in.domain_name = &name;
141         r.out.info = &info;
142
143         connect_r.in.system_name = 0;
144         connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
145         connect_r.out.connect_handle = &handle;
146         
147         printf("Testing Connect and OpenDomain on BUILTIN\n");
148
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;
157                 }
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;
163                 
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;
168                 }
169         }
170
171         printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
172         
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;
180                         }
181                 }
182         }
183
184         return true;
185 }
186
187
188 /*
189   do some lsa ops using the schannel connection
190  */
191 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
192 {
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;
198
199         printf("\nTesting GetUserName\n");
200
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;
205
206         /* do several ops to test credential chaining and various operations */
207         status = dcerpc_lsa_GetUserName(p, tctx, &r);
208
209         authority_name_p = *r.out.authority_name;
210
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;
219                 }
220                 
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;
227                         }
228                 }
229                 if (!authority_name_p || !authority_name_p->string) {
230                         return false;
231                 }
232                 
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;
239                         }
240                 }
241         }
242         if (!test_many_LookupSids(p, tctx, NULL)) {
243                 printf("LsaLookupSids3 failed!\n");
244                 return false;
245         }
246
247         return ret;
248 }
249
250
251 /*
252   test a schannel connection with the given flags
253  */
254 static bool test_schannel(struct torture_context *tctx,
255                           uint16_t acct_flags, uint32_t dcerpc_flags,
256                           int i)
257 {
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;
270
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");
275
276         status = dcerpc_parse_binding(tctx, binding, &b);
277         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
278
279         b->flags &= ~DCERPC_AUTH_OPTIONS;
280         b->flags |= dcerpc_flags;
281
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");
286
287         torture_assert(tctx, test_samr_ops(tctx, p), 
288                        "Failed to process schannel secured SAMR ops");
289
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 */
293
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");
297
298         status = dcerpc_secondary_connection(p, &p_netlogon, 
299                                              b);
300         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
301
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);
307
308         torture_assert_ntstatus_ok(tctx, status, "bind auth");
309
310         status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
311         torture_assert_ntstatus_ok(tctx, status, "schannel creds");
312
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");
316
317         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
318                 "Failed to process schannel secured NETLOGON EX ops");
319
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");
323
324         status = dcerpc_secondary_connection(p, &p_lsa, 
325                                              b);
326
327         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
328
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);
334
335         torture_assert_ntstatus_ok(tctx, status, "bind auth");
336
337         torture_assert(tctx, test_lsa_ops(tctx, p_lsa), 
338                 "Failed to process schannel secured LSA ops");
339
340         /* Drop the socket, we want to start from scratch */
341         talloc_free(p);
342         p = NULL;
343
344         /* Now see what we are still allowed to do */
345         
346         status = dcerpc_parse_binding(tctx, binding, &b);
347         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
348
349         b->flags &= ~DCERPC_AUTH_OPTIONS;
350         b->flags |= dcerpc_flags;
351
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");
356
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)");
360
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");
364
365         status = dcerpc_secondary_connection(p_samr2, &p_netlogon2, 
366                                              b);
367         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
368
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);
375
376         torture_assert_ntstatus_ok(tctx, status, "auth failed");
377         
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)");
381                 
382
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)");
387
388         /* Drop the socket, we want to start from scratch (again) */
389         talloc_free(p_samr2);
390
391         /* We don't want schannel for this test */
392         b->flags &= ~DCERPC_AUTH_OPTIONS;
393
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");
397
398         torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
399                         "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
400
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;
404
405         torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
406                         "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
407
408         torture_leave_domain(tctx, join_ctx);
409         return true;
410 }
411
412
413
414 /*
415   a schannel test suite
416  */
417 bool torture_rpc_schannel(struct torture_context *torture)
418 {
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 }
432         };
433         int i;
434
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;
442                 }
443         }
444
445         return ret;
446 }
447
448 /*
449   test two schannel connections
450  */
451 bool torture_rpc_schannel2(struct torture_context *torture)
452 {
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;
460
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");
465
466         credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
467         credentials1->netlogon_creds = NULL;
468         credentials2->netlogon_creds = NULL;
469
470         status = dcerpc_parse_binding(torture, binding, &b);
471         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
472
473         b->flags &= ~DCERPC_AUTH_OPTIONS;
474         b->flags |= dcerpc_flags;
475
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");
480
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");
485
486         credentials1->netlogon_creds = NULL;
487         credentials2->netlogon_creds = NULL;
488
489         torture_comment(torture, "Testing logon on pipe1\n");
490         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
491                 return false;
492
493         torture_comment(torture, "Testing logon on pipe2\n");
494         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
495                 return false;
496
497         torture_comment(torture, "Again on pipe1\n");
498         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
499                 return false;
500
501         torture_comment(torture, "Again on pipe2\n");
502         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
503                 return false;
504
505         torture_leave_domain(torture, join_ctx);
506         return true;
507 }
508
509 struct torture_schannel_bench;
510
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;
521 };
522
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;
541 };
542
543 static void torture_schannel_bench_connected(struct composite_context *c)
544 {
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);
549
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++;
554         }
555 }
556
557 static void torture_schannel_bench_recv(struct rpc_request *req);
558
559 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
560 {
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;
567
568         if (conn->total % 2) {
569                 user_creds = s->user1_creds;
570         } else {
571                 user_creds = s->user2_creds;
572         }
573
574         if (lp_client_lanman_auth(s->tctx->lp_ctx)) {
575                 flags |= CLI_CRED_LANMAN_AUTH;
576         }
577
578         if (lp_client_ntlmv2_auth(s->tctx->lp_ctx)) {
579                 flags |= CLI_CRED_NTLMv2_AUTH;
580         }
581
582         talloc_free(conn->tmp);
583         conn->tmp = talloc_new(s);
584         ZERO_STRUCT(conn->ninfo);
585         ZERO_STRUCT(conn->r);
586
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);
590
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));
595
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));
599
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");
608
609         conn->ninfo.lm.data = lm_resp.data;
610         conn->ninfo.lm.length = lm_resp.length;
611
612         conn->ninfo.nt.data = nt_resp.data;
613         conn->ninfo.nt.length = nt_resp.length;
614
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);
619
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;
630
631         req = dcerpc_netr_LogonSamLogonEx_send(conn->pipe, conn->tmp, &conn->r);
632         torture_assert(s->tctx, req, "Failed to setup LogonSamLogonEx request");
633
634         req->async.callback = torture_schannel_bench_recv;
635         req->async.private_data = conn;
636
637         return true;
638 }
639
640 static void torture_schannel_bench_recv(struct rpc_request *req)
641 {
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);
647
648         s->error = dcerpc_ndr_request_recv(req);
649         if (!NT_STATUS_IS_OK(s->error)) {
650                 return;
651         }
652
653         conn->total++;
654         conn->count++;
655
656         if (s->stopped) {
657                 return;
658         }
659
660         ret = torture_schannel_bench_start(conn);
661         if (!ret) {
662                 s->error = NT_STATUS_INTERNAL_ERROR;
663         }
664 }
665
666 /*
667   test multiple schannel connection in parallel
668  */
669 bool torture_rpc_schannel_bench1(struct torture_context *torture)
670 {
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;
679
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);
686
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);
693         }
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);
700         }
701
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");
710
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);
713
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));
722                 }
723                 s->conns[i].wks_creds->netlogon_creds = NULL;
724         }
725
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;
730
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
740                 /*
741                  * This path doesn't work against windows,
742                  * because of windows drops the connections
743                  * which haven't reached a session setup yet
744                  *
745                  * The same as the reset on zero vc stuff.
746                  */
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];
756         }
757
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
762         }
763         torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
764
765         /*
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.
769          */
770
771         {
772                 struct netr_ServerPasswordSet pwset;
773                 char *password = generate_random_str(s->join_ctx1, 8);
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;
778
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);
783
784                 torture_assert_ntstatus_ok(torture, status,
785                                            "dcerpc_pipe_connect_b failed");
786
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;
797
798                 E_md4hash(password, new_password.hash);
799
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);
804
805                 status = dcerpc_netr_ServerPasswordSet(net_pipe, torture, &pwset);
806                 torture_assert_ntstatus_ok(torture, status,
807                                            "ServerPasswordSet failed");
808
809                 if (!netlogon_creds_client_check(creds_state,
810                                         &pwset.out.return_authenticator->cred)) {
811                         printf("Credential chaining failed\n");
812                 }
813
814                 cli_credentials_set_password(s->wks_creds1, password,
815                                              CRED_SPECIFIED);
816
817                 talloc_free(net_pipe);
818
819                 /* Just as a test, connect with the new creds */
820
821                 talloc_free(s->wks_creds1->netlogon_creds);
822                 s->wks_creds1->netlogon_creds = NULL;
823
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);
828
829                 torture_assert_ntstatus_ok(torture, status,
830                                            "dcerpc_pipe_connect_b failed");
831
832                 talloc_free(net_pipe);
833         }
834
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");
840         }
841
842         start = timeval_current();
843         end = timeval_add(&start, s->timelimit, 0);
844
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");
848         }
849         torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
850         s->stopped = true;
851         talloc_free(s->conns);
852
853         for (i=0; i < s->nprocs; i++) {
854                 s->total += s->conns[i].total;
855         }
856
857         torture_comment(torture,
858                         "Total ops[%llu] (%u ops/s)\n",
859                         (unsigned long long)s->total,
860                         (unsigned)s->total/s->timelimit);
861
862         torture_leave_domain(torture, s->join_ctx1);
863         torture_leave_domain(torture, s->join_ctx2);
864         return true;
865 }