xref: /onnv-gate/usr/src/cmd/svr4pkg/pkgtrans/main.c (revision 9781:ccf49524d5dc)
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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM #include <locale.h>
32*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
33*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
34*9781SMoriah.Waterland@Sun.COM #include <signal.h>
35*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
36*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
37*9781SMoriah.Waterland@Sun.COM #include <string.h>
38*9781SMoriah.Waterland@Sun.COM #include <pkgtrans.h>
39*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
40*9781SMoriah.Waterland@Sun.COM #include <pkglocs.h>
41*9781SMoriah.Waterland@Sun.COM #include <libadm.h>
42*9781SMoriah.Waterland@Sun.COM #include <libinst.h>
43*9781SMoriah.Waterland@Sun.COM 
44*9781SMoriah.Waterland@Sun.COM static int	options;
45*9781SMoriah.Waterland@Sun.COM static keystore_handle_t	keystore = NULL;
46*9781SMoriah.Waterland@Sun.COM 
47*9781SMoriah.Waterland@Sun.COM static void	usage(void);
48*9781SMoriah.Waterland@Sun.COM static void	trap(int signo);
49*9781SMoriah.Waterland@Sun.COM 
50*9781SMoriah.Waterland@Sun.COM #define	PASSWD_CMDLINE \
51*9781SMoriah.Waterland@Sun.COM 		"## WARNING: USING <%s> MAKES PASSWORD " \
52*9781SMoriah.Waterland@Sun.COM 		"VISIBLE TO ALL USERS."
53*9781SMoriah.Waterland@Sun.COM 
54*9781SMoriah.Waterland@Sun.COM #define	PASSPHRASE_PROMPT	"Enter keystore password:"
55*9781SMoriah.Waterland@Sun.COM #define	KEYSTORE_OPEN	"Retrieving signing certificates from keystore <%s>"
56*9781SMoriah.Waterland@Sun.COM #define	PARAM_LEN		"Parameter <%s> too long"
57*9781SMoriah.Waterland@Sun.COM 
58*9781SMoriah.Waterland@Sun.COM int
main(int argc,char * argv[])59*9781SMoriah.Waterland@Sun.COM main(int argc, char *argv[])
60*9781SMoriah.Waterland@Sun.COM {
61*9781SMoriah.Waterland@Sun.COM 	int	c;
62*9781SMoriah.Waterland@Sun.COM 	void	(*func)();
63*9781SMoriah.Waterland@Sun.COM 	extern char	*optarg;
64*9781SMoriah.Waterland@Sun.COM 	extern int	optind;
65*9781SMoriah.Waterland@Sun.COM 	char		*keystore_alias = NULL;
66*9781SMoriah.Waterland@Sun.COM 	char		*keystore_file = NULL;
67*9781SMoriah.Waterland@Sun.COM 	boolean_t	create_sig = B_FALSE;
68*9781SMoriah.Waterland@Sun.COM 	char		*homedir = NULL;
69*9781SMoriah.Waterland@Sun.COM 	PKG_ERR		*err;
70*9781SMoriah.Waterland@Sun.COM 	int		ret, len, homelen;
71*9781SMoriah.Waterland@Sun.COM 
72*9781SMoriah.Waterland@Sun.COM 	(void) setlocale(LC_ALL, "");
73*9781SMoriah.Waterland@Sun.COM 
74*9781SMoriah.Waterland@Sun.COM #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
75*9781SMoriah.Waterland@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"
76*9781SMoriah.Waterland@Sun.COM #endif
77*9781SMoriah.Waterland@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
78*9781SMoriah.Waterland@Sun.COM 
79*9781SMoriah.Waterland@Sun.COM 	(void) set_prog_name(argv[0]);
80*9781SMoriah.Waterland@Sun.COM 
81*9781SMoriah.Waterland@Sun.COM 	while ((c = getopt(argc, argv, "ga:P:k:snio?")) != EOF) {
82*9781SMoriah.Waterland@Sun.COM 		switch (c) {
83*9781SMoriah.Waterland@Sun.COM 		case 'n':
84*9781SMoriah.Waterland@Sun.COM 			options |= PT_RENAME;
85*9781SMoriah.Waterland@Sun.COM 			break;
86*9781SMoriah.Waterland@Sun.COM 
87*9781SMoriah.Waterland@Sun.COM 		case 'i':
88*9781SMoriah.Waterland@Sun.COM 			options |= PT_INFO_ONLY;
89*9781SMoriah.Waterland@Sun.COM 			break;
90*9781SMoriah.Waterland@Sun.COM 
91*9781SMoriah.Waterland@Sun.COM 		case 'o':
92*9781SMoriah.Waterland@Sun.COM 			options |= PT_OVERWRITE;
93*9781SMoriah.Waterland@Sun.COM 			break;
94*9781SMoriah.Waterland@Sun.COM 
95*9781SMoriah.Waterland@Sun.COM 		case 's':
96*9781SMoriah.Waterland@Sun.COM 			options |= PT_ODTSTREAM;
97*9781SMoriah.Waterland@Sun.COM 			break;
98*9781SMoriah.Waterland@Sun.COM 
99*9781SMoriah.Waterland@Sun.COM 		case 'g':
100*9781SMoriah.Waterland@Sun.COM 			/* this should eventually be a PT_ option */
101*9781SMoriah.Waterland@Sun.COM 			create_sig = B_TRUE;
102*9781SMoriah.Waterland@Sun.COM 			break;
103*9781SMoriah.Waterland@Sun.COM 
104*9781SMoriah.Waterland@Sun.COM 		case 'k':
105*9781SMoriah.Waterland@Sun.COM 			keystore_file = optarg;
106*9781SMoriah.Waterland@Sun.COM 			break;
107*9781SMoriah.Waterland@Sun.COM 
108*9781SMoriah.Waterland@Sun.COM 		case 'a':
109*9781SMoriah.Waterland@Sun.COM 			keystore_alias = optarg;
110*9781SMoriah.Waterland@Sun.COM 			break;
111*9781SMoriah.Waterland@Sun.COM 
112*9781SMoriah.Waterland@Sun.COM 		case 'P':
113*9781SMoriah.Waterland@Sun.COM 			set_passphrase_passarg(optarg);
114*9781SMoriah.Waterland@Sun.COM 			if (ci_strneq(optarg, "pass:", 5)) {
115*9781SMoriah.Waterland@Sun.COM 				/*
116*9781SMoriah.Waterland@Sun.COM 				 * passwords on the command line are highly
117*9781SMoriah.Waterland@Sun.COM 				 * insecure.  complain.
118*9781SMoriah.Waterland@Sun.COM 				 */
119*9781SMoriah.Waterland@Sun.COM 				logerr(gettext(PASSWD_CMDLINE), "pass:<pass>");
120*9781SMoriah.Waterland@Sun.COM 			}
121*9781SMoriah.Waterland@Sun.COM 			break;
122*9781SMoriah.Waterland@Sun.COM 
123*9781SMoriah.Waterland@Sun.COM 		default:
124*9781SMoriah.Waterland@Sun.COM 			usage();
125*9781SMoriah.Waterland@Sun.COM 			return (1);
126*9781SMoriah.Waterland@Sun.COM 		}
127*9781SMoriah.Waterland@Sun.COM 	}
128*9781SMoriah.Waterland@Sun.COM 	func = signal(SIGINT, trap);
129*9781SMoriah.Waterland@Sun.COM 	if (func != SIG_DFL)
130*9781SMoriah.Waterland@Sun.COM 		(void) signal(SIGINT, func);
131*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGHUP, trap);
132*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGQUIT, trap);
133*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGTERM, trap);
134*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGPIPE, trap);
135*9781SMoriah.Waterland@Sun.COM #ifndef SUNOS41
136*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGPWR, trap);
137*9781SMoriah.Waterland@Sun.COM #endif
138*9781SMoriah.Waterland@Sun.COM 
139*9781SMoriah.Waterland@Sun.COM 	if ((argc-optind) < 2) {
140*9781SMoriah.Waterland@Sun.COM 		usage();
141*9781SMoriah.Waterland@Sun.COM 		return (1);
142*9781SMoriah.Waterland@Sun.COM 	}
143*9781SMoriah.Waterland@Sun.COM 
144*9781SMoriah.Waterland@Sun.COM 	if (create_sig) {
145*9781SMoriah.Waterland@Sun.COM 		sec_init();
146*9781SMoriah.Waterland@Sun.COM 		err = pkgerr_new();
147*9781SMoriah.Waterland@Sun.COM 
148*9781SMoriah.Waterland@Sun.COM 		/* figure out which keystore to use */
149*9781SMoriah.Waterland@Sun.COM 		if (keystore_file == NULL) {
150*9781SMoriah.Waterland@Sun.COM 			if (geteuid() == 0) {
151*9781SMoriah.Waterland@Sun.COM 				/* we are superuser, so use their keystore */
152*9781SMoriah.Waterland@Sun.COM 				keystore_file = PKGSEC;
153*9781SMoriah.Waterland@Sun.COM 			} else {
154*9781SMoriah.Waterland@Sun.COM 				if ((homedir = getenv("HOME")) == NULL) {
155*9781SMoriah.Waterland@Sun.COM 				/*
156*9781SMoriah.Waterland@Sun.COM 				 * not superuser, but no home dir, so
157*9781SMoriah.Waterland@Sun.COM 				 * use superuser's keystore
158*9781SMoriah.Waterland@Sun.COM 				 */
159*9781SMoriah.Waterland@Sun.COM 					keystore_file = PKGSEC;
160*9781SMoriah.Waterland@Sun.COM 				} else {
161*9781SMoriah.Waterland@Sun.COM 				/* $HOME/.pkg/security\0 */
162*9781SMoriah.Waterland@Sun.COM 					homelen = strlen(homedir) + 15;
163*9781SMoriah.Waterland@Sun.COM 					keystore_file =
164*9781SMoriah.Waterland@Sun.COM 					    malloc(strlen(homedir) + 15);
165*9781SMoriah.Waterland@Sun.COM 					if (((len = snprintf(keystore_file,
166*9781SMoriah.Waterland@Sun.COM 					    homelen, "%s/%s", homedir,
167*9781SMoriah.Waterland@Sun.COM 					    ".pkg/security")) < 0) ||
168*9781SMoriah.Waterland@Sun.COM 					    (len >= homelen)) {
169*9781SMoriah.Waterland@Sun.COM 						logerr(gettext(PARAM_LEN),
170*9781SMoriah.Waterland@Sun.COM 						    "$HOME");
171*9781SMoriah.Waterland@Sun.COM 						quit(1);
172*9781SMoriah.Waterland@Sun.COM 					}
173*9781SMoriah.Waterland@Sun.COM 				}
174*9781SMoriah.Waterland@Sun.COM 			}
175*9781SMoriah.Waterland@Sun.COM 		}
176*9781SMoriah.Waterland@Sun.COM 
177*9781SMoriah.Waterland@Sun.COM 		logerr(gettext(KEYSTORE_OPEN), keystore_file);
178*9781SMoriah.Waterland@Sun.COM 
179*9781SMoriah.Waterland@Sun.COM 		set_passphrase_prompt(gettext(PASSPHRASE_PROMPT));
180*9781SMoriah.Waterland@Sun.COM 
181*9781SMoriah.Waterland@Sun.COM 		/* open keystore for reading */
182*9781SMoriah.Waterland@Sun.COM 		if (open_keystore(err, keystore_file, get_prog_name(),
183*9781SMoriah.Waterland@Sun.COM 		    pkg_passphrase_cb, KEYSTORE_DFLT_FLAGS, &keystore) != 0) {
184*9781SMoriah.Waterland@Sun.COM 			pkgerr(err);
185*9781SMoriah.Waterland@Sun.COM 			pkgerr_free(err);
186*9781SMoriah.Waterland@Sun.COM 			quit(1);
187*9781SMoriah.Waterland@Sun.COM 		}
188*9781SMoriah.Waterland@Sun.COM 
189*9781SMoriah.Waterland@Sun.COM 	} else {
190*9781SMoriah.Waterland@Sun.COM 		/* no signature, so don't use a keystore */
191*9781SMoriah.Waterland@Sun.COM 		keystore = NULL;
192*9781SMoriah.Waterland@Sun.COM 	}
193*9781SMoriah.Waterland@Sun.COM 
194*9781SMoriah.Waterland@Sun.COM 	ret = pkgtrans(flex_device(argv[optind], 1),
195*9781SMoriah.Waterland@Sun.COM 	    flex_device(argv[optind+1], 1), &argv[optind+2], options,
196*9781SMoriah.Waterland@Sun.COM 	    keystore, keystore_alias);
197*9781SMoriah.Waterland@Sun.COM 
198*9781SMoriah.Waterland@Sun.COM 	if (create_sig) {
199*9781SMoriah.Waterland@Sun.COM 		/* close keystore */
200*9781SMoriah.Waterland@Sun.COM 		if (close_keystore(err, keystore, NULL) != 0) {
201*9781SMoriah.Waterland@Sun.COM 			pkgerr(err);
202*9781SMoriah.Waterland@Sun.COM 			pkgerr_free(err);
203*9781SMoriah.Waterland@Sun.COM 			quit(1);
204*9781SMoriah.Waterland@Sun.COM 		}
205*9781SMoriah.Waterland@Sun.COM 		keystore = NULL;
206*9781SMoriah.Waterland@Sun.COM 	}
207*9781SMoriah.Waterland@Sun.COM 
208*9781SMoriah.Waterland@Sun.COM 	quit(ret);
209*9781SMoriah.Waterland@Sun.COM 	/*NOTREACHED*/
210*9781SMoriah.Waterland@Sun.COM }
211*9781SMoriah.Waterland@Sun.COM 
212*9781SMoriah.Waterland@Sun.COM void
quit(int retcode)213*9781SMoriah.Waterland@Sun.COM quit(int retcode)
214*9781SMoriah.Waterland@Sun.COM {
215*9781SMoriah.Waterland@Sun.COM 	PKG_ERR	*err;
216*9781SMoriah.Waterland@Sun.COM 
217*9781SMoriah.Waterland@Sun.COM 	err = pkgerr_new();
218*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGINT, SIG_IGN);
219*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGHUP, SIG_IGN);
220*9781SMoriah.Waterland@Sun.COM 	(void) ds_close(1);
221*9781SMoriah.Waterland@Sun.COM 	(void) pkghead(NULL);
222*9781SMoriah.Waterland@Sun.COM 	if (keystore != NULL) {
223*9781SMoriah.Waterland@Sun.COM 		(void) close_keystore(err, keystore, NULL);
224*9781SMoriah.Waterland@Sun.COM 		pkgerr_free(err);
225*9781SMoriah.Waterland@Sun.COM 	}
226*9781SMoriah.Waterland@Sun.COM 	exit(retcode);
227*9781SMoriah.Waterland@Sun.COM }
228*9781SMoriah.Waterland@Sun.COM 
229*9781SMoriah.Waterland@Sun.COM static void
trap(int signo)230*9781SMoriah.Waterland@Sun.COM trap(int signo)
231*9781SMoriah.Waterland@Sun.COM {
232*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGINT, SIG_IGN);
233*9781SMoriah.Waterland@Sun.COM 	(void) signal(SIGHUP, SIG_IGN);
234*9781SMoriah.Waterland@Sun.COM 
235*9781SMoriah.Waterland@Sun.COM 	if (signo == SIGINT) {
236*9781SMoriah.Waterland@Sun.COM 		progerr(gettext("aborted at user request.\n"));
237*9781SMoriah.Waterland@Sun.COM 		quit(3);
238*9781SMoriah.Waterland@Sun.COM 	}
239*9781SMoriah.Waterland@Sun.COM 	progerr(gettext("aborted by signal %d\n"), signo);
240*9781SMoriah.Waterland@Sun.COM 	quit(1);
241*9781SMoriah.Waterland@Sun.COM }
242*9781SMoriah.Waterland@Sun.COM 
243*9781SMoriah.Waterland@Sun.COM static void
usage(void)244*9781SMoriah.Waterland@Sun.COM usage(void)
245*9781SMoriah.Waterland@Sun.COM {
246*9781SMoriah.Waterland@Sun.COM 	(void) fprintf(stderr,
247*9781SMoriah.Waterland@Sun.COM 	    gettext("usage: %s [-ionsg] [-k keystore] " \
248*9781SMoriah.Waterland@Sun.COM 	    "[-a alias] [-P password] srcdev dstdev [pkg [pkg...]]\n"),
249*9781SMoriah.Waterland@Sun.COM 	    get_prog_name());
250*9781SMoriah.Waterland@Sun.COM }
251