c0c75ab21abf07df030fae05fd1f214a49b80805
[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 "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 static bool torture_rpc_teardown (struct torture_context *tcase, 
31                                           void *data)
32 {
33         struct torture_rpc_tcase_data *tcase_data = 
34                 (struct torture_rpc_tcase_data *)data;
35         if (tcase_data->join_ctx != NULL)
36             torture_leave_domain(tcase_data->join_ctx);
37         talloc_free(tcase_data);
38         return true;
39 }
40
41 /**
42  * Obtain the DCE/RPC binding context associated with a torture context.
43  *
44  * @param tctx Torture context
45  * @param binding Pointer to store DCE/RPC binding
46  */
47 NTSTATUS torture_rpc_binding(struct torture_context *tctx,
48                              struct dcerpc_binding **binding)
49 {
50         NTSTATUS status;
51         const char *binding_string = torture_setting_string(tctx, "binding", 
52                                                             NULL);
53
54         if (binding_string == NULL) {
55                 torture_comment(tctx, 
56                                 "You must specify a DCE/RPC binding string\n");
57                 return NT_STATUS_INVALID_PARAMETER;
58         }
59
60         status = dcerpc_parse_binding(tctx, binding_string, binding);
61         if (NT_STATUS_IS_ERR(status)) {
62                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", 
63                          binding_string));
64                 return status;
65         }
66
67         return NT_STATUS_OK;    
68 }
69
70 /**
71  * open a rpc connection to the chosen binding string
72  */
73 _PUBLIC_ NTSTATUS torture_rpc_connection(struct torture_context *tctx,
74                                 struct dcerpc_pipe **p, 
75                                 const struct ndr_interface_table *table)
76 {
77         NTSTATUS status;
78         struct dcerpc_binding *binding;
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, NULL);
87  
88         if (NT_STATUS_IS_ERR(status)) {
89                 printf("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 {
105         NTSTATUS status;
106         struct dcerpc_binding *binding;
107
108         status = torture_rpc_binding(tctx, &binding);
109         if (NT_STATUS_IS_ERR(status))
110                 return status;
111
112         binding->transport = transport;
113         binding->assoc_group_id = assoc_group_id;
114
115         status = dcerpc_pipe_connect_b(tctx, p, binding, table,
116                                        cmdline_credentials, NULL);
117                                            
118         if (NT_STATUS_IS_ERR(status)) {
119                 *p = NULL;
120         }
121
122         return status;
123 }
124
125 static bool torture_rpc_setup_machine(struct torture_context *tctx,
126                                       void **data)
127 {
128         NTSTATUS status;
129         struct dcerpc_binding *binding;
130         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
131                                                 struct torture_rpc_tcase);
132         struct torture_rpc_tcase_data *tcase_data;
133
134         status = torture_rpc_binding(tctx, &binding);
135         if (NT_STATUS_IS_ERR(status))
136                 return false;
137
138         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
139         tcase_data->credentials = cmdline_credentials;
140         tcase_data->join_ctx = torture_join_domain(tctx, tcase->machine_name,
141                                                    ACB_SVRTRUST, 
142                                                    &tcase_data->credentials);
143         if (tcase_data->join_ctx == NULL)
144             torture_fail(tctx, "Failed to join as BDC");
145
146         status = dcerpc_pipe_connect_b(tctx, 
147                                 &(tcase_data->pipe),
148                                 binding,
149                                 tcase->table,
150                                 tcase_data->credentials, NULL);
151
152         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
153
154         return true;
155 }
156
157 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_machine_rpc_iface_tcase(
158                                 struct torture_suite *suite, 
159                                 const char *name,
160                                 const struct ndr_interface_table *table,
161                                 const char *machine_name)
162 {
163         struct torture_rpc_tcase *tcase = talloc(suite, 
164                                                  struct torture_rpc_tcase);
165
166         torture_suite_init_rpc_tcase(suite, tcase, name, table);
167
168         tcase->machine_name = talloc_strdup(tcase, machine_name);
169         tcase->tcase.setup = torture_rpc_setup_machine;
170         tcase->tcase.teardown = torture_rpc_teardown;
171
172         return tcase;
173 }
174
175 _PUBLIC_ bool torture_suite_init_rpc_tcase(struct torture_suite *suite, 
176                                            struct torture_rpc_tcase *tcase, 
177                                            const char *name,
178                                            const struct ndr_interface_table *table)
179 {
180         if (!torture_suite_init_tcase(suite, (struct torture_tcase *)tcase, name))
181                 return false;
182
183         tcase->table = table;
184
185         return true;
186 }
187
188 static bool torture_rpc_setup_anonymous(struct torture_context *tctx, 
189                                         void **data)
190 {
191         NTSTATUS status;
192         struct dcerpc_binding *binding;
193         struct torture_rpc_tcase_data *tcase_data;
194         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
195                                                           struct torture_rpc_tcase);
196
197         status = torture_rpc_binding(tctx, &binding);
198         if (NT_STATUS_IS_ERR(status))
199                 return false;
200
201         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
202         tcase_data->credentials = cli_credentials_init_anon(tctx);
203
204         status = dcerpc_pipe_connect_b(tctx, 
205                                 &(tcase_data->pipe),
206                                 binding,
207                                 tcase->table,
208                                 tcase_data->credentials, NULL);
209
210         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
211
212         return true;
213 }
214
215 static bool torture_rpc_setup (struct torture_context *tctx, void **data)
216 {
217         NTSTATUS status;
218         struct torture_rpc_tcase *tcase = talloc_get_type(
219                                                 tctx->active_tcase, struct torture_rpc_tcase);
220         struct torture_rpc_tcase_data *tcase_data;
221
222         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
223         tcase_data->credentials = cmdline_credentials;
224         
225         status = torture_rpc_connection(tctx, 
226                                 &(tcase_data->pipe),
227                                 tcase->table);
228
229         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
230
231         return true;
232 }
233
234
235
236 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_anon_rpc_iface_tcase(struct torture_suite *suite, 
237                                                                 const char *name,
238                                                                 const struct ndr_interface_table *table)
239 {
240         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
241
242         torture_suite_init_rpc_tcase(suite, tcase, name, table);
243
244         tcase->tcase.setup = torture_rpc_setup_anonymous;
245         tcase->tcase.teardown = torture_rpc_teardown;
246
247         return tcase;
248 }
249
250
251 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_rpc_iface_tcase(struct torture_suite *suite, 
252                                                                 const char *name,
253                                                                 const struct ndr_interface_table *table)
254 {
255         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
256
257         torture_suite_init_rpc_tcase(suite, tcase, name, table);
258
259         tcase->tcase.setup = torture_rpc_setup;
260         tcase->tcase.teardown = torture_rpc_teardown;
261
262         return tcase;
263 }
264
265 static bool torture_rpc_wrap_test(struct torture_context *tctx, 
266                                                                   struct torture_tcase *tcase,
267                                                                   struct torture_test *test)
268 {
269         bool (*fn) (struct torture_context *, struct dcerpc_pipe *);
270         struct torture_rpc_tcase_data *tcase_data = 
271                 (struct torture_rpc_tcase_data *)tcase->data;
272
273         fn = test->fn;
274
275         return fn(tctx, tcase_data->pipe);
276 }
277
278 static bool torture_rpc_wrap_test_ex(struct torture_context *tctx, 
279                                                                   struct torture_tcase *tcase,
280                                                                   struct torture_test *test)
281 {
282         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, const void *);
283         struct torture_rpc_tcase_data *tcase_data = 
284                 (struct torture_rpc_tcase_data *)tcase->data;
285
286         fn = test->fn;
287
288         return fn(tctx, tcase_data->pipe, test->data);
289 }
290
291
292 static bool torture_rpc_wrap_test_creds(struct torture_context *tctx, 
293                                                                   struct torture_tcase *tcase,
294                                                                   struct torture_test *test)
295 {
296         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *);
297         struct torture_rpc_tcase_data *tcase_data = 
298                 (struct torture_rpc_tcase_data *)tcase->data;
299
300         fn = test->fn;
301
302         return fn(tctx, tcase_data->pipe, tcase_data->credentials);
303 }
304
305 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test(
306                                         struct torture_rpc_tcase *tcase, 
307                                         const char *name, 
308                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *))
309 {
310         struct torture_test *test;
311
312         test = talloc(tcase, struct torture_test);
313
314         test->name = talloc_strdup(test, name);
315         test->description = NULL;
316         test->run = torture_rpc_wrap_test;
317         test->dangerous = false;
318         test->data = NULL;
319         test->fn = fn;
320
321         DLIST_ADD(tcase->tcase.tests, test);
322
323         return test;
324 }
325
326 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_creds(
327                                         struct torture_rpc_tcase *tcase, 
328                                         const char *name, 
329                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *))
330 {
331         struct torture_test *test;
332
333         test = talloc(tcase, struct torture_test);
334
335         test->name = talloc_strdup(test, name);
336         test->description = NULL;
337         test->run = torture_rpc_wrap_test_creds;
338         test->dangerous = false;
339         test->data = NULL;
340         test->fn = fn;
341
342         DLIST_ADD(tcase->tcase.tests, test);
343
344         return test;
345 }
346
347 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_ex(
348                                         struct torture_rpc_tcase *tcase, 
349                                         const char *name, 
350                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *,
351                                                                 void *),
352                                         void *userdata)
353 {
354         struct torture_test *test;
355
356         test = talloc(tcase, struct torture_test);
357
358         test->name = talloc_strdup(test, name);
359         test->description = NULL;
360         test->run = torture_rpc_wrap_test_ex;
361         test->dangerous = false;
362         test->data = userdata;
363         test->fn = fn;
364
365         DLIST_ADD(tcase->tcase.tests, test);
366
367         return test;
368 }
369
370 NTSTATUS torture_rpc_init(void)
371 {
372         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "RPC");
373
374         dcerpc_init();
375
376         ndr_table_init();
377
378         torture_suite_add_simple_test(suite, "LSA", torture_rpc_lsa);
379         torture_suite_add_simple_test(suite, "LSALOOKUP", torture_rpc_lsa_lookup);
380         torture_suite_add_simple_test(suite, "LSA-GETUSER", torture_rpc_lsa_get_user);
381         torture_suite_add_suite(suite, torture_rpc_lsa_secrets(suite));
382         torture_suite_add_suite(suite, torture_rpc_echo(suite));
383         torture_suite_add_simple_test(suite, "DFS", torture_rpc_dfs);
384         torture_suite_add_suite(suite, torture_rpc_frsapi(suite));
385         torture_suite_add_suite(suite, torture_rpc_unixinfo(suite));
386         torture_suite_add_suite(suite, torture_rpc_eventlog(suite));
387         torture_suite_add_suite(suite, torture_rpc_atsvc(suite));
388         torture_suite_add_suite(suite, torture_rpc_wkssvc(suite));
389         torture_suite_add_suite(suite, torture_rpc_handles(suite));
390         torture_suite_add_suite(suite, torture_rpc_winreg(suite));
391         torture_suite_add_simple_test(suite, "SPOOLSS", torture_rpc_spoolss);
392         torture_suite_add_suite(suite, torture_rpc_spoolss_notify(suite));
393         torture_suite_add_simple_test(suite, "SAMR", torture_rpc_samr);
394         torture_suite_add_simple_test(suite, "SAMR-USERS", torture_rpc_samr_users);
395         torture_suite_add_simple_test(suite, "SAMR-PASSWORDS", torture_rpc_samr_passwords);
396         torture_suite_add_suite(suite, torture_rpc_netlogon(suite));
397         torture_suite_add_simple_test(suite, "SAMLOGON", torture_rpc_samlogon);
398         torture_suite_add_simple_test(suite, "SAMSYNC", torture_rpc_samsync);
399         torture_suite_add_simple_test(suite, "SCHANNEL", torture_rpc_schannel);
400         torture_suite_add_simple_test(suite, "SCHANNEL2", torture_rpc_schannel2);
401         torture_suite_add_suite(suite, torture_rpc_srvsvc(suite));
402         torture_suite_add_suite(suite, torture_rpc_svcctl(suite));
403         torture_suite_add_suite(suite, torture_rpc_samr_accessmask(suite));
404         torture_suite_add_suite(suite, torture_rpc_epmapper(suite));
405         torture_suite_add_suite(suite, torture_rpc_initshutdown(suite));
406         torture_suite_add_suite(suite, torture_rpc_oxidresolve(suite));
407         torture_suite_add_suite(suite, torture_rpc_remact(suite));
408         torture_suite_add_simple_test(suite, "MGMT", torture_rpc_mgmt);
409         torture_suite_add_simple_test(suite, "SCANNER", torture_rpc_scanner);
410         torture_suite_add_simple_test(suite, "AUTOIDL", torture_rpc_autoidl);
411         torture_suite_add_simple_test(suite, "COUNTCALLS", torture_rpc_countcalls);
412         torture_suite_add_simple_test(suite, "MULTIBIND", torture_multi_bind);
413         torture_suite_add_simple_test(suite, "AUTHCONTEXT", torture_bind_authcontext);
414         torture_suite_add_simple_test(suite, "BINDSAMBA3", torture_bind_samba3);
415         torture_suite_add_simple_test(suite, "NETLOGSAMBA3", torture_netlogon_samba3);
416         torture_suite_add_simple_test(suite, "SAMBA3SESSIONKEY", torture_samba3_sessionkey);
417         torture_suite_add_simple_test(suite, "SAMBA3-SRVSVC", torture_samba3_rpc_srvsvc);
418         torture_suite_add_simple_test(suite, "SAMBA3-SHARESEC",
419                             torture_samba3_rpc_sharesec);
420         torture_suite_add_simple_test(suite, "SAMBA3-GETUSERNAME",
421                             torture_samba3_rpc_getusername);
422         torture_suite_add_simple_test(suite, "SAMBA3-RANDOMAUTH2",
423                                       torture_samba3_rpc_randomauth2);
424         torture_suite_add_simple_test(suite, "SAMBA3-LSA", torture_samba3_rpc_lsa);
425         torture_suite_add_simple_test(suite, "SAMBA3-SPOOLSS", torture_samba3_rpc_spoolss);
426         torture_suite_add_simple_test(suite, "SAMBA3-WKSSVC", torture_samba3_rpc_wkssvc);
427         torture_suite_add_simple_test(suite, "SAMBA3-WINREG", torture_samba3_rpc_winreg);
428         torture_suite_add_simple_test(suite, "DRSUAPI", torture_rpc_drsuapi);
429         torture_suite_add_simple_test(suite, "CRACKNAMES", torture_rpc_drsuapi_cracknames);
430         torture_suite_add_suite(suite, torture_rpc_dssetup(suite));
431         torture_suite_add_simple_test(suite, "SAMBA3-REGCONFIG", torture_samba3_regconfig);
432         torture_suite_add_simple_test(suite, "ALTERCONTEXT", torture_rpc_alter_context);
433         torture_suite_add_simple_test(suite, "JOIN", torture_rpc_join);
434         torture_suite_add_simple_test(suite, "DSSYNC", torture_rpc_dssync);
435         torture_suite_add_simple_test(suite, "BENCH-RPC", torture_bench_rpc);
436         torture_suite_add_simple_test(suite, "ASYNCBIND", torture_async_bind);
437
438         suite->description = talloc_strdup(suite, "DCE/RPC protocol and interface tests");
439
440         torture_register_suite(suite);
441
442         return NT_STATUS_OK;
443 }