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
This commit is contained in:
Ingo Klöcker 2024-06-12 10:45:11 +02:00
parent 307256d277
commit 3b9815bd8a
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
11 changed files with 77 additions and 7 deletions

3
NEWS
View File

@ -9,6 +9,8 @@ Noteworthy changes in version 1.24.0 (unrelease)
* Add information about designated revocation keys. [T7118] * Add information about designated revocation keys. [T7118]
* New context flag "import-options". [#7152]
* cpp: Provide information about designated revocation keys for a Key. * cpp: Provide information about designated revocation keys for a Key.
[T7118] [T7118]
@ -24,6 +26,7 @@ Noteworthy changes in version 1.24.0 (unrelease)
GPGME_SIG_MODE_FILE NEW. GPGME_SIG_MODE_FILE NEW.
gpgme_key_t EXTENDED: New field 'revkeys'. gpgme_key_t EXTENDED: New field 'revkeys'.
gpgme_revocation_key_t NEW. gpgme_revocation_key_t NEW.
gpgme_set_ctx_flag EXTENDED: New flag 'import-options'.
cpp: Context::EncryptFile NEW. cpp: Context::EncryptFile NEW.
cpp: SignatureMode::SignFile NEW. cpp: SignatureMode::SignFile NEW.
cpp: RevocationKey NEW. cpp: RevocationKey NEW.

View File

@ -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 filter when importing keys. Valid values are documented in the GnuPG
manual and the gpg man page under the option @option{--import-filter}. 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" @item "no-auto-check-trustdb"
@since{1.19.0} @since{1.19.0}
Setting the @var{value} to "1" forces the GPG backend to disable the Setting the @var{value} to "1" forces the GPG backend to disable the

View File

@ -186,6 +186,9 @@ struct gpgme_context
/* The optional import filter. */ /* The optional import filter. */
char *import_filter; char *import_filter;
/* The optional import options. */
char *import_options;
/* The operation data hooked into the context. */ /* The operation data hooked into the context. */
ctx_op_data_t op_data; ctx_op_data_t op_data;

View File

@ -98,6 +98,7 @@ struct engine_ops
gpgme_key_t *keyarray, gpgme_key_t *keyarray,
const char *keyids[], const char *keyids[],
const char *import_filter, const char *import_filter,
const char *import_options,
const char *key_origin); const char *key_origin);
gpgme_error_t (*keylist) (void *engine, const char *pattern, gpgme_error_t (*keylist) (void *engine, const char *pattern,
int secret_only, gpgme_keylist_mode_t mode); int secret_only, gpgme_keylist_mode_t mode);

View File

