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