xref: /openbsd/sbin/mount_vnd/mount_vnd.c (revision df69c215)
1*df69c215Sderaadt /*	$OpenBSD: mount_vnd.c,v 1.22 2019/06/28 13:32:45 deraadt Exp $	*/
21067ebfdSgrunk /*
31067ebfdSgrunk  * Copyright (c) 1993 University of Utah.
41067ebfdSgrunk  * Copyright (c) 1990, 1993
51067ebfdSgrunk  *	The Regents of the University of California.  All rights reserved.
61067ebfdSgrunk  *
71067ebfdSgrunk  * This code is derived from software contributed to Berkeley by
81067ebfdSgrunk  * the Systems Programming Group of the University of Utah Computer
91067ebfdSgrunk  * Science Department.
101067ebfdSgrunk  *
111067ebfdSgrunk  * Redistribution and use in source and binary forms, with or without
121067ebfdSgrunk  * modification, are permitted provided that the following conditions
131067ebfdSgrunk  * are met:
141067ebfdSgrunk  * 1. Redistributions of source code must retain the above copyright
151067ebfdSgrunk  *    notice, this list of conditions and the following disclaimer.
161067ebfdSgrunk  * 2. Redistributions in binary form must reproduce the above copyright
171067ebfdSgrunk  *    notice, this list of conditions and the following disclaimer in the
181067ebfdSgrunk  *    documentation and/or other materials provided with the distribution.
191067ebfdSgrunk  * 3. Neither the name of the University nor the names of its contributors
201067ebfdSgrunk  *    may be used to endorse or promote products derived from this software
211067ebfdSgrunk  *    without specific prior written permission.
221067ebfdSgrunk  *
231067ebfdSgrunk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241067ebfdSgrunk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251067ebfdSgrunk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261067ebfdSgrunk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271067ebfdSgrunk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281067ebfdSgrunk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291067ebfdSgrunk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301067ebfdSgrunk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311067ebfdSgrunk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321067ebfdSgrunk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331067ebfdSgrunk  * SUCH DAMAGE.
341067ebfdSgrunk  */
351067ebfdSgrunk 
36b9fc9a72Sderaadt #include <sys/param.h>	/* DEV_BSIZE */
371067ebfdSgrunk #include <sys/ioctl.h>
381067ebfdSgrunk #include <sys/mount.h>
391067ebfdSgrunk #include <sys/stat.h>
4014fc046cSkrw #include <sys/disklabel.h>
411067ebfdSgrunk 
421067ebfdSgrunk #include <dev/vndioctl.h>
431067ebfdSgrunk 
441067ebfdSgrunk #include <blf.h>
451067ebfdSgrunk #include <err.h>
461067ebfdSgrunk #include <errno.h>
471067ebfdSgrunk #include <fcntl.h>
48daacae51Stedu #include <readpassphrase.h>
491067ebfdSgrunk #include <stdio.h>
501067ebfdSgrunk #include <stdlib.h>
511067ebfdSgrunk #include <string.h>
521067ebfdSgrunk #include <unistd.h>
53b9fc9a72Sderaadt #include <limits.h>
541067ebfdSgrunk #include <util.h>
551067ebfdSgrunk 
561067ebfdSgrunk __dead void	 usage(void);
577d9a3febSderaadt int		 config(char *, char *, struct disklabel *, char *, size_t);
581067ebfdSgrunk char		*get_pkcs_key(char *, char *);
591067ebfdSgrunk 
601067ebfdSgrunk int
main(int argc,char ** argv)611067ebfdSgrunk main(int argc, char **argv)
621067ebfdSgrunk {
637d9a3febSderaadt 	int	 ch, rv, opt_k = 0, opt_K = 0;
647d9a3febSderaadt 	char	*key = NULL, *mntopts = NULL, *rounds = NULL, *saltopt = NULL;
6514fc046cSkrw 	size_t	 keylen = 0;
6614fc046cSkrw 	struct disklabel *dp = NULL;
671067ebfdSgrunk 
687d9a3febSderaadt 	while ((ch = getopt(argc, argv, "kK:o:S:t:")) != -1) {
691067ebfdSgrunk 		switch (ch) {
701067ebfdSgrunk 		case 'k':
711067ebfdSgrunk 			opt_k = 1;
721067ebfdSgrunk 			break;
731067ebfdSgrunk 		case 'K':
741067ebfdSgrunk 			opt_K = 1;
751067ebfdSgrunk 			rounds = optarg;
761067ebfdSgrunk 			break;
771067ebfdSgrunk 		case 'o':
781067ebfdSgrunk 			mntopts = optarg;
791067ebfdSgrunk 			break;
801067ebfdSgrunk 		case 'S':
811067ebfdSgrunk 			saltopt = optarg;
821067ebfdSgrunk 			break;
8314fc046cSkrw 		case 't':
8414fc046cSkrw 			dp = getdiskbyname(optarg);
8514fc046cSkrw 			if (dp == NULL)
8614fc046cSkrw 				errx(1, "unknown disk type: %s", optarg);
87c902fadbSjsing 			break;
881067ebfdSgrunk 		default:
891067ebfdSgrunk 			usage();
901067ebfdSgrunk 			/* NOTREACHED */
911067ebfdSgrunk 		}
921067ebfdSgrunk 	}
931067ebfdSgrunk 	argc -= optind;
941067ebfdSgrunk 	argv += optind;
951067ebfdSgrunk 
967d9a3febSderaadt 	if (saltopt && !opt_K)
971067ebfdSgrunk 		errx(1, "-S only makes sense when used with -K");
981067ebfdSgrunk 
997d9a3febSderaadt 	if (argc != 2)
1007d9a3febSderaadt 		usage();
1011067ebfdSgrunk 
1027d9a3febSderaadt 	if (opt_k || opt_K)
1037d9a3febSderaadt 		fprintf(stderr, "WARNING: Consider using softraid crypto.\n");
1041067ebfdSgrunk 	if (opt_k) {
1051067ebfdSgrunk 		if (opt_K)
1061067ebfdSgrunk 			errx(1, "-k and -K are mutually exclusive");
1071067ebfdSgrunk 		key = getpass("Encryption key: ");
1081067ebfdSgrunk 		if (key == NULL || (keylen = strlen(key)) == 0)
1091067ebfdSgrunk 			errx(1, "Need an encryption key");
1101067ebfdSgrunk 	} else if (opt_K) {
1111067ebfdSgrunk 		key = get_pkcs_key(rounds, saltopt);
1121067ebfdSgrunk 		keylen = BLF_MAXUTILIZED;
1131067ebfdSgrunk 	}
1147d9a3febSderaadt 	rv = config(argv[1], argv[0], dp, key, keylen);
1151067ebfdSgrunk 
1161067ebfdSgrunk 	exit(rv);
1171067ebfdSgrunk }
1181067ebfdSgrunk 
1191067ebfdSgrunk char *
get_pkcs_key(char * arg,char * saltopt)1201067ebfdSgrunk get_pkcs_key(char *arg, char *saltopt)
1211067ebfdSgrunk {
122a22d5032Smmcc 	char		 passphrase[128] = {'\0'};
123a22d5032Smmcc 	char		 saltbuf[128] = {'\0'}, saltfilebuf[PATH_MAX];
1241067ebfdSgrunk 	char		*key = NULL;
125daacae51Stedu 	char		*saltfile;
1261067ebfdSgrunk 	const char	*errstr;
1271067ebfdSgrunk 	int		 rounds;
1281067ebfdSgrunk 
1291067ebfdSgrunk 	rounds = strtonum(arg, 1000, INT_MAX, &errstr);
1301067ebfdSgrunk 	if (errstr)
1311067ebfdSgrunk 		err(1, "rounds: %s", errstr);
132daacae51Stedu 	if (readpassphrase("Encryption key: ", passphrase, sizeof(passphrase),
133daacae51Stedu 	    RPP_REQUIRE_TTY) == NULL)
134daacae51Stedu 		errx(1, "Unable to read passphrase");
1351067ebfdSgrunk 	if (saltopt)
1361067ebfdSgrunk 		saltfile = saltopt;
1371067ebfdSgrunk 	else {
1381067ebfdSgrunk 		printf("Salt file: ");
1391067ebfdSgrunk 		fflush(stdout);
1401067ebfdSgrunk 		saltfile = fgets(saltfilebuf, sizeof(saltfilebuf), stdin);
141dbb07f5aSray 		if (saltfile)
142dbb07f5aSray 			saltfile[strcspn(saltfile, "\n")] = '\0';
1431067ebfdSgrunk 	}
144a22d5032Smmcc 	if (!saltfile || saltfile[0] == '\0')
1451067ebfdSgrunk 		warnx("Skipping salt file, insecure");
146a22d5032Smmcc 	else {
1471067ebfdSgrunk 		int fd;
1481067ebfdSgrunk 
1491067ebfdSgrunk 		fd = open(saltfile, O_RDONLY);
1501067ebfdSgrunk 		if (fd == -1) {
1511067ebfdSgrunk 			int *s;
1521067ebfdSgrunk 
153daacae51Stedu 			fprintf(stderr, "Salt file not found, attempting to "
154daacae51Stedu 			    "create one\n");
1551067ebfdSgrunk 			fd = open(saltfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1561067ebfdSgrunk 			if (fd == -1)
1571067ebfdSgrunk 				err(1, "Unable to create salt file: '%s'",
1581067ebfdSgrunk 				    saltfile);
1591067ebfdSgrunk 			for (s = (int *)saltbuf;
1601067ebfdSgrunk 			    s < (int *)(saltbuf + sizeof(saltbuf)); s++)
1611067ebfdSgrunk 				*s = arc4random();
1621067ebfdSgrunk 			if (write(fd, saltbuf, sizeof(saltbuf))
1631067ebfdSgrunk 			    != sizeof(saltbuf))
164daacae51Stedu 				err(1, "Unable to write salt file: '%s'",
165daacae51Stedu 				    saltfile);
166daacae51Stedu 			fprintf(stderr, "Salt file created as '%s'\n",
167daacae51Stedu 			    saltfile);
1681067ebfdSgrunk 		} else {
1691067ebfdSgrunk 			if (read(fd, saltbuf, sizeof(saltbuf))
1701067ebfdSgrunk 			    != sizeof(saltbuf))
171daacae51Stedu 				err(1, "Unable to read salt file: '%s'",
172daacae51Stedu 				    saltfile);
1731067ebfdSgrunk 		}
1741067ebfdSgrunk 		close(fd);
1751067ebfdSgrunk 	}
176daacae51Stedu 	if ((key = calloc(1, BLF_MAXUTILIZED)) == NULL)
177daacae51Stedu 		err(1, NULL);
178daacae51Stedu 	if (pkcs5_pbkdf2(passphrase, sizeof(passphrase), saltbuf,
179daacae51Stedu 	    sizeof (saltbuf), key, BLF_MAXUTILIZED, rounds))
1801067ebfdSgrunk 		errx(1, "pkcs5_pbkdf2 failed");
181487fbab9Smmcc 	explicit_bzero(passphrase, sizeof(passphrase));
1821067ebfdSgrunk 
1831067ebfdSgrunk 	return (key);
1841067ebfdSgrunk }
1851067ebfdSgrunk 
1861067ebfdSgrunk int
config(char * dev,char * file,struct disklabel * dp,char * key,size_t keylen)1877d9a3febSderaadt config(char *dev, char *file, struct disklabel *dp, char *key, size_t keylen)
1881067ebfdSgrunk {
1891067ebfdSgrunk 	struct vnd_ioctl vndio;
1901067ebfdSgrunk 	char *rdev;
191ff631dd3Stedu 	int fd, rv = -1;
1921067ebfdSgrunk 
193*df69c215Sderaadt 	if ((fd = opendev(dev, O_RDONLY, OPENDEV_PART, &rdev)) == -1) {
1941067ebfdSgrunk 		err(4, "%s", rdev);
1951067ebfdSgrunk 		goto out;
1961067ebfdSgrunk 	}
197ff631dd3Stedu 
1981067ebfdSgrunk 	vndio.vnd_file = file;
19914fc046cSkrw 	vndio.vnd_secsize = (dp && dp->d_secsize) ? dp->d_secsize : DEV_BSIZE;
20014fc046cSkrw 	vndio.vnd_nsectors = (dp && dp->d_nsectors) ? dp->d_nsectors : 100;
20114fc046cSkrw 	vndio.vnd_ntracks = (dp && dp->d_ntracks) ? dp->d_ntracks : 1;
2021067ebfdSgrunk 	vndio.vnd_key = (u_char *)key;
2031067ebfdSgrunk 	vndio.vnd_keylen = keylen;
2041067ebfdSgrunk 
2051067ebfdSgrunk 	/*
2061067ebfdSgrunk 	 * Configure the device
2071067ebfdSgrunk 	 */
208ff631dd3Stedu 	rv = ioctl(fd, VNDIOCSET, &vndio);
2091067ebfdSgrunk 	if (rv)
2101067ebfdSgrunk 		warn("VNDIOCSET");
2111067ebfdSgrunk 
212ff631dd3Stedu 	close(fd);
2131067ebfdSgrunk 	fflush(stdout);
2141067ebfdSgrunk  out:
2151067ebfdSgrunk 	if (key)
216487fbab9Smmcc 		explicit_bzero(key, keylen);
2171067ebfdSgrunk 	return (rv < 0);
2181067ebfdSgrunk }
2191067ebfdSgrunk 
2201067ebfdSgrunk __dead void
usage(void)2211067ebfdSgrunk usage(void)
2221067ebfdSgrunk {
2231067ebfdSgrunk 	(void)fprintf(stderr,
224a02dafe8Sjsing 	    "usage: mount_vnd [-k] [-K rounds] [-o options] "
22514fc046cSkrw 	    "[-S saltfile] [-t disktype]\n"
226a02dafe8Sjsing 	    "\t\t image vnd_dev\n");
2271067ebfdSgrunk 	exit(1);
2281067ebfdSgrunk }
229