@ -3075,7 +3075,7 @@ string_from_data (gpgme_data_t data, int delim,
static gpgme_error_t static gpgme_error_t
gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray,
const char *keyids[], const char *import_filter, const char *keyids[], const char *import_filter,
const char *key_origin) const char *import_options, const char *key_origin)
{ {
engine_gpg_t gpg = engine; engine_gpg_t gpg = engine;
gpgme_error_t err; gpgme_error_t err;
@ -3090,6 +3090,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray,
if (keyids) if (keyids)
{ {
err = add_arg (gpg, "--recv-keys"); 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")) if (!err && import_filter && have_gpg_version (gpg, "2.1.14"))
{ {
err = add_arg (gpg, "--import-filter"); 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) else if (keyarray)
{ {
err = add_arg (gpg, "--recv-keys"); 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")) if (!err && import_filter && have_gpg_version (gpg, "2.1.14"))
{ {
err = add_arg (gpg, "--import-filter"); 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 should use an option to gpg to modify such commands (ala
--multifile). */ --multifile). */
err = add_arg (gpg, "--fetch-keys"); 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")) if (!err && import_filter && have_gpg_version (gpg, "2.1.14"))
{ {
err = add_arg (gpg, "--import-filter"); err = add_arg (gpg, "--import-filter");
@ -3165,6 +3183,12 @@ gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray,
else else
{ {
err = add_arg (gpg, "--import"); 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")) if (!err && import_filter && have_gpg_version (gpg, "2.1.14"))
{ {
err = add_arg (gpg, "--import-filter"); err = add_arg (gpg, "--import-filter");

View File

@ -1795,7 +1795,7 @@ gpgsm_genkey (void *engine,
static gpgme_error_t static gpgme_error_t
gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray, gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray,
const char *keyids[], const char *import_filter, const char *keyids[], const char *import_filter,
const char *key_origin) const char *import_options, const char *key_origin)
{ {
engine_gpgsm_t gpgsm = engine; engine_gpgsm_t gpgsm = engine;
gpgme_error_t err; gpgme_error_t err;
@ -1803,6 +1803,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray,
int idx; int idx;
(void)import_filter; (void)import_filter;
(void)import_options;
(void)key_origin; (void)key_origin;
if (!gpgsm) if (!gpgsm)

View File

@ -858,7 +858,8 @@ _gpgme_engine_op_tofu_policy (engine_t engine,
gpgme_error_t gpgme_error_t
_gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata,
gpgme_key_t *keyarray, const char *keyids[], 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) if (!engine)
return gpg_error (GPG_ERR_INV_VALUE); 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 gpg_error (GPG_ERR_NOT_IMPLEMENTED);
return (*engine->ops->import) (engine->engine, keydata, keyarray, keyids, return (*engine->ops->import) (engine->engine, keydata, keyarray, keyids,
import_filter, key_origin); import_filter, import_options, key_origin);
} }

View File

@ -144,6 +144,7 @@ gpgme_error_t _gpgme_engine_op_import (engine_t engine,
gpgme_key_t *keyarray, gpgme_key_t *keyarray,
const char *keyids[], const char *keyids[],
const char *import_filter, const char *import_filter,
const char *import_options,
const char *key_origin); const char *key_origin);
gpgme_error_t _gpgme_engine_op_keylist (engine_t engine, gpgme_error_t _gpgme_engine_op_keylist (engine_t engine,
const char *pattern, const char *pattern,

View File

@ -261,6 +261,7 @@ gpgme_release (gpgme_ctx_t ctx)
free (ctx->cert_expire); free (ctx->cert_expire);
free (ctx->key_origin); free (ctx->key_origin);
free (ctx->import_filter); free (ctx->import_filter);
free (ctx->import_options);
_gpgme_engine_info_release (ctx->engine_info); _gpgme_engine_info_release (ctx->engine_info);
ctx->engine_info = NULL; ctx->engine_info = NULL;
DESTROY_LOCK (ctx->lock); 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) if (!ctx->import_filter)
err = gpg_error_from_syserror (); 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")) else if (!strcmp (name, "no-auto-check-trustdb"))
{ {
ctx->no_auto_check_trustdb = abool; 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 : ""; 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")) else if (!strcmp (name, "no-auto-check-trustdb"))
{ {
return ctx->no_auto_check_trustdb? "1":""; return ctx->no_auto_check_trustdb? "1":"";

View File

@ -368,7 +368,8 @@ _gpgme_op_import_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t keydata)
ctx); ctx);
return _gpgme_engine_op_import (ctx->engine, keydata, NULL, NULL, 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); ctx);
return _gpgme_engine_op_import (ctx->engine, NULL, keys, NULL, 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); ctx);
return _gpgme_engine_op_import (ctx->engine, NULL, NULL, keyids, 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);
} }

View File

@ -48,6 +48,7 @@ show_usage (int ex)
" --cms use the CMS protocol\n" " --cms use the CMS protocol\n"
" --offline use offline mode\n" " --offline use offline mode\n"
" --key-origin use the specified key origin\n" " --key-origin use the specified key origin\n"
" --import-options use the specified import options\n"
" --url import from given URLs\n" " --url import from given URLs\n"
" -0 URLs are delimited by a nul\n" " -0 URLs are delimited by a nul\n"
, stderr); , stderr);
@ -65,6 +66,7 @@ main (int argc, char **argv)
gpgme_import_result_t impres; gpgme_import_result_t impres;
gpgme_data_t data; gpgme_data_t data;
gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
char *import_options = NULL;
char *import_filter = NULL; char *import_filter = NULL;
char *key_origin = NULL; char *key_origin = NULL;
int offline = 0; int offline = 0;
@ -106,6 +108,14 @@ main (int argc, char **argv)
protocol = GPGME_PROTOCOL_CMS; protocol = GPGME_PROTOCOL_CMS;
argc--; argv++; 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")) else if (!strcmp (*argv, "--import-filter"))
{ {
argc--; argv++; argc--; argv++;
@ -143,6 +153,11 @@ main (int argc, char **argv)
gpgme_set_offline (ctx, offline); 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) if (import_filter)
{ {
err = gpgme_set_ctx_flag (ctx, "import-filter", import_filter); err = gpgme_set_ctx_flag (ctx, "import-filter", import_filter);