libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source4 / torture / rpc / dfs.c
blob8eb4f9f8e2d113dbdf8ab36b4c6a9c56e6bf6e86
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for rpc dfs operations
5 Copyright (C) Andrew Tridgell 2003
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.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "torture/rpc/torture_rpc.h"
24 #include "librpc/gen_ndr/ndr_dfs_c.h"
25 #include "libnet/libnet.h"
26 #include "torture/util.h"
27 #include "libcli/libcli.h"
28 #include "lib/cmdline/popt_common.h"
30 #define SMBTORTURE_DFS_SHARENAME "smbtorture_dfs_share"
31 #define SMBTORTURE_DFS_DIRNAME "\\smbtorture_dfs_dir"
32 #define SMBTORTURE_DFS_PATHNAME "C:"SMBTORTURE_DFS_DIRNAME
34 #define IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(x,y)\
35 if (x == DFS_MANAGER_VERSION_W2K3) {\
36 if (!W_ERROR_EQUAL(y,WERR_NOT_SUPPORTED)) {\
37 printf("expected WERR_NOT_SUPPORTED\n");\
38 return false;\
40 return true;\
43 static bool test_NetShareAdd(struct torture_context *tctx,
44 const char *host,
45 const char *sharename,
46 const char *dir)
48 NTSTATUS status;
49 struct srvsvc_NetShareInfo2 i;
50 struct libnet_context* libnetctx;
51 struct libnet_AddShare r;
53 printf("Creating share %s\n", sharename);
55 if (!(libnetctx = libnet_context_init(tctx->ev, tctx->lp_ctx))) {
56 return false;
59 libnetctx->cred = cmdline_credentials;
61 i.name = sharename;
62 i.type = STYPE_DISKTREE;
63 i.path = dir;
64 i.max_users = (uint32_t) -1;
65 i.comment = "created by smbtorture";
66 i.password = NULL;
67 i.permissions = 0x0;
68 i.current_users = 0x0;
70 r.level = 2;
71 r.in.server_name = host;
72 r.in.share = i;
74 status = libnet_AddShare(libnetctx, tctx, &r);
75 if (!NT_STATUS_IS_OK(status)) {
76 d_printf("Failed to add new share: %s (%s)\n",
77 nt_errstr(status), r.out.error_string);
78 return false;
81 return true;
84 static bool test_NetShareDel(struct torture_context *tctx,
85 const char *host,
86 const char *sharename)
88 NTSTATUS status;
89 struct libnet_context* libnetctx;
90 struct libnet_DelShare r;
92 torture_comment(tctx, "Deleting share %s\n", sharename);
94 if (!(libnetctx = libnet_context_init(tctx->ev, tctx->lp_ctx))) {
95 return false;
98 libnetctx->cred = cmdline_credentials;
100 r.in.share_name = sharename;
101 r.in.server_name = host;
103 status = libnet_DelShare(libnetctx, tctx, &r);
104 if (!NT_STATUS_IS_OK(status)) {
105 d_printf("Failed to delete share: %s (%s)\n",
106 nt_errstr(status), r.out.error_string);
107 return false;
110 return true;
113 static bool test_CreateDir(TALLOC_CTX *mem_ctx,
114 struct smbcli_state **cli,
115 struct torture_context *tctx,
116 const char *host,
117 const char *share,
118 const char *dir)
120 printf("Creating directory %s\n", dir);
122 if (!torture_open_connection_share(mem_ctx, cli, tctx, host, share, tctx->ev)) {
123 return false;
126 if (!torture_setup_dir(*cli, dir)) {
127 return false;
130 return true;
133 static bool test_DeleteDir(struct torture_context *tctx,
134 struct smbcli_state *cli,
135 const char *dir)
137 torture_comment(tctx, "Deleting directory %s\n", dir);
139 if (smbcli_deltree(cli->tree, dir) == -1) {
140 printf("Unable to delete dir %s - %s\n", dir,
141 smbcli_errstr(cli->tree));
142 return false;
145 return true;
148 static bool test_GetManagerVersion_opts(struct torture_context *tctx,
149 struct dcerpc_binding_handle *b,
150 enum dfs_ManagerVersion *version_p)
152 struct dfs_GetManagerVersion r;
153 enum dfs_ManagerVersion version;
155 r.out.version = &version;
157 torture_assert_ntstatus_ok(tctx,
158 dcerpc_dfs_GetManagerVersion_r(b, tctx, &r),
159 "GetManagerVersion failed");
161 if (version_p) {
162 *version_p = version;
165 return true;
169 static bool test_GetManagerVersion(struct torture_context *tctx,
170 struct dcerpc_pipe *p)
172 struct dcerpc_binding_handle *b = p->binding_handle;
174 return test_GetManagerVersion_opts(tctx, b, NULL);
177 static bool test_ManagerInitialize(struct torture_context *tctx,
178 struct dcerpc_pipe *p)
180 enum dfs_ManagerVersion version;
181 struct dfs_ManagerInitialize r;
182 struct dcerpc_binding_handle *b = p->binding_handle;
183 const char *host = torture_setting_string(tctx, "host", NULL);
185 torture_comment(tctx, "Testing ManagerInitialize\n");
187 torture_assert(tctx,
188 test_GetManagerVersion_opts(tctx, b, &version),
189 "GetManagerVersion failed");
191 r.in.servername = host;
192 r.in.flags = 0;
194 torture_assert_ntstatus_ok(tctx,
195 dcerpc_dfs_ManagerInitialize_r(b, tctx, &r),
196 "ManagerInitialize failed");
197 if (!W_ERROR_IS_OK(r.out.result)) {
198 torture_warning(tctx, "dfs_ManagerInitialize failed - %s\n",
199 win_errstr(r.out.result));
200 IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
201 return false;
204 return true;
207 static bool test_GetInfoLevel(struct torture_context *tctx,
208 struct dcerpc_binding_handle *b,
209 uint16_t level,
210 const char *root)
212 struct dfs_GetInfo r;
213 union dfs_Info info;
215 torture_comment(tctx, "Testing GetInfo level %u on '%s'\n", level, root);
217 r.in.dfs_entry_path = root;
218 r.in.servername = NULL;
219 r.in.sharename = NULL;
220 r.in.level = level;
221 r.out.info = &info;
223 torture_assert_ntstatus_ok(tctx,
224 dcerpc_dfs_GetInfo_r(b, tctx, &r),
225 "GetInfo failed");
227 if (!W_ERROR_IS_OK(r.out.result) &&
228 !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
229 torture_warning(tctx, "dfs_GetInfo failed - %s\n", win_errstr(r.out.result));
230 return false;
233 return true;
236 static bool test_GetInfo(struct torture_context *tctx,
237 struct dcerpc_binding_handle *b,
238 const char *root)
240 bool ret = true;
241 /* 103, 104, 105, 106 is only available on Set */
242 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 100, 101, 102, 103, 104, 105, 106};
243 int i;
245 for (i=0;i<ARRAY_SIZE(levels);i++) {
246 if (!test_GetInfoLevel(tctx, b, levels[i], root)) {
247 ret = false;
250 return ret;
253 static bool test_EnumLevelEx(struct torture_context *tctx,
254 struct dcerpc_binding_handle *b,
255 uint16_t level,
256 const char *dfs_name)
258 struct dfs_EnumEx rex;
259 uint32_t total=0;
260 struct dfs_EnumStruct e;
261 struct dfs_Info1 s;
262 struct dfs_EnumArray1 e1;
263 bool ret = true;
265 rex.in.level = level;
266 rex.in.bufsize = (uint32_t)-1;
267 rex.in.total = &total;
268 rex.in.info = &e;
269 rex.in.dfs_name = dfs_name;
271 e.level = rex.in.level;
272 e.e.info1 = &e1;
273 e.e.info1->count = 0;
274 e.e.info1->s = &s;
275 s.path = NULL;
277 torture_comment(tctx, "Testing EnumEx level %u on '%s'\n", level, dfs_name);
279 torture_assert_ntstatus_ok(tctx,
280 dcerpc_dfs_EnumEx_r(b, tctx, &rex),
281 "EnumEx failed");
282 torture_assert_werr_ok(tctx, rex.out.result,
283 "EnumEx failed");
285 if (level == 1 && rex.out.total) {
286 int i;
287 for (i=0;i<*rex.out.total;i++) {
288 const char *root = rex.out.info->e.info1->s[i].path;
289 if (!test_GetInfo(tctx, b, root)) {
290 ret = false;
295 if (level == 300 && rex.out.total) {
296 int i,k;
297 for (i=0;i<*rex.out.total;i++) {
298 uint16_t levels[] = {1, 2, 3, 4, 200}; /* 300 */
299 const char *root = rex.out.info->e.info300->s[i].dom_root;
300 for (k=0;k<ARRAY_SIZE(levels);k++) {
301 if (!test_EnumLevelEx(tctx, b,
302 levels[k], root))
304 ret = false;
307 if (!test_GetInfo(tctx, b, root)) {
308 ret = false;
313 return ret;
317 static bool test_EnumLevel(struct torture_context *tctx,
318 struct dcerpc_binding_handle *b,
319 uint16_t level)
321 struct dfs_Enum r;
322 uint32_t total=0;
323 struct dfs_EnumStruct e;
324 struct dfs_Info1 s;
325 struct dfs_EnumArray1 e1;
326 bool ret = true;
328 r.in.level = level;
329 r.in.bufsize = (uint32_t)-1;
330 r.in.total = &total;
331 r.in.info = &e;
333 e.level = r.in.level;
334 e.e.info1 = &e1;
335 e.e.info1->count = 0;
336 e.e.info1->s = &s;
337 s.path = NULL;
339 torture_comment(tctx, "Testing Enum level %u\n", level);
341 torture_assert_ntstatus_ok(tctx,
342 dcerpc_dfs_Enum_r(b, tctx, &r),
343 "Enum failed");
345 if (!W_ERROR_IS_OK(r.out.result) &&
346 !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
347 torture_warning(tctx, "dfs_Enum failed - %s\n", win_errstr(r.out.result));
348 return false;
351 if (level == 1 && r.out.total) {
352 int i;
353 for (i=0;i<*r.out.total;i++) {
354 const char *root = r.out.info->e.info1->s[i].path;
355 if (!test_GetInfo(tctx, b, root)) {
356 ret = false;
361 return ret;
365 static bool test_Enum(struct torture_context *tctx,
366 struct dcerpc_pipe *p)
368 bool ret = true;
369 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 200, 300};
370 int i;
371 struct dcerpc_binding_handle *b = p->binding_handle;
373 for (i=0;i<ARRAY_SIZE(levels);i++) {
374 if (!test_EnumLevel(tctx, b, levels[i])) {
375 ret = false;
379 return ret;
382 static bool test_EnumEx(struct torture_context *tctx,
383 struct dcerpc_pipe *p)
385 bool ret = true;
386 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 200, 300};
387 int i;
388 struct dcerpc_binding_handle *b = p->binding_handle;
389 const char *host = torture_setting_string(tctx, "host", NULL);
391 for (i=0;i<ARRAY_SIZE(levels);i++) {
392 if (!test_EnumLevelEx(tctx, b, levels[i], host)) {
393 ret = false;
397 return ret;
400 static bool test_RemoveStdRoot(struct torture_context *tctx,
401 struct dcerpc_binding_handle *b,
402 const char *host,
403 const char *sharename)
405 struct dfs_RemoveStdRoot r;
407 torture_comment(tctx, "Testing RemoveStdRoot\n");
409 r.in.servername = host;
410 r.in.rootshare = sharename;
411 r.in.flags = 0;
413 torture_assert_ntstatus_ok(tctx,
414 dcerpc_dfs_RemoveStdRoot_r(b, tctx, &r),
415 "RemoveStdRoot failed");
416 torture_assert_werr_ok(tctx, r.out.result,
417 "dfs_RemoveStdRoot failed");
419 return true;
422 static bool test_AddStdRoot(struct torture_context *tctx,
423 struct dcerpc_binding_handle *b,
424 const char *host,
425 const char *sharename)
427 struct dfs_AddStdRoot r;
429 torture_comment(tctx, "Testing AddStdRoot\n");
431 r.in.servername = host;
432 r.in.rootshare = sharename;
433 r.in.comment = "standard dfs standalone DFS root created by smbtorture (dfs_AddStdRoot)";
434 r.in.flags = 0;
436 torture_assert_ntstatus_ok(tctx,
437 dcerpc_dfs_AddStdRoot_r(b, tctx, &r),
438 "AddStdRoot failed");
439 torture_assert_werr_ok(tctx, r.out.result,
440 "AddStdRoot failed");
442 return true;
445 static bool test_AddStdRootForced(struct torture_context *tctx,
446 struct dcerpc_binding_handle *b,
447 const char *host,
448 const char *sharename)
450 struct dfs_AddStdRootForced r;
451 enum dfs_ManagerVersion version;
453 torture_comment(tctx, "Testing AddStdRootForced\n");
455 torture_assert(tctx,
456 test_GetManagerVersion_opts(tctx, b, &version),
457 "GetManagerVersion failed");
459 r.in.servername = host;
460 r.in.rootshare = sharename;
461 r.in.comment = "standard dfs forced standalone DFS root created by smbtorture (dfs_AddStdRootForced)";
462 r.in.store = SMBTORTURE_DFS_PATHNAME;
464 torture_assert_ntstatus_ok(tctx,
465 dcerpc_dfs_AddStdRootForced_r(b, tctx, &r),
466 "AddStdRootForced failed");
467 if (!W_ERROR_IS_OK(r.out.result)) {
468 torture_warning(tctx, "dfs_AddStdRootForced failed - %s\n",
469 win_errstr(r.out.result));
470 IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
471 return false;
474 return test_RemoveStdRoot(tctx, b, host, sharename);
477 static void test_cleanup_stdroot(struct torture_context *tctx,
478 struct dcerpc_binding_handle *b,
479 const char *host,
480 const char *sharename,
481 const char *dir)
483 struct smbcli_state *cli;
485 torture_comment(tctx, "Cleaning up StdRoot\n");
487 test_RemoveStdRoot(tctx, b, host, sharename);
488 test_NetShareDel(tctx, host, sharename);
489 if (torture_open_connection_share(tctx, &cli, tctx, host, "C$", tctx->ev)) {
490 test_DeleteDir(tctx, cli, dir);
491 torture_close_connection(cli);
495 static bool test_StdRoot(struct torture_context *tctx,
496 struct dcerpc_pipe *p)
498 const char *sharename = SMBTORTURE_DFS_SHARENAME;
499 const char *dir = SMBTORTURE_DFS_DIRNAME;
500 const char *path = SMBTORTURE_DFS_PATHNAME;
501 struct smbcli_state *cli;
502 bool ret = true;
503 const char *host = torture_setting_string(tctx, "host", NULL);
504 struct dcerpc_binding_handle *b = p->binding_handle;
506 torture_comment(tctx, "Testing StdRoot\n");
508 test_cleanup_stdroot(tctx, b, host, sharename, dir);
510 torture_assert(tctx,
511 test_CreateDir(tctx, &cli, tctx, host, "C$", dir),
512 "failed to connect C$ share and to create directory");
513 torture_assert(tctx,
514 test_NetShareAdd(tctx, host, sharename, path),
515 "failed to create new share");
517 ret &= test_AddStdRoot(tctx, b, host, sharename);
518 ret &= test_RemoveStdRoot(tctx, b, host, sharename);
519 ret &= test_AddStdRootForced(tctx, b, host, sharename);
520 ret &= test_NetShareDel(tctx, host, sharename);
521 ret &= test_DeleteDir(tctx, cli, dir);
523 torture_close_connection(cli);
525 return ret;
528 static bool test_GetDcAddress(struct torture_context *tctx,
529 struct dcerpc_binding_handle *b,
530 const char *host)
532 struct dfs_GetDcAddress r;
533 uint8_t is_root = 0;
534 uint32_t ttl = 0;
535 const char *ptr;
537 torture_comment(tctx, "Testing GetDcAddress\n");
539 ptr = host;
541 r.in.servername = host;
542 r.in.server_fullname = r.out.server_fullname = &ptr;
543 r.in.is_root = r.out.is_root = &is_root;
544 r.in.ttl = r.out.ttl = &ttl;
546 torture_assert_ntstatus_ok(tctx,
547 dcerpc_dfs_GetDcAddress_r(b, tctx, &r),
548 "GetDcAddress failed");
549 torture_assert_werr_ok(tctx, r.out.result,
550 "dfs_GetDcAddress failed");
552 return true;
555 static bool test_SetDcAddress(struct torture_context *tctx,
556 struct dcerpc_binding_handle *b,
557 const char *host)
559 struct dfs_SetDcAddress r;
561 torture_comment(tctx, "Testing SetDcAddress\n");
563 r.in.servername = host;
564 r.in.server_fullname = host;
565 r.in.flags = 0;
566 r.in.ttl = 1000;
568 torture_assert_ntstatus_ok(tctx,
569 dcerpc_dfs_SetDcAddress_r(b, tctx, &r),
570 "SetDcAddress failed");
571 torture_assert_werr_ok(tctx, r.out.result,
572 "dfs_SetDcAddress failed");
574 return true;
577 static bool test_DcAddress(struct torture_context *tctx,
578 struct dcerpc_pipe *p)
580 const char *host = torture_setting_string(tctx, "host", NULL);
581 struct dcerpc_binding_handle *b = p->binding_handle;
583 if (!test_GetDcAddress(tctx, b, host)) {
584 return false;
587 if (!test_SetDcAddress(tctx, b, host)) {
588 return false;
591 return true;
594 static bool test_FlushFtTable(struct torture_context *tctx,
595 struct dcerpc_binding_handle *b,
596 const char *host,
597 const char *sharename)
599 struct dfs_FlushFtTable r;
600 enum dfs_ManagerVersion version;
602 torture_comment(tctx, "Testing FlushFtTable\n");
604 torture_assert(tctx,
605 test_GetManagerVersion_opts(tctx, b, &version),
606 "GetManagerVersion failed");
608 r.in.servername = host;
609 r.in.rootshare = sharename;
611 torture_assert_ntstatus_ok(tctx,
612 dcerpc_dfs_FlushFtTable_r(b, tctx, &r),
613 "FlushFtTable failed");
614 if (!W_ERROR_IS_OK(r.out.result)) {
615 torture_warning(tctx, "dfs_FlushFtTable failed - %s\n",
616 win_errstr(r.out.result));
617 IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
618 return false;
621 return true;
624 static bool test_FtRoot(struct torture_context *tctx,
625 struct dcerpc_pipe *p)
627 const char *sharename = SMBTORTURE_DFS_SHARENAME;
628 const char *host = torture_setting_string(tctx, "host", NULL);
629 struct dcerpc_binding_handle *b = p->binding_handle;
631 return test_FlushFtTable(tctx, b, host, sharename);
634 struct torture_suite *torture_rpc_dfs(TALLOC_CTX *mem_ctx)
636 struct torture_rpc_tcase *tcase;
637 struct torture_suite *suite = torture_suite_create(mem_ctx, "dfs");
639 tcase = torture_suite_add_rpc_iface_tcase(suite, "netdfs",
640 &ndr_table_netdfs);
642 torture_rpc_tcase_add_test(tcase, "GetManagerVersion", test_GetManagerVersion);
643 torture_rpc_tcase_add_test(tcase, "ManagerInitialize", test_ManagerInitialize);
644 torture_rpc_tcase_add_test(tcase, "Enum", test_Enum);
645 torture_rpc_tcase_add_test(tcase, "EnumEx", test_EnumEx);
646 torture_rpc_tcase_add_test(tcase, "StdRoot", test_StdRoot);
647 torture_rpc_tcase_add_test(tcase, "FtRoot", test_FtRoot);
648 torture_rpc_tcase_add_test(tcase, "DcAddress", test_DcAddress);
650 return suite;