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