NeoMutt  2025-12-11-1059-g2f5469
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_invoke_import()

Import a key from a message into the user's public key ring. More...

Collaboration diagram for pgp_invoke_import():

Functions

void pgp_gpgme_invoke_import (const char *fname)
 Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke_import() -.
void pgp_class_invoke_import (const char *fname)
 Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke_import() -.

Detailed Description

Import a key from a message into the user's public key ring.

Parameters
fnameFile containing the message

Function Documentation

◆ pgp_gpgme_invoke_import()

void pgp_gpgme_invoke_import ( const char * fname)

Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke_import() -.

Definition at line 2341 of file crypt_gpgme.c.

2342{
2343 gpgme_ctx_t ctx = create_gpgme_context(false);
2344 gpgme_data_t keydata = NULL;
2345 gpgme_import_result_t impres = NULL;
2346 gpgme_import_status_t st = NULL;
2347 bool any;
2348
2349 FILE *fp_in = mutt_file_fopen(fname, "r");
2350 if (!fp_in)
2351 {
2352 mutt_perror("%s", fname);
2353 goto leave;
2354 }
2355 /* Note that the stream, "fp_in", needs to be kept open while the keydata
2356 * is used. */
2357 gpgme_error_t err = gpgme_data_new_from_stream(&keydata, fp_in);
2358 if (err != GPG_ERR_NO_ERROR)
2359 {
2360 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
2361 goto leave;
2362 }
2363
2364 err = gpgme_op_import(ctx, keydata);
2365 if (err != GPG_ERR_NO_ERROR)
2366 {
2367 mutt_error(_("Error importing key: %s"), gpgme_strerror(err));
2368 goto leave;
2369 }
2370
2371 /* Print infos about the imported keys to stdout. */
2372 impres = gpgme_op_import_result(ctx);
2373 if (!impres)
2374 {
2375 fputs("oops: no import result returned\n", stdout);
2376 goto leave;
2377 }
2378
2379 for (st = impres->imports; st; st = st->next)
2380 {
2381 if (st->result)
2382 continue;
2383 printf("key %s imported (", NONULL(st->fpr));
2384 /* Note that we use the singular even if it is possible that
2385 * several uids etc are new. This simply looks better. */
2386 any = false;
2387 if (st->status & GPGME_IMPORT_SECRET)
2388 {
2389 printf("secret parts");
2390 any = true;
2391 }
2392 if ((st->status & GPGME_IMPORT_NEW))
2393 {
2394 printf("%snew key", any ? ", " : "");
2395 any = true;
2396 }
2397 if ((st->status & GPGME_IMPORT_UID))
2398 {
2399 printf("%snew uid", any ? ", " : "");
2400 any = true;
2401 }
2402 if ((st->status & GPGME_IMPORT_SIG))
2403 {
2404 printf("%snew sig", any ? ", " : "");
2405 any = true;
2406 }
2407 if ((st->status & GPGME_IMPORT_SUBKEY))
2408 {
2409 printf("%snew subkey", any ? ", " : "");
2410 any = true;
2411 }
2412 printf("%s)\n", any ? "" : "not changed");
2413 /* Fixme: Should we lookup each imported key and print more infos? */
2414 }
2415 /* Now print keys which failed the import. Unfortunately in most
2416 * cases gpg will bail out early and not tell GPGME about. */
2417 /* FIXME: We could instead use the new GPGME_AUDITLOG_DIAG to show
2418 * the actual gpg diagnostics. But I fear that would clutter the
2419 * output too much. Maybe a dedicated prompt or option to do this
2420 * would be helpful. */
2421 for (st = impres->imports; st; st = st->next)
2422 {
2423 if (st->result == 0)
2424 continue;
2425 printf("key %s import failed: %s\n", NONULL(st->fpr), gpgme_strerror(st->result));
2426 }
2427 fflush(stdout);
2428
2429leave:
2430 gpgme_release(ctx);
2431 gpgme_data_release(keydata);
2432 mutt_file_fclose(&fp_in);
2433}
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
#define mutt_error(...)
Definition logging2.h:94
#define mutt_perror(...)
Definition logging2.h:95
#define _(a)
Definition message.h:28
#define NONULL(x)
Definition string2.h:44
Here is the call graph for this function:

◆ pgp_class_invoke_import()

void pgp_class_invoke_import ( const char * fname)

Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke_import() -.

Definition at line 287 of file pgpinvoke.c.

288{
289 struct PgpCommandContext cctx = { 0 };
290
291 struct Buffer *buf_fname = buf_pool_get();
292 struct Buffer *cmd = buf_pool_get();
293
294 buf_quote_filename(buf_fname, fname, true);
295 cctx.fname = buf_string(buf_fname);
296 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
297 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
298 if (c_pgp_sign_as)
299 cctx.signas = c_pgp_sign_as;
300 else
301 cctx.signas = c_pgp_default_key;
302
303 const struct Expando *c_pgp_import_command = cs_subset_expando(NeoMutt->sub, "pgp_import_command");
304 mutt_pgp_command(cmd, &cctx, c_pgp_import_command);
305 if (mutt_system(buf_string(cmd)) != 0)
306 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
307
308 buf_pool_release(&buf_fname);
309 buf_pool_release(&cmd);
310}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:99
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
void buf_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
Quote a filename to survive the shell's quoting rules.
Definition file.c:803
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
int mutt_system(const char *cmd)
Run an external command.
Definition system.c:51
static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx, const struct Expando *exp)
Prepare a PGP Command.
Definition pgpinvoke.c:60
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:37
Parsed Expando trees.
Definition expando.h:41
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Data for a PGP command.
Definition pgp.h:43
const char * signas
a
Definition pgp.h:47
const char * fname
f
Definition pgp.h:45
Here is the call graph for this function:
Here is the caller graph for this function: