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