cgdconfig.c revision 1.2 1 1.2 elric /* $NetBSD: cgdconfig.c,v 1.2 2002/10/12 15:56:26 elric Exp $ */
2 1.1 elric
3 1.1 elric /*-
4 1.1 elric * Copyright (c) 2002 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.1 elric "@(#) Copyright (c) 2002\
43 1.1 elric The NetBSD Foundation, Inc. All rights reserved.");
44 1.2 elric __RCSID("$NetBSD: cgdconfig.c,v 1.2 2002/10/12 15:56:26 elric Exp $");
45 1.1 elric #endif
46 1.1 elric
47 1.1 elric #include <errno.h>
48 1.1 elric #include <fcntl.h>
49 1.1 elric #include <libgen.h>
50 1.1 elric #include <malloc.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.1 elric #include <sys/param.h>
59 1.1 elric
60 1.1 elric #include <dev/cgdvar.h>
61 1.1 elric
62 1.1 elric #include "params.h"
63 1.1 elric #include "pkcs5_pbkdf2.h"
64 1.1 elric #include "utils.h"
65 1.1 elric
66 1.1 elric #define CGDCONFIG_DIR "/etc/cgd"
67 1.1 elric #define CGDCONFIG_CFILE CGDCONFIG_DIR "/cgd.conf"
68 1.1 elric #define DEFAULT_SALTLEN 128
69 1.1 elric
70 1.1 elric #define ACTION_CONFIGURE 0x1 /* configure, with paramsfile */
71 1.1 elric #define ACTION_UNCONFIGURE 0x2 /* unconfigure */
72 1.1 elric #define ACTION_GENERATE 0x3 /* generate a paramsfile */
73 1.1 elric #define ACTION_CONFIGALL 0x4 /* configure all from config file */
74 1.1 elric #define ACTION_UNCONFIGALL 0x5 /* unconfigure all from config file */
75 1.1 elric #define ACTION_CONFIGSTDIN 0x6 /* configure, key from stdin */
76 1.1 elric
77 1.1 elric /* if nflag is set, do not configure/unconfigure the cgd's */
78 1.1 elric
79 1.1 elric int nflag = 0;
80 1.1 elric
81 1.1 elric static int configure(int, char **, int);
82 1.1 elric static int configure_stdin(struct params *, int argc, char **);
83 1.1 elric static int generate(struct params *, int, char **, const char *);
84 1.1 elric static int unconfigure(int, char **, int);
85 1.1 elric static int do_all(const char *, int, char **, int (*)(int, char **, int));
86 1.1 elric
87 1.1 elric #define CONFIG_FLAGS_FROMALL 1 /* called from configure_all() */
88 1.1 elric #define CONFIG_FLAGS_FROMMAIN 2 /* called from main() */
89 1.1 elric
90 1.2 elric static int configure_params(int, const char *, const char *,
91 1.2 elric struct params *);
92 1.1 elric static void key_print(FILE *, const u_int8_t *, int);
93 1.1 elric static char *getrandbits(int);
94 1.1 elric static int getkey(const char *, struct params *);
95 1.1 elric static int getkeyfrompassphrase(const char *, struct params *);
96 1.1 elric static int getkeyfromfile(FILE *, struct params *);
97 1.2 elric static int opendisk_werror(const char *, char *, int);
98 1.1 elric
99 1.1 elric static void usage(void);
100 1.1 elric
101 1.1 elric /* Verbose Framework */
102 1.1 elric int verbose = 0;
103 1.1 elric
104 1.1 elric #define VERBOSE(x,y) if (verbose >= x) y
105 1.1 elric #define VPRINTF(x,y) if (verbose >= x) printf y
106 1.1 elric
107 1.1 elric static void
108 1.1 elric usage(void)
109 1.1 elric {
110 1.1 elric
111 1.1 elric fprintf(stderr, "usage: %s [-nv] cgd dev [paramsfile]\n",
112 1.1 elric getprogname());
113 1.1 elric fprintf(stderr, " %s -C [-nv] [-f configfile]\n", getprogname());
114 1.1 elric fprintf(stderr, " %s -U [-nv] [-f configfile]\n", getprogname());
115 1.1 elric fprintf(stderr, " %s -g [-nv] [-i ivmeth] [-k kgmeth] "
116 1.1 elric "[-o outfile] alg [keylen]\n", getprogname());
117 1.1 elric fprintf(stderr, " %s -s [-nv] [-i ivmeth] cgd dev alg "
118 1.1 elric "[keylen]\n", getprogname());
119 1.1 elric fprintf(stderr, " %s -u [-nv] cgd\n", getprogname());
120 1.1 elric exit(1);
121 1.1 elric }
122 1.1 elric
123 1.1 elric int
124 1.1 elric main(int argc, char **argv)
125 1.1 elric {
126 1.1 elric struct params cf;
127 1.1 elric int action = ACTION_CONFIGURE;
128 1.1 elric int actions = 0;
129 1.1 elric int ch;
130 1.1 elric int ret;
131 1.1 elric char cfile[FILENAME_MAX] = "";
132 1.1 elric char outfile[FILENAME_MAX] = "";
133 1.1 elric
134 1.1 elric setprogname(*argv);
135 1.1 elric params_init(&cf);
136 1.1 elric
137 1.1 elric while ((ch = getopt(argc, argv, "CUb:f:gi:k:no:usv")) != -1)
138 1.1 elric switch (ch) {
139 1.1 elric case 'C':
140 1.1 elric action = ACTION_CONFIGALL;
141 1.1 elric actions++;
142 1.1 elric break;
143 1.1 elric case 'U':
144 1.1 elric action = ACTION_UNCONFIGALL;
145 1.1 elric actions++;
146 1.1 elric break;
147 1.1 elric
148 1.1 elric case 'b':
149 1.1 elric ret = params_setbsize(&cf, atoi(optarg));
150 1.1 elric if (ret)
151 1.1 elric usage();
152 1.1 elric break;
153 1.1 elric case 'f':
154 1.1 elric strncpy(cfile, optarg, FILENAME_MAX);
155 1.1 elric break;
156 1.1 elric case 'g':
157 1.1 elric action = ACTION_GENERATE;
158 1.1 elric actions++;
159 1.1 elric break;
160 1.1 elric case 'i':
161 1.1 elric params_setivmeth(&cf, optarg);
162 1.1 elric break;
163 1.1 elric case 'k':
164 1.1 elric ret = params_setkeygen_method_str(&cf, optarg);
165 1.1 elric if (ret)
166 1.1 elric usage();
167 1.1 elric break;
168 1.1 elric case 'n':
169 1.1 elric nflag = 1;
170 1.1 elric break;
171 1.1 elric case 'o':
172 1.1 elric strncpy(outfile, optarg, FILENAME_MAX);
173 1.1 elric break;
174 1.1 elric case 's':
175 1.1 elric action = ACTION_CONFIGSTDIN;
176 1.1 elric actions++;
177 1.1 elric break;
178 1.1 elric
179 1.1 elric case 'u':
180 1.1 elric action = ACTION_UNCONFIGURE;
181 1.1 elric actions++;
182 1.1 elric break;
183 1.1 elric case 'v':
184 1.1 elric verbose++;
185 1.1 elric break;
186 1.1 elric default:
187 1.1 elric usage();
188 1.1 elric /* NOTREACHED */
189 1.1 elric }
190 1.1 elric
191 1.1 elric argc -= optind;
192 1.1 elric argv += optind;
193 1.1 elric
194 1.1 elric /* validate the consistency of the arguments */
195 1.1 elric
196 1.1 elric if (actions > 1)
197 1.1 elric usage();
198 1.1 elric if (action == ACTION_CONFIGURE && params_changed(&cf))
199 1.1 elric usage();
200 1.1 elric
201 1.1 elric switch (action) {
202 1.1 elric case ACTION_CONFIGURE:
203 1.1 elric return configure(argc, argv, CONFIG_FLAGS_FROMMAIN);
204 1.1 elric case ACTION_UNCONFIGURE:
205 1.1 elric return unconfigure(argc, argv, CONFIG_FLAGS_FROMMAIN);
206 1.1 elric case ACTION_GENERATE:
207 1.1 elric return generate(&cf, argc, argv, outfile);
208 1.1 elric case ACTION_CONFIGALL:
209 1.1 elric return do_all(cfile, argc, argv, configure);
210 1.1 elric case ACTION_UNCONFIGALL:
211 1.1 elric return do_all(cfile, argc, argv, unconfigure);
212 1.1 elric case ACTION_CONFIGSTDIN:
213 1.1 elric return configure_stdin(&cf, argc, argv);
214 1.1 elric default:
215 1.1 elric fprintf(stderr, "undefined action\n");
216 1.1 elric return 1;
217 1.1 elric }
218 1.1 elric /* NOTREACHED */
219 1.1 elric }
220 1.1 elric
221 1.1 elric static int
222 1.1 elric getkey(const char *target, struct params *p)
223 1.1 elric {
224 1.1 elric
225 1.1 elric switch (p->keygen_method) {
226 1.1 elric case KEYGEN_RANDOMKEY:
227 1.1 elric p->key = getrandbits(p->keylen);
228 1.1 elric if (!p->key)
229 1.1 elric return -1;
230 1.1 elric return 0;
231 1.1 elric case KEYGEN_PKCS5_PBKDF2:
232 1.1 elric return getkeyfrompassphrase(target, p);
233 1.1 elric default:
234 1.1 elric fprintf(stderr, "getkey: unknown keygen_method\n");
235 1.1 elric return -1;
236 1.1 elric }
237 1.1 elric /* NOTREACHED */
238 1.1 elric }
239 1.1 elric
240 1.1 elric static int
241 1.1 elric getkeyfromfile(FILE *f, struct params *p)
242 1.1 elric {
243 1.1 elric int ret;
244 1.1 elric
245 1.1 elric /* XXXrcd: data hiding? */
246 1.1 elric p->key = malloc(p->keylen);
247 1.1 elric if (!p->key)
248 1.1 elric return -1;
249 1.1 elric ret = fread(p->key, p->keylen, 1, f);
250 1.1 elric if (ret < 1) {
251 1.1 elric fprintf(stderr, "failed to read key from stdin\n");
252 1.1 elric return -1;
253 1.1 elric }
254 1.1 elric return 0;
255 1.1 elric }
256 1.1 elric
257 1.1 elric static int
258 1.1 elric getkeyfrompassphrase(const char *target, struct params *p)
259 1.1 elric {
260 1.1 elric int ret;
261 1.1 elric char *passp;
262 1.1 elric char buf[1024];
263 1.1 elric
264 1.1 elric snprintf(buf, 1024, "%s's passphrase:", target);
265 1.1 elric passp = getpass(buf);
266 1.1 elric /* XXXrcd: data hiding ? we should be allocating the key here. */
267 1.1 elric ret = pkcs5_pbkdf2(&p->key, BITS2BYTES(p->keylen), passp,
268 1.1 elric strlen(passp), p->keygen_salt, BITS2BYTES(p->keygen_saltlen),
269 1.1 elric p->keygen_iterations);
270 1.1 elric if (p->xor_key)
271 1.1 elric memxor(p->key, p->xor_key, BITS2BYTES(p->keylen));
272 1.1 elric return ret;
273 1.1 elric }
274 1.1 elric
275 1.1 elric static int
276 1.1 elric unconfigure(int argc, char **argv, int flags)
277 1.1 elric {
278 1.1 elric struct cgd_ioctl ci;
279 1.1 elric int fd;
280 1.1 elric int ret;
281 1.1 elric char buf[MAXPATHLEN] = "";
282 1.1 elric
283 1.1 elric /* only complain about additional arguments, if called from main() */
284 1.1 elric if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
285 1.1 elric usage();
286 1.1 elric
287 1.1 elric /* if called from do_all(), then ensure that 2 or 3 args exist */
288 1.1 elric if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
289 1.1 elric return -1;
290 1.1 elric
291 1.1 elric fd = opendisk(*argv, O_RDWR, buf, sizeof(buf), 1);
292 1.1 elric if (fd == -1) {
293 1.1 elric fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
294 1.1 elric *argv, buf, strerror(errno));
295 1.1 elric
296 1.1 elric /* this isn't fatal with nflag != 0 */
297 1.1 elric if (!nflag)
298 1.1 elric return errno;
299 1.1 elric }
300 1.1 elric
301 1.1 elric VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
302 1.1 elric
303 1.1 elric if (nflag)
304 1.1 elric return 0;
305 1.1 elric
306 1.1 elric ret = ioctl(fd, CGDIOCCLR, &ci);
307 1.1 elric if (ret == -1) {
308 1.1 elric perror("ioctl");
309 1.1 elric return errno;
310 1.1 elric }
311 1.1 elric
312 1.1 elric return 0;
313 1.1 elric }
314 1.1 elric
315 1.1 elric /* ARGSUSED */
316 1.1 elric static int
317 1.1 elric configure(int argc, char **argv, int flags)
318 1.1 elric {
319 1.1 elric struct params params;
320 1.2 elric int fd;
321 1.1 elric int ret;
322 1.1 elric char pfile[FILENAME_MAX];
323 1.2 elric char cgdname[PATH_MAX];
324 1.1 elric
325 1.1 elric params_init(¶ms);
326 1.1 elric
327 1.1 elric switch (argc) {
328 1.1 elric case 2:
329 1.1 elric strlcpy(pfile, CGDCONFIG_DIR, FILENAME_MAX);
330 1.1 elric strlcat(pfile, "/", FILENAME_MAX);
331 1.1 elric strlcat(pfile, basename(argv[1]), FILENAME_MAX);
332 1.1 elric break;
333 1.1 elric case 3:
334 1.1 elric strlcpy(pfile, argv[2], FILENAME_MAX);
335 1.1 elric break;
336 1.1 elric default:
337 1.1 elric /* print usage and exit, only if called from main() */
338 1.1 elric if (flags == CONFIG_FLAGS_FROMMAIN)
339 1.1 elric usage();
340 1.1 elric return -1;
341 1.1 elric /* NOTREACHED */
342 1.1 elric }
343 1.1 elric
344 1.1 elric ret = params_cget(¶ms, pfile);
345 1.1 elric if (ret)
346 1.1 elric return ret;
347 1.1 elric ret = params_filldefaults(¶ms);
348 1.1 elric if (ret)
349 1.1 elric return ret;
350 1.2 elric
351 1.2 elric fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
352 1.2 elric if (fd == -1)
353 1.2 elric return -1;
354 1.2 elric
355 1.1 elric ret = getkey(argv[1], ¶ms);
356 1.1 elric if (ret)
357 1.1 elric return ret;
358 1.1 elric
359 1.2 elric ret = configure_params(fd, cgdname, argv[1], ¶ms);
360 1.2 elric
361 1.1 elric params_free(¶ms);
362 1.1 elric return ret;
363 1.1 elric }
364 1.1 elric
365 1.1 elric static int
366 1.1 elric configure_stdin(struct params *p, int argc, char **argv)
367 1.1 elric {
368 1.2 elric int fd;
369 1.1 elric int ret;
370 1.2 elric char cgdname[PATH_MAX];
371 1.1 elric
372 1.1 elric if (argc < 3 || argc > 4)
373 1.1 elric usage();
374 1.1 elric
375 1.1 elric ret = params_setalgorithm(p, argv[2]);
376 1.1 elric if (ret)
377 1.1 elric return ret;
378 1.1 elric if (argc > 3) {
379 1.1 elric ret = params_setkeylen(p, atoi(argv[3]));
380 1.1 elric if (ret)
381 1.1 elric return ret;
382 1.1 elric }
383 1.1 elric
384 1.1 elric ret = params_filldefaults(p);
385 1.1 elric if (ret)
386 1.1 elric return ret;
387 1.1 elric
388 1.2 elric fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
389 1.2 elric if (fd == -1)
390 1.2 elric return -1;
391 1.2 elric
392 1.1 elric ret = getkeyfromfile(stdin, p);
393 1.1 elric if (ret)
394 1.1 elric return -1;
395 1.1 elric
396 1.2 elric return configure_params(fd, cgdname, argv[1], p);
397 1.1 elric }
398 1.1 elric
399 1.1 elric static int
400 1.2 elric opendisk_werror(const char *cgd, char *buf, int buflen)
401 1.2 elric {
402 1.2 elric int fd;
403 1.2 elric
404 1.2 elric /* sanity */
405 1.2 elric if (!cgd || !buf)
406 1.2 elric return -1;
407 1.2 elric
408 1.2 elric if (nflag) {
409 1.2 elric strncpy(buf, cgd, buflen);
410 1.2 elric return 0;
411 1.2 elric }
412 1.2 elric
413 1.2 elric fd = opendisk(cgd, O_RDWR, buf, buflen, 1);
414 1.2 elric if (fd == -1)
415 1.2 elric fprintf(stderr, "can't open cgd \"%s\", \"%s\": %s\n",
416 1.2 elric cgd, buf, strerror(errno));
417 1.2 elric return fd;
418 1.2 elric }
419 1.2 elric
420 1.2 elric static int
421 1.2 elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
422 1.1 elric {
423 1.1 elric struct cgd_ioctl ci;
424 1.1 elric int ret;
425 1.1 elric
426 1.1 elric /* sanity */
427 1.1 elric if (!cgd || !dev)
428 1.1 elric return -1;
429 1.1 elric
430 1.1 elric memset(&ci, 0x0, sizeof(ci));
431 1.1 elric ci.ci_disk = (char *)dev;
432 1.1 elric ci.ci_alg = p->alg;
433 1.1 elric ci.ci_ivmethod = p->ivmeth;
434 1.1 elric ci.ci_key = p->key;
435 1.1 elric ci.ci_keylen = p->keylen;
436 1.1 elric ci.ci_blocksize = p->bsize;
437 1.1 elric
438 1.2 elric VPRINTF(1, ("attaching: %s attach to %s\n", cgd, dev));
439 1.1 elric VPRINTF(1, (" with alg %s keylen %d blocksize %d ivmethod %s\n",
440 1.1 elric p->alg, p->keylen, p->bsize, p->ivmeth));
441 1.1 elric VERBOSE(2, key_print(stdout, p->key, p->keylen));
442 1.1 elric
443 1.1 elric if (nflag)
444 1.1 elric return 0;
445 1.1 elric
446 1.1 elric ret = ioctl(fd, CGDIOCSET, &ci);
447 1.1 elric if (ret == -1) {
448 1.1 elric perror("ioctl");
449 1.1 elric return errno;
450 1.1 elric }
451 1.1 elric
452 1.1 elric return 0;
453 1.1 elric }
454 1.1 elric
455 1.1 elric static int
456 1.1 elric generate(struct params *p, int argc, char **argv, const char *outfile)
457 1.1 elric {
458 1.1 elric FILE *f;
459 1.1 elric int ret;
460 1.1 elric char *tmp;
461 1.1 elric
462 1.1 elric if (argc < 1 || argc > 2)
463 1.1 elric usage();
464 1.1 elric
465 1.1 elric ret = params_setalgorithm(p, argv[0]);
466 1.1 elric if (ret)
467 1.1 elric return ret;
468 1.1 elric if (argc > 1) {
469 1.1 elric ret = params_setkeylen(p, atoi(argv[1]));
470 1.1 elric if (ret)
471 1.1 elric return ret;
472 1.1 elric }
473 1.1 elric
474 1.1 elric ret = params_filldefaults(p);
475 1.1 elric if (ret)
476 1.1 elric return ret;
477 1.1 elric
478 1.1 elric if (!p->keygen_method != KEYGEN_RANDOMKEY) {
479 1.1 elric tmp = getrandbits(DEFAULT_SALTLEN);
480 1.1 elric params_setkeygen_salt(p, tmp, DEFAULT_SALTLEN);
481 1.1 elric free(tmp);
482 1.1 elric tmp = getrandbits(p->keylen);
483 1.1 elric params_setxor_key(p, tmp, p->keylen);
484 1.1 elric free(tmp);
485 1.1 elric
486 1.1 elric /* XXXrcd: generate key hash, if desired */
487 1.1 elric }
488 1.1 elric
489 1.1 elric if (*outfile) {
490 1.1 elric f = fopen(outfile, "w");
491 1.1 elric if (!f) {
492 1.1 elric fprintf(stderr, "could not open outfile \"%s\": %s\n",
493 1.1 elric outfile, strerror(errno));
494 1.1 elric perror("fopen");
495 1.1 elric return -1;
496 1.1 elric }
497 1.1 elric } else {
498 1.1 elric f = stdout;
499 1.1 elric }
500 1.1 elric
501 1.1 elric ret = params_fput(p, f);
502 1.1 elric params_free(p);
503 1.1 elric return ret;
504 1.1 elric }
505 1.1 elric
506 1.1 elric static int
507 1.1 elric do_all(const char *cfile, int argc, char **argv,
508 1.1 elric int (*conf)(int, char **, int))
509 1.1 elric {
510 1.1 elric FILE *f;
511 1.1 elric size_t len;
512 1.1 elric size_t lineno;
513 1.1 elric int my_argc;
514 1.1 elric int ret;
515 1.1 elric const char *fn;
516 1.1 elric char *line;
517 1.1 elric char **my_argv;
518 1.1 elric
519 1.1 elric if (argc > 0)
520 1.1 elric usage();
521 1.1 elric
522 1.1 elric if (!cfile[0])
523 1.1 elric fn = CGDCONFIG_CFILE;
524 1.1 elric else
525 1.1 elric fn = cfile;
526 1.1 elric
527 1.1 elric f = fopen(fn, "r");
528 1.1 elric if (!f) {
529 1.1 elric fprintf(stderr, "could not open config file \"%s\": %s\n",
530 1.1 elric fn, strerror(errno));
531 1.1 elric return -1;
532 1.1 elric }
533 1.1 elric
534 1.1 elric ret = 0;
535 1.1 elric lineno = 0;
536 1.1 elric for (;;) {
537 1.1 elric
538 1.1 elric line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
539 1.1 elric if (!line)
540 1.1 elric break;
541 1.1 elric if (!*line)
542 1.1 elric continue;
543 1.1 elric
544 1.1 elric my_argv = words(line, &my_argc);
545 1.1 elric ret = conf(my_argc, my_argv, CONFIG_FLAGS_FROMALL);
546 1.1 elric if (ret) {
547 1.1 elric fprintf(stderr, "on \"%s\" line %lu\n", fn,
548 1.1 elric (u_long)lineno);
549 1.1 elric break;
550 1.1 elric }
551 1.1 elric words_free(my_argv, my_argc);
552 1.1 elric }
553 1.1 elric return ret;
554 1.1 elric }
555 1.1 elric
556 1.1 elric /*
557 1.1 elric * XXX: key_print doesn't work quite exactly properly if the keylength
558 1.1 elric * is not evenly divisible by 8. If the key is not divisible by
559 1.1 elric * 8 then a few extra bits are printed.
560 1.1 elric */
561 1.1 elric
562 1.1 elric static void
563 1.1 elric key_print(FILE *f, const u_int8_t *key, int len)
564 1.1 elric {
565 1.1 elric int i;
566 1.1 elric int col;
567 1.1 elric
568 1.1 elric len = BITS2BYTES(len);
569 1.1 elric fprintf(f, "key: ");
570 1.1 elric for (i=0, col=5; i < len; i++, col+=2) {
571 1.1 elric fprintf(f, "%02x", key[i]);
572 1.1 elric if (col > 70) {
573 1.1 elric col = 5 - 2;
574 1.1 elric fprintf(f, "\n ");
575 1.1 elric }
576 1.1 elric }
577 1.1 elric fprintf(f, "\n");
578 1.1 elric }
579 1.1 elric
580 1.1 elric static char *
581 1.1 elric getrandbits(int len)
582 1.1 elric {
583 1.1 elric FILE *f;
584 1.1 elric int ret;
585 1.1 elric char *res;
586 1.1 elric
587 1.1 elric len = (len + 7) / 8;
588 1.1 elric res = malloc(len);
589 1.1 elric if (!res)
590 1.1 elric return NULL;
591 1.1 elric f = fopen("/dev/random", "r");
592 1.1 elric if (!f)
593 1.1 elric return NULL;
594 1.1 elric ret = fread(res, len, 1, f);
595 1.1 elric if (ret != 1) {
596 1.1 elric free(res);
597 1.1 elric return NULL;
598 1.1 elric }
599 1.1 elric return res;
600 1.1 elric }
601