1*6185db85Sdougm /*
2*6185db85Sdougm  * CDDL HEADER START
3*6185db85Sdougm  *
4*6185db85Sdougm  * The contents of this file are subject to the terms of the
5*6185db85Sdougm  * Common Development and Distribution License (the "License").
6*6185db85Sdougm  * You may not use this file except in compliance with the License.
7*6185db85Sdougm  *
8*6185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
10*6185db85Sdougm  * See the License for the specific language governing permissions
11*6185db85Sdougm  * and limitations under the License.
12*6185db85Sdougm  *
13*6185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
14*6185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
16*6185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
17*6185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
18*6185db85Sdougm  *
19*6185db85Sdougm  * CDDL HEADER END
20*6185db85Sdougm  */
21*6185db85Sdougm 
22*6185db85Sdougm /*
23*6185db85Sdougm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*6185db85Sdougm  * Use is subject to license terms.
25*6185db85Sdougm  */
26*6185db85Sdougm 
27*6185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*6185db85Sdougm 
29*6185db85Sdougm #include <stdlib.h>
30*6185db85Sdougm #include <stdio.h>
31*6185db85Sdougm #include <string.h>
32*6185db85Sdougm #include <ctype.h>
33*6185db85Sdougm #include <unistd.h>
34*6185db85Sdougm #include <getopt.h>
35*6185db85Sdougm #include <libgen.h>
36*6185db85Sdougm 
37*6185db85Sdougm #include <libshare.h>
38*6185db85Sdougm #include "sharemgr.h"
39*6185db85Sdougm #include <libintl.h>
40*6185db85Sdougm #include <locale.h>
41*6185db85Sdougm 
42*6185db85Sdougm char *protocol = NULL;
43*6185db85Sdougm static int help = 0;
44*6185db85Sdougm 
45*6185db85Sdougm static int run_command(char *, int, char **, char *);
46*6185db85Sdougm extern sa_command_t *sa_lookup(char *, char *);
47*6185db85Sdougm extern void sub_command_help(char *proto);
48*6185db85Sdougm 
49*6185db85Sdougm static void
50*6185db85Sdougm global_help()
51*6185db85Sdougm {
52*6185db85Sdougm 	(void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
53*6185db85Sdougm 	sub_command_help(NULL);
54*6185db85Sdougm }
55*6185db85Sdougm 
56*6185db85Sdougm int
57*6185db85Sdougm main(int argc, char *argv[])
58*6185db85Sdougm {
59*6185db85Sdougm 	int c;
60*6185db85Sdougm 	int rval;
61*6185db85Sdougm 	char *command = NULL;
62*6185db85Sdougm 
63*6185db85Sdougm 	/*
64*6185db85Sdougm 	 * make sure locale and gettext domain is setup
65*6185db85Sdougm 	 */
66*6185db85Sdougm 	(void) setlocale(LC_ALL, "");
67*6185db85Sdougm 	(void) textdomain(TEXT_DOMAIN);
68*6185db85Sdougm 
69*6185db85Sdougm 	/*
70*6185db85Sdougm 	 * initialize the plugin architecture.
71*6185db85Sdougm 	 * Plugins are needed in the event of a global help
72*6185db85Sdougm 	 * request.
73*6185db85Sdougm 	 */
74*6185db85Sdougm 
75*6185db85Sdougm 	sa_init(SA_INIT_SHARE_API);
76*6185db85Sdougm 	optind = 1;	/* reset to beginning */
77*6185db85Sdougm 
78*6185db85Sdougm 	/*
79*6185db85Sdougm 	 * parse enough of command line to get protocol, if any.
80*6185db85Sdougm 	 * Note that options need to come "after" the subcommand.
81*6185db85Sdougm 	 */
82*6185db85Sdougm 	command = basename(argv[0]);
83*6185db85Sdougm 	if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
84*6185db85Sdougm 	    while ((c = getopt(argc, argv, "h?")) != EOF) {
85*6185db85Sdougm 		switch (c) {
86*6185db85Sdougm 		default:
87*6185db85Sdougm 		case 'h':
88*6185db85Sdougm 		case '?':
89*6185db85Sdougm 		    help = 1;
90*6185db85Sdougm 		    break;
91*6185db85Sdougm 		}
92*6185db85Sdougm 	    }
93*6185db85Sdougm 	    if (argc == 1)
94*6185db85Sdougm 		help = 1;
95*6185db85Sdougm 	}
96*6185db85Sdougm 
97*6185db85Sdougm 	if (strcmp(command, "sharemgr") == 0) {
98*6185db85Sdougm 	    command = argv[optind];
99*6185db85Sdougm 	    argv++;
100*6185db85Sdougm 	    argc--;
101*6185db85Sdougm 	}
102*6185db85Sdougm 
103*6185db85Sdougm 	if (help) {
104*6185db85Sdougm 		/* no subcommand */
105*6185db85Sdougm 		global_help();
106*6185db85Sdougm 		exit(SA_OK);
107*6185db85Sdougm 	}
108*6185db85Sdougm 
109*6185db85Sdougm 	/*
110*6185db85Sdougm 	 * now have enough to parse rest of command line
111*6185db85Sdougm 	 */
112*6185db85Sdougm 	rval = run_command(command, argc, argv, protocol);
113*6185db85Sdougm 
114*6185db85Sdougm 	sa_fini();
115*6185db85Sdougm 	return (rval);
116*6185db85Sdougm }
117*6185db85Sdougm 
118*6185db85Sdougm static int
119*6185db85Sdougm run_command(char *command, int argc, char *argv[], char *proto)
120*6185db85Sdougm {
121*6185db85Sdougm 	sa_command_t *cmdvec;
122*6185db85Sdougm 	int ret;
123*6185db85Sdougm 
124*6185db85Sdougm 	/*
125*6185db85Sdougm 	 * To get here, we know there should be a command due to the
126*6185db85Sdougm 	 * preprocessing done earlier.  Need to find the protocol
127*6185db85Sdougm 	 * that is being affected. If no protocol, then it is ALL
128*6185db85Sdougm 	 * protocols.
129*6185db85Sdougm 	 *
130*6185db85Sdougm 	 * We don't currently use the protocol here at this point. It
131*6185db85Sdougm 	 * is left in as a placeholder for the future addition of
132*6185db85Sdougm 	 * protocol specific sub-commands.
133*6185db85Sdougm 	 *
134*6185db85Sdougm 	 * Known sub-commands are handled at this level. An unknown
135*6185db85Sdougm 	 * command will be passed down to the shared object that
136*6185db85Sdougm 	 * actually implements it. We can do this since the semantics
137*6185db85Sdougm 	 * of the common sub-commands is well defined.
138*6185db85Sdougm 	 */
139*6185db85Sdougm 
140*6185db85Sdougm 	cmdvec = sa_lookup(command, proto);
141*6185db85Sdougm 	if (cmdvec == NULL) {
142*6185db85Sdougm 		(void) printf(gettext("command %s not found\n"), command);
143*6185db85Sdougm 		exit(1);
144*6185db85Sdougm 	}
145*6185db85Sdougm 	/*
146*6185db85Sdougm 	 * need to check priviledges and restrict what can be done
147*6185db85Sdougm 	 * based on least priviledge and sub-command so pass this in
148*6185db85Sdougm 	 * as a flag.
149*6185db85Sdougm 	 */
150*6185db85Sdougm 	ret = cmdvec->cmdfunc(cmdvec->priv, argc, argv);
151*6185db85Sdougm 	return (ret);
152*6185db85Sdougm }
153