From 3b9815bd8ad723445128ae99883be0072ae389a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Wed, 12 Jun 2024 10:45:11 +0200 Subject: [PATCH] core: Allow setting import options when importing keys * src/context.h (struct gpgme_context): New field import_options. * src/engine-backend.h (struct engine_ops): Add arg import_options to field 'import'. * src/engine-gpg.c (gpg_import): Add arg import_options and pass option --import-options with argument value to gpg. * src/engine-gpgsm.c (gpgsm_import): Add dummy arg import_options. * src/engine.c (_gpgme_engine_op_import): Add arg import_options and pass option to import function of engine. * src/engine.h (_gpgme_engine_op_import): Add arg import_options. * src/gpgme.c (gpgme_release): Free 'import_options'. (gpgme_set_ctx_flag, gpgme_get_ctx_flag): New flag "import-options". * src/import.c (_gpgme_op_import_start, _gpgme_op_import_keys_start, _gpgme_op_receive_keys_start): Pass import options stored in context to _gpgme_engine_op_import. * tests/run-import.c (show_usage, main): Add option --import-options. -- This makes the --import-options option available in the GPGME API for key imports. GnuPG-bug-id: 7152 --- NEWS | 3 +++ doc/gpgme.texi | 6 ++++++ src/context.h | 3 +++ src/engine-backend.h | 1 + src/engine-gpg.c | 26 +++++++++++++++++++++++++- src/engine-gpgsm.c | 3 ++- src/engine.c | 5 +++-- src/engine.h | 1 + src/gpgme.c | 12 ++++++++++++ src/import.c | 9 ++++++--- tests/run-import.c | 15 +++++++++++++++ 11 files changed, 77 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index b7309454..7335e198 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,8 @@ Noteworthy changes in version 1.24.0 (unrelease) * Add information about designated revocation keys. [T7118] + * New context flag "import-options". [#7152] + * cpp: Provide information about designated revocation keys for a Key. [T7118] @@ -24,6 +26,7 @@ Noteworthy changes in version 1.24.0 (unrelease) GPGME_SIG_MODE_FILE NEW. gpgme_key_t EXTENDED: New field 'revkeys'. gpgme_revocation_key_t NEW. + gpgme_set_ctx_flag EXTENDED: New flag 'import-options'. cpp: Context::EncryptFile NEW. cpp: SignatureMode::SignFile NEW. cpp: RevocationKey NEW. diff --git a/doc/gpgme.texi b/doc/gpgme.texi index 45dc9e9b..b2bf6f8c 100644 --- a/doc/gpgme.texi +++ b/doc/gpgme.texi @@ -3279,6 +3279,12 @@ The string given in @var{value} is passed to the GnuPG engine to use as filter when importing keys. Valid values are documented in the GnuPG manual and the gpg man page under the option @option{--import-filter}. +@item "import-options" +@since{1.24.0} +The string given in @var{value} is passed to the GnuPG engine to use as +options when importing keys. Valid values are documented in the GnuPG +manual and the gpg man page under the option @option{--import-options}. + @item "no-auto-check-trustdb" @since{1.19.0} Setting the @var{value} to "1" forces the GPG backend to disable the diff --git a/src/context.h b/src/context.h index 7a1b9ada..454625e7 100644 --- a/src/context.h +++ b/src/context.h @@ -186,6 +186,9 @@ struct gpgme_context /* The optional import filter. */ char *import_filter; + /* The optional import options. */ + char *import_options; + /* The operation data hooked into the context. */ ctx_op_data_t op_data; diff --git a/src/engine-backend.h b/src/engine-backend.h index 850ffaa9..8c98e5a3 100644 --- a/src/engine-backend.h +++ b/src/engine-backend.h @@ -98,6 +98,7 @@ struct engine_ops gpgme_key_t *keyarray, const char *keyids[], const char *import_filter, + const char *import_options, const char *key_origin); gpgme_error_t (*keylist) (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode); diff --git a/src/engine-gpg.c b/src/engine-gpg.c index fd7064b5..376dad0a 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -3075,7 +3075,7 @@ string_from_data (gpgme_data_t data, int delim, static gpgme_error_t gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, const char *keyids[], const char *import_filter, - const char *key_origin) + const char *import_options, const char *key_origin) { engine_gpg_t gpg = engine; gpgme_error_t err; @@ -3090,6 +3090,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, if (keyids) { err = add_arg (gpg, "--recv-keys"); + if (!err && import_options) + { + err = add_arg (gpg, "--import-options"); + if (!err) + err = add_arg (gpg, import_options); + } if (!err && import_filter && have_gpg_version (gpg, "2.1.14")) { err = add_arg (gpg, "--import-filter"); @@ -3104,6 +3110,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, else if (keyarray) { err = add_arg (gpg, "--recv-keys"); + if (!err && import_options) + { + err = add_arg (gpg, "--import-options"); + if (!err) + err = add_arg (gpg, import_options); + } if (!err && import_filter && have_gpg_version (gpg, "2.1.14")) { err = add_arg (gpg, "--import-filter"); @@ -3141,6 +3153,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, should use an option to gpg to modify such commands (ala --multifile). */ err = add_arg (gpg, "--fetch-keys"); + if (!err && import_options) + { + err = add_arg (gpg, "--import-options"); + if (!err) + err = add_arg (gpg, import_options); + } if (!err && import_filter && have_gpg_version (gpg, "2.1.14")) { err = add_arg (gpg, "--import-filter"); @@ -3165,6 +3183,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, else { err = add_arg (gpg, "--import"); + if (!err && import_options) + { + err = add_arg (gpg, "--import-options"); + if (!err) + err = add_arg (gpg, import_options); + } if (!err && import_filter && have_gpg_version (gpg, "2.1.14")) { err = add_arg (gpg, "--import-filter"); diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index fa89ae3f..8ee29de8 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -1795,7 +1795,7 @@ gpgsm_genkey (void *engine, static gpgme_error_t gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, const char *keyids[], const char *import_filter, - const char *key_origin) + const char *import_options, const char *key_origin) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; @@ -1803,6 +1803,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, int idx; (void)import_filter; + (void)import_options; (void)key_origin; if (!gpgsm) diff --git a/src/engine.c b/src/engine.c index 252d8805..f1efdf34 100644 --- a/src/engine.c +++ b/src/engine.c @@ -858,7 +858,8 @@ _gpgme_engine_op_tofu_policy (engine_t engine, gpgme_error_t _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, gpgme_key_t *keyarray, const char *keyids[], - const char *import_filter, const char *key_origin) + const char *import_filter, const char *import_options, + const char *key_origin) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); @@ -867,7 +868,7 @@ _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->import) (engine->engine, keydata, keyarray, keyids, - import_filter, key_origin); + import_filter, import_options, key_origin); } diff --git a/src/engine.h b/src/engine.h index 654deddf..ae57cbed 100644 --- a/src/engine.h +++ b/src/engine.h @@ -144,6 +144,7 @@ gpgme_error_t _gpgme_engine_op_import (engine_t engine, gpgme_key_t *keyarray, const char *keyids[], const char *import_filter, + const char *import_options, const char *key_origin); gpgme_error_t _gpgme_engine_op_keylist (engine_t engine, const char *pattern, diff --git a/src/gpgme.c b/src/gpgme.c index 135a75a7..f6d7f38d 100644 --- a/src/gpgme.c +++ b/src/gpgme.c @@ -261,6 +261,7 @@ gpgme_release (gpgme_ctx_t ctx) free (ctx->cert_expire); free (ctx->key_origin); free (ctx->import_filter); + free (ctx->import_options); _gpgme_engine_info_release (ctx->engine_info); ctx->engine_info = NULL; DESTROY_LOCK (ctx->lock); @@ -607,6 +608,13 @@ gpgme_set_ctx_flag (gpgme_ctx_t ctx, const char *name, const char *value) if (!ctx->import_filter) err = gpg_error_from_syserror (); } + else if (!strcmp (name, "import-options")) + { + free (ctx->import_options); + ctx->import_options = strdup (value); + if (!ctx->import_options) + err = gpg_error_from_syserror (); + } else if (!strcmp (name, "no-auto-check-trustdb")) { ctx->no_auto_check_trustdb = abool; @@ -692,6 +700,10 @@ gpgme_get_ctx_flag (gpgme_ctx_t ctx, const char *name) { return ctx->import_filter? ctx->import_filter : ""; } + else if (!strcmp (name, "import-options")) + { + return ctx->import_options? ctx->import_options : ""; + } else if (!strcmp (name, "no-auto-check-trustdb")) { return ctx->no_auto_check_trustdb? "1":""; diff --git a/src/import.c b/src/import.c index f288a811..d8c9adcb 100644 --- a/src/import.c +++ b/src/import.c @@ -368,7 +368,8 @@ _gpgme_op_import_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t keydata) ctx); return _gpgme_engine_op_import (ctx->engine, keydata, NULL, NULL, - ctx->import_filter, ctx->key_origin); + ctx->import_filter, ctx->import_options, + ctx->key_origin); } @@ -448,7 +449,8 @@ _gpgme_op_import_keys_start (gpgme_ctx_t ctx, int synchronous, ctx); return _gpgme_engine_op_import (ctx->engine, NULL, keys, NULL, - ctx->import_filter, ctx->key_origin); + ctx->import_filter, ctx->import_options, + ctx->key_origin); } @@ -541,7 +543,8 @@ _gpgme_op_receive_keys_start (gpgme_ctx_t ctx, int synchronous, const char *keyi ctx); return _gpgme_engine_op_import (ctx->engine, NULL, NULL, keyids, - ctx->import_filter, ctx->key_origin); + ctx->import_filter, ctx->import_options, + ctx->key_origin); } diff --git a/tests/run-import.c b/tests/run-import.c index b0320ef8..0e9a300f 100644 --- a/tests/run-import.c +++ b/tests/run-import.c @@ -48,6 +48,7 @@ show_usage (int ex) " --cms use the CMS protocol\n" " --offline use offline mode\n" " --key-origin use the specified key origin\n" + " --import-options use the specified import options\n" " --url import from given URLs\n" " -0 URLs are delimited by a nul\n" , stderr); @@ -65,6 +66,7 @@ main (int argc, char **argv) gpgme_import_result_t impres; gpgme_data_t data; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; + char *import_options = NULL; char *import_filter = NULL; char *key_origin = NULL; int offline = 0; @@ -106,6 +108,14 @@ main (int argc, char **argv) protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } + else if (!strcmp (*argv, "--import-options")) + { + argc--; argv++; + if (!argc) + show_usage (1); + import_options = strdup (*argv); + argc--; argv++; + } else if (!strcmp (*argv, "--import-filter")) { argc--; argv++; @@ -143,6 +153,11 @@ main (int argc, char **argv) gpgme_set_offline (ctx, offline); + if (import_options) + { + err = gpgme_set_ctx_flag (ctx, "import-options", import_options); + fail_if_err (err); + } if (import_filter) { err = gpgme_set_ctx_flag (ctx, "import-filter", import_filter);