Home | History | Annotate | Line # | Download | only in cgdconfig
cgdconfig.c revision 1.1
      1  1.1  elric /* $NetBSD: cgdconfig.c,v 1.1 2002/10/04 18:37:20 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.1  elric __RCSID("$NetBSD: cgdconfig.c,v 1.1 2002/10/04 18:37:20 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.1  elric #include <sys/param.h>
     59  1.1  elric 
     60  1.1  elric #include <dev/cgdvar.h>
     61  1.1  elric 
     62  1.1  elric #include "params.h"
     63  1.1  elric #include "pkcs5_pbkdf2.h"
     64  1.1  elric #include "utils.h"
     65  1.1  elric 
     66  1.1  elric #define CGDCONFIG_DIR		"/etc/cgd"
     67  1.1  elric #define CGDCONFIG_CFILE		CGDCONFIG_DIR "/cgd.conf"
     68  1.1  elric #define DEFAULT_SALTLEN		128
     69  1.1  elric 
     70  1.1  elric #define ACTION_CONFIGURE	0x1	/* configure, with paramsfile */
     71  1.1  elric #define ACTION_UNCONFIGURE	0x2	/* unconfigure */
     72  1.1  elric #define ACTION_GENERATE		0x3	/* generate a paramsfile */
     73  1.1  elric #define ACTION_CONFIGALL	0x4	/* configure all from config file */
     74  1.1  elric #define ACTION_UNCONFIGALL	0x5	/* unconfigure all from config file */
     75  1.1  elric #define ACTION_CONFIGSTDIN	0x6	/* configure, key from stdin */
     76  1.1  elric 
     77  1.1  elric /* if nflag is set, do not configure/unconfigure the cgd's */
     78  1.1  elric 
     79  1.1  elric int	nflag = 0;
     80  1.1  elric 
     81  1.1  elric static int	configure(int, char **, int);
     82  1.1  elric static int	configure_stdin(struct params *, int argc, char **);
     83  1.1  elric static int	generate(struct params *, int, char **, const char *);
     84  1.1  elric static int	unconfigure(int, char **, int);
     85  1.1  elric static int	do_all(const char *, int, char **, int (*)(int, char **, int));
     86  1.1  elric 
     87  1.1  elric #define CONFIG_FLAGS_FROMALL	1	/* called from configure_all() */
     88  1.1  elric #define CONFIG_FLAGS_FROMMAIN	2	/* called from main() */
     89  1.1  elric 
     90  1.1  elric static int	 configure_params(const char *, const char *, struct params *);
     91  1.1  elric static void	 key_print(FILE *, const u_int8_t *, int);
     92  1.1  elric static char	*getrandbits(int);
     93  1.1  elric static int	 getkey(const char *, struct params *);
     94  1.1  elric static int	 getkeyfrompassphrase(const char *, struct params *);
     95  1.1  elric static int	 getkeyfromfile(FILE *, struct params *);
     96  1.1  elric 
     97  1.1  elric static void	usage(void);
     98  1.1  elric 
     99  1.1  elric /* Verbose Framework */
    100  1.1  elric int	verbose = 0;
    101  1.1  elric 
    102  1.1  elric #define VERBOSE(x,y)	if (verbose >= x) y
    103  1.1  elric #define VPRINTF(x,y)	if (verbose >= x) printf y
    104  1.1  elric 
    105  1.1  elric static void
    106  1.1  elric usage(void)
    107  1.1  elric {
    108  1.1  elric 
    109  1.1  elric 	fprintf(stderr, "usage: %s [-nv] cgd dev [paramsfile]\n",
    110  1.1  elric 	    getprogname());
    111  1.1  elric 	fprintf(stderr, "       %s -C [-nv] [-f configfile]\n", getprogname());
    112  1.1  elric 	fprintf(stderr, "       %s -U [-nv] [-f configfile]\n", getprogname());
    113  1.1  elric 	fprintf(stderr, "       %s -g [-nv] [-i ivmeth] [-k kgmeth] "
    114  1.1  elric 	    "[-o outfile] alg [keylen]\n", getprogname());
    115  1.1  elric 	fprintf(stderr, "       %s -s [-nv] [-i ivmeth] cgd dev alg "
    116  1.1  elric 	    "[keylen]\n", getprogname());
    117  1.1  elric 	fprintf(stderr, "       %s -u [-nv] cgd\n", getprogname());
    118  1.1  elric 	exit(1);
    119  1.1  elric }
    120  1.1  elric 
    121  1.1  elric int
    122  1.1  elric main(int argc, char **argv)
    123  1.1  elric {
    124  1.1  elric 	struct params cf;
    125  1.1  elric 	int	action = ACTION_CONFIGURE;
    126  1.1  elric 	int	actions = 0;
    127  1.1  elric 	int	ch;
    128  1.1  elric 	int	ret;
    129  1.1  elric 	char	cfile[FILENAME_MAX] = "";
    130  1.1  elric 	char	outfile[FILENAME_MAX] = "";
    131  1.1  elric 
    132  1.1  elric 	setprogname(*argv);
    133  1.1  elric 	params_init(&cf);
    134  1.1  elric 
    135  1.1  elric 	while ((ch = getopt(argc, argv, "CUb:f:gi:k:no:usv")) != -1)
    136  1.1  elric 		switch (ch) {
    137  1.1  elric 		case 'C':
    138  1.1  elric 			action = ACTION_CONFIGALL;
    139  1.1  elric 			actions++;
    140  1.1  elric 			break;
    141  1.1  elric 		case 'U':
    142  1.1  elric 			action = ACTION_UNCONFIGALL;
    143  1.1  elric 			actions++;
    144  1.1  elric 			break;
    145  1.1  elric 
    146  1.1  elric 		case 'b':
    147  1.1  elric 			ret = params_setbsize(&cf, atoi(optarg));
    148  1.1  elric 			if (ret)
    149  1.1  elric 				usage();
    150  1.1  elric 			break;
    151  1.1  elric 		case 'f':
    152  1.1  elric 			strncpy(cfile, optarg, FILENAME_MAX);
    153  1.1  elric 			break;
    154  1.1  elric 		case 'g':
    155  1.1  elric 			action = ACTION_GENERATE;
    156  1.1  elric 			actions++;
    157  1.1  elric 			break;
    158  1.1  elric 		case 'i':
    159  1.1  elric 			params_setivmeth(&cf, optarg);
    160  1.1  elric 			break;
    161  1.1  elric 		case 'k':
    162  1.1  elric 			ret = params_setkeygen_method_str(&cf, optarg);
    163  1.1  elric 			if (ret)
    164  1.1  elric 				usage();
    165  1.1  elric 			break;
    166  1.1  elric 		case 'n':
    167  1.1  elric 			nflag = 1;
    168  1.1  elric 			break;
    169  1.1  elric 		case 'o':
    170  1.1  elric 			strncpy(outfile, optarg, FILENAME_MAX);
    171  1.1  elric 			break;
    172  1.1  elric 		case 's':
    173  1.1  elric 			action = ACTION_CONFIGSTDIN;
    174  1.1  elric 			actions++;
    175  1.1  elric 			break;
    176  1.1  elric 
    177  1.1  elric 		case 'u':
    178  1.1  elric 			action = ACTION_UNCONFIGURE;
    179  1.1  elric 			actions++;
    180  1.1  elric 			break;
    181  1.1  elric 		case 'v':
    182  1.1  elric 			verbose++;
    183  1.1  elric 			break;
    184  1.1  elric 		default:
    185  1.1  elric 			usage();
    186  1.1  elric 			/* NOTREACHED */
    187  1.1  elric 		}
    188  1.1  elric 
    189  1.1  elric 	argc -= optind;
    190  1.1  elric 	argv += optind;
    191  1.1  elric 
    192  1.1  elric 	/* validate the consistency of the arguments */
    193  1.1  elric 
    194  1.1  elric 	if (actions > 1)
    195  1.1  elric 		usage();
    196  1.1  elric 	if (action == ACTION_CONFIGURE && params_changed(&cf))
    197  1.1  elric 		usage();
    198  1.1  elric 
    199  1.1  elric 	switch (action) {
    200  1.1  elric 	case ACTION_CONFIGURE:
    201  1.1  elric 		return configure(argc, argv, CONFIG_FLAGS_FROMMAIN);
    202  1.1  elric 	case ACTION_UNCONFIGURE:
    203  1.1  elric 		return unconfigure(argc, argv, CONFIG_FLAGS_FROMMAIN);
    204  1.1  elric 	case ACTION_GENERATE:
    205  1.1  elric 		return generate(&cf, argc, argv, outfile);
    206  1.1  elric 	case ACTION_CONFIGALL:
    207  1.1  elric 		return do_all(cfile, argc, argv, configure);
    208  1.1  elric 	case ACTION_UNCONFIGALL:
    209  1.1  elric 		return do_all(cfile, argc, argv, unconfigure);
    210  1.1  elric 	case ACTION_CONFIGSTDIN:
    211  1.1  elric 		return configure_stdin(&cf, argc, argv);
    212  1.1  elric 	default:
    213  1.1  elric 		fprintf(stderr, "undefined action\n");
    214  1.1  elric 		return 1;
    215  1.1  elric 	}
    216  1.1  elric 	/* NOTREACHED */
    217  1.1  elric }
    218  1.1  elric 
    219  1.1  elric static int
    220  1.1  elric getkey(const char *target, struct params *p)
    221  1.1  elric {
    222  1.1  elric 
    223  1.1  elric 	switch (p->keygen_method) {
    224  1.1  elric 	case KEYGEN_RANDOMKEY:
    225  1.1  elric 		p->key = getrandbits(p->keylen);
    226  1.1  elric 		if (!p->key)
    227  1.1  elric 			return -1;
    228  1.1  elric 		return 0;
    229  1.1  elric 	case KEYGEN_PKCS5_PBKDF2:
    230  1.1  elric 		return getkeyfrompassphrase(target, p);
    231  1.1  elric 	default:
    232  1.1  elric 		fprintf(stderr, "getkey: unknown keygen_method\n");
    233  1.1  elric 		return -1;
    234  1.1  elric 	}
    235  1.1  elric 	/* NOTREACHED */
    236  1.1  elric }
    237  1.1  elric 
    238  1.1  elric static int
    239  1.1  elric getkeyfromfile(FILE *f, struct params *p)
    240  1.1  elric {
    241  1.1  elric 	int	ret;
    242  1.1  elric 
    243  1.1  elric 	/* XXXrcd: data hiding? */
    244  1.1  elric 	p->key = malloc(p->keylen);
    245  1.1  elric 	if (!p->key)
    246  1.1  elric 		return -1;
    247  1.1  elric 	ret = fread(p->key, p->keylen, 1, f);
    248  1.1  elric 	if (ret < 1) {
    249  1.1  elric 		fprintf(stderr, "failed to read key from stdin\n");
    250  1.1  elric 		return -1;
    251  1.1  elric 	}
    252  1.1  elric 	return 0;
    253  1.1  elric }
    254  1.1  elric 
    255  1.1  elric static int
    256  1.1  elric getkeyfrompassphrase(const char *target, struct params *p)
    257  1.1  elric {
    258  1.1  elric 	int	 ret;
    259  1.1  elric 	char	*passp;
    260  1.1  elric 	char	 buf[1024];
    261  1.1  elric 
    262  1.1  elric 	snprintf(buf, 1024, "%s's passphrase:", target);
    263  1.1  elric 	passp = getpass(buf);
    264  1.1  elric 	/* XXXrcd: data hiding ? we should be allocating the key here. */
    265  1.1  elric 	ret = pkcs5_pbkdf2(&p->key, BITS2BYTES(p->keylen), passp,
    266  1.1  elric 	    strlen(passp), p->keygen_salt, BITS2BYTES(p->keygen_saltlen),
    267  1.1  elric 	    p->keygen_iterations);
    268  1.1  elric 	if (p->xor_key)
    269  1.1  elric 		memxor(p->key, p->xor_key, BITS2BYTES(p->keylen));
    270  1.1  elric 	return ret;
    271  1.1  elric }
    272  1.1  elric 
    273  1.1  elric static int
    274  1.1  elric unconfigure(int argc, char **argv, int flags)
    275  1.1  elric {
    276  1.1  elric 	struct	cgd_ioctl ci;
    277  1.1  elric 	int	fd;
    278  1.1  elric 	int	ret;
    279  1.1  elric 	char	buf[MAXPATHLEN] = "";
    280  1.1  elric 
    281  1.1  elric 	/* only complain about additional arguments, if called from main() */
    282  1.1  elric 	if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
    283  1.1  elric 		usage();
    284  1.1  elric 
    285  1.1  elric 	/* if called from do_all(), then ensure that 2 or 3 args exist */
    286  1.1  elric 	if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
    287  1.1  elric 		return -1;
    288  1.1  elric 
    289  1.1  elric 	fd = opendisk(*argv, O_RDWR, buf, sizeof(buf), 1);
    290  1.1  elric 	if (fd == -1) {
    291  1.1  elric 		fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
    292  1.1  elric 		    *argv, buf, strerror(errno));
    293  1.1  elric 
    294  1.1  elric 		/* this isn't fatal with nflag != 0 */
    295  1.1  elric 		if (!nflag)
    296  1.1  elric 			return errno;
    297  1.1  elric 	}
    298  1.1  elric 
    299  1.1  elric 	VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
    300  1.1  elric 
    301  1.1  elric 	if (nflag)
    302  1.1  elric 		return 0;
    303  1.1  elric 
    304  1.1  elric 	ret = ioctl(fd, CGDIOCCLR, &ci);
    305  1.1  elric 	if (ret == -1) {
    306  1.1  elric 		perror("ioctl");
    307  1.1  elric 		return errno;
    308  1.1  elric 	}
    309  1.1  elric 
    310  1.1  elric 	return 0;
    311  1.1  elric }
    312  1.1  elric 
    313  1.1  elric /* ARGSUSED */
    314  1.1  elric static int
    315  1.1  elric configure(int argc, char **argv, int flags)
    316  1.1  elric {
    317  1.1  elric 	struct params	params;
    318  1.1  elric 	int		ret;
    319  1.1  elric 	char		pfile[FILENAME_MAX];
    320  1.1  elric 
    321  1.1  elric 	params_init(&params);
    322  1.1  elric 
    323  1.1  elric 	switch (argc) {
    324  1.1  elric 	case 2:
    325  1.1  elric 		strlcpy(pfile, CGDCONFIG_DIR, FILENAME_MAX);
    326  1.1  elric 		strlcat(pfile, "/", FILENAME_MAX);
    327  1.1  elric 		strlcat(pfile, basename(argv[1]), FILENAME_MAX);
    328  1.1  elric 		break;
    329  1.1  elric 	case 3:
    330  1.1  elric 		strlcpy(pfile, argv[2], FILENAME_MAX);
    331  1.1  elric 		break;
    332  1.1  elric 	default:
    333  1.1  elric 		/* print usage and exit, only if called from main() */
    334  1.1  elric 		if (flags == CONFIG_FLAGS_FROMMAIN)
    335  1.1  elric 			usage();
    336  1.1  elric 		return -1;
    337  1.1  elric 		/* NOTREACHED */
    338  1.1  elric 	}
    339  1.1  elric 
    340  1.1  elric 	ret = params_cget(&params, pfile);
    341  1.1  elric 	if (ret)
    342  1.1  elric 		return ret;
    343  1.1  elric 	ret = params_filldefaults(&params);
    344  1.1  elric 	if (ret)
    345  1.1  elric 		return ret;
    346  1.1  elric 	ret = getkey(argv[1], &params);
    347  1.1  elric 	if (ret)
    348  1.1  elric 		return ret;
    349  1.1  elric 
    350  1.1  elric 	ret = configure_params(argv[0], argv[1], &params);
    351  1.1  elric 	params_free(&params);
    352  1.1  elric 	return ret;
    353  1.1  elric }
    354  1.1  elric 
    355  1.1  elric static int
    356  1.1  elric configure_stdin(struct params *p, int argc, char **argv)
    357  1.1  elric {
    358  1.1  elric 	int	ret;
    359  1.1  elric 
    360  1.1  elric 	if (argc < 3 || argc > 4)
    361  1.1  elric 		usage();
    362  1.1  elric 
    363  1.1  elric 	ret = params_setalgorithm(p, argv[2]);
    364  1.1  elric 	if (ret)
    365  1.1  elric 		return ret;
    366  1.1  elric 	if (argc > 3) {
    367  1.1  elric 		ret = params_setkeylen(p, atoi(argv[3]));
    368  1.1  elric 		if (ret)
    369  1.1  elric 			return ret;
    370  1.1  elric 	}
    371  1.1  elric 
    372  1.1  elric 	ret = params_filldefaults(p);
    373  1.1  elric 	if (ret)
    374  1.1  elric 		return ret;
    375  1.1  elric 
    376  1.1  elric 	ret = getkeyfromfile(stdin, p);
    377  1.1  elric 	if (ret)
    378  1.1  elric 		return -1;
    379  1.1  elric 
    380  1.1  elric 	return configure_params(argv[0], argv[1], p);
    381  1.1  elric }
    382  1.1  elric 
    383  1.1  elric static int
    384  1.1  elric configure_params(const char *cgd, const char *dev, struct params *p)
    385  1.1  elric {
    386  1.1  elric 	struct cgd_ioctl ci;
    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.1  elric 	/* sanity */
    392  1.1  elric 	if (!cgd || !dev)
    393  1.1  elric 		return -1;
    394  1.1  elric 
    395  1.1  elric 	memset(&ci, 0x0, sizeof(ci));
    396  1.1  elric 	ci.ci_disk = (char *)dev;
    397  1.1  elric 	ci.ci_alg = p->alg;
    398  1.1  elric 	ci.ci_ivmethod = p->ivmeth;
    399  1.1  elric 	ci.ci_key = p->key;
    400  1.1  elric 	ci.ci_keylen = p->keylen;
    401  1.1  elric 	ci.ci_blocksize = p->bsize;
    402  1.1  elric 
    403  1.1  elric 	fd = opendisk(cgd, O_RDWR, buf, sizeof(buf), 1);
    404  1.1  elric 	if (fd == -1) {
    405  1.1  elric 		fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
    406  1.1  elric 		    cgd, buf, strerror(errno));
    407  1.1  elric 
    408  1.1  elric 		/* with nflag, this is not necessarily a fatal error */
    409  1.1  elric 		if (!nflag)
    410  1.1  elric 			return errno;
    411  1.1  elric 	}
    412  1.1  elric 
    413  1.1  elric 	VPRINTF(1, ("attaching: %s (%s) attach to %s\n", cgd, buf, dev));
    414  1.1  elric 	VPRINTF(1, ("    with alg %s keylen %d blocksize %d ivmethod %s\n",
    415  1.1  elric 	    p->alg, p->keylen, p->bsize, p->ivmeth));
    416  1.1  elric 	VERBOSE(2, key_print(stdout, p->key, p->keylen));
    417  1.1  elric 
    418  1.1  elric 	if (nflag)
    419  1.1  elric 		return 0;
    420  1.1  elric 
    421  1.1  elric 	ret = ioctl(fd, CGDIOCSET, &ci);
    422  1.1  elric 	if (ret == -1) {
    423  1.1  elric 		perror("ioctl");
    424  1.1  elric 		return errno;
    425  1.1  elric 	}
    426  1.1  elric 
    427  1.1  elric 	return 0;
    428  1.1  elric }
    429  1.1  elric 
    430  1.1  elric static int
    431  1.1  elric generate(struct params *p, int argc, char **argv, const char *outfile)
    432  1.1  elric {
    433  1.1  elric 	FILE	*f;
    434  1.1  elric 	int	 ret;
    435  1.1  elric 	char	*tmp;
    436  1.1  elric 
    437  1.1  elric 	if (argc < 1 || argc > 2)
    438  1.1  elric 		usage();
    439  1.1  elric 
    440  1.1  elric 	ret = params_setalgorithm(p, argv[0]);
    441  1.1  elric 	if (ret)
    442  1.1  elric 		return ret;
    443  1.1  elric 	if (argc > 1) {
    444  1.1  elric 		ret = params_setkeylen(p, atoi(argv[1]));
    445  1.1  elric 		if (ret)
    446  1.1  elric 			return ret;
    447  1.1  elric 	}
    448  1.1  elric 
    449  1.1  elric 	ret = params_filldefaults(p);
    450  1.1  elric 	if (ret)
    451  1.1  elric 		return ret;
    452  1.1  elric 
    453  1.1  elric 	if (!p->keygen_method != KEYGEN_RANDOMKEY) {
    454  1.1  elric 		tmp = getrandbits(DEFAULT_SALTLEN);
    455  1.1  elric 		params_setkeygen_salt(p, tmp, DEFAULT_SALTLEN);
    456  1.1  elric 		free(tmp);
    457  1.1  elric 		tmp = getrandbits(p->keylen);
    458  1.1  elric 		params_setxor_key(p, tmp, p->keylen);
    459  1.1  elric 		free(tmp);
    460  1.1  elric 
    461  1.1  elric 		/* XXXrcd: generate key hash, if desired */
    462  1.1  elric 	}
    463  1.1  elric 
    464  1.1  elric 	if (*outfile) {
    465  1.1  elric 		f = fopen(outfile, "w");
    466  1.1  elric 		if (!f) {
    467  1.1  elric 			fprintf(stderr, "could not open outfile \"%s\": %s\n",
    468  1.1  elric 			    outfile, strerror(errno));
    469  1.1  elric 			perror("fopen");
    470  1.1  elric 			return -1;
    471  1.1  elric 		}
    472  1.1  elric 	} else {
    473  1.1  elric 		f = stdout;
    474  1.1  elric 	}
    475  1.1  elric 
    476  1.1  elric 	ret = params_fput(p, f);
    477  1.1  elric 	params_free(p);
    478  1.1  elric 	return ret;
    479  1.1  elric }
    480  1.1  elric 
    481  1.1  elric static int
    482  1.1  elric do_all(const char *cfile, int argc, char **argv,
    483  1.1  elric        int (*conf)(int, char **, int))
    484  1.1  elric {
    485  1.1  elric 	FILE		 *f;
    486  1.1  elric 	size_t		  len;
    487  1.1  elric 	size_t		  lineno;
    488  1.1  elric 	int		  my_argc;
    489  1.1  elric 	int		  ret;
    490  1.1  elric 	const char	 *fn;
    491  1.1  elric 	char		 *line;
    492  1.1  elric 	char		**my_argv;
    493  1.1  elric 
    494  1.1  elric 	if (argc > 0)
    495  1.1  elric 		usage();
    496  1.1  elric 
    497  1.1  elric 	if (!cfile[0])
    498  1.1  elric 		fn = CGDCONFIG_CFILE;
    499  1.1  elric 	else
    500  1.1  elric 		fn = cfile;
    501  1.1  elric 
    502  1.1  elric 	f = fopen(fn, "r");
    503  1.1  elric 	if (!f) {
    504  1.1  elric 		fprintf(stderr, "could not open config file \"%s\": %s\n",
    505  1.1  elric 		    fn, strerror(errno));
    506  1.1  elric 		return -1;
    507  1.1  elric 	}
    508  1.1  elric 
    509  1.1  elric 	ret = 0;
    510  1.1  elric 	lineno = 0;
    511  1.1  elric 	for (;;) {
    512  1.1  elric 
    513  1.1  elric 		line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
    514  1.1  elric 		if (!line)
    515  1.1  elric 			break;
    516  1.1  elric 		if (!*line)
    517  1.1  elric 			continue;
    518  1.1  elric 
    519  1.1  elric 		my_argv = words(line, &my_argc);
    520  1.1  elric 		ret = conf(my_argc, my_argv, CONFIG_FLAGS_FROMALL);
    521  1.1  elric 		if (ret) {
    522  1.1  elric 			fprintf(stderr, "on \"%s\" line %lu\n", fn,
    523  1.1  elric 			    (u_long)lineno);
    524  1.1  elric 			break;
    525  1.1  elric 		}
    526  1.1  elric 		words_free(my_argv, my_argc);
    527  1.1  elric 	}
    528  1.1  elric 	return ret;
    529  1.1  elric }
    530  1.1  elric 
    531  1.1  elric /*
    532  1.1  elric  * XXX: key_print doesn't work quite exactly properly if the keylength
    533  1.1  elric  *      is not evenly divisible by 8.  If the key is not divisible by
    534  1.1  elric  *      8 then a few extra bits are printed.
    535  1.1  elric  */
    536  1.1  elric 
    537  1.1  elric static void
    538  1.1  elric key_print(FILE *f, const u_int8_t *key, int len)
    539  1.1  elric {
    540  1.1  elric 	int	i;
    541  1.1  elric 	int	col;
    542  1.1  elric 
    543  1.1  elric 	len = BITS2BYTES(len);
    544  1.1  elric 	fprintf(f, "key: ");
    545  1.1  elric 	for (i=0, col=5; i < len; i++, col+=2) {
    546  1.1  elric 		fprintf(f, "%02x", key[i]);
    547  1.1  elric 		if (col > 70) {
    548  1.1  elric 			col = 5 - 2;
    549  1.1  elric 			fprintf(f, "\n     ");
    550  1.1  elric 		}
    551  1.1  elric 	}
    552  1.1  elric 	fprintf(f, "\n");
    553  1.1  elric }
    554  1.1  elric 
    555  1.1  elric static char *
    556  1.1  elric getrandbits(int len)
    557  1.1  elric {
    558  1.1  elric 	FILE	*f;
    559  1.1  elric 	int	 ret;
    560  1.1  elric 	char	*res;
    561  1.1  elric 
    562  1.1  elric 	len = (len + 7) / 8;
    563  1.1  elric 	res = malloc(len);
    564  1.1  elric 	if (!res)
    565  1.1  elric 		return NULL;
    566  1.1  elric 	f = fopen("/dev/random", "r");
    567  1.1  elric 	if (!f)
    568  1.1  elric 		return NULL;
    569  1.1  elric 	ret = fread(res, len, 1, f);
    570  1.1  elric 	if (ret != 1) {
    571  1.1  elric 		free(res);
    572  1.1  elric 		return NULL;
    573  1.1  elric 	}
    574  1.1  elric 	return res;
    575  1.1  elric }
    576