s4:torture/netlogon: add/use test_SetupCredentialsPipe() helper function
[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/torture_rpc.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "../libcli/auth/schannel.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 "libcli/composite/composite.h"
36 #include "lib/events/events.h"
37
38 #define TEST_MACHINE_NAME "schannel"
39
40 /*
41   try a netlogon SamLogon
42 */
43 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx,
44                           struct cli_credentials *credentials,
45                           struct netlogon_creds_CredentialState *creds)
46 {
47         NTSTATUS status;
48         struct netr_LogonSamLogonEx r;
49         struct netr_NetworkInfo ninfo;
50         union netr_LogonLevel logon;
51         union netr_Validation validation;
52         uint8_t authoritative = 0;
53         uint32_t _flags = 0;
54         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
55         int i;
56         int flags = CLI_CRED_NTLM_AUTH;
57         struct dcerpc_binding_handle *b = p->binding_handle;
58
59         struct netr_UserSessionKey key;
60         struct netr_LMSessionKey LMSessKey;
61         uint32_t validation_levels[] = { 2, 3 };
62         struct netr_SamBaseInfo *base = NULL;
63         const char *crypto_alg = "";
64         bool can_do_validation_6 = true;
65
66         if (lpcfg_client_lanman_auth(tctx->lp_ctx)) {
67                 flags |= CLI_CRED_LANMAN_AUTH;
68         }
69
70         if (lpcfg_client_ntlmv2_auth(tctx->lp_ctx)) {
71                 flags |= CLI_CRED_NTLMv2_AUTH;
72         }
73
74         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
75                                                  &ninfo.identity_info.account_name.string,
76                                                  &ninfo.identity_info.domain_name.string);
77
78         generate_random_buffer(ninfo.challenge,
79                                sizeof(ninfo.challenge));
80         chal = data_blob_const(ninfo.challenge,
81                                sizeof(ninfo.challenge));
82
83         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
84                                                 cli_credentials_get_domain(credentials));
85
86         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
87                                                    &flags,
88                                                    chal,
89                                                    names_blob,
90                                                    &lm_resp, &nt_resp,
91                                                    NULL, NULL);
92         torture_assert_ntstatus_ok(tctx, status,
93                                    "cli_credentials_get_ntlm_response failed");
94
95         ninfo.lm.data = lm_resp.data;
96         ninfo.lm.length = lm_resp.length;
97
98         ninfo.nt.data = nt_resp.data;
99         ninfo.nt.length = nt_resp.length;
100
101         ninfo.identity_info.parameter_control = 0;
102         ninfo.identity_info.logon_id_low = 0;
103         ninfo.identity_info.logon_id_high = 0;
104         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
105
106         logon.network = &ninfo;
107
108         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
109         r.in.computer_name = cli_credentials_get_workstation(credentials);
110         r.in.logon_level = NetlogonNetworkInformation;
111         r.in.logon= &logon;
112         r.in.flags = &_flags;
113         r.out.validation = &validation;
114         r.out.authoritative = &authoritative;
115         r.out.flags = &_flags;
116
117         /*
118         - retrieve level6
119         - save usrsession and lmsession key
120         - retrieve level 2
121         - calculate, compare
122         - retrieve level 3
123         - calculate, compare
124         */
125
126         if (creds) {
127                 if (creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
128                         crypto_alg = "AES";
129                 } else if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
130                         crypto_alg = "ARCFOUR";
131                 }
132         }
133
134         r.in.validation_level = 6;
135
136         torture_comment(tctx,
137                         "Testing LogonSamLogonEx with name %s using %s and validation_level: %d\n",
138                         ninfo.identity_info.account_name.string, crypto_alg,
139                         r.in.validation_level);
140
141         torture_assert_ntstatus_ok(tctx,
142                 dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
143                 "LogonSamLogonEx failed");
144         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_INVALID_INFO_CLASS)) {
145                 can_do_validation_6 = false;
146         } else {
147                 torture_assert_ntstatus_ok(tctx, r.out.result,
148                         "LogonSamLogonEx failed");
149
150                 key = r.out.validation->sam6->base.key;
151                 LMSessKey = r.out.validation->sam6->base.LMSessKey;
152
153                 DEBUG(1,("unencrypted session keys from validation_level 6:\n"));
154                 dump_data(1, r.out.validation->sam6->base.key.key, 16);
155                 dump_data(1, r.out.validation->sam6->base.LMSessKey.key, 8);
156         }
157
158         for (i=0; i < ARRAY_SIZE(validation_levels); i++) {
159
160                 r.in.validation_level = validation_levels[i];
161
162                 torture_comment(tctx,
163                         "Testing LogonSamLogonEx with name %s using %s and validation_level: %d\n",
164                         ninfo.identity_info.account_name.string, crypto_alg,
165                         r.in.validation_level);
166
167                 torture_assert_ntstatus_ok(tctx,
168                         dcerpc_netr_LogonSamLogonEx_r(b, tctx, &r),
169                         "LogonSamLogonEx failed");
170                 torture_assert_ntstatus_ok(tctx, r.out.result,
171                         "LogonSamLogonEx failed");
172
173                 if (creds == NULL) {
174                         /* when this test is called without creds no point in
175                          * testing the session keys */
176                         continue;
177                 }
178
179                 switch (validation_levels[i]) {
180                 case 2:
181                         base = &r.out.validation->sam2->base;
182                         break;
183                 case 3:
184                         base = &r.out.validation->sam3->base;
185                         break;
186                 default:
187                         break;
188                 }
189
190                 DEBUG(1,("encrypted keys validation_level %d:\n",
191                         validation_levels[i]));
192                 dump_data(1, base->key.key, 16);
193                 dump_data(1, base->LMSessKey.key, 8);
194
195                 if (creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
196                         netlogon_creds_aes_decrypt(creds, base->key.key, 16);
197                         netlogon_creds_aes_decrypt(creds, base->LMSessKey.key, 8);
198                 } else if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
199                         netlogon_creds_arcfour_crypt(creds, base->key.key, 16);
200                         netlogon_creds_arcfour_crypt(creds, base->LMSessKey.key, 8);
201                 }
202
203                 DEBUG(1,("decryped keys validation_level %d\n",
204                         validation_levels[i]));
205
206                 dump_data(1, base->key.key, 16);
207                 dump_data(1, base->LMSessKey.key, 8);
208
209                 if (!can_do_validation_6) {
210                         /* we cant compare against unencrypted keys */
211                         continue;
212                 }
213
214                 torture_assert_mem_equal(tctx,
215                                          base->key.key,
216                                          key.key,
217                                          16,
218                                          "unexpected user session key\n");
219                 torture_assert_mem_equal(tctx,
220                                          base->LMSessKey.key,
221                                          LMSessKey.key,
222                                          8,
223                                          "unexpected LM session key\n");
224         }
225
226         return true;
227 }
228
229 /*
230   do some samr ops using the schannel connection
231  */
232 static bool test_samr_ops(struct torture_context *tctx,
233                           struct dcerpc_binding_handle *b)
234 {
235         struct samr_GetDomPwInfo r;
236         struct samr_PwInfo info;
237         struct samr_Connect connect_r;
238         struct samr_OpenDomain opendom;
239         int i;
240         struct lsa_String name;
241         struct policy_handle handle;
242         struct policy_handle domain_handle;
243
244         name.string = lpcfg_workgroup(tctx->lp_ctx);
245         r.in.domain_name = &name;
246         r.out.info = &info;
247
248         connect_r.in.system_name = 0;
249         connect_r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
250         connect_r.out.connect_handle = &handle;
251
252         torture_comment(tctx, "Testing Connect and OpenDomain on BUILTIN\n");
253
254         torture_assert_ntstatus_ok(tctx, dcerpc_samr_Connect_r(b, tctx, &connect_r),
255                 "Connect failed");
256         if (!NT_STATUS_IS_OK(connect_r.out.result)) {
257                 if (NT_STATUS_EQUAL(connect_r.out.result, NT_STATUS_ACCESS_DENIED)) {
258                         torture_comment(tctx, "Connect failed (expected, schannel mapped to anonymous): %s\n",
259                                nt_errstr(connect_r.out.result));
260                 } else {
261                         torture_comment(tctx, "Connect failed - %s\n", nt_errstr(connect_r.out.result));
262                         return false;
263                 }
264         } else {
265                 opendom.in.connect_handle = &handle;
266                 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
267                 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
268                 opendom.out.domain_handle = &domain_handle;
269
270                 torture_assert_ntstatus_ok(tctx, dcerpc_samr_OpenDomain_r(b, tctx, &opendom),
271                         "OpenDomain failed");
272                 if (!NT_STATUS_IS_OK(opendom.out.result)) {
273                         torture_comment(tctx, "OpenDomain failed - %s\n", nt_errstr(opendom.out.result));
274                         return false;
275                 }
276         }
277
278         torture_comment(tctx, "Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
279
280         /* do several ops to test credential chaining */
281         for (i=0;i<5;i++) {
282                 torture_assert_ntstatus_ok(tctx, dcerpc_samr_GetDomPwInfo_r(b, tctx, &r),
283                         "GetDomPwInfo failed");
284                 if (!NT_STATUS_IS_OK(r.out.result)) {
285                         if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED)) {
286                                 torture_comment(tctx, "GetDomPwInfo op %d failed - %s\n", i, nt_errstr(r.out.result));
287                                 return false;
288                         }
289                 }
290         }
291
292         return true;
293 }
294
295
296 /*
297   do some lsa ops using the schannel connection
298  */
299 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
300 {
301         struct lsa_GetUserName r;
302         bool ret = true;
303         struct lsa_String *account_name_p = NULL;
304         struct lsa_String *authority_name_p = NULL;
305         struct dcerpc_binding_handle *b = p->binding_handle;
306
307         torture_comment(tctx, "\nTesting GetUserName\n");
308
309         r.in.system_name = "\\";
310         r.in.account_name = &account_name_p;
311         r.in.authority_name = &authority_name_p;
312         r.out.account_name = &account_name_p;
313
314         /* do several ops to test credential chaining and various operations */
315         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
316                 "lsa_GetUserName failed");
317
318         authority_name_p = *r.out.authority_name;
319
320         if (!NT_STATUS_IS_OK(r.out.result)) {
321                 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(r.out.result));
322                 return false;
323         } else {
324                 if (!r.out.account_name) {
325                         return false;
326                 }
327
328                 if (strcmp(account_name_p->string, "ANONYMOUS LOGON") != 0) {
329                         torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
330                                account_name_p->string, "ANONYMOUS LOGON");
331                         /* FIXME: gd */
332                         if (!torture_setting_bool(tctx, "samba3", false)) {
333                                 return false;
334                         }
335                 }
336                 if (!authority_name_p || !authority_name_p->string) {
337                         return false;
338                 }
339
340                 if (strcmp(authority_name_p->string, "NT AUTHORITY") != 0) {
341                         torture_comment(tctx, "GetUserName returned wrong user: %s, expected %s\n",
342                                authority_name_p->string, "NT AUTHORITY");
343                         /* FIXME: gd */
344                         if (!torture_setting_bool(tctx, "samba3", false)) {
345                                 return false;
346                         }
347                 }
348         }
349
350         return ret;
351 }
352
353
354 /*
355   test a schannel connection with the given flags
356  */
357 static bool test_schannel(struct torture_context *tctx,
358                           uint16_t acct_flags, uint32_t dcerpc_flags,
359                           int i)
360 {
361         struct test_join *join_ctx;
362         NTSTATUS status;
363         const char *binding = torture_setting_string(tctx, "binding", NULL);
364         struct dcerpc_binding *b;
365         struct dcerpc_pipe *p = NULL;
366         struct dcerpc_pipe *p_netlogon = NULL;
367         struct dcerpc_pipe *p_netlogon2 = NULL;
368         struct dcerpc_pipe *p_netlogon3 = NULL;
369         struct dcerpc_pipe *p_samr2 = NULL;
370         struct dcerpc_pipe *p_lsa = NULL;
371         struct netlogon_creds_CredentialState *creds;
372         struct cli_credentials *credentials;
373         enum dcerpc_transport_t transport;
374
375         join_ctx = torture_join_domain(tctx,
376                                        talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i),
377                                        acct_flags, &credentials);
378         torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
379
380         status = dcerpc_parse_binding(tctx, binding, &b);
381         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
382
383         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
384         torture_assert_ntstatus_ok(tctx, status, "set flags");
385
386         status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
387                                        credentials, tctx->ev, tctx->lp_ctx);
388         torture_assert_ntstatus_ok(tctx, status,
389                 "Failed to connect to samr with schannel");
390
391         torture_assert(tctx, test_samr_ops(tctx, p->binding_handle),
392                        "Failed to process schannel secured SAMR ops");
393
394         /* Also test that when we connect to the netlogon pipe, that
395          * the credentials we setup on the first pipe are valid for
396          * the second */
397
398         /* Swap the binding details from SAMR to NETLOGON */
399         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
400         torture_assert_ntstatus_ok(tctx, status, "epm map");
401
402         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
403         torture_assert_ntstatus_ok(tctx, status, "set flags");
404
405         status = dcerpc_secondary_auth_connection(p, b, &ndr_table_netlogon,
406                                                   credentials, tctx->lp_ctx,
407                                                   tctx, &p_netlogon);
408         torture_assert_ntstatus_ok(tctx, status, "Failed to create secondary connection");
409
410         creds = cli_credentials_get_netlogon_creds(credentials);
411         torture_assert(tctx, (creds != NULL), "schannel creds");
412
413         /* checks the capabilities */
414         torture_assert(tctx, test_netlogon_capabilities(p_netlogon, tctx, credentials, creds),
415                        "Failed to process schannel secured capability ops (on fresh connection)");
416
417         /* do a couple of logins */
418         torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
419                 "Failed to process schannel secured NETLOGON ops");
420
421         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
422                 "Failed to process schannel secured NETLOGON EX ops");
423
424         /* we *MUST* use ncacn_np for openpolicy etc. */
425         transport = dcerpc_binding_get_transport(b);
426         status = dcerpc_binding_set_transport(b, NCACN_NP);
427         torture_assert_ntstatus_ok(tctx, status, "set transport");
428
429         /* Swap the binding details from SAMR to LSARPC */
430         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx);
431         torture_assert_ntstatus_ok(tctx, status, "epm map");
432
433         torture_assert_ntstatus_ok(tctx,
434                 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
435                                       credentials, tctx->ev, tctx->lp_ctx),
436                 "failed to connect lsarpc with schannel");
437
438         torture_assert(tctx, test_lsa_ops(tctx, p_lsa),
439                 "Failed to process schannel secured LSA ops");
440
441         talloc_free(p_lsa);
442         p_lsa = NULL;
443
444         /* we *MUST* use ncacn_ip_tcp for lookupsids3/lookupnames4 */
445         status = dcerpc_binding_set_transport(b, NCACN_IP_TCP);
446         torture_assert_ntstatus_ok(tctx, status, "set transport");
447
448         torture_assert_ntstatus_ok(tctx,
449                 dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, tctx->ev, tctx->lp_ctx),
450                 "failed to call epm map");
451
452         torture_assert_ntstatus_ok(tctx,
453                 dcerpc_pipe_connect_b(tctx, &p_lsa, b, &ndr_table_lsarpc,
454                                       credentials, tctx->ev, tctx->lp_ctx),
455                 "failed to connect lsarpc with schannel");
456
457         torture_assert(tctx,
458                 test_many_LookupSids(p_lsa, tctx, NULL),
459                 "LsaLookupSids3 failed!\n");
460
461         status = dcerpc_binding_set_transport(b, transport);
462         torture_assert_ntstatus_ok(tctx, status, "set transport");
463
464
465         /* Drop the socket, we want to start from scratch */
466         talloc_free(p);
467         p = NULL;
468
469         /* Now see what we are still allowed to do */
470
471         status = dcerpc_parse_binding(tctx, binding, &b);
472         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
473
474         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
475         torture_assert_ntstatus_ok(tctx, status, "set flags");
476
477         status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
478                                        credentials, tctx->ev, tctx->lp_ctx);
479         torture_assert_ntstatus_ok(tctx, status,
480                 "Failed to connect with schannel");
481
482         /* do a some SAMR operations.  We have *not* done a new serverauthenticate */
483         torture_assert (tctx, test_samr_ops(tctx, p_samr2->binding_handle),
484                         "Failed to process schannel secured SAMR ops (on fresh connection)");
485
486         /* Swap the binding details from SAMR to NETLOGON */
487         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, tctx->ev, tctx->lp_ctx);
488         torture_assert_ntstatus_ok(tctx, status, "epm");
489
490         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
491         torture_assert_ntstatus_ok(tctx, status, "set flags");
492
493         status = dcerpc_secondary_auth_connection(p_samr2, b, &ndr_table_netlogon,
494                                                   credentials, tctx->lp_ctx,
495                                                   tctx, &p_netlogon2);
496         torture_assert_ntstatus_ok(tctx, status, "Failed to create secondary connection");
497
498         /* checks the capabilities */
499         torture_assert(tctx, test_netlogon_capabilities(p_netlogon2, tctx, credentials, creds),
500                        "Failed to process schannel secured capability ops (on fresh connection)");
501
502         /* Try the schannel-only SamLogonEx operation */
503         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds),
504                        "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
505
506
507         /* And the more traditional style, proving that the
508          * credentials chaining state is fully present */
509         torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
510                              "Failed to process schannel secured NETLOGON ops (on fresh connection)");
511
512         /* Drop the socket, we want to start from scratch (again) */
513         talloc_free(p_samr2);
514
515         /* We don't want schannel for this test */
516         status = dcerpc_binding_set_flags(b, 0, DCERPC_AUTH_OPTIONS);
517         torture_assert_ntstatus_ok(tctx, status, "set flags");
518
519         status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
520                                        credentials, tctx->ev, tctx->lp_ctx);
521         torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
522
523         torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
524                         "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
525
526         /* Required because the previous call will mark the current context as having failed */
527         tctx->last_result = TORTURE_OK;
528         tctx->last_reason = NULL;
529
530         torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
531                         "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
532
533         torture_leave_domain(tctx, join_ctx);
534         return true;
535 }
536
537 /*
538  * Purpose of this test is to demonstrate that a netlogon server carefully deals
539  * with anonymous attempts to set passwords, in particular when the server
540  * enforces the use of schannel. This test makes most sense to be run in an
541  * environment where the netlogon server enforces use of schannel.
542  */
543
544 static bool test_schannel_anonymous_setPassword(struct torture_context *tctx,
545                                                 uint32_t dcerpc_flags,
546                                                 bool use2)
547 {
548         NTSTATUS status, result;
549         const char *binding = torture_setting_string(tctx, "binding", NULL);
550         struct dcerpc_binding *b;
551         struct dcerpc_pipe *p = NULL;
552         struct cli_credentials *credentials;
553         bool ok = true;
554
555         credentials = cli_credentials_init(NULL);
556         torture_assert(tctx, credentials != NULL, "Bad credentials");
557         cli_credentials_set_anonymous(credentials);
558
559         status = dcerpc_parse_binding(tctx, binding, &b);
560         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
561
562         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
563         torture_assert_ntstatus_ok(tctx, status, "set flags");
564
565         status = dcerpc_pipe_connect_b(tctx,
566                                        &p,
567                                        b,
568                                        &ndr_table_netlogon,
569                                        credentials,
570                                        tctx->ev,
571                                        tctx->lp_ctx);
572         torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
573
574         if (use2) {
575                 struct netr_ServerPasswordSet2 r = {};
576                 struct netr_Authenticator credential = {};
577                 struct netr_Authenticator return_authenticator = {};
578                 struct netr_CryptPassword new_password = {};
579
580                 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
581                 r.in.account_name = talloc_asprintf(tctx, "%s$", TEST_MACHINE_NAME);
582                 r.in.secure_channel_type = 0;
583                 r.in.computer_name = TEST_MACHINE_NAME;
584                 r.in.credential = &credential;
585                 r.in.new_password = &new_password;
586                 r.out.return_authenticator = &return_authenticator;
587
588                 status = dcerpc_netr_ServerPasswordSet2_r(p->binding_handle, tctx, &r);
589                 result = r.out.result;
590         } else {
591                 struct netr_ServerPasswordSet r = {};
592                 struct netr_Authenticator credential = {};
593                 struct netr_Authenticator return_authenticator = {};
594                 struct samr_Password new_password = {};
595
596                 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
597                 r.in.account_name = talloc_asprintf(tctx, "%s$", TEST_MACHINE_NAME);
598                 r.in.secure_channel_type = 0;
599                 r.in.computer_name = TEST_MACHINE_NAME;
600                 r.in.credential = &credential;
601                 r.in.new_password = &new_password;
602                 r.out.return_authenticator = &return_authenticator;
603
604                 status = dcerpc_netr_ServerPasswordSet_r(p->binding_handle, tctx, &r);
605                 result = r.out.result;
606         }
607
608         torture_assert_ntstatus_ok(tctx, status, "ServerPasswordSet failed");
609
610         if (NT_STATUS_IS_OK(result)) {
611                 torture_fail(tctx, "unexpectedly received NT_STATUS_OK");
612         }
613
614         return ok;
615 }
616
617
618 /*
619   a schannel test suite
620  */
621 bool torture_rpc_schannel(struct torture_context *torture)
622 {
623         bool ret = true;
624         struct {
625                 uint16_t acct_flags;
626                 uint32_t dcerpc_flags;
627         } tests[] = {
628                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
629                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
630                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
631                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
632                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AES},
633                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AES },
634                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO},
635                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AUTO},
636                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
637                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
638                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AES },
639                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_AES }
640         };
641         int i;
642
643         for (i=0;i<ARRAY_SIZE(tests);i++) {
644                 torture_comment(torture, "Testing with acct_flags=0x%x dcerpc_flags=0x%x \n",
645                        tests[i].acct_flags, tests[i].dcerpc_flags);
646
647                 if (!test_schannel(torture,
648                                    tests[i].acct_flags, tests[i].dcerpc_flags,
649                                    i)) {
650                         torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
651                                tests[i].acct_flags, tests[i].dcerpc_flags);
652                         ret = false;
653                 }
654         }
655
656         return ret;
657 }
658
659 bool torture_rpc_schannel_anon_setpw(struct torture_context *torture)
660 {
661         bool ret = true;
662         bool ok;
663         uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_AUTO;
664
665         ok = test_schannel_anonymous_setPassword(torture,
666                                                  dcerpc_flags,
667                                                  true);
668         if (!ok) {
669                 torture_comment(torture,
670                                 "Failed with dcerpc_flags=0x%x\n",
671                                 dcerpc_flags);
672                 ret = false;
673         }
674
675         ok = test_schannel_anonymous_setPassword(torture,
676                                                  dcerpc_flags,
677                                                  false);
678         if (!ok) {
679                 torture_comment(torture,
680                                 "Failed with dcerpc_flags=0x%x\n",
681                                 dcerpc_flags);
682                 ret = false;
683         }
684
685         return ret;
686 }
687
688 /*
689   test two schannel connections
690  */
691 bool torture_rpc_schannel2(struct torture_context *torture)
692 {
693         struct test_join *join_ctx;
694         NTSTATUS status;
695         const char *binding = torture_setting_string(torture, "binding", NULL);
696         struct dcerpc_binding *b;
697         struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
698         struct cli_credentials *credentials1, *credentials2;
699         uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
700
701         join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME),
702                                        ACB_WSTRUST, &credentials1);
703         torture_assert(torture, join_ctx != NULL,
704                        "Failed to join domain with acct_flags=ACB_WSTRUST");
705
706         credentials2 = cli_credentials_shallow_copy(torture, credentials1);
707         cli_credentials_set_netlogon_creds(credentials1, NULL);
708         cli_credentials_set_netlogon_creds(credentials2, NULL);
709
710         status = dcerpc_parse_binding(torture, binding, &b);
711         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
712
713         status = dcerpc_binding_set_flags(b, dcerpc_flags, DCERPC_AUTH_OPTIONS);
714         torture_assert_ntstatus_ok(torture, status, "set flags");
715
716         torture_comment(torture, "Opening first connection\n");
717         status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
718                                        credentials1, torture->ev, torture->lp_ctx);
719         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
720
721         torture_comment(torture, "Opening second connection\n");
722         status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
723                                        credentials2, torture->ev, torture->lp_ctx);
724         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
725
726         cli_credentials_set_netlogon_creds(credentials1, NULL);
727         cli_credentials_set_netlogon_creds(credentials2, NULL);
728
729         torture_comment(torture, "Testing logon on pipe1\n");
730         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
731                 return false;
732
733         torture_comment(torture, "Testing logon on pipe2\n");
734         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
735                 return false;
736
737         torture_comment(torture, "Again on pipe1\n");
738         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
739                 return false;
740
741         torture_comment(torture, "Again on pipe2\n");
742         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
743                 return false;
744
745         torture_leave_domain(torture, join_ctx);
746         return true;
747 }
748
749 struct torture_schannel_bench;
750
751 struct torture_schannel_bench_conn {
752         struct torture_schannel_bench *s;
753         int index;
754         struct cli_credentials *wks_creds;
755         struct dcerpc_pipe *pipe;
756         struct netr_LogonSamLogonEx r;
757         struct netr_NetworkInfo ninfo;
758         TALLOC_CTX *tmp;
759         uint64_t total;
760         uint32_t count;
761 };
762
763 struct torture_schannel_bench {
764         struct torture_context *tctx;
765         bool progress;
766         int timelimit;
767         int nprocs;
768         int nconns;
769         struct torture_schannel_bench_conn *conns;
770         struct test_join *join_ctx1;
771         struct cli_credentials *wks_creds1;
772         struct test_join *join_ctx2;
773         struct cli_credentials *wks_creds2;
774         struct cli_credentials *user1_creds;
775         struct cli_credentials *user2_creds;
776         struct dcerpc_binding *b;
777         NTSTATUS error;
778         uint64_t total;
779         uint32_t count;
780         bool stopped;
781 };
782
783 #if 0
784 static void torture_schannel_bench_connected(struct composite_context *c)
785 {
786         struct torture_schannel_bench_conn *conn =
787                 (struct torture_schannel_bench_conn *)c->async.private_data;
788         struct torture_schannel_bench *s = talloc_get_type(conn->s,
789                                            struct torture_schannel_bench);
790
791         s->error = dcerpc_pipe_connect_b_recv(c, s->conns, &conn->pipe);
792         torture_comment(s->tctx, "conn[%u]: %s\n", conn->index, nt_errstr(s->error));
793         if (NT_STATUS_IS_OK(s->error)) {
794                 s->nconns++;
795         }
796 }
797 #endif
798
799 static void torture_schannel_bench_recv(struct tevent_req *subreq);
800
801 static bool torture_schannel_bench_start(struct torture_schannel_bench_conn *conn)
802 {
803         struct torture_schannel_bench *s = conn->s;
804         NTSTATUS status;
805         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
806         int flags = CLI_CRED_NTLM_AUTH;
807         struct tevent_req *subreq;
808         struct cli_credentials *user_creds;
809
810         if (conn->total % 2) {
811                 user_creds = s->user1_creds;
812         } else {
813                 user_creds = s->user2_creds;
814         }
815
816         if (lpcfg_client_lanman_auth(s->tctx->lp_ctx)) {
817                 flags |= CLI_CRED_LANMAN_AUTH;
818         }
819
820         if (lpcfg_client_ntlmv2_auth(s->tctx->lp_ctx)) {
821                 flags |= CLI_CRED_NTLMv2_AUTH;
822         }
823
824         talloc_free(conn->tmp);
825         conn->tmp = talloc_new(s);
826         ZERO_STRUCT(conn->ninfo);
827         ZERO_STRUCT(conn->r);
828
829         cli_credentials_get_ntlm_username_domain(user_creds, conn->tmp,
830                                                  &conn->ninfo.identity_info.account_name.string,
831                                                  &conn->ninfo.identity_info.domain_name.string);
832
833         generate_random_buffer(conn->ninfo.challenge,
834                                sizeof(conn->ninfo.challenge));
835         chal = data_blob_const(conn->ninfo.challenge,
836                                sizeof(conn->ninfo.challenge));
837
838         names_blob = NTLMv2_generate_names_blob(conn->tmp,
839                                                 cli_credentials_get_workstation(conn->wks_creds),
840                                                 cli_credentials_get_domain(conn->wks_creds));
841
842         status = cli_credentials_get_ntlm_response(user_creds, conn->tmp,
843                                                    &flags,
844                                                    chal,
845                                                    names_blob,
846                                                    &lm_resp, &nt_resp,
847                                                    NULL, NULL);
848         torture_assert_ntstatus_ok(s->tctx, status,
849                                    "cli_credentials_get_ntlm_response failed");
850
851         conn->ninfo.lm.data = lm_resp.data;
852         conn->ninfo.lm.length = lm_resp.length;
853
854         conn->ninfo.nt.data = nt_resp.data;
855         conn->ninfo.nt.length = nt_resp.length;
856
857         conn->ninfo.identity_info.parameter_control = 0;
858         conn->ninfo.identity_info.logon_id_low = 0;
859         conn->ninfo.identity_info.logon_id_high = 0;
860         conn->ninfo.identity_info.workstation.string = cli_credentials_get_workstation(conn->wks_creds);
861
862         conn->r.in.server_name = talloc_asprintf(conn->tmp, "\\\\%s", dcerpc_server_name(conn->pipe));
863         conn->r.in.computer_name = cli_credentials_get_workstation(conn->wks_creds);
864         conn->r.in.logon_level = NetlogonNetworkInformation;
865         conn->r.in.logon = talloc(conn->tmp, union netr_LogonLevel);
866         conn->r.in.logon->network = &conn->ninfo;
867         conn->r.in.flags = talloc(conn->tmp, uint32_t);
868         conn->r.in.validation_level = 2;
869         conn->r.out.validation = talloc(conn->tmp, union netr_Validation);
870         conn->r.out.authoritative = talloc(conn->tmp, uint8_t);
871         conn->r.out.flags = conn->r.in.flags;
872
873         subreq = dcerpc_netr_LogonSamLogonEx_r_send(s, s->tctx->ev,
874                                                     conn->pipe->binding_handle,
875                                                     &conn->r);
876         torture_assert(s->tctx, subreq, "Failed to setup LogonSamLogonEx request");
877
878         tevent_req_set_callback(subreq, torture_schannel_bench_recv, conn);
879
880         return true;
881 }
882
883 static void torture_schannel_bench_recv(struct tevent_req *subreq)
884 {
885         bool ret;
886         struct torture_schannel_bench_conn *conn =
887                 (struct torture_schannel_bench_conn *)tevent_req_callback_data_void(subreq);
888         struct torture_schannel_bench *s = talloc_get_type(conn->s,
889                                            struct torture_schannel_bench);
890
891         s->error = dcerpc_netr_LogonSamLogonEx_r_recv(subreq, subreq);
892         TALLOC_FREE(subreq);
893         if (!NT_STATUS_IS_OK(s->error)) {
894                 return;
895         }
896
897         conn->total++;
898         conn->count++;
899
900         if (s->stopped) {
901                 return;
902         }
903
904         ret = torture_schannel_bench_start(conn);
905         if (!ret) {
906                 s->error = NT_STATUS_INTERNAL_ERROR;
907         }
908 }
909
910 /*
911   test multiple schannel connection in parallel
912  */
913 bool torture_rpc_schannel_bench1(struct torture_context *torture)
914 {
915         bool ret = true;
916         NTSTATUS status;
917         const char *binding = torture_setting_string(torture, "binding", NULL);
918         struct torture_schannel_bench *s;
919         struct timeval start;
920         struct timeval end;
921         int i;
922         const char *tmp;
923
924         s = talloc_zero(torture, struct torture_schannel_bench);
925         s->tctx = torture;
926         s->progress = torture_setting_bool(torture, "progress", true);
927         s->timelimit = torture_setting_int(torture, "timelimit", 10);
928         s->nprocs = torture_setting_int(torture, "nprocs", 4);
929         s->conns = talloc_zero_array(s, struct torture_schannel_bench_conn, s->nprocs);
930
931         s->user1_creds = cli_credentials_shallow_copy(s, cmdline_credentials);
932         tmp = torture_setting_string(s->tctx, "extra_user1", NULL);
933         if (tmp) {
934                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
935         }
936         s->user2_creds = cli_credentials_shallow_copy(s, cmdline_credentials);
937         tmp = torture_setting_string(s->tctx, "extra_user2", NULL);
938         if (tmp) {
939                 cli_credentials_parse_string(s->user1_creds, tmp, CRED_SPECIFIED);
940         }
941
942         s->join_ctx1 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sb", TEST_MACHINE_NAME),
943                                            ACB_WSTRUST, &s->wks_creds1);
944         torture_assert(torture, s->join_ctx1 != NULL,
945                        "Failed to join domain with acct_flags=ACB_WSTRUST");
946         s->join_ctx2 = torture_join_domain(s->tctx, talloc_asprintf(s, "%sc", TEST_MACHINE_NAME),
947                                            ACB_WSTRUST, &s->wks_creds2);
948         torture_assert(torture, s->join_ctx2 != NULL,
949                        "Failed to join domain with acct_flags=ACB_WSTRUST");
950
951         cli_credentials_set_kerberos_state(s->wks_creds1, CRED_DONT_USE_KERBEROS);
952         cli_credentials_set_kerberos_state(s->wks_creds2, CRED_DONT_USE_KERBEROS);
953
954         for (i=0; i < s->nprocs; i++) {
955                 struct cli_credentials *wks = s->wks_creds1;
956
957                 if ((i % 2) && (torture_setting_bool(torture, "multijoin", false))) {
958                         wks = s->wks_creds2;
959                 }
960
961                 s->conns[i].s = s;
962                 s->conns[i].index = i;
963                 s->conns[i].wks_creds = cli_credentials_shallow_copy(s->conns, wks);
964                 cli_credentials_set_netlogon_creds(s->conns[i].wks_creds, NULL);
965         }
966
967         status = dcerpc_parse_binding(s, binding, &s->b);
968         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
969
970         status = dcerpc_binding_set_flags(s->b, DCERPC_SCHANNEL | DCERPC_SIGN,
971                                           DCERPC_AUTH_OPTIONS);
972         torture_assert_ntstatus_ok(torture, status, "set flags");
973
974         torture_comment(torture, "Opening %d connections in parallel\n", s->nprocs);
975         for (i=0; i < s->nprocs; i++) {
976 #if 1
977                 s->error = dcerpc_pipe_connect_b(s->conns, &s->conns[i].pipe, s->b,
978                                                  &ndr_table_netlogon,
979                                                  s->conns[i].wks_creds,
980                                                  torture->ev, torture->lp_ctx);
981                 torture_assert_ntstatus_ok(torture, s->error, "Failed to connect with schannel");
982 #else
983                 /*
984                  * This path doesn't work against windows,
985                  * because of windows drops the connections
986                  * which haven't reached a session setup yet
987                  *
988                  * The same as the reset on zero vc stuff.
989                  */
990                 struct composite_context *c;
991                 c = dcerpc_pipe_connect_b_send(s->conns, s->b,
992                                                &ndr_table_netlogon,
993                                                s->conns[i].wks_creds,
994                                                torture->ev,
995                                                torture->lp_ctx);
996                 torture_assert(torture, c != NULL, "Failed to setup connect");
997                 c->async.fn = torture_schannel_bench_connected;
998                 c->async.private_data = &s->conns[i];
999         }
1000
1001         while (NT_STATUS_IS_OK(s->error) && s->nprocs != s->nconns) {
1002                 int ev_ret = tevent_loop_once(torture->ev);
1003                 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
1004 #endif
1005         }
1006         torture_assert_ntstatus_ok(torture, s->error, "Failed establish a connect");
1007
1008         /*
1009          * Change the workstation password after establishing the netlogon
1010          * schannel connections to prove that existing connections are not
1011          * affected by a wks pwchange.
1012          */
1013
1014         {
1015                 struct netr_ServerPasswordSet pwset;
1016                 char *password = generate_random_password(s->join_ctx1, 8, 255);
1017                 struct netlogon_creds_CredentialState *creds_state;
1018                 struct dcerpc_pipe *net_pipe;
1019                 struct netr_Authenticator credential, return_authenticator;
1020                 struct samr_Password new_password;
1021
1022                 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
1023                                                &ndr_table_netlogon,
1024                                                s->wks_creds1,
1025                                                torture->ev, torture->lp_ctx);
1026
1027                 torture_assert_ntstatus_ok(torture, status,
1028                                            "dcerpc_pipe_connect_b failed");
1029
1030                 pwset.in.server_name = talloc_asprintf(
1031                         net_pipe, "\\\\%s", dcerpc_server_name(net_pipe));
1032                 pwset.in.computer_name =
1033                         cli_credentials_get_workstation(s->wks_creds1);
1034                 pwset.in.account_name = talloc_asprintf(
1035                         net_pipe, "%s$", pwset.in.computer_name);
1036                 pwset.in.secure_channel_type = SEC_CHAN_WKSTA;
1037                 pwset.in.credential = &credential;
1038                 pwset.in.new_password = &new_password;
1039                 pwset.out.return_authenticator = &return_authenticator;
1040
1041                 E_md4hash(password, new_password.hash);
1042
1043                 creds_state = cli_credentials_get_netlogon_creds(
1044                         s->wks_creds1);
1045                 netlogon_creds_des_encrypt(creds_state, &new_password);
1046                 netlogon_creds_client_authenticator(creds_state, &credential);
1047
1048                 torture_assert_ntstatus_ok(torture, dcerpc_netr_ServerPasswordSet_r(net_pipe->binding_handle, torture, &pwset),
1049                         "ServerPasswordSet failed");
1050                 torture_assert_ntstatus_ok(torture, pwset.out.result,
1051                                            "ServerPasswordSet failed");
1052
1053                 if (!netlogon_creds_client_check(creds_state,
1054                                         &pwset.out.return_authenticator->cred)) {
1055                         torture_comment(torture, "Credential chaining failed\n");
1056                 }
1057
1058                 cli_credentials_set_password(s->wks_creds1, password,
1059                                              CRED_SPECIFIED);
1060
1061                 talloc_free(net_pipe);
1062
1063                 /* Just as a test, connect with the new creds */
1064
1065                 cli_credentials_set_netlogon_creds(s->wks_creds1, NULL);
1066
1067                 status = dcerpc_pipe_connect_b(s, &net_pipe, s->b,
1068                                                &ndr_table_netlogon,
1069                                                s->wks_creds1,
1070                                                torture->ev, torture->lp_ctx);
1071
1072                 torture_assert_ntstatus_ok(torture, status,
1073                                            "dcerpc_pipe_connect_b failed");
1074
1075                 talloc_free(net_pipe);
1076         }
1077
1078         torture_comment(torture, "Start looping LogonSamLogonEx on %d connections for %d secs\n",
1079                         s->nprocs, s->timelimit);
1080         for (i=0; i < s->nprocs; i++) {
1081                 ret = torture_schannel_bench_start(&s->conns[i]);
1082                 torture_assert(torture, ret, "Failed to setup LogonSamLogonEx");
1083         }
1084
1085         start = timeval_current();
1086         end = timeval_add(&start, s->timelimit, 0);
1087
1088         while (NT_STATUS_IS_OK(s->error) && !timeval_expired(&end)) {
1089                 int ev_ret = tevent_loop_once(torture->ev);
1090                 torture_assert(torture, ev_ret == 0, "tevent_loop_once failed");
1091         }
1092         torture_assert_ntstatus_ok(torture, s->error, "Failed some request");
1093         s->stopped = true;
1094         talloc_free(s->conns);
1095
1096         for (i=0; i < s->nprocs; i++) {
1097                 s->total += s->conns[i].total;
1098         }
1099
1100         torture_comment(torture,
1101                         "Total ops[%llu] (%u ops/s)\n",
1102                         (unsigned long long)s->total,
1103                         (unsigned)s->total/s->timelimit);
1104
1105         torture_leave_domain(torture, s->join_ctx1);
1106         torture_leave_domain(torture, s->join_ctx2);
1107         return true;
1108 }