summaryrefslogtreecommitdiffstats
path: root/source3/rpcclient/cmd_dfs.c
blob: 8177871dc1790543f55fcc97d5e600a8c13e9463 (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
/*
   Unix SMB/CIFS implementation.
   RPC pipe client

   Copyright (C) Tim Potter 2000
   Copyright (C) Jelmer Vernooij       2005.

   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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "rpcclient.h"
#include "../librpc/gen_ndr/ndr_dfs_c.h"

/* Check DFS is supported by the remote server */

static WERROR cmd_dfs_version(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			      int argc, const char **argv)
{
	enum dfs_ManagerVersion version;
	NTSTATUS result;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	if (argc != 1) {
		printf("Usage: %s\n", argv[0]);
		return WERR_OK;
	}

	result = dcerpc_dfs_GetManagerVersion(b, mem_ctx, &version);

	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}

	if (version > 0) {
		printf("dfs is present (%d)\n", version);
	} else {
		printf("dfs is not present\n");
	}

	return WERR_OK;
}

static WERROR cmd_dfs_add(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			  int argc, const char **argv)
{
	NTSTATUS result;
	WERROR werr;
	const char *path, *servername, *sharename, *comment;
	uint32_t flags = 0;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	if (argc != 5) {
		printf("Usage: %s path servername sharename comment\n",
		       argv[0]);
		return WERR_OK;
	}

	path = argv[1];
	servername = argv[2];
	sharename = argv[3];
	comment = argv[4];

	result = dcerpc_dfs_Add(b, mem_ctx, path, servername,
				sharename, comment, flags, &werr);
	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}

	return werr;
}

static WERROR cmd_dfs_remove(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			     int argc, const char **argv)
{
	NTSTATUS result;
	WERROR werr;
	const char *path, *servername, *sharename;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	if (argc != 4) {
		printf("Usage: %s path servername sharename\n", argv[0]);
		return WERR_OK;
	}

	path = argv[1];
	servername = argv[2];
	sharename = argv[3];

	result = dcerpc_dfs_Remove(b, mem_ctx, path, servername,
				   sharename, &werr);
	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}

	return werr;
}

/* Display a DFS_INFO_1 structure */

static void display_dfs_info_1(struct dfs_Info1 *info1)
{
	printf("path: %s\n", info1->path);
}

/* Display a DFS_INFO_2 structure */

static void display_dfs_info_2(struct dfs_Info2 *info2)
{
	printf("path: %s\n", info2->path);
	printf("\tcomment: %s\n", info2->comment);

	printf("\tstate: %d\n", info2->state);
	printf("\tnum_stores: %d\n", info2->num_stores);
}

/* Display a DFS_INFO_3 structure */

static void display_dfs_info_3(struct dfs_Info3 *info3)
{
	int i;

	printf("path: %s\n", info3->path);

	printf("\tcomment: %s\n", info3->comment);

	printf("\tstate: %d\n", info3->state);
	printf("\tnum_stores: %d\n", info3->num_stores);

	for (i = 0; i < info3->num_stores; i++) {
		struct dfs_StorageInfo *dsi = &info3->stores[i];

		printf("\t\tstorage[%d] server: %s\n", i, dsi->server);

		printf("\t\tstorage[%d] share: %s\n", i, dsi->share);
	}
}


/* Display a DFS_INFO_CTR structure */
static void display_dfs_info(uint32_t level, union dfs_Info *ctr)
{
	switch (level) {
		case 0x01:
			display_dfs_info_1(ctr->info1);
			break;
		case 0x02:
			display_dfs_info_2(ctr->info2);
			break;
		case 0x03:
			display_dfs_info_3(ctr->info3);
			break;
		default:
			printf("unsupported info level %d\n",
			       level);
			break;
	}
}

static void display_dfs_enumstruct(struct dfs_EnumStruct *ctr)
{
	int i;

	/* count is always the first element, so we can just use info1 here */
	for (i = 0; i < ctr->e.info1->count; i++) {
		switch (ctr->level) {
		case 1: display_dfs_info_1(&ctr->e.info1->s[i]); break;
		case 2: display_dfs_info_2(&ctr->e.info2->s[i]); break;
		case 3: display_dfs_info_3(&ctr->e.info3->s[i]); break;
		default:
				printf("unsupported info level %d\n",
			       ctr->level);
				return;
		}
	}
}

/* Enumerate dfs shares */

