The branch master has been updated via 678cae0295e3fe600edc049742b8c765a58edebc (commit) via 3372039252c4d9c67de784a0fbdad5589991a347 (commit) from 046a7aaa5e3c398b19fcdb5b486d57ab9c6ced30 (commit)
- Log ----------------------------------------------------------------- commit 678cae0295e3fe600edc049742b8c765a58edebc Author: Dr. David von Oheimb <david.von.ohe...@siemens.com> Date: Thu Jan 7 10:16:12 2021 +0100 APPS: Print help also on -h and --h; print high-level help when no cmd given Reviewed-by: Tomas Mraz <tm...@fedoraproject.org> (Merged from https://github.com/openssl/openssl/pull/13799) commit 3372039252c4d9c67de784a0fbdad5589991a347 Author: Dr. David von Oheimb <david.von.ohe...@siemens.com> Date: Thu Jan 7 09:00:02 2021 +0100 APPS: Fix confusion between program and app/command name used in diagnostic/help output Reviewed-by: Tomas Mraz <tm...@fedoraproject.org> (Merged from https://github.com/openssl/openssl/pull/13799) ----------------------------------------------------------------------- Summary of changes: apps/cmp.c | 6 ++---- apps/dgst.c | 3 +-- apps/enc.c | 16 ++++++++-------- apps/include/opt.h | 1 + apps/lib/opt.c | 13 +++++++++++-- apps/openssl.c | 37 ++++++++++++++++++------------------- apps/s_client.c | 3 +-- test/recipes/20-test_app.t | 10 ++++++++-- 8 files changed, 50 insertions(+), 39 deletions(-) diff --git a/apps/cmp.c b/apps/cmp.c index a484234f90..b28b7431ce 100644 --- a/apps/cmp.c +++ b/apps/cmp.c @@ -42,6 +42,7 @@ #include <openssl/objects.h> #include <openssl/x509.h> +static char *prog; static char *opt_config = NULL; #define CMP_SECTION "cmp" #define SECTION_NAME_MAX 40 /* max length of section name */ @@ -49,10 +50,6 @@ static char *opt_config = NULL; static char *opt_section = CMP_SECTION; static int opt_verbosity = OSSL_CMP_LOG_INFO; -#undef PROG -#define PROG cmp_main -static char *prog = "cmp"; - static int read_config(void); static CONF *conf = NULL; /* OpenSSL config file context structure */ @@ -2625,6 +2622,7 @@ int cmp_main(int argc, char **argv) int ret = 0; /* default: failure */ if (argc <= 1) { + prog = opt_appname(argv[0]); opt_help(cmp_options); goto err; } diff --git a/apps/dgst.c b/apps/dgst.c index 7110a97cf4..845c2eabc9 100644 --- a/apps/dgst.c +++ b/apps/dgst.c @@ -111,9 +111,8 @@ int dgst_main(int argc, char **argv) int engine_impl = 0; struct doall_dgst_digests dec; - prog = opt_progname(argv[0]); buf = app_malloc(BUFSIZE, "I/O buffer"); - md = EVP_get_digestbyname(prog); + md = EVP_get_digestbyname(argv[0]); prog = opt_init(argc, argv, dgst_options); while ((o = opt_next()) != OPT_EOF) { diff --git a/apps/enc.c b/apps/enc.c index f97621b1a6..42b14d4993 100644 --- a/apps/enc.c +++ b/apps/enc.c @@ -112,7 +112,7 @@ int enc_main(int argc, char **argv) const EVP_CIPHER *cipher = NULL, *c; const EVP_MD *dgst = NULL; char *hkey = NULL, *hiv = NULL, *hsalt = NULL, *p; - char *infile = NULL, *outfile = NULL, *prog; + char *infile = NULL, *outfile = NULL, *prog, *arg0; char *str = NULL, *passarg = NULL, *pass = NULL, *strbuf = NULL; char mbuf[sizeof(magic) - 1]; OPTION_CHOICE o; @@ -131,18 +131,18 @@ int enc_main(int argc, char **argv) BIO *bzl = NULL; #endif - /* first check the program name */ - prog = opt_progname(argv[0]); - if (strcmp(prog, "base64") == 0) { + /* first check the command name */ + arg0 = argv[0]; + if (strcmp(arg0, "base64") == 0) { base64 = 1; #ifdef ZLIB - } else if (strcmp(prog, "zlib") == 0) { + } else if (strcmp(arg0, "zlib") == 0) { do_zlib = 1; #endif } else { - cipher = EVP_get_cipherbyname(prog); - if (cipher == NULL && strcmp(prog, "enc") != 0) { - BIO_printf(bio_err, "%s is not a known cipher\n", prog); + cipher = EVP_get_cipherbyname(arg0); + if (cipher == NULL && strcmp(arg0, "enc") != 0) { + BIO_printf(bio_err, "%s is not a known cipher\n", arg0); goto end; } } diff --git a/apps/include/opt.h b/apps/include/opt.h index 56de57cf4c..15375e3a80 100644 --- a/apps/include/opt.h +++ b/apps/include/opt.h @@ -341,6 +341,7 @@ typedef struct string_int_pair_st { const char *opt_path_end(const char *filename); char *opt_progname(const char *argv0); +char *opt_appname(const char *arg0); char *opt_getprog(void); char *opt_init(int ac, char **av, const OPTIONS * o); int opt_next(void); diff --git a/apps/lib/opt.c b/apps/lib/opt.c index 260ff3b1c2..22d4138301 100644 --- a/apps/lib/opt.c +++ b/apps/lib/opt.c @@ -138,6 +138,15 @@ char *opt_progname(const char *argv0) } #endif +char *opt_appname(const char *arg0) +{ + size_t len = strlen(prog); + + if (arg0 != NULL) + snprintf(prog + len, sizeof(prog) - len - 1, " %s", arg0); + return prog; +} + char *opt_getprog(void) { return prog; @@ -151,7 +160,6 @@ char *opt_init(int ac, char **av, const OPTIONS *o) argv = av; opt_begin(); opts = o; - opt_progname(av[0]); unknown = NULL; /* Check all options up until the PARAM marker (if present) */ @@ -724,7 +732,8 @@ int opt_next(void) *arg++ = '\0'; for (o = opts; o->name; ++o) { /* If not this option, move on to the next one. */ - if (strcmp(p, o->name) != 0) + if (!(strcmp(p, "h") == 0 && strcmp(o->name, "help") == 0) + && strcmp(p, o->name) != 0) continue; /* If it doesn't take a value, make sure none was given. */ diff --git a/apps/openssl.c b/apps/openssl.c index e6746087ad..b61ed5f81d 100644 --- a/apps/openssl.c +++ b/apps/openssl.c @@ -235,7 +235,9 @@ int main(int argc, char *argv[]) FUNCTION f, *fp; LHASH_OF(FUNCTION) *prog = NULL; char *pname; + const char *fname; ARGS arg; + int global_help = 0; int ret = 0; arg.argv = NULL; @@ -249,9 +251,7 @@ int main(int argc, char *argv[]) #if defined(OPENSSL_SYS_VMS) && defined(__DECC) argv = copy_argv(&argc, argv); #elif defined(_WIN32) - /* - * Replace argv[] with UTF-8 encoded strings. - */ + /* Replace argv[] with UTF-8 encoded strings. */ win32_utf8argv(&argc, &argv); #endif @@ -259,18 +259,11 @@ int main(int argc, char *argv[]) setup_trace(getenv("OPENSSL_TRACE")); #endif - if (!apps_startup()) { - BIO_printf(bio_err, - "FATAL: Startup failure (dev note: apps_startup() failed)\n"); - ERR_print_errors(bio_err); - ret = 1; - goto end; - } - - prog = prog_init(); - if (prog == NULL) { + if ((fname = "apps_startup", !apps_startup()) + || (fname = "prog_init", (prog = prog_init()) == NULL)) { BIO_printf(bio_err, - "FATAL: Startup failure (dev note: prog_init() failed)\n"); + "FATAL: Startup failure (dev note: %s()) for %s\n", + fname, argv[0]); ERR_print_errors(bio_err); ret = 1; goto end; @@ -285,15 +278,21 @@ int main(int argc, char *argv[]) f.name = pname; fp = lh_FUNCTION_retrieve(prog, &f); if (fp == NULL) { - /* We assume we've been called as 'openssl cmd' */ + /* We assume we've been called as 'openssl ...' */ + global_help = argc > 1 + && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0 + || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0); argc--; argv++; + opt_appname(argc == 1 || global_help ? "help" : argv[0]); + } else { + argv[0] = pname; } /* If there's a command, run with that, otherwise "help". */ - ret = argc > 0 - ? do_cmd(prog, argc, argv) - : do_cmd(prog, 1, help_argv); + ret = argc == 0 || global_help + ? do_cmd(prog, 1, help_argv) + : do_cmd(prog, argc, argv); end: OPENSSL_free(default_config_file); @@ -360,7 +359,7 @@ int help_main(int argc, char **argv) } calculate_columns(functions, &dc); - BIO_printf(bio_err, "Standard commands"); + BIO_printf(bio_err, "%s:\n\nStandard commands", prog); i = 0; tp = FT_none; for (fp = functions; fp->name != NULL; fp++) { diff --git a/apps/s_client.c b/apps/s_client.c index 56444baeca..25c01f4088 100644 --- a/apps/s_client.c +++ b/apps/s_client.c @@ -1010,7 +1010,6 @@ int s_client_main(int argc, char **argv) # endif #endif - prog = opt_progname(argv[0]); c_quiet = 0; c_debug = 0; c_showcerts = 0; @@ -1019,7 +1018,7 @@ int s_client_main(int argc, char **argv) cctx = SSL_CONF_CTX_new(); if (vpm == NULL || cctx == NULL) { - BIO_printf(bio_err, "%s: out of memory\n", prog); + BIO_printf(bio_err, "%s: out of memory\n", opt_getprog()); goto end; } diff --git a/test/recipes/20-test_app.t b/test/recipes/20-test_app.t index e7246565f2..dfd0db25b8 100644 --- a/test/recipes/20-test_app.t +++ b/test/recipes/20-test_app.t @@ -13,7 +13,7 @@ use OpenSSL::Test; setup("test_app"); -plan tests => 3; +plan tests => 5; ok(run(app(["openssl"])), "Run openssl app with no args"); @@ -21,5 +21,11 @@ ok(run(app(["openssl"])), ok(run(app(["openssl", "help"])), "Run openssl app with help"); -ok(!run(app(["openssl", "-help"])), +ok(!run(app(["openssl", "-wrong"])), "Run openssl app with incorrect arg"); + +ok(run(app(["openssl", "-help"])), + "Run openssl app with -help"); + +ok(run(app(["openssl", "--help"])), + "Run openssl app with --help");