From ce70ec47fb1c7dabeccfafbd93fa47e0b4c6c8ad Mon Sep 17 00:00:00 2001 From: mdw Date: Wed, 21 Feb 2001 20:04:27 +0000 Subject: [PATCH] Provide help on individual commands (some need it desparately). Allow atomic retagging of keys. --- keyutil.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 145 insertions(+), 48 deletions(-) diff --git a/keyutil.c b/keyutil.c index 25be851..80278ad 100644 --- a/keyutil.c +++ b/keyutil.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: keyutil.c,v 1.12 2001/02/03 11:58:22 mdw Exp $ + * $Id: keyutil.c,v 1.13 2001/02/21 20:04:27 mdw Exp $ * * Simple key manager program * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: keyutil.c,v $ + * 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. @@ -195,6 +199,7 @@ typedef struct keyopts { #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@ --- * * @@ -717,14 +722,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' }, + { "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:r:lqLS", 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; @@ -809,10 +814,13 @@ 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': + case 'R': rtag = optarg; break; case 'l': @@ -875,8 +883,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)); } @@ -1330,7 +1343,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) { @@ -1398,14 +1411,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); } @@ -1560,24 +1598,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: [-lqLS] [-a alg] [-b|-B bits] [-p param] [-r tag]\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 [-f filter] file [tag...]" }, + { "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 } }; @@ -1586,6 +1658,37 @@ 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) @@ -1598,17 +1701,28 @@ void version(FILE *fp) 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 [-options] %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\ @@ -1617,9 +1731,10 @@ recognized are:\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@ --- * @@ -1671,8 +1786,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); @@ -1712,26 +1828,7 @@ int main(int argc, char *argv[]) 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 -------------------------------------------------*/ -- 2.11.0