summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dfs.cmds/sharectl/sharectl.c
blob: 7c4e985a9e49d65cf59f3593f5b5e8576e40ef7e (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2012 Milan Jurik. All rights reserved.
 * Copyright 2016 Nexenta Systems, Inc.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>

#include "libshare.h"
#include <sharemgr.h>

#include <libintl.h>
#include <locale.h>

static int run_command(char *, int, char **, sa_handle_t);
static void sub_command_help(char *proto);

static void
global_help()
{
	(void) printf(gettext("usage: sharectl <subcommand> [<options>]\n"));
	sub_command_help(NULL);
}

int
main(int argc, char *argv[])
{
	int c;
	int help = 0;
	int rval;
	char *command;
	sa_handle_t handle;

	/*
	 * make sure locale and gettext domain is setup
	 */
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	handle = sa_init(SA_INIT_CONTROL_API);

	while ((c = getopt(argc, argv, "h?")) != EOF) {
		switch (c) {
		case '?':
		case 'h':
			help = 1;
			break;
		default:
			(void) printf(gettext("Invalid option: %c\n"), c);
		}
	}
	if (optind == argc || help) {
		/* no subcommand */
		global_help();
		exit(0);
	}
	optind = 1;

	/*
	 * now have enough to parse rest of command line
	 */
	command = argv[optind];
	rval = run_command(command, argc - optind, argv + optind, handle);

	sa_fini(handle);
	return (rval);
}

char *
sc_get_usage(sc_usage_t index)
{
	char *ret = NULL;

	switch (index) {
	case USAGE_CTL_DELSECT:
		ret = gettext("delsect\t<section> <proto>");
		break;
	case USAGE_CTL_GET:
		ret = gettext("get\t[-p <property>]... <proto>");
		break;
	case USAGE_CTL_SET:
		ret = gettext("set\t{-p <property>=<value>}... <proto>");
		break;
	case USAGE_CTL_STATUS:
		ret = gettext("status\t[<proto>]...");
		break;
	}
	return (ret);
}

/*ARGSUSED*/
static int
sc_get(sa_handle_t handle, int flags, int argc, char *argv[])
{
	char *proto = NULL;
	struct options *optlist = NULL;
	int ret = SA_OK;
	int c;
	sa_protocol_properties_t propset, propsect;
	sa_property_t prop;
	char *section, *value, *name;
	int first = 1;

	while ((c = getopt(argc, argv, "?hp:")) != EOF) {
		switch (c) {
		case 'p':
			ret = add_opt(&optlist, optarg, 1);
			if (ret != SA_OK) {
				(void) printf(gettext(
				    "Problem with property: %s\n"), optarg);
				return (SA_NO_MEMORY);
			}
			break;
		default:
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_GET));
			return (SA_SYNTAX_ERR);
		case '?':
		case 'h':
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_GET));
			return (SA_OK);
		}
	}

	if (optind >= argc) {
		(void) printf(gettext("usage: %s\n"),
		    sc_get_usage(USAGE_CTL_GET));
		(void) printf(gettext("\tprotocol must be specified.\n"));
		return (SA_INVALID_PROTOCOL);
	}

	proto = argv[optind];
	if (!sa_valid_protocol(proto)) {
		(void) printf(gettext("Invalid protocol specified: %s\n"),
		    proto);
		return (SA_INVALID_PROTOCOL);
	}
	propset = sa_proto_get_properties(proto);
	if (propset == NULL)
		return (ret);

	if (optlist == NULL) {
		/* Display all known properties for this protocol */
		for (propsect = sa_get_protocol_section(propset, NULL);
		    propsect != NULL;
		    propsect = sa_get_next_protocol_section(propsect, NULL)) {
			section = sa_get_property_attr(propsect,
			    "name");
			/*
			 * If properties are organized into sections, as
			 * in the SMB client, print the section name.
			 */
			if (sa_proto_get_featureset(proto) &
			    SA_FEATURE_HAS_SECTIONS) {
				if (!first)
					(void) printf("\n");
				first = 0;
				(void) printf("[%s]\n",
				    section != NULL ? section : "");
			}
			if (section != NULL)
				sa_free_attr_string(section);

			/* Display properties for this section */
			for (prop = sa_get_protocol_property(propsect, NULL);
			    prop != NULL;
			    prop = sa_get_next_protocol_property(prop, NULL)) {

				/* get and display the property and value */
				name = sa_get_property_attr(prop, "type");
				if (name != NULL) {
					value = sa_get_property_attr(prop,
					    "value");
					(void) printf(gettext("%s=%s\n"), name,
					    value != NULL ? value : "");
				}
				if (value != NULL)
					sa_free_attr_string(value);
				if (name != NULL)
					sa_free_attr_string(name);
			}
		}
	} else {
		struct options *opt;

		/* list the specified option(s) */
		for (opt = optlist; opt != NULL; opt = opt->next) {
			int printed = 0;

			for (propsect = sa_get_protocol_section(propset, NULL);
			    propsect != NULL;
			    propsect = sa_get_next_protocol_section(propsect,
			    NULL)) {

				section = sa_get_property_attr(propsect,
				    "name");
				for (prop = sa_get_protocol_property(propsect,
				    opt->optname);
				    prop != NULL;
				    prop = sa_get_next_protocol_property(
				    propsect, opt->optname)) {
					value = sa_get_property_attr(prop,
					    "value");
					if (sa_proto_get_featureset(proto) &
					    SA_FEATURE_HAS_SECTIONS) {
						(void) printf(
						    gettext("[%s] %s=%s\n"),
						    section != NULL ?
						    section : "", opt->optname,
						    value != NULL ? value : "");
					} else {
						(void) printf(
						    gettext("%s=%s\n"),
						    opt->optname,
						    value != NULL ? value : "");
					}
					if (value != NULL)
						sa_free_attr_string(value);
					printed = 1;
				}
				if (section != NULL)
					sa_free_attr_string(section);
			}
			if (!printed) {
				(void) printf(gettext("%s: not defined\n"),
				    opt->optname);
				ret = SA_NO_SUCH_PROP;
			}
		}
	}
	return (ret);
}

