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

   Copyright (C) Andrew Tridgell 1992-1999
   Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999
   Copyright (C) Tim Potter 2000,2002

   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"

/* Display server query info */

static char *get_server_type_str(uint32 type)
{
	static fstring typestr;
	int i;

	if (type == SV_TYPE_ALL) {
		fstrcpy(typestr, "All");
		return typestr;
	}
		
	typestr[0] = 0;

	for (i = 0; i < 32; i++) {
		if (type & (1 << i)) {
			switch (1 << i) {
			case SV_TYPE_WORKSTATION:
				fstrcat(typestr, "Wk ");
				break;
			case SV_TYPE_SERVER:
				fstrcat(typestr, "Sv ");
				break;
			case SV_TYPE_SQLSERVER:
				fstrcat(typestr, "Sql ");
				break;
			case SV_TYPE_DOMAIN_CTRL:
				fstrcat(typestr, "PDC ");
				break;
			case SV_TYPE_DOMAIN_BAKCTRL:
				fstrcat(typestr, "BDC ");
				break;
			case SV_TYPE_TIME_SOURCE:
				fstrcat(typestr, "Tim ");
				break;
			case SV_TYPE_AFP:
				fstrcat(typestr, "AFP ");
				break;
			case SV_TYPE_NOVELL:
				fstrcat(typestr, "Nov ");
				break;
			case SV_TYPE_DOMAIN_MEMBER:
				fstrcat(typestr, "Dom ");
				break;
			case SV_TYPE_PRINTQ_SERVER:
				fstrcat(typestr, "PrQ ");
				break;
			case SV_TYPE_DIALIN_SERVER:
				fstrcat(typestr, "Din ");
				break;
			case SV_TYPE_SERVER_UNIX:
				fstrcat(typestr, "Unx ");
				break;
			case SV_TYPE_NT:
				fstrcat(typestr, "NT ");
				break;
			case SV_TYPE_WFW:
				fstrcat(typestr, "Wfw ");
				break;
			case SV_TYPE_SERVER_MFPN:
				fstrcat(typestr, "Mfp ");
				break;
			case SV_TYPE_SERVER_NT:
				fstrcat(typestr, "SNT ");
				break;
			case SV_TYPE_POTENTIAL_BROWSER:
				fstrcat(typestr, "PtB ");
				break;
			case SV_TYPE_BACKUP_BROWSER:
				fstrcat(typestr, "BMB ");
				break;
			case SV_TYPE_MASTER_BROWSER:
				fstrcat(typestr, "LMB ");
				break;
			case SV_TYPE_DOMAIN_MASTER:
				fstrcat(typestr, "DMB ");
				break;
			case SV_TYPE_SERVER_OSF:
				fstrcat(typestr, "OSF ");
				break;
			case SV_TYPE_SERVER_VMS:
				fstrcat(typestr, "VMS ");
				break;
			case SV_TYPE_WIN95_PLUS:
				fstrcat(typestr, "W95 ");
				break;
			case SV_TYPE_ALTERNATE_XPORT:
				fstrcat(typestr, "Xpt ");
				break;
			case SV_TYPE_LOCAL_LIST_ONLY:
				fstrcat(typestr, "Dom ");
				break;
			case SV_TYPE_DOMAIN_ENUM:
				fstrcat(typestr, "Loc ");
				break;
			}
		}
	}

	i = strlen(typestr) - 1;

	if (typestr[i] == ' ')
		typestr[i] = 0;
	
	return typestr;
}

static void display_server(char *sname, uint32 type, const char *comment)
{
	printf("\t%-15.15s%-20s %s\n", sname, get_server_type_str(type), 
	       comment);
}

static void display_srv_info_101(SRV_INFO_101 *sv101)
{
	fstring name;
	fstring comment;

	unistr2_to_ascii(name, &sv101->uni_name, sizeof(name));
	unistr2_to_ascii(comment, &sv101->uni_comment, sizeof(comment));

	display_server(name, sv101->srv_type, comment);

	printf("\tplatform_id     :\t%d\n", sv101->platform_id);
	printf("\tos version      :\t%d.%d\n", sv101->ver_major, 
	       sv101->ver_minor);

	printf("\tserver type     :\t0x%x\n", sv101->srv_type);
}

