X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/252c122db9653732c758e08c41bedd08096f0749..283b9af095a5b24ae71b49a6d2dcbdcdaae47c40:/keyutil.c diff --git a/keyutil.c b/keyutil.c index b6b1bf9..3f3218e 100644 --- a/keyutil.c +++ b/keyutil.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: keyutil.c,v 1.4 1999/12/22 15:48:10 mdw Exp $ + * $Id: keyutil.c,v 1.14 2001/02/23 09:03:27 mdw Exp $ * * Simple key manager program * @@ -30,6 +30,40 @@ /*----- Revision history --------------------------------------------------* * * $Log: keyutil.c,v $ + * Revision 1.14 2001/02/23 09:03:27 mdw + * Simplify usage message by removing nonexistant options. + * + * Revision 1.13 2001/02/21 20:04:27 mdw + * Provide help on individual commands (some need it desparately). Allow + * atomic retagging of keys. + * + * Revision 1.12 2001/02/03 11:58:22 mdw + * Store the correct seed information and count for DSA keys now that it's + * available. + * + * Revision 1.11 2000/12/06 20:33:27 mdw + * Make flags be macros rather than enumerations, to ensure that they're + * unsigned. + * + * Revision 1.10 2000/10/08 12:02:21 mdw + * Use @MP_EQ@ instead of @MP_CMP@. + * + * Revision 1.9 2000/08/15 21:40:49 mdw + * Minor formatting change in listing attributes. + * + * Revision 1.8 2000/07/29 09:59:13 mdw + * Support Lim-Lee primes in Diffie-Hellman parameter generation. + * + * Revision 1.7 2000/07/01 11:18:51 mdw + * Use new interfaces for key manipulation. + * + * Revision 1.6 2000/06/17 11:28:22 mdw + * Use secure memory interface from MP library. `rand_getgood' is + * deprecated. + * + * Revision 1.5 2000/02/12 18:21:03 mdw + * Overhaul of key management (again). + * * Revision 1.4 1999/12/22 15:48:10 mdw * Track new key-management changes. Support new key generation * algorithms. @@ -66,6 +100,7 @@ #include #include "bbs.h" +#include "dh.h" #include "dsa.h" #include "fibrand.h" #include "getdate.h" @@ -97,7 +132,7 @@ static const char *keyfile = "keyring"; static void doopen(key_file *f, unsigned how) { if (key_open(f, keyfile, how, key_moan, 0)) - die(1, "couldn't open file `%s': %s", keyfile, strerror(errno)); + die(1, "couldn't open keyring `%s': %s", keyfile, strerror(errno)); } /* --- @doclose@ --- * @@ -162,11 +197,12 @@ typedef struct keyopts { key *p; /* Parameters key-data */ } keyopts; -enum { - f_bogus = 1, /* Error in parsing */ - f_lock = 2, /* Passphrase-lock private key */ - f_quiet = 4 /* Don't show a progress indicator */ -}; +#define f_bogus 1u /* Error in parsing */ +#define f_lock 2u /* Passphrase-lock private key */ +#define f_quiet 4u /* Don't show a progress indicator */ +#define f_limlee 8u /* Generate Lim-Lee primes */ +#define f_subgroup 16u /* Generate a subgroup */ +#define f_retag 32u /* Remove any existing tag */ /* --- @dolock@ --- * * @@ -268,6 +304,58 @@ static mp *getmp(key_data *k, const char *tag) return (k->u.m); } +/* --- @keyrand@ --- * + * + * Arguments: @key_file *kf@ = pointer to key file + * @const char *id@ = pointer to key id (or null) + * + * Returns: --- + * + * Use: Keys the random number generator. + */ + +static void keyrand(key_file *kf, const char *id) +{ + key *k; + + /* --- Find the key --- */ + + if (id) { + if ((k = key_bytag(kf, id)) == 0) + die(EXIT_FAILURE, "key `%s' not found", id); + } else + k = key_bytype(kf, "catacomb-rand"); + + if (k) { + key_data *kd = &k->k, kkd; + + again: + switch (kd->e & KF_ENCMASK) { + case KENC_BINARY: + break; + case KENC_ENCRYPT: { + dstr d = DSTR_INIT; + key_fulltag(k, &d); + if (key_punlock(d.buf, kd, &kkd)) + die(EXIT_FAILURE, "error unlocking key `%s'", d.buf); + dstr_destroy(&d); + kd = &kkd; + } goto again; + default: { + dstr d = DSTR_INIT; + key_fulltag(k, &d); + die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf); + } break; + } + + /* --- Key the generator --- */ + + rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz); + if (kd == &kkd) + key_destroy(&kkd); + } +} + /* --- Key generation algorithms --- */ static void alg_binary(keyopts *k) @@ -284,7 +372,7 @@ static void alg_binary(keyopts *k) sz = (k->bits + 7) >> 3; p = sub_alloc(sz); m = (1 << (((k->bits - 1) & 7) + 1)) - 1; - rand_getgood(RAND_GLOBAL, p, sz); + rand_get(RAND_GLOBAL, p, sz); *p &= m; key_binary(&k->k->k, p, sz); k->k->k.e |= KCAT_SYMM | KF_BURN; @@ -308,12 +396,12 @@ static void alg_des(keyopts *k) sz = k->bits / 7; p = sub_alloc(sz); - rand_getgood(RAND_GLOBAL, p, sz); /* Too much work done here! */ + rand_get(RAND_GLOBAL, p, sz); /* Too much work done here! */ for (i = 0; i < sz; i++) { - octet x = p[i] & 0xfe; + octet x = p[i] | 0x01; x = x ^ (x >> 4); x = x ^ (x >> 2); - x = x ^ (x >> 1) ^ 1; + x = x ^ (x >> 1); p[i] = (p[i] & 0xfe) | (x & 0x01); } key_binary(&k->k->k, p, sz); @@ -325,7 +413,7 @@ static void alg_des(keyopts *k) static void alg_rsa(keyopts *k) { - rsa_param rp; + rsa_priv rp; key_data *kd; /* --- Sanity checking --- */ @@ -345,23 +433,16 @@ static void alg_rsa(keyopts *k) { grand *g = fibrand_create(rand_global.ops->word(&rand_global)); - mpmont mm; + rsa_pub rpp; mp *m = mprand_range(MP_NEW, rp.n, g, 0); mp *c; - /* --- Encrypt the plaintext --- */ - - mpmont_create(&mm, rp.n); - c = mpmont_exp(&mm, MP_NEW, m, rp.e); - mpmont_destroy(&mm); - - /* --- Decrypt the ciphertext --- */ + rpp.n = rp.n; + rpp.e = rp.e; + c = rsa_qpubop(&rpp, MP_NEW, m); + c = rsa_qprivop(&rp, c, c, g); - c = rsa_decrypt(&rp, c, c, g); - - /* --- Check everything went OK --- */ - - if (MP_CMP(c, !=, m)) + if (!MP_EQ(c, m)) die(EXIT_FAILURE, "test encryption failed"); mp_drop(c); mp_drop(m); @@ -385,8 +466,7 @@ static void alg_rsa(keyopts *k) mpkey(kd, "d-mod-q", rp.dq, KCAT_PRIV | KF_BURN); dolock(k, kd, "private"); - mp_drop(rp.p); mp_drop(rp.q); mp_drop(rp.n); mp_drop(rp.q_inv); - mp_drop(rp.e); mp_drop(rp.d); mp_drop(rp.dp); mp_drop(rp.dq); + rsa_privfree(&rp); } static void alg_dsaparam(keyopts *k) @@ -399,6 +479,7 @@ static void alg_dsaparam(keyopts *k) dstr d = DSTR_INIT; base64_ctx c; key_data *kd = &k->k->k; + dsa_seed ds; /* --- Choose appropriate bit lengths if necessary --- */ @@ -411,12 +492,12 @@ static void alg_dsaparam(keyopts *k) sz = (k->qbits + 7) >> 3; p = sub_alloc(sz); - rand_getgood(RAND_GLOBAL, p, sz); + rand_get(RAND_GLOBAL, p, sz); /* --- Allocate the parameters --- */ - if (dsa_seed(&dp, k->qbits, k->bits, 0, p, sz, - (k->f & f_quiet) ? 0 : pgen_ev, 0)) + if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, &ds, + (k->f & f_quiet) ? 0 : pgen_ev, 0)) die(EXIT_FAILURE, "DSA parameter generation failed"); /* --- Store the parameters --- */ @@ -434,9 +515,13 @@ static void alg_dsaparam(keyopts *k) base64_init(&c); c.maxline = 0; c.indent = ""; - base64_encode(&c, p, sz, &d); + base64_encode(&c, ds.p, ds.sz, &d); base64_encode(&c, 0, 0, &d); key_putattr(k->kf, k->k, "seed", d.buf); + DRESET(&d); + dstr_putf(&d, "%u", ds.count); + key_putattr(k->kf, k->k, "count", d.buf); + xfree(ds.p); sub_free(p, sz); dstr_destroy(&d); } @@ -458,8 +543,7 @@ static void alg_dsa(keyopts *k) /* --- Choose a private key --- */ - x = mprand_range(MP_NEW, q, &rand_global, 0); - mp_burn(x); + x = mprand_range(MP_NEWSEC, q, &rand_global, 0); mpmont_create(&mm, p); y = mpmont_exp(&mm, MP_NEW, g, x); @@ -471,50 +555,65 @@ static void alg_dsa(keyopts *k) key_structure(kd); mpkey(kd, "x", x, KCAT_PRIV | KF_BURN); dolock(k, kd, "private"); + + mp_drop(x); mp_drop(y); } static void alg_dhparam(keyopts *k) { - static const char *pl[] = { "p", "g", 0 }; + static const char *pl[] = { "p", "q", "g", 0 }; if (!copyparam(k, pl)) { - pgen_safetestctx c; - mp *p, *q; + dh_param dp; key_data *kd = &k->k->k; + int rc; if (!k->bits) k->bits = 1024; /* --- Choose a large safe prime number --- */ - q = MP_NEW; - q = mprand(q, k->bits, &rand_global, 3); - p = pgen("p", MP_NEW, q, (k->f & f_quiet) ? 0 : pgen_ev, 0, - 0, pgen_safestep, &c.c, - rabin_iters(k->bits), pgen_safetest, &c); - if (!p) + if (k->f & f_limlee) { + mp **f; + size_t nf; + if (!k->qbits) + k->qbits = 256; + rc = dh_limlee(&dp, k->qbits, k->bits, + (k->f & f_subgroup) ? DH_SUBGROUP : 0, + 0, &rand_global, (k->f & f_quiet) ? 0 : pgen_ev, 0, + (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f); + if (!rc) { + dstr d = DSTR_INIT; + size_t i; + for (i = 0; i < nf; i++) { + if (i) + dstr_puts(&d, ", "); + mp_writedstr(f[i], &d, 10); + mp_drop(f[i]); + } + key_putattr(k->kf, k->k, "factors", d.buf); + dstr_destroy(&d); + } + } else + rc = dh_gen(&dp, k->qbits, k->bits, 0, &rand_global, + (k->f & f_quiet) ? 0 : pgen_ev, 0); + + if (rc) die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed"); key_structure(kd); - mpkey(kd, "p", p, KCAT_SHARE); - mp_drop(q); - mp_drop(p); - - /* --- The generator 4 is good --- * - * - * Since 4 is clearly a quadratic residue, and %$p = 2q + 1$% for prime - * %$q$%, the number 4 has order %$q$%. This is better than choosing a - * real primitive element, because it could conceivably be trapped in an - * order-2 subgroup. (Not very likely, I'll admit, but possible.) - */ - - mpkey(kd, "g", MP_FOUR, KCAT_SHARE); + mpkey(kd, "p", dp.p, KCAT_SHARE); + mpkey(kd, "q", dp.q, KCAT_SHARE); + mpkey(kd, "g", dp.g, KCAT_SHARE); + mp_drop(dp.q); + mp_drop(dp.p); + mp_drop(dp.g); } } static void alg_dh(keyopts *k) { mp *x, *y; - mp *p, *g; + mp *p, *q, *g; mpmont mm; key_data *kd = &k->k->k; @@ -522,6 +621,7 @@ static void alg_dh(keyopts *k) alg_dhparam(k); p = getmp(kd, "p"); + q = getmp(kd, "q"); g = getmp(kd, "g"); /* --- Choose a suitable private key --- * @@ -529,14 +629,12 @@ static void alg_dh(keyopts *k) * Since %$g$% has order %$q$%, choose %$x < q$%. */ - y = mp_lsr(MP_NEW, p, 1); - x = mprand_range(MP_NEW, y, &rand_global, 0); - mp_burn(x); + x = mprand_range(MP_NEWSEC, q, &rand_global, 0); /* --- Compute the public key %$y = g^x \bmod p$% --- */ mpmont_create(&mm, p); - y = mpmont_exp(&mm, y, g, x); + y = mpmont_exp(&mm, MP_NEW, g, x); mpmont_destroy(&mm); /* --- Store everything away --- */ @@ -547,13 +645,14 @@ static void alg_dh(keyopts *k) key_structure(kd); mpkey(kd, "x", x, KCAT_PRIV | KF_BURN); dolock(k, kd, "private"); + + mp_drop(x); mp_drop(y); } static void alg_bbs(keyopts *k) { - bbs_param bp; + bbs_priv bp; key_data *kd; - mp *p, *q; /* --- Sanity checking --- */ @@ -564,12 +663,9 @@ static void alg_bbs(keyopts *k) /* --- Generate the BBS parameters --- */ - p = mprand(MP_NEW, k->bits / 2, &rand_global, 3); - q = mprand(MP_NEW, k->bits - k->bits / 2, &rand_global, 3); - mp_burn(p); mp_burn(q); - if (bbs_gen(&bp, p, q, 0, (k->f & f_quiet) ? 0 : pgen_ev, 0)) + if (bbs_gen(&bp, k->bits, &rand_global, 0, + (k->f & f_quiet) ? 0 : pgen_ev, 0)) die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed"); - mp_drop(p); mp_drop(q); /* --- Allrighty then --- */ @@ -583,7 +679,7 @@ static void alg_bbs(keyopts *k) mpkey(kd, "q", bp.q, KCAT_PRIV | KF_BURN); dolock(k, kd, "private"); - mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n); + bbs_privfree(&bp); } /* --- The algorithm tables --- */ @@ -615,6 +711,7 @@ static int cmd_add(int argc, char *argv[]) const char *tag = 0, *ptag = 0; const char *c = 0; keyalg *alg = algtab; + const char *rtag = 0; keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0 }; /* --- Parse options for the subcommand --- */ @@ -628,11 +725,14 @@ static int cmd_add(int argc, char *argv[]) { "expire", OPTF_ARGREQ, 0, 'e' }, { "comment", OPTF_ARGREQ, 0, 'c' }, { "tag", OPTF_ARGREQ, 0, 't' }, + { "rand-id", OPTF_ARGREQ, 0, 'R' }, { "lock", 0, 0, 'l' }, { "quiet", 0, 0, 'q' }, + { "lim-lee", 0, 0, 'L' }, + { "subgroup", 0, 0, 'S' }, { 0, 0, 0, 0 } }; - int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:l", opt, 0, 0, 0); + int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:R:lqrLS", opt, 0, 0, 0); if (i < 0) break; @@ -717,15 +817,27 @@ static int cmd_add(int argc, char *argv[]) die(EXIT_FAILURE, "bad tag string `%s'", optarg); tag = optarg; break; + case 'r': + k.f |= f_retag; + break; /* --- Other flags --- */ + case 'R': + rtag = optarg; + break; case 'l': k.f |= f_lock; break; case 'q': k.f |= f_quiet; break; + case 'L': + k.f |= f_limlee; + break; + case 'S': + k.f |= f_subgroup; + break; /* --- Other things are bogus --- */ @@ -757,6 +869,10 @@ static int cmd_add(int argc, char *argv[]) doopen(&f, KOPEN_WRITE); k.kf = &f; + /* --- Key the generator --- */ + + keyrand(&f, rtag); + for (;;) { uint32 id = rand_global.ops->word(&rand_global); int err; @@ -770,8 +886,13 @@ static int cmd_add(int argc, char *argv[]) /* --- Set various simple attributes --- */ if (tag) { - int err = key_settag(&f, k.k, tag); - if (err) + int err; + key *kk; + if (k.f & f_retag) { + if ((kk = key_bytag(&f, tag)) != 0 && strcmp(kk->tag, tag) == 0) + key_settag(&f, kk, 0); + } + if ((err = key_settag(&f, k.k, tag)) != 0) die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err)); } @@ -818,11 +939,9 @@ typedef struct listopts { /* --- Listing flags --- */ -enum { - f_newline = 2, /* Write newline before next entry */ - f_attr = 4, /* Written at least one attribute */ - f_utc = 8 /* Emit UTC time, not local time */ -}; +#define f_newline 2u /* Write newline before next entry */ +#define f_attr 4u /* Written at least one attribute */ +#define f_utc 8u /* Emit UTC time, not local time */ /* --- @showkeydata@ --- * * @@ -1009,7 +1128,7 @@ static void showkey(key *k, listopts *o) o->f &= ~f_attr; printf("attributes:"); for (key_mkattriter(&i, k); key_nextattr(&i, &an, &av); ) { - printf("\n\t%s = %s", an, av); + printf("\n %s = %s", an, av); o->f |= f_attr; } if (o->f & f_attr) @@ -1192,26 +1311,6 @@ static int cmd_setattr(int argc, char *argv[]) /* --- @cmd_finger@ --- */ -static int fpkey(key_data *kd, dstr *d, void *p) -{ - rmd160_ctx *r = p; - switch (kd->e & KF_ENCMASK) { - case KENC_BINARY: - case KENC_ENCRYPT: - rmd160_hash(r, kd->u.k.k, kd->u.k.sz); - break; - case KENC_MP: { - size_t sz = mp_octets(kd->u.m); - octet *q = sub_alloc(sz); - mp_storeb(kd->u.m, q, sz); - rmd160_hash(r, q, sz); - memset(q, 0, sz); - sub_free(q, sz); - } break; - } - return (0); -} - static void fingerprint(key *k, const key_filter *kf) { rmd160_ctx r; @@ -1219,12 +1318,13 @@ static void fingerprint(key *k, const key_filter *kf) dstr d = DSTR_INIT; int i; - if (!key_match(&k->k, kf)) + if (!key_encode(&k->k, &d, kf)) return; rmd160_init(&r); - key_do(&k->k, kf, 0, fpkey, &r); + rmd160_hash(&r, d.buf, d.len); rmd160_done(&r, hash); - + + DRESET(&d); key_fulltag(k, &d); for (i = 0; i < sizeof(hash); i++) { if (i && i % 4 == 0) @@ -1246,7 +1346,7 @@ static int cmd_finger(int argc, char *argv[]) { "filter", OPTF_ARGREQ, 0, 'f' }, { 0, 0, 0, 0 } }; - int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0); + int i = mdwopt(argc, argv, "+f:", opt, 0, 0, 0); if (i < 0) break; switch (i) { @@ -1314,14 +1414,39 @@ static int cmd_tag(int argc, char *argv[]) key_file f; key *k; int err; + unsigned flags = 0; + int rc = 0; - if (argc < 2 || argc > 3) - die(EXIT_FAILURE, "Usage: tag tag [new-tag]"); + for (;;) { + static struct option opt[] = { + { "retag", 0, 0, 'r' }, + { 0, 0, 0, 0 } + }; + int i = mdwopt(argc, argv, "+r", opt, 0, 0, 0); + if (i < 0) + break; + switch (i) { + case 'r': + flags |= f_retag; + break; + default: + rc = 1; + break; + } + } + + argv += optind; argc -= optind; + if (argc < 1 || argc > 2 || rc) + die(EXIT_FAILURE, "Usage: tag [-r] tag [new-tag]"); doopen(&f, KOPEN_WRITE); - if ((k = key_bytag(&f, argv[1])) == 0) - die(EXIT_FAILURE, "key `%s' not found", argv[1]); - if ((err = key_settag(&f, k, argv[2])) != 0) - die(EXIT_FAILURE, "bad tag `%s': %s", argv[2], key_strerror(err)); + if (flags & f_retag) { + if ((k = key_bytag(&f, argv[1])) != 0 && strcmp(k->tag, argv[1]) == 0) + key_settag(&f, k, 0); + } + if ((k = key_bytag(&f, argv[0])) == 0) + die(EXIT_FAILURE, "key `%s' not found", argv[0]); + if ((err = key_settag(&f, k, argv[1])) != 0) + die(EXIT_FAILURE, "bad tag `%s': %s", argv[1], key_strerror(err)); doclose(&f); return (0); } @@ -1476,24 +1601,58 @@ static int cmd_merge(int argc, char *argv[]) static struct cmd { const char *name; int (*cmd)(int /*argc*/, char */*argv*/[]); + const char *usage; const char *help; } cmds[] = { { "add", cmd_add, "add [options] type [attr...]\n\ - Options: [-l] [-a alg] [-b bits] [-p param]\n\ - [-e expire] [-t tag] [-c comment]" - }, + Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\ + [-e expire] [-t tag] [-c comment]", "\ +Options:\n\ +\n\ +-a, --algorithm=ALG Generate keys suitable for ALG.\n\ +-b, --bits=N Generate an N-bit key.\n\ +-B, --qbits=N Use an N-bit subgroup or factors.\n\ +-p, --parameters=TAG Get group parameters from TAG.\n\ +-e, --expire=TIME Make the key expire after TIME.\n\ +-c, --comment=STRING Attach the command STRING to the key.\n\ +-t, --tag=TAG Tag the key with the name TAG.\n\ +-r, --retag Untag any key currently with that tag.\n\ +-R, --rand-id=TAG Use key named TAG for the random number generator.\n\ +-l, --lock Lock the generated key with a passphrase.\n\ +-q, --quiet Don't give progress indicators while working.\n\ +-L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\ +-S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\ +" }, { "expire", cmd_expire, "expire tag..." }, { "delete", cmd_delete, "delete tag..." }, - { "tag", cmd_tag, "tag tag [new-tag]" }, + { "tag", cmd_tag, "tag [-r] tag [new-tag]", "\ +Options:\n\ +\n\ +-r, --retag Untag any key currently called new-tag.\n\ +" }, { "setattr", cmd_setattr, "setattr tag attr..." }, { "comment", cmd_comment, "comment tag [comment]" }, { "lock", cmd_lock, "lock qtag" }, { "unlock", cmd_unlock, "unlock qtag" }, - { "list", cmd_list, "list [-uqv] [-f filter] [tag...]" }, - { "fingerprint", cmd_finger, "fingerprint [-f filter] [tag...]" }, + { "list", cmd_list, "list [-uqv] [-f filter] [tag...]", "\ +Options:\n\ +\n\ +-u, --utc Display expiry times etc. in UTC, not local time.\n\ +-q, --quiet Show less information.\n\ +-v, --verbose Show more information.\n\ +" }, + { "fingerprint", cmd_finger, "fingerprint [-f filter] [tag...]", "\ +Options:\n\ +\n\ +-f, --filter=FILT Only hash key components matching FILT.\n\ +" }, { "tidy", cmd_tidy, "tidy" }, - { "extract", cmd_extract, "extract file qtag..." }, + { "extract", cmd_extract, "extract [-f filter] file [tag...]", "\ +Options:\n\ +\n\ +-f, --filter=FILT Only extract key components matching FILT.\n\ +" }, { "merge", cmd_merge, "merge file" }, { 0, 0, 0 } }; @@ -1502,38 +1661,83 @@ typedef struct cmd cmd; /*----- Main code ---------------------------------------------------------*/ +/* --- @findcmd@ --- * + * + * Arguments: @const char *name@ = a command name + * + * Returns: Pointer to the command structure. + * + * Use: Looks up a command by name. If the command isn't found, an + * error is reported and the program is terminated. + */ + +static cmd *findcmd(const char *name) +{ + cmd *c, *chosen = 0; + size_t sz = strlen(name); + + for (c = cmds; c->name; c++) { + if (strncmp(name, c->name, sz) == 0) { + if (c->name[sz] == 0) { + chosen = c; + break; + } else if (chosen) + die(EXIT_FAILURE, "ambiguous command name `%s'", name); + else + chosen = c; + } + } + if (!chosen) + die(EXIT_FAILURE, "unknown command name `%s'", name); + return (chosen); +} + /* --- Helpful GNUy functions --- */ void usage(FILE *fp) { - fprintf(fp, "Usage: %s [-k file] command [args]\n", QUIS); + pquis(fp, "Usage: $ [-k keyring] command [args]\n"); } void version(FILE *fp) { - fprintf(fp, "%s, Catacomb version " VERSION "\n", QUIS); + pquis(fp, "$, Catacomb version " VERSION "\n"); } -void help(FILE *fp) +void help(FILE *fp, char **argv) { cmd *c; + version(fp); fputc('\n', fp); - usage(fp); - fputs("\n\ + if (*argv) { + c = findcmd(*argv); + fprintf(fp, "Usage: %s [-k keyring] %s\n", QUIS, c->usage); + if (c->help) { + fputc('\n', fp); + fputs(c->help, fp); + } + } else { + version(fp); + fputc('\n', fp); + usage(fp); + fputs("\n\ Performs various simple key management operations. Command line options\n\ recognized are:\n\ \n\ --h, --help Display this help text.\n\ +-h, --help [COMMAND] Display this help text (or help for COMMAND).\n\ -v, --version Display version number.\n\ -u, --usage Display short usage summary.\n\ \n\ -k, --keyring=FILE Read and write keys in FILE.\n\ +-i, --id=TAG Use key TAG for random number generator.\n\ +-t, --type=TYPE Use key TYPE for random number generator.\n\ \n\ The following commands are understood:\n\n", - fp); - for (c = cmds; c->name; c++) - fprintf(fp, "%s\n", c->help); + fp); + for (c = cmds; c->name; c++) + fprintf(fp, "%s\n", c->usage); + } } /* --- @main@ --- * @@ -1550,20 +1754,13 @@ int main(int argc, char *argv[]) { unsigned f = 0; - enum { - f_bogus = 1 - }; +#define f_bogus 1u /* --- Initialization --- */ ego(argv[0]); sub_init(); - /* --- Initialize the Catacomb random number generator --- */ - - rand_init(RAND_GLOBAL); - rand_noisesrc(RAND_GLOBAL, &noise_source); - /* --- Parse command line options --- */ for (;;) { @@ -1590,8 +1787,9 @@ int main(int argc, char *argv[]) switch (i) { /* --- GNU help options --- */ + case 'h': - help(stdout); + help(stdout, argv + optind); exit(0); case 'v': version(stdout); @@ -1621,31 +1819,17 @@ int main(int argc, char *argv[]) exit(1); } + /* --- Initialize the Catacomb random number generator --- */ + + rand_noisesrc(RAND_GLOBAL, &noise_source); + rand_seed(RAND_GLOBAL, 160); + /* --- Dispatch to appropriate command handler --- */ argc -= optind; argv += optind; optind = 0; - - { - cmd *c, *chosen = 0; - size_t sz = strlen(argv[0]); - - for (c = cmds; c->name; c++) { - if (strncmp(argv[0], c->name, sz) == 0) { - if (c->name[sz] == 0) { - chosen = c; - break; - } else if (chosen) - die(EXIT_FAILURE, "ambiguous command name `%s'", argv[0]); - else - chosen = c; - } - } - if (!chosen) - die(EXIT_FAILURE, "unknown command name `%s'", argv[0]); - return (chosen->cmd(argc, argv)); - } + return (findcmd(argv[0])->cmd(argc, argv)); } /*----- That's all, folks -------------------------------------------------*/