From 7645b51c29c02f060a94cb5a8516fb1a5c6cf9e5 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Apr 23 2015 21:42:59 +0000 Subject: Port the local signer to use popt Use popt instead of getopt to parse arguments to the local signing helper. --- diff --git a/src/Makefile.am b/src/Makefile.am index 02b086d..8169058 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -244,10 +244,11 @@ scep_submit_LDADD = $(CURL_LIBS) $(XML_LIBS) $(NSS_LIBS) \ $(GMP_LIBS) $(UUID_LIBS) $(LTLIBICONV) $(POPT_LIBS) local_submit_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS) local_submit_SOURCES = local.c env.h store.h store-gen.c submit-o.c submit-o.h \ - submit-u.c submit-u.h env-session.c env-shared.c log.c log.h \ - prefs.c prefs.h prefs-o.c prefs-o.h tm.c tm.h \ + submit-u.c submit-u.h env-session.c env-shared.c log.c \ + log.h prefs.c prefs.h prefs-o.c prefs-o.h tm.c tm.h \ util.c util.h util-o.c util-o.h pin.c pin.h -local_submit_LDADD = $(NSS_LIBS) $(OPENSSL_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) +local_submit_LDADD = $(NSS_LIBS) $(OPENSSL_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) \ + $(POPT_LIBS) submit_d_CFLAGS = $(AM_CFLAGS) $(CURL_CFLAGS) $(XML_CFLAGS) -DCM_SUBMIT_D_MAIN submit_d_SOURCES = submit-d.c submit-d.h submit-h.c submit-h.h \ submit-u.c submit-u.h log.c log.h tm.c tm.h util-m.c util-m.h diff --git a/src/local.c b/src/local.c index 4c83a23..e9b9678 100644 --- a/src/local.c +++ b/src/local.c @@ -43,6 +43,8 @@ #include +#include + #include "env.h" #include "log.h" #include "prefs.h" @@ -65,16 +67,6 @@ static unsigned char uuid[16]; static void -help(const char *argv0) -{ - fprintf(stderr, - "Usage: %s [-v] [-d ca-data-directory] [csrfile]\n", - strchr(argv0, '/') ? - strrchr(argv0, '/') + 1 : - argv0); -} - -static void set_ca_extensions(void *parent, X509_REQ *req, EVP_PKEY *key) { STACK_OF(X509_EXTENSION) *exts; @@ -426,16 +418,23 @@ local_lock(void *parent, const char *localdir) } int -main(int argc, char **argv) +main(int argc, const char **argv) { int i, c, verbose = 0, lfd = -1; void *parent; - const char *mode = CM_OP_SUBMIT; + const char *mode = CM_OP_SUBMIT, *csrfile; char *csr, *localdir = NULL, *hexserial = NULL, *serial, buf[LINE_MAX]; FILE *fp; X509 **roots = NULL, *signer = NULL, *cert = NULL; EVP_PKEY *key = NULL; time_t now; + poptContext pctx; + const struct poptOption popts[] = { + {"ca-data-directory", 'd', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &localdir, 0, "storage location for the CA's data", "DIRECTORY"}, + {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', NULL, NULL}, + POPT_AUTOHELP + POPT_TABLEEND + }; #ifdef ENABLE_NLS bindtextdomain(PACKAGE, MYLOCALEDIR); @@ -466,20 +465,22 @@ main(int argc, char **argv) if (localdir == NULL) { localdir = cm_env_local_ca_dir(); } - while ((c = getopt(argc, argv, "d:v")) != -1) { + pctx = poptGetContext(argv[0], argc, argv, popts, 0); + if (pctx == NULL) { + return CM_SUBMIT_STATUS_UNCONFIGURED; + } + poptSetOtherOptionHelp(pctx, "[options...] [csrfile]"); + while ((c = poptGetNextOpt(pctx)) > 0) { switch (c) { - case 'd': - localdir = optarg; - break; case 'v': verbose++; break; - default: - help(argv[0]); - return CM_SUBMIT_STATUS_UNCONFIGURED; - break; } } + if (c != -1) { + poptPrintUsage(pctx, stdout, 0); + return CM_SUBMIT_STATUS_UNCONFIGURED; + } umask(S_IRWXG | S_IRWXO); @@ -487,7 +488,7 @@ main(int argc, char **argv) cm_log_set_level(verbose); if (localdir == NULL) { - help(argv[0]); + poptPrintUsage(pctx, stdout, 0); return CM_SUBMIT_STATUS_UNCONFIGURED; } @@ -538,8 +539,9 @@ main(int argc, char **argv) (strcasecmp(mode, CM_OP_POLL) == 0)) { /* Read the CSR from the environment, or from the file named on * the command-line. */ - if (optind < argc) { - csr = cm_submit_u_from_file(argv[optind++]); + csrfile = poptGetArg(pctx); + if (csrfile != NULL) { + csr = cm_submit_u_from_file(csrfile); } else { csr = getenv(CM_SUBMIT_CSR_ENV); if (csr != NULL) { @@ -549,7 +551,7 @@ main(int argc, char **argv) if ((csr == NULL) || (strlen(csr) == 0)) { printf(_("Unable to read signing request.\n")); cm_log(1, "Unable to read signing request.\n"); - help(argv[0]); + poptPrintUsage(pctx, stdout, 0); return CM_SUBMIT_STATUS_UNCONFIGURED; } /* Take the lock. */