static WERROR cmd_dfs_enum(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			   int argc, const char **argv)
{
	struct dfs_EnumStruct str;
	struct dfs_EnumArray1 info1;
	struct dfs_EnumArray2 info2;
	struct dfs_EnumArray3 info3;
	struct dfs_EnumArray4 info4;
	struct dfs_EnumArray200 info200;
	struct dfs_EnumArray300 info300;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	NTSTATUS result;
	WERROR werr;
	uint32_t total = 0;

	if (argc > 2) {
		printf("Usage: %s [info_level]\n", argv[0]);
		return WERR_OK;
	}

	str.level = 1;
	if (argc == 2)
		str.level = atoi(argv[1]);

	switch (str.level) {
	case 1: str.e.info1 = &info1; ZERO_STRUCT(info1); break;
	case 2: str.e.info2 = &info2; ZERO_STRUCT(info2); break;
	case 3: str.e.info3 = &info3; ZERO_STRUCT(info3); break;
	case 4: str.e.info4 = &info4; ZERO_STRUCT(info4); break;
	case 200: str.e.info200 = &info200; ZERO_STRUCT(info200); break;
	case 300: str.e.info300 = &info300; ZERO_STRUCT(info300); break;
	default:
			  printf("Unknown info level %d\n", str.level);
			  return WERR_OK;
	}

	result = dcerpc_dfs_Enum(b, mem_ctx, str.level, 0xFFFFFFFF, &str,
				 &total, &werr);
	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}
	if (W_ERROR_IS_OK(werr)) {
		display_dfs_enumstruct(&str);
	}

	return werr;
}

/* Enumerate dfs shares */

static WERROR cmd_dfs_enumex(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			     int argc, const char **argv)
{
	struct dfs_EnumStruct str;
	struct dfs_EnumArray1 info1;
	struct dfs_EnumArray2 info2;
	struct dfs_EnumArray3 info3;
	struct dfs_EnumArray4 info4;
	struct dfs_EnumArray200 info200;
	struct dfs_EnumArray300 info300;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	NTSTATUS result;
	WERROR werr;
	uint32_t total = 0;

	if (argc < 2 || argc > 3) {
		printf("Usage: %s dfs_name [info_level]\n", argv[0]);
		return WERR_OK;
	}

	str.level = 1;

	if (argc == 3)
		str.level = atoi(argv[2]);

	switch (str.level) {
	case 1: str.e.info1 = &info1; ZERO_STRUCT(info1); break;
	case 2: str.e.info2 = &info2; ZERO_STRUCT(info2); break;
	case 3: str.e.info3 = &info3; ZERO_STRUCT(info3); break;
	case 4: str.e.info4 = &info4; ZERO_STRUCT(info4); break;
	case 200: str.e.info200 = &info200; ZERO_STRUCT(info200); break;
	case 300: str.e.info300 = &info300; ZERO_STRUCT(info300); break;
	default:
		  printf("Unknown info level %d\n", str.level);
		  return WERR_OK;
	}

	result = dcerpc_dfs_EnumEx(b, mem_ctx, argv[1], str.level,
				   0xFFFFFFFF, &str, &total, &werr);
	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}
	if (W_ERROR_IS_OK(werr)) {
		display_dfs_enumstruct(&str);
	}

	return werr;
}


static WERROR cmd_dfs_getinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
			      int argc, const char **argv)
{
	NTSTATUS result;
	WERROR werr;
	const char *path, *servername, *sharename;
	uint32_t info_level = 1;
	union dfs_Info ctr;
	struct dcerpc_binding_handle *b = cli->binding_handle;

	if (argc < 4 || argc > 5) {
		printf("Usage: %s path servername sharename "
                       "[info_level]\n", argv[0]);
		return WERR_OK;
	}

	path = argv[1];
	servername = argv[2];
	sharename = argv[3];

	if (argc == 5)
		info_level = atoi(argv[4]);

	result = dcerpc_dfs_GetInfo(b, mem_ctx, path, servername,
				    sharename, info_level, &ctr, &werr);
	if (!NT_STATUS_IS_OK(result)) {
		return ntstatus_to_werror(result);
	}
	if (W_ERROR_IS_OK(werr)) {
		display_dfs_info(info_level, &ctr);
	}

	return werr;
}

/* List of commands exported by this module */

struct cmd_set dfs_commands[] = {

	{ .name = "DFS" },

	{
		.name               = "dfsversion",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_version,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Query DFS support",
		.usage              = "",
	},
	{
		.name               = "dfsadd",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_add,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Add a DFS share",
		.usage              = "",
	},
	{
		.name               = "dfsremove",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_remove,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Remove a DFS share",
		.usage              = "",
	},
	{
		.name               = "dfsgetinfo",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_getinfo,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Query DFS share info",
		.usage              = "",
	},
	{
		.name               = "dfsenum",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_enum,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Enumerate dfs shares",
		.usage              = "",
	},
	{
		.name               = "dfsenumex",
		.returntype         = RPC_RTYPE_WERROR,
		.ntfn               = NULL,
		.wfn                = cmd_dfs_enumex,
		.table              = &ndr_table_netdfs,
		.rpc_pipe           = NULL,
		.description        = "Enumerate dfs shares",
		.usage              = "",
	},

	{ .name = NULL }
};