Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-trivial
[kai/samba.git] / source4 / torture / rpc / schannel.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_netlogon_c.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "auth/credentials/credentials.h"
27 #include "torture/rpc/rpc.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "auth/gensec/schannel_proto.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "libcli/security/security.h"
32 #include "system/filesys.h"
33 #include "param/param.h"
34
35 #define TEST_MACHINE_NAME "schannel"
36
37 /*
38   try a netlogon SamLogon
39 */
40 bool test_netlogon_ex_ops(struct dcerpc_pipe *p, struct torture_context *tctx, 
41                           struct cli_credentials *credentials, 
42                           struct creds_CredentialState *creds)
43 {
44         NTSTATUS status;
45         struct netr_LogonSamLogonEx r;
46         struct netr_NetworkInfo ninfo;
47         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
48         int i;
49         int flags = CLI_CRED_NTLM_AUTH;
50         if (lp_client_lanman_auth(tctx->lp_ctx)) {
51                 flags |= CLI_CRED_LANMAN_AUTH;
52         }
53
54         if (lp_client_ntlmv2_auth(tctx->lp_ctx)) {
55                 flags |= CLI_CRED_NTLMv2_AUTH;
56         }
57
58         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx, 
59                                                  &ninfo.identity_info.account_name.string,
60                                                  &ninfo.identity_info.domain_name.string);
61         
62         generate_random_buffer(ninfo.challenge, 
63                                sizeof(ninfo.challenge));
64         chal = data_blob_const(ninfo.challenge, 
65                                sizeof(ninfo.challenge));
66
67         names_blob = NTLMv2_generate_names_blob(tctx, lp_iconv_convenience(tctx->lp_ctx), cli_credentials_get_workstation(credentials), 
68                                                 cli_credentials_get_domain(credentials));
69
70         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx, 
71                                                    &flags, 
72                                                    chal,
73                                                    names_blob,
74                                                    &lm_resp, &nt_resp,
75                                                    NULL, NULL);
76         torture_assert_ntstatus_ok(tctx, status, 
77                                    "cli_credentials_get_ntlm_response failed");
78
79         ninfo.lm.data = lm_resp.data;
80         ninfo.lm.length = lm_resp.length;
81
82         ninfo.nt.data = nt_resp.data;
83         ninfo.nt.length = nt_resp.length;
84
85         ninfo.identity_info.parameter_control = 0;
86         ninfo.identity_info.logon_id_low = 0;
87         ninfo.identity_info.logon_id_high = 0;
88         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
89
90         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
91         r.in.computer_name = cli_credentials_get_workstation(credentials);
92         r.in.logon_level = 2;
93         r.in.logon.network = &ninfo;
94         r.in.flags = 0;
95
96         torture_comment(tctx, 
97                         "Testing LogonSamLogonEx with name %s\n", 
98                         ninfo.identity_info.account_name.string);
99         
100         for (i=2;i<3;i++) {
101                 r.in.validation_level = i;
102                 
103                 status = dcerpc_netr_LogonSamLogonEx(p, tctx, &r);
104                 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
105         }
106
107         return true;
108 }
109
110 /*
111   do some samr ops using the schannel connection
112  */
113 static bool test_samr_ops(struct torture_context *tctx,
114                           struct dcerpc_pipe *p)
115 {
116         NTSTATUS status;
117         struct samr_GetDomPwInfo r;
118         struct samr_Connect connect;
119         struct samr_OpenDomain opendom;
120         int i;
121         struct lsa_String name;
122         struct policy_handle handle;
123         struct policy_handle domain_handle;
124
125         name.string = lp_workgroup(tctx->lp_ctx);
126         r.in.domain_name = &name;
127
128         connect.in.system_name = 0;
129         connect.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
130         connect.out.connect_handle = &handle;
131         
132         printf("Testing Connect and OpenDomain on BUILTIN\n");
133
134         status = dcerpc_samr_Connect(p, tctx, &connect);
135         if (!NT_STATUS_IS_OK(status)) {
136                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
137                         printf("Connect failed (expected, schannel mapped to anonymous): %s\n",
138                                nt_errstr(status));
139                 } else {
140                         printf("Connect failed - %s\n", nt_errstr(status));
141                         return false;
142                 }
143         } else {
144                 opendom.in.connect_handle = &handle;
145                 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
146                 opendom.in.sid = dom_sid_parse_talloc(tctx, "S-1-5-32");
147                 opendom.out.domain_handle = &domain_handle;
148                 
149                 status = dcerpc_samr_OpenDomain(p, tctx, &opendom);
150                 if (!NT_STATUS_IS_OK(status)) {
151                         printf("OpenDomain failed - %s\n", nt_errstr(status));
152                         return false;
153                 }
154         }
155
156         printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
157         
158         /* do several ops to test credential chaining */
159         for (i=0;i<5;i++) {
160                 status = dcerpc_samr_GetDomPwInfo(p, tctx, &r);
161                 if (!NT_STATUS_IS_OK(status)) {
162                         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
163                                 printf("GetDomPwInfo op %d failed - %s\n", i, nt_errstr(status));
164                                 return false;
165                         }
166                 }
167         }
168
169         return true;
170 }
171
172
173 /*
174   do some lsa ops using the schannel connection
175  */
176 static bool test_lsa_ops(struct torture_context *tctx, struct dcerpc_pipe *p)
177 {
178         struct lsa_GetUserName r;
179         NTSTATUS status;
180         bool ret = true;
181         struct lsa_StringPointer authority_name_p;
182
183         printf("\nTesting GetUserName\n");
184
185         r.in.system_name = "\\";        
186         r.in.account_name = NULL;       
187         r.in.authority_name = &authority_name_p;
188         authority_name_p.string = NULL;
189
190         /* do several ops to test credential chaining and various operations */
191         status = dcerpc_lsa_GetUserName(p, tctx, &r);
192         
193         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
194                 printf("not considering %s to be an error\n", nt_errstr(status));
195         } else if (!NT_STATUS_IS_OK(status)) {
196                 printf("GetUserName failed - %s\n", nt_errstr(status));
197                 return false;
198         } else {
199                 if (!r.out.account_name) {
200                         return false;
201                 }
202                 
203                 if (strcmp(r.out.account_name->string, "ANONYMOUS LOGON") != 0) {
204                         printf("GetUserName returned wrong user: %s, expected %s\n",
205                                r.out.account_name->string, "ANONYMOUS LOGON");
206                         return false;
207                 }
208                 if (!r.out.authority_name || !r.out.authority_name->string) {
209                         return false;
210                 }
211                 
212                 if (strcmp(r.out.authority_name->string->string, "NT AUTHORITY") != 0) {
213                         printf("GetUserName returned wrong user: %s, expected %s\n",
214                                r.out.authority_name->string->string, "NT AUTHORITY");
215                         return false;
216                 }
217         }
218         if (!test_many_LookupSids(p, tctx, NULL)) {
219                 printf("LsaLookupSids3 failed!\n");
220                 return false;
221         }
222
223         return ret;
224 }
225
226
227 /*
228   test a schannel connection with the given flags
229  */
230 static bool test_schannel(struct torture_context *tctx,
231                           uint16_t acct_flags, uint32_t dcerpc_flags,
232                           int i)
233 {
234         struct test_join *join_ctx;
235         NTSTATUS status;
236         const char *binding = torture_setting_string(tctx, "binding", NULL);
237         struct dcerpc_binding *b;
238         struct dcerpc_pipe *p = NULL;
239         struct dcerpc_pipe *p_netlogon = NULL;
240         struct dcerpc_pipe *p_netlogon2 = NULL;
241         struct dcerpc_pipe *p_netlogon3 = NULL;
242         struct dcerpc_pipe *p_samr2 = NULL;
243         struct dcerpc_pipe *p_lsa = NULL;
244         struct creds_CredentialState *creds;
245         struct cli_credentials *credentials;
246
247         join_ctx = torture_join_domain(tctx, 
248                                        talloc_asprintf(tctx, "%s%d", TEST_MACHINE_NAME, i), 
249                                        acct_flags, &credentials);
250         torture_assert(tctx, join_ctx != NULL, "Failed to join domain");
251
252         status = dcerpc_parse_binding(tctx, binding, &b);
253         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
254
255         b->flags &= ~DCERPC_AUTH_OPTIONS;
256         b->flags |= dcerpc_flags;
257
258         status = dcerpc_pipe_connect_b(tctx, &p, b, &ndr_table_samr,
259                                        credentials, NULL, tctx->lp_ctx);
260         torture_assert_ntstatus_ok(tctx, status, 
261                 "Failed to connect with schannel");
262
263         torture_assert(tctx, test_samr_ops(tctx, p), 
264                        "Failed to process schannel secured SAMR ops");
265
266         /* Also test that when we connect to the netlogon pipe, that
267          * the credentials we setup on the first pipe are valid for
268          * the second */
269
270         /* Swap the binding details from SAMR to NETLOGON */
271         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, NULL, tctx->lp_ctx);
272         torture_assert_ntstatus_ok(tctx, status, "epm map");
273
274         status = dcerpc_secondary_connection(p, &p_netlogon, 
275                                              b);
276         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
277
278         status = dcerpc_bind_auth(p_netlogon, &ndr_table_netlogon, 
279                                   credentials, tctx->lp_ctx,
280                                   DCERPC_AUTH_TYPE_SCHANNEL,
281                                   dcerpc_auth_level(p->conn),
282                                   NULL);
283
284         torture_assert_ntstatus_ok(tctx, status, "bind auth");
285
286         status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, tctx, &creds);
287         torture_assert_ntstatus_ok(tctx, status, "schannel creds");
288
289         /* do a couple of logins */
290         torture_assert(tctx, test_netlogon_ops(p_netlogon, tctx, credentials, creds),
291                 "Failed to process schannel secured NETLOGON ops");
292
293         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon, tctx, credentials, creds),
294                 "Failed to process schannel secured NETLOGON EX ops");
295
296         /* Swap the binding details from SAMR to LSARPC */
297         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_lsarpc, NULL, tctx->lp_ctx);
298         torture_assert_ntstatus_ok(tctx, status, "epm map");
299
300         status = dcerpc_secondary_connection(p, &p_lsa, 
301                                              b);
302
303         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
304
305         status = dcerpc_bind_auth(p_lsa, &ndr_table_lsarpc,
306                                   credentials, tctx->lp_ctx,
307                                   DCERPC_AUTH_TYPE_SCHANNEL,
308                                   dcerpc_auth_level(p->conn),
309                                   NULL);
310
311         torture_assert_ntstatus_ok(tctx, status, "bind auth");
312
313         torture_assert(tctx, test_lsa_ops(tctx, p_lsa), 
314                 "Failed to process schannel secured LSA ops");
315
316         /* Drop the socket, we want to start from scratch */
317         talloc_free(p);
318         p = NULL;
319
320         /* Now see what we are still allowed to do */
321         
322         status = dcerpc_parse_binding(tctx, binding, &b);
323         torture_assert_ntstatus_ok(tctx, status, "Bad binding string");
324
325         b->flags &= ~DCERPC_AUTH_OPTIONS;
326         b->flags |= dcerpc_flags;
327
328         status = dcerpc_pipe_connect_b(tctx, &p_samr2, b, &ndr_table_samr,
329                                        credentials, NULL, tctx->lp_ctx);
330         torture_assert_ntstatus_ok(tctx, status, 
331                 "Failed to connect with schannel");
332
333         /* do a some SAMR operations.  We have *not* done a new serverauthenticate */
334         torture_assert (tctx, test_samr_ops(tctx, p_samr2), 
335                         "Failed to process schannel secured SAMR ops (on fresh connection)");
336
337         /* Swap the binding details from SAMR to NETLOGON */
338         status = dcerpc_epm_map_binding(tctx, b, &ndr_table_netlogon, NULL, tctx->lp_ctx);
339         torture_assert_ntstatus_ok(tctx, status, "epm");
340
341         status = dcerpc_secondary_connection(p_samr2, &p_netlogon2, 
342                                              b);
343         torture_assert_ntstatus_ok(tctx, status, "seconday connection");
344
345         /* and now setup an SCHANNEL bind on netlogon */
346         status = dcerpc_bind_auth(p_netlogon2, &ndr_table_netlogon,
347                                   credentials, tctx->lp_ctx,
348                                   DCERPC_AUTH_TYPE_SCHANNEL,
349                                   dcerpc_auth_level(p_samr2->conn),
350                                   NULL);
351
352         torture_assert_ntstatus_ok(tctx, status, "auth failed");
353         
354         /* Try the schannel-only SamLogonEx operation */
355         torture_assert(tctx, test_netlogon_ex_ops(p_netlogon2, tctx, credentials, creds), 
356                        "Failed to process schannel secured NETLOGON EX ops (on fresh connection)");
357                 
358
359         /* And the more traditional style, proving that the
360          * credentials chaining state is fully present */
361         torture_assert(tctx, test_netlogon_ops(p_netlogon2, tctx, credentials, creds),
362                              "Failed to process schannel secured NETLOGON ops (on fresh connection)");
363
364         /* Drop the socket, we want to start from scratch (again) */
365         talloc_free(p_samr2);
366
367         /* We don't want schannel for this test */
368         b->flags &= ~DCERPC_AUTH_OPTIONS;
369
370         status = dcerpc_pipe_connect_b(tctx, &p_netlogon3, b, &ndr_table_netlogon,
371                                        credentials, NULL, tctx->lp_ctx);
372         torture_assert_ntstatus_ok(tctx, status, "Failed to connect without schannel");
373
374         torture_assert(tctx, !test_netlogon_ex_ops(p_netlogon3, tctx, credentials, creds),
375                         "Processed NOT schannel secured NETLOGON EX ops without SCHANNEL (unsafe)");
376
377         /* Required because the previous call will mark the current context as having failed */
378         tctx->last_result = TORTURE_OK;
379         tctx->last_reason = NULL;
380
381         torture_assert(tctx, test_netlogon_ops(p_netlogon3, tctx, credentials, creds),
382                         "Failed to processed NOT schannel secured NETLOGON ops without new ServerAuth");
383
384         torture_leave_domain(join_ctx);
385         return true;
386 }
387
388
389
390 /*
391   a schannel test suite
392  */
393 bool torture_rpc_schannel(struct torture_context *torture)
394 {
395         bool ret = true;
396         struct {
397                 uint16_t acct_flags;
398                 uint32_t dcerpc_flags;
399         } tests[] = {
400                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN},
401                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL},
402                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128},
403                 { ACB_WSTRUST,   DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 },
404                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN },
405                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL },
406                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128 },
407                 { ACB_SVRTRUST,  DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128 }
408         };
409         int i;
410
411         for (i=0;i<ARRAY_SIZE(tests);i++) {
412                 if (!test_schannel(torture, 
413                                    tests[i].acct_flags, tests[i].dcerpc_flags,
414                                    i)) {
415                         torture_comment(torture, "Failed with acct_flags=0x%x dcerpc_flags=0x%x \n",
416                                tests[i].acct_flags, tests[i].dcerpc_flags);
417                         ret = false;
418                 }
419         }
420
421         return ret;
422 }
423
424 /*
425   test two schannel connections
426  */
427 bool torture_rpc_schannel2(struct torture_context *torture)
428 {
429         struct test_join *join_ctx;
430         NTSTATUS status;
431         const char *binding = torture_setting_string(torture, "binding", NULL);
432         struct dcerpc_binding *b;
433         struct dcerpc_pipe *p1 = NULL, *p2 = NULL;
434         struct cli_credentials *credentials1, *credentials2;
435         uint32_t dcerpc_flags = DCERPC_SCHANNEL | DCERPC_SIGN;
436
437         join_ctx = torture_join_domain(torture, talloc_asprintf(torture, "%s2", TEST_MACHINE_NAME), 
438                                        ACB_WSTRUST, &credentials1);
439         torture_assert(torture, join_ctx != NULL, 
440                        "Failed to join domain with acct_flags=ACB_WSTRUST");
441
442         credentials2 = (struct cli_credentials *)talloc_memdup(torture, credentials1, sizeof(*credentials1));
443         credentials1->netlogon_creds = NULL;
444         credentials2->netlogon_creds = NULL;
445
446         status = dcerpc_parse_binding(torture, binding, &b);
447         torture_assert_ntstatus_ok(torture, status, "Bad binding string");
448
449         b->flags &= ~DCERPC_AUTH_OPTIONS;
450         b->flags |= dcerpc_flags;
451
452         printf("Opening first connection\n");
453         status = dcerpc_pipe_connect_b(torture, &p1, b, &ndr_table_netlogon,
454                                        credentials1, NULL, torture->lp_ctx);
455         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
456
457         torture_comment(torture, "Opening second connection\n");
458         status = dcerpc_pipe_connect_b(torture, &p2, b, &ndr_table_netlogon,
459                                        credentials2, NULL, torture->lp_ctx);
460         torture_assert_ntstatus_ok(torture, status, "Failed to connect with schannel");
461
462         credentials1->netlogon_creds = NULL;
463         credentials2->netlogon_creds = NULL;
464
465         torture_comment(torture, "Testing logon on pipe1\n");
466         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
467                 return false;
468
469         torture_comment(torture, "Testing logon on pipe2\n");
470         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
471                 return false;
472
473         torture_comment(torture, "Again on pipe1\n");
474         if (!test_netlogon_ex_ops(p1, torture, credentials1, NULL))
475                 return false;
476
477         torture_comment(torture, "Again on pipe2\n");
478         if (!test_netlogon_ex_ops(p2, torture, credentials2, NULL))
479                 return false;
480
481         torture_leave_domain(join_ctx);
482         return true;
483 }
484