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