catcrypt.c: Don't close output file twice.
[u/mdw/catacomb] / catcrypt.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Command-line encryption tool
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include "config.h"
33
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <mLib/base64.h>
40 #include <mLib/dstr.h>
41 #include <mLib/mdwopt.h>
42 #include <mLib/quis.h>
43 #include <mLib/report.h>
44 #include <mLib/sub.h>
45
46 #include "buf.h"
47 #include "rand.h"
48 #include "noise.h"
49 #include "mprand.h"
50 #include "key.h"
51 #include "cc.h"
52
53 #include "ectab.h"
54 #include "ptab.h"
55
56 /*----- Static variables --------------------------------------------------*/
57
58 static const char *keyring = "keyring";
59
60 /*----- Data format -------------------------------------------------------*/
61
62 /* --- Overview --- *
63 *
64 * The encrypted message is divided into chunks, each preceded by a two-octet
65 * length. The chunks don't need to be large -- the idea is that we can
66 * stream the chunks in and out.
67 *
68 * The first chunk is a header. It contains the decryption key-id, and maybe
69 * the verification key-id if the message is signed.
70 *
71 * Next comes the key-encapsulation chunk. This is decrypted in some
72 * KEM-specific way to yield a secret hash. The hash is expanded using an
73 * MGF (or similar) to make a symmetric encryption and MAC key.
74 *
75 * If the message is signed, there comes a signature chunk. The signature is
76 * on the header and key-encapsulation chunks, and further output of the MGF.
77 * This means that the recipient can modify the message and still have a
78 * valid signature, so it's not useful for proving things to other people;
79 * but it also means that the recipient knows that the message is from
80 * someone who knows the hash, which limits the possiblities to (a) whoever
81 * encrypted the message (good!) and (b) whoever knows the recipient's
82 * private key.
83 *
84 * Then come message chunks. Each one begins with a MAC over an implicit
85 * sequence number and the ciphertext. The final chunk's ciphertext is
86 * empty; no other chunk is empty. Thus can the correct end-of-file be
87 * discerned.
88 */
89
90 /*----- Chunk I/O ---------------------------------------------------------*/
91
92 static void chunk_write(enc *e, buf *b)
93 {
94 octet l[2];
95 size_t n = BLEN(b);
96 assert(n <= MASK16);
97 STORE16(l, n);
98 if (e->ops->write(e, l, 2) ||
99 e->ops->write(e, BBASE(b), BLEN(b)))
100 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
101 }
102
103 static void chunk_read(enc *e, dstr *d, buf *b)
104 {
105 octet l[2];
106 size_t n;
107
108 dstr_reset(d);
109 errno = 0;
110 if (e->ops->read(e, l, 2) != 2)
111 goto err;
112 n = LOAD16(l);
113 dstr_ensure(d, n);
114 if (e->ops->read(e, d->buf, n) != n)
115 goto err;
116 d->len = n;
117 buf_init(b, d->buf, d->len);
118 return;
119
120 err:
121 if (!errno) die(EXIT_FAILURE, "unexpected end-of-file on input");
122 else die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
123 }
124
125 /*----- Encryption --------------------------------------------------------*/
126
127 static int encrypt(int argc, char *argv[])
128 {
129 const char *of = 0, *kn = "ccrypt", *skn = 0;
130 FILE *ofp = 0;
131 FILE *fp = 0;
132 const char *ef = "binary";
133 const char *err;
134 int i;
135 int en;
136 size_t n, chsz;
137 dstr d = DSTR_INIT;
138 octet *tag, *ct;
139 buf b;
140 size_t seq;
141 char bb[65536];
142 unsigned f = 0;
143 key_file kf;
144 key *k;
145 key *sk = 0;
146 kem *km;
147 sig *s = 0;
148 gcipher *cx, *c;
149 gmac *m;
150 ghash *h;
151 const encops *eo;
152 enc *e;
153
154 #define f_bogus 1u
155 #define f_nocheck 2u
156
157 for (;;) {
158 static const struct option opt[] = {
159 { "key", OPTF_ARGREQ, 0, 'k' },
160 { "sign-key", OPTF_ARGREQ, 0, 's' },
161 { "armour", 0, 0, 'a' },
162 { "armor", 0, 0, 'a' },
163 { "format", OPTF_ARGREQ, 0, 'f' },
164 { "output", OPTF_ARGREQ, 0, 'o' },
165 { "nocheck", 0, 0, 'C' },
166 { 0, 0, 0, 0 }
167 };
168 i = mdwopt(argc, argv, "k:s:af:o:C", opt, 0, 0, 0);
169 if (i < 0) break;
170 switch (i) {
171 case 'k': kn = optarg; break;
172 case 's': skn = optarg; break;
173 case 'a': ef = "pem"; break;
174 case 'f': ef = optarg; break;
175 case 'o': of = optarg; break;
176 case 'C': f |= f_nocheck; break;
177 default: f |= f_bogus; break;
178 }
179 }
180 if (argc - optind > 1 || (f & f_bogus))
181 die(EXIT_FAILURE, "Usage: encrypt [-OPTIONS] [FILE]");
182
183 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
184 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
185 if ((k = key_bytag(&kf, kn)) == 0)
186 die(EXIT_FAILURE, "key `%s' not found", kn);
187 if (skn && (sk = key_bytag(&kf, skn)) == 0)
188 die(EXIT_FAILURE, "key `%s' not found", skn);
189
190 if ((eo = getenc(ef)) == 0)
191 die(EXIT_FAILURE, "encoding `%s' not found", ef);
192
193 if (optind == argc)
194 fp = stdin;
195 else if (strcmp(argv[optind], "-") == 0) {
196 fp = stdin;
197 optind++;
198 } else if ((fp = fopen(argv[optind], "rb")) == 0) {
199 die(EXIT_FAILURE, "couldn't open file `%s': %s",
200 argv[optind], strerror(errno));
201 } else
202 optind++;
203
204 if (!of || strcmp(of, "-") == 0)
205 ofp = stdout;
206 else if ((ofp = fopen(of, eo->wmode)) == 0) {
207 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
208 ofp, strerror(errno));
209 }
210
211 dstr_reset(&d);
212 key_fulltag(k, &d);
213 e = initenc(eo, ofp, "CATCRYPT ENCRYPTED MESSAGE");
214 km = getkem(k, "cckem", 0);
215 if (!(f & f_nocheck) && (err = km->ops->check(km)) != 0)
216 moan("key %s fails check: %s", d.buf, err);
217 if (sk) {
218 dstr_reset(&d);
219 key_fulltag(sk, &d);
220 s = getsig(sk, "ccsig", 1);
221 if ((err = s->ops->check(s)) != 0)
222 moan("key %s fails check: %s", d.buf, err);
223 }
224
225 /* --- Build the header chunk --- */
226
227 dstr_reset(&d);
228 dstr_ensure(&d, 256);
229 buf_init(&b, d.buf, 256);
230 buf_putu32(&b, k->id);
231 if (sk) buf_putu32(&b, sk->id);
232 assert(BOK(&b));
233 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
234 chunk_write(e, &b);
235
236 /* --- Build the KEM chunk --- */
237
238 dstr_reset(&d);
239 if (setupkem(km, &d, &cx, &c, &m))
240 die(EXIT_FAILURE, "failed to encapsulate key");
241 buf_init(&b, d.buf, d.len);
242 BSTEP(&b, d.len);
243 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
244 chunk_write(e, &b);
245
246 /* --- Write the signature chunk --- */
247
248 if (s) {
249 GC_ENCRYPT(cx, 0, bb, 1024);
250 GH_HASH(s->h, bb, 1024);
251 dstr_reset(&d);
252 if ((en = s->ops->doit(s, &d)) != 0)
253 die(EXIT_FAILURE, "error creating signature: %s", key_strerror(en));
254 buf_init(&b, d.buf, d.len);
255 BSTEP(&b, d.len);
256 chunk_write(e, &b);
257 }
258
259 /* --- Now do the main crypto --- */
260
261 assert(GC_CLASS(c)->blksz <= sizeof(bb));
262 dstr_ensure(&d, sizeof(bb) + GM_CLASS(m)->hashsz);
263 seq = 0;
264 chsz = MASK16 - GM_CLASS(m)->hashsz;
265 for (;;) {
266 h = GM_INIT(m);
267 GH_HASHU32(h, seq);
268 seq++;
269 if (GC_CLASS(c)->blksz) {
270 GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz);
271 GC_SETIV(c, bb);
272 }
273 n = fread(bb, 1, chsz, fp);
274 if (!n) break;
275 buf_init(&b, d.buf, d.sz);
276 tag = buf_get(&b, GM_CLASS(m)->hashsz);
277 ct = buf_get(&b, n);
278 assert(tag); assert(ct);
279 GC_ENCRYPT(c, bb, ct, n);
280 GH_HASH(h, ct, n);
281 GH_DONE(h, tag);
282 GH_DESTROY(h);
283 chunk_write(e, &b);
284 }
285
286 /* --- Final terminator packet --- */
287
288 buf_init(&b, d.buf, d.sz);
289 tag = buf_get(&b, GM_CLASS(m)->hashsz);
290 assert(tag);
291 GH_DONE(h, tag);
292 GH_DESTROY(h);
293 chunk_write(e, &b);
294
295 /* --- All done --- */
296
297 e->ops->encdone(e);
298 GM_DESTROY(m);
299 GC_DESTROY(c);
300 GC_DESTROY(cx);
301 freeenc(e);
302 if (s) freesig(s);
303 freekem(km);
304 if (fp != stdin) fclose(fp);
305 if (of) fclose(ofp);
306 key_close(&kf);
307 dstr_destroy(&d);
308 return (0);
309
310 #undef f_bogus
311 #undef f_nocheck
312 }
313
314 /*---- Decryption ---------------------------------------------------------*/
315
316 static int decrypt(int argc, char *argv[])
317 {
318 const char *of = 0;
319 FILE *ofp = 0, *rfp = 0;
320 FILE *fp = 0;
321 const char *ef = "binary";
322 int i;
323 size_t n;
324 dstr d = DSTR_INIT;
325 buf b;
326 key_file kf;
327 size_t seq;
328 uint32 id;
329 key *k;
330 key *sk = 0;
331 kem *km;
332 sig *s = 0;
333 gcipher *cx;
334 gcipher *c;
335 ghash *h;
336 gmac *m;
337 octet *tag;
338 unsigned f = 0;
339 const encops *eo;
340 const char *err;
341 int verb = 1;
342 enc *e;
343
344 #define f_bogus 1u
345 #define f_buffer 2u
346 #define f_nocheck 4u
347
348 for (;;) {
349 static const struct option opt[] = {
350 { "armour", 0, 0, 'a' },
351 { "armor", 0, 0, 'a' },
352 { "buffer", 0, 0, 'b' },
353 { "verbose", 0, 0, 'v' },
354 { "quiet", 0, 0, 'q' },
355 { "nocheck", 0, 0, 'C' },
356 { "format", OPTF_ARGREQ, 0, 'f' },
357 { "output", OPTF_ARGREQ, 0, 'o' },
358 { 0, 0, 0, 0 }
359 };
360 i = mdwopt(argc, argv, "abf:o:qvC", opt, 0, 0, 0);
361 if (i < 0) break;
362 switch (i) {
363 case 'a': ef = "pem"; break;
364 case 'b': f |= f_buffer; break;
365 case 'v': verb++; break;
366 case 'q': if (verb) verb--; break;
367 case 'C': f |= f_nocheck; break;
368 case 'f': ef = optarg; break;
369 case 'o': of = optarg; break;
370 default: f |= f_bogus; break;
371 }
372 }
373 if (argc - optind > 1 || (f & f_bogus))
374 die(EXIT_FAILURE, "Usage: decrypt [-OPTIONS] [FILE]");
375
376 if ((eo = getenc(ef)) == 0)
377 die(EXIT_FAILURE, "encoding `%s' not found", ef);
378
379 if (optind == argc)
380 fp = stdin;
381 else if (strcmp(argv[optind], "-") == 0) {
382 fp = stdin;
383 optind++;
384 } else if ((fp = fopen(argv[optind], eo->rmode)) == 0) {
385 die(EXIT_FAILURE, "couldn't open file `%s': %s",
386 argv[optind], strerror(errno));
387 } else
388 optind++;
389
390 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
391 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
392
393 e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE");
394
395 /* --- Read the header chunk --- */
396
397 chunk_read(e, &d, &b);
398 if (buf_getu32(&b, &id)) {
399 if (verb) printf("FAIL malformed header: missing keyid\n");
400 exit(EXIT_FAILURE);
401 }
402 if ((k = key_byid(&kf, id)) == 0) {
403 if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id);
404 exit(EXIT_FAILURE);
405 }
406 if (BLEFT(&b)) {
407 if (buf_getu32(&b, &id)) {
408 if (verb) printf("FAIL malformed header: missing signature keyid\n");
409 exit(EXIT_FAILURE);
410 }
411 if ((sk = key_byid(&kf, id)) == 0) {
412 if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id);
413 exit(EXIT_FAILURE);
414 }
415 }
416 if (BLEFT(&b)) {
417 if (verb) printf("FAIL malformed header: junk at end\n");
418 exit(EXIT_FAILURE);
419 }
420 if (sk) {
421 s = getsig(sk, "ccsig", 0);
422 if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0) {
423 dstr_reset(&d);
424 key_fulltag(sk, &d);
425 printf("WARN verification key %s fails check: %s\n", d.buf, err);
426 }
427 GH_HASHBUF16(s->h, BBASE(&b), BSZ(&b));
428 }
429
430 /* --- Find the key --- */
431
432 km = getkem(k, "cckem", 1);
433
434 /* --- Read the KEM chunk --- */
435
436 chunk_read(e, &d, &b);
437 if (setupkem(km, &d, &cx, &c, &m)) {
438 if (verb) printf("FAIL failed to decapsulate key\n");
439 exit(EXIT_FAILURE);
440 }
441 if (s) GH_HASHBUF16(s->h, d.buf, d.len);
442
443 /* --- Verify the signature, if there is one --- */
444
445 if (sk) {
446 dstr_reset(&d);
447 dstr_ensure(&d, 1024);
448 GC_ENCRYPT(cx, 0, d.buf, 1024);
449 GH_HASH(s->h, d.buf, 1024);
450 chunk_read(e, &d, &b);
451 if (s->ops->doit(s, &d)) {
452 if (verb) printf("FAIL signature verification failed\n");
453 exit(EXIT_FAILURE);
454 }
455 if (verb) {
456 dstr_reset(&d);
457 key_fulltag(sk, &d);
458 printf("INFO good-signature %s\n", d.buf);
459 }
460 freesig(s);
461 } else if (verb)
462 printf("INFO no-signature\n");
463
464 /* --- Now decrypt the main body --- */
465
466 if (!of || strcmp(of, "-") == 0) {
467 ofp = stdout;
468 f |= f_buffer;
469 }
470 if (!(f & f_buffer)) {
471 if ((ofp = fopen(of, "wb")) == 0) {
472 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
473 ofp, strerror(errno));
474 }
475 rfp = ofp;
476 } else if ((rfp = tmpfile()) == 0)
477 die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno));
478
479 seq = 0;
480 dstr_ensure(&d, GC_CLASS(c)->blksz);
481 dstr_ensure(&d, 4);
482 for (;;) {
483 if (GC_CLASS(c)->blksz) {
484 GC_ENCRYPT(cx, 0, d.buf, GC_CLASS(c)->blksz);
485 GC_SETIV(c, d.buf);
486 }
487 h = GM_INIT(m);
488 GH_HASHU32(h, seq);
489 seq++;
490 chunk_read(e, &d, &b);
491 if ((tag = buf_get(&b, GM_CLASS(m)->hashsz)) == 0) {
492 if (verb) printf("FAIL bad ciphertext chunk: no tag\n");
493 exit(EXIT_FAILURE);
494 }
495 GH_HASH(h, BCUR(&b), BLEFT(&b));
496 if (memcmp(tag, GH_DONE(h, 0), GM_CLASS(m)->hashsz) != 0) {
497 if (verb)
498 printf("FAIL bad ciphertext chunk: authentication failure\n");
499 exit(EXIT_FAILURE);
500 }
501 GH_DESTROY(h);
502 if (!BLEFT(&b))
503 break;
504 GC_DECRYPT(c, BCUR(&b), BCUR(&b), BLEFT(&b));
505 if (fwrite(BCUR(&b), 1, BLEFT(&b), rfp) != BLEFT(&b)) {
506 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
507 exit(EXIT_FAILURE);
508 }
509 }
510
511 if (fflush(rfp) || ferror(rfp)) {
512 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
513 exit(EXIT_FAILURE);
514 }
515 if (f & f_buffer) {
516 if (!ofp && (ofp = fopen(of, "wb")) == 0) {
517 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
518 of, strerror(errno));
519 }
520 rewind(rfp);
521 dstr_reset(&d);
522 dstr_ensure(&d, 65536);
523 if (verb && ofp == stdout) printf("DATA\n");
524 for (;;) {
525 n = fread(d.buf, 1, d.sz, rfp);
526 if (!n) break;
527 if (fwrite(d.buf, 1, n, ofp) < n)
528 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
529 }
530 if (ferror(rfp) || fclose(rfp))
531 die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno));
532 }
533
534 e->ops->decdone(e);
535 if (verb && ofp != stdout)
536 printf("OK decrypted successfully\n");
537 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
538 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
539 freeenc(e);
540 GC_DESTROY(c);
541 GC_DESTROY(cx);
542 GM_DESTROY(m);
543 freekem(km);
544 if (fp != stdin) fclose(fp);
545 key_close(&kf);
546 dstr_destroy(&d);
547 return (0);
548
549 #undef f_bogus
550 #undef f_buffer
551 #undef f_nocheck
552 }
553
554 /*----- Main code ---------------------------------------------------------*/
555
556 #define LISTS(LI) \
557 LI("Lists", list, \
558 listtab[i].name, listtab[i].name) \
559 LI("Key-encapsulation mechanisms", kem, \
560 kemtab[i].name, kemtab[i].name) \
561 LI("Signature schemes", sig, \
562 sigtab[i].name, sigtab[i].name) \
563 LI("Encodings", enc, \
564 enctab[i].name, enctab[i].name) \
565 LI("Symmetric encryption algorithms", cipher, \
566 gciphertab[i], gciphertab[i]->name) \
567 LI("Hash functions", hash, \
568 ghashtab[i], ghashtab[i]->name) \
569 LI("Message authentication codes", mac, \
570 gmactab[i], gmactab[i]->name)
571
572 MAKELISTTAB(listtab, LISTS)
573
574 int cmd_show(int argc, char *argv[])
575 {
576 return (displaylists(listtab, argv + 1));
577 }
578
579 static int cmd_help(int, char **);
580
581 static cmd cmdtab[] = {
582 { "help", cmd_help, "help [COMMAND...]" },
583 { "show", cmd_show, "show [ITEM...]" },
584 CMD_ENCODE,
585 CMD_DECODE,
586 { "encrypt", encrypt,
587 "encrypt [-aC] [-k TAG] [-s TAG] [-f FORMAT]\n\t\
588 [-o OUTPUT] [FILE]", "\
589 Options:\n\
590 \n\
591 -a, --armour Same as `-f pem'.\n\
592 -f, --format=FORMAT Encode as FORMAT.\n\
593 -k, --key=TAG Use public encryption key named by TAG.\n\
594 -s, --sign-key=TAG Use private signature key named by TAG.\n\
595 -o, --output=FILE Write output to FILE.\n\
596 -C, --nocheck Don't check the public key.\n\
597 " },
598 { "decrypt", decrypt,
599 "decrypt [-abqvC] [-f FORMAT] [-o OUTPUT] [FILE]", "\
600 Options:\n\
601 \n\
602 -a, --armour Same as `-f pem'.\n\
603 -b, --buffer Buffer output until we're sure we have it all.\n\
604 -f, --format=FORMAT Decode as FORMAT.\n\
605 -o, --output=FILE Write output to FILE.\n\
606 -q, --quiet Produce fewer messages.\n\
607 -v, --verbose Produce more verbose messages.\n\
608 -C, --nocheck Don't check the private key.\n\
609 " }, /* ' emacs is confused */
610 { 0, 0, 0 }
611 };
612
613 static int cmd_help(int argc, char **argv)
614 {
615 sc_help(cmdtab, stdout, argv + 1);
616 return (0);
617 }
618
619 void version(FILE *fp)
620 {
621 pquis(fp, "$, Catacomb version " VERSION "\n");
622 }
623
624 static void usage(FILE *fp)
625 {
626 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
627 }
628
629 void help_global(FILE *fp)
630 {
631 usage(fp);
632 fputs("\n\
633 Encrypt and decrypt files.\n\
634 \n\
635 Global command-line options:\n\
636 \n\
637 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
638 -v, --version Show program version number.\n\
639 -u, --usage Show a terse usage message.\n\
640 \n\
641 -k, --keyring=FILE Read keys from FILE.\n",
642 fp);
643 }
644
645 /* --- @main@ --- *
646 *
647 * Arguments: @int argc@ = number of command line arguments
648 * @char *argv[]@ = vector of command line arguments
649 *
650 * Returns: Zero if successful, nonzero otherwise.
651 *
652 * Use: Encrypts or decrypts files.
653 */
654
655 int main(int argc, char *argv[])
656 {
657 unsigned f = 0;
658
659 #define f_bogus 1u
660
661 /* --- Initialize the library --- */
662
663 ego(argv[0]);
664 sub_init();
665 rand_noisesrc(RAND_GLOBAL, &noise_source);
666 rand_seed(RAND_GLOBAL, 160);
667
668 /* --- Parse options --- */
669
670 for (;;) {
671 static struct option opts[] = {
672 { "help", 0, 0, 'h' },
673 { "version", 0, 0, 'v' },
674 { "usage", 0, 0, 'u' },
675 { "keyring", OPTF_ARGREQ, 0, 'k' },
676 { 0, 0, 0, 0 }
677 };
678 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
679 if (i < 0)
680 break;
681 switch (i) {
682 case 'h':
683 sc_help(cmdtab, stdout, argv + optind);
684 exit(0);
685 break;
686 case 'v':
687 version(stdout);
688 exit(0);
689 break;
690 case 'u':
691 usage(stdout);
692 exit(0);
693 case 'k':
694 keyring = optarg;
695 break;
696 default:
697 f |= f_bogus;
698 break;
699 }
700 }
701
702 argc -= optind;
703 argv += optind;
704 optind = 0;
705 if (f & f_bogus || argc < 1) {
706 usage(stderr);
707 exit(EXIT_FAILURE);
708 }
709
710 /* --- Dispatch to the correct subcommand handler --- */
711
712 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
713
714 #undef f_bogus
715 }
716
717 /*----- That's all, folks -------------------------------------------------*/