diff options
author | Martin Willi <martin@strongswan.org> | 2009-09-10 12:44:06 +0200 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2009-09-10 16:18:30 +0200 |
commit | 3ce9438b6010e6fd5f61531ae25473ea60a83cd9 (patch) | |
tree | fcd733fb577ce734edf8f3f9eacae37e498dc954 /src/pki | |
parent | 6be68cc1c72ab076fbdfd05f24662519e62cfca8 (diff) | |
download | strongswan-3ce9438b6010e6fd5f61531ae25473ea60a83cd9.tar.bz2 strongswan-3ce9438b6010e6fd5f61531ae25473ea60a83cd9.tar.xz |
Use dynamic registration/usage invocation of command types
Diffstat (limited to 'src/pki')
-rw-r--r-- | src/pki/Makefile.am | 5 | ||||
-rw-r--r-- | src/pki/command.c | 71 | ||||
-rw-r--r-- | src/pki/command.h | 25 | ||||
-rw-r--r-- | src/pki/commands/gen.c | 12 | ||||
-rw-r--r-- | src/pki/commands/issue.c | 4 | ||||
-rw-r--r-- | src/pki/commands/keyid.c | 8 | ||||
-rw-r--r-- | src/pki/commands/pub.c | 10 | ||||
-rw-r--r-- | src/pki/commands/self.c | 4 | ||||
-rw-r--r-- | src/pki/commands/verify.c | 6 |
9 files changed, 77 insertions, 68 deletions
diff --git a/src/pki/Makefile.am b/src/pki/Makefile.am index 980ca5637..3f2694d97 100644 --- a/src/pki/Makefile.am +++ b/src/pki/Makefile.am @@ -1,8 +1,9 @@ ipsec_PROGRAMS = pki pki_SOURCES = pki.c pki.h command.c command.h \ - commands/gen.c commands/pub.c commands/keyid.c \ - commands/self.c commands/issue.c commands/verify.c + commands/verify.c commands/issue.c commands/self.c \ + commands/keyid.c commands/pub.c commands/gen.c + pki_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la INCLUDES = -I$(top_srcdir)/src/libstrongswan AM_CFLAGS = \ diff --git a/src/pki/command.c b/src/pki/command.c index 9b02596db..10559557a 100644 --- a/src/pki/command.c +++ b/src/pki/command.c @@ -23,24 +23,39 @@ /** * Registered commands. */ -command_t cmds[CMD_MAX]; +command_t cmds[MAX_COMMANDS]; + +/** + * active command. + */ +static int active = 0; + +/** + * number of registered commands + */ +static int registered = 0; + +/** + * help command index + */ +static int help_idx; /** * Global options used by all subcommands */ -struct option command_opts[CMD_MAX > MAX_OPTIONS ?: MAX_OPTIONS]; +struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS]; /** * Build long_opts for a specific command */ -static void build_opts(command_type_t cmd) +static void build_opts() { int i; memset(command_opts, 0, sizeof(command_opts)); - if (cmd == CMD_HELP) + if (active == help_idx) { - for (i = 0; i < CMD_MAX; i++) + for (i = 0; cmds[i].cmd; i++) { command_opts[i].name = cmds[i].cmd; command_opts[i].val = cmds[i].op; @@ -48,11 +63,11 @@ static void build_opts(command_type_t cmd) } else { - for (i = 0; cmds[cmd].options[i].name; i++) + for (i = 0; cmds[active].options[i].name; i++) { - command_opts[i].name = cmds[cmd].options[i].name; - command_opts[i].has_arg = cmds[cmd].options[i].arg; - command_opts[i].val = cmds[cmd].options[i].op; + command_opts[i].name = cmds[active].options[i].name; + command_opts[i].has_arg = cmds[active].options[i].arg; + command_opts[i].val = cmds[active].options[i].op; } } } @@ -60,15 +75,15 @@ static void build_opts(command_type_t cmd) /** * Register a command */ -void command_register(command_type_t type, command_t command) +void command_register(command_t command) { - cmds[type] = command; + cmds[registered++] = command; } /** * Print usage text, with an optional error */ -int command_usage(command_type_t cmd, char *error) +int command_usage(char *error) { FILE *out = stdout; int i; @@ -80,43 +95,43 @@ int command_usage(command_type_t cmd, char *error) } fprintf(out, "strongSwan %s PKI tool\n", VERSION); fprintf(out, "usage:\n"); - if (cmd == CMD_HELP) + if (active == help_idx) { - for (i = 0; i < CMD_MAX; i++) + for (i = 0; cmds[i].cmd; i++) { fprintf(out, " pki --%-6s %s\n", cmds[i].cmd, cmds[i].description); } } else { - for (i = 0; cmds[cmd].line[i]; i++) + for (i = 0; cmds[active].line[i]; i++) { if (i == 0) { - fprintf(out, " pki --%s %s\n", cmds[cmd].cmd, cmds[cmd].line[i]); + fprintf(out, " pki --%s %s\n", + cmds[active].cmd, cmds[active].line[i]); } else { - fprintf(out, " %s\n", cmds[cmd].line[i]); + fprintf(out, " %s\n", cmds[active].line[i]); } } - for (i = 0; cmds[cmd].options[i].name; i++) + for (i = 0; cmds[active].options[i].name; i++) { fprintf(out, " --%-8s %s\n", - cmds[cmd].options[i].name, cmds[cmd].options[i].desc); + cmds[active].options[i].name, cmds[active].options[i].desc); } } return error != NULL; } - /** * Show usage information */ static int help(int argc, char *argv[]) { - return command_usage(CMD_HELP, NULL); + return command_usage(NULL); } /** @@ -126,18 +141,20 @@ int command_dispatch(int argc, char *argv[]) { int op, i; - command_register(CMD_HELP, (command_t) { - help, 'h', "help", "show usage information"}); - build_opts(CMD_HELP); + active = help_idx = registered; + command_register((command_t){help, 'h', "help", "show usage information"}); + + build_opts(); op = getopt_long(argc, argv, "", command_opts, NULL); - for (i = 0; i < CMD_MAX; i++) + for (i = 0; cmds[i].cmd; i++) { if (cmds[i].op == op) { - build_opts(i); + active = i; + build_opts(); return cmds[i].call(argc, argv); } } - return command_usage(CMD_HELP, "invalid operation"); + return command_usage("invalid operation"); } diff --git a/src/pki/command.h b/src/pki/command.h index 852a84d2a..b6418146b 100644 --- a/src/pki/command.h +++ b/src/pki/command.h @@ -25,6 +25,11 @@ #include <getopt.h> /** + * Maximum number of commands. + */ +#define MAX_COMMANDS 10 + +/** * Maximum number of options in a command (+1) */ #define MAX_OPTIONS 14 @@ -65,28 +70,14 @@ struct command_t { }; /** - * Type of available commands - */ -enum command_type_t { - CMD_HELP = 0, - CMD_GEN, - CMD_PUB, - CMD_KEYID, - CMD_SELF, - CMD_ISSUE, - CMD_VERIFY, - CMD_MAX -}; - -/** - * Options of the currently processing command. + * Options of the active command. */ extern struct option command_opts[]; /** * Register a command. */ -void command_register(command_type_t type, command_t command); +void command_register(command_t command); /** * Dispatch commands. @@ -96,6 +87,6 @@ int command_dispatch(int argc, char *argv[]); /** * Show usage information of active command. */ -int command_usage(command_type_t cmd, char *error); +int command_usage(char *error); #endif /* COMMAND_H_ @}*/ diff --git a/src/pki/commands/gen.c b/src/pki/commands/gen.c index 4bc8f8c2d..791c1353e 100644 --- a/src/pki/commands/gen.c +++ b/src/pki/commands/gen.c @@ -31,7 +31,7 @@ static int gen(int argc, char *argv[]) switch (getopt_long(argc, argv, "", command_opts, NULL)) { case 'h': - return command_usage(CMD_GEN, NULL); + return command_usage(NULL); case 't': if (streq(optarg, "rsa")) { @@ -43,26 +43,26 @@ static int gen(int argc, char *argv[]) } else { - return command_usage(CMD_GEN, "invalid key type"); + return command_usage("invalid key type"); } continue; case 'o': if (!get_form(optarg, &form, FALSE)) { - return command_usage(CMD_GEN, "invalid key output format"); + return command_usage("invalid key output format"); } continue; case 's': size = atoi(optarg); if (!size) { - return command_usage(CMD_GEN, "invalid key size"); + return command_usage("invalid key size"); } continue; case EOF: break; default: - return command_usage(CMD_GEN, "invalid --gen option"); + return command_usage("invalid --gen option"); } break; } @@ -110,7 +110,7 @@ static int gen(int argc, char *argv[]) */ static void __attribute__ ((constructor))reg() { - command_register(CMD_GEN, (command_t) { + command_register((command_t) { gen, 'g', "gen", "generate a new private key", {"[--type rsa|ecdsa] [--size bits] [--outform der|pem|pgp]"}, { diff --git a/src/pki/commands/issue.c b/src/pki/commands/issue.c index 097d7d22f..c141827fa 100644 --- a/src/pki/commands/issue.c +++ b/src/pki/commands/issue.c @@ -247,7 +247,7 @@ end: usage: san->destroy_offset(san, offsetof(identification_t, destroy)); options->destroy(options); - return command_usage(CMD_ISSUE, error); + return command_usage(error); } /** @@ -255,7 +255,7 @@ usage: */ static void __attribute__ ((constructor))reg() { - command_register(CMD_ISSUE, (command_t) { + command_register((command_t) { issue, 'i', "issue", "issue a certificate using a CA certificate and key", {"[--in file] [--type pub|pkcs10]", diff --git a/src/pki/commands/keyid.c b/src/pki/commands/keyid.c index e270c9460..128645ee0 100644 --- a/src/pki/commands/keyid.c +++ b/src/pki/commands/keyid.c @@ -37,7 +37,7 @@ static int keyid(int argc, char *argv[]) switch (getopt_long(argc, argv, "", command_opts, NULL)) { case 'h': - return command_usage(CMD_KEYID, NULL); + return command_usage(NULL); case 't': if (streq(optarg, "rsa-priv")) { @@ -61,7 +61,7 @@ static int keyid(int argc, char *argv[]) } else { - return command_usage(CMD_KEYID, "invalid input type"); + return command_usage( "invalid input type"); } continue; case 'i': @@ -70,7 +70,7 @@ static int keyid(int argc, char *argv[]) case EOF: break; default: - return command_usage(CMD_KEYID, "invalid --keyid option"); + return command_usage("invalid --keyid option"); } break; } @@ -144,7 +144,7 @@ static int keyid(int argc, char *argv[]) */ static void __attribute__ ((constructor))reg() { - command_register(CMD_KEYID, (command_t) + command_register((command_t) { keyid, 'k', "keyid", "calculate key identifiers of a key/certificate", {"[--in file] [--type rsa-priv|ecdsa-priv|pub|x509]"}, diff --git a/src/pki/commands/pub.c b/src/pki/commands/pub.c index 5e7839fd6..16bd0b049 100644 --- a/src/pki/commands/pub.c +++ b/src/pki/commands/pub.c @@ -38,7 +38,7 @@ static int pub(int argc, char *argv[]) switch (getopt_long(argc, argv, "", command_opts, NULL)) { case 'h': - return command_usage(CMD_PUB, NULL); + return command_usage(NULL); case 't': if (streq(optarg, "rsa")) { @@ -57,13 +57,13 @@ static int pub(int argc, char *argv[]) } else { - return command_usage(CMD_PUB, "invalid input type"); + return command_usage("invalid input type"); } continue; case 'f': if (!get_form(optarg, &form, TRUE)) { - return command_usage(CMD_PUB, "invalid output format"); + return command_usage("invalid output format"); } continue; case 'i': @@ -72,7 +72,7 @@ static int pub(int argc, char *argv[]) case EOF: break; default: - return command_usage(CMD_PUB, "invalid --pub option"); + return command_usage("invalid --pub option"); } break; } @@ -136,7 +136,7 @@ static int pub(int argc, char *argv[]) */ static void __attribute__ ((constructor))reg() { - command_register(CMD_PUB, (command_t) { + command_register((command_t) { pub, 'p', "pub", "extract the public key from a private key/certificate", {"[--in file] [--type rsa|ecdsa|x509] [--outform der|pem|pgp]"}, diff --git a/src/pki/commands/self.c b/src/pki/commands/self.c index 1f1138e9c..de1761c9c 100644 --- a/src/pki/commands/self.c +++ b/src/pki/commands/self.c @@ -206,7 +206,7 @@ end: usage: san->destroy_offset(san, offsetof(identification_t, destroy)); options->destroy(options); - return command_usage(CMD_SELF, error); + return command_usage(error); } /** @@ -214,7 +214,7 @@ usage: */ static void __attribute__ ((constructor))reg() { - command_register(CMD_SELF, (command_t) { + command_register((command_t) { self, 's', "self", "create a self signed certificate", {"[--in file] [--type rsa|ecdsa]", diff --git a/src/pki/commands/verify.c b/src/pki/commands/verify.c index 6905112fb..d089d3384 100644 --- a/src/pki/commands/verify.c +++ b/src/pki/commands/verify.c @@ -32,7 +32,7 @@ static int verify(int argc, char *argv[]) switch (getopt_long(argc, argv, "", command_opts, NULL)) { case 'h': - return command_usage(CMD_VERIFY, NULL); + return command_usage(NULL); case 'i': file = optarg; continue; @@ -42,7 +42,7 @@ static int verify(int argc, char *argv[]) case EOF: break; default: - return command_usage(CMD_VERIFY, "invalid --verify option"); + return command_usage("invalid --verify option"); } break; } @@ -121,7 +121,7 @@ static int verify(int argc, char *argv[]) */ static void __attribute__ ((constructor))reg() { - command_register(CMD_VERIFY, (command_t) { + command_register((command_t) { verify, 'v', "verify", "verify a certificate using the CA certificate", {"[--in file] [--ca file]"}, |