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