summaryrefslogtreecommitdiffstats
path: root/source4/torture/rpc/dfs.c
blob: 8c06859265f952409355b2498ee075cdd9ac32b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
   Unix SMB/CIFS implementation.
   test suite for rpc dfs operations

   Copyright (C) Andrew Tridgell 2003

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "torture/rpc/torture_rpc.h"
#include "librpc/gen_ndr/ndr_dfs_c.h"
#include "libnet/libnet.h"
#include "torture/util.h"
#include "libcli/libcli.h"
#include "lib/cmdline/popt_common.h"

#define SMBTORTURE_DFS_SHARENAME "smbtorture_dfs_share"
#define SMBTORTURE_DFS_DIRNAME "\\smbtorture_dfs_dir"
#define SMBTORTURE_DFS_PATHNAME "C:"SMBTORTURE_DFS_DIRNAME

#define IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(x,y)\
	if (x == DFS_MANAGER_VERSION_W2K3) {\
		if (!W_ERROR_EQUAL(y,WERR_NOT_SUPPORTED)) {\
			printf("expected WERR_NOT_SUPPORTED\n");\
			return false;\
		}\
		return true;\
	}\

static bool test_NetShareAdd(struct torture_context *tctx,
			     const char *host,
			     const char *sharename,
			     const char *dir)
{
	NTSTATUS status;
	struct srvsvc_NetShareInfo2 i;
	struct libnet_context* libnetctx;
	struct libnet_AddShare r;

	printf("Creating share %s\n", sharename);

	if (!(libnetctx = libnet_context_init(tctx->ev, tctx->lp_ctx))) {
		return false;
	}

	libnetctx->cred = cmdline_credentials;

	i.name			= sharename;
	i.type			= STYPE_DISKTREE;
	i.path			= dir;
	i.max_users		= (uint32_t) -1;
	i.comment		= "created by smbtorture";
	i.password		= NULL;
	i.permissions		= 0x0;
	i.current_users		= 0x0;

	r.level 		= 2;
	r.in.server_name	= host;
	r.in.share 		= i;

	status = libnet_AddShare(libnetctx, tctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("Failed to add new share: %s (%s)\n",
			nt_errstr(status), r.out.error_string);
		return false;
	}

	return true;
}

static bool test_NetShareDel(struct torture_context *tctx,
			     const char *host,
			     const char *sharename)
{
	NTSTATUS status;
	struct libnet_context* libnetctx;
	struct libnet_DelShare r;

	torture_comment(tctx, "Deleting share %s\n", sharename);

	if (!(libnetctx = libnet_context_init(tctx->ev, tctx->lp_ctx))) {
		return false;
	}

	libnetctx->cred = cmdline_credentials;

	r.in.share_name		= sharename;
	r.in.server_name	= host;

	status = libnet_DelShare(libnetctx, tctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("Failed to delete share: %s (%s)\n",
			nt_errstr(status), r.out.error_string);
		return false;
	}

	return true;
}

static bool test_CreateDir(TALLOC_CTX *mem_ctx,
			   struct smbcli_state **cli,
			   struct torture_context *tctx,
			   const char *host,
			   const char *share,
			   const char *dir)
{
	printf("Creating directory %s\n", dir);

	if (!torture_open_connection_share(mem_ctx, cli, tctx, host, share, tctx->ev)) {
		return false;
	}

	if (!torture_setup_dir(*cli, dir)) {
		return false;
	}

	return true;
}

static bool test_DeleteDir(struct torture_context *tctx,
			   struct smbcli_state *cli,
			   const char *dir)
{
	torture_comment(tctx, "Deleting directory %s\n", dir);

	if (smbcli_deltree(cli->tree, dir) == -1) {
		printf("Unable to delete dir %s - %s\n", dir,
			smbcli_errstr(cli->tree));
		return false;
	}

	return true;
}

static bool test_GetManagerVersion_opts(struct torture_context *tctx,
					struct dcerpc_binding_handle *b,
					enum dfs_ManagerVersion *version_p)
{
	struct dfs_GetManagerVersion r;
	enum dfs_ManagerVersion version;

	r.out.version = &version;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_GetManagerVersion_r(b, tctx, &r),
		"GetManagerVersion failed");

	if (version_p) {
		*version_p = version;
	}

	return true;
}


static bool test_GetManagerVersion(struct torture_context *tctx,
				   struct dcerpc_pipe *p)
{
	struct dcerpc_binding_handle *b = p->binding_handle;

	return test_GetManagerVersion_opts(tctx, b, NULL);
}

static bool test_ManagerInitialize(struct torture_context *tctx,
				   struct dcerpc_pipe *p)
{
	enum dfs_ManagerVersion version;
	struct dfs_ManagerInitialize r;
	struct dcerpc_binding_handle *b = p->binding_handle;
	const char *host = torture_setting_string(tctx, "host", NULL);

