xref: /titanic_50/usr/src/cmd/svr4pkg/pkgtrans/main.c (revision 382f00c9f2bd34208f10bb7d2be96bc6696eb8c4)
15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland  * CDDL HEADER START
35c51f124SMoriah Waterland  *
45c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland  *
85c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland  * See the License for the specific language governing permissions
115c51f124SMoriah Waterland  * and limitations under the License.
125c51f124SMoriah Waterland  *
135c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland  *
195c51f124SMoriah Waterland  * CDDL HEADER END
205c51f124SMoriah Waterland  */
215c51f124SMoriah Waterland 
225c51f124SMoriah Waterland /*
235c51f124SMoriah Waterland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
245c51f124SMoriah Waterland  * Use is subject to license terms.
255c51f124SMoriah Waterland  */
265c51f124SMoriah Waterland 
275c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
285c51f124SMoriah Waterland /* All Rights Reserved */
295c51f124SMoriah Waterland 
30*382f00c9SGarrett D'Amore /*
31*382f00c9SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
32*382f00c9SGarrett D'Amore  */
335c51f124SMoriah Waterland 
345c51f124SMoriah Waterland #include <locale.h>
355c51f124SMoriah Waterland #include <libintl.h>
365c51f124SMoriah Waterland #include <stdio.h>
375c51f124SMoriah Waterland #include <signal.h>
385c51f124SMoriah Waterland #include <stdlib.h>
395c51f124SMoriah Waterland #include <unistd.h>
405c51f124SMoriah Waterland #include <string.h>
415c51f124SMoriah Waterland #include <pkgtrans.h>
425c51f124SMoriah Waterland #include <pkglib.h>
435c51f124SMoriah Waterland #include <pkglocs.h>
445c51f124SMoriah Waterland #include <libadm.h>
455c51f124SMoriah Waterland #include <libinst.h>
46*382f00c9SGarrett D'Amore #include <messages.h>
475c51f124SMoriah Waterland 
485c51f124SMoriah Waterland static int	options;
495c51f124SMoriah Waterland static keystore_handle_t	keystore = NULL;
505c51f124SMoriah Waterland 
515c51f124SMoriah Waterland static void	usage(void);
525c51f124SMoriah Waterland static void	trap(int signo);
535c51f124SMoriah Waterland 
545c51f124SMoriah Waterland #define	KEYSTORE_OPEN	"Retrieving signing certificates from keystore <%s>"
555c51f124SMoriah Waterland 
565c51f124SMoriah Waterland int
main(int argc,char * argv[])575c51f124SMoriah Waterland main(int argc, char *argv[])
585c51f124SMoriah Waterland {
595c51f124SMoriah Waterland 	int	c;
605c51f124SMoriah Waterland 	void	(*func)();
615c51f124SMoriah Waterland 	extern char	*optarg;
625c51f124SMoriah Waterland 	extern int	optind;
635c51f124SMoriah Waterland 	char		*keystore_alias = NULL;
645c51f124SMoriah Waterland 	char		*keystore_file = NULL;
655c51f124SMoriah Waterland 	boolean_t	create_sig = B_FALSE;
665c51f124SMoriah Waterland 	char		*homedir = NULL;
675c51f124SMoriah Waterland 	PKG_ERR		*err;
68*382f00c9SGarrett D'Amore 	int		ret, len;
695c51f124SMoriah Waterland 
705c51f124SMoriah Waterland 	(void) setlocale(LC_ALL, "");
715c51f124SMoriah Waterland 
725c51f124SMoriah Waterland #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
735c51f124SMoriah Waterland #define	TEXT_DOMAIN "SYS_TEST"
745c51f124SMoriah Waterland #endif
755c51f124SMoriah Waterland 	(void) textdomain(TEXT_DOMAIN);
765c51f124SMoriah Waterland 
775c51f124SMoriah Waterland 	(void) set_prog_name(argv[0]);
785c51f124SMoriah Waterland 
795c51f124SMoriah Waterland 	while ((c = getopt(argc, argv, "ga:P:k:snio?")) != EOF) {
805c51f124SMoriah Waterland 		switch (c) {
815c51f124SMoriah Waterland 		case 'n':
825c51f124SMoriah Waterland 			options |= PT_RENAME;
835c51f124SMoriah Waterland 			break;
845c51f124SMoriah Waterland 
855c51f124SMoriah Waterland 		case 'i':
865c51f124SMoriah Waterland 			options |= PT_INFO_ONLY;
875c51f124SMoriah Waterland 			break;
885c51f124SMoriah Waterland 
895c51f124SMoriah Waterland 		case 'o':
905c51f124SMoriah Waterland 			options |= PT_OVERWRITE;
915c51f124SMoriah Waterland 			break;
925c51f124SMoriah Waterland 
935c51f124SMoriah Waterland 		case 's':
945c51f124SMoriah Waterland 			options |= PT_ODTSTREAM;
955c51f124SMoriah Waterland 			break;
965c51f124SMoriah Waterland 
975c51f124SMoriah Waterland 		case 'g':
985c51f124SMoriah Waterland 			/* this should eventually be a PT_ option */
995c51f124SMoriah Waterland 			create_sig = B_TRUE;
1005c51f124SMoriah Waterland 			break;
1015c51f124SMoriah Waterland 
1025c51f124SMoriah Waterland 		case 'k':
1035c51f124SMoriah Waterland 			keystore_file = optarg;
1045c51f124SMoriah Waterland 			break;
1055c51f124SMoriah Waterland 
1065c51f124SMoriah Waterland 		case 'a':
1075c51f124SMoriah Waterland 			keystore_alias = optarg;
1085c51f124SMoriah Waterland 			break;
1095c51f124SMoriah Waterland 
1105c51f124SMoriah Waterland 		case 'P':
1115c51f124SMoriah Waterland 			set_passphrase_passarg(optarg);
1125c51f124SMoriah Waterland 			if (ci_strneq(optarg, "pass:", 5)) {
1135c51f124SMoriah Waterland 				/*
1145c51f124SMoriah Waterland 				 * passwords on the command line are highly
1155c51f124SMoriah Waterland 				 * insecure.  complain.
1165c51f124SMoriah Waterland 				 */
117*382f00c9SGarrett D'Amore 				logerr(PASSWD_CMDLINE, "pass:<pass>");
1185c51f124SMoriah Waterland 			}
1195c51f124SMoriah Waterland 			break;
1205c51f124SMoriah Waterland 
1215c51f124SMoriah Waterland 		default:
1225c51f124SMoriah Waterland 			usage();
1235c51f124SMoriah Waterland 			return (1);
1245c51f124SMoriah Waterland 		}
1255c51f124SMoriah Waterland 	}
1265c51f124SMoriah Waterland 	func = signal(SIGINT, trap);
1275c51f124SMoriah Waterland 	if (func != SIG_DFL)
1285c51f124SMoriah Waterland 		(void) signal(SIGINT, func);
1295c51f124SMoriah Waterland 	(void) signal(SIGHUP, trap);
1305c51f124SMoriah Waterland 	(void) signal(SIGQUIT, trap);
1315c51f124SMoriah Waterland 	(void) signal(SIGTERM, trap);
1325c51f124SMoriah Waterland 	(void) signal(SIGPIPE, trap);
1335c51f124SMoriah Waterland 	(void) signal(SIGPWR, trap);
1345c51f124SMoriah Waterland 
1355c51f124SMoriah Waterland 	if ((argc-optind) < 2) {
1365c51f124SMoriah Waterland 		usage();
1375c51f124SMoriah Waterland 		return (1);
1385c51f124SMoriah Waterland 	}
1395c51f124SMoriah Waterland 
1405c51f124SMoriah Waterland 	if (create_sig) {
1415c51f124SMoriah Waterland 		sec_init();
1425c51f124SMoriah Waterland 		err = pkgerr_new();
1435c51f124SMoriah Waterland 
1445c51f124SMoriah Waterland 		/* figure out which keystore to use */
1455c51f124SMoriah Waterland 		if (keystore_file == NULL) {
1465c51f124SMoriah Waterland 			if (geteuid() == 0) {
1475c51f124SMoriah Waterland 				/* we are superuser, so use their keystore */
1485c51f124SMoriah Waterland 				keystore_file = PKGSEC;
149*382f00c9SGarrett D'Amore 
150*382f00c9SGarrett D'Amore 			} else if ((homedir = getenv("HOME")) == NULL) {
1515c51f124SMoriah Waterland 				/*
1525c51f124SMoriah Waterland 				 * not superuser, but no home dir, so
1535c51f124SMoriah Waterland 				 * use superuser's keystore
1545c51f124SMoriah Waterland 				 */
1555c51f124SMoriah Waterland 				keystore_file = PKGSEC;
156*382f00c9SGarrett D'Amore 
157*382f00c9SGarrett D'Amore 			} else if (asprintf(&keystore_file, "%s/.pkg/security",
158*382f00c9SGarrett D'Amore 			    homedir) < 0) {
159*382f00c9SGarrett D'Amore 				logerr(ERR_MEM);
1605c51f124SMoriah Waterland 				quit(1);
1615c51f124SMoriah Waterland 			}
1625c51f124SMoriah Waterland 		}
1635c51f124SMoriah Waterland 
1645c51f124SMoriah Waterland 		logerr(gettext(KEYSTORE_OPEN), keystore_file);
1655c51f124SMoriah Waterland 
166*382f00c9SGarrett D'Amore 		set_passphrase_prompt(MSG_PASSPROMPT);
1675c51f124SMoriah Waterland 
1685c51f124SMoriah Waterland 		/* open keystore for reading */
1695c51f124SMoriah Waterland 		if (open_keystore(err, keystore_file, get_prog_name(),
1705c51f124SMoriah Waterland 		    pkg_passphrase_cb, KEYSTORE_DFLT_FLAGS, &keystore) != 0) {
1715c51f124SMoriah Waterland 			pkgerr(err);
1725c51f124SMoriah Waterland 			pkgerr_free(err);
1735c51f124SMoriah Waterland 			quit(1);
1745c51f124SMoriah Waterland 		}
1755c51f124SMoriah Waterland 
1765c51f124SMoriah Waterland 	} else {
1775c51f124SMoriah Waterland 		/* no signature, so don't use a keystore */
1785c51f124SMoriah Waterland 		keystore = NULL;
1795c51f124SMoriah Waterland 	}
1805c51f124SMoriah Waterland 
1815c51f124SMoriah Waterland 	ret = pkgtrans(flex_device(argv[optind], 1),
1825c51f124SMoriah Waterland 	    flex_device(argv[optind+1], 1), &argv[optind+2], options,
1835c51f124SMoriah Waterland 	    keystore, keystore_alias);
1845c51f124SMoriah Waterland 
1855c51f124SMoriah Waterland 	if (create_sig) {
1865c51f124SMoriah Waterland 		/* close keystore */
1875c51f124SMoriah Waterland 		if (close_keystore(err, keystore, NULL) != 0) {
1885c51f124SMoriah Waterland 			pkgerr(err);
1895c51f124SMoriah Waterland 			pkgerr_free(err);
1905c51f124SMoriah Waterland 			quit(1);
1915c51f124SMoriah Waterland 		}
1925c51f124SMoriah Waterland 		keystore = NULL;
1935c51f124SMoriah Waterland 	}
1945c51f124SMoriah Waterland 
1955c51f124SMoriah Waterland 	quit(ret);
1965c51f124SMoriah Waterland 	/*NOTREACHED*/
1975c51f124SMoriah Waterland }
1985c51f124SMoriah Waterland 
1995c51f124SMoriah Waterland void
quit(int retcode)2005c51f124SMoriah Waterland quit(int retcode)
2015c51f124SMoriah Waterland {
2025c51f124SMoriah Waterland 	PKG_ERR	*err;
2035c51f124SMoriah Waterland 
2045c51f124SMoriah Waterland 	err = pkgerr_new();
2055c51f124SMoriah Waterland 	(void) signal(SIGINT, SIG_IGN);
2065c51f124SMoriah Waterland 	(void) signal(SIGHUP, SIG_IGN);
2075c51f124SMoriah Waterland 	(void) ds_close(1);
2085c51f124SMoriah Waterland 	(void) pkghead(NULL);
2095c51f124SMoriah Waterland 	if (keystore != NULL) {
2105c51f124SMoriah Waterland 		(void) close_keystore(err, keystore, NULL);
2115c51f124SMoriah Waterland 		pkgerr_free(err);
2125c51f124SMoriah Waterland 	}
2135c51f124SMoriah Waterland 	exit(retcode);
2145c51f124SMoriah Waterland }
2155c51f124SMoriah Waterland 
2165c51f124SMoriah Waterland static void
trap(int signo)2175c51f124SMoriah Waterland trap(int signo)
2185c51f124SMoriah Waterland {
2195c51f124SMoriah Waterland 	(void) signal(SIGINT, SIG_IGN);
2205c51f124SMoriah Waterland 	(void) signal(SIGHUP, SIG_IGN);
2215c51f124SMoriah Waterland 
2225c51f124SMoriah Waterland 	if (signo == SIGINT) {
2235c51f124SMoriah Waterland 		progerr(gettext("aborted at user request.\n"));
2245c51f124SMoriah Waterland 		quit(3);
2255c51f124SMoriah Waterland 	}
2265c51f124SMoriah Waterland 	progerr(gettext("aborted by signal %d\n"), signo);
2275c51f124SMoriah Waterland 	quit(1);
2285c51f124SMoriah Waterland }
2295c51f124SMoriah Waterland 
2305c51f124SMoriah Waterland static void
usage(void)2315c51f124SMoriah Waterland usage(void)
2325c51f124SMoriah Waterland {
2335c51f124SMoriah Waterland 	(void) fprintf(stderr,
2345c51f124SMoriah Waterland 	    gettext("usage: %s [-ionsg] [-k keystore] " \
2355c51f124SMoriah Waterland 	    "[-a alias] [-P password] srcdev dstdev [pkg [pkg...]]\n"),
2365c51f124SMoriah Waterland 	    get_prog_name());
2375c51f124SMoriah Waterland }
238