e0ed70ccbc95518570023e3d2d42b82438aabe55
[gd/samba-autobuild/.git] / source4 / torture / rpc / rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/cmdline/popt_common.h"
23 #include "torture/rpc/torture_rpc.h"
24 #include "torture/smbtorture.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "../lib/util/dlinklist.h"
27
28 static bool torture_rpc_teardown (struct torture_context *tcase, 
29                                           void *data)
30 {
31         struct torture_rpc_tcase_data *tcase_data = 
32                 (struct torture_rpc_tcase_data *)data;
33         if (tcase_data->join_ctx != NULL)
34             torture_leave_domain(tcase, tcase_data->join_ctx);
35         talloc_free(tcase_data);
36         return true;
37 }
38
39 /**
40  * Obtain the DCE/RPC binding context associated with a torture context.
41  *
42  * @param tctx Torture context
43  * @param binding Pointer to store DCE/RPC binding
44  */
45 NTSTATUS torture_rpc_binding(struct torture_context *tctx,
46                              struct dcerpc_binding **binding)
47 {
48         NTSTATUS status;
49         const char *binding_string = torture_setting_string(tctx, "binding", 
50                                                             NULL);
51
52         if (binding_string == NULL) {
53                 torture_comment(tctx, 
54                                 "You must specify a DCE/RPC binding string\n");
55                 return NT_STATUS_INVALID_PARAMETER;
56         }
57
58         status = dcerpc_parse_binding(tctx, binding_string, binding);
59         if (NT_STATUS_IS_ERR(status)) {
60                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", 
61                          binding_string));
62                 return status;
63         }
64
65         return NT_STATUS_OK;    
66 }
67
68 /**
69  * open a rpc connection to the chosen binding string
70  */
71 _PUBLIC_ NTSTATUS torture_rpc_connection(struct torture_context *tctx,
72                                 struct dcerpc_pipe **p, 
73                                 const struct ndr_interface_table *table)
74 {
75         NTSTATUS status;
76         struct dcerpc_binding *binding;
77
78         dcerpc_init();
79
80         status = torture_rpc_binding(tctx, &binding);
81         if (NT_STATUS_IS_ERR(status))
82                 return status;
83
84         status = dcerpc_pipe_connect_b(tctx, 
85                                      p, binding, table,
86                                      cmdline_credentials, tctx->ev, tctx->lp_ctx);
87  
88         if (NT_STATUS_IS_ERR(status)) {
89                 torture_warning(tctx, "Failed to connect to remote server: %s %s\n",
90                            dcerpc_binding_string(tctx, binding), nt_errstr(status));
91         }
92
93         return status;
94 }
95
96 /**
97  * open a rpc connection to a specific transport
98  */
99 NTSTATUS torture_rpc_connection_transport(struct torture_context *tctx, 
100                                           struct dcerpc_pipe **p, 
101                                           const struct ndr_interface_table *table,
102                                           enum dcerpc_transport_t transport,
103                                           uint32_t assoc_group_id,
104                                           uint32_t extra_flags)
105 {
106         NTSTATUS status;
107         struct dcerpc_binding *binding;
108
109         *p = NULL;
110
111         status = torture_rpc_binding(tctx, &binding);
112         if (!NT_STATUS_IS_OK(status)) {
113                 return status;
114         }
115
116         status = dcerpc_binding_set_transport(binding, transport);
117         if (!NT_STATUS_IS_OK(status)) {
118                 return status;
119         }
120
121         status = dcerpc_binding_set_assoc_group_id(binding, assoc_group_id);
122         if (!NT_STATUS_IS_OK(status)) {
123                 return status;
124         }
125
126         status = dcerpc_binding_set_flags(binding, extra_flags, 0);
127         if (!NT_STATUS_IS_OK(status)) {
128                 return status;
129         }
130
131         status = dcerpc_pipe_connect_b(tctx, p, binding, table,
132                                        cmdline_credentials,
133                                        tctx->ev, tctx->lp_ctx);
134         if (!NT_STATUS_IS_OK(status)) {
135                 *p = NULL;
136                 return status;
137         }
138
139         return NT_STATUS_OK;
140 }
141
142 static bool torture_rpc_setup_machine_workstation(struct torture_context *tctx,
143                                                   void **data)
144 {
145         NTSTATUS status;
146         struct dcerpc_binding *binding;
147         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase,
148                                                 struct torture_rpc_tcase);
149         struct torture_rpc_tcase_data *tcase_data;
150
151         status = torture_rpc_binding(tctx, &binding);
152         if (NT_STATUS_IS_ERR(status))
153                 return false;
154
155         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
156         tcase_data->credentials = cmdline_credentials;
157         tcase_data->join_ctx = torture_join_domain(tctx, tcase->machine_name,
158                                                    ACB_WSTRUST,
159                                                    &tcase_data->credentials);
160         if (tcase_data->join_ctx == NULL)
161             torture_fail(tctx, "Failed to join as WORKSTATION");
162
163         status = dcerpc_pipe_connect_b(tctx,
164                                 &(tcase_data->pipe),
165                                 binding,
166                                 tcase->table,
167                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
168
169         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
170
171         return NT_STATUS_IS_OK(status);
172 }
173
174 static bool torture_rpc_setup_machine_bdc(struct torture_context *tctx,
175                                           void **data)
176 {
177         NTSTATUS status;
178         struct dcerpc_binding *binding;
179         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
180                                                 struct torture_rpc_tcase);
181         struct torture_rpc_tcase_data *tcase_data;
182
183         status = torture_rpc_binding(tctx, &binding);
184         if (NT_STATUS_IS_ERR(status))
185                 return false;
186
187         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
188         tcase_data->credentials = cmdline_credentials;
189         tcase_data->join_ctx = torture_join_domain(tctx, tcase->machine_name,
190                                                    ACB_SVRTRUST, 
191                                                    &tcase_data->credentials);
192         if (tcase_data->join_ctx == NULL)
193             torture_fail(tctx, "Failed to join as BDC");
194
195         status = dcerpc_pipe_connect_b(tctx, 
196                                 &(tcase_data->pipe),
197                                 binding,
198                                 tcase->table,
199                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
200
201         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
202
203         return NT_STATUS_IS_OK(status);
204 }
205
206 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_machine_workstation_rpc_iface_tcase(
207                                 struct torture_suite *suite,
208                                 const char *name,
209                                 const struct ndr_interface_table *table,
210                                 const char *machine_name)
211 {
212         struct torture_rpc_tcase *tcase = talloc(suite,
213                                                  struct torture_rpc_tcase);
214
215         torture_suite_init_rpc_tcase(suite, tcase, name, table);
216
217         tcase->machine_name = talloc_strdup(tcase, machine_name);
218         tcase->tcase.setup = torture_rpc_setup_machine_workstation;
219         tcase->tcase.teardown = torture_rpc_teardown;
220
221         return tcase;
222 }
223
224 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_machine_bdc_rpc_iface_tcase(
225                                 struct torture_suite *suite, 
226                                 const char *name,
227                                 const struct ndr_interface_table *table,
228                                 const char *machine_name)
229 {
230         struct torture_rpc_tcase *tcase = talloc(suite, 
231                                                  struct torture_rpc_tcase);
232
233         torture_suite_init_rpc_tcase(suite, tcase, name, table);
234
235         tcase->machine_name = talloc_strdup(tcase, machine_name);
236         tcase->tcase.setup = torture_rpc_setup_machine_bdc;
237         tcase->tcase.teardown = torture_rpc_teardown;
238
239         return tcase;
240 }
241
242 _PUBLIC_ bool torture_suite_init_rpc_tcase(struct torture_suite *suite, 
243                                            struct torture_rpc_tcase *tcase, 
244                                            const char *name,
245                                            const struct ndr_interface_table *table)
246 {
247         if (!torture_suite_init_tcase(suite, (struct torture_tcase *)tcase, name))
248                 return false;
249
250         tcase->table = table;
251
252         return true;
253 }
254
255 static bool torture_rpc_setup_anonymous(struct torture_context *tctx, 
256                                         void **data)
257 {
258         NTSTATUS status;
259         struct dcerpc_binding *binding;
260         struct torture_rpc_tcase_data *tcase_data;
261         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
262                                                           struct torture_rpc_tcase);
263
264         status = torture_rpc_binding(tctx, &binding);
265         if (NT_STATUS_IS_ERR(status))
266                 return false;
267
268         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
269         tcase_data->credentials = cli_credentials_init_anon(tctx);
270
271         status = dcerpc_pipe_connect_b(tctx, 
272                                 &(tcase_data->pipe),
273                                 binding,
274                                 tcase->table,
275                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
276
277         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
278
279         return NT_STATUS_IS_OK(status);
280 }
281
282 static bool torture_rpc_setup (struct torture_context *tctx, void **data)
283 {
284         NTSTATUS status;
285         struct torture_rpc_tcase *tcase = talloc_get_type(
286                                                 tctx->active_tcase, struct torture_rpc_tcase);
287         struct torture_rpc_tcase_data *tcase_data;
288
289         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
290         tcase_data->credentials = cmdline_credentials;
291         
292         status = torture_rpc_connection(tctx, 
293                                 &(tcase_data->pipe),
294                                 tcase->table);
295
296         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
297
298         return NT_STATUS_IS_OK(status);
299 }
300
301
302
303 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_anon_rpc_iface_tcase(struct torture_suite *suite, 
304                                                                 const char *name,
305                                                                 const struct ndr_interface_table *table)
306 {
307         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
308
309         torture_suite_init_rpc_tcase(suite, tcase, name, table);
310
311         tcase->tcase.setup = torture_rpc_setup_anonymous;
312         tcase->tcase.teardown = torture_rpc_teardown;
313
314         return tcase;
315 }
316
317
318 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_rpc_iface_tcase(struct torture_suite *suite, 
319                                                                 const char *name,
320                                                                 const struct ndr_interface_table *table)
321 {
322         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
323
324         torture_suite_init_rpc_tcase(suite, tcase, name, table);
325
326         tcase->tcase.setup = torture_rpc_setup;
327         tcase->tcase.teardown = torture_rpc_teardown;
328
329         return tcase;
330 }
331
332 static bool torture_rpc_wrap_test(struct torture_context *tctx, 
333                                                                   struct torture_tcase *tcase,
334                                                                   struct torture_test *test)
335 {
336         bool (*fn) (struct torture_context *, struct dcerpc_pipe *);
337         struct torture_rpc_tcase_data *tcase_data = 
338                 (struct torture_rpc_tcase_data *)tcase->data;
339
340         fn = test->fn;
341
342         return fn(tctx, tcase_data->pipe);
343 }
344
345 static bool torture_rpc_wrap_test_ex(struct torture_context *tctx, 
346                                                                   struct torture_tcase *tcase,
347                                                                   struct torture_test *test)
348 {
349         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, const void *);
350         struct torture_rpc_tcase_data *tcase_data = 
351                 (struct torture_rpc_tcase_data *)tcase->data;
352
353         fn = test->fn;
354
355         return fn(tctx, tcase_data->pipe, test->data);
356 }
357
358
359 static bool torture_rpc_wrap_test_creds(struct torture_context *tctx, 
360                                                                   struct torture_tcase *tcase,
361                                                                   struct torture_test *test)
362 {
363         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *);
364         struct torture_rpc_tcase_data *tcase_data = 
365                 (struct torture_rpc_tcase_data *)tcase->data;
366
367         fn = test->fn;
368
369         return fn(tctx, tcase_data->pipe, tcase_data->credentials);
370 }
371
372 static bool torture_rpc_wrap_test_join(struct torture_context *tctx,
373                                        struct torture_tcase *tcase,
374                                        struct torture_test *test)
375 {
376         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *, struct test_join *);
377         struct torture_rpc_tcase_data *tcase_data =
378                 (struct torture_rpc_tcase_data *)tcase->data;
379
380         fn = test->fn;
381
382         return fn(tctx, tcase_data->pipe, tcase_data->credentials, tcase_data->join_ctx);
383 }
384
385 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test(
386                                         struct torture_rpc_tcase *tcase, 
387                                         const char *name, 
388                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *))
389 {
390         struct torture_test *test;
391
392         test = talloc(tcase, struct torture_test);
393
394         test->name = talloc_strdup(test, name);
395         test->description = NULL;
396         test->run = torture_rpc_wrap_test;
397         test->dangerous = false;
398         test->data = NULL;
399         test->fn = fn;
400
401         DLIST_ADD(tcase->tcase.tests, test);
402
403         return test;
404 }
405
406 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_creds(
407                                         struct torture_rpc_tcase *tcase, 
408                                         const char *name, 
409                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *))
410 {
411         struct torture_test *test;
412
413         test = talloc(tcase, struct torture_test);
414
415         test->name = talloc_strdup(test, name);
416         test->description = NULL;
417         test->run = torture_rpc_wrap_test_creds;
418         test->dangerous = false;
419         test->data = NULL;
420         test->fn = fn;
421
422         DLIST_ADD(tcase->tcase.tests, test);
423
424         return test;
425 }
426
427 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_join(
428                                         struct torture_rpc_tcase *tcase,
429                                         const char *name,
430                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *,
431                                                     struct cli_credentials *, struct test_join *))
432 {
433         struct torture_test *test;
434
435         test = talloc(tcase, struct torture_test);
436
437         test->name = talloc_strdup(test, name);
438         test->description = NULL;
439         test->run = torture_rpc_wrap_test_join;
440         test->dangerous = false;
441         test->data = NULL;
442         test->fn = fn;
443
444         DLIST_ADD(tcase->tcase.tests, test);
445
446         return test;
447 }
448
449 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_ex(
450                                         struct torture_rpc_tcase *tcase, 
451                                         const char *name, 
452                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *,
453                                                                 void *),
454                                         void *userdata)
455 {
456         struct torture_test *test;
457
458         test = talloc(tcase, struct torture_test);
459
460         test->name = talloc_strdup(test, name);
461         test->description = NULL;
462         test->run = torture_rpc_wrap_test_ex;
463         test->dangerous = false;
464         test->data = userdata;
465         test->fn = fn;
466
467         DLIST_ADD(tcase->tcase.tests, test);
468
469         return test;
470 }
471
472 NTSTATUS torture_rpc_init(void)
473 {
474         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "rpc");
475
476         ndr_table_init();
477
478         torture_suite_add_simple_test(suite, "lsa", torture_rpc_lsa);
479         torture_suite_add_simple_test(suite, "lsalookup", torture_rpc_lsa_lookup);
480         torture_suite_add_simple_test(suite, "lsa-getuser", torture_rpc_lsa_get_user);
481         torture_suite_add_suite(suite, torture_rpc_lsa_lookup_sids(suite));
482         torture_suite_add_suite(suite, torture_rpc_lsa_lookup_names(suite));
483         torture_suite_add_suite(suite, torture_rpc_lsa_secrets(suite));
484         torture_suite_add_suite(suite, torture_rpc_lsa_trusted_domains(suite));
485         torture_suite_add_suite(suite, torture_rpc_lsa_forest_trust(suite));
486         torture_suite_add_suite(suite, torture_rpc_lsa_privileges(suite));
487         torture_suite_add_suite(suite, torture_rpc_echo(suite));
488         torture_suite_add_suite(suite, torture_rpc_dfs(suite));
489         torture_suite_add_suite(suite, torture_rpc_frsapi(suite));
490         torture_suite_add_suite(suite, torture_rpc_unixinfo(suite));
491         torture_suite_add_suite(suite, torture_rpc_eventlog(suite));
492         torture_suite_add_suite(suite, torture_rpc_atsvc(suite));
493         torture_suite_add_suite(suite, torture_rpc_wkssvc(suite));
494         torture_suite_add_suite(suite, torture_rpc_handles(suite));
495         torture_suite_add_suite(suite, torture_rpc_object_uuid(suite));
496         torture_suite_add_suite(suite, torture_rpc_winreg(suite));
497         torture_suite_add_suite(suite, torture_rpc_spoolss(suite));
498 #ifdef WITH_NTVFS_FILESERVER
499         torture_suite_add_suite(suite, torture_rpc_spoolss_notify(suite));
500 #endif
501         torture_suite_add_suite(suite, torture_rpc_spoolss_win(suite));
502         torture_suite_add_suite(suite, torture_rpc_spoolss_driver(suite));
503         torture_suite_add_suite(suite, torture_rpc_spoolss_access(suite));
504         torture_suite_add_simple_test(suite, "samr", torture_rpc_samr);
505         torture_suite_add_simple_test(suite, "samr.users", torture_rpc_samr_users);
506         torture_suite_add_simple_test(suite, "samr.passwords", torture_rpc_samr_passwords);
507         torture_suite_add_suite(suite, torture_rpc_netlogon(suite));
508         torture_suite_add_suite(suite, torture_rpc_netlogon_s3(suite));
509         torture_suite_add_suite(suite, torture_rpc_netlogon_admin(suite));
510         torture_suite_add_suite(suite, torture_rpc_remote_pac(suite));
511         torture_suite_add_simple_test(suite, "samlogon", torture_rpc_samlogon);
512         torture_suite_add_simple_test(suite, "samsync", torture_rpc_samsync);
513         torture_suite_add_simple_test(suite, "schannel", torture_rpc_schannel);
514         torture_suite_add_simple_test(suite, "schannel2", torture_rpc_schannel2);
515         torture_suite_add_simple_test(suite, "bench-schannel1", torture_rpc_schannel_bench1);
516         torture_suite_add_simple_test(suite, "schannel_anon_setpw", torture_rpc_schannel_anon_setpw);
517         torture_suite_add_suite(suite, torture_rpc_srvsvc(suite));
518         torture_suite_add_suite(suite, torture_rpc_svcctl(suite));
519         torture_suite_add_suite(suite, torture_rpc_samr_accessmask(suite));
520         torture_suite_add_suite(suite, torture_rpc_samr_workstation_auth(suite));
521         torture_suite_add_suite(suite, torture_rpc_samr_passwords_pwdlastset(suite));
522         torture_suite_add_suite(suite, torture_rpc_samr_passwords_badpwdcount(suite));
523         torture_suite_add_suite(suite, torture_rpc_samr_passwords_lockout(suite));
524         torture_suite_add_suite(suite, torture_rpc_samr_passwords_validate(suite));
525         torture_suite_add_suite(suite, torture_rpc_samr_user_privileges(suite));
526         torture_suite_add_suite(suite, torture_rpc_samr_large_dc(suite));
527         torture_suite_add_suite(suite, torture_rpc_samr_priv(suite));
528         torture_suite_add_suite(suite, torture_rpc_epmapper(suite));
529         torture_suite_add_suite(suite, torture_rpc_initshutdown(suite));
530         torture_suite_add_suite(suite, torture_rpc_oxidresolve(suite));
531         torture_suite_add_suite(suite, torture_rpc_remact(suite));
532         torture_suite_add_simple_test(suite, "mgmt", torture_rpc_mgmt);
533         torture_suite_add_simple_test(suite, "scanner", torture_rpc_scanner);
534         torture_suite_add_simple_test(suite, "autoidl", torture_rpc_autoidl);
535         torture_suite_add_simple_test(suite, "countcalls", torture_rpc_countcalls);
536         torture_suite_add_simple_test(suite, "authcontext", torture_bind_authcontext);
537         torture_suite_add_suite(suite, torture_rpc_samba3(suite));
538         torture_rpc_drsuapi_tcase(suite);
539         torture_rpc_drsuapi_w2k8_tcase(suite);
540         torture_rpc_drsuapi_cracknames_tcase(suite);
541         torture_suite_add_suite(suite, torture_rpc_dssetup(suite));
542         torture_suite_add_suite(suite, torture_rpc_browser(suite));
543         torture_suite_add_simple_test(suite, "altercontext", torture_rpc_alter_context);
544         torture_suite_add_simple_test(suite, "join", torture_rpc_join);
545         torture_drs_rpc_dsgetinfo_tcase(suite);
546         torture_suite_add_simple_test(suite, "bench-rpc", torture_bench_rpc);
547         torture_suite_add_simple_test(suite, "asyncbind", torture_async_bind);
548         torture_suite_add_suite(suite, torture_rpc_ntsvcs(suite));
549         torture_suite_add_suite(suite, torture_rpc_bind(suite));
550 #ifdef AD_DC_BUILD_IS_ENABLED
551         torture_suite_add_suite(suite, torture_rpc_backupkey(suite));
552 #endif
553         torture_suite_add_suite(suite, torture_rpc_fsrvp(suite));
554         torture_suite_add_suite(suite, torture_rpc_clusapi(suite));
555         torture_suite_add_suite(suite, torture_rpc_witness(suite));
556
557         suite->description = talloc_strdup(suite, "DCE/RPC protocol and interface tests");
558
559         torture_register_suite(suite);
560
561         return NT_STATUS_OK;
562 }