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