	torture_comment(tctx, "Testing ManagerInitialize\n");

	torture_assert(tctx,
		test_GetManagerVersion_opts(tctx, b, &version),
		"GetManagerVersion failed");

	r.in.servername = host;
	r.in.flags = 0;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_ManagerInitialize_r(b, tctx, &r),
		"ManagerInitialize failed");
	if (!W_ERROR_IS_OK(r.out.result)) {
		torture_warning(tctx, "dfs_ManagerInitialize failed - %s\n",
			win_errstr(r.out.result));
		IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
		return false;
	}

	return true;
}

static bool test_GetInfoLevel(struct torture_context *tctx,
			      struct dcerpc_binding_handle *b,
			      uint16_t level,
			      const char *root)
{
	struct dfs_GetInfo r;
	union dfs_Info info;

	torture_comment(tctx, "Testing GetInfo level %u on '%s'\n", level, root);

	r.in.dfs_entry_path = root;
	r.in.servername = NULL;
	r.in.sharename = NULL;
	r.in.level = level;
	r.out.info = &info;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_GetInfo_r(b, tctx, &r),
		"GetInfo failed");

	if (!W_ERROR_IS_OK(r.out.result) &&
	    !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
		torture_warning(tctx, "dfs_GetInfo failed - %s\n", win_errstr(r.out.result));
		return false;
	}

	return true;
}

static bool test_GetInfo(struct torture_context *tctx,
			 struct dcerpc_binding_handle *b,
			 const char *root)
{
	bool ret = true;
	/* 103, 104, 105, 106 is only available on Set */
	uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 100, 101, 102, 103, 104, 105, 106};
	int i;

	for (i=0;i<ARRAY_SIZE(levels);i++) {
		if (!test_GetInfoLevel(tctx, b, levels[i], root)) {
			ret = false;
		}
	}
	return ret;
}

static bool test_EnumLevelEx(struct torture_context *tctx,
			     struct dcerpc_binding_handle *b,
			     uint16_t level,
			     const char *dfs_name)
{
	struct dfs_EnumEx rex;
	uint32_t total=0;
	struct dfs_EnumStruct e;
	struct dfs_Info1 s;
	struct dfs_EnumArray1 e1;
	bool ret = true;

	rex.in.level = level;
	rex.in.bufsize = (uint32_t)-1;
	rex.in.total = &total;
	rex.in.info = &e;
	rex.in.dfs_name = dfs_name;

	e.level = rex.in.level;
	e.e.info1 = &e1;
	e.e.info1->count = 0;
	e.e.info1->s = &s;
	s.path = NULL;

	torture_comment(tctx, "Testing EnumEx level %u on '%s'\n", level, dfs_name);

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_EnumEx_r(b, tctx, &rex),
		"EnumEx failed");
	torture_assert_werr_ok(tctx, rex.out.result,
		"EnumEx failed");

	if (level == 1 && rex.out.total) {
		int i;
		for (i=0;i<*rex.out.total;i++) {
			const char *root = rex.out.info->e.info1->s[i].path;
			if (!test_GetInfo(tctx, b, root)) {
				ret = false;
			}
		}
	}

	if (level == 300 && rex.out.total) {
		int i,k;
		for (i=0;i<*rex.out.total;i++) {
			uint16_t levels[] = {1, 2, 3, 4, 200}; /* 300 */
			const char *root = rex.out.info->e.info300->s[i].dom_root;
			for (k=0;k<ARRAY_SIZE(levels);k++) {
				if (!test_EnumLevelEx(tctx, b,
						      levels[k], root))
				{
					ret = false;
				}
			}
			if (!test_GetInfo(tctx, b, root)) {
				ret = false;
			}
		}
	}

	return ret;
}


static bool test_EnumLevel(struct torture_context *tctx,
			   struct dcerpc_binding_handle *b,
			   uint16_t level)
{
	struct dfs_Enum r;
	uint32_t total=0;
	struct dfs_EnumStruct e;
	struct dfs_Info1 s;
	struct dfs_EnumArray1 e1;
	bool ret = true;

	r.in.level = level;
	r.in.bufsize = (uint32_t)-1;
	r.in.total = &total;
	r.in.info = &e;

	e.level = r.in.level;
	e.e.info1 = &e1;
	e.e.info1->count = 0;
	e.e.info1->s = &s;
	s.path = NULL;

	torture_comment(tctx, "Testing Enum level %u\n", level);

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_Enum_r(b, tctx, &r),
		"Enum failed");

	if (!W_ERROR_IS_OK(r.out.result) &&
	    !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
		torture_warning(tctx, "dfs_Enum failed - %s\n", win_errstr(r.out.result));
		return false;
	}

	if (level == 1 && r.out.total) {
		int i;
		for (i=0;i<*r.out.total;i++) {
			const char *root = r.out.info->e.info1->s[i].path;
			if (!test_GetInfo(tctx, b, root)) {
				ret = false;
			}
		}
	}

	return ret;
}


