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