static void display_srv_info_102(SRV_INFO_102 *sv102)
{
	fstring name;
	fstring comment;
	fstring usr_path;
	
	unistr2_to_ascii(name, &sv102->uni_name, sizeof(name));
	unistr2_to_ascii(comment, &sv102->uni_comment, sizeof(comment));
	unistr2_to_ascii(usr_path, &sv102->uni_usr_path, sizeof(usr_path));

	display_server(name, sv102->srv_type, comment);

	printf("\tplatform_id     :\t%d\n", sv102->platform_id);
	printf("\tos version      :\t%d.%d\n", sv102->ver_major, 
	       sv102->ver_minor);

	printf("\tusers           :\t%x\n", sv102->users);
	printf("\tdisc, hidden    :\t%x, %x\n", sv102->disc, sv102->hidden);
	printf("\tannounce, delta :\t%d, %d\n", sv102->announce, 
	       sv102->ann_delta);
	printf("\tlicenses        :\t%d\n", sv102->licenses);
	printf("\tuser path       :\t%s\n", usr_path);
}

/* Server query info */
static WERROR cmd_srvsvc_srv_query_info(struct rpc_pipe_client *cli, 
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
{
	uint32 info_level = 101;
	SRV_INFO_CTR ctr;
	WERROR result;

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

	if (argc == 2)
		info_level = atoi(argv[1]);

	result = rpccli_srvsvc_net_srv_get_info(cli, mem_ctx, info_level,
					     &ctr);

	if (!W_ERROR_IS_OK(result)) {
		goto done;
	}

	/* Display results */

	switch (info_level) {
	case 101:
		display_srv_info_101(&ctr.srv.sv101);
		break;
	case 102:
		display_srv_info_102(&ctr.srv.sv102);
		break;
	default:
		printf("unsupported info level %d\n", info_level);
		break;
	}

 done:
	return result;
}

static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
{
	fstring netname = "", remark = "";

	rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
	rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);

	printf("netname: %s\n", netname);
	printf("\tremark:\t%s\n", remark);
}

static void display_share_info_2(SRV_SHARE_INFO_2 *info2)
{
	fstring netname = "", remark = "", path = "", passwd = "";

	rpcstr_pull_unistr2_fstring(netname, &info2->info_2_str.uni_netname);
	rpcstr_pull_unistr2_fstring(remark, &info2->info_2_str.uni_remark);
	rpcstr_pull_unistr2_fstring(path, &info2->info_2_str.uni_path);
	rpcstr_pull_unistr2_fstring(passwd, &info2->info_2_str.uni_passwd);

	printf("netname: %s\n", netname);
	printf("\tremark:\t%s\n", remark);
	printf("\tpath:\t%s\n", path);
	printf("\tpassword:\t%s\n", passwd);
}

static void display_share_info_502(SRV_SHARE_INFO_502 *info502)
{
	fstring netname = "", remark = "", path = "", passwd = "";

	rpcstr_pull_unistr2_fstring(netname, &info502->info_502_str.uni_netname);
	rpcstr_pull_unistr2_fstring(remark, &info502->info_502_str.uni_remark);
	rpcstr_pull_unistr2_fstring(path, &info502->info_502_str.uni_path);
	rpcstr_pull_unistr2_fstring(passwd, &info502->info_502_str.uni_passwd);

	printf("netname: %s\n", netname);
	printf("\tremark:\t%s\n", remark);
	printf("\tpath:\t%s\n", path);
	printf("\tpassword:\t%s\n", passwd);

	printf("\ttype:\t0x%x\n", info502->info_502.type);
	printf("\tperms:\t%d\n", info502->info_502.perms);
	printf("\tmax_uses:\t%d\n", info502->info_502.max_uses);
	printf("\tnum_uses:\t%d\n", info502->info_502.num_uses);
	
	if (info502->info_502_str.sd)
		display_sec_desc(info502->info_502_str.sd);

}

