From fdb653a33ea1a24d1159880624dbbcc0867865b5 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Mon, 24 Oct 2016 12:55:21 +0200 Subject: [PATCH] agent: Slightly change structure of cmd_readkey. * agent/command.c (cmd_readkey): Avoid a leave label in the middle of the code. Remove the special return. -- This helps to get better debug output. The set_error macro which is used by parse_keygrip merely sets the error code into the Assuan context. It is thus no problem anymore to call leave_cmd after having used set_error. This might havve been diffferent in the past. Signed-off-by: Werner Koch --- agent/command.c | 68 +++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/agent/command.c b/agent/command.c index 1a13084ed..999f608af 100644 --- a/agent/command.c +++ b/agent/command.c @@ -391,7 +391,9 @@ progress_cb (ctrl_t ctrl, const char *what, int printchar, } -/* Helper to print a message while leaving a command. */ +/* Helper to print a message while leaving a command. Note that this + * function does not call assuan_set_error; the caller may do this + * prior to calling us. */ static gpg_error_t leave_cmd (assuan_context_t ctx, gpg_error_t err) { @@ -1000,17 +1002,19 @@ cmd_readkey (assuan_context_t ctx, char *line) unsigned char grip[20]; gcry_sexp_t s_pkey = NULL; unsigned char *pkbuf = NULL; + char *serialno = NULL; size_t pkbuflen; + const char *opt_card; if (ctrl->restricted) return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN)); - if (has_option_name (line, "--card")) - { - const char *keyid; - char *serialno = NULL; + opt_card = has_option_name (line, "--card"); + line = skip_options (line); - keyid = skip_options (line); + if (opt_card) + { + const char *keyid = opt_card; rc = agent_card_getattr (ctrl, "SERIALNO", &serialno); if (rc) @@ -1042,35 +1046,33 @@ cmd_readkey (assuan_context_t ctx, char *line) goto leave; rc = assuan_send_data (ctx, pkbuf, pkbuflen); + } + else + { + rc = parse_keygrip (ctx, line, grip); + if (rc) + goto leave; + + rc = agent_public_key_from_file (ctrl, grip, &s_pkey); + if (!rc) + { + pkbuflen = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, NULL, 0); + log_assert (pkbuflen); + pkbuf = xtrymalloc (pkbuflen); + if (!pkbuf) + rc = gpg_error_from_syserror (); + else + { + gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, pkbuf, pkbuflen); + rc = assuan_send_data (ctx, pkbuf, pkbuflen); + } + } + } leave: - xfree (serialno); - xfree (pkbuf); - gcry_sexp_release (s_pkey); - return leave_cmd (ctx, rc); - } - - rc = parse_keygrip (ctx, line, grip); - if (rc) - return rc; /* Return immediately as this is already an Assuan error code.*/ - - rc = agent_public_key_from_file (ctrl, grip, &s_pkey); - if (!rc) - { - pkbuflen = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, NULL, 0); - assert (pkbuflen); - pkbuf = xtrymalloc (pkbuflen); - if (!pkbuf) - rc = gpg_error_from_syserror (); - else - { - gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, pkbuf, pkbuflen); - rc = assuan_send_data (ctx, pkbuf, pkbuflen); - xfree (pkbuf); - } - gcry_sexp_release (s_pkey); - } - + xfree (serialno); + xfree (pkbuf); + gcry_sexp_release (s_pkey); return leave_cmd (ctx, rc); }