s4-smbtorture: remove trailing whitespace from schannel test.
[sfrench/samba-autobuild/.git] / source4 / torture / rpc / schannel.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_netlogon_c.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "auth/credentials/credentials.h"
27 #include "torture/rpc/torture_rpc.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "auth/gensec/schannel.h"
30 #include "../libcli/auth/schannel.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "libcli/security/security.h"
33 #include "system/filesys.h"
34 #include "param/param.h"
35 #include "librpc/rpc/dcerpc_proto.h"
36 #include "auth/gensec/gensec.h"
37 #include "libcli/composite/composite.h"
38 #include "lib/events/events.h"
39
40 #define TEST_MACHINE_NAME "schannel"
41
42 /*
43   try a netlogon SamLogon
44 */
45 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx,
46                           struct cli_credentials *credentials,
47                           struct netlogon_creds_CredentialState *creds)
48 {
49         NTSTATUS status;
50         struct netr_LogonSamLogonEx r;
51         struct netr_NetworkInfo ninfo;
52         union netr_LogonLevel logon;
53         union netr_Validation validation;
54         uint8_t authoritative = 0;
55         uint32_t _flags = 0;
56         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
57         int i;
58         int flags = CLI_CRED_NTLM_AUTH;
59         struct dcerpc_binding_handle *b = p->binding_handle;
60
61         if (lpcfg_client_lanman_auth(tctx->lp_ctx)) {
62                 flags |= CLI_CRED_LANMAN_AUTH;
63         }
64
65         if (lpcfg_client_ntlmv2_auth(tctx->lp_ctx)) {
66                 flags |= CLI_CRED_NTLMv2_AUTH;
67         }
68
69         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
70                                                  &ninfo.identity_info.account_name.string,
71                                                  &ninfo.identity_info.domain_name.string);
72
73         generate_random_buffer(ninfo.challenge,
74                                sizeof(ninfo.challenge));
75         chal = data_blob_const(ninfo.challenge,
76                                sizeof(ninfo.challenge));
77
78         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
79                                                 cli_credentials_get_domain(credentials));
80
81         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
82                                                    &flags,
83                                                    chal,
84                                                    names_blob,
85                                                    &lm_resp, &nt_resp,
86                                                    NULL, NULL);
87         torture_assert_ntstatus_ok(tctx, status,
88                                    "cli_credentials_get_ntlm_response failed");
89
90         ninfo.lm.data = lm_resp.data;
91         ninfo.lm.length = lm_resp.length;
92
93         ninfo.nt.data = nt_resp.data;
94         ninfo.nt.length = nt_resp.length;
95
96         ninfo.identity_info.parameter_control = 0;
97         ninfo.identity_info.logon_id_low = 0;
98         ninfo.identity_info.logon_id_high = 0;
99         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
100
101         logon.network = &ninfo;
102
103         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
104         r.in.computer_name = cli_credentials_get_workstation(credentials);
105         r.in.logon_level = 2;
106         r.in.logon= &logon;
107         r.in.flags = &_flags;
108         r.out.validation = &validation;
109         r.out.authoritative = &authoritative;
110         r.out.flags = &_flags;
111
112         torture_comment(tctx,
113                         "Testing LogonSamLogonEx with name %s\n",
114                         ninfo.identity_info.account_name.string);
115
116         for (i=2;i<3;i++) {
117                 r.in.validation_level = i;
118
119                 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
120                         "LogonSamLogon failed");
121                 torture_assert_ntstatus_ok(tctx, r.out.result, "LogonSamLogon failed");
122         }
123
124         return true;
125 }
126
127 /*
128   do some samr ops using the schannel connection
129  */
130 static bool test_samr_ops(struct torture_context *tctx,
131                           struct dcerpc_binding_handle *b)
132 {
133         struct samr_GetDomPwInfo r;
134         struct samr_PwInfo info;
135         struct samr_Connect connect_r;
136         struct samr_OpenDomain opendom;
137         int i;
138         struct lsa_String name;
139         struct policy_handle handle;
140         struct policy_handle domain_handle;
141
142         name.string = lpcfg_workgroup(tctx->lp_ctx);
143         r.in.domain_name = &name;
144         r.out.info = &info;
145
146         connect_r.in.system_name = 0;
147         connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
148         connect_r.out.connect_handle = &handle;
149
150         printf("Testing Connect and OpenDomain on BUILTIN\n");
151
152         torture_assert_ntstatus_ok(tctx, dcerpc_samr_Connect_r(b, tctx, &connect_r),
153                 "Connect failed");
154         if (!NT_STATUS_IS_OK(connect_r.out.result)) {
155                 if (NT_STATUS_EQUAL(connect_r.out.result, NT_STATUS_ACCESS_DENIED)) {
156                         printf("Connect failed (expected, schannel mapped to anonymous): %s\n",
157                                nt_errstr(connect_r.out.result));
158                 } else {
159                         printf("Connect failed - %s\n", nt_errstr(connect_r.out.result));
160                         return false;
161                 }
162         } else {
163                 opendom.in.connect_handle = &handle;
164                 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
165                 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
166                 opendom.out.domain_handle = &domain_handle;
167
168                 torture_assert_ntstatus_ok(tctx, dcerpc_samr_OpenDomain_r(b, tctx, &opendom),
169                         "OpenDomain failed");
170                 if (!NT_STATUS_IS_OK(opendom.out.result)) {
171                         printf("OpenDomain failed - %s\n", nt_errstr(opendom.out.result));
172                         return false;
173                 }
174         }
175
176         printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
177
178         /* do several ops to test credential chaining */
179         for (i=0;i<5;i++) {
180                 torture_assert_ntstatus_ok(tctx, dcerpc_samr_GetDomPwInfo_r(b, tctx, &r),
181                         "GetDomPwInfo failed");
182                 if (!NT_STATUS_IS_OK(r.out.result)) {
183                         if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED)) {
184                                 printf("GetDomPwInfo op %d failed - %s\n", i, nt_errstr(r.out.result));
185                                 return false;
186                         }
187                 }
188         }
189
190         return true;
191 }
192
193
194 /*
195   do some lsa ops using the schannel connection
196  */
197 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
198 {
199         struct lsa_GetUserName r;
200         bool ret = true;
201         struct lsa_String *account_name_p = NULL;
202         struct lsa_String *authority_name_p = NULL;
203         struct dcerpc_binding_handle *b = p->binding_handle;
204
205         printf("\nTesting GetUserName\n");
206
207         r.in.system_name = "\\";
208         r.in.account_name = &account_name_p;
209         r.in.authority_name = &authority_name_p;
210         r.out.account_name = &account_name_p;
211
212         /* do several ops to test credential chaining and various operations */
213         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
214                 "lsa_GetUserName failed");
215
216         authority_name_p = *r.out.authority_name;
217
218         if (!NT_STATUS_IS_OK(r.out.result)) {
219                 printf("GetUserName failed - %s\n", nt_errstr(r.out.result));
220                 return false;
221         } else {
222                 if (!r.out.account_name) {
223                         return false;
224                 }
225
226                 if (strcmp(account_name_p->string, "ANONYMOUS LOGON") != 0) {
227                         printf("GetUserName returned wrong user: %s, expected %s\n",
228                                account_name_p->string, "ANONYMOUS LOGON");
229                         /* FIXME: gd */
230                         if (!torture_setting_bool(tctx, "samba3", false)) {
231                                 return false;
232                         }
233                 }
234                 if (!authority_name_p || !authority_name_p->string) {
235                         return false;
236                 }
237
238                 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
239                         printf("GetUserName returned wrong user: %s, expected %s\n",
240                                authority_name_p->string, "NT AUTHORITY");
241                         /* FIXME: gd */
242                         if (!torture_setting_bool(tctx, "samba3", false)) {
243                                 return false;
244                         }
245                 }
246         }
247         if (!test_many_LookupSids(p, tctx, NULL)) {
248                 printf("LsaLookupSids3 failed!\n");
249                 return false;
250         }
251
252         return ret;
253 }
254
255
256 /*
257   test a schannel connection with the given flags
258  */
259 static bool test_schannel(struct torture_context *tctx,
260                           uint16_t acct_flags, uint32_t dcerpc_flags,
261                           int i)
262 {
263         struct test_join *join_ctx;
264         NTSTATUS status;
265         const char *binding = torture_setting_string(tctx, "binding", NULL);
266         struct dcerpc_binding *b;
267         struct dcerpc_pipe *p = NULL;
268         struct dcerpc_pipe *p_netlogon = NULL;
269         struct dcerpc_pipe *p_netlogon2 = NULL;
270         struct dcerpc_pipe *p_netlogon3 = NULL;
271         struct dcerpc_pipe *p_samr2 = NULL;
272         struct dcerpc_pipe *p_lsa = NULL;
273         struct netlogon_creds_CredentialState *creds;
274         struct cli_credentials *credentials;
275
276         join_ctx = torture_join_domain(tctx,
277                                        talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
278                                        acct_flags, &credentials);
279         torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
280
281         status = dcerpc_parse_binding(tctx, binding, &b);
282         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
283
284         b->flags &= ~DCERPC_AUTH_OPTIONS;
285         b->flags |= dcerpc_flags;
286
287         status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
288                                        credentials, tctx->ev, tctx->lp_ctx);
289         torture_assert_ntstatus_ok(tctx, status,
290                 "Failed to connect with schannel");
291
292         torture_assert(tctx, test_samr_ops(tctx, p->binding_handle),
293                        "Failed to process schannel secured SAMR ops");
294
295         /* Also test that when we connect to the netlogon pipe, that
296          * the credentials we setup on the first pipe are valid for
297          * the second */
298
299         /* Swap the binding details from SAMR to NETLOGON */
300         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
301         torture_assert_ntstatus_ok(tctx, status, "epm map");
302
303         status = dcerpc_secondary_connection(p, &p_netlogon,
304                                              b);
305         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
306
307         status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon,
308                                   credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
309                                   DCERPC_AUTH_TYPE_SCHANNEL,
310                                   dcerpc_auth_level(p->conn),
311                                   NULL);
312
313         torture_assert_ntstatus_ok(tctx, status, "bind auth");
314
315         status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
316         torture_assert_ntstatus_ok(tctx, status, "schannel creds");
317
318         /* do a couple of logins */
319         torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
320                 "Failed to process schannel secured NETLOGON ops");
321
322         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
323                 "Failed to process schannel secured NETLOGON EX ops");
324
325         /* Swap the binding details from SAMR to LSARPC */
326         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
327         torture_assert_ntstatus_ok(tctx, status, "epm map");
328
329         status = dcerpc_secondary_connection(p, &p_lsa,
330                                              b);
331
332         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
333
334         status = dcerpc_bind_auth(p_lsa, &ndr_table_lsarpc,
335                                   credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
336                                   DCERPC_AUTH_TYPE_SCHANNEL,
337                                   dcerpc_auth_level(p->conn),
338                                   NULL);
339
340         torture_assert_ntstatus_ok(tctx, status, "bind auth");
341
342         torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
343                 "Failed to process schannel secured LSA ops");
344
345         /* Drop the socket, we want to start from scratch */
346         talloc_free(p);
347         p = NULL;
348
349         /* Now see what we are still allowed to do */
350
351         status = dcerpc_parse_binding(tctx, binding, &b);
352         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
353
354         b->flags &= ~DCERPC_AUTH_OPTIONS;
355         b->flags |= dcerpc_flags;
356
357         status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
358                                        credentials, tctx->ev, tctx->lp_ctx);
359         torture_assert_ntstatus_ok(tctx, status,
360                 "Failed to connect with schannel");
361
362         /* do a some SAMR operations.  We have *not* done a new serverauthenticate */
363         torture_assert (tctx, test_samr_ops(tctx, p_samr2->binding_handle),
364                         "Failed to process schannel secured SAMR ops (on fresh connection)");
365
366         /* Swap the binding details from SAMR to NETLOGON */
367         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
368         torture_assert_ntstatus_ok(tctx, status, "epm");
369
370         status = dcerpc_secondary_connection(p_samr2, &p_netlogon2,
371                                              b);
372         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
373
374         /* and now setup an SCHANNEL bind on netlogon */
375         status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
376                                   credentials, lpcfg_gensec_settings(tctx, tctx->lp_ctx),
377                                   DCERPC_AUTH_TYPE_SCHANNEL,
378                                   dcerpc_auth_level(p_samr2->conn),
379                                   NULL);
380
381         torture_assert_ntstatus_ok(tctx, status, "auth failed");
382
383         /* Try the schannel-only SamLogonEx operation */
384         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
385                        "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
386
387
388         /* And the more traditional style, proving that the
389          * credentials chaining state is fully present */
390         torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
391                              "Failed to process schannel secured NETLOGON ops (on fresh connection)");
392
393         /* Drop the socket, we want to start from scratch (again) */
394         talloc_free(p_samr2);
395
396         /* We don't want schannel for this test */
397         b->flags &= ~DCERPC_AUTH_OPTIONS;
398
399         status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
400                                        credentials, tctx->ev, tctx->lp_ctx);
401         torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
402
403         torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
404                         "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
405
406         /* Required because the previous call will mark the current context as having failed */
407         tctx->last_result = TORTURE_OK;
408         tctx->last_reason = NULL;
409
410         torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
411                         "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
412
413         torture_leave_domain(tctx, join_ctx);
414         return true;
415 }
416
417
418
419 /*
420   a schannel test suite
421  */
422 bool torture_rpc_schannel(struct torture_context *torture)
423 {
424         bool ret = true;
425         struct {
426                 uint16_t acct_flags;
427                 uint32_t dcerpc_flags;
428         } tests[] = {
429                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN},
430                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL},
431                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
432                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
433                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN },
434                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL },
435                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
436                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
437         };
438         int i;
439
440         for (i=0;i<ARRAY_SIZE(tests);i++) {
441                 if (!test_schannel(torture,
442                                    tests[i].acct_flags, tests[i].dcerpc_flags,
443                                    i)) {
444                         torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
445                                tests[i].acct_flags, tests[i].dcerpc_flags);
446                         ret = false;
447                 }
448         }
449
450         return ret;
451 }
452
453 /*
454   test two schannel connections
455  */
456 bool torture_rpc_schannel2(struct torture_context *torture)
457 {
458         struct test_join *join_ctx;
459         NTSTATUS status;
460         const char *binding = torture_setting_string(torture, "binding", NULL);
461         struct dcerpc_binding *b;
462         struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
463         struct cli_credentials *credentials1, *credentials2;
464         uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
465
466         join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
467                                        ACB_WSTRUST, &credentials1);
468         torture_assert(torture, join_ctx != NULL,
469                        "Failed to join domain with acct_flags=ACB_WSTRUST");
470
471         credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
472         credentials1->netlogon_creds = NULL;
473         credentials2->netlogon_creds = NULL;
474
475         status = dcerpc_parse_binding(torture, binding, &b);
476         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
477
478         b->flags &= ~DCERPC_AUTH_OPTIONS;
479         b->flags |= dcerpc_flags;
480
481         printf("Opening first connection\n");
482         status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
483                                        credentials1, torture->ev, torture->lp_ctx);
484         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
485
486         torture_comment(torture, "Opening second connection\n");
487         status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
488                                        credentials2, torture->ev, torture->lp_ctx);
489         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
490
491         credentials1->netlogon_creds = NULL;
492         credentials2->netlogon_creds = NULL;
493
494         torture_comment(torture, "Testing logon on pipe1\n");
495         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
496                 return false;
497
498         torture_comment(torture, "Testing logon on pipe2\n");
499         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
500                 return false;
501
502         torture_comment(torture, "Again on pipe1\n");
503         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
504                 return false;
505
506         torture_comment(torture, "Again on pipe2\n");
507         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
508                 return false;
509
510         torture_leave_domain(torture, join_ctx);
511         return true;
512 }
513
514 struct torture_schannel_bench;
515
516 struct torture_schannel_bench_conn {
517         struct torture_schannel_bench *s;
518         int index;
519         struct cli_credentials *wks_creds;
520         struct dcerpc_pipe *pipe;
521         struct netr_LogonSamLogonEx r;
522         struct netr_NetworkInfo ninfo;
523         TALLOC_CTX *tmp;
524         uint64_t total;
525         uint32_t count;
526 };
527
528 struct torture_schannel_bench {
529         struct torture_context *tctx;
530         bool progress;
531         int timelimit;
532         int nprocs;
533         int nconns;
534         struct torture_schannel_bench_conn *conns;
535         struct test_join *join_ctx1;
536         struct cli_credentials *wks_creds1;
537         struct test_join *join_ctx2;
538         struct cli_credentials *wks_creds2;
539         struct cli_credentials *user1_creds;
540         struct cli_credentials *user2_creds;
541         struct dcerpc_binding *b;
542         NTSTATUS error;
543         uint64_t total;
544         uint32_t count;
545         bool stopped;
546 };
547
548 static void torture_schannel_bench_connected(struct composite_context *c)
549 {
550         struct torture_schannel_bench_conn *conn =
551                 (struct torture_schannel_bench_conn *)c->async.private_data;
552         struct torture_schannel_bench *s = talloc_get_type(conn->s,
553                                            struct torture_schannel_bench);
554
555         s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
556         torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
557         if (NT_STATUS_IS_OK(s->error)) {
558                 s->nconns++;
559         }
560 }
561
562 static void torture_schannel_bench_recv(struct tevent_req *subreq);
563
564 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
565 {
566         struct torture_schannel_bench *s = conn->s;
567         NTSTATUS status;
568         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
569         int flags = CLI_CRED_NTLM_AUTH;
570         struct tevent_req *subreq;
571         struct cli_credentials *user_creds;
572
573         if (conn->total % 2) {
574                 user_creds = s->user1_creds;
575         } else {
576                 user_creds = s->user2_creds;
577         }
578
579         if (lpcfg_client_lanman_auth(s->tctx->lp_ctx)) {
580                 flags |= CLI_CRED_LANMAN_AUTH;
581         }
582
583         if (lpcfg_client_ntlmv2_auth(s->tctx->lp_ctx)) {
584                 flags |= CLI_CRED_NTLMv2_AUTH;
585         }
586
587         talloc_free(conn->tmp);
588         conn->tmp = talloc_new(s);
589         ZERO_STRUCT(conn->ninfo);
590         ZERO_STRUCT(conn->r);
591
592         cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
593                                                  &conn->ninfo.identity_info.account_name.string,
594                                                  &conn->ninfo.identity_info.domain_name.string);
595
596         generate_random_buffer(conn->ninfo.challenge,
597                                sizeof(conn->ninfo.challenge));
598         chal = data_blob_const(conn->ninfo.challenge,
599                                sizeof(conn->ninfo.challenge));
600
601         names_blob = NTLMv2_generate_names_blob(conn->tmp,
602                                                 cli_credentials_get_workstation(conn->wks_creds),
603                                                 cli_credentials_get_domain(conn->wks_creds));
604
605         status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
606                                                    &flags,
607                                                    chal,
608                                                    names_blob,
609                                                    &lm_resp, &nt_resp,
610                                                    NULL, NULL);
611         torture_assert_ntstatus_ok(s->tctx, status,
612                                    "cli_credentials_get_ntlm_response failed");
613
614         conn->ninfo.lm.data = lm_resp.data;
615         conn->ninfo.lm.length = lm_resp.length;
616
617         conn->ninfo.nt.data = nt_resp.data;
618         conn->ninfo.nt.length = nt_resp.length;
619
620         conn->ninfo.identity_info.parameter_control = 0;
621         conn->ninfo.identity_info.logon_id_low = 0;
622         conn->ninfo.identity_info.logon_id_high = 0;
623         conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
624
625         conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
626         conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
627         conn->r.in.logon_level = 2;
628         conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
629         conn->r.in.logon->network = &conn->ninfo;
630         conn->r.in.flags = talloc(conn->tmp, uint32_t);
631         conn->r.in.validation_level = 2;
632         conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
633         conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
634         conn->r.out.flags = conn->r.in.flags;
635
636         subreq = dcerpc_netr_LogonSamLogonEx_r_send(s, s->tctx->ev,
637                                                     conn->pipe->binding_handle,
638                                                     &conn->r);
639         torture_assert(s->tctx, subreq, "Failed to setup LogonSamLogonEx request");
640
641         tevent_req_set_callback(subreq, torture_schannel_bench_recv, conn);
642
643         return true;
644 }
645
646 static void torture_schannel_bench_recv(struct tevent_req *subreq)
647 {
648         bool ret;
649         struct torture_schannel_bench_conn *conn =
650                 (struct torture_schannel_bench_conn *)tevent_req_callback_data_void(subreq);
651         struct torture_schannel_bench *s = talloc_get_type(conn->s,
652                                            struct torture_schannel_bench);
653
654         s->error = dcerpc_netr_LogonSamLogonEx_r_recv(subreq, subreq);
655         TALLOC_FREE(subreq);
656         if (!NT_STATUS_IS_OK(s->error)) {
657                 return;
658         }
659
660         conn->total++;
661         conn->count++;
662
663         if (s->stopped) {
664                 return;
665         }
666
667         ret = torture_schannel_bench_start(conn);
668         if (!ret) {
669                 s->error = NT_STATUS_INTERNAL_ERROR;
670         }
671 }
672
673 /*
674   test multiple schannel connection in parallel
675  */
676 bool torture_rpc_schannel_bench1(struct torture_context *torture)
677 {
678         bool ret = true;
679         NTSTATUS status;
680         const char *binding = torture_setting_string(torture, "binding", NULL);
681         struct torture_schannel_bench *s;
682         struct timeval start;
683         struct timeval end;
684         int i;
685         const char *tmp;
686
687         s = talloc_zero(torture, struct torture_schannel_bench);
688         s->tctx = torture;
689         s->progress = torture_setting_bool(torture, "progress", true);
690         s->timelimit = torture_setting_int(torture, "timelimit", 10);
691         s->nprocs = torture_setting_int(torture, "nprocs", 4);
692         s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
693
694         s->user1_creds = (struct cli_credentials *)talloc_memdup(s,
695                                                                  cmdline_credentials,
696                                                                  sizeof(*s->user1_creds));
697         tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
698         if (tmp) {
699                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
700         }
701         s->user2_creds = (struct cli_credentials *)talloc_memdup(s,
702                                                                  cmdline_credentials,
703                                                                  sizeof(*s->user1_creds));
704         tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
705         if (tmp) {
706                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
707         }
708
709         s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
710                                            ACB_WSTRUST, &s->wks_creds1);
711         torture_assert(torture, s->join_ctx1 != NULL,
712                        "Failed to join domain with acct_flags=ACB_WSTRUST");
713         s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
714                                            ACB_WSTRUST, &s->wks_creds2);
715         torture_assert(torture, s->join_ctx2 != NULL,
716                        "Failed to join domain with acct_flags=ACB_WSTRUST");
717
718         cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
719         cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
720
721         for (i=0; i < s->nprocs; i++) {
722                 s->conns[i].s = s;
723                 s->conns[i].index = i;
724                 s->conns[i].wks_creds = (struct cli_credentials *)talloc_memdup(
725                         s->conns, s->wks_creds1,sizeof(*s->wks_creds1));
726                 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
727                         memcpy(s->conns[i].wks_creds, s->wks_creds2,
728                                talloc_get_size(s->conns[i].wks_creds));
729                 }
730                 s->conns[i].wks_creds->netlogon_creds = NULL;
731         }
732
733         status = dcerpc_parse_binding(s, binding, &s->b);
734         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
735         s->b->flags &= ~DCERPC_AUTH_OPTIONS;
736         s->b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
737
738         torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
739         for (i=0; i < s->nprocs; i++) {
740 #if 1
741                 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
742                                                  &ndr_table_netlogon,
743                                                  s->conns[i].wks_creds,
744                                                  torture->ev, torture->lp_ctx);
745                 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
746 #else
747                 /*
748                  * This path doesn't work against windows,
749                  * because of windows drops the connections
750                  * which haven't reached a session setup yet
751                  *
752                  * The same as the reset on zero vc stuff.
753                  */
754                 struct composite_context *c;
755                 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
756                                                &ndr_table_netlogon,
757                                                s->conns[i].wks_creds,
758                                                torture->ev,
759                                                torture->lp_ctx);
760                 torture_assert(torture, c != NULL, "Failed to setup connect");
761                 c->async.fn = torture_schannel_bench_connected;
762                 c->async.private_data = &s->conns[i];
763         }
764
765         while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
766                 int ev_ret = tevent_loop_once(torture->ev);
767                 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
768 #endif
769         }
770         torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
771
772         /*
773          * Change the workstation password after establishing the netlogon
774          * schannel connections to prove that existing connections are not
775          * affected by a wks pwchange.
776          */
777
778         {
779                 struct netr_ServerPasswordSet pwset;
780                 char *password = generate_random_password(s->join_ctx1, 8, 255);
781                 struct netlogon_creds_CredentialState *creds_state;
782                 struct dcerpc_pipe *net_pipe;
783                 struct netr_Authenticator credential, return_authenticator;
784                 struct samr_Password new_password;
785
786                 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
787                                                &ndr_table_netlogon,
788                                                s->wks_creds1,
789                                                torture->ev, torture->lp_ctx);
790
791                 torture_assert_ntstatus_ok(torture, status,
792                                            "dcerpc_pipe_connect_b failed");
793
794                 pwset.in.server_name = talloc_asprintf(
795                         net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
796                 pwset.in.computer_name =
797                         cli_credentials_get_workstation(s->wks_creds1);
798                 pwset.in.account_name = talloc_asprintf(
799                         net_pipe, "%s$", pwset.in.computer_name);
800                 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
801                 pwset.in.credential = &credential;
802                 pwset.in.new_password = &new_password;
803                 pwset.out.return_authenticator = &return_authenticator;
804
805                 E_md4hash(password, new_password.hash);
806
807                 creds_state = cli_credentials_get_netlogon_creds(
808                         s->wks_creds1);
809                 netlogon_creds_des_encrypt(creds_state, &new_password);
810                 netlogon_creds_client_authenticator(creds_state, &credential);
811
812                 torture_assert_ntstatus_ok(torture, dcerpc_netr_ServerPasswordSet_r(net_pipe->binding_handle, torture, &pwset),
813                         "ServerPasswordSet failed");
814                 torture_assert_ntstatus_ok(torture, pwset.out.result,
815                                            "ServerPasswordSet failed");
816
817                 if (!netlogon_creds_client_check(creds_state,
818                                         &pwset.out.return_authenticator->cred)) {
819                         printf("Credential chaining failed\n");
820                 }
821
822                 cli_credentials_set_password(s->wks_creds1, password,
823                                              CRED_SPECIFIED);
824
825                 talloc_free(net_pipe);
826
827                 /* Just as a test, connect with the new creds */
828
829                 talloc_free(s->wks_creds1->netlogon_creds);
830                 s->wks_creds1->netlogon_creds = NULL;
831
832                 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
833                                                &ndr_table_netlogon,
834                                                s->wks_creds1,
835                                                torture->ev, torture->lp_ctx);
836
837                 torture_assert_ntstatus_ok(torture, status,
838                                            "dcerpc_pipe_connect_b failed");
839
840                 talloc_free(net_pipe);
841         }
842
843         torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
844                         s->nprocs, s->timelimit);
845         for (i=0; i < s->nprocs; i++) {
846                 ret = torture_schannel_bench_start(&s->conns[i]);
847                 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
848         }
849
850         start = timeval_current();
851         end = timeval_add(&start, s->timelimit, 0);
852
853         while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
854                 int ev_ret = tevent_loop_once(torture->ev);
855                 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
856         }
857         torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
858         s->stopped = true;
859         talloc_free(s->conns);
860
861         for (i=0; i < s->nprocs; i++) {
862                 s->total += s->conns[i].total;
863         }
864
865         torture_comment(torture,
866                         "Total ops[%llu] (%u ops/s)\n",
867                         (unsigned long long)s->total,
868                         (unsigned)s->total/s->timelimit);
869
870         torture_leave_domain(torture, s->join_ctx1);
871         torture_leave_domain(torture, s->join_ctx2);
872         return true;
873 }