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