static bool test_Enum(struct torture_context *tctx,
		      struct dcerpc_pipe *p)
{
	bool ret = true;
	uint16_t levels[] = {1, 2, 3, 4, 5, 6, 200, 300};
	int i;
	struct dcerpc_binding_handle *b = p->binding_handle;

	for (i=0;i<ARRAY_SIZE(levels);i++) {
		if (!test_EnumLevel(tctx, b, levels[i])) {
			ret = false;
		}
	}

	return ret;
}

static bool test_EnumEx(struct torture_context *tctx,
			struct dcerpc_pipe *p)
{
	bool ret = true;
	uint16_t levels[] = {1, 2, 3, 4, 5, 6, 200, 300};
	int i;
	struct dcerpc_binding_handle *b = p->binding_handle;
	const char *host = torture_setting_string(tctx, "host", NULL);

	for (i=0;i<ARRAY_SIZE(levels);i++) {
		if (!test_EnumLevelEx(tctx, b, levels[i], host)) {
			ret = false;
		}
	}

	return ret;
}

static bool test_RemoveStdRoot(struct torture_context *tctx,
			       struct dcerpc_binding_handle *b,
			       const char *host,
			       const char *sharename)
{
	struct dfs_RemoveStdRoot r;

	torture_comment(tctx, "Testing RemoveStdRoot\n");

	r.in.servername	= host;
	r.in.rootshare	= sharename;
	r.in.flags	= 0;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_RemoveStdRoot_r(b, tctx, &r),
		"RemoveStdRoot failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"dfs_RemoveStdRoot failed");

	return true;
}

static bool test_AddStdRoot(struct torture_context *tctx,
			    struct dcerpc_binding_handle *b,
			    const char *host,
			    const char *sharename)
{
	struct dfs_AddStdRoot r;

	torture_comment(tctx, "Testing AddStdRoot\n");

	r.in.servername	= host;
	r.in.rootshare	= sharename;
	r.in.comment	= "standard dfs standalone DFS root created by smbtorture (dfs_AddStdRoot)";
	r.in.flags	= 0;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_AddStdRoot_r(b, tctx, &r),
		"AddStdRoot failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"AddStdRoot failed");

	return true;
}

static bool test_AddStdRootForced(struct torture_context *tctx,
				  struct dcerpc_binding_handle *b,
				  const char *host,
				  const char *sharename)
{
	struct dfs_AddStdRootForced r;
	enum dfs_ManagerVersion version;

	torture_comment(tctx, "Testing AddStdRootForced\n");

	torture_assert(tctx,
		test_GetManagerVersion_opts(tctx, b, &version),
		"GetManagerVersion failed");

	r.in.servername	= host;
	r.in.rootshare	= sharename;
	r.in.comment	= "standard dfs forced standalone DFS root created by smbtorture (dfs_AddStdRootForced)";
	r.in.store	= SMBTORTURE_DFS_PATHNAME;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_AddStdRootForced_r(b, tctx, &r),
		"AddStdRootForced failed");
	if (!W_ERROR_IS_OK(r.out.result)) {
		torture_warning(tctx, "dfs_AddStdRootForced failed - %s\n",
			win_errstr(r.out.result));
		IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
		return false;
	}

	return test_RemoveStdRoot(tctx, b, host, sharename);
}

static void test_cleanup_stdroot(struct torture_context *tctx,
				 struct dcerpc_binding_handle *b,
				 const char *host,
				 const char *sharename,
				 const char *dir)
{
	struct smbcli_state *cli;

	torture_comment(tctx, "Cleaning up StdRoot\n");

	test_RemoveStdRoot(tctx, b, host, sharename);
	test_NetShareDel(tctx, host, sharename);
	if (torture_open_connection_share(tctx, &cli, tctx, host, "C$", tctx->ev)) {
		test_DeleteDir(tctx, cli, dir);
		torture_close_connection(cli);
	}
}

static bool test_StdRoot(struct torture_context *tctx,
			 struct dcerpc_pipe *p)
{
	const char *sharename = SMBTORTURE_DFS_SHARENAME;
	const char *dir = SMBTORTURE_DFS_DIRNAME;
	const char *path = SMBTORTURE_DFS_PATHNAME;
	struct smbcli_state *cli;
	bool ret = true;
	const char *host = torture_setting_string(tctx, "host", NULL);
	struct dcerpc_binding_handle *b = p->binding_handle;

