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