NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
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)
 Implements CryptModuleSpecs::pgp_invoke_import() -. More...
 
void pgp_class_invoke_import (const char *fname)
 Implements CryptModuleSpecs::pgp_invoke_import() -. More...
 

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)

Implements CryptModuleSpecs::pgp_invoke_import() -.

Definition at line 2672 of file crypt_gpgme.c.

2673 {
2674  gpgme_ctx_t ctx = create_gpgme_context(false);
2675  gpgme_data_t keydata = NULL;
2676  gpgme_import_result_t impres;
2677  gpgme_import_status_t st;
2678  bool any;
2679 
2680  FILE *fp_in = mutt_file_fopen(fname, "r");
2681  if (!fp_in)
2682  {
2683  mutt_perror(fname);
2684  goto leave;
2685  }
2686  /* Note that the stream, "fp_in", needs to be kept open while the keydata
2687  * is used. */
2688  gpgme_error_t err = gpgme_data_new_from_stream(&keydata, fp_in);
2689  if (err != GPG_ERR_NO_ERROR)
2690  {
2691  mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
2692  goto leave;
2693  }
2694 
2695  err = gpgme_op_import(ctx, keydata);
2696  if (err != 0)
2697  {
2698  mutt_error(_("Error importing key: %s"), gpgme_strerror(err));
2699  goto leave;
2700  }
2701 
2702  /* Print infos about the imported keys to stdout. */
2703  impres = gpgme_op_import_result(ctx);
2704  if (!impres)
2705  {
2706  fputs("oops: no import result returned\n", stdout);
2707  goto leave;
2708  }
2709 
2710  for (st = impres->imports; st; st = st->next)
2711  {
2712  if (st->result)
2713  continue;
2714  printf("key %s imported (", NONULL(st->fpr));
2715  /* Note that we use the singular even if it is possible that
2716  * several uids etc are new. This simply looks better. */
2717  any = false;
2718  if (st->status & GPGME_IMPORT_SECRET)
2719  {
2720  printf("secret parts");
2721  any = true;
2722  }
2723  if ((st->status & GPGME_IMPORT_NEW))
2724  {
2725  printf("%snew key", any ? ", " : "");
2726  any = true;
2727  }
2728  if ((st->status & GPGME_IMPORT_UID))
2729  {
2730  printf("%snew uid", any ? ", " : "");
2731  any = true;
2732  }
2733  if ((st->status & GPGME_IMPORT_SIG))
2734  {
2735  printf("%snew sig", any ? ", " : "");
2736  any = true;
2737  }
2738  if ((st->status & GPGME_IMPORT_SUBKEY))
2739  {
2740  printf("%snew subkey", any ? ", " : "");
2741  any = true;
2742  }
2743  printf("%s)\n", any ? "" : "not changed");
2744  /* Fixme: Should we lookup each imported key and print more infos? */
2745  }
2746  /* Now print keys which failed the import. Unfortunately in most
2747  * cases gpg will bail out early and not tell GPGME about. */
2748  /* FIXME: We could instead use the new GPGME_AUDITLOG_DIAG to show
2749  * the actual gpg diagnostics. But I fear that would clutter the
2750  * output too much. Maybe a dedicated prompt or option to do this
2751  * would be helpful. */
2752  for (st = impres->imports; st; st = st->next)
2753  {
2754  if (st->result == 0)
2755  continue;
2756  printf("key %s import failed: %s\n", NONULL(st->fpr), gpgme_strerror(st->result));
2757  }
2758  fflush(stdout);
2759 
2760 leave:
2761  gpgme_release(ctx);
2762  gpgme_data_release(keydata);
2763  mutt_file_fclose(&fp_in);
2764 }
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:573
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:593
#define mutt_error(...)
Definition: logging.h:87
#define mutt_perror(...)
Definition: logging.h:88
#define _(a)
Definition: message.h:28
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:

◆ pgp_class_invoke_import()

void pgp_class_invoke_import ( const char *  fname)

Implements CryptModuleSpecs::pgp_invoke_import() -.

Definition at line 416 of file pgpinvoke.c.

417 {
418  char cmd[STR_COMMAND];
419  struct PgpCommandContext cctx = { 0 };
420 
421  struct Buffer *buf_fname = mutt_buffer_pool_get();
422 
423  mutt_buffer_quote_filename(buf_fname, fname, true);
424  cctx.fname = mutt_buffer_string(buf_fname);
425  const char *const c_pgp_sign_as =
426  cs_subset_string(NeoMutt->sub, "pgp_sign_as");
427  const char *const c_pgp_default_key =
428  cs_subset_string(NeoMutt->sub, "pgp_default_key");
429  if (c_pgp_sign_as)
430  cctx.signas = c_pgp_sign_as;
431  else
432  cctx.signas = c_pgp_default_key;
433 
434  const char *const c_pgp_import_command =
435  cs_subset_string(NeoMutt->sub, "pgp_import_command");
436  mutt_pgp_command(cmd, sizeof(cmd), &cctx, c_pgp_import_command);
437  if (mutt_system(cmd) != 0)
438  mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", cmd);
439 
440  mutt_buffer_pool_release(&buf_fname);
441 }
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
void mutt_buffer_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
Quote a filename to survive the shell's quoting rules.
Definition: file.c:866
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
static void mutt_pgp_command(char *buf, size_t buflen, struct PgpCommandContext *cctx, const char *fmt)
Prepare a PGP Command.
Definition: pgpinvoke.c:173
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
#define STR_COMMAND
Enough space for a long command line.
Definition: string2.h:35
String manipulation buffer.
Definition: buffer.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Data for a PGP command.
Definition: pgpinvoke.c:59
const char * signas
a
Definition: pgpinvoke.c:63
const char * fname
f
Definition: pgpinvoke.c:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function: