16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
23549ec3ffSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm #include <stdlib.h>
286185db85Sdougm #include <stdio.h>
296185db85Sdougm #include <string.h>
306185db85Sdougm #include <ctype.h>
316185db85Sdougm #include <unistd.h>
326185db85Sdougm #include <getopt.h>
336185db85Sdougm #include <libgen.h>
346185db85Sdougm 
356185db85Sdougm #include <libshare.h>
366185db85Sdougm #include "sharemgr.h"
376185db85Sdougm #include <libintl.h>
386185db85Sdougm #include <locale.h>
396185db85Sdougm 
406185db85Sdougm char *protocol = NULL;
416185db85Sdougm static int help = 0;
426185db85Sdougm 
43549ec3ffSdougm static int run_command(char *, int, char **, char *, sa_handle_t);
446185db85Sdougm extern sa_command_t *sa_lookup(char *, char *);
456185db85Sdougm extern void sub_command_help(char *proto);
466185db85Sdougm 
476185db85Sdougm static void
global_help()486185db85Sdougm global_help()
496185db85Sdougm {
506185db85Sdougm 	(void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
516185db85Sdougm 	sub_command_help(NULL);
526185db85Sdougm }
536185db85Sdougm 
546185db85Sdougm int
main(int argc,char * argv[])556185db85Sdougm main(int argc, char *argv[])
566185db85Sdougm {
576185db85Sdougm 	int c;
586185db85Sdougm 	int rval;
596185db85Sdougm 	char *command = NULL;
60549ec3ffSdougm 	sa_handle_t handle;
616185db85Sdougm 
626185db85Sdougm 	/*
636185db85Sdougm 	 * make sure locale and gettext domain is setup
646185db85Sdougm 	 */
656185db85Sdougm 	(void) setlocale(LC_ALL, "");
666185db85Sdougm 	(void) textdomain(TEXT_DOMAIN);
676185db85Sdougm 
686185db85Sdougm 	/*
696185db85Sdougm 	 * parse enough of command line to get protocol, if any.
706185db85Sdougm 	 * Note that options need to come "after" the subcommand.
716185db85Sdougm 	 */
726185db85Sdougm 	command = basename(argv[0]);
736185db85Sdougm 	if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
74*25a68471Sdougm 		while ((c = getopt(argc, argv, "h?")) != EOF) {
75*25a68471Sdougm 			switch (c) {
76*25a68471Sdougm 			default:
77*25a68471Sdougm 			case 'h':
78*25a68471Sdougm 			case '?':
79*25a68471Sdougm 				help = 1;
80*25a68471Sdougm 				break;
81*25a68471Sdougm 			}
826185db85Sdougm 		}
83*25a68471Sdougm 		if (argc == 1)
84*25a68471Sdougm 			help = 1;
856185db85Sdougm 	}
866185db85Sdougm 
876185db85Sdougm 	if (strcmp(command, "sharemgr") == 0) {
88*25a68471Sdougm 		command = argv[optind];
89*25a68471Sdougm 		argv++;
90*25a68471Sdougm 		argc--;
916185db85Sdougm 	}
926185db85Sdougm 
936185db85Sdougm 	if (help) {
946185db85Sdougm 		/* no subcommand */
956185db85Sdougm 		global_help();
966185db85Sdougm 		exit(SA_OK);
976185db85Sdougm 	}
986185db85Sdougm 
996185db85Sdougm 	/*
1006185db85Sdougm 	 * now have enough to parse rest of command line
1017d968cb8Sdougm 	 *
1027d968cb8Sdougm 	 * First, initialize the plugin architecture.
1037d968cb8Sdougm 	 * Plugins are needed in the event of a global help
1047d968cb8Sdougm 	 * request.
1057d968cb8Sdougm 	 *
1067d968cb8Sdougm 	 * reset optind to 1 so the parsing that takes place in
1077d968cb8Sdougm 	 * sa_init() will work correctly.
1087d968cb8Sdougm 	 */
1097d968cb8Sdougm 
1107d968cb8Sdougm 	optind = 1;
111549ec3ffSdougm 	handle = sa_init(SA_INIT_SHARE_API);
1127d968cb8Sdougm 
1137d968cb8Sdougm 	/*
1147d968cb8Sdougm 	 * reset optind again since we will start parsing all over in
1157d968cb8Sdougm 	 * the sub-commands.
1166185db85Sdougm 	 */
1177d968cb8Sdougm 	optind = 1;
118549ec3ffSdougm 	rval = run_command(command, argc, argv, protocol, handle);
1196185db85Sdougm 
120549ec3ffSdougm 	sa_fini(handle);
1216185db85Sdougm 	return (rval);
1226185db85Sdougm }
1236185db85Sdougm 
1246185db85Sdougm static int
run_command(char * command,int argc,char * argv[],char * proto,sa_handle_t handle)125549ec3ffSdougm run_command(char *command, int argc, char *argv[], char *proto,
126549ec3ffSdougm 		sa_handle_t handle)
1276185db85Sdougm {
1286185db85Sdougm 	sa_command_t *cmdvec;
1296185db85Sdougm 	int ret;
1306185db85Sdougm 
1316185db85Sdougm 	/*
1326185db85Sdougm 	 * To get here, we know there should be a command due to the
1336185db85Sdougm 	 * preprocessing done earlier.  Need to find the protocol
1346185db85Sdougm 	 * that is being affected. If no protocol, then it is ALL
1356185db85Sdougm 	 * protocols.
1366185db85Sdougm 	 *
1376185db85Sdougm 	 * We don't currently use the protocol here at this point. It
1386185db85Sdougm 	 * is left in as a placeholder for the future addition of
1396185db85Sdougm 	 * protocol specific sub-commands.
1406185db85Sdougm 	 *
1416185db85Sdougm 	 * Known sub-commands are handled at this level. An unknown
1426185db85Sdougm 	 * command will be passed down to the shared object that
1436185db85Sdougm 	 * actually implements it. We can do this since the semantics
1446185db85Sdougm 	 * of the common sub-commands is well defined.
1456185db85Sdougm 	 */
1466185db85Sdougm 
1476185db85Sdougm 	cmdvec = sa_lookup(command, proto);
1486185db85Sdougm 	if (cmdvec == NULL) {
1496185db85Sdougm 		(void) printf(gettext("command %s not found\n"), command);
1506185db85Sdougm 		exit(1);
1516185db85Sdougm 	}
1526185db85Sdougm 	/*
1536185db85Sdougm 	 * need to check priviledges and restrict what can be done
1546185db85Sdougm 	 * based on least priviledge and sub-command so pass this in
1556185db85Sdougm 	 * as a flag.
1566185db85Sdougm 	 */
157549ec3ffSdougm 	ret = cmdvec->cmdfunc(handle, cmdvec->priv, argc, argv);
1586185db85Sdougm 	return (ret);
1596185db85Sdougm }
160