Home | History | Annotate | Line # | Download | only in cgdconfig
cgdconfig.c revision 1.3
      1  1.3  elric /* $NetBSD: cgdconfig.c,v 1.3 2002/10/12 21:02:18 elric Exp $ */
      2  1.1  elric 
      3  1.1  elric /*-
      4  1.1  elric  * Copyright (c) 2002 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.1  elric "@(#) Copyright (c) 2002\
     43  1.1  elric 	The NetBSD Foundation, Inc.  All rights reserved.");
     44  1.3  elric __RCSID("$NetBSD: cgdconfig.c,v 1.3 2002/10/12 21:02:18 elric Exp $");
     45  1.1  elric #endif
     46  1.1  elric 
     47  1.1  elric #include <errno.h>
     48  1.1  elric #include <fcntl.h>
     49  1.1  elric #include <libgen.h>
     50  1.1  elric #include <malloc.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.1  elric #include "params.h"
     64  1.1  elric #include "pkcs5_pbkdf2.h"
     65  1.1  elric #include "utils.h"
     66  1.1  elric 
     67  1.1  elric #define CGDCONFIG_DIR		"/etc/cgd"
     68  1.1  elric #define CGDCONFIG_CFILE		CGDCONFIG_DIR "/cgd.conf"
     69  1.1  elric #define DEFAULT_SALTLEN		128
     70  1.1  elric 
     71  1.1  elric #define ACTION_CONFIGURE	0x1	/* configure, with paramsfile */
     72  1.1  elric #define ACTION_UNCONFIGURE	0x2	/* unconfigure */
     73  1.1  elric #define ACTION_GENERATE		0x3	/* generate a paramsfile */
     74  1.1  elric #define ACTION_CONFIGALL	0x4	/* configure all from config file */
     75  1.1  elric #define ACTION_UNCONFIGALL	0x5	/* unconfigure all from config file */
     76  1.1  elric #define ACTION_CONFIGSTDIN	0x6	/* configure, key from stdin */
     77  1.1  elric 
     78  1.1  elric /* if nflag is set, do not configure/unconfigure the cgd's */
     79  1.1  elric 
     80  1.1  elric int	nflag = 0;
     81  1.1  elric 
     82  1.3  elric static int	configure(int, char **, struct params *, int);
     83  1.1  elric static int	configure_stdin(struct params *, int argc, char **);
     84  1.1  elric static int	generate(struct params *, int, char **, const char *);
     85  1.3  elric static int	unconfigure(int, char **, struct params *, int);
     86  1.3  elric static int	do_all(const char *, int, char **,
     87  1.3  elric 		       int (*)(int, char **, struct params *, int));
     88  1.1  elric 
     89  1.1  elric #define CONFIG_FLAGS_FROMALL	1	/* called from configure_all() */
     90  1.1  elric #define CONFIG_FLAGS_FROMMAIN	2	/* called from main() */
     91  1.1  elric 
     92  1.2  elric static int	 configure_params(int, const char *, const char *,
     93  1.2  elric 				  struct params *);
     94  1.1  elric static void	 key_print(FILE *, const u_int8_t *, int);
     95  1.1  elric static char	*getrandbits(int);
     96  1.1  elric static int	 getkey(const char *, struct params *);
     97  1.1  elric static int	 getkeyfrompassphrase(const char *, struct params *);
     98  1.1  elric static int	 getkeyfromfile(FILE *, struct params *);
     99  1.2  elric static int	 opendisk_werror(const char *, char *, int);
    100  1.3  elric static int	 unconfigure_fd(int);
    101  1.3  elric static int	 verify(struct params *, int);
    102  1.1  elric 
    103  1.3  elric static void	 usage(void);
    104  1.1  elric 
    105  1.1  elric /* Verbose Framework */
    106  1.1  elric int	verbose = 0;
    107  1.1  elric 
    108  1.1  elric #define VERBOSE(x,y)	if (verbose >= x) y
    109  1.1  elric #define VPRINTF(x,y)	if (verbose >= x) printf y
    110  1.1  elric 
    111  1.1  elric static void
    112  1.1  elric usage(void)
    113  1.1  elric {
    114  1.1  elric 
    115  1.3  elric 	fprintf(stderr, "usage: %s [-nv] [-V vmeth] cgd dev [paramsfile]\n",
    116  1.1  elric 	    getprogname());
    117  1.1  elric 	fprintf(stderr, "       %s -C [-nv] [-f configfile]\n", getprogname());
    118  1.1  elric 	fprintf(stderr, "       %s -U [-nv] [-f configfile]\n", getprogname());
    119  1.1  elric 	fprintf(stderr, "       %s -g [-nv] [-i ivmeth] [-k kgmeth] "
    120  1.3  elric 	    "[-o outfile] [-V vmeth] alg [keylen]\n", getprogname());
    121  1.1  elric 	fprintf(stderr, "       %s -s [-nv] [-i ivmeth] cgd dev alg "
    122  1.1  elric 	    "[keylen]\n", getprogname());
    123  1.1  elric 	fprintf(stderr, "       %s -u [-nv] cgd\n", getprogname());
    124  1.1  elric 	exit(1);
    125  1.1  elric }
    126  1.1  elric 
    127  1.1  elric int
    128  1.1  elric main(int argc, char **argv)
    129  1.1  elric {
    130  1.1  elric 	struct params cf;
    131  1.1  elric 	int	action = ACTION_CONFIGURE;
    132  1.1  elric 	int	actions = 0;
    133  1.1  elric 	int	ch;
    134  1.1  elric 	int	ret;
    135  1.1  elric 	char	cfile[FILENAME_MAX] = "";
    136  1.1  elric 	char	outfile[FILENAME_MAX] = "";
    137  1.1  elric 
    138  1.1  elric 	setprogname(*argv);
    139  1.1  elric 	params_init(&cf);
    140  1.1  elric 
    141  1.3  elric 	while ((ch = getopt(argc, argv, "CUV:b:f:gi:k:no:usv")) != -1)
    142  1.1  elric 		switch (ch) {
    143  1.1  elric 		case 'C':
    144  1.1  elric 			action = ACTION_CONFIGALL;
    145  1.1  elric 			actions++;
    146  1.1  elric 			break;
    147  1.1  elric 		case 'U':
    148  1.1  elric 			action = ACTION_UNCONFIGALL;
    149  1.1  elric 			actions++;
    150  1.1  elric 			break;
    151  1.3  elric 		case 'V':
    152  1.3  elric 			ret = params_setverify_method_str(&cf, optarg);
    153  1.3  elric 			if (ret)
    154  1.3  elric 				usage();
    155  1.3  elric 			break;
    156  1.1  elric 		case 'b':
    157  1.1  elric 			ret = params_setbsize(&cf, atoi(optarg));
    158  1.1  elric 			if (ret)
    159  1.1  elric 				usage();
    160  1.1  elric 			break;
    161  1.1  elric 		case 'f':
    162  1.1  elric 			strncpy(cfile, optarg, FILENAME_MAX);
    163  1.1  elric 			break;
    164  1.1  elric 		case 'g':
    165  1.1  elric 			action = ACTION_GENERATE;
    166  1.1  elric 			actions++;
    167  1.1  elric 			break;
    168  1.1  elric 		case 'i':
    169  1.1  elric 			params_setivmeth(&cf, optarg);
    170  1.1  elric 			break;
    171  1.1  elric 		case 'k':
    172  1.1  elric 			ret = params_setkeygen_method_str(&cf, optarg);
    173  1.1  elric 			if (ret)
    174  1.1  elric 				usage();
    175  1.1  elric 			break;
    176  1.1  elric 		case 'n':
    177  1.1  elric 			nflag = 1;
    178  1.1  elric 			break;
    179  1.1  elric 		case 'o':
    180  1.1  elric 			strncpy(outfile, optarg, FILENAME_MAX);
    181  1.1  elric 			break;
    182  1.1  elric 		case 's':
    183  1.1  elric 			action = ACTION_CONFIGSTDIN;
    184  1.1  elric 			actions++;
    185  1.1  elric 			break;
    186  1.1  elric 
    187  1.1  elric 		case 'u':
    188  1.1  elric 			action = ACTION_UNCONFIGURE;
    189  1.1  elric 			actions++;
    190  1.1  elric 			break;
    191  1.1  elric 		case 'v':
    192  1.1  elric 			verbose++;
    193  1.1  elric 			break;
    194  1.1  elric 		default:
    195  1.1  elric 			usage();
    196  1.1  elric 			/* NOTREACHED */
    197  1.1  elric 		}
    198  1.1  elric 
    199  1.1  elric 	argc -= optind;
    200  1.1  elric 	argv += optind;
    201  1.1  elric 
    202  1.1  elric 	/* validate the consistency of the arguments */
    203  1.1  elric 
    204  1.1  elric 	if (actions > 1)
    205  1.1  elric 		usage();
    206  1.1  elric 	if (action == ACTION_CONFIGURE && params_changed(&cf))
    207  1.1  elric 		usage();
    208  1.1  elric 
    209  1.1  elric 	switch (action) {
    210  1.1  elric 	case ACTION_CONFIGURE:
    211  1.3  elric 		return configure(argc, argv, &cf, CONFIG_FLAGS_FROMMAIN);
    212  1.1  elric 	case ACTION_UNCONFIGURE:
    213  1.3  elric 		return unconfigure(argc, argv, NULL, CONFIG_FLAGS_FROMMAIN);
    214  1.1  elric 	case ACTION_GENERATE:
    215  1.1  elric 		return generate(&cf, argc, argv, outfile);
    216  1.1  elric 	case ACTION_CONFIGALL:
    217  1.1  elric 		return do_all(cfile, argc, argv, configure);
    218  1.1  elric 	case ACTION_UNCONFIGALL:
    219  1.1  elric 		return do_all(cfile, argc, argv, unconfigure);
    220  1.1  elric 	case ACTION_CONFIGSTDIN:
    221  1.1  elric 		return configure_stdin(&cf, argc, argv);
    222  1.1  elric 	default:
    223  1.1  elric 		fprintf(stderr, "undefined action\n");
    224  1.1  elric 		return 1;
    225  1.1  elric 	}
    226  1.1  elric 	/* NOTREACHED */
    227  1.1  elric }
    228  1.1  elric 
    229  1.1  elric static int
    230  1.1  elric getkey(const char *target, struct params *p)
    231  1.1  elric {
    232  1.1  elric 
    233  1.1  elric 	switch (p->keygen_method) {
    234  1.1  elric 	case KEYGEN_RANDOMKEY:
    235  1.1  elric 		p->key = getrandbits(p->keylen);
    236  1.1  elric 		if (!p->key)
    237  1.1  elric 			return -1;
    238  1.1  elric 		return 0;
    239  1.1  elric 	case KEYGEN_PKCS5_PBKDF2:
    240  1.1  elric 		return getkeyfrompassphrase(target, p);
    241  1.1  elric 	default:
    242  1.1  elric 		fprintf(stderr, "getkey: unknown keygen_method\n");
    243  1.1  elric 		return -1;
    244  1.1  elric 	}
    245  1.1  elric 	/* NOTREACHED */
    246  1.1  elric }
    247  1.1  elric 
    248  1.1  elric static int
    249  1.1  elric getkeyfromfile(FILE *f, struct params *p)
    250  1.1  elric {
    251  1.1  elric 	int	ret;
    252  1.1  elric 
    253  1.1  elric 	/* XXXrcd: data hiding? */
    254  1.1  elric 	p->key = malloc(p->keylen);
    255  1.1  elric 	if (!p->key)
    256  1.1  elric 		return -1;
    257  1.1  elric 	ret = fread(p->key, p->keylen, 1, f);
    258  1.1  elric 	if (ret < 1) {
    259  1.1  elric 		fprintf(stderr, "failed to read key from stdin\n");
    260  1.1  elric 		return -1;
    261  1.1  elric 	}
    262  1.1  elric 	return 0;
    263  1.1  elric }
    264  1.1  elric 
    265  1.1  elric static int
    266  1.1  elric getkeyfrompassphrase(const char *target, struct params *p)
    267  1.1  elric {
    268  1.1  elric 	int	 ret;
    269  1.1  elric 	char	*passp;
    270  1.1  elric 	char	 buf[1024];
    271  1.1  elric 
    272  1.1  elric 	snprintf(buf, 1024, "%s's passphrase:", target);
    273  1.1  elric 	passp = getpass(buf);
    274  1.1  elric 	/* XXXrcd: data hiding ? we should be allocating the key here. */
    275  1.3  elric 	if (!p->key)
    276  1.3  elric 		free(p->key);
    277  1.1  elric 	ret = pkcs5_pbkdf2(&p->key, BITS2BYTES(p->keylen), passp,
    278  1.1  elric 	    strlen(passp), p->keygen_salt, BITS2BYTES(p->keygen_saltlen),
    279  1.1  elric 	    p->keygen_iterations);
    280  1.1  elric 	if (p->xor_key)
    281  1.1  elric 		memxor(p->key, p->xor_key, BITS2BYTES(p->keylen));
    282  1.1  elric 	return ret;
    283  1.1  elric }
    284  1.1  elric 
    285  1.3  elric /* ARGSUSED */
    286  1.1  elric static int
    287  1.3  elric unconfigure(int argc, char **argv, struct params *inparams, int flags)
    288  1.1  elric {
    289  1.1  elric 	int	fd;
    290  1.1  elric 	int	ret;
    291  1.1  elric 	char	buf[MAXPATHLEN] = "";
    292  1.1  elric 
    293  1.1  elric 	/* only complain about additional arguments, if called from main() */
    294  1.1  elric 	if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
    295  1.1  elric 		usage();
    296  1.1  elric 
    297  1.1  elric 	/* if called from do_all(), then ensure that 2 or 3 args exist */
    298  1.1  elric 	if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
    299  1.1  elric 		return -1;
    300  1.1  elric 
    301  1.1  elric 	fd = opendisk(*argv, O_RDWR, buf, sizeof(buf), 1);
    302  1.1  elric 	if (fd == -1) {
    303  1.1  elric 		fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
    304  1.1  elric 		    *argv, buf, strerror(errno));
    305  1.1  elric 
    306  1.1  elric 		/* this isn't fatal with nflag != 0 */
    307  1.1  elric 		if (!nflag)
    308  1.1  elric 			return errno;
    309  1.1  elric 	}
    310  1.1  elric 
    311  1.1  elric 	VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
    312  1.1  elric 
    313  1.1  elric 	if (nflag)
    314  1.1  elric 		return 0;
    315  1.1  elric 
    316  1.3  elric 	ret = unconfigure_fd(fd);
    317  1.3  elric 	close(fd);
    318  1.3  elric 	return ret;
    319  1.3  elric }
    320  1.3  elric 
    321  1.3  elric static int
    322  1.3  elric unconfigure_fd(int fd)
    323  1.3  elric {
    324  1.3  elric 	struct	cgd_ioctl ci;
    325  1.3  elric 	int	ret;
    326  1.3  elric 
    327  1.1  elric 	ret = ioctl(fd, CGDIOCCLR, &ci);
    328  1.1  elric 	if (ret == -1) {
    329  1.1  elric 		perror("ioctl");
    330  1.3  elric 		return -1;
    331  1.1  elric 	}
    332  1.1  elric 
    333  1.1  elric 	return 0;
    334  1.1  elric }
    335  1.1  elric 
    336  1.1  elric /* ARGSUSED */
    337  1.1  elric static int
    338  1.3  elric configure(int argc, char **argv, struct params *inparams, int flags)
    339  1.1  elric {
    340  1.1  elric 	struct params	params;
    341  1.2  elric 	int		fd;
    342  1.1  elric 	int		ret;
    343  1.1  elric 	char		pfile[FILENAME_MAX];
    344  1.2  elric 	char		cgdname[PATH_MAX];
    345  1.1  elric 
    346  1.1  elric 	params_init(&params);
    347  1.1  elric 
    348  1.1  elric 	switch (argc) {
    349  1.1  elric 	case 2:
    350  1.1  elric 		strlcpy(pfile, CGDCONFIG_DIR, FILENAME_MAX);
    351  1.1  elric 		strlcat(pfile, "/", FILENAME_MAX);
    352  1.1  elric 		strlcat(pfile, basename(argv[1]), FILENAME_MAX);
    353  1.1  elric 		break;
    354  1.1  elric 	case 3:
    355  1.1  elric 		strlcpy(pfile, argv[2], FILENAME_MAX);
    356  1.1  elric 		break;
    357  1.1  elric 	default:
    358  1.1  elric 		/* print usage and exit, only if called from main() */
    359  1.1  elric 		if (flags == CONFIG_FLAGS_FROMMAIN)
    360  1.1  elric 			usage();
    361  1.1  elric 		return -1;
    362  1.1  elric 		/* NOTREACHED */
    363  1.1  elric 	}
    364  1.1  elric 
    365  1.1  elric 	ret = params_cget(&params, pfile);
    366  1.1  elric 	if (ret)
    367  1.1  elric 		return ret;
    368  1.1  elric 	ret = params_filldefaults(&params);
    369  1.1  elric 	if (ret)
    370  1.1  elric 		return ret;
    371  1.2  elric 
    372  1.3  elric 	/*
    373  1.3  elric 	 * over-ride the verify method with that specified on the
    374  1.3  elric 	 * command line
    375  1.3  elric 	 */
    376  1.3  elric 
    377  1.3  elric 	if (inparams && inparams->verify_method != VERIFY_UNKNOWN)
    378  1.3  elric 		params.verify_method = inparams->verify_method;
    379  1.3  elric 
    380  1.3  elric 	/*
    381  1.3  elric 	 * loop over configuring the disk and checking to see if it
    382  1.3  elric 	 * verifies properly.  We open and close the disk device each
    383  1.3  elric 	 * time, because if the user passes us the block device we
    384  1.3  elric 	 * need to flush the buffer cache.
    385  1.3  elric 	 */
    386  1.3  elric 
    387  1.3  elric 	for (;;) {
    388  1.3  elric 		fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    389  1.3  elric 		if (fd == -1)
    390  1.3  elric 			return -1;
    391  1.3  elric 
    392  1.3  elric 		ret = getkey(argv[1], &params);
    393  1.3  elric 		if (ret)
    394  1.3  elric 			goto bail_err;
    395  1.3  elric 
    396  1.3  elric 		ret = configure_params(fd, cgdname, argv[1], &params);
    397  1.3  elric 		if (ret)
    398  1.3  elric 			goto bail_err;
    399  1.3  elric 
    400  1.3  elric 		ret = verify(&params, fd);
    401  1.3  elric 		if (ret == -1)
    402  1.3  elric 			goto bail_err;
    403  1.3  elric 		if (!ret)
    404  1.3  elric 			break;
    405  1.2  elric 
    406  1.3  elric 		fprintf(stderr, "verification failed, please reenter "
    407  1.3  elric 		    "passphrase\n");
    408  1.1  elric 
    409  1.3  elric 		unconfigure_fd(fd);
    410  1.3  elric 		close(fd);
    411  1.3  elric 	}
    412  1.2  elric 
    413  1.1  elric 	params_free(&params);
    414  1.3  elric 	close(fd);
    415  1.3  elric 	return 0;
    416  1.3  elric bail_err:
    417  1.3  elric 	close(fd);
    418  1.3  elric 	return -1;
    419  1.1  elric }
    420  1.1  elric 
    421  1.1  elric static int
    422  1.1  elric configure_stdin(struct params *p, int argc, char **argv)
    423  1.1  elric {
    424  1.2  elric 	int	fd;
    425  1.1  elric 	int	ret;
    426  1.2  elric 	char	cgdname[PATH_MAX];
    427  1.1  elric 
    428  1.1  elric 	if (argc < 3 || argc > 4)
    429  1.1  elric 		usage();
    430  1.1  elric 
    431  1.1  elric 	ret = params_setalgorithm(p, argv[2]);
    432  1.1  elric 	if (ret)
    433  1.1  elric 		return ret;
    434  1.1  elric 	if (argc > 3) {
    435  1.1  elric 		ret = params_setkeylen(p, atoi(argv[3]));
    436  1.1  elric 		if (ret)
    437  1.1  elric 			return ret;
    438  1.1  elric 	}
    439  1.1  elric 
    440  1.1  elric 	ret = params_filldefaults(p);
    441  1.1  elric 	if (ret)
    442  1.1  elric 		return ret;
    443  1.1  elric 
    444  1.2  elric 	fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    445  1.2  elric 	if (fd == -1)
    446  1.2  elric 		return -1;
    447  1.2  elric 
    448  1.1  elric 	ret = getkeyfromfile(stdin, p);
    449  1.1  elric 	if (ret)
    450  1.1  elric 		return -1;
    451  1.1  elric 
    452  1.2  elric 	return configure_params(fd, cgdname, argv[1], p);
    453  1.1  elric }
    454  1.1  elric 
    455  1.1  elric static int
    456  1.2  elric opendisk_werror(const char *cgd, char *buf, int buflen)
    457  1.2  elric {
    458  1.2  elric 	int	fd;
    459  1.2  elric 
    460  1.2  elric 	/* sanity */
    461  1.2  elric 	if (!cgd || !buf)
    462  1.2  elric 		return -1;
    463  1.2  elric 
    464  1.2  elric 	if (nflag) {
    465  1.2  elric 		strncpy(buf, cgd, buflen);
    466  1.2  elric 		return 0;
    467  1.2  elric 	}
    468  1.2  elric 
    469  1.3  elric 	fd = opendisk(cgd, O_RDWR, buf, buflen, 0);
    470  1.2  elric 	if (fd == -1)
    471  1.2  elric 		fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
    472  1.2  elric 		    cgd, buf, strerror(errno));
    473  1.2  elric 	return fd;
    474  1.2  elric }
    475  1.2  elric 
    476  1.2  elric static int
    477  1.2  elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
    478  1.1  elric {
    479  1.1  elric 	struct cgd_ioctl ci;
    480  1.1  elric 	int	  ret;
    481  1.1  elric 
    482  1.1  elric 	/* sanity */
    483  1.1  elric 	if (!cgd || !dev)
    484  1.1  elric 		return -1;
    485  1.1  elric 
    486  1.1  elric 	memset(&ci, 0x0, sizeof(ci));
    487  1.1  elric 	ci.ci_disk = (char *)dev;
    488  1.1  elric 	ci.ci_alg = p->alg;
    489  1.1  elric 	ci.ci_ivmethod = p->ivmeth;
    490  1.1  elric 	ci.ci_key = p->key;
    491  1.1  elric 	ci.ci_keylen = p->keylen;
    492  1.1  elric 	ci.ci_blocksize = p->bsize;
    493  1.1  elric 
    494  1.2  elric 	VPRINTF(1, ("attaching: %s attach to %s\n", cgd, dev));
    495  1.1  elric 	VPRINTF(1, ("    with alg %s keylen %d blocksize %d ivmethod %s\n",
    496  1.1  elric 	    p->alg, p->keylen, p->bsize, p->ivmeth));
    497  1.1  elric 	VERBOSE(2, key_print(stdout, p->key, p->keylen));
    498  1.1  elric 
    499  1.1  elric 	if (nflag)
    500  1.1  elric 		return 0;
    501  1.1  elric 
    502  1.1  elric 	ret = ioctl(fd, CGDIOCSET, &ci);
    503  1.1  elric 	if (ret == -1) {
    504  1.1  elric 		perror("ioctl");
    505  1.1  elric 		return errno;
    506  1.1  elric 	}
    507  1.1  elric 
    508  1.1  elric 	return 0;
    509  1.1  elric }
    510  1.1  elric 
    511  1.3  elric /*
    512  1.3  elric  * verify returns 0 for success, -1 for unrecoverable error, or 1 for retry.
    513  1.3  elric  */
    514  1.3  elric 
    515  1.3  elric #define SCANSIZE	8192
    516  1.3  elric 
    517  1.3  elric static int
    518  1.3  elric verify(struct params *p, int fd)
    519  1.3  elric {
    520  1.3  elric 	struct	disklabel l;
    521  1.3  elric 	int	ret;
    522  1.3  elric 	char	buf[SCANSIZE];
    523  1.3  elric 
    524  1.3  elric 	switch (p->verify_method) {
    525  1.3  elric 	case VERIFY_NONE:
    526  1.3  elric 		return 0;
    527  1.3  elric 	case VERIFY_DISKLABEL:
    528  1.3  elric 		/*
    529  1.3  elric 		 * for now this is the only method, so we just perform it
    530  1.3  elric 		 * in this function.
    531  1.3  elric 		 */
    532  1.3  elric 		break;
    533  1.3  elric 	default:
    534  1.3  elric 		fprintf(stderr, "verify: unimplemented verification method\n");
    535  1.3  elric 		return -1;
    536  1.3  elric 	}
    537  1.3  elric 
    538  1.3  elric 	/*
    539  1.3  elric 	 * we simply scan the first few blocks for a disklabel, ignoring
    540  1.3  elric 	 * any MBR/filecore sorts of logic.  MSDOS and RiscOS can't read
    541  1.3  elric 	 * a cgd, anyway, so it is unlikely that there will be non-native
    542  1.3  elric 	 * partition information.
    543  1.3  elric 	 */
    544  1.3  elric 
    545  1.3  elric 	ret = pread(fd, buf, 8192, 0);
    546  1.3  elric 	if (ret == -1) {
    547  1.3  elric 		fprintf(stderr, "verify: can't read disklabel area\n");
    548  1.3  elric 		return -1;
    549  1.3  elric 	}
    550  1.3  elric 
    551  1.3  elric 	/* now scan for the disklabel */
    552  1.3  elric 
    553  1.3  elric 	return disklabel_scan(&l, buf, sizeof(buf));
    554  1.3  elric }
    555  1.3  elric 
    556  1.1  elric static int
    557  1.1  elric generate(struct params *p, int argc, char **argv, const char *outfile)
    558  1.1  elric {
    559  1.1  elric 	FILE	*f;
    560  1.1  elric 	int	 ret;
    561  1.1  elric 	char	*tmp;
    562  1.1  elric 
    563  1.1  elric 	if (argc < 1 || argc > 2)
    564  1.1  elric 		usage();
    565  1.1  elric 
    566  1.1  elric 	ret = params_setalgorithm(p, argv[0]);
    567  1.1  elric 	if (ret)
    568  1.1  elric 		return ret;
    569  1.1  elric 	if (argc > 1) {
    570  1.1  elric 		ret = params_setkeylen(p, atoi(argv[1]));
    571  1.1  elric 		if (ret)
    572  1.1  elric 			return ret;
    573  1.1  elric 	}
    574  1.1  elric 
    575  1.1  elric 	ret = params_filldefaults(p);
    576  1.1  elric 	if (ret)
    577  1.1  elric 		return ret;
    578  1.1  elric 
    579  1.1  elric 	if (!p->keygen_method != KEYGEN_RANDOMKEY) {
    580  1.1  elric 		tmp = getrandbits(DEFAULT_SALTLEN);
    581  1.1  elric 		params_setkeygen_salt(p, tmp, DEFAULT_SALTLEN);
    582  1.1  elric 		free(tmp);
    583  1.1  elric 		tmp = getrandbits(p->keylen);
    584  1.1  elric 		params_setxor_key(p, tmp, p->keylen);
    585  1.1  elric 		free(tmp);
    586  1.1  elric 
    587  1.1  elric 		/* XXXrcd: generate key hash, if desired */
    588  1.1  elric 	}
    589  1.1  elric 
    590  1.1  elric 	if (*outfile) {
    591  1.1  elric 		f = fopen(outfile, "w");
    592  1.1  elric 		if (!f) {
    593  1.1  elric 			fprintf(stderr, "could not open outfile \"%s\": %s\n",
    594  1.1  elric 			    outfile, strerror(errno));
    595  1.1  elric 			perror("fopen");
    596  1.1  elric 			return -1;
    597  1.1  elric 		}
    598  1.1  elric 	} else {
    599  1.1  elric 		f = stdout;
    600  1.1  elric 	}
    601  1.1  elric 
    602  1.1  elric 	ret = params_fput(p, f);
    603  1.1  elric 	params_free(p);
    604  1.1  elric 	return ret;
    605  1.1  elric }
    606  1.1  elric 
    607  1.1  elric static int
    608  1.1  elric do_all(const char *cfile, int argc, char **argv,
    609  1.3  elric        int (*conf)(int, char **, struct params *, int))
    610  1.1  elric {
    611  1.1  elric 	FILE		 *f;
    612  1.1  elric 	size_t		  len;
    613  1.1  elric 	size_t		  lineno;
    614  1.1  elric 	int		  my_argc;
    615  1.1  elric 	int		  ret;
    616  1.1  elric 	const char	 *fn;
    617  1.1  elric 	char		 *line;
    618  1.1  elric 	char		**my_argv;
    619  1.1  elric 
    620  1.1  elric 	if (argc > 0)
    621  1.1  elric 		usage();
    622  1.1  elric 
    623  1.1  elric 	if (!cfile[0])
    624  1.1  elric 		fn = CGDCONFIG_CFILE;
    625  1.1  elric 	else
    626  1.1  elric 		fn = cfile;
    627  1.1  elric 
    628  1.1  elric 	f = fopen(fn, "r");
    629  1.1  elric 	if (!f) {
    630  1.1  elric 		fprintf(stderr, "could not open config file \"%s\": %s\n",
    631  1.1  elric 		    fn, strerror(errno));
    632  1.1  elric 		return -1;
    633  1.1  elric 	}
    634  1.1  elric 
    635  1.1  elric 	ret = 0;
    636  1.1  elric 	lineno = 0;
    637  1.1  elric 	for (;;) {
    638  1.1  elric 
    639  1.1  elric 		line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
    640  1.1  elric 		if (!line)
    641  1.1  elric 			break;
    642  1.1  elric 		if (!*line)
    643  1.1  elric 			continue;
    644  1.1  elric 
    645  1.1  elric 		my_argv = words(line, &my_argc);
    646  1.3  elric 		ret = conf(my_argc, my_argv, NULL, CONFIG_FLAGS_FROMALL);
    647  1.1  elric 		if (ret) {
    648  1.1  elric 			fprintf(stderr, "on \"%s\" line %lu\n", fn,
    649  1.1  elric 			    (u_long)lineno);
    650  1.1  elric 			break;
    651  1.1  elric 		}
    652  1.1  elric 		words_free(my_argv, my_argc);
    653  1.1  elric 	}
    654  1.1  elric 	return ret;
    655  1.1  elric }
    656  1.1  elric 
    657  1.1  elric /*
    658  1.1  elric  * XXX: key_print doesn't work quite exactly properly if the keylength
    659  1.1  elric  *      is not evenly divisible by 8.  If the key is not divisible by
    660  1.1  elric  *      8 then a few extra bits are printed.
    661  1.1  elric  */
    662  1.1  elric 
    663  1.1  elric static void
    664  1.1  elric key_print(FILE *f, const u_int8_t *key, int len)
    665  1.1  elric {
    666  1.1  elric 	int	i;
    667  1.1  elric 	int	col;
    668  1.1  elric 
    669  1.1  elric 	len = BITS2BYTES(len);
    670  1.1  elric 	fprintf(f, "key: ");
    671  1.1  elric 	for (i=0, col=5; i < len; i++, col+=2) {
    672  1.1  elric 		fprintf(f, "%02x", key[i]);
    673  1.1  elric 		if (col > 70) {
    674  1.1  elric 			col = 5 - 2;
    675  1.1  elric 			fprintf(f, "\n     ");
    676  1.1  elric 		}
    677  1.1  elric 	}
    678  1.1  elric 	fprintf(f, "\n");
    679  1.1  elric }
    680  1.1  elric 
    681  1.1  elric static char *
    682  1.1  elric getrandbits(int len)
    683  1.1  elric {
    684  1.1  elric 	FILE	*f;
    685  1.1  elric 	int	 ret;
    686  1.1  elric 	char	*res;
    687  1.1  elric 
    688  1.1  elric 	len = (len + 7) / 8;
    689  1.1  elric 	res = malloc(len);
    690  1.1  elric 	if (!res)
    691  1.1  elric 		return NULL;
    692  1.1  elric 	f = fopen("/dev/random", "r");
    693  1.1  elric 	if (!f)
    694  1.1  elric 		return NULL;
    695  1.1  elric 	ret = fread(res, len, 1, f);
    696  1.1  elric 	if (ret != 1) {
    697  1.1  elric 		free(res);
    698  1.1  elric 		return NULL;
    699  1.1  elric 	}
    700  1.1  elric 	return res;
    701  1.1  elric }
    702