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  */
226185db85Sdougm /*
23549ec3ffSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
276185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
296185db85Sdougm #include <stdlib.h>
306185db85Sdougm #include <stdio.h>
316185db85Sdougm #include <string.h>
326185db85Sdougm #include <ctype.h>
336185db85Sdougm #include <unistd.h>
346185db85Sdougm #include <getopt.h>
356185db85Sdougm #include <libgen.h>
376185db85Sdougm #include <libshare.h>
386185db85Sdougm #include "sharemgr.h"
396185db85Sdougm #include <libintl.h>
406185db85Sdougm #include <locale.h>
426185db85Sdougm char *protocol = NULL;
436185db85Sdougm static int help = 0;
45549ec3ffSdougm static int run_command(char *, int, char **, char *, sa_handle_t);
466185db85Sdougm extern sa_command_t *sa_lookup(char *, char *);
476185db85Sdougm extern void sub_command_help(char *proto);
496185db85Sdougm static void
global_help()506185db85Sdougm global_help()
516185db85Sdougm {
526185db85Sdougm 	(void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
536185db85Sdougm 	sub_command_help(NULL);
546185db85Sdougm }
566185db85Sdougm int
main(int argc,char * argv[])576185db85Sdougm main(int argc, char *argv[])
586185db85Sdougm {
596185db85Sdougm 	int c;
606185db85Sdougm 	int rval;
616185db85Sdougm 	char *command = NULL;
62549ec3ffSdougm 	sa_handle_t handle;
646185db85Sdougm 	/*
656185db85Sdougm 	 * make sure locale and gettext domain is setup
666185db85Sdougm 	 */
676185db85Sdougm 	(void) setlocale(LC_ALL, "");
686185db85Sdougm 	(void) textdomain(TEXT_DOMAIN);
706185db85Sdougm 	/*
716185db85Sdougm 	 * parse enough of command line to get protocol, if any.
726185db85Sdougm 	 * Note that options need to come "after" the subcommand.
736185db85Sdougm 	 */
746185db85Sdougm 	command = basename(argv[0]);
756185db85Sdougm 	if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
76*25a68471Sdougm 		while ((c = getopt(argc, argv, "h?")) != EOF) {
77*25a68471Sdougm 			switch (c) {
78*25a68471Sdougm 			default:
79*25a68471Sdougm 			case 'h':
80*25a68471Sdougm 			case '?':
81*25a68471Sdougm 				help = 1;
82*25a68471Sdougm 				break;
83*25a68471Sdougm 			}
846185db85Sdougm 		}
85*25a68471Sdougm 		if (argc == 1)
86*25a68471Sdougm 			help = 1;
876185db85Sdougm 	}
896185db85Sdougm 	if (strcmp(command, "sharemgr") == 0) {
90*25a68471Sdougm 		command = argv[optind];
91*25a68471Sdougm 		argv++;
92*25a68471Sdougm 		argc--;
936185db85Sdougm 	}
956185db85Sdougm 	if (help) {
966185db85Sdougm 		/* no subcommand */
976185db85Sdougm 		global_help();
986185db85Sdougm 		exit(SA_OK);
996185db85Sdougm 	}
1016185db85Sdougm 	/*
1026185db85Sdougm 	 * now have enough to parse rest of command line
1037d968cb8Sdougm 	 *
1047d968cb8Sdougm 	 * First, initialize the plugin architecture.
1057d968cb8Sdougm 	 * Plugins are needed in the event of a global help
1067d968cb8Sdougm 	 * request.
1077d968cb8Sdougm 	 *
1087d968cb8Sdougm 	 * reset optind to 1 so the parsing that takes place in
1097d968cb8Sdougm 	 * sa_init() will work correctly.
1107d968cb8Sdougm 	 */
1127d968cb8Sdougm 	optind = 1;
113549ec3ffSdougm 	handle = sa_init(SA_INIT_SHARE_API);
1157d968cb8Sdougm 	/*
1167d968cb8Sdougm 	 * reset optind again since we will start parsing all over in
1177d968cb8Sdougm 	 * the sub-commands.
1186185db85Sdougm 	 */
1197d968cb8Sdougm 	optind = 1;
120549ec3ffSdougm 	rval = run_command(command, argc, argv, protocol, handle);
122549ec3ffSdougm 	sa_fini(handle);
1236185db85Sdougm 	return (rval);
1246185db85Sdougm }
1266185db85Sdougm static int
run_command(char * command,int argc,char * argv[],char * proto,sa_handle_t handle)127549ec3ffSdougm run_command(char *command, int argc, char *argv[], char *proto,
128549ec3ffSdougm 		sa_handle_t handle)
1296185db85Sdougm {
1306185db85Sdougm 	sa_command_t *cmdvec;
1316185db85Sdougm 	int ret;
1336185db85Sdougm 	/*
1346185db85Sdougm 	 * To get here, we know there should be a command due to the
1356185db85Sdougm 	 * preprocessing done earlier.  Need to find the protocol
1366185db85Sdougm 	 * that is being affected. If no protocol, then it is ALL
1376185db85Sdougm 	 * protocols.
1386185db85Sdougm 	 *
1396185db85Sdougm 	 * We don't currently use the protocol here at this point. It
1406185db85Sdougm 	 * is left in as a placeholder for the future addition of
1416185db85Sdougm 	 * protocol specific sub-commands.
1426185db85Sdougm 	 *
1436185db85Sdougm 	 * Known sub-commands are handled at this level. An unknown
1446185db85Sdougm 	 * command will be passed down to the shared object that
1456185db85Sdougm 	 * actually implements it. We can do this since the semantics
1466185db85Sdougm 	 * of the common sub-commands is well defined.
1476185db85Sdougm 	 */
1496185db85Sdougm 	cmdvec = sa_lookup(command, proto);
1506185db85Sdougm 	if (cmdvec == NULL) {
1516185db85Sdougm 		(void) printf(gettext("command %s not found\n"), command);
1526185db85Sdougm 		exit(1);
1536185db85Sdougm 	}
1546185db85Sdougm 	/*
1556185db85Sdougm 	 * need to check priviledges and restrict what can be done
1566185db85Sdougm 	 * based on least priviledge and sub-command so pass this in
1576185db85Sdougm 	 * as a flag.
1586185db85Sdougm 	 */
159549ec3ffSdougm 	ret = cmdvec->cmdfunc(handle, cmdvec->priv, argc, argv);
1606185db85Sdougm 	return (ret);
1616185db85Sdougm }