From 69e0b080f06b66eee96327617c6fbffe8a88d586 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 7 May 2019 11:50:38 +0200 Subject: agent: If a Label is make sure that label is part of the prompt. * agent/findkey.c (has_comment_expando): New. (agent_key_from_file): Modify DESC_TEXT. -- A Label entry in the keyfile is always set manually and thus we can assume that the user wants to have this label in the prompt. In case the prompt template does not demand a comment this patch appends a comment to thhe template. This is a common case for on-disk keys used by gpg. Signed-off-by: Werner Koch --- agent/findkey.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/agent/findkey.c b/agent/findkey.c index 5699a215e..755a90be1 100644 --- a/agent/findkey.c +++ b/agent/findkey.c @@ -401,6 +401,33 @@ try_unprotect_cb (struct pin_entry_info_s *pi) } +/* Return true if the STRING has an %C or %c expando. */ +static int +has_comment_expando (const char *string) +{ + const char *s; + int percent = 0; + + if (!string) + return 0; + + for (s = string; *s; s++) + { + if (percent) + { + if (*s == 'c' || *s == 'C') + return 1; + percent = 0; + } + else if (*s == '%') + percent = 1; + } + return 0; +} + + + + /* Modify a Key description, replacing certain special format characters. List of currently supported replacements: @@ -946,6 +973,7 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce, size_t len, buflen, erroff; gcry_sexp_t s_skey; nvc_t keymeta = NULL; + char *desc_text_buffer = NULL; /* Used in case we extend DESC_TEXT. */ *result = NULL; if (shadow_info) @@ -968,6 +996,7 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce, if (err) { nvc_release (keymeta); + xfree (desc_text_buffer); return err; } @@ -1006,6 +1035,14 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce, if (strchr (comment, '\n') && (comment_buffer = linefeed_to_percent0A (comment))) comment = comment_buffer; + /* In case DESC_TEXT has no escape pattern for a comment + * we append one. */ + if (desc_text && !has_comment_expando (desc_text)) + { + desc_text_buffer = strconcat (desc_text, "%0A%C", NULL); + if (desc_text_buffer) + desc_text = desc_text_buffer; + } } else { @@ -1078,6 +1115,7 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce, *r_passphrase = NULL; } nvc_release (keymeta); + xfree (desc_text_buffer); return err; } @@ -1095,11 +1133,13 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce, *r_passphrase = NULL; } nvc_release (keymeta); + xfree (desc_text_buffer); return err; } *result = s_skey; nvc_release (keymeta); + xfree (desc_text_buffer); return 0; } -- cgit v1.2.3