Home | History | Annotate | Line # | Download | only in cgdconfig
cgdconfig.c revision 1.21
      1  1.21    martin /* $NetBSD: cgdconfig.c,v 1.21 2008/04/28 20:23:08 martin Exp $ */
      2   1.1     elric 
      3   1.1     elric /*-
      4   1.5     elric  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5   1.1     elric  * All rights reserved.
      6   1.1     elric  *
      7   1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     elric  * by Roland C. Dowdeswell.
      9   1.1     elric  *
     10   1.1     elric  * Redistribution and use in source and binary forms, with or without
     11   1.1     elric  * modification, are permitted provided that the following conditions
     12   1.1     elric  * are met:
     13   1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14   1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15   1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     elric  *    documentation and/or other materials provided with the distribution.
     18   1.1     elric  *
     19   1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     elric  */
     31   1.1     elric 
     32   1.1     elric #include <sys/cdefs.h>
     33   1.1     elric #ifndef lint
     34   1.1     elric __COPYRIGHT(
     35   1.5     elric "@(#) Copyright (c) 2002, 2003\
     36   1.1     elric 	The NetBSD Foundation, Inc.  All rights reserved.");
     37  1.21    martin __RCSID("$NetBSD: cgdconfig.c,v 1.21 2008/04/28 20:23:08 martin Exp $");
     38   1.1     elric #endif
     39   1.1     elric 
     40   1.5     elric #include <err.h>
     41   1.1     elric #include <errno.h>
     42   1.1     elric #include <fcntl.h>
     43   1.1     elric #include <libgen.h>
     44   1.1     elric #include <stdio.h>
     45   1.1     elric #include <stdlib.h>
     46   1.1     elric #include <string.h>
     47   1.1     elric #include <unistd.h>
     48   1.1     elric #include <util.h>
     49   1.1     elric 
     50   1.1     elric #include <sys/ioctl.h>
     51   1.3     elric #include <sys/disklabel.h>
     52  1.14     elric #include <sys/mman.h>
     53   1.1     elric #include <sys/param.h>
     54  1.13     elric #include <sys/resource.h>
     55   1.1     elric 
     56   1.1     elric #include <dev/cgdvar.h>
     57   1.1     elric 
     58   1.5     elric #include <ufs/ffs/fs.h>
     59   1.5     elric 
     60   1.1     elric #include "params.h"
     61   1.1     elric #include "pkcs5_pbkdf2.h"
     62   1.1     elric #include "utils.h"
     63   1.1     elric 
     64   1.1     elric #define CGDCONFIG_DIR		"/etc/cgd"
     65   1.1     elric #define CGDCONFIG_CFILE		CGDCONFIG_DIR "/cgd.conf"
     66   1.1     elric 
     67  1.17    cbiere enum action {
     68  1.17    cbiere 	 ACTION_DEFAULT,		/* default -> configure */
     69  1.17    cbiere 	 ACTION_CONFIGURE,		/* configure, with paramsfile */
     70  1.17    cbiere 	 ACTION_UNCONFIGURE,		/* unconfigure */
     71  1.17    cbiere 	 ACTION_GENERATE,		/* generate a paramsfile */
     72  1.17    cbiere 	 ACTION_GENERATE_CONVERT,	/* generate a ``dup'' paramsfile */
     73  1.17    cbiere 	 ACTION_CONFIGALL,		/* configure all from config file */
     74  1.17    cbiere 	 ACTION_UNCONFIGALL,		/* unconfigure all from config file */
     75  1.17    cbiere 	 ACTION_CONFIGSTDIN		/* configure, key from stdin */
     76  1.17    cbiere };
     77   1.1     elric 
     78   1.1     elric /* if nflag is set, do not configure/unconfigure the cgd's */
     79   1.1     elric 
     80   1.1     elric int	nflag = 0;
     81   1.1     elric 
     82   1.3     elric static int	configure(int, char **, struct params *, int);
     83   1.1     elric static int	configure_stdin(struct params *, int argc, char **);
     84   1.1     elric static int	generate(struct params *, int, char **, const char *);
     85   1.5     elric static int	generate_convert(struct params *, int, char **, const char *);
     86   1.3     elric static int	unconfigure(int, char **, struct params *, int);
     87   1.3     elric static int	do_all(const char *, int, char **,
     88   1.3     elric 		       int (*)(int, char **, struct params *, int));
     89   1.1     elric 
     90   1.1     elric #define CONFIG_FLAGS_FROMALL	1	/* called from configure_all() */
     91   1.1     elric #define CONFIG_FLAGS_FROMMAIN	2	/* called from main() */
     92   1.1     elric 
     93   1.2     elric static int	 configure_params(int, const char *, const char *,
     94   1.2     elric 				  struct params *);
     95  1.13     elric static void	 eliminate_cores(void);
     96  1.19  christos static bits_t	*getkey(const char *, struct keygen *, size_t);
     97  1.19  christos static bits_t	*getkey_storedkey(const char *, struct keygen *, size_t);
     98  1.19  christos static bits_t	*getkey_randomkey(const char *, struct keygen *, size_t, int);
     99  1.19  christos static bits_t	*getkey_pkcs5_pbkdf2(const char *, struct keygen *, size_t, int);
    100  1.17    cbiere static int	 opendisk_werror(const char *, char *, size_t);
    101   1.3     elric static int	 unconfigure_fd(int);
    102   1.3     elric static int	 verify(struct params *, int);
    103   1.5     elric static int	 verify_disklabel(int);
    104   1.5     elric static int	 verify_ffs(int);
    105   1.9        cb static int	 verify_reenter(struct params *);
    106   1.1     elric 
    107   1.3     elric static void	 usage(void);
    108   1.1     elric 
    109   1.1     elric /* Verbose Framework */
    110  1.17    cbiere unsigned	verbose = 0;
    111   1.1     elric 
    112   1.1     elric #define VERBOSE(x,y)	if (verbose >= x) y
    113  1.19  christos #define VPRINTF(x,y)	if (verbose >= x) (void)printf y
    114   1.1     elric 
    115   1.1     elric static void
    116   1.1     elric usage(void)
    117   1.1     elric {
    118   1.1     elric 
    119  1.19  christos 	(void)fprintf(stderr, "usage: %s [-nv] [-V vmeth] cgd dev [paramsfile]\n",
    120   1.1     elric 	    getprogname());
    121  1.19  christos 	(void)fprintf(stderr, "       %s -C [-nv] [-f configfile]\n", getprogname());
    122  1.19  christos 	(void)fprintf(stderr, "       %s -U [-nv] [-f configfile]\n", getprogname());
    123  1.19  christos 	(void)fprintf(stderr, "       %s -G [-nv] [-i ivmeth] [-k kgmeth] "
    124   1.5     elric 	    "[-o outfile] paramsfile\n", getprogname());
    125  1.19  christos 	(void)fprintf(stderr, "       %s -g [-nv] [-i ivmeth] [-k kgmeth] "
    126   1.5     elric 	    "[-o outfile] alg [keylen]\n", getprogname());
    127  1.19  christos 	(void)fprintf(stderr, "       %s -s [-nv] [-i ivmeth] cgd dev alg "
    128   1.1     elric 	    "[keylen]\n", getprogname());
    129  1.19  christos 	(void)fprintf(stderr, "       %s -u [-nv] cgd\n", getprogname());
    130  1.17    cbiere 	exit(EXIT_FAILURE);
    131  1.17    cbiere }
    132  1.17    cbiere 
    133  1.17    cbiere static int
    134  1.19  christos parse_size_t(const char *s, size_t *l)
    135  1.17    cbiere {
    136  1.17    cbiere 	char *endptr;
    137  1.17    cbiere 	long v;
    138  1.17    cbiere 
    139  1.17    cbiere 	errno = 0;
    140  1.17    cbiere 	v = strtol(s, &endptr, 10);
    141  1.17    cbiere 	if ((v == LONG_MIN || v == LONG_MAX) && errno)
    142  1.17    cbiere 		return -1;
    143  1.17    cbiere 	if (v < INT_MIN || v > INT_MAX) {
    144  1.17    cbiere 		errno = ERANGE;
    145  1.17    cbiere 		return -1;
    146  1.17    cbiere 	}
    147  1.17    cbiere 	if (endptr == s) {
    148  1.17    cbiere 		errno = EINVAL;
    149  1.17    cbiere 		return -1;
    150  1.17    cbiere 	}
    151  1.19  christos 	*l = (size_t)v;
    152  1.19  christos 	return 0;
    153  1.17    cbiere }
    154  1.17    cbiere 
    155  1.17    cbiere static void
    156  1.17    cbiere set_action(enum action *action, enum action value)
    157  1.17    cbiere {
    158  1.17    cbiere 	if (*action != ACTION_DEFAULT)
    159  1.17    cbiere 		usage();
    160  1.17    cbiere 	*action = value;
    161   1.1     elric }
    162   1.1     elric 
    163   1.1     elric int
    164   1.1     elric main(int argc, char **argv)
    165   1.1     elric {
    166   1.5     elric 	struct params *p;
    167   1.5     elric 	struct params *tp;
    168   1.5     elric 	struct keygen *kg;
    169  1.17    cbiere 	enum action action = ACTION_DEFAULT;
    170   1.1     elric 	int	ch;
    171  1.17    cbiere 	const char	*cfile = NULL;
    172  1.17    cbiere 	const char	*outfile = NULL;
    173   1.1     elric 
    174  1.15     elric 	setprogname(*argv);
    175  1.13     elric 	eliminate_cores();
    176  1.14     elric 	if (mlockall(MCL_FUTURE))
    177  1.14     elric 		err(EXIT_FAILURE, "Can't lock memory");
    178   1.5     elric 	p = params_new();
    179   1.5     elric 	kg = NULL;
    180   1.1     elric 
    181   1.5     elric 	while ((ch = getopt(argc, argv, "CGUV:b:f:gi:k:no:usv")) != -1)
    182   1.1     elric 		switch (ch) {
    183   1.1     elric 		case 'C':
    184  1.17    cbiere 			set_action(&action, ACTION_CONFIGALL);
    185   1.1     elric 			break;
    186   1.5     elric 		case 'G':
    187  1.17    cbiere 			set_action(&action, ACTION_GENERATE_CONVERT);
    188   1.5     elric 			break;
    189   1.1     elric 		case 'U':
    190  1.17    cbiere 			set_action(&action, ACTION_UNCONFIGALL);
    191   1.1     elric 			break;
    192   1.3     elric 		case 'V':
    193   1.5     elric 			tp = params_verify_method(string_fromcharstar(optarg));
    194   1.5     elric 			if (!tp)
    195   1.3     elric 				usage();
    196   1.5     elric 			p = params_combine(p, tp);
    197   1.3     elric 			break;
    198   1.1     elric 		case 'b':
    199  1.17    cbiere 			{
    200  1.19  christos 				size_t size;
    201  1.17    cbiere 
    202  1.19  christos 				if (parse_size_t(optarg, &size) == -1)
    203  1.17    cbiere 					usage();
    204  1.17    cbiere 				tp = params_bsize(size);
    205  1.17    cbiere 				if (!tp)
    206  1.17    cbiere 					usage();
    207  1.17    cbiere 				p = params_combine(p, tp);
    208  1.17    cbiere 			}
    209   1.1     elric 			break;
    210   1.1     elric 		case 'f':
    211  1.17    cbiere 			if (cfile)
    212  1.17    cbiere 				usage();
    213  1.17    cbiere 			cfile = estrdup(optarg);
    214   1.1     elric 			break;
    215   1.1     elric 		case 'g':
    216  1.17    cbiere 			set_action(&action, ACTION_GENERATE);
    217   1.1     elric 			break;
    218   1.1     elric 		case 'i':
    219   1.5     elric 			tp = params_ivmeth(string_fromcharstar(optarg));
    220   1.5     elric 			p = params_combine(p, tp);
    221   1.1     elric 			break;
    222   1.1     elric 		case 'k':
    223   1.5     elric 			kg = keygen_method(string_fromcharstar(optarg));
    224   1.5     elric 			if (!kg)
    225   1.1     elric 				usage();
    226   1.5     elric 			keygen_addlist(&p->keygen, kg);
    227   1.1     elric 			break;
    228   1.1     elric 		case 'n':
    229   1.1     elric 			nflag = 1;
    230   1.1     elric 			break;
    231   1.1     elric 		case 'o':
    232  1.17    cbiere 			if (outfile)
    233  1.17    cbiere 				usage();
    234  1.17    cbiere 			outfile = estrdup(optarg);
    235   1.1     elric 			break;
    236   1.1     elric 		case 's':
    237  1.17    cbiere 			set_action(&action, ACTION_CONFIGSTDIN);
    238   1.1     elric 			break;
    239   1.1     elric 
    240   1.1     elric 		case 'u':
    241  1.17    cbiere 			set_action(&action, ACTION_UNCONFIGURE);
    242   1.1     elric 			break;
    243   1.1     elric 		case 'v':
    244   1.1     elric 			verbose++;
    245   1.1     elric 			break;
    246   1.1     elric 		default:
    247   1.1     elric 			usage();
    248   1.1     elric 			/* NOTREACHED */
    249   1.1     elric 		}
    250   1.1     elric 
    251   1.1     elric 	argc -= optind;
    252   1.1     elric 	argv += optind;
    253   1.1     elric 
    254  1.17    cbiere 	if (!outfile)
    255  1.17    cbiere 		outfile = "";
    256  1.17    cbiere 	if (!cfile)
    257  1.17    cbiere 		cfile = "";
    258  1.17    cbiere 
    259   1.1     elric 	/* validate the consistency of the arguments */
    260   1.1     elric 
    261   1.1     elric 	switch (action) {
    262  1.17    cbiere 	case ACTION_DEFAULT:	/* ACTION_CONFIGURE is the default */
    263   1.1     elric 	case ACTION_CONFIGURE:
    264   1.5     elric 		return configure(argc, argv, p, CONFIG_FLAGS_FROMMAIN);
    265   1.1     elric 	case ACTION_UNCONFIGURE:
    266   1.3     elric 		return unconfigure(argc, argv, NULL, CONFIG_FLAGS_FROMMAIN);
    267   1.1     elric 	case ACTION_GENERATE:
    268   1.5     elric 		return generate(p, argc, argv, outfile);
    269   1.5     elric 	case ACTION_GENERATE_CONVERT:
    270   1.5     elric 		return generate_convert(p, argc, argv, outfile);
    271   1.1     elric 	case ACTION_CONFIGALL:
    272   1.1     elric 		return do_all(cfile, argc, argv, configure);
    273   1.1     elric 	case ACTION_UNCONFIGALL:
    274   1.1     elric 		return do_all(cfile, argc, argv, unconfigure);
    275   1.1     elric 	case ACTION_CONFIGSTDIN:
    276   1.5     elric 		return configure_stdin(p, argc, argv);
    277  1.19  christos 	default:
    278  1.19  christos 		errx(EXIT_FAILURE, "undefined action");
    279  1.19  christos 		/* NOTREACHED */
    280   1.1     elric 	}
    281   1.1     elric }
    282   1.1     elric 
    283   1.5     elric static bits_t *
    284  1.19  christos getkey(const char *dev, struct keygen *kg, size_t len)
    285   1.1     elric {
    286   1.5     elric 	bits_t	*ret = NULL;
    287   1.5     elric 	bits_t	*tmp;
    288   1.1     elric 
    289  1.19  christos 	VPRINTF(3, ("getkey(\"%s\", %p, %zu) called\n", dev, kg, len));
    290   1.5     elric 	for (; kg; kg=kg->next) {
    291   1.5     elric 		switch (kg->kg_method) {
    292   1.5     elric 		case KEYGEN_STOREDKEY:
    293   1.5     elric 			tmp = getkey_storedkey(dev, kg, len);
    294   1.5     elric 			break;
    295   1.5     elric 		case KEYGEN_RANDOMKEY:
    296  1.12        tv 			tmp = getkey_randomkey(dev, kg, len, 1);
    297  1.12        tv 			break;
    298  1.12        tv 		case KEYGEN_URANDOMKEY:
    299  1.12        tv 			tmp = getkey_randomkey(dev, kg, len, 0);
    300   1.5     elric 			break;
    301  1.10       dan 		case KEYGEN_PKCS5_PBKDF2_SHA1:
    302  1.10       dan 			tmp = getkey_pkcs5_pbkdf2(dev, kg, len, 0);
    303  1.10       dan 			break;
    304  1.10       dan 		/* provide backwards compatibility for old config files */
    305  1.10       dan 		case KEYGEN_PKCS5_PBKDF2_OLD:
    306  1.10       dan 			tmp = getkey_pkcs5_pbkdf2(dev, kg, len, 1);
    307   1.5     elric 			break;
    308   1.5     elric 		default:
    309   1.5     elric 			warnx("unrecognised keygen method %d in getkey()",
    310   1.5     elric 			    kg->kg_method);
    311   1.5     elric 			if (ret)
    312   1.5     elric 				bits_free(ret);
    313   1.5     elric 			return NULL;
    314   1.5     elric 		}
    315   1.5     elric 
    316   1.5     elric 		if (ret)
    317   1.5     elric 			ret = bits_xor_d(tmp, ret);
    318   1.5     elric 		else
    319   1.5     elric 			ret = tmp;
    320   1.1     elric 	}
    321   1.5     elric 
    322   1.5     elric 	return ret;
    323   1.5     elric }
    324   1.5     elric 
    325   1.5     elric /*ARGSUSED*/
    326   1.5     elric static bits_t *
    327  1.19  christos getkey_storedkey(const char *target, struct keygen *kg, size_t keylen)
    328   1.5     elric {
    329   1.5     elric 	return bits_dup(kg->kg_key);
    330   1.1     elric }
    331   1.1     elric 
    332   1.5     elric /*ARGSUSED*/
    333   1.5     elric static bits_t *
    334  1.19  christos getkey_randomkey(const char *target, struct keygen *kg, size_t keylen, int hard)
    335   1.1     elric {
    336  1.12        tv 	return bits_getrandombits(keylen, hard);
    337   1.1     elric }
    338   1.1     elric 
    339   1.5     elric /*ARGSUSED*/
    340  1.10       dan /*
    341  1.10       dan  * XXX take, and pass through, a compat flag that indicates whether we
    342  1.10       dan  * provide backwards compatibility with a previous bug.  The previous
    343  1.10       dan  * behaviour is indicated by the keygen method pkcs5_pbkdf2, and a
    344  1.10       dan  * non-zero compat flag. The new default, and correct keygen method is
    345  1.10       dan  * called pcks5_pbkdf2/sha1.  When the old method is removed, so will
    346  1.10       dan  * be the compat argument.
    347  1.10       dan  */
    348   1.5     elric static bits_t *
    349  1.19  christos getkey_pkcs5_pbkdf2(const char *target, struct keygen *kg, size_t keylen,
    350  1.19  christos     int compat)
    351   1.1     elric {
    352   1.5     elric 	bits_t		*ret;
    353  1.19  christos 	const u_int8_t	*passp;
    354   1.5     elric 	char		 buf[1024];
    355   1.5     elric 	u_int8_t	*tmp;
    356   1.1     elric 
    357   1.5     elric 	snprintf(buf, sizeof(buf), "%s's passphrase:", target);
    358  1.19  christos 	passp = (const u_int8_t *)(void *)getpass(buf);
    359   1.5     elric 	if (pkcs5_pbkdf2(&tmp, BITS2BYTES(keylen), passp, strlen(passp),
    360   1.5     elric 	    bits_getbuf(kg->kg_salt), BITS2BYTES(bits_len(kg->kg_salt)),
    361  1.10       dan 	    kg->kg_iterations, compat)) {
    362   1.5     elric 		warnx("failed to generate PKCS#5 PBKDF2 key");
    363   1.5     elric 		return NULL;
    364   1.5     elric 	}
    365   1.5     elric 
    366   1.5     elric 	ret = bits_new(tmp, keylen);
    367   1.9        cb 	kg->kg_key = bits_dup(ret);
    368   1.5     elric 	free(tmp);
    369   1.1     elric 	return ret;
    370   1.1     elric }
    371   1.1     elric 
    372   1.5     elric /*ARGSUSED*/
    373   1.1     elric static int
    374   1.3     elric unconfigure(int argc, char **argv, struct params *inparams, int flags)
    375   1.1     elric {
    376   1.1     elric 	int	fd;
    377   1.1     elric 	int	ret;
    378   1.1     elric 	char	buf[MAXPATHLEN] = "";
    379   1.1     elric 
    380   1.1     elric 	/* only complain about additional arguments, if called from main() */
    381   1.1     elric 	if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
    382   1.1     elric 		usage();
    383   1.1     elric 
    384   1.1     elric 	/* if called from do_all(), then ensure that 2 or 3 args exist */
    385   1.1     elric 	if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
    386   1.1     elric 		return -1;
    387   1.1     elric 
    388   1.1     elric 	fd = opendisk(*argv, O_RDWR, buf, sizeof(buf), 1);
    389   1.1     elric 	if (fd == -1) {
    390  1.17    cbiere 		int saved_errno = errno;
    391  1.17    cbiere 
    392   1.5     elric 		warn("can't open cgd \"%s\", \"%s\"", *argv, buf);
    393   1.1     elric 
    394   1.1     elric 		/* this isn't fatal with nflag != 0 */
    395   1.1     elric 		if (!nflag)
    396  1.17    cbiere 			return saved_errno;
    397   1.1     elric 	}
    398   1.1     elric 
    399   1.1     elric 	VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
    400   1.1     elric 
    401   1.1     elric 	if (nflag)
    402   1.1     elric 		return 0;
    403   1.1     elric 
    404   1.3     elric 	ret = unconfigure_fd(fd);
    405  1.19  christos 	(void)close(fd);
    406   1.3     elric 	return ret;
    407   1.3     elric }
    408   1.3     elric 
    409   1.3     elric static int
    410   1.3     elric unconfigure_fd(int fd)
    411   1.3     elric {
    412   1.3     elric 	struct	cgd_ioctl ci;
    413   1.3     elric 
    414  1.17    cbiere 	if (ioctl(fd, CGDIOCCLR, &ci) == -1) {
    415  1.17    cbiere 		warn("ioctl");
    416   1.3     elric 		return -1;
    417   1.1     elric 	}
    418   1.1     elric 
    419   1.1     elric 	return 0;
    420   1.1     elric }
    421   1.1     elric 
    422   1.5     elric /*ARGSUSED*/
    423   1.1     elric static int
    424   1.3     elric configure(int argc, char **argv, struct params *inparams, int flags)
    425   1.1     elric {
    426   1.5     elric 	struct params	*p;
    427   1.5     elric 	int		 fd;
    428   1.5     elric 	int		 ret;
    429   1.5     elric 	char		 cgdname[PATH_MAX];
    430   1.1     elric 
    431  1.17    cbiere 	if (argc == 2) {
    432  1.17    cbiere 		char *pfile;
    433  1.17    cbiere 
    434  1.17    cbiere 		if (asprintf(&pfile, "%s/%s",
    435  1.17    cbiere 		    CGDCONFIG_DIR, basename(argv[1])) == -1)
    436  1.17    cbiere 			return -1;
    437  1.17    cbiere 
    438  1.17    cbiere 		p = params_cget(pfile);
    439  1.17    cbiere 		free(pfile);
    440  1.17    cbiere 	} else if (argc == 3) {
    441  1.17    cbiere 		p = params_cget(argv[2]);
    442  1.17    cbiere 	} else {
    443   1.1     elric 		/* print usage and exit, only if called from main() */
    444   1.5     elric 		if (flags == CONFIG_FLAGS_FROMMAIN) {
    445   1.5     elric 			warnx("wrong number of args");
    446   1.1     elric 			usage();
    447   1.5     elric 		}
    448   1.1     elric 		return -1;
    449   1.1     elric 	}
    450   1.1     elric 
    451   1.5     elric 	if (!p)
    452   1.5     elric 		return -1;
    453   1.2     elric 
    454   1.3     elric 	/*
    455   1.5     elric 	 * over-ride with command line specifications and fill in default
    456   1.5     elric 	 * values.
    457   1.3     elric 	 */
    458   1.3     elric 
    459   1.5     elric 	p = params_combine(p, inparams);
    460   1.5     elric 	ret = params_filldefaults(p);
    461   1.5     elric 	if (ret) {
    462   1.5     elric 		params_free(p);
    463   1.5     elric 		return ret;
    464   1.5     elric 	}
    465   1.5     elric 
    466   1.5     elric 	if (!params_verify(p)) {
    467   1.5     elric 		warnx("params invalid");
    468   1.5     elric 		return -1;
    469   1.5     elric 	}
    470   1.3     elric 
    471   1.3     elric 	/*
    472   1.3     elric 	 * loop over configuring the disk and checking to see if it
    473   1.3     elric 	 * verifies properly.  We open and close the disk device each
    474   1.3     elric 	 * time, because if the user passes us the block device we
    475   1.3     elric 	 * need to flush the buffer cache.
    476   1.3     elric 	 */
    477   1.3     elric 
    478   1.3     elric 	for (;;) {
    479   1.3     elric 		fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    480   1.3     elric 		if (fd == -1)
    481   1.3     elric 			return -1;
    482   1.3     elric 
    483   1.5     elric 		if (p->key)
    484   1.5     elric 			bits_free(p->key);
    485   1.5     elric 
    486   1.5     elric 		p->key = getkey(argv[1], p->keygen, p->keylen);
    487   1.5     elric 		if (!p->key)
    488   1.3     elric 			goto bail_err;
    489   1.3     elric 
    490   1.5     elric 		ret = configure_params(fd, cgdname, argv[1], p);
    491   1.3     elric 		if (ret)
    492   1.3     elric 			goto bail_err;
    493   1.3     elric 
    494   1.5     elric 		ret = verify(p, fd);
    495   1.3     elric 		if (ret == -1)
    496   1.3     elric 			goto bail_err;
    497   1.3     elric 		if (!ret)
    498   1.3     elric 			break;
    499   1.2     elric 
    500  1.17    cbiere 		warnx("verification failed, please reenter passphrase");
    501   1.1     elric 
    502  1.19  christos 		(void)unconfigure_fd(fd);
    503  1.19  christos 		(void)close(fd);
    504   1.3     elric 	}
    505   1.2     elric 
    506   1.5     elric 	params_free(p);
    507  1.19  christos 	(void)close(fd);
    508   1.3     elric 	return 0;
    509   1.3     elric bail_err:
    510   1.5     elric 	params_free(p);
    511  1.19  christos 	(void)close(fd);
    512   1.3     elric 	return -1;
    513   1.1     elric }
    514   1.1     elric 
    515   1.1     elric static int
    516   1.1     elric configure_stdin(struct params *p, int argc, char **argv)
    517   1.1     elric {
    518   1.2     elric 	int	fd;
    519   1.1     elric 	int	ret;
    520   1.2     elric 	char	cgdname[PATH_MAX];
    521   1.1     elric 
    522   1.1     elric 	if (argc < 3 || argc > 4)
    523   1.1     elric 		usage();
    524   1.1     elric 
    525   1.5     elric 	p->algorithm = string_fromcharstar(argv[2]);
    526  1.17    cbiere 	if (argc > 3) {
    527  1.19  christos 		size_t keylen;
    528  1.17    cbiere 
    529  1.19  christos 		if (parse_size_t(argv[3], &keylen) == -1) {
    530  1.17    cbiere 			warn("failed to parse key length");
    531  1.17    cbiere 			return -1;
    532  1.17    cbiere 		}
    533  1.17    cbiere 		p->keylen = keylen;
    534  1.17    cbiere 	}
    535   1.1     elric 
    536   1.1     elric 	ret = params_filldefaults(p);
    537   1.1     elric 	if (ret)
    538   1.1     elric 		return ret;
    539   1.1     elric 
    540   1.2     elric 	fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    541   1.2     elric 	if (fd == -1)
    542   1.2     elric 		return -1;
    543   1.2     elric 
    544   1.5     elric 	p->key = bits_fget(stdin, p->keylen);
    545   1.5     elric 	if (!p->key) {
    546   1.5     elric 		warnx("failed to read key from stdin");
    547   1.1     elric 		return -1;
    548   1.5     elric 	}
    549   1.1     elric 
    550   1.2     elric 	return configure_params(fd, cgdname, argv[1], p);
    551   1.1     elric }
    552   1.1     elric 
    553   1.1     elric static int
    554  1.17    cbiere opendisk_werror(const char *cgd, char *buf, size_t buflen)
    555   1.2     elric {
    556   1.2     elric 	int	fd;
    557   1.2     elric 
    558  1.18    cbiere 	VPRINTF(3, ("opendisk_werror(%s, %s, %zu) called.\n", cgd, buf, buflen));
    559   1.5     elric 
    560   1.2     elric 	/* sanity */
    561   1.2     elric 	if (!cgd || !buf)
    562   1.2     elric 		return -1;
    563   1.2     elric 
    564   1.2     elric 	if (nflag) {
    565  1.17    cbiere 		if (strlcpy(buf, cgd, buflen) >= buflen)
    566  1.17    cbiere 			return -1;
    567   1.2     elric 		return 0;
    568   1.2     elric 	}
    569   1.2     elric 
    570   1.3     elric 	fd = opendisk(cgd, O_RDWR, buf, buflen, 0);
    571   1.2     elric 	if (fd == -1)
    572   1.5     elric 		warnx("can't open cgd \"%s\", \"%s\"", cgd, buf);
    573   1.5     elric 
    574   1.2     elric 	return fd;
    575   1.2     elric }
    576   1.2     elric 
    577   1.2     elric static int
    578   1.2     elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
    579   1.1     elric {
    580   1.1     elric 	struct cgd_ioctl ci;
    581   1.1     elric 
    582   1.1     elric 	/* sanity */
    583   1.1     elric 	if (!cgd || !dev)
    584   1.1     elric 		return -1;
    585   1.1     elric 
    586  1.19  christos 	(void)memset(&ci, 0x0, sizeof(ci));
    587  1.16  christos 	ci.ci_disk = dev;
    588  1.16  christos 	ci.ci_alg = string_tocharstar(p->algorithm);
    589  1.16  christos 	ci.ci_ivmethod = string_tocharstar(p->ivmeth);
    590  1.16  christos 	ci.ci_key = bits_getbuf(p->key);
    591   1.1     elric 	ci.ci_keylen = p->keylen;
    592   1.1     elric 	ci.ci_blocksize = p->bsize;
    593   1.1     elric 
    594  1.20    martin 	VPRINTF(1, ("    with alg %s keylen %zu blocksize %zu ivmethod %s\n",
    595   1.5     elric 	    string_tocharstar(p->algorithm), p->keylen, p->bsize,
    596   1.5     elric 	    string_tocharstar(p->ivmeth)));
    597   1.5     elric 	VPRINTF(2, ("key: "));
    598   1.5     elric 	VERBOSE(2, bits_fprint(stdout, p->key));
    599   1.5     elric 	VPRINTF(2, ("\n"));
    600   1.1     elric 
    601   1.1     elric 	if (nflag)
    602   1.1     elric 		return 0;
    603   1.1     elric 
    604  1.17    cbiere 	if (ioctl(fd, CGDIOCSET, &ci) == -1) {
    605  1.17    cbiere 		int saved_errno = errno;
    606  1.17    cbiere 		warn("ioctl");
    607  1.17    cbiere 		return saved_errno;
    608   1.1     elric 	}
    609   1.1     elric 
    610   1.1     elric 	return 0;
    611   1.1     elric }
    612   1.1     elric 
    613   1.3     elric /*
    614   1.3     elric  * verify returns 0 for success, -1 for unrecoverable error, or 1 for retry.
    615   1.3     elric  */
    616   1.3     elric 
    617   1.3     elric #define SCANSIZE	8192
    618   1.3     elric 
    619   1.3     elric static int
    620   1.3     elric verify(struct params *p, int fd)
    621   1.3     elric {
    622   1.3     elric 
    623   1.3     elric 	switch (p->verify_method) {
    624   1.3     elric 	case VERIFY_NONE:
    625   1.3     elric 		return 0;
    626   1.3     elric 	case VERIFY_DISKLABEL:
    627   1.5     elric 		return verify_disklabel(fd);
    628   1.5     elric 	case VERIFY_FFS:
    629   1.5     elric 		return verify_ffs(fd);
    630   1.9        cb 	case VERIFY_REENTER:
    631   1.9        cb 		return verify_reenter(p);
    632   1.3     elric 	default:
    633   1.5     elric 		warnx("unimplemented verification method");
    634   1.3     elric 		return -1;
    635   1.3     elric 	}
    636   1.5     elric }
    637   1.5     elric 
    638   1.5     elric static int
    639   1.5     elric verify_disklabel(int fd)
    640   1.5     elric {
    641   1.5     elric 	struct	disklabel l;
    642  1.17    cbiere 	ssize_t	ret;
    643   1.5     elric 	char	buf[SCANSIZE];
    644   1.3     elric 
    645   1.3     elric 	/*
    646   1.3     elric 	 * we simply scan the first few blocks for a disklabel, ignoring
    647   1.3     elric 	 * any MBR/filecore sorts of logic.  MSDOS and RiscOS can't read
    648   1.3     elric 	 * a cgd, anyway, so it is unlikely that there will be non-native
    649   1.3     elric 	 * partition information.
    650   1.3     elric 	 */
    651   1.3     elric 
    652   1.3     elric 	ret = pread(fd, buf, 8192, 0);
    653  1.17    cbiere 	if (ret < 0) {
    654   1.5     elric 		warn("can't read disklabel area");
    655   1.3     elric 		return -1;
    656   1.3     elric 	}
    657   1.3     elric 
    658   1.3     elric 	/* now scan for the disklabel */
    659   1.3     elric 
    660  1.17    cbiere 	return disklabel_scan(&l, buf, (size_t)ret);
    661   1.3     elric }
    662   1.3     elric 
    663   1.7      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
    664   1.7      fvdl 
    665   1.1     elric static int
    666   1.5     elric verify_ffs(int fd)
    667   1.5     elric {
    668  1.19  christos 	size_t	i;
    669   1.5     elric 
    670   1.7      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    671  1.19  christos 		union {
    672  1.19  christos 		    char	buf[SBLOCKSIZE];
    673  1.19  christos 		    struct	fs fs;
    674  1.19  christos 		} u;
    675  1.17    cbiere 		ssize_t ret;
    676  1.17    cbiere 
    677  1.19  christos 		ret = pread(fd, &u, sizeof(u), sblock_try[i]);
    678  1.17    cbiere 		if (ret < 0) {
    679   1.7      fvdl 			warn("pread");
    680  1.17    cbiere 			break;
    681  1.19  christos 		} else if ((size_t)ret < sizeof(u)) {
    682  1.17    cbiere 			warnx("pread: incomplete block");
    683  1.17    cbiere 			break;
    684   1.7      fvdl 		}
    685  1.19  christos 		switch (u.fs.fs_magic) {
    686   1.7      fvdl 		case FS_UFS1_MAGIC:
    687   1.7      fvdl 		case FS_UFS2_MAGIC:
    688   1.7      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
    689   1.7      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
    690   1.7      fvdl 			return 0;
    691   1.7      fvdl 		default:
    692   1.7      fvdl 			continue;
    693   1.7      fvdl 		}
    694   1.5     elric 	}
    695  1.17    cbiere 
    696  1.17    cbiere 	return 1;	/* failure */
    697   1.9        cb }
    698   1.9        cb 
    699   1.9        cb static int
    700   1.9        cb verify_reenter(struct params *p)
    701   1.9        cb {
    702   1.9        cb 	struct keygen *kg;
    703   1.9        cb 	bits_t *orig_key, *key;
    704   1.9        cb 	int ret;
    705   1.9        cb 
    706   1.9        cb 	ret = 0;
    707   1.9        cb 	for (kg = p->keygen; kg && !ret; kg = kg->next) {
    708  1.10       dan 		if ((kg->kg_method != KEYGEN_PKCS5_PBKDF2_SHA1) &&
    709  1.10       dan 		    (kg->kg_method != KEYGEN_PKCS5_PBKDF2_OLD ))
    710   1.9        cb 			continue;
    711   1.9        cb 
    712   1.9        cb 		orig_key = kg->kg_key;
    713   1.9        cb 		kg->kg_key = NULL;
    714   1.9        cb 
    715  1.10       dan 		/* add a compat flag till the _OLD method goes away */
    716   1.9        cb 		key = getkey_pkcs5_pbkdf2("re-enter device", kg,
    717  1.10       dan 			bits_len(orig_key), kg->kg_method == KEYGEN_PKCS5_PBKDF2_OLD);
    718   1.9        cb 		ret = !bits_match(key, orig_key);
    719   1.9        cb 
    720   1.9        cb 		bits_free(key);
    721   1.9        cb 		bits_free(kg->kg_key);
    722   1.9        cb 		kg->kg_key = orig_key;
    723   1.9        cb 	}
    724   1.9        cb 
    725   1.9        cb 	return ret;
    726   1.5     elric }
    727   1.5     elric 
    728   1.5     elric static int
    729   1.1     elric generate(struct params *p, int argc, char **argv, const char *outfile)
    730   1.1     elric {
    731   1.1     elric 	int	 ret;
    732   1.1     elric 
    733   1.1     elric 	if (argc < 1 || argc > 2)
    734   1.1     elric 		usage();
    735   1.1     elric 
    736   1.5     elric 	p->algorithm = string_fromcharstar(argv[0]);
    737  1.17    cbiere 	if (argc > 1) {
    738  1.19  christos 		size_t keylen;
    739  1.17    cbiere 
    740  1.19  christos 		if (parse_size_t(argv[1], &keylen) == -1) {
    741  1.17    cbiere 			warn("Failed to parse key length");
    742  1.17    cbiere 			return -1;
    743  1.17    cbiere 		}
    744  1.17    cbiere 		p->keylen = keylen;
    745  1.17    cbiere 	}
    746   1.5     elric 
    747   1.5     elric 	ret = params_filldefaults(p);
    748   1.1     elric 	if (ret)
    749   1.1     elric 		return ret;
    750   1.5     elric 
    751   1.5     elric 	if (!p->keygen) {
    752  1.10       dan 		p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
    753   1.5     elric 		if (!p->keygen)
    754   1.5     elric 			return -1;
    755   1.5     elric 	}
    756   1.5     elric 
    757   1.5     elric 	if (keygen_filldefaults(p->keygen, p->keylen)) {
    758   1.5     elric 		warnx("Failed to generate defaults for keygen");
    759   1.5     elric 		return -1;
    760   1.5     elric 	}
    761   1.5     elric 
    762   1.5     elric 	if (!params_verify(p)) {
    763   1.5     elric 		warnx("invalid parameters generated");
    764   1.5     elric 		return -1;
    765   1.5     elric 	}
    766   1.5     elric 
    767   1.5     elric 	return params_cput(p, outfile);
    768   1.5     elric }
    769   1.5     elric 
    770   1.5     elric static int
    771   1.5     elric generate_convert(struct params *p, int argc, char **argv, const char *outfile)
    772   1.5     elric {
    773   1.5     elric 	struct params	*oldp;
    774   1.5     elric 	struct keygen	*kg;
    775   1.5     elric 
    776   1.5     elric 	if (argc != 1)
    777   1.5     elric 		usage();
    778   1.5     elric 
    779   1.5     elric 	oldp = params_cget(*argv);
    780   1.5     elric 	if (!oldp)
    781   1.5     elric 		return -1;
    782   1.5     elric 
    783   1.5     elric 	/* for sanity, we ensure that none of the keygens are randomkey */
    784   1.5     elric 	for (kg=p->keygen; kg; kg=kg->next)
    785   1.5     elric 		if (kg->kg_method == KEYGEN_RANDOMKEY)
    786   1.5     elric 			goto bail;
    787   1.5     elric 	for (kg=oldp->keygen; kg; kg=kg->next)
    788   1.5     elric 		if (kg->kg_method == KEYGEN_RANDOMKEY)
    789   1.5     elric 			goto bail;
    790   1.5     elric 
    791   1.5     elric 	if (!params_verify(oldp)) {
    792   1.5     elric 		warnx("invalid old parameters file \"%s\"", *argv);
    793   1.5     elric 		return -1;
    794   1.1     elric 	}
    795   1.1     elric 
    796   1.5     elric 	oldp->key = getkey("old file", oldp->keygen, oldp->keylen);
    797   1.5     elric 
    798   1.5     elric 	/* we copy across the non-keygen info, here. */
    799   1.5     elric 
    800   1.5     elric 	string_free(p->algorithm);
    801   1.5     elric 	string_free(p->ivmeth);
    802   1.5     elric 
    803   1.5     elric 	p->algorithm = string_dup(oldp->algorithm);
    804   1.5     elric 	p->ivmeth = string_dup(oldp->ivmeth);
    805   1.5     elric 	p->keylen = oldp->keylen;
    806   1.5     elric 	p->bsize = oldp->bsize;
    807   1.5     elric 	if (p->verify_method == VERIFY_UNKNOWN)
    808   1.5     elric 		p->verify_method = oldp->verify_method;
    809   1.5     elric 
    810   1.5     elric 	params_free(oldp);
    811   1.1     elric 
    812   1.5     elric 	if (!p->keygen) {
    813  1.10       dan 		p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
    814   1.5     elric 		if (!p->keygen)
    815   1.1     elric 			return -1;
    816   1.5     elric 	}
    817  1.19  christos 	(void)params_filldefaults(p);
    818  1.19  christos 	(void)keygen_filldefaults(p->keygen, p->keylen);
    819   1.5     elric 	p->key = getkey("new file", p->keygen, p->keylen);
    820   1.5     elric 
    821   1.5     elric 	kg = keygen_generate(KEYGEN_STOREDKEY);
    822   1.5     elric 	kg->kg_key = bits_xor(p->key, oldp->key);
    823   1.5     elric 	keygen_addlist(&p->keygen, kg);
    824   1.5     elric 
    825   1.5     elric 	if (!params_verify(p)) {
    826   1.5     elric 		warnx("can't generate new parameters file");
    827   1.5     elric 		return -1;
    828   1.1     elric 	}
    829   1.1     elric 
    830   1.5     elric 	return params_cput(p, outfile);
    831   1.5     elric bail:
    832   1.5     elric 	params_free(oldp);
    833   1.5     elric 	return -1;
    834   1.1     elric }
    835   1.1     elric 
    836   1.1     elric static int
    837  1.19  christos /*ARGSUSED*/
    838   1.1     elric do_all(const char *cfile, int argc, char **argv,
    839   1.3     elric        int (*conf)(int, char **, struct params *, int))
    840   1.1     elric {
    841   1.1     elric 	FILE		 *f;
    842   1.1     elric 	size_t		  len;
    843   1.1     elric 	size_t		  lineno;
    844   1.1     elric 	int		  my_argc;
    845   1.1     elric 	int		  ret;
    846   1.1     elric 	const char	 *fn;
    847   1.1     elric 	char		 *line;
    848   1.1     elric 	char		**my_argv;
    849   1.1     elric 
    850   1.1     elric 	if (argc > 0)
    851   1.1     elric 		usage();
    852   1.1     elric 
    853   1.1     elric 	if (!cfile[0])
    854   1.1     elric 		fn = CGDCONFIG_CFILE;
    855   1.1     elric 	else
    856   1.1     elric 		fn = cfile;
    857   1.1     elric 
    858   1.1     elric 	f = fopen(fn, "r");
    859   1.1     elric 	if (!f) {
    860   1.5     elric 		warn("could not open config file \"%s\"", fn);
    861   1.1     elric 		return -1;
    862   1.1     elric 	}
    863   1.1     elric 
    864   1.5     elric 	ret = chdir(CGDCONFIG_DIR);
    865   1.5     elric 	if (ret == -1)
    866   1.5     elric 		warn("could not chdir to %s", CGDCONFIG_DIR);
    867   1.5     elric 
    868   1.1     elric 	ret = 0;
    869   1.1     elric 	lineno = 0;
    870   1.1     elric 	for (;;) {
    871   1.1     elric 		line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
    872   1.1     elric 		if (!line)
    873   1.1     elric 			break;
    874   1.1     elric 		if (!*line)
    875   1.1     elric 			continue;
    876   1.1     elric 
    877   1.1     elric 		my_argv = words(line, &my_argc);
    878   1.3     elric 		ret = conf(my_argc, my_argv, NULL, CONFIG_FLAGS_FROMALL);
    879   1.1     elric 		if (ret) {
    880   1.5     elric 			warnx("action failed on \"%s\" line %lu", fn,
    881   1.1     elric 			    (u_long)lineno);
    882   1.1     elric 			break;
    883   1.1     elric 		}
    884   1.1     elric 		words_free(my_argv, my_argc);
    885   1.1     elric 	}
    886   1.1     elric 	return ret;
    887   1.1     elric }
    888  1.13     elric 
    889  1.13     elric static void
    890  1.13     elric eliminate_cores(void)
    891  1.13     elric {
    892  1.13     elric 	struct rlimit	rlp;
    893  1.13     elric 
    894  1.13     elric 	rlp.rlim_cur = 0;
    895  1.13     elric 	rlp.rlim_max = 0;
    896  1.17    cbiere 	if (setrlimit(RLIMIT_CORE, &rlp) == -1)
    897  1.13     elric 		err(EXIT_FAILURE, "Can't disable cores");
    898  1.13     elric }
    899