Home | History | Annotate | Line # | Download | only in cgdconfig
cgdconfig.c revision 1.5
      1  1.5  elric /* $NetBSD: cgdconfig.c,v 1.5 2003/03/24 02:02:50 elric 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.5  elric __RCSID("$NetBSD: cgdconfig.c,v 1.5 2003/03/24 02:02:50 elric 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.1  elric #include <sys/param.h>
     60  1.1  elric 
     61  1.1  elric #include <dev/cgdvar.h>
     62  1.1  elric 
     63  1.5  elric #include <ufs/ffs/fs.h>
     64  1.5  elric 
     65  1.1  elric #include "params.h"
     66  1.1  elric #include "pkcs5_pbkdf2.h"
     67  1.1  elric #include "utils.h"
     68  1.1  elric 
     69  1.1  elric #define CGDCONFIG_DIR		"/etc/cgd"
     70  1.1  elric #define CGDCONFIG_CFILE		CGDCONFIG_DIR "/cgd.conf"
     71  1.1  elric 
     72  1.1  elric #define ACTION_CONFIGURE	0x1	/* configure, with paramsfile */
     73  1.1  elric #define ACTION_UNCONFIGURE	0x2	/* unconfigure */
     74  1.1  elric #define ACTION_GENERATE		0x3	/* generate a paramsfile */
     75  1.5  elric #define ACTION_GENERATE_CONVERT	0x4	/* generate a ``dup'' paramsfile */
     76  1.5  elric #define ACTION_CONFIGALL	0x5	/* configure all from config file */
     77  1.5  elric #define ACTION_UNCONFIGALL	0x6	/* unconfigure all from config file */
     78  1.5  elric #define ACTION_CONFIGSTDIN	0x7	/* configure, key from stdin */
     79  1.1  elric 
     80  1.1  elric /* if nflag is set, do not configure/unconfigure the cgd's */
     81  1.1  elric 
     82  1.1  elric int	nflag = 0;
     83  1.1  elric 
     84  1.3  elric static int	configure(int, char **, struct params *, int);
     85  1.1  elric static int	configure_stdin(struct params *, int argc, char **);
     86  1.1  elric static int	generate(struct params *, int, char **, const char *);
     87  1.5  elric static int	generate_convert(struct params *, int, char **, const char *);
     88  1.3  elric static int	unconfigure(int, char **, struct params *, int);
     89  1.3  elric static int	do_all(const char *, int, char **,
     90  1.3  elric 		       int (*)(int, char **, struct params *, int));
     91  1.1  elric 
     92  1.1  elric #define CONFIG_FLAGS_FROMALL	1	/* called from configure_all() */
     93  1.1  elric #define CONFIG_FLAGS_FROMMAIN	2	/* called from main() */
     94  1.1  elric 
     95  1.2  elric static int	 configure_params(int, const char *, const char *,
     96  1.2  elric 				  struct params *);
     97  1.5  elric static bits_t	*getkey(const char *, struct keygen *, int);
     98  1.5  elric static bits_t	*getkey_storedkey(const char *, struct keygen *, int);
     99  1.5  elric static bits_t	*getkey_randomkey(const char *, struct keygen *, int);
    100  1.5  elric static bits_t	*getkey_pkcs5_pbkdf2(const char *, struct keygen *, int);
    101  1.2  elric static int	 opendisk_werror(const char *, char *, int);
    102  1.3  elric static int	 unconfigure_fd(int);
    103  1.3  elric static int	 verify(struct params *, int);
    104  1.5  elric static int	 verify_disklabel(int);
    105  1.5  elric static int	 verify_ffs(int);
    106  1.1  elric 
    107  1.3  elric static void	 usage(void);
    108  1.1  elric 
    109  1.1  elric /* Verbose Framework */
    110  1.1  elric int	verbose = 0;
    111  1.1  elric 
    112  1.1  elric #define VERBOSE(x,y)	if (verbose >= x) y
    113  1.1  elric #define VPRINTF(x,y)	if (verbose >= x) 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.3  elric 	fprintf(stderr, "usage: %s [-nv] [-V vmeth] cgd dev [paramsfile]\n",
    120  1.1  elric 	    getprogname());
    121  1.1  elric 	fprintf(stderr, "       %s -C [-nv] [-f configfile]\n", getprogname());
    122  1.1  elric 	fprintf(stderr, "       %s -U [-nv] [-f configfile]\n", getprogname());
    123  1.5  elric 	fprintf(stderr, "       %s -G [-nv] [-i ivmeth] [-k kgmeth] "
    124  1.5  elric 	    "[-o outfile] paramsfile\n", getprogname());
    125  1.1  elric 	fprintf(stderr, "       %s -g [-nv] [-i ivmeth] [-k kgmeth] "
    126  1.5  elric 	    "[-o outfile] alg [keylen]\n", getprogname());
    127  1.1  elric 	fprintf(stderr, "       %s -s [-nv] [-i ivmeth] cgd dev alg "
    128  1.1  elric 	    "[keylen]\n", getprogname());
    129  1.1  elric 	fprintf(stderr, "       %s -u [-nv] cgd\n", getprogname());
    130  1.1  elric 	exit(1);
    131  1.1  elric }
    132  1.1  elric 
    133  1.1  elric int
    134  1.1  elric main(int argc, char **argv)
    135  1.1  elric {
    136  1.5  elric 	struct params *p;
    137  1.5  elric 	struct params *tp;
    138  1.5  elric 	struct keygen *kg;
    139  1.1  elric 	int	action = ACTION_CONFIGURE;
    140  1.1  elric 	int	actions = 0;
    141  1.1  elric 	int	ch;
    142  1.1  elric 	char	cfile[FILENAME_MAX] = "";
    143  1.1  elric 	char	outfile[FILENAME_MAX] = "";
    144  1.1  elric 
    145  1.1  elric 	setprogname(*argv);
    146  1.5  elric 	p = params_new();
    147  1.5  elric 	kg = NULL;
    148  1.1  elric 
    149  1.5  elric 	while ((ch = getopt(argc, argv, "CGUV:b:f:gi:k:no:usv")) != -1)
    150  1.1  elric 		switch (ch) {
    151  1.1  elric 		case 'C':
    152  1.1  elric 			action = ACTION_CONFIGALL;
    153  1.1  elric 			actions++;
    154  1.1  elric 			break;
    155  1.5  elric 		case 'G':
    156  1.5  elric 			action = ACTION_GENERATE_CONVERT;
    157  1.5  elric 			actions++;
    158  1.5  elric 			break;
    159  1.1  elric 		case 'U':
    160  1.1  elric 			action = ACTION_UNCONFIGALL;
    161  1.1  elric 			actions++;
    162  1.1  elric 			break;
    163  1.3  elric 		case 'V':
    164  1.5  elric 			tp = params_verify_method(string_fromcharstar(optarg));
    165  1.5  elric 			if (!tp)
    166  1.3  elric 				usage();
    167  1.5  elric 			p = params_combine(p, tp);
    168  1.3  elric 			break;
    169  1.1  elric 		case 'b':
    170  1.5  elric 			tp = params_bsize(atoi(optarg));
    171  1.5  elric 			if (!tp)
    172  1.1  elric 				usage();
    173  1.5  elric 			p = params_combine(p, tp);
    174  1.1  elric 			break;
    175  1.1  elric 		case 'f':
    176  1.1  elric 			strncpy(cfile, optarg, FILENAME_MAX);
    177  1.1  elric 			break;
    178  1.1  elric 		case 'g':
    179  1.1  elric 			action = ACTION_GENERATE;
    180  1.1  elric 			actions++;
    181  1.1  elric 			break;
    182  1.1  elric 		case 'i':
    183  1.5  elric 			tp = params_ivmeth(string_fromcharstar(optarg));
    184  1.5  elric 			p = params_combine(p, tp);
    185  1.1  elric 			break;
    186  1.1  elric 		case 'k':
    187  1.5  elric 			kg = keygen_method(string_fromcharstar(optarg));
    188  1.5  elric 			if (!kg)
    189  1.1  elric 				usage();
    190  1.5  elric 			keygen_addlist(&p->keygen, kg);
    191  1.1  elric 			break;
    192  1.1  elric 		case 'n':
    193  1.1  elric 			nflag = 1;
    194  1.1  elric 			break;
    195  1.1  elric 		case 'o':
    196  1.1  elric 			strncpy(outfile, optarg, FILENAME_MAX);
    197  1.1  elric 			break;
    198  1.1  elric 		case 's':
    199  1.1  elric 			action = ACTION_CONFIGSTDIN;
    200  1.1  elric 			actions++;
    201  1.1  elric 			break;
    202  1.1  elric 
    203  1.1  elric 		case 'u':
    204  1.1  elric 			action = ACTION_UNCONFIGURE;
    205  1.1  elric 			actions++;
    206  1.1  elric 			break;
    207  1.1  elric 		case 'v':
    208  1.1  elric 			verbose++;
    209  1.1  elric 			break;
    210  1.1  elric 		default:
    211  1.1  elric 			usage();
    212  1.1  elric 			/* NOTREACHED */
    213  1.1  elric 		}
    214  1.1  elric 
    215  1.1  elric 	argc -= optind;
    216  1.1  elric 	argv += optind;
    217  1.1  elric 
    218  1.1  elric 	/* validate the consistency of the arguments */
    219  1.1  elric 
    220  1.1  elric 	if (actions > 1)
    221  1.1  elric 		usage();
    222  1.1  elric 
    223  1.1  elric 	switch (action) {
    224  1.1  elric 	case ACTION_CONFIGURE:
    225  1.5  elric 		return configure(argc, argv, p, CONFIG_FLAGS_FROMMAIN);
    226  1.1  elric 	case ACTION_UNCONFIGURE:
    227  1.3  elric 		return unconfigure(argc, argv, NULL, CONFIG_FLAGS_FROMMAIN);
    228  1.1  elric 	case ACTION_GENERATE:
    229  1.5  elric 		return generate(p, argc, argv, outfile);
    230  1.5  elric 	case ACTION_GENERATE_CONVERT:
    231  1.5  elric 		return generate_convert(p, argc, argv, outfile);
    232  1.1  elric 	case ACTION_CONFIGALL:
    233  1.1  elric 		return do_all(cfile, argc, argv, configure);
    234  1.1  elric 	case ACTION_UNCONFIGALL:
    235  1.1  elric 		return do_all(cfile, argc, argv, unconfigure);
    236  1.1  elric 	case ACTION_CONFIGSTDIN:
    237  1.5  elric 		return configure_stdin(p, argc, argv);
    238  1.1  elric 	default:
    239  1.5  elric 		errx(EXIT_FAILURE, "undefined action");
    240  1.1  elric 	}
    241  1.1  elric 	/* NOTREACHED */
    242  1.1  elric }
    243  1.1  elric 
    244  1.5  elric static bits_t *
    245  1.5  elric getkey(const char *dev, struct keygen *kg, int len)
    246  1.1  elric {
    247  1.5  elric 	bits_t	*ret = NULL;
    248  1.5  elric 	bits_t	*tmp;
    249  1.1  elric 
    250  1.5  elric 	VPRINTF(3, ("getkey(\"%s\", %p, %d) called\n", dev, kg, len));
    251  1.5  elric 	for (; kg; kg=kg->next) {
    252  1.5  elric 		switch (kg->kg_method) {
    253  1.5  elric 		case KEYGEN_STOREDKEY:
    254  1.5  elric 			tmp = getkey_storedkey(dev, kg, len);
    255  1.5  elric 			break;
    256  1.5  elric 		case KEYGEN_RANDOMKEY:
    257  1.5  elric 			tmp = getkey_randomkey(dev, kg, len);
    258  1.5  elric 			break;
    259  1.5  elric 		case KEYGEN_PKCS5_PBKDF2:
    260  1.5  elric 			tmp = getkey_pkcs5_pbkdf2(dev, kg, len);
    261  1.5  elric 			break;
    262  1.5  elric 		default:
    263  1.5  elric 			warnx("unrecognised keygen method %d in getkey()",
    264  1.5  elric 			    kg->kg_method);
    265  1.5  elric 			if (ret)
    266  1.5  elric 				bits_free(ret);
    267  1.5  elric 			return NULL;
    268  1.5  elric 		}
    269  1.5  elric 
    270  1.5  elric 		if (ret)
    271  1.5  elric 			ret = bits_xor_d(tmp, ret);
    272  1.5  elric 		else
    273  1.5  elric 			ret = tmp;
    274  1.1  elric 	}
    275  1.5  elric 
    276  1.5  elric 	return ret;
    277  1.5  elric }
    278  1.5  elric 
    279  1.5  elric /*ARGSUSED*/
    280  1.5  elric static bits_t *
    281  1.5  elric getkey_storedkey(const char *target, struct keygen *kg, int keylen)
    282  1.5  elric {
    283  1.5  elric 
    284  1.5  elric 	return bits_dup(kg->kg_key);
    285  1.1  elric }
    286  1.1  elric 
    287  1.5  elric /*ARGSUSED*/
    288  1.5  elric static bits_t *
    289  1.5  elric getkey_randomkey(const char *target, struct keygen *kg, int keylen)
    290  1.1  elric {
    291  1.1  elric 
    292  1.5  elric 	return bits_getrandombits(keylen);
    293  1.1  elric }
    294  1.1  elric 
    295  1.5  elric /*ARGSUSED*/
    296  1.5  elric static bits_t *
    297  1.5  elric getkey_pkcs5_pbkdf2(const char *target, struct keygen *kg, int keylen)
    298  1.1  elric {
    299  1.5  elric 	bits_t		*ret;
    300  1.5  elric 	char		*passp;
    301  1.5  elric 	char		 buf[1024];
    302  1.5  elric 	u_int8_t	*tmp;
    303  1.1  elric 
    304  1.5  elric 	snprintf(buf, sizeof(buf), "%s's passphrase:", target);
    305  1.1  elric 	passp = getpass(buf);
    306  1.5  elric 	if (pkcs5_pbkdf2(&tmp, BITS2BYTES(keylen), passp, strlen(passp),
    307  1.5  elric 	    bits_getbuf(kg->kg_salt), BITS2BYTES(bits_len(kg->kg_salt)),
    308  1.5  elric 	    kg->kg_iterations)) {
    309  1.5  elric 		warnx("failed to generate PKCS#5 PBKDF2 key");
    310  1.5  elric 		return NULL;
    311  1.5  elric 	}
    312  1.5  elric 
    313  1.5  elric 	ret = bits_new(tmp, keylen);
    314  1.5  elric 	free(tmp);
    315  1.1  elric 	return ret;
    316  1.1  elric }
    317  1.1  elric 
    318  1.5  elric /*ARGSUSED*/
    319  1.1  elric static int
    320  1.3  elric unconfigure(int argc, char **argv, struct params *inparams, int flags)
    321  1.1  elric {
    322  1.1  elric 	int	fd;
    323  1.1  elric 	int	ret;
    324  1.1  elric 	char	buf[MAXPATHLEN] = "";
    325  1.1  elric 
    326  1.1  elric 	/* only complain about additional arguments, if called from main() */
    327  1.1  elric 	if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
    328  1.1  elric 		usage();
    329  1.1  elric 
    330  1.1  elric 	/* if called from do_all(), then ensure that 2 or 3 args exist */
    331  1.1  elric 	if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
    332  1.1  elric 		return -1;
    333  1.1  elric 
    334  1.1  elric 	fd = opendisk(*argv, O_RDWR, buf, sizeof(buf), 1);
    335  1.1  elric 	if (fd == -1) {
    336  1.5  elric 		warn("can't open cgd \"%s\", \"%s\"", *argv, buf);
    337  1.1  elric 
    338  1.1  elric 		/* this isn't fatal with nflag != 0 */
    339  1.1  elric 		if (!nflag)
    340  1.1  elric 			return errno;
    341  1.1  elric 	}
    342  1.1  elric 
    343  1.1  elric 	VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
    344  1.1  elric 
    345  1.1  elric 	if (nflag)
    346  1.1  elric 		return 0;
    347  1.1  elric 
    348  1.3  elric 	ret = unconfigure_fd(fd);
    349  1.3  elric 	close(fd);
    350  1.3  elric 	return ret;
    351  1.3  elric }
    352  1.3  elric 
    353  1.3  elric static int
    354  1.3  elric unconfigure_fd(int fd)
    355  1.3  elric {
    356  1.3  elric 	struct	cgd_ioctl ci;
    357  1.3  elric 	int	ret;
    358  1.3  elric 
    359  1.1  elric 	ret = ioctl(fd, CGDIOCCLR, &ci);
    360  1.1  elric 	if (ret == -1) {
    361  1.1  elric 		perror("ioctl");
    362  1.3  elric 		return -1;
    363  1.1  elric 	}
    364  1.1  elric 
    365  1.1  elric 	return 0;
    366  1.1  elric }
    367  1.1  elric 
    368  1.5  elric /*ARGSUSED*/
    369  1.1  elric static int
    370  1.3  elric configure(int argc, char **argv, struct params *inparams, int flags)
    371  1.1  elric {
    372  1.5  elric 	struct params	*p;
    373  1.5  elric 	int		 fd;
    374  1.5  elric 	int		 ret;
    375  1.5  elric 	char		 pfile[FILENAME_MAX];
    376  1.5  elric 	char		 cgdname[PATH_MAX];
    377  1.1  elric 
    378  1.1  elric 	switch (argc) {
    379  1.1  elric 	case 2:
    380  1.1  elric 		strlcpy(pfile, CGDCONFIG_DIR, FILENAME_MAX);
    381  1.1  elric 		strlcat(pfile, "/", FILENAME_MAX);
    382  1.1  elric 		strlcat(pfile, basename(argv[1]), FILENAME_MAX);
    383  1.1  elric 		break;
    384  1.1  elric 	case 3:
    385  1.1  elric 		strlcpy(pfile, argv[2], FILENAME_MAX);
    386  1.1  elric 		break;
    387  1.1  elric 	default:
    388  1.1  elric 		/* print usage and exit, only if called from main() */
    389  1.5  elric 		if (flags == CONFIG_FLAGS_FROMMAIN) {
    390  1.5  elric 			warnx("wrong number of args");
    391  1.1  elric 			usage();
    392  1.5  elric 		}
    393  1.1  elric 		return -1;
    394  1.1  elric 		/* NOTREACHED */
    395  1.1  elric 	}
    396  1.1  elric 
    397  1.5  elric 	p = params_cget(pfile);
    398  1.5  elric 	if (!p)
    399  1.5  elric 		return -1;
    400  1.2  elric 
    401  1.3  elric 	/*
    402  1.5  elric 	 * over-ride with command line specifications and fill in default
    403  1.5  elric 	 * values.
    404  1.3  elric 	 */
    405  1.3  elric 
    406  1.5  elric 	p = params_combine(p, inparams);
    407  1.5  elric 	ret = params_filldefaults(p);
    408  1.5  elric 	if (ret) {
    409  1.5  elric 		params_free(p);
    410  1.5  elric 		return ret;
    411  1.5  elric 	}
    412  1.5  elric 
    413  1.5  elric 	if (!params_verify(p)) {
    414  1.5  elric 		warnx("params invalid");
    415  1.5  elric 		return -1;
    416  1.5  elric 	}
    417  1.3  elric 
    418  1.3  elric 	/*
    419  1.3  elric 	 * loop over configuring the disk and checking to see if it
    420  1.3  elric 	 * verifies properly.  We open and close the disk device each
    421  1.3  elric 	 * time, because if the user passes us the block device we
    422  1.3  elric 	 * need to flush the buffer cache.
    423  1.3  elric 	 */
    424  1.3  elric 
    425  1.3  elric 	for (;;) {
    426  1.3  elric 		fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    427  1.3  elric 		if (fd == -1)
    428  1.3  elric 			return -1;
    429  1.3  elric 
    430  1.5  elric 		if (p->key)
    431  1.5  elric 			bits_free(p->key);
    432  1.5  elric 
    433  1.5  elric 		p->key = getkey(argv[1], p->keygen, p->keylen);
    434  1.5  elric 		if (!p->key)
    435  1.3  elric 			goto bail_err;
    436  1.3  elric 
    437  1.5  elric 		ret = configure_params(fd, cgdname, argv[1], p);
    438  1.3  elric 		if (ret)
    439  1.3  elric 			goto bail_err;
    440  1.3  elric 
    441  1.5  elric 		ret = verify(p, fd);
    442  1.3  elric 		if (ret == -1)
    443  1.3  elric 			goto bail_err;
    444  1.3  elric 		if (!ret)
    445  1.3  elric 			break;
    446  1.2  elric 
    447  1.3  elric 		fprintf(stderr, "verification failed, please reenter "
    448  1.3  elric 		    "passphrase\n");
    449  1.1  elric 
    450  1.3  elric 		unconfigure_fd(fd);
    451  1.3  elric 		close(fd);
    452  1.3  elric 	}
    453  1.2  elric 
    454  1.5  elric 	params_free(p);
    455  1.3  elric 	close(fd);
    456  1.3  elric 	return 0;
    457  1.3  elric bail_err:
    458  1.5  elric 	params_free(p);
    459  1.3  elric 	close(fd);
    460  1.3  elric 	return -1;
    461  1.1  elric }
    462  1.1  elric 
    463  1.1  elric static int
    464  1.1  elric configure_stdin(struct params *p, int argc, char **argv)
    465  1.1  elric {
    466  1.2  elric 	int	fd;
    467  1.1  elric 	int	ret;
    468  1.2  elric 	char	cgdname[PATH_MAX];
    469  1.1  elric 
    470  1.1  elric 	if (argc < 3 || argc > 4)
    471  1.1  elric 		usage();
    472  1.1  elric 
    473  1.5  elric 	p->algorithm = string_fromcharstar(argv[2]);
    474  1.5  elric 	if (argc > 3)
    475  1.5  elric 		p->keylen = atoi(argv[3]);
    476  1.1  elric 
    477  1.1  elric 	ret = params_filldefaults(p);
    478  1.1  elric 	if (ret)
    479  1.1  elric 		return ret;
    480  1.1  elric 
    481  1.2  elric 	fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    482  1.2  elric 	if (fd == -1)
    483  1.2  elric 		return -1;
    484  1.2  elric 
    485  1.5  elric 	p->key = bits_fget(stdin, p->keylen);
    486  1.5  elric 	if (!p->key) {
    487  1.5  elric 		warnx("failed to read key from stdin");
    488  1.1  elric 		return -1;
    489  1.5  elric 	}
    490  1.1  elric 
    491  1.2  elric 	return configure_params(fd, cgdname, argv[1], p);
    492  1.1  elric }
    493  1.1  elric 
    494  1.1  elric static int
    495  1.2  elric opendisk_werror(const char *cgd, char *buf, int buflen)
    496  1.2  elric {
    497  1.2  elric 	int	fd;
    498  1.2  elric 
    499  1.5  elric 	VPRINTF(3, ("opendisk_werror(%s, %s, %d) called.\n", cgd, buf, buflen));
    500  1.5  elric 
    501  1.2  elric 	/* sanity */
    502  1.2  elric 	if (!cgd || !buf)
    503  1.2  elric 		return -1;
    504  1.2  elric 
    505  1.2  elric 	if (nflag) {
    506  1.2  elric 		strncpy(buf, cgd, buflen);
    507  1.2  elric 		return 0;
    508  1.2  elric 	}
    509  1.2  elric 
    510  1.3  elric 	fd = opendisk(cgd, O_RDWR, buf, buflen, 0);
    511  1.2  elric 	if (fd == -1)
    512  1.5  elric 		warnx("can't open cgd \"%s\", \"%s\"", cgd, buf);
    513  1.5  elric 
    514  1.2  elric 	return fd;
    515  1.2  elric }
    516  1.2  elric 
    517  1.2  elric static int
    518  1.2  elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
    519  1.1  elric {
    520  1.1  elric 	struct cgd_ioctl ci;
    521  1.1  elric 	int	  ret;
    522  1.1  elric 
    523  1.1  elric 	/* sanity */
    524  1.1  elric 	if (!cgd || !dev)
    525  1.1  elric 		return -1;
    526  1.1  elric 
    527  1.1  elric 	memset(&ci, 0x0, sizeof(ci));
    528  1.1  elric 	ci.ci_disk = (char *)dev;
    529  1.5  elric 	ci.ci_alg = (char *)string_tocharstar(p->algorithm);
    530  1.5  elric 	ci.ci_ivmethod = (char *)string_tocharstar(p->ivmeth);
    531  1.5  elric 	ci.ci_key = (char *)bits_getbuf(p->key);
    532  1.1  elric 	ci.ci_keylen = p->keylen;
    533  1.1  elric 	ci.ci_blocksize = p->bsize;
    534  1.1  elric 
    535  1.1  elric 	VPRINTF(1, ("    with alg %s keylen %d blocksize %d ivmethod %s\n",
    536  1.5  elric 	    string_tocharstar(p->algorithm), p->keylen, p->bsize,
    537  1.5  elric 	    string_tocharstar(p->ivmeth)));
    538  1.5  elric 	VPRINTF(2, ("key: "));
    539  1.5  elric 	VERBOSE(2, bits_fprint(stdout, p->key));
    540  1.5  elric 	VPRINTF(2, ("\n"));
    541  1.1  elric 
    542  1.1  elric 	if (nflag)
    543  1.1  elric 		return 0;
    544  1.1  elric 
    545  1.1  elric 	ret = ioctl(fd, CGDIOCSET, &ci);
    546  1.1  elric 	if (ret == -1) {
    547  1.1  elric 		perror("ioctl");
    548  1.1  elric 		return errno;
    549  1.1  elric 	}
    550  1.1  elric 
    551  1.1  elric 	return 0;
    552  1.1  elric }
    553  1.1  elric 
    554  1.3  elric /*
    555  1.3  elric  * verify returns 0 for success, -1 for unrecoverable error, or 1 for retry.
    556  1.3  elric  */
    557  1.3  elric 
    558  1.3  elric #define SCANSIZE	8192
    559  1.3  elric 
    560  1.3  elric static int
    561  1.3  elric verify(struct params *p, int fd)
    562  1.3  elric {
    563  1.3  elric 
    564  1.3  elric 	switch (p->verify_method) {
    565  1.3  elric 	case VERIFY_NONE:
    566  1.3  elric 		return 0;
    567  1.3  elric 	case VERIFY_DISKLABEL:
    568  1.5  elric 		return verify_disklabel(fd);
    569  1.5  elric 	case VERIFY_FFS:
    570  1.5  elric 		return verify_ffs(fd);
    571  1.3  elric 	default:
    572  1.5  elric 		warnx("unimplemented verification method");
    573  1.3  elric 		return -1;
    574  1.3  elric 	}
    575  1.5  elric }
    576  1.5  elric 
    577  1.5  elric static int
    578  1.5  elric verify_disklabel(int fd)
    579  1.5  elric {
    580  1.5  elric 	struct	disklabel l;
    581  1.5  elric 	int	ret;
    582  1.5  elric 	char	buf[SCANSIZE];
    583  1.3  elric 
    584  1.3  elric 	/*
    585  1.3  elric 	 * we simply scan the first few blocks for a disklabel, ignoring
    586  1.3  elric 	 * any MBR/filecore sorts of logic.  MSDOS and RiscOS can't read
    587  1.3  elric 	 * a cgd, anyway, so it is unlikely that there will be non-native
    588  1.3  elric 	 * partition information.
    589  1.3  elric 	 */
    590  1.3  elric 
    591  1.3  elric 	ret = pread(fd, buf, 8192, 0);
    592  1.3  elric 	if (ret == -1) {
    593  1.5  elric 		warn("can't read disklabel area");
    594  1.3  elric 		return -1;
    595  1.3  elric 	}
    596  1.3  elric 
    597  1.3  elric 	/* now scan for the disklabel */
    598  1.3  elric 
    599  1.3  elric 	return disklabel_scan(&l, buf, sizeof(buf));
    600  1.3  elric }
    601  1.3  elric 
    602  1.1  elric static int
    603  1.5  elric verify_ffs(int fd)
    604  1.5  elric {
    605  1.5  elric 	struct	fs *fs;
    606  1.5  elric 	int	ret;
    607  1.5  elric 	char	buf[SBSIZE];
    608  1.5  elric 
    609  1.5  elric 	ret = pread(fd, buf, SBSIZE, SBOFF);
    610  1.5  elric 	fs = (struct fs *)buf;
    611  1.5  elric 	if (ret == -1) {
    612  1.5  elric 		warn("pread");
    613  1.5  elric 		return 0;
    614  1.5  elric 	}
    615  1.5  elric 	if (fs->fs_magic == FS_MAGIC)
    616  1.5  elric 		return 0;
    617  1.5  elric 	if (fs->fs_magic == bswap32(FS_MAGIC))
    618  1.5  elric 		return 0;
    619  1.5  elric 	return 1;
    620  1.5  elric }
    621  1.5  elric 
    622  1.5  elric static int
    623  1.1  elric generate(struct params *p, int argc, char **argv, const char *outfile)
    624  1.1  elric {
    625  1.1  elric 	int	 ret;
    626  1.1  elric 
    627  1.1  elric 	if (argc < 1 || argc > 2)
    628  1.1  elric 		usage();
    629  1.1  elric 
    630  1.5  elric 	p->algorithm = string_fromcharstar(argv[0]);
    631  1.5  elric 	if (argc > 1)
    632  1.5  elric 		p->keylen = atoi(argv[1]);
    633  1.5  elric 
    634  1.5  elric 	ret = params_filldefaults(p);
    635  1.1  elric 	if (ret)
    636  1.1  elric 		return ret;
    637  1.5  elric 
    638  1.5  elric 	if (!p->keygen) {
    639  1.5  elric 		p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2);
    640  1.5  elric 		if (!p->keygen)
    641  1.5  elric 			return -1;
    642  1.5  elric 	}
    643  1.5  elric 
    644  1.5  elric 	if (keygen_filldefaults(p->keygen, p->keylen)) {
    645  1.5  elric 		warnx("Failed to generate defaults for keygen");
    646  1.5  elric 		return -1;
    647  1.5  elric 	}
    648  1.5  elric 
    649  1.5  elric 	if (!params_verify(p)) {
    650  1.5  elric 		warnx("invalid parameters generated");
    651  1.5  elric 		return -1;
    652  1.5  elric 	}
    653  1.5  elric 
    654  1.5  elric 	return params_cput(p, outfile);
    655  1.5  elric }
    656  1.5  elric 
    657  1.5  elric static int
    658  1.5  elric generate_convert(struct params *p, int argc, char **argv, const char *outfile)
    659  1.5  elric {
    660  1.5  elric 	struct params	*oldp;
    661  1.5  elric 	struct keygen	*kg;
    662  1.5  elric 
    663  1.5  elric 	if (argc != 1)
    664  1.5  elric 		usage();
    665  1.5  elric 
    666  1.5  elric 	oldp = params_cget(*argv);
    667  1.5  elric 	if (!oldp)
    668  1.5  elric 		return -1;
    669  1.5  elric 
    670  1.5  elric 	/* for sanity, we ensure that none of the keygens are randomkey */
    671  1.5  elric 	for (kg=p->keygen; kg; kg=kg->next)
    672  1.5  elric 		if (kg->kg_method == KEYGEN_RANDOMKEY)
    673  1.5  elric 			goto bail;
    674  1.5  elric 	for (kg=oldp->keygen; kg; kg=kg->next)
    675  1.5  elric 		if (kg->kg_method == KEYGEN_RANDOMKEY)
    676  1.5  elric 			goto bail;
    677  1.5  elric 
    678  1.5  elric 	if (!params_verify(oldp)) {
    679  1.5  elric 		warnx("invalid old parameters file \"%s\"", *argv);
    680  1.5  elric 		return -1;
    681  1.1  elric 	}
    682  1.1  elric 
    683  1.5  elric 	oldp->key = getkey("old file", oldp->keygen, oldp->keylen);
    684  1.5  elric 
    685  1.5  elric 	/* we copy across the non-keygen info, here. */
    686  1.5  elric 
    687  1.5  elric 	string_free(p->algorithm);
    688  1.5  elric 	string_free(p->ivmeth);
    689  1.5  elric 
    690  1.5  elric 	p->algorithm = string_dup(oldp->algorithm);
    691  1.5  elric 	p->ivmeth = string_dup(oldp->ivmeth);
    692  1.5  elric 	p->keylen = oldp->keylen;
    693  1.5  elric 	p->bsize = oldp->bsize;
    694  1.5  elric 	if (p->verify_method == VERIFY_UNKNOWN)
    695  1.5  elric 		p->verify_method = oldp->verify_method;
    696  1.5  elric 
    697  1.5  elric 	params_free(oldp);
    698  1.1  elric 
    699  1.5  elric 	if (!p->keygen) {
    700  1.5  elric 		p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2);
    701  1.5  elric 		if (!p->keygen)
    702  1.1  elric 			return -1;
    703  1.5  elric 		keygen_filldefaults(p->keygen, p->keylen);
    704  1.5  elric 	}
    705  1.5  elric 	params_filldefaults(p);
    706  1.5  elric 	p->key = getkey("new file", p->keygen, p->keylen);
    707  1.5  elric 
    708  1.5  elric 	kg = keygen_generate(KEYGEN_STOREDKEY);
    709  1.5  elric 	kg->kg_key = bits_xor(p->key, oldp->key);
    710  1.5  elric 	keygen_addlist(&p->keygen, kg);
    711  1.5  elric 
    712  1.5  elric 	if (!params_verify(p)) {
    713  1.5  elric 		warnx("can't generate new parameters file");
    714  1.5  elric 		return -1;
    715  1.1  elric 	}
    716  1.1  elric 
    717  1.5  elric 	return params_cput(p, outfile);
    718  1.5  elric bail:
    719  1.5  elric 	params_free(oldp);
    720  1.5  elric 	return -1;
    721  1.1  elric }
    722  1.1  elric 
    723  1.1  elric static int
    724  1.1  elric do_all(const char *cfile, int argc, char **argv,
    725  1.3  elric        int (*conf)(int, char **, struct params *, int))
    726  1.1  elric {
    727  1.1  elric 	FILE		 *f;
    728  1.1  elric 	size_t		  len;
    729  1.1  elric 	size_t		  lineno;
    730  1.1  elric 	int		  my_argc;
    731  1.1  elric 	int		  ret;
    732  1.1  elric 	const char	 *fn;
    733  1.1  elric 	char		 *line;
    734  1.1  elric 	char		**my_argv;
    735  1.1  elric 
    736  1.1  elric 	if (argc > 0)
    737  1.1  elric 		usage();
    738  1.1  elric 
    739  1.1  elric 	if (!cfile[0])
    740  1.1  elric 		fn = CGDCONFIG_CFILE;
    741  1.1  elric 	else
    742  1.1  elric 		fn = cfile;
    743  1.1  elric 
    744  1.1  elric 	f = fopen(fn, "r");
    745  1.1  elric 	if (!f) {
    746  1.5  elric 		warn("could not open config file \"%s\"", fn);
    747  1.1  elric 		return -1;
    748  1.1  elric 	}
    749  1.1  elric 
    750  1.5  elric 	ret = chdir(CGDCONFIG_DIR);
    751  1.5  elric 	if (ret == -1)
    752  1.5  elric 		warn("could not chdir to %s", CGDCONFIG_DIR);
    753  1.5  elric 
    754  1.1  elric 	ret = 0;
    755  1.1  elric 	lineno = 0;
    756  1.1  elric 	for (;;) {
    757  1.1  elric 		line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
    758  1.1  elric 		if (!line)
    759  1.1  elric 			break;
    760  1.1  elric 		if (!*line)
    761  1.1  elric 			continue;
    762  1.1  elric 
    763  1.1  elric 		my_argv = words(line, &my_argc);
    764  1.3  elric 		ret = conf(my_argc, my_argv, NULL, CONFIG_FLAGS_FROMALL);
    765  1.1  elric 		if (ret) {
    766  1.5  elric 			warnx("action failed on \"%s\" line %lu", fn,
    767  1.1  elric 			    (u_long)lineno);
    768  1.1  elric 			break;
    769  1.1  elric 		}
    770  1.1  elric 		words_free(my_argv, my_argc);
    771  1.1  elric 	}
    772  1.1  elric 	return ret;
    773  1.1  elric }
    774