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