From 1024884e07f750ce781fa74dffa62e126bdda622 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 4 Jun 2019 09:29:09 +0200 Subject: [PATCH] core: Implement recpstring option parsing for gpgsm. * src/engine-gpg.c (append_args_from_recipients_string): Detect bad options. * src/engine-gpgsm.c (set_recipients_from_string): Implement option parsing. -- The only option we actually implement is "--" but the code layout is now very simlar to engine-gpg and can easily be extended if ever needed. Signed-off-by: Werner Koch --- src/engine-gpg.c | 2 ++ src/engine-gpgsm.c | 40 ++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/engine-gpg.c b/src/engine-gpg.c index 31d219a3..dc2d9455 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -2075,6 +2075,8 @@ append_args_from_recipients_string (engine_gpg_t gpg, file = 0; flags = orig_flags; } + else if (!ignore && n > 2 && !memcmp (string, "--", 2)) + err = gpg_error (GPG_ERR_UNKNOWN_OPTION); else if (n) /* Not empty - use it. */ { err = add_arg (gpg, file? (hidden? "-F":"-f") : (hidden? "-R":"-r")); diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index d20da655..62920473 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -1407,11 +1407,12 @@ set_recipients_from_string (engine_gpgsm_t gpgsm, const char *string) { gpg_error_t err = 0; char *line = NULL; - int no_pubkey = 0; + int ignore = 0; + int any = 0; const char *s; int n; - for (;;) + do { while (*string == ' ' || *string == '\t') string++; @@ -1426,25 +1427,32 @@ set_recipients_from_string (engine_gpgsm_t gpgsm, const char *string) while (n && (string[n-1] == ' ' || string[n-1] == '\t')) n--; - gpgrt_free (line); - if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) + if (!ignore && n == 2 && !memcmp (string, "--", 2)) + ignore = 1; + else if (!ignore && n > 2 && !memcmp (string, "--", 2)) + err = gpg_error (GPG_ERR_UNKNOWN_OPTION); + else if (n) /* Not empty - use it. */ { - err = gpg_error_from_syserror (); - break; + gpgrt_free (line); + if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) + err = gpg_error_from_syserror (); + else + { + err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, + gpgsm->status.fnc_value); + if (!err) + any = 1; + } } + string += n + !!s; - - err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, - gpgsm->status.fnc_value); - - /* Fixme: Improve error reporting. */ - if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) - no_pubkey++; - else if (err) - break; } + while (!err); + + if (!err && !any) + err = gpg_error (GPG_ERR_MISSING_KEY); gpgrt_free (line); - return err? err : no_pubkey? gpg_error (GPG_ERR_NO_PUBKEY) : 0; + return err; }