APPS: Fix confusion between program and app/command name used in diagnostic/help...
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 7 Jan 2021 08:00:02 +0000 (09:00 +0100)
committerDr. David von Oheimb <dev@ddvo.net>
Mon, 11 Jan 2021 18:39:49 +0000 (19:39 +0100)
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/13799)

apps/cmp.c
apps/dgst.c
apps/enc.c
apps/include/opt.h
apps/lib/opt.c
apps/openssl.c
apps/s_client.c

index a484234f9078bb8636e348deba24ab12a798a9ee..b28b7431ced524af97c7bd76bbd4775ed8d49288 100644 (file)
@@ -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;
     }
index 7110a97cf4f1c27f5c53b83bdf17b52f5e464581..845c2eabc99b80157a3c59098e73219944ade53e 100644 (file)
@@ -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) {
index f97621b1a68a1857225c061d144dc8591ae0319a..42b14d49930dd5d3cb126eb1b26aa7c594e751ee 100644 (file)
@@ -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;
         }
     }
index 56de57cf4cc1c0cab46050fc2d3b7e1f8a23f9bb..15375e3a80f4bcc28a210468d53e5983bd64c467 100644 (file)
@@ -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);
index 260ff3b1c24d6f1f71cb7509caacd9fba70b32a3..5ddf1c7a74d7384f686748ab8a6f51cfcefafc55 100644 (file)
@@ -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) */
index e6746087ad96d870876bae0c4b91b4194ca7ca0d..0c95a7585ecc0ca8b1f5bf0e57bf1f5ba58664db 100644 (file)
@@ -235,6 +235,7 @@ int main(int argc, char *argv[])
     FUNCTION f, *fp;
     LHASH_OF(FUNCTION) *prog = NULL;
     char *pname;
+    const char *fname;
     ARGS arg;
     int ret = 0;
 
@@ -249,9 +250,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 +258,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;
@@ -288,6 +280,9 @@ int main(int argc, char *argv[])
         /* We assume we've been called as 'openssl cmd' */
         argc--;
         argv++;
+        opt_appname(argv[0]);
+    } else {
+        argv[0] = pname;
     }
 
     /* If there's a command, run with that, otherwise "help". */
@@ -360,7 +355,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++) {
index 56444baecac45adb3f5e384514a4ee319f232b2a..25c01f40884d4d0409bcb339cfc4d862c3490e5f 100644 (file)
@@ -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;
     }