	torture_comment(tctx, "Testing StdRoot\n");

	test_cleanup_stdroot(tctx, b, host, sharename, dir);

	torture_assert(tctx,
		test_CreateDir(tctx, &cli, tctx, host, "C$", dir),
		"failed to connect C$ share and to create directory");
	torture_assert(tctx,
		test_NetShareAdd(tctx, host, sharename, path),
		"failed to create new share");

	ret &= test_AddStdRoot(tctx, b, host, sharename);
	ret &= test_RemoveStdRoot(tctx, b, host, sharename);
	ret &= test_AddStdRootForced(tctx, b, host, sharename);
	ret &= test_NetShareDel(tctx, host, sharename);
	ret &= test_DeleteDir(tctx, cli, dir);

	torture_close_connection(cli);

	return ret;
}

static bool test_GetDcAddress(struct torture_context *tctx,
			      struct dcerpc_binding_handle *b,
			      const char *host)
{
	struct dfs_GetDcAddress r;
	uint8_t is_root = 0;
	uint32_t ttl = 0;
	const char *ptr;

	torture_comment(tctx, "Testing GetDcAddress\n");

	ptr = host;

	r.in.servername = host;
	r.in.server_fullname = r.out.server_fullname = &ptr;
	r.in.is_root = r.out.is_root = &is_root;
	r.in.ttl = r.out.ttl = &ttl;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_GetDcAddress_r(b, tctx, &r),
		"GetDcAddress failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"dfs_GetDcAddress failed");

	return true;
}

static bool test_SetDcAddress(struct torture_context *tctx,
			      struct dcerpc_binding_handle *b,
			      const char *host)
{
	struct dfs_SetDcAddress r;

	torture_comment(tctx, "Testing SetDcAddress\n");

	r.in.servername = host;
	r.in.server_fullname = host;
	r.in.flags = 0;
	r.in.ttl = 1000;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_SetDcAddress_r(b, tctx, &r),
		"SetDcAddress failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"dfs_SetDcAddress failed");

	return true;
}

static bool test_DcAddress(struct torture_context *tctx,
			   struct dcerpc_pipe *p)
{
	const char *host = torture_setting_string(tctx, "host", NULL);
	struct dcerpc_binding_handle *b = p->binding_handle;

	if (!test_GetDcAddress(tctx, b, host)) {
		return false;
	}

	if (!test_SetDcAddress(tctx, b, host)) {
		return false;
	}

	return true;
}

static bool test_FlushFtTable(struct torture_context *tctx,
			      struct dcerpc_binding_handle *b,
			      const char *host,
			      const char *sharename)
{
	struct dfs_FlushFtTable r;
	enum dfs_ManagerVersion version;

	torture_comment(tctx, "Testing FlushFtTable\n");

	torture_assert(tctx,
		test_GetManagerVersion_opts(tctx, b, &version),
		"GetManagerVersion failed");

	r.in.servername = host;
	r.in.rootshare = sharename;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_dfs_FlushFtTable_r(b, tctx, &r),
		"FlushFtTable failed");
	if (!W_ERROR_IS_OK(r.out.result)) {
		torture_warning(tctx, "dfs_FlushFtTable failed - %s\n",
			win_errstr(r.out.result));
		IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
		return false;
	}

	return true;
}

static bool test_FtRoot(struct torture_context *tctx,
			struct dcerpc_pipe *p)
{
	const char *sharename = SMBTORTURE_DFS_SHARENAME;
	const char *host = torture_setting_string(tctx, "host", NULL);
	struct dcerpc_binding_handle *b = p->binding_handle;

	return test_FlushFtTable(tctx, b, host, sharename);
}

struct torture_suite *torture_rpc_dfs(TALLOC_CTX *mem_ctx)
{
	struct torture_rpc_tcase *tcase;
	struct torture_suite *suite = torture_suite_create(mem_ctx, "DFS");

	tcase = torture_suite_add_rpc_iface_tcase(suite, "netdfs",
						  &ndr_table_netdfs);

	torture_rpc_tcase_add_test(tcase, "GetManagerVersion", test_GetManagerVersion);
	torture_rpc_tcase_add_test(tcase, "ManagerInitialize", test_ManagerInitialize);
	torture_rpc_tcase_add_test(tcase, "Enum", test_Enum);
	torture_rpc_tcase_add_test(tcase, "EnumEx", test_EnumEx);
	torture_rpc_tcase_add_test(tcase, "StdRoot", test_StdRoot);
	torture_rpc_tcase_add_test(tcase, "FtRoot", test_FtRoot);
	torture_rpc_tcase_add_test(tcase, "DcAddress", test_DcAddress);

	return suite;
}