/*ARGSUSED*/
static int
sc_set(sa_handle_t handle, int flags, int argc, char *argv[])
{
	char *proto = NULL;
	struct options *optlist = NULL;
	sa_protocol_properties_t propsect;
	int ret = SA_OK;
	int c;
	int err;
	sa_protocol_properties_t propset;
	sa_property_t prop;

	while ((c = getopt(argc, argv, "?hp:")) != EOF) {
		switch (c) {
		case 'p':
			ret = add_opt(&optlist, optarg, 0);
			if (ret != SA_OK) {
				(void) printf(gettext(
				    "Problem with property: %s\n"), optarg);
				return (SA_NO_MEMORY);
			}
			break;
		default:
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_SET));
			return (SA_SYNTAX_ERR);
		case '?':
		case 'h':
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_SET));
			return (SA_OK);
		}
	}

	if (optind >= argc) {
		(void) printf(gettext("usage: %s\n"),
		    sc_get_usage(USAGE_CTL_SET));
		(void) printf(gettext("\tprotocol must be specified.\n"));
		return (SA_INVALID_PROTOCOL);
	}

	proto = argv[optind];
	if (!sa_valid_protocol(proto)) {
		(void) printf(gettext("Invalid protocol specified: %s\n"),
		    proto);
		return (SA_INVALID_PROTOCOL);
	}
	propset = sa_proto_get_properties(proto);
	if (propset == NULL)
		return (ret);

	if (optlist == NULL) {
		(void) printf(gettext("usage: %s\n"),
		    sc_get_usage(USAGE_CTL_SET));
		(void) printf(gettext(
		    "\tat least one property and value "
		    "must be specified\n"));
	} else {
		struct options *opt;
		char *section = NULL;
		/* fetch and change the specified option(s) */
		for (opt = optlist; opt != NULL; opt = opt->next) {
			if (strncmp("section", opt->optname, 7) == 0) {
				if (section != NULL)
					free(section);
				section = strdup(opt->optvalue);
				continue;
			}
			if (sa_proto_get_featureset(proto) &
			    SA_FEATURE_HAS_SECTIONS) {
				propsect = sa_get_protocol_section(propset,
				    section);
				prop = sa_get_protocol_property(propsect,
				    opt->optname);
			} else {
				prop = sa_get_protocol_property(propset,
				    opt->optname);
			}
			if (prop == NULL && sa_proto_get_featureset(proto) &
			    SA_FEATURE_ADD_PROPERTIES) {
				sa_property_t sect;
				sect = sa_create_section(section, NULL);
				sa_set_section_attr(sect, "type", proto);
				(void) sa_add_protocol_property(propset, sect);
				prop = sa_create_property(
				    opt->optname, opt->optvalue);
				(void) sa_add_protocol_property(sect, prop);
			}
			if (prop != NULL) {
				/*
				 * "err" is used in order to prevent
				 * setting ret to SA_OK if there has
				 * been a real error. We want to be
				 * able to return an error status on
				 * exit in that case. Error messages
				 * are printed for each error, so we
				 * only care on exit that there was an
				 * error and not the specific error
				 * value.
				 */
				err = sa_set_protocol_property(prop, section,
				    opt->optvalue);
				if (err != SA_OK) {
					(void) printf(gettext(
					    "Could not set property"
					    " %s: %s\n"),
					    opt->optname, sa_errorstr(err));
					ret = err;
				}
			} else {
				(void) printf(gettext("%s: not defined\n"),
				    opt->optname);
				ret = SA_NO_SUCH_PROP;
			}
		}
	}
	return (ret);
}

