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