cgdconfig.c revision 1.36 1 1.36 mlelstv /* $NetBSD: cgdconfig.c,v 1.36 2014/12/14 11:31:39 mlelstv 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.36 mlelstv __RCSID("$NetBSD: cgdconfig.c,v 1.36 2014/12/14 11:31:39 mlelstv 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.35 christos #include <paths.h>
49 1.35 christos #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.34 christos #include <sys/statvfs.h>
57 1.35 christos #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.34 christos ACTION_CONFIGSTDIN, /* configure, key from stdin */
81 1.34 christos 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.34 christos 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.34 christos (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.34 christos (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.34 christos 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.34 christos case 'l':
246 1.34 christos set_action(&action, ACTION_LIST);
247 1.34 christos 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.34 christos case ACTION_LIST:
304 1.34 christos 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.36 mlelstv char devname[PATH_MAX];
512 1.36 mlelstv const char *dev;
513 1.36 mlelstv
514 1.36 mlelstv if (argc == 2 || argc == 3) {
515 1.36 mlelstv dev = getfsspecname(devname, sizeof(devname), argv[1]);
516 1.36 mlelstv if (dev == NULL) {
517 1.36 mlelstv warnx("getfsspecname failed: %s", devname);
518 1.36 mlelstv return -1;
519 1.36 mlelstv }
520 1.36 mlelstv }
521 1.1 elric
522 1.34 christos if (argc == 2) {
523 1.36 mlelstv char *pfile, *base;
524 1.36 mlelstv
525 1.36 mlelstv /* make string writable for basename */
526 1.36 mlelstv base = strdup(dev);
527 1.36 mlelstv if (base == NULL)
528 1.36 mlelstv return -1;
529 1.17 cbiere
530 1.17 cbiere if (asprintf(&pfile, "%s/%s",
531 1.36 mlelstv CGDCONFIG_DIR, basename(base)) == -1) {
532 1.36 mlelstv free(base);
533 1.17 cbiere return -1;
534 1.36 mlelstv }
535 1.17 cbiere
536 1.17 cbiere p = params_cget(pfile);
537 1.17 cbiere free(pfile);
538 1.36 mlelstv free(base);
539 1.17 cbiere } else if (argc == 3) {
540 1.17 cbiere p = params_cget(argv[2]);
541 1.17 cbiere } else {
542 1.1 elric /* print usage and exit, only if called from main() */
543 1.5 elric if (flags == CONFIG_FLAGS_FROMMAIN) {
544 1.5 elric warnx("wrong number of args");
545 1.1 elric usage();
546 1.5 elric }
547 1.1 elric return -1;
548 1.1 elric }
549 1.1 elric
550 1.5 elric if (!p)
551 1.5 elric return -1;
552 1.2 elric
553 1.3 elric /*
554 1.5 elric * over-ride with command line specifications and fill in default
555 1.5 elric * values.
556 1.3 elric */
557 1.3 elric
558 1.5 elric p = params_combine(p, inparams);
559 1.5 elric ret = params_filldefaults(p);
560 1.5 elric if (ret) {
561 1.5 elric params_free(p);
562 1.5 elric return ret;
563 1.5 elric }
564 1.5 elric
565 1.5 elric if (!params_verify(p)) {
566 1.5 elric warnx("params invalid");
567 1.5 elric return -1;
568 1.5 elric }
569 1.3 elric
570 1.3 elric /*
571 1.3 elric * loop over configuring the disk and checking to see if it
572 1.3 elric * verifies properly. We open and close the disk device each
573 1.3 elric * time, because if the user passes us the block device we
574 1.3 elric * need to flush the buffer cache.
575 1.22 elric *
576 1.22 elric * We only loop if one of the verification methods prompts for
577 1.22 elric * a password.
578 1.3 elric */
579 1.3 elric
580 1.22 elric for (kg = p->keygen; pflag == PFLAG_GETPASS && kg; kg = kg->next)
581 1.22 elric if ((kg->kg_method == KEYGEN_PKCS5_PBKDF2_SHA1) ||
582 1.22 elric (kg->kg_method == KEYGEN_PKCS5_PBKDF2_OLD )) {
583 1.22 elric loop = 1;
584 1.22 elric break;
585 1.22 elric }
586 1.22 elric
587 1.3 elric for (;;) {
588 1.3 elric fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
589 1.3 elric if (fd == -1)
590 1.3 elric return -1;
591 1.3 elric
592 1.5 elric if (p->key)
593 1.5 elric bits_free(p->key);
594 1.5 elric
595 1.5 elric p->key = getkey(argv[1], p->keygen, p->keylen);
596 1.5 elric if (!p->key)
597 1.3 elric goto bail_err;
598 1.3 elric
599 1.36 mlelstv ret = configure_params(fd, cgdname, dev, p);
600 1.3 elric if (ret)
601 1.3 elric goto bail_err;
602 1.3 elric
603 1.5 elric ret = verify(p, fd);
604 1.3 elric if (ret == -1)
605 1.3 elric goto bail_err;
606 1.3 elric if (!ret)
607 1.3 elric break;
608 1.2 elric
609 1.19 christos (void)unconfigure_fd(fd);
610 1.32 pooka (void)prog_close(fd);
611 1.22 elric
612 1.22 elric if (!loop) {
613 1.22 elric warnx("verification failed permanently");
614 1.22 elric goto bail_err;
615 1.22 elric }
616 1.22 elric
617 1.22 elric warnx("verification failed, please reenter passphrase");
618 1.3 elric }
619 1.2 elric
620 1.5 elric params_free(p);
621 1.32 pooka (void)prog_close(fd);
622 1.3 elric return 0;
623 1.3 elric bail_err:
624 1.5 elric params_free(p);
625 1.32 pooka (void)prog_close(fd);
626 1.3 elric return -1;
627 1.1 elric }
628 1.1 elric
629 1.1 elric static int
630 1.1 elric configure_stdin(struct params *p, int argc, char **argv)
631 1.1 elric {
632 1.36 mlelstv int fd;
633 1.36 mlelstv int ret;
634 1.36 mlelstv char cgdname[PATH_MAX];
635 1.36 mlelstv char devname[PATH_MAX];
636 1.36 mlelstv const char *dev;
637 1.1 elric
638 1.1 elric if (argc < 3 || argc > 4)
639 1.1 elric usage();
640 1.1 elric
641 1.36 mlelstv dev = getfsspecname(devname, sizeof(devname), argv[1]);
642 1.36 mlelstv if (dev == NULL) {
643 1.36 mlelstv warnx("getfsspecname failed: %s", devname);
644 1.36 mlelstv return -1;
645 1.36 mlelstv }
646 1.36 mlelstv
647 1.5 elric p->algorithm = string_fromcharstar(argv[2]);
648 1.17 cbiere if (argc > 3) {
649 1.19 christos size_t keylen;
650 1.17 cbiere
651 1.19 christos if (parse_size_t(argv[3], &keylen) == -1) {
652 1.17 cbiere warn("failed to parse key length");
653 1.17 cbiere return -1;
654 1.17 cbiere }
655 1.17 cbiere p->keylen = keylen;
656 1.17 cbiere }
657 1.1 elric
658 1.1 elric ret = params_filldefaults(p);
659 1.1 elric if (ret)
660 1.1 elric return ret;
661 1.1 elric
662 1.2 elric fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
663 1.2 elric if (fd == -1)
664 1.2 elric return -1;
665 1.2 elric
666 1.5 elric p->key = bits_fget(stdin, p->keylen);
667 1.5 elric if (!p->key) {
668 1.5 elric warnx("failed to read key from stdin");
669 1.1 elric return -1;
670 1.5 elric }
671 1.1 elric
672 1.36 mlelstv return configure_params(fd, cgdname, dev, p);
673 1.1 elric }
674 1.1 elric
675 1.1 elric static int
676 1.17 cbiere opendisk_werror(const char *cgd, char *buf, size_t buflen)
677 1.2 elric {
678 1.2 elric int fd;
679 1.2 elric
680 1.18 cbiere VPRINTF(3, ("opendisk_werror(%s, %s, %zu) called.\n", cgd, buf, buflen));
681 1.5 elric
682 1.2 elric /* sanity */
683 1.2 elric if (!cgd || !buf)
684 1.2 elric return -1;
685 1.2 elric
686 1.2 elric if (nflag) {
687 1.17 cbiere if (strlcpy(buf, cgd, buflen) >= buflen)
688 1.17 cbiere return -1;
689 1.2 elric return 0;
690 1.2 elric }
691 1.2 elric
692 1.32 pooka fd = opendisk1(cgd, O_RDWR, buf, buflen, 0, prog_open);
693 1.2 elric if (fd == -1)
694 1.5 elric warnx("can't open cgd \"%s\", \"%s\"", cgd, buf);
695 1.5 elric
696 1.2 elric return fd;
697 1.2 elric }
698 1.2 elric
699 1.2 elric static int
700 1.2 elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
701 1.1 elric {
702 1.1 elric struct cgd_ioctl ci;
703 1.1 elric
704 1.1 elric /* sanity */
705 1.1 elric if (!cgd || !dev)
706 1.1 elric return -1;
707 1.1 elric
708 1.19 christos (void)memset(&ci, 0x0, sizeof(ci));
709 1.16 christos ci.ci_disk = dev;
710 1.16 christos ci.ci_alg = string_tocharstar(p->algorithm);
711 1.16 christos ci.ci_ivmethod = string_tocharstar(p->ivmeth);
712 1.16 christos ci.ci_key = bits_getbuf(p->key);
713 1.1 elric ci.ci_keylen = p->keylen;
714 1.1 elric ci.ci_blocksize = p->bsize;
715 1.1 elric
716 1.20 martin VPRINTF(1, (" with alg %s keylen %zu blocksize %zu ivmethod %s\n",
717 1.5 elric string_tocharstar(p->algorithm), p->keylen, p->bsize,
718 1.5 elric string_tocharstar(p->ivmeth)));
719 1.5 elric VPRINTF(2, ("key: "));
720 1.5 elric VERBOSE(2, bits_fprint(stdout, p->key));
721 1.5 elric VPRINTF(2, ("\n"));
722 1.1 elric
723 1.1 elric if (nflag)
724 1.1 elric return 0;
725 1.1 elric
726 1.32 pooka if (prog_ioctl(fd, CGDIOCSET, &ci) == -1) {
727 1.17 cbiere int saved_errno = errno;
728 1.17 cbiere warn("ioctl");
729 1.17 cbiere return saved_errno;
730 1.1 elric }
731 1.1 elric
732 1.1 elric return 0;
733 1.1 elric }
734 1.1 elric
735 1.3 elric /*
736 1.3 elric * verify returns 0 for success, -1 for unrecoverable error, or 1 for retry.
737 1.3 elric */
738 1.3 elric
739 1.3 elric #define SCANSIZE 8192
740 1.3 elric
741 1.3 elric static int
742 1.3 elric verify(struct params *p, int fd)
743 1.3 elric {
744 1.3 elric
745 1.3 elric switch (p->verify_method) {
746 1.3 elric case VERIFY_NONE:
747 1.3 elric return 0;
748 1.3 elric case VERIFY_DISKLABEL:
749 1.5 elric return verify_disklabel(fd);
750 1.5 elric case VERIFY_FFS:
751 1.5 elric return verify_ffs(fd);
752 1.9 cb case VERIFY_REENTER:
753 1.9 cb return verify_reenter(p);
754 1.3 elric default:
755 1.5 elric warnx("unimplemented verification method");
756 1.3 elric return -1;
757 1.3 elric }
758 1.5 elric }
759 1.5 elric
760 1.5 elric static int
761 1.5 elric verify_disklabel(int fd)
762 1.5 elric {
763 1.5 elric struct disklabel l;
764 1.17 cbiere ssize_t ret;
765 1.5 elric char buf[SCANSIZE];
766 1.3 elric
767 1.3 elric /*
768 1.3 elric * we simply scan the first few blocks for a disklabel, ignoring
769 1.3 elric * any MBR/filecore sorts of logic. MSDOS and RiscOS can't read
770 1.3 elric * a cgd, anyway, so it is unlikely that there will be non-native
771 1.3 elric * partition information.
772 1.3 elric */
773 1.3 elric
774 1.32 pooka ret = prog_pread(fd, buf, 8192, 0);
775 1.17 cbiere if (ret < 0) {
776 1.5 elric warn("can't read disklabel area");
777 1.3 elric return -1;
778 1.3 elric }
779 1.3 elric
780 1.3 elric /* now scan for the disklabel */
781 1.3 elric
782 1.17 cbiere return disklabel_scan(&l, buf, (size_t)ret);
783 1.3 elric }
784 1.3 elric
785 1.7 fvdl static off_t sblock_try[] = SBLOCKSEARCH;
786 1.7 fvdl
787 1.1 elric static int
788 1.5 elric verify_ffs(int fd)
789 1.5 elric {
790 1.19 christos size_t i;
791 1.5 elric
792 1.7 fvdl for (i = 0; sblock_try[i] != -1; i++) {
793 1.19 christos union {
794 1.19 christos char buf[SBLOCKSIZE];
795 1.19 christos struct fs fs;
796 1.19 christos } u;
797 1.17 cbiere ssize_t ret;
798 1.17 cbiere
799 1.32 pooka ret = prog_pread(fd, &u, sizeof(u), sblock_try[i]);
800 1.17 cbiere if (ret < 0) {
801 1.7 fvdl warn("pread");
802 1.17 cbiere break;
803 1.19 christos } else if ((size_t)ret < sizeof(u)) {
804 1.17 cbiere warnx("pread: incomplete block");
805 1.17 cbiere break;
806 1.7 fvdl }
807 1.19 christos switch (u.fs.fs_magic) {
808 1.7 fvdl case FS_UFS1_MAGIC:
809 1.7 fvdl case FS_UFS2_MAGIC:
810 1.7 fvdl case FS_UFS1_MAGIC_SWAPPED:
811 1.7 fvdl case FS_UFS2_MAGIC_SWAPPED:
812 1.7 fvdl return 0;
813 1.7 fvdl default:
814 1.7 fvdl continue;
815 1.7 fvdl }
816 1.5 elric }
817 1.17 cbiere
818 1.17 cbiere return 1; /* failure */
819 1.9 cb }
820 1.9 cb
821 1.9 cb static int
822 1.9 cb verify_reenter(struct params *p)
823 1.9 cb {
824 1.9 cb struct keygen *kg;
825 1.9 cb bits_t *orig_key, *key;
826 1.9 cb int ret;
827 1.9 cb
828 1.9 cb ret = 0;
829 1.9 cb for (kg = p->keygen; kg && !ret; kg = kg->next) {
830 1.29 elric if ((kg->kg_method != KEYGEN_PKCS5_PBKDF2_SHA1) &&
831 1.10 dan (kg->kg_method != KEYGEN_PKCS5_PBKDF2_OLD ))
832 1.9 cb continue;
833 1.9 cb
834 1.9 cb orig_key = kg->kg_key;
835 1.9 cb kg->kg_key = NULL;
836 1.9 cb
837 1.10 dan /* add a compat flag till the _OLD method goes away */
838 1.9 cb key = getkey_pkcs5_pbkdf2("re-enter device", kg,
839 1.10 dan bits_len(orig_key), kg->kg_method == KEYGEN_PKCS5_PBKDF2_OLD);
840 1.9 cb ret = !bits_match(key, orig_key);
841 1.9 cb
842 1.9 cb bits_free(key);
843 1.9 cb bits_free(kg->kg_key);
844 1.9 cb kg->kg_key = orig_key;
845 1.9 cb }
846 1.9 cb
847 1.9 cb return ret;
848 1.5 elric }
849 1.5 elric
850 1.5 elric static int
851 1.1 elric generate(struct params *p, int argc, char **argv, const char *outfile)
852 1.1 elric {
853 1.1 elric int ret;
854 1.1 elric
855 1.1 elric if (argc < 1 || argc > 2)
856 1.1 elric usage();
857 1.1 elric
858 1.5 elric p->algorithm = string_fromcharstar(argv[0]);
859 1.17 cbiere if (argc > 1) {
860 1.19 christos size_t keylen;
861 1.17 cbiere
862 1.19 christos if (parse_size_t(argv[1], &keylen) == -1) {
863 1.17 cbiere warn("Failed to parse key length");
864 1.17 cbiere return -1;
865 1.17 cbiere }
866 1.17 cbiere p->keylen = keylen;
867 1.17 cbiere }
868 1.5 elric
869 1.5 elric ret = params_filldefaults(p);
870 1.1 elric if (ret)
871 1.1 elric return ret;
872 1.5 elric
873 1.5 elric if (!p->keygen) {
874 1.10 dan p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
875 1.5 elric if (!p->keygen)
876 1.5 elric return -1;
877 1.5 elric }
878 1.5 elric
879 1.5 elric if (keygen_filldefaults(p->keygen, p->keylen)) {
880 1.5 elric warnx("Failed to generate defaults for keygen");
881 1.5 elric return -1;
882 1.5 elric }
883 1.5 elric
884 1.5 elric if (!params_verify(p)) {
885 1.5 elric warnx("invalid parameters generated");
886 1.5 elric return -1;
887 1.5 elric }
888 1.5 elric
889 1.5 elric return params_cput(p, outfile);
890 1.5 elric }
891 1.5 elric
892 1.5 elric static int
893 1.5 elric generate_convert(struct params *p, int argc, char **argv, const char *outfile)
894 1.5 elric {
895 1.5 elric struct params *oldp;
896 1.5 elric struct keygen *kg;
897 1.5 elric
898 1.5 elric if (argc != 1)
899 1.5 elric usage();
900 1.5 elric
901 1.5 elric oldp = params_cget(*argv);
902 1.5 elric if (!oldp)
903 1.5 elric return -1;
904 1.5 elric
905 1.5 elric /* for sanity, we ensure that none of the keygens are randomkey */
906 1.5 elric for (kg=p->keygen; kg; kg=kg->next)
907 1.30 elric if ((kg->kg_method == KEYGEN_RANDOMKEY) ||
908 1.30 elric (kg->kg_method == KEYGEN_URANDOMKEY)) {
909 1.30 elric warnx("can't preserve randomly generated key");
910 1.5 elric goto bail;
911 1.30 elric }
912 1.5 elric for (kg=oldp->keygen; kg; kg=kg->next)
913 1.30 elric if ((kg->kg_method == KEYGEN_RANDOMKEY) ||
914 1.30 elric (kg->kg_method == KEYGEN_URANDOMKEY)) {
915 1.30 elric warnx("can't preserve randomly generated key");
916 1.5 elric goto bail;
917 1.30 elric }
918 1.5 elric
919 1.5 elric if (!params_verify(oldp)) {
920 1.5 elric warnx("invalid old parameters file \"%s\"", *argv);
921 1.5 elric return -1;
922 1.1 elric }
923 1.1 elric
924 1.5 elric oldp->key = getkey("old file", oldp->keygen, oldp->keylen);
925 1.5 elric
926 1.5 elric /* we copy across the non-keygen info, here. */
927 1.5 elric
928 1.5 elric string_free(p->algorithm);
929 1.5 elric string_free(p->ivmeth);
930 1.5 elric
931 1.5 elric p->algorithm = string_dup(oldp->algorithm);
932 1.5 elric p->ivmeth = string_dup(oldp->ivmeth);
933 1.5 elric p->keylen = oldp->keylen;
934 1.5 elric p->bsize = oldp->bsize;
935 1.5 elric if (p->verify_method == VERIFY_UNKNOWN)
936 1.5 elric p->verify_method = oldp->verify_method;
937 1.5 elric
938 1.5 elric params_free(oldp);
939 1.1 elric
940 1.5 elric if (!p->keygen) {
941 1.10 dan p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
942 1.5 elric if (!p->keygen)
943 1.1 elric return -1;
944 1.5 elric }
945 1.19 christos (void)params_filldefaults(p);
946 1.19 christos (void)keygen_filldefaults(p->keygen, p->keylen);
947 1.5 elric p->key = getkey("new file", p->keygen, p->keylen);
948 1.5 elric
949 1.5 elric kg = keygen_generate(KEYGEN_STOREDKEY);
950 1.5 elric kg->kg_key = bits_xor(p->key, oldp->key);
951 1.5 elric keygen_addlist(&p->keygen, kg);
952 1.5 elric
953 1.5 elric if (!params_verify(p)) {
954 1.5 elric warnx("can't generate new parameters file");
955 1.5 elric return -1;
956 1.1 elric }
957 1.1 elric
958 1.5 elric return params_cput(p, outfile);
959 1.5 elric bail:
960 1.5 elric params_free(oldp);
961 1.5 elric return -1;
962 1.1 elric }
963 1.1 elric
964 1.1 elric static int
965 1.19 christos /*ARGSUSED*/
966 1.1 elric do_all(const char *cfile, int argc, char **argv,
967 1.3 elric int (*conf)(int, char **, struct params *, int))
968 1.1 elric {
969 1.1 elric FILE *f;
970 1.1 elric size_t len;
971 1.1 elric size_t lineno;
972 1.1 elric int my_argc;
973 1.1 elric int ret;
974 1.1 elric const char *fn;
975 1.1 elric char *line;
976 1.1 elric char **my_argv;
977 1.1 elric
978 1.1 elric if (argc > 0)
979 1.1 elric usage();
980 1.1 elric
981 1.1 elric if (!cfile[0])
982 1.1 elric fn = CGDCONFIG_CFILE;
983 1.1 elric else
984 1.1 elric fn = cfile;
985 1.1 elric
986 1.1 elric f = fopen(fn, "r");
987 1.1 elric if (!f) {
988 1.5 elric warn("could not open config file \"%s\"", fn);
989 1.1 elric return -1;
990 1.1 elric }
991 1.1 elric
992 1.5 elric ret = chdir(CGDCONFIG_DIR);
993 1.5 elric if (ret == -1)
994 1.5 elric warn("could not chdir to %s", CGDCONFIG_DIR);
995 1.5 elric
996 1.1 elric ret = 0;
997 1.1 elric lineno = 0;
998 1.1 elric for (;;) {
999 1.1 elric line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
1000 1.1 elric if (!line)
1001 1.1 elric break;
1002 1.1 elric if (!*line)
1003 1.1 elric continue;
1004 1.1 elric
1005 1.1 elric my_argv = words(line, &my_argc);
1006 1.3 elric ret = conf(my_argc, my_argv, NULL, CONFIG_FLAGS_FROMALL);
1007 1.1 elric if (ret) {
1008 1.5 elric warnx("action failed on \"%s\" line %lu", fn,
1009 1.1 elric (u_long)lineno);
1010 1.1 elric break;
1011 1.1 elric }
1012 1.1 elric words_free(my_argv, my_argc);
1013 1.1 elric }
1014 1.1 elric return ret;
1015 1.1 elric }
1016 1.13 elric
1017 1.34 christos static const char *
1018 1.34 christos iv_method(int mode)
1019 1.34 christos {
1020 1.34 christos
1021 1.34 christos switch (mode) {
1022 1.34 christos case CGD_CIPHER_CBC_ENCBLKNO8:
1023 1.34 christos return "encblkno8";
1024 1.34 christos case CGD_CIPHER_CBC_ENCBLKNO1:
1025 1.34 christos return "encblkno1";
1026 1.34 christos default:
1027 1.34 christos return "unknown";
1028 1.34 christos }
1029 1.34 christos }
1030 1.34 christos
1031 1.35 christos
1032 1.35 christos static void
1033 1.35 christos show(const char *dev) {
1034 1.35 christos char path[64];
1035 1.35 christos struct cgd_user cgu;
1036 1.35 christos int fd;
1037 1.35 christos
1038 1.35 christos fd = opendisk(dev, O_RDONLY, path, sizeof(path), 0);
1039 1.35 christos if (fd == -1) {
1040 1.35 christos warn("open: %s", dev);
1041 1.35 christos return;
1042 1.35 christos }
1043 1.35 christos
1044 1.35 christos cgu.cgu_unit = -1;
1045 1.35 christos if (prog_ioctl(fd, CGDIOCGET, &cgu) == -1) {
1046 1.35 christos close(fd);
1047 1.35 christos err(1, "CGDIOCGET");
1048 1.35 christos }
1049 1.35 christos
1050 1.35 christos printf("%s: ", dev);
1051 1.35 christos
1052 1.35 christos if (cgu.cgu_dev == 0) {
1053 1.35 christos printf("not in use");
1054 1.35 christos goto out;
1055 1.35 christos }
1056 1.35 christos
1057 1.35 christos dev = devname(cgu.cgu_dev, S_IFBLK);
1058 1.35 christos if (dev != NULL)
1059 1.35 christos printf("%s ", dev);
1060 1.35 christos else
1061 1.35 christos printf("dev %llu,%llu ", (unsigned long long)major(cgu.cgu_dev),
1062 1.35 christos (unsigned long long)minor(cgu.cgu_dev));
1063 1.35 christos
1064 1.35 christos if (verbose)
1065 1.35 christos printf("%s ", cgu.cgu_alg);
1066 1.35 christos if (verbose > 1) {
1067 1.35 christos printf("keylen %d ", cgu.cgu_keylen);
1068 1.35 christos printf("blksize %zd ", cgu.cgu_blocksize);
1069 1.35 christos printf("%s ", iv_method(cgu.cgu_mode));
1070 1.35 christos }
1071 1.35 christos
1072 1.35 christos out:
1073 1.35 christos putchar('\n');
1074 1.35 christos close(fd);
1075 1.35 christos }
1076 1.35 christos
1077 1.34 christos static int
1078 1.34 christos do_list(int argc, char **argv)
1079 1.34 christos {
1080 1.34 christos
1081 1.34 christos if (argc != 0 && argc != 1)
1082 1.34 christos usage();
1083 1.34 christos
1084 1.35 christos if (argc) {
1085 1.35 christos show(argv[0]);
1086 1.35 christos return 0;
1087 1.35 christos }
1088 1.34 christos
1089 1.35 christos DIR *dirp;
1090 1.35 christos struct dirent *dp;
1091 1.35 christos __BITMAP_TYPE(, uint32_t, 65536) bm;
1092 1.35 christos
1093 1.35 christos __BITMAP_ZERO(&bm);
1094 1.35 christos
1095 1.35 christos if ((dirp = opendir(_PATH_DEV)) == NULL)
1096 1.35 christos err(1, "opendir: %s", _PATH_DEV);
1097 1.35 christos
1098 1.35 christos while ((dp = readdir(dirp)) != NULL) {
1099 1.35 christos char *ep;
1100 1.35 christos if (strncmp(dp->d_name, "rcgd", 4) != 0)
1101 1.35 christos continue;
1102 1.35 christos errno = 0;
1103 1.35 christos int n = (int)strtol(dp->d_name + 4, &ep, 0);
1104 1.35 christos if (ep == dp->d_name + 4 || errno != 0) {
1105 1.35 christos warnx("bad name %s", dp->d_name);
1106 1.35 christos continue;
1107 1.34 christos }
1108 1.35 christos *ep = '\0';
1109 1.35 christos if (__BITMAP_ISSET(n, &bm))
1110 1.35 christos continue;
1111 1.35 christos __BITMAP_SET(n, &bm);
1112 1.35 christos show(dp->d_name + 1);
1113 1.34 christos }
1114 1.34 christos
1115 1.35 christos closedir(dirp);
1116 1.34 christos return 0;
1117 1.34 christos }
1118 1.34 christos
1119 1.13 elric static void
1120 1.13 elric eliminate_cores(void)
1121 1.13 elric {
1122 1.13 elric struct rlimit rlp;
1123 1.13 elric
1124 1.13 elric rlp.rlim_cur = 0;
1125 1.13 elric rlp.rlim_max = 0;
1126 1.17 cbiere if (setrlimit(RLIMIT_CORE, &rlp) == -1)
1127 1.13 elric err(EXIT_FAILURE, "Can't disable cores");
1128 1.13 elric }
1129