static WERROR cmd_srvsvc_net_share_enum(struct rpc_pipe_client *cli, 
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
{
	uint32 info_level = 2;
	SRV_SHARE_INFO_CTR ctr;
	WERROR result;
	ENUM_HND hnd;
	uint32 preferred_len = 0xffffffff, i;

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

	if (argc == 2)
		info_level = atoi(argv[1]);

	init_enum_hnd(&hnd, 0);

	result = rpccli_srvsvc_net_share_enum(
		cli, mem_ctx, info_level, &ctr, preferred_len, &hnd);

	if (!W_ERROR_IS_OK(result) || !ctr.num_entries)
		goto done;

	/* Display results */

	switch (info_level) {
	case 1:
		for (i = 0; i < ctr.num_entries; i++)
			display_share_info_1(&ctr.share.info1[i]);
		break;
	case 2:
		for (i = 0; i < ctr.num_entries; i++)
			display_share_info_2(&ctr.share.info2[i]);
		break;
	case 502:
		for (i = 0; i < ctr.num_entries; i++)
			display_share_info_502(&ctr.share.info502[i]);
		break;
	default:
		printf("unsupported info level %d\n", info_level);
		break;
	}

 done:
	return result;
}

static WERROR cmd_srvsvc_net_share_get_info(struct rpc_pipe_client *cli, 
					    TALLOC_CTX *mem_ctx,
					    int argc, const char **argv)
{
	uint32 info_level = 502;
	SRV_SHARE_INFO info;
	WERROR result;

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

	if (argc == 3)
		info_level = atoi(argv[2]);

	result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info);

	if (!W_ERROR_IS_OK(result))
		goto done;

	/* Display results */

	switch (info_level) {
	case 1:
		display_share_info_1(&info.share.info1);
		break;
	case 2:
		display_share_info_2(&info.share.info2);
		break;
	case 502:
		display_share_info_502(&info.share.info502);
		break;
	default:
		printf("unsupported info level %d\n", info_level);
		break;
	}

 done:
	return result;
}

static WERROR cmd_srvsvc_net_share_set_info(struct rpc_pipe_client *cli, 
					    TALLOC_CTX *mem_ctx,
					    int argc, const char **argv)
{
	uint32 info_level = 502;
	SRV_SHARE_INFO info_get;
	WERROR result;

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

	/* retrieve share info */
	result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info_get);
	if (!W_ERROR_IS_OK(result))
		goto done;

	info_get.switch_value = info_level;
	info_get.ptr_share_ctr = 1;
	init_unistr2(&(info_get.share.info502.info_502_str.uni_remark), argv[2], UNI_STR_TERMINATE);
	
	/* set share info */
	result = rpccli_srvsvc_net_share_set_info(cli, mem_ctx, argv[1], info_level, &info_get);

	if (!W_ERROR_IS_OK(result))
		goto done;

	/* re-retrieve share info and display */
	result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info_get);
	if (!W_ERROR_IS_OK(result))
		goto done;

	display_share_info_502(&info_get.share.info502);
	
 done:
	return result;
}

static WERROR cmd_srvsvc_net_remote_tod(struct rpc_pipe_client *cli, 
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
{
	TIME_OF_DAY_INFO tod;
	fstring srv_name_slash;
	WERROR result;

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

	fstr_sprintf(srv_name_slash, "\\\\%s", cli->cli->desthost);
	result = rpccli_srvsvc_net_remote_tod(
		cli, mem_ctx, srv_name_slash, &tod);

	if (!W_ERROR_IS_OK(result))
		goto done;

 done:
	return result;
}

static WERROR cmd_srvsvc_net_file_enum(struct rpc_pipe_client *cli, 
					 TALLOC_CTX *mem_ctx,
					 int argc, const char **argv)
{
	uint32 info_level = 3;
	SRV_FILE_INFO_CTR ctr;
	WERROR result;
	ENUM_HND hnd;
	uint32 preferred_len = 0xffff;

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

	if (argc == 2)
		info_level = atoi(argv[1]);

	init_enum_hnd(&hnd, 0);

	ZERO_STRUCT(ctr);

	result = rpccli_srvsvc_net_file_enum(
		cli, mem_ctx, info_level, NULL, &ctr, preferred_len, &hnd);

	if (!W_ERROR_IS_OK(result))
		goto done;

 done:
	return result;
}

/* List of commands exported by this module */

struct cmd_set srvsvc_commands[] = {

	{ "SRVSVC" },

	{ "srvinfo",     RPC_RTYPE_WERROR, NULL, cmd_srvsvc_srv_query_info, PI_SRVSVC, NULL, "Server query info", "" },
	{ "netshareenum",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_enum, PI_SRVSVC, NULL, "Enumerate shares", "" },
	{ "netsharegetinfo",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_get_info, PI_SRVSVC, NULL, "Get Share Info", "" },
	{ "netsharesetinfo",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_set_info, PI_SRVSVC, NULL, "Set Share Info", "" },
	{ "netfileenum", RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_file_enum,  PI_SRVSVC, NULL, "Enumerate open files", "" },
	{ "netremotetod",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_remote_tod, PI_SRVSVC, NULL, "Fetch remote time of day", "" },

	{ NULL }
};