1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM 
28*9781SMoriah.Waterland@Sun.COM /* unix system includes */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
31*9781SMoriah.Waterland@Sun.COM #include <stdarg.h>
32*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
33*9781SMoriah.Waterland@Sun.COM #include <string.h>
34*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
35*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
36*9781SMoriah.Waterland@Sun.COM #include <locale.h>
37*9781SMoriah.Waterland@Sun.COM #include <sys/param.h>
38*9781SMoriah.Waterland@Sun.COM #include <openssl/bio.h>
39*9781SMoriah.Waterland@Sun.COM 
40*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
41*9781SMoriah.Waterland@Sun.COM #include <pkgerr.h>
42*9781SMoriah.Waterland@Sun.COM #include <keystore.h>
43*9781SMoriah.Waterland@Sun.COM #include "pkgadm.h"
44*9781SMoriah.Waterland@Sun.COM #include "pkgadm_msgs.h"
45*9781SMoriah.Waterland@Sun.COM 
46*9781SMoriah.Waterland@Sun.COM /* initial error message buffer size */
47*9781SMoriah.Waterland@Sun.COM 
48*9781SMoriah.Waterland@Sun.COM #define	ERR_BUFSIZE		2048
49*9781SMoriah.Waterland@Sun.COM 
50*9781SMoriah.Waterland@Sun.COM /* Local Function Prototypes */
51*9781SMoriah.Waterland@Sun.COM 
52*9781SMoriah.Waterland@Sun.COM static void			print_version();
53*9781SMoriah.Waterland@Sun.COM int				get_dbstatus(int argc, char **argv);
54*9781SMoriah.Waterland@Sun.COM 
55*9781SMoriah.Waterland@Sun.COM /* holds subcommands and their definitions */
56*9781SMoriah.Waterland@Sun.COM struct cmd {
57*9781SMoriah.Waterland@Sun.COM 	char		*c_name;
58*9781SMoriah.Waterland@Sun.COM 	int		(*c_func)(int, char **);
59*9781SMoriah.Waterland@Sun.COM };
60*9781SMoriah.Waterland@Sun.COM 
61*9781SMoriah.Waterland@Sun.COM struct cmd  cmds[] = {
62*9781SMoriah.Waterland@Sun.COM 	{ "dbstatus",		get_dbstatus},
63*9781SMoriah.Waterland@Sun.COM 	{ "lock",		admin_lock},
64*9781SMoriah.Waterland@Sun.COM 	/* last one must be all NULLs */
65*9781SMoriah.Waterland@Sun.COM 	{ NULL, NULL }
66*9781SMoriah.Waterland@Sun.COM };
67*9781SMoriah.Waterland@Sun.COM 
68*9781SMoriah.Waterland@Sun.COM struct cmd cert_cmds[] = {
69*9781SMoriah.Waterland@Sun.COM 	{ "addcert",		addcert},
70*9781SMoriah.Waterland@Sun.COM 	{ "listcert",		listcert},
71*9781SMoriah.Waterland@Sun.COM 	{ "removecert",		removecert},
72*9781SMoriah.Waterland@Sun.COM 	/* last one must be all NULLs */
73*9781SMoriah.Waterland@Sun.COM 	{ NULL, NULL }
74*9781SMoriah.Waterland@Sun.COM };
75*9781SMoriah.Waterland@Sun.COM 
76*9781SMoriah.Waterland@Sun.COM 
77*9781SMoriah.Waterland@Sun.COM /*
78*9781SMoriah.Waterland@Sun.COM  * Function:	main
79*9781SMoriah.Waterland@Sun.COM  *
80*9781SMoriah.Waterland@Sun.COM  * Return:	0	- subprocessing successful
81*9781SMoriah.Waterland@Sun.COM  *			  scripts and reboot
82*9781SMoriah.Waterland@Sun.COM  *	[other]	- subprocessing-specific failure
83*9781SMoriah.Waterland@Sun.COM  */
84*9781SMoriah.Waterland@Sun.COM int
85*9781SMoriah.Waterland@Sun.COM main(int argc, char **argv)
86*9781SMoriah.Waterland@Sun.COM {
87*9781SMoriah.Waterland@Sun.COM 	char	cur_cmd;
88*9781SMoriah.Waterland@Sun.COM 	int	newargc;
89*9781SMoriah.Waterland@Sun.COM 	char	**newargv;
90*9781SMoriah.Waterland@Sun.COM 	int	i;
91*9781SMoriah.Waterland@Sun.COM 
92*9781SMoriah.Waterland@Sun.COM 	/* Should be defined by cc -D */
93*9781SMoriah.Waterland@Sun.COM #if	!defined(TEXT_DOMAIN)
94*9781SMoriah.Waterland@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"
95*9781SMoriah.Waterland@Sun.COM #endif
96*9781SMoriah.Waterland@Sun.COM 
97*9781SMoriah.Waterland@Sun.COM 	/* set the default text domain for messaging */
98*9781SMoriah.Waterland@Sun.COM 	(void) setlocale(LC_ALL, "");
99*9781SMoriah.Waterland@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
100*9781SMoriah.Waterland@Sun.COM 
101*9781SMoriah.Waterland@Sun.COM 	if (getenv("PKGADM_VERBOSE")) {
102*9781SMoriah.Waterland@Sun.COM 	    set_verbose(B_TRUE);
103*9781SMoriah.Waterland@Sun.COM 	}
104*9781SMoriah.Waterland@Sun.COM 
105*9781SMoriah.Waterland@Sun.COM 	/* Superficial check of the arguments. */
106*9781SMoriah.Waterland@Sun.COM 	if (argc <= 1) {
107*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_INFO, MSG_USAGE);
108*9781SMoriah.Waterland@Sun.COM 		return (1);
109*9781SMoriah.Waterland@Sun.COM 	}
110*9781SMoriah.Waterland@Sun.COM 
111*9781SMoriah.Waterland@Sun.COM 	/* first, process any arguments that can appear before the subcommand */
112*9781SMoriah.Waterland@Sun.COM 	while ((i = getopt(argc, argv, "vV?")) != EOF) {
113*9781SMoriah.Waterland@Sun.COM 		switch (i) {
114*9781SMoriah.Waterland@Sun.COM 		case 'v':	/* verbose mode enabled */
115*9781SMoriah.Waterland@Sun.COM 			set_verbose(B_TRUE);
116*9781SMoriah.Waterland@Sun.COM 			break;
117*9781SMoriah.Waterland@Sun.COM 		case 'V':
118*9781SMoriah.Waterland@Sun.COM 			print_version();
119*9781SMoriah.Waterland@Sun.COM 			return (0);
120*9781SMoriah.Waterland@Sun.COM 		case '?':
121*9781SMoriah.Waterland@Sun.COM 			log_msg(LOG_MSG_INFO, MSG_USAGE);
122*9781SMoriah.Waterland@Sun.COM 			return (0);
123*9781SMoriah.Waterland@Sun.COM 		}
124*9781SMoriah.Waterland@Sun.COM 	}
125*9781SMoriah.Waterland@Sun.COM 
126*9781SMoriah.Waterland@Sun.COM 	/* OK, hand it off to the subcommand processors */
127*9781SMoriah.Waterland@Sun.COM 	for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
128*9781SMoriah.Waterland@Sun.COM 		if (ci_streq(argv[optind], cmds[cur_cmd].c_name)) {
129*9781SMoriah.Waterland@Sun.COM 			/* make subcommand the first option */
130*9781SMoriah.Waterland@Sun.COM 			newargc = argc - optind;
131*9781SMoriah.Waterland@Sun.COM 			newargv = argv + optind;
132*9781SMoriah.Waterland@Sun.COM 			opterr = optind = 1; optopt = 0;
133*9781SMoriah.Waterland@Sun.COM 			return (cmds[cur_cmd].c_func(newargc, newargv));
134*9781SMoriah.Waterland@Sun.COM 		}
135*9781SMoriah.Waterland@Sun.COM 	}
136*9781SMoriah.Waterland@Sun.COM 
137*9781SMoriah.Waterland@Sun.COM 	/* initialize security library */
138*9781SMoriah.Waterland@Sun.COM 	sec_init();
139*9781SMoriah.Waterland@Sun.COM 
140*9781SMoriah.Waterland@Sun.COM 	/* OK, hand it off to the subcommand processors */
141*9781SMoriah.Waterland@Sun.COM 	for (cur_cmd = 0; cert_cmds[cur_cmd].c_name != NULL; cur_cmd++) {
142*9781SMoriah.Waterland@Sun.COM 		if (ci_streq(argv[optind], cert_cmds[cur_cmd].c_name)) {
143*9781SMoriah.Waterland@Sun.COM 			/* make subcommand the first option */
144*9781SMoriah.Waterland@Sun.COM 			newargc = argc - optind;
145*9781SMoriah.Waterland@Sun.COM 			newargv = argv + optind;
146*9781SMoriah.Waterland@Sun.COM 			opterr = optind = 1; optopt = 0;
147*9781SMoriah.Waterland@Sun.COM 			return (cert_cmds[cur_cmd].c_func(newargc, newargv));
148*9781SMoriah.Waterland@Sun.COM 		}
149*9781SMoriah.Waterland@Sun.COM 	}
150*9781SMoriah.Waterland@Sun.COM 
151*9781SMoriah.Waterland@Sun.COM 	/* bad subcommand */
152*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_ERR, MSG_BAD_SUB, argv[optind]);
153*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_INFO, MSG_USAGE);
154*9781SMoriah.Waterland@Sun.COM 	return (1);
155*9781SMoriah.Waterland@Sun.COM }
156*9781SMoriah.Waterland@Sun.COM 
157*9781SMoriah.Waterland@Sun.COM /*
158*9781SMoriah.Waterland@Sun.COM  * Name:	set_verbose
159*9781SMoriah.Waterland@Sun.COM  * Description:	Turns on verbose output
160*9781SMoriah.Waterland@Sun.COM  * Scope:	public
161*9781SMoriah.Waterland@Sun.COM  * Arguments:	verbose = B_TRUE indicates verbose mode
162*9781SMoriah.Waterland@Sun.COM  * Returns:	none
163*9781SMoriah.Waterland@Sun.COM  */
164*9781SMoriah.Waterland@Sun.COM void
165*9781SMoriah.Waterland@Sun.COM set_verbose(boolean_t setting)
166*9781SMoriah.Waterland@Sun.COM {
167*9781SMoriah.Waterland@Sun.COM 	log_set_verbose(setting);
168*9781SMoriah.Waterland@Sun.COM }
169*9781SMoriah.Waterland@Sun.COM 
170*9781SMoriah.Waterland@Sun.COM /*
171*9781SMoriah.Waterland@Sun.COM  * Name:	get_verbose
172*9781SMoriah.Waterland@Sun.COM  * Description:	Returns whether or not to output verbose messages
173*9781SMoriah.Waterland@Sun.COM  * Scope:	public
174*9781SMoriah.Waterland@Sun.COM  * Arguments:	none
175*9781SMoriah.Waterland@Sun.COM  * Returns:	B_TRUE - verbose messages should be output
176*9781SMoriah.Waterland@Sun.COM  */
177*9781SMoriah.Waterland@Sun.COM boolean_t
178*9781SMoriah.Waterland@Sun.COM get_verbose()
179*9781SMoriah.Waterland@Sun.COM {
180*9781SMoriah.Waterland@Sun.COM 	return (log_get_verbose());
181*9781SMoriah.Waterland@Sun.COM }
182*9781SMoriah.Waterland@Sun.COM 
183*9781SMoriah.Waterland@Sun.COM /*
184*9781SMoriah.Waterland@Sun.COM  * Name:	log_pkgerr
185*9781SMoriah.Waterland@Sun.COM  * Description:	Outputs pkgerr messages to logging facility.
186*9781SMoriah.Waterland@Sun.COM  * Scope:	public
187*9781SMoriah.Waterland@Sun.COM  * Arguments:	type - the severity of the message
188*9781SMoriah.Waterland@Sun.COM  *		err - error stack to dump to facility
189*9781SMoriah.Waterland@Sun.COM  * Returns:	none
190*9781SMoriah.Waterland@Sun.COM  */
191*9781SMoriah.Waterland@Sun.COM void
192*9781SMoriah.Waterland@Sun.COM log_pkgerr(LogMsgType type, PKG_ERR *err)
193*9781SMoriah.Waterland@Sun.COM {
194*9781SMoriah.Waterland@Sun.COM 	int i;
195*9781SMoriah.Waterland@Sun.COM 	for (i = 0; i < pkgerr_num(err); i++) {
196*9781SMoriah.Waterland@Sun.COM 		log_msg(type, "%s", pkgerr_get(err, i));
197*9781SMoriah.Waterland@Sun.COM 	}
198*9781SMoriah.Waterland@Sun.COM }
199*9781SMoriah.Waterland@Sun.COM 
200*9781SMoriah.Waterland@Sun.COM /*
201*9781SMoriah.Waterland@Sun.COM  * Name:	print_Version
202*9781SMoriah.Waterland@Sun.COM  * Desc:  Prints Version of packaging tools
203*9781SMoriah.Waterland@Sun.COM  * Arguments: none
204*9781SMoriah.Waterland@Sun.COM  * Returns: none
205*9781SMoriah.Waterland@Sun.COM  */
206*9781SMoriah.Waterland@Sun.COM static void
207*9781SMoriah.Waterland@Sun.COM print_version()
208*9781SMoriah.Waterland@Sun.COM {
209*9781SMoriah.Waterland@Sun.COM 	/* ignore any and all arguments, print version only */
210*9781SMoriah.Waterland@Sun.COM 	(void) fprintf(stdout, "%s\n", SUNW_PKGVERS);
211*9781SMoriah.Waterland@Sun.COM }
212*9781SMoriah.Waterland@Sun.COM 
213*9781SMoriah.Waterland@Sun.COM /*
214*9781SMoriah.Waterland@Sun.COM  * usage
215*9781SMoriah.Waterland@Sun.COM  *
216*9781SMoriah.Waterland@Sun.COM  * Outputs the usage string.
217*9781SMoriah.Waterland@Sun.COM  *
218*9781SMoriah.Waterland@Sun.COM  * Return:1
219*9781SMoriah.Waterland@Sun.COM  * Side effects: none
220*9781SMoriah.Waterland@Sun.COM  */
221*9781SMoriah.Waterland@Sun.COM static int
222*9781SMoriah.Waterland@Sun.COM usage()
223*9781SMoriah.Waterland@Sun.COM {
224*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_INFO, MSG_USAGE);
225*9781SMoriah.Waterland@Sun.COM 	return (1);
226*9781SMoriah.Waterland@Sun.COM }
227*9781SMoriah.Waterland@Sun.COM 
228*9781SMoriah.Waterland@Sun.COM /*
229*9781SMoriah.Waterland@Sun.COM  * get_dbstatus
230*9781SMoriah.Waterland@Sun.COM  *
231*9781SMoriah.Waterland@Sun.COM  * Return 'text' as the db status.
232*9781SMoriah.Waterland@Sun.COM  * Use the command line to determine if there is an alternate root.
233*9781SMoriah.Waterland@Sun.COM  *
234*9781SMoriah.Waterland@Sun.COM  * Return: 0 on success, nonzero on failure
235*9781SMoriah.Waterland@Sun.COM  * Side effects: none
236*9781SMoriah.Waterland@Sun.COM  */
237*9781SMoriah.Waterland@Sun.COM int
238*9781SMoriah.Waterland@Sun.COM get_dbstatus(int argc, char **argv)
239*9781SMoriah.Waterland@Sun.COM {
240*9781SMoriah.Waterland@Sun.COM 	/* Either accept 1 argument or 3 arguments where the second is -R */
241*9781SMoriah.Waterland@Sun.COM 	if (argc != 1 && (argc != 3 || strcmp(argv[1], "-R")))
242*9781SMoriah.Waterland@Sun.COM 		return (usage());
243*9781SMoriah.Waterland@Sun.COM 
244*9781SMoriah.Waterland@Sun.COM 	(void) printf("%s\n", PKGADM_DBSTATUS_TEXT);
245*9781SMoriah.Waterland@Sun.COM 
246*9781SMoriah.Waterland@Sun.COM 	return (0);
247*9781SMoriah.Waterland@Sun.COM }
248