torture: add new test RPC-SAMR-PASSWORDS-LOCKOUT
[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 "lib/cmdline/popt_common.h"
23 #include "torture/rpc/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(tctx->lp_ctx);
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                 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, tctx->ev, tctx->lp_ctx);
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_workstation(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_WSTRUST,
142                                                    &tcase_data->credentials);
143         if (tcase_data->join_ctx == NULL)
144             torture_fail(tctx, "Failed to join as WORKSTATION");
145
146         status = dcerpc_pipe_connect_b(tctx,
147                                 &(tcase_data->pipe),
148                                 binding,
149                                 tcase->table,
150                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
151
152         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
153
154         return true;
155 }
156
157 static bool torture_rpc_setup_machine_bdc(struct torture_context *tctx,
158                                           void **data)
159 {
160         NTSTATUS status;
161         struct dcerpc_binding *binding;
162         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
163                                                 struct torture_rpc_tcase);
164         struct torture_rpc_tcase_data *tcase_data;
165
166         status = torture_rpc_binding(tctx, &binding);
167         if (NT_STATUS_IS_ERR(status))
168                 return false;
169
170         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
171         tcase_data->credentials = cmdline_credentials;
172         tcase_data->join_ctx = torture_join_domain(tctx, tcase->machine_name,
173                                                    ACB_SVRTRUST, 
174                                                    &tcase_data->credentials);
175         if (tcase_data->join_ctx == NULL)
176             torture_fail(tctx, "Failed to join as BDC");
177
178         status = dcerpc_pipe_connect_b(tctx, 
179                                 &(tcase_data->pipe),
180                                 binding,
181                                 tcase->table,
182                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
183
184         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
185
186         return true;
187 }
188
189 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_machine_workstation_rpc_iface_tcase(
190                                 struct torture_suite *suite,
191                                 const char *name,
192                                 const struct ndr_interface_table *table,
193                                 const char *machine_name)
194 {
195         struct torture_rpc_tcase *tcase = talloc(suite,
196                                                  struct torture_rpc_tcase);
197
198         torture_suite_init_rpc_tcase(suite, tcase, name, table);
199
200         tcase->machine_name = talloc_strdup(tcase, machine_name);
201         tcase->tcase.setup = torture_rpc_setup_machine_workstation;
202         tcase->tcase.teardown = torture_rpc_teardown;
203
204         return tcase;
205 }
206
207 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_machine_bdc_rpc_iface_tcase(
208                                 struct torture_suite *suite, 
209                                 const char *name,
210                                 const struct ndr_interface_table *table,
211                                 const char *machine_name)
212 {
213         struct torture_rpc_tcase *tcase = talloc(suite, 
214                                                  struct torture_rpc_tcase);
215
216         torture_suite_init_rpc_tcase(suite, tcase, name, table);
217
218         tcase->machine_name = talloc_strdup(tcase, machine_name);
219         tcase->tcase.setup = torture_rpc_setup_machine_bdc;
220         tcase->tcase.teardown = torture_rpc_teardown;
221
222         return tcase;
223 }
224
225 _PUBLIC_ bool torture_suite_init_rpc_tcase(struct torture_suite *suite, 
226                                            struct torture_rpc_tcase *tcase, 
227                                            const char *name,
228                                            const struct ndr_interface_table *table)
229 {
230         if (!torture_suite_init_tcase(suite, (struct torture_tcase *)tcase, name))
231                 return false;
232
233         tcase->table = table;
234
235         return true;
236 }
237
238 static bool torture_rpc_setup_anonymous(struct torture_context *tctx, 
239                                         void **data)
240 {
241         NTSTATUS status;
242         struct dcerpc_binding *binding;
243         struct torture_rpc_tcase_data *tcase_data;
244         struct torture_rpc_tcase *tcase = talloc_get_type(tctx->active_tcase, 
245                                                           struct torture_rpc_tcase);
246
247         status = torture_rpc_binding(tctx, &binding);
248         if (NT_STATUS_IS_ERR(status))
249                 return false;
250
251         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
252         tcase_data->credentials = cli_credentials_init_anon(tctx);
253
254         status = dcerpc_pipe_connect_b(tctx, 
255                                 &(tcase_data->pipe),
256                                 binding,
257                                 tcase->table,
258                                 tcase_data->credentials, tctx->ev, tctx->lp_ctx);
259
260         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
261
262         return true;
263 }
264
265 static bool torture_rpc_setup (struct torture_context *tctx, void **data)
266 {
267         NTSTATUS status;
268         struct torture_rpc_tcase *tcase = talloc_get_type(
269                                                 tctx->active_tcase, struct torture_rpc_tcase);
270         struct torture_rpc_tcase_data *tcase_data;
271
272         *data = tcase_data = talloc_zero(tctx, struct torture_rpc_tcase_data);
273         tcase_data->credentials = cmdline_credentials;
274         
275         status = torture_rpc_connection(tctx, 
276                                 &(tcase_data->pipe),
277                                 tcase->table);
278
279         torture_assert_ntstatus_ok(tctx, status, "Error connecting to server");
280
281         return true;
282 }
283
284
285
286 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_anon_rpc_iface_tcase(struct torture_suite *suite, 
287                                                                 const char *name,
288                                                                 const struct ndr_interface_table *table)
289 {
290         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
291
292         torture_suite_init_rpc_tcase(suite, tcase, name, table);
293
294         tcase->tcase.setup = torture_rpc_setup_anonymous;
295         tcase->tcase.teardown = torture_rpc_teardown;
296
297         return tcase;
298 }
299
300
301 _PUBLIC_ struct torture_rpc_tcase *torture_suite_add_rpc_iface_tcase(struct torture_suite *suite, 
302                                                                 const char *name,
303                                                                 const struct ndr_interface_table *table)
304 {
305         struct torture_rpc_tcase *tcase = talloc(suite, struct torture_rpc_tcase);
306
307         torture_suite_init_rpc_tcase(suite, tcase, name, table);
308
309         tcase->tcase.setup = torture_rpc_setup;
310         tcase->tcase.teardown = torture_rpc_teardown;
311
312         return tcase;
313 }
314
315 static bool torture_rpc_wrap_test(struct torture_context *tctx, 
316                                                                   struct torture_tcase *tcase,
317                                                                   struct torture_test *test)
318 {
319         bool (*fn) (struct torture_context *, struct dcerpc_pipe *);
320         struct torture_rpc_tcase_data *tcase_data = 
321                 (struct torture_rpc_tcase_data *)tcase->data;
322
323         fn = test->fn;
324
325         return fn(tctx, tcase_data->pipe);
326 }
327
328 static bool torture_rpc_wrap_test_ex(struct torture_context *tctx, 
329                                                                   struct torture_tcase *tcase,
330                                                                   struct torture_test *test)
331 {
332         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, const void *);
333         struct torture_rpc_tcase_data *tcase_data = 
334                 (struct torture_rpc_tcase_data *)tcase->data;
335
336         fn = test->fn;
337
338         return fn(tctx, tcase_data->pipe, test->data);
339 }
340
341
342 static bool torture_rpc_wrap_test_creds(struct torture_context *tctx, 
343                                                                   struct torture_tcase *tcase,
344                                                                   struct torture_test *test)
345 {
346         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *);
347         struct torture_rpc_tcase_data *tcase_data = 
348                 (struct torture_rpc_tcase_data *)tcase->data;
349
350         fn = test->fn;
351
352         return fn(tctx, tcase_data->pipe, tcase_data->credentials);
353 }
354
355 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test(
356                                         struct torture_rpc_tcase *tcase, 
357                                         const char *name, 
358                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *))
359 {
360         struct torture_test *test;
361
362         test = talloc(tcase, struct torture_test);
363
364         test->name = talloc_strdup(test, name);
365         test->description = NULL;
366         test->run = torture_rpc_wrap_test;
367         test->dangerous = false;
368         test->data = NULL;
369         test->fn = fn;
370
371         DLIST_ADD(tcase->tcase.tests, test);
372
373         return test;
374 }
375
376 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_creds(
377                                         struct torture_rpc_tcase *tcase, 
378                                         const char *name, 
379                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *, struct cli_credentials *))
380 {
381         struct torture_test *test;
382
383         test = talloc(tcase, struct torture_test);
384
385         test->name = talloc_strdup(test, name);
386         test->description = NULL;
387         test->run = torture_rpc_wrap_test_creds;
388         test->dangerous = false;
389         test->data = NULL;
390         test->fn = fn;
391
392         DLIST_ADD(tcase->tcase.tests, test);
393
394         return test;
395 }
396
397 _PUBLIC_ struct torture_test *torture_rpc_tcase_add_test_ex(
398                                         struct torture_rpc_tcase *tcase, 
399                                         const char *name, 
400                                         bool (*fn) (struct torture_context *, struct dcerpc_pipe *,
401                                                                 void *),
402                                         void *userdata)
403 {
404         struct torture_test *test;
405
406         test = talloc(tcase, struct torture_test);
407
408         test->name = talloc_strdup(test, name);
409         test->description = NULL;
410         test->run = torture_rpc_wrap_test_ex;
411         test->dangerous = false;
412         test->data = userdata;
413         test->fn = fn;
414
415         DLIST_ADD(tcase->tcase.tests, test);
416
417         return test;
418 }
419
420 NTSTATUS torture_rpc_init(void)
421 {
422         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "RPC");
423
424         ndr_table_init();
425
426         torture_suite_add_simple_test(suite, "LSA", torture_rpc_lsa);
427         torture_suite_add_simple_test(suite, "LSALOOKUP", torture_rpc_lsa_lookup);
428         torture_suite_add_simple_test(suite, "LSA-GETUSER", torture_rpc_lsa_get_user);
429         torture_suite_add_suite(suite, torture_rpc_lsa_lookup_sids(suite));
430         torture_suite_add_suite(suite, torture_rpc_lsa_lookup_names(suite));
431         torture_suite_add_suite(suite, torture_rpc_lsa_secrets(suite));
432         torture_suite_add_suite(suite, torture_rpc_lsa_trusted_domains(suite));
433         torture_suite_add_suite(suite, torture_rpc_lsa_privileges(suite));
434         torture_suite_add_suite(suite, torture_rpc_echo(suite));
435         torture_suite_add_simple_test(suite, "DFS", torture_rpc_dfs);
436         torture_suite_add_suite(suite, torture_rpc_frsapi(suite));
437         torture_suite_add_suite(suite, torture_rpc_unixinfo(suite));
438         torture_suite_add_suite(suite, torture_rpc_eventlog(suite));
439         torture_suite_add_suite(suite, torture_rpc_atsvc(suite));
440         torture_suite_add_suite(suite, torture_rpc_wkssvc(suite));
441         torture_suite_add_suite(suite, torture_rpc_handles(suite));
442         torture_suite_add_suite(suite, torture_rpc_object_uuid(suite));
443         torture_suite_add_suite(suite, torture_rpc_winreg(suite));
444         torture_suite_add_simple_test(suite, "SPOOLSS", torture_rpc_spoolss);
445         torture_suite_add_suite(suite, torture_rpc_spoolss_notify(suite));
446         torture_suite_add_suite(suite, torture_rpc_spoolss_win(suite));
447         torture_suite_add_suite(suite, torture_rpc_spoolss_printer(suite));
448         torture_suite_add_simple_test(suite, "SAMR", torture_rpc_samr);
449         torture_suite_add_simple_test(suite, "SAMR-USERS", torture_rpc_samr_users);
450         torture_suite_add_simple_test(suite, "SAMR-PASSWORDS", torture_rpc_samr_passwords);
451         torture_suite_add_suite(suite, torture_rpc_netlogon(suite));
452         torture_suite_add_suite(suite, torture_rpc_netlogon_s3(suite));
453         torture_suite_add_suite(suite, torture_rpc_netlogon_admin(suite));
454         torture_suite_add_suite(suite, torture_rpc_remote_pac(suite));
455         torture_suite_add_simple_test(suite, "SAMLOGON", torture_rpc_samlogon);
456         torture_suite_add_simple_test(suite, "SAMSYNC", torture_rpc_samsync);
457         torture_suite_add_simple_test(suite, "SCHANNEL", torture_rpc_schannel);
458         torture_suite_add_simple_test(suite, "SCHANNEL2", torture_rpc_schannel2);
459         torture_suite_add_simple_test(suite, "BENCH-SCHANNEL1", torture_rpc_schannel_bench1);
460         torture_suite_add_suite(suite, torture_rpc_srvsvc(suite));
461         torture_suite_add_suite(suite, torture_rpc_svcctl(suite));
462         torture_suite_add_suite(suite, torture_rpc_samr_accessmask(suite));
463         torture_suite_add_suite(suite, torture_rpc_samr_workstation_auth(suite));
464         torture_suite_add_suite(suite, torture_rpc_samr_passwords_pwdlastset(suite));
465         torture_suite_add_suite(suite, torture_rpc_samr_passwords_badpwdcount(suite));
466         torture_suite_add_suite(suite, torture_rpc_samr_passwords_lockout(suite));
467         torture_suite_add_suite(suite, torture_rpc_samr_user_privileges(suite));
468         torture_suite_add_suite(suite, torture_rpc_samr_large_dc(suite));
469         torture_suite_add_suite(suite, torture_rpc_epmapper(suite));
470         torture_suite_add_suite(suite, torture_rpc_initshutdown(suite));
471         torture_suite_add_suite(suite, torture_rpc_oxidresolve(suite));
472         torture_suite_add_suite(suite, torture_rpc_remact(suite));
473         torture_suite_add_simple_test(suite, "MGMT", torture_rpc_mgmt);
474         torture_suite_add_simple_test(suite, "SCANNER", torture_rpc_scanner);
475         torture_suite_add_simple_test(suite, "AUTOIDL", torture_rpc_autoidl);
476         torture_suite_add_simple_test(suite, "COUNTCALLS", torture_rpc_countcalls);
477         torture_suite_add_simple_test(suite, "MULTIBIND", torture_multi_bind);
478         torture_suite_add_simple_test(suite, "AUTHCONTEXT", torture_bind_authcontext);
479         torture_suite_add_simple_test(suite, "BINDSAMBA3", torture_bind_samba3);
480         torture_suite_add_simple_test(suite, "NETLOGSAMBA3", torture_netlogon_samba3);
481         torture_suite_add_simple_test(suite, "SAMBA3SESSIONKEY", torture_samba3_sessionkey);
482         torture_suite_add_simple_test(suite, "SAMBA3-SRVSVC", torture_samba3_rpc_srvsvc);
483         torture_suite_add_simple_test(suite, "SAMBA3-SHARESEC",
484                             torture_samba3_rpc_sharesec);
485         torture_suite_add_simple_test(suite, "SAMBA3-GETUSERNAME",
486                             torture_samba3_rpc_getusername);
487         torture_suite_add_simple_test(suite, "SAMBA3-RANDOMAUTH2",
488                                       torture_samba3_rpc_randomauth2);
489         torture_suite_add_simple_test(suite, "SAMBA3-LSA", torture_samba3_rpc_lsa);
490         torture_suite_add_simple_test(suite, "SAMBA3-SPOOLSS", torture_samba3_rpc_spoolss);
491         torture_suite_add_simple_test(suite, "SAMBA3-WKSSVC", torture_samba3_rpc_wkssvc);
492         torture_suite_add_simple_test(suite, "SAMBA3-WINREG", torture_samba3_rpc_winreg);
493         torture_suite_add_simple_test(suite, "SAMBA3-GETALIASMEMBERSHIP-0",
494                                       torture_samba3_getaliasmembership_0);
495         torture_rpc_drsuapi_tcase(suite);
496         torture_rpc_drsuapi_cracknames_tcase(suite);
497         torture_suite_add_suite(suite, torture_rpc_dssetup(suite));
498         torture_suite_add_suite(suite, torture_rpc_browser(suite));
499         torture_suite_add_simple_test(suite, "SAMBA3-REGCONFIG", torture_samba3_regconfig);
500         torture_suite_add_simple_test(suite, "ALTERCONTEXT", torture_rpc_alter_context);
501         torture_suite_add_simple_test(suite, "JOIN", torture_rpc_join);
502         torture_drs_rpc_dssync_tcase(suite);
503         torture_drs_rpc_dsgetinfo_tcase(suite);
504         torture_suite_add_simple_test(suite, "BENCH-RPC", torture_bench_rpc);
505         torture_suite_add_simple_test(suite, "ASYNCBIND", torture_async_bind);
506         torture_suite_add_suite(suite, torture_rpc_ntsvcs(suite));
507
508         suite->description = talloc_strdup(suite, "DCE/RPC protocol and interface tests");
509
510         torture_register_suite(suite);
511
512         return NT_STATUS_OK;
513 }