From cd2d685230ecafb7df504ef2b16cf1ec9a014300 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Thu, 5 Nov 2015 13:59:33 +0100 Subject: [PATCH] tools: Fix gpgkey2ssh's most gratuitous errors. Use gpg2, not gpg. * tools/gpgkey2ssh.c (main): Add support for --help. Replace the most gratuitous asserts with error messages. Invoke gpg2, not gpg. -- Signed-off-by: Neal H. Walfield Debian-bug-id: 380241 --- tools/gpgkey2ssh.c | 47 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/tools/gpgkey2ssh.c b/tools/gpgkey2ssh.c index d22c5acf2..20541509c 100644 --- a/tools/gpgkey2ssh.c +++ b/tools/gpgkey2ssh.c @@ -248,7 +248,7 @@ main (int argc, char **argv) int algorithm_id; pkdbuf_t *pkdbuf; size_t pkdbuf_n; - char *command; + char *command = NULL; FILE *fp; int ret; gcry_error_t err; @@ -263,21 +263,50 @@ main (int argc, char **argv) algorithm_id = 0; /* (avoid cc warning) */ identifier = NULL; /* (avoid cc warning) */ - assert (argc == 2); + if (argc != 2) + { + fprintf (stderr, "Usage: %s KEYID\n", argv[0]); + exit (1); + } + if (strcmp (argv[1], "--help") == 0) + { + fprintf (stderr, "Usage: %s KEYID\n", argv[0]); + fprintf (stderr, "\n"); + fprintf (stderr, + "Convert a gpg key to a format appropriate for inclusion in an\n" + "ssh authorized_keys file.\n"); + exit (0); + } keyid = argv[1]; - ret = asprintf (&command, - "gpg --list-keys --with-colons --with-key-data '%s'", - keyid); - assert (ret > 0); + asprintf (&command, + "gpg2 --list-keys --with-colons --with-key-data '%s'", + keyid); + if (! command) + { + fprintf (stderr, "Out of memory.\n"); + exit (1); + } fp = popen (command, "r"); - assert (fp); + if (! fp) + { + fprintf (stderr, "Failed to running: '%s'\n", command); + exit (1); + } err = retrieve_key_material (fp, keyid, &algorithm_id, &pkdbuf, &pkdbuf_n); - assert (! err); - assert ((algorithm_id == 1) || (algorithm_id == 17)); + if (err) + { + fprintf (stderr, "Error looking up key: %s\n", gpg_strerror (err)); + exit (1); + } + if (! ((algorithm_id == 1) || (algorithm_id == 17))) + { + fprintf (stderr, "Unsupported algorithm: %d\n", algorithm_id); + exit (1); + } if (algorithm_id == 1) {