r24741: More use of the torture API.
[kai/samba.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 "auth/credentials/credentials.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "librpc/rpc/dcerpc.h"
25 #include "torture/rpc/rpc.h"
26 #include "torture/torture.h"
27 #include "librpc/ndr/ndr_table.h"
28 #include "lib/util/dlinklist.h"
29
30 struct torture_rpc_tcase {
31         struct torture_tcase tcase;
32         const struct ndr_interface_table *table;
33         struct dcerpc_pipe *pipe;
34 };
35
36 /* open a rpc connection to the chosen binding string */
37 _PUBLIC_ NTSTATUS torture_rpc_connection(struct torture_context *tctx,
38                                 struct dcerpc_pipe **p, 
39                                 const struct ndr_interface_table *table)
40 {
41         NTSTATUS status;
42         const char *binding = torture_setting_string(tctx, "binding", NULL);
43
44         if (!binding) {
45                 printf("You must specify a ncacn binding string\n");
46                 return NT_STATUS_INVALID_PARAMETER;
47         }
48
49         status = dcerpc_pipe_connect(tctx, 
50                                      p, binding, table,
51                                      cmdline_credentials, NULL);
52  
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("Failed to connect to remote server: %s %s\n", binding, nt_errstr(status));
55         }
56
57         return status;
58 }
59
60 /* open a rpc connection to a specific transport */
61 NTSTATUS torture_rpc_connection_transport(struct torture_context *tctx, 
62                                           struct dcerpc_pipe **p, 
63                                           const struct ndr_interface_table *table,
64                                           enum dcerpc_transport_t transport,
65                                           uint32_t assoc_group_id)
66 {
67         NTSTATUS status;
68         const char *binding = torture_setting_string(tctx, "binding", NULL);
69         struct dcerpc_binding *b;
70         TALLOC_CTX *mem_ctx = talloc_named(tctx, 0, "torture_rpc_connection_smb");
71
72         if (!binding) {
73                 printf("You must specify a ncacn binding string\n");
74                 talloc_free(mem_ctx);
75                 return NT_STATUS_INVALID_PARAMETER;
76         }
77
78         status = dcerpc_parse_binding(mem_ctx, binding, &b);
79         if (!NT_STATUS_IS_OK(status)) {
80                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
81                 talloc_free(mem_ctx);
82                 return status;
83         }
84
85         b->transport = transport;
86         b->assoc_group_id = assoc_group_id;
87
88         status = dcerpc_pipe_connect_b(mem_ctx, p, b, table,
89                                        cmdline_credentials, NULL);
90                                            
91         if (NT_STATUS_IS_OK(status)) {
92                 *p = talloc_reference(tctx, *p);
93         } else {
94                 *p = NULL;
95         }
96         talloc_free(mem_ctx);
97         return status;
98 }
99
100 static bool torture_rpc_setup_anonymous(struct torture_context *tctx, 
101                                                                                 void **data)
102 {
103         struct cli_credentials *anon_credentials;
104         NTSTATUS status;
105         const char *binding = torture_setting_string(tctx, "binding", NULL);
106         struct torture_rpc_tcase *tcase = talloc_get_type(
107                                                 tctx->active_tcase, struct torture_rpc_tcase);
108
109         anon_credentials = cli_credentials_init_anon(tctx);
110
111         status = dcerpc_pipe_connect(tctx, 
112                                 (struct dcerpc_pipe **)data, 
113                                 binding,
114                                 tcase->table,
115                                 anon_credentials, NULL);
116
117         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
118
119         return true;
120 }
121
122 static bool torture_rpc_setup (struct torture_context *tctx, void **data)
123 {
124         NTSTATUS status;
125         struct torture_rpc_tcase *tcase = talloc_get_type(
126                                                 tctx->active_tcase, struct torture_rpc_tcase);
127         
128         status = torture_rpc_connection(tctx, 
129                                 (struct dcerpc_pipe **)data, 
130                                 (const struct ndr_interface_table *)tcase->table);
131
132         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
133
134         return true;
135 }
136
137 static bool torture_rpc_teardown (struct torture_context *tcase, void *data)
138 {
139         talloc_free(data);
140         return true;
141 }
142
143 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_anon_rpc_iface_tcase(struct torture_suite *suite, 
144                                                                 const char *name,
145                                                                 const struct ndr_interface_table *table)
146 {
147         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
148
149         torture_suite_init_tcase(suite, (struct torture_tcase *)tcase, name);
150
151         tcase->tcase.setup = torture_rpc_setup_anonymous;
152         tcase->tcase.teardown = torture_rpc_teardown;
153         tcase->table = table;
154
155         return tcase;
156 }
157
158
159 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_rpc_iface_tcase(struct torture_suite *suite, 
160                                                                 const char *name,
161                                                                 const struct ndr_interface_table *table)
162 {
163         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
164
165         torture_suite_init_tcase(suite, (struct torture_tcase *)tcase, name);
166
167         tcase->tcase.setup = torture_rpc_setup;
168         tcase->tcase.teardown = torture_rpc_teardown;
169         tcase->table = table;
170
171         return tcase;
172 }
173
174 static bool torture_rpc_wrap_test(struct torture_context *tctx, 
175                                                                   struct torture_tcase *tcase,
176                                                                   struct torture_test *test)
177 {
178         bool (*fn) (struct torture_context *, struct dcerpc_pipe *);
179
180         fn = test->fn;
181
182         return fn(tctx, (struct dcerpc_pipe *)tcase->data);
183 }
184
185 static bool torture_rpc_wrap_test_ex(struct torture_context *tctx, 
186                                                                   struct torture_tcase *tcase,
187                                                                   struct torture_test *test)
188 {
189         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, const void *);
190
191         fn = test->fn;
192
193         return fn(tctx, (struct dcerpc_pipe *)tcase->data, test->data);
194 }
195
196 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test(
197                                         struct torture_rpc_tcase *tcase, 
198                                         const char *name, 
199                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *))
200 {
201         struct torture_test *test;
202
203         test = talloc(tcase, struct torture_test);
204
205         test->name = talloc_strdup(test, name);
206         test->description = NULL;
207         test->run = torture_rpc_wrap_test;
208         test->dangerous = false;
209         test->data = NULL;
210         test->fn = fn;
211
212         DLIST_ADD(tcase->tcase.tests, test);
213
214         return test;
215 }
216
217 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_ex(
218                                         struct torture_rpc_tcase *tcase, 
219                                         const char *name, 
220                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *,
221                                                                 void *),
222                                         void *userdata)
223 {
224         struct torture_test *test;
225
226         test = talloc(tcase, struct torture_test);
227
228         test->name = talloc_strdup(test, name);
229         test->description = NULL;
230         test->run = torture_rpc_wrap_test_ex;
231         test->dangerous = false;
232         test->data = userdata;
233         test->fn = fn;
234
235         DLIST_ADD(tcase->tcase.tests, test);
236
237         return test;
238 }
239
240 NTSTATUS torture_rpc_init(void)
241 {
242         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "RPC");
243
244         dcerpc_init();
245
246         ndr_table_init();
247
248         torture_suite_add_simple_test(suite, "LSA", torture_rpc_lsa);
249         torture_suite_add_simple_test(suite, "LSALOOKUP", torture_rpc_lsa_lookup);
250         torture_suite_add_simple_test(suite, "LSA-GETUSER", torture_rpc_lsa_get_user);
251         torture_suite_add_simple_test(suite, "SECRETS", torture_rpc_lsa_secrets);
252         torture_suite_add_suite(suite, torture_rpc_echo());
253         torture_suite_add_suite(suite, torture_rpc_dfs());
254         torture_suite_add_suite(suite, torture_rpc_unixinfo());
255         torture_suite_add_suite(suite, torture_rpc_eventlog());
256         torture_suite_add_suite(suite, torture_rpc_atsvc());
257         torture_suite_add_suite(suite, torture_rpc_wkssvc());
258         torture_suite_add_suite(suite, torture_rpc_handles(suite));
259         torture_suite_add_suite(suite, torture_rpc_winreg(suite));
260         torture_suite_add_simple_test(suite, "SPOOLSS", torture_rpc_spoolss);
261         torture_suite_add_simple_test(suite, "SAMR", torture_rpc_samr);
262         torture_suite_add_simple_test(suite, "SAMR-USERS", torture_rpc_samr_users);
263         torture_suite_add_simple_test(suite, "SAMR-PASSWORDS", torture_rpc_samr_passwords);
264         torture_suite_add_simple_test(suite, "NETLOGON", torture_rpc_netlogon);
265         torture_suite_add_simple_test(suite, "SAMLOGON", torture_rpc_samlogon);
266         torture_suite_add_simple_test(suite, "SAMSYNC", torture_rpc_samsync);
267         torture_suite_add_simple_test(suite, "SCHANNEL", torture_rpc_schannel);
268         torture_suite_add_simple_test(suite, "SCHANNEL2", torture_rpc_schannel2);
269         torture_suite_add_suite(suite, torture_rpc_srvsvc(suite));
270         torture_suite_add_suite(suite, torture_rpc_svcctl(suite));
271         torture_suite_add_simple_test(suite, "EPMAPPER", torture_rpc_epmapper);
272         torture_suite_add_simple_test(suite, "INITSHUTDOWN", torture_rpc_initshutdown);
273         torture_suite_add_simple_test(suite, "OXIDRESOLVE", torture_rpc_oxidresolve);
274         torture_suite_add_simple_test(suite, "REMACT", torture_rpc_remact);
275         torture_suite_add_simple_test(suite, "MGMT", torture_rpc_mgmt);
276         torture_suite_add_simple_test(suite, "SCANNER", torture_rpc_scanner);
277         torture_suite_add_simple_test(suite, "AUTOIDL", torture_rpc_autoidl);
278         torture_suite_add_simple_test(suite, "COUNTCALLS", torture_rpc_countcalls);
279         torture_suite_add_simple_test(suite, "MULTIBIND", torture_multi_bind);
280         torture_suite_add_simple_test(suite, "AUTHCONTEXT", torture_bind_authcontext);
281         torture_suite_add_simple_test(suite, "BINDSAMBA3", torture_bind_samba3);
282         torture_suite_add_simple_test(suite, "NETLOGSAMBA3", torture_netlogon_samba3);
283         torture_suite_add_simple_test(suite, "SAMBA3SESSIONKEY", torture_samba3_sessionkey);
284         torture_suite_add_simple_test(suite, "SAMBA3-SRVSVC", torture_samba3_rpc_srvsvc);
285         torture_suite_add_simple_test(suite, "SAMBA3-SHARESEC",
286                             torture_samba3_rpc_sharesec);
287         torture_suite_add_simple_test(suite, "SAMBA3-GETUSERNAME",
288                             torture_samba3_rpc_getusername);
289         torture_suite_add_simple_test(suite, "SAMBA3-LSA", torture_samba3_rpc_lsa);
290         torture_suite_add_simple_test(suite, "SAMBA3-SPOOLSS", torture_samba3_rpc_spoolss);
291         torture_suite_add_simple_test(suite, "SAMBA3-WKSSVC", torture_samba3_rpc_wkssvc);
292         torture_suite_add_simple_test(suite, "RPC-SAMBA3-WINREG", torture_samba3_rpc_winreg);
293         torture_suite_add_simple_test(suite, "DRSUAPI", torture_rpc_drsuapi);
294         torture_suite_add_simple_test(suite, "CRACKNAMES", torture_rpc_drsuapi_cracknames);
295         torture_suite_add_simple_test(suite, "ROT", torture_rpc_rot);
296         torture_suite_add_simple_test(suite, "DSSETUP", torture_rpc_dssetup);
297         torture_suite_add_simple_test(suite, "ALTERCONTEXT", torture_rpc_alter_context);
298         torture_suite_add_simple_test(suite, "JOIN", torture_rpc_join);
299         torture_suite_add_simple_test(suite, "DSSYNC", torture_rpc_dssync);
300         torture_suite_add_simple_test(suite, "BENCH-RPC", torture_bench_rpc);
301         torture_suite_add_simple_test(suite, "ASYNCBIND", torture_async_bind);
302
303         suite->description = talloc_strdup(suite, "DCE/RPC protocol and interface tests");
304
305         torture_register_suite(suite);
306
307         return NT_STATUS_OK;
308 }