static void
show_status(char *proto)
{
	char *status;
	uint64_t features;

	status = sa_get_protocol_status(proto);
	features = sa_proto_get_featureset(proto);
	(void) printf("%s\t%s", proto, status ? gettext(status) : "-");
	if (status != NULL)
		free(status);
	/*
	 * Need to flag a client only protocol so test suites can
	 * remove it from consideration.
	 */
	if (!(features & SA_FEATURE_SERVER))
		(void) printf(" client");
	(void) printf("\n");
}

static int
valid_proto(char **protos, int num, char *proto)
{
	int i;
	for (i = 0; i < num; i++)
		if (strcmp(protos[i], proto) == 0)
			return (1);
	return (0);
}

/*ARGSUSED*/
static int
sc_status(sa_handle_t handle, int flags, int argc, char *argv[])
{
	char **protos;
	int ret = SA_OK;
	int c;
	int i;
	int num_proto;
	int verbose = 0;

	while ((c = getopt(argc, argv, "?hv")) != EOF) {
		switch (c) {
		case 'v':
			verbose++;
			break;
		case '?':
		case 'h':
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_STATUS));
			return (SA_OK);
		default:
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_STATUS));
			return (SA_SYNTAX_ERR);
		}
	}

	num_proto = sa_get_protocols(&protos);
	if (optind == argc) {
		/* status for all protocols */
		for (i = 0; i < num_proto; i++) {
			show_status(protos[i]);
		}
	} else {
		for (i = optind; i < argc; i++) {
			if (valid_proto(protos, num_proto, argv[i])) {
				show_status(argv[i]);
			} else {
				(void) printf(gettext("Invalid protocol: %s\n"),
				    argv[i]);
				ret = SA_INVALID_PROTOCOL;
			}
		}
	}
	if (protos != NULL)
		free(protos);
	return (ret);
}

