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