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