/*ARGSUSED*/
static int
sc_delsect(sa_handle_t handle, int flags, int argc, char *argv[])
{
	char *proto = NULL;
	char *section = NULL;
	sa_protocol_properties_t propset;
	sa_protocol_properties_t propsect;
	int ret = SA_OK;
	int c;

	while ((c = getopt(argc, argv, "?h")) != EOF) {
		switch (c) {
		default:
			ret = SA_SYNTAX_ERR;
			/*FALLTHROUGH*/
		case '?':
		case 'h':
			(void) printf(gettext("usage: %s\n"),
			    sc_get_usage(USAGE_CTL_DELSECT));
			return (ret);
		}
		/*NOTREACHED*/
	}

	section = argv[optind++];

	if (optind >= argc) {
		(void) printf(gettext("usage: %s\n"),
		    sc_get_usage(USAGE_CTL_DELSECT));
		(void) printf(gettext(
		    "\tsection and protocol must be specified.\n"));
		return (SA_INVALID_PROTOCOL);
	}

	proto = argv[optind];
	if (!sa_valid_protocol(proto)) {
		(void) printf(gettext("Invalid protocol specified: %s\n"),
		    proto);
		return (SA_INVALID_PROTOCOL);
	}

	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
		(void) printf(gettext("Protocol %s does not have sections\n"),
		    proto);
		return (SA_NOT_SUPPORTED);
	}

	propset = sa_proto_get_properties(proto);
	if (propset == NULL) {
		(void) printf(gettext("Cannot get properties for %s\n"),
		    proto);
		return (SA_NO_PROPERTIES);
	}

	propsect = sa_get_protocol_section(propset, section);
	if (propsect == NULL) {
		(void) printf(gettext("Cannot find section %s for proto %s\n"),
		    section, proto);
		return (SA_NO_SUCH_SECTION);
	}

	ret = sa_proto_delete_section(proto, section);

	return (ret);
}

static sa_command_t commands[] = {
	{"delsect", 0, sc_delsect, USAGE_CTL_DELSECT},
	{"get", 0, sc_get, USAGE_CTL_GET},
	{"set", 0, sc_set, USAGE_CTL_SET},
	{"status", 0, sc_status, USAGE_CTL_STATUS},
	{NULL, 0, NULL, 0},
};

/*ARGSUSED*/
void
sub_command_help(char *proto)
{
	int i;

	for (i = 0; commands[i].cmdname != NULL; i++) {
		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
			(void) printf("\t%s\n",
			    sc_get_usage((sc_usage_t)commands[i].cmdidx));
	}
}

sa_command_t *
sa_lookup(char *cmd)
{
	int i;
	size_t len;

	len = strlen(cmd);
	for (i = 0; commands[i].cmdname != NULL; i++) {
		if (strncmp(cmd, commands[i].cmdname, len) == 0)
			return (&commands[i]);
	}
	return (NULL);
}

static int
run_command(char *command, int argc, char *argv[], sa_handle_t handle)
{
	sa_command_t *cmdvec;
	int ret;

	/*
	 * To get here, we know there should be a command due to the
	 * preprocessing done earlier.  Need to find the protocol
	 * that is being affected. If no protocol, then it is ALL
	 * protocols.
	 *
	 * ??? do we really need the protocol at this level? it may be
	 * sufficient to let the commands look it up if needed since
	 * not all commands do proto specific things
	 *
	 * Known sub-commands are handled at this level. An unknown
	 * command will be passed down to the shared object that
	 * actually implements it. We can do this since the semantics
	 * of the common sub-commands is well defined.
	 */

	cmdvec = sa_lookup(command);
	if (cmdvec == NULL) {
		(void) printf(gettext("command %s not found\n"), command);
		exit(1);
	}
	/*
	 * need to check priviledges and restrict what can be done
	 * based on least priviledge and sub-command.
	 */
	ret = cmdvec->cmdfunc(handle, 0, argc, argv);
	return (ret);
}