NeoMutt  2022-04-29-323-g5fcc6c
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 2248 of file crypt_gpgme.c.

2249{
2250 gpgme_ctx_t ctx = create_gpgme_context(false);
2251 gpgme_data_t keydata = NULL;
2252 gpgme_import_result_t impres = NULL;
2253 gpgme_import_status_t st = NULL;
2254 bool any;
2255
2256 FILE *fp_in = mutt_file_fopen(fname, "r");
2257 if (!fp_in)
2258 {
2259 mutt_perror(fname);
2260 goto leave;
2261 }
2262 /* Note that the stream, "fp_in", needs to be kept open while the keydata
2263 * is used. */
2264 gpgme_error_t err = gpgme_data_new_from_stream(&keydata, fp_in);
2265 if (err != GPG_ERR_NO_ERROR)
2266 {
2267 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
2268 goto leave;
2269 }
2270
2271 err = gpgme_op_import(ctx, keydata);
2272 if (err != 0)
2273 {
2274 mutt_error(_("Error importing key: %s"), gpgme_strerror(err));
2275 goto leave;
2276 }
2277
2278 /* Print infos about the imported keys to stdout. */
2279 impres = gpgme_op_import_result(ctx);
2280 if (!impres)
2281 {
2282 fputs("oops: no import result returned\n", stdout);
2283 goto leave;
2284 }
2285
2286 for (st = impres->imports; st; st = st->next)
2287 {
2288 if (st->result)
2289 continue;
2290 printf("key %s imported (", NONULL(st->fpr));
2291 /* Note that we use the singular even if it is possible that
2292 * several uids etc are new. This simply looks better. */
2293 any = false;
2294 if (st->status & GPGME_IMPORT_SECRET)
2295 {
2296 printf("secret parts");
2297 any = true;
2298 }
2299 if ((st->status & GPGME_IMPORT_NEW))
2300 {
2301 printf("%snew key", any ? ", " : "");
2302 any = true;
2303 }
2304 if ((st->status & GPGME_IMPORT_UID))
2305 {
2306 printf("%snew uid", any ? ", " : "");
2307 any = true;
2308 }
2309 if ((st->status & GPGME_IMPORT_SIG))
2310 {
2311 printf("%snew sig", any ? ", " : "");
2312 any = true;
2313 }
2314 if ((st->status & GPGME_IMPORT_SUBKEY))
2315 {
2316 printf("%snew subkey", any ? ", " : "");
2317 any = true;
2318 }
2319 printf("%s)\n", any ? "" : "not changed");
2320 /* Fixme: Should we lookup each imported key and print more infos? */
2321 }
2322 /* Now print keys which failed the import. Unfortunately in most
2323 * cases gpg will bail out early and not tell GPGME about. */
2324 /* FIXME: We could instead use the new GPGME_AUDITLOG_DIAG to show
2325 * the actual gpg diagnostics. But I fear that would clutter the
2326 * output too much. Maybe a dedicated prompt or option to do this
2327 * would be helpful. */
2328 for (st = impres->imports; st; st = st->next)
2329 {
2330 if (st->result == 0)
2331 continue;
2332 printf("key %s import failed: %s\n", NONULL(st->fpr), gpgme_strerror(st->result));
2333 }
2334 fflush(stdout);
2335
2336leave:
2337 gpgme_release(ctx);
2338 gpgme_data_release(keydata);
2339 mutt_file_fclose(&fp_in);
2340}
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:358
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:151
#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 408 of file pgpinvoke.c.

409{
410 char cmd[STR_COMMAND] = { 0 };
411 struct PgpCommandContext cctx = { 0 };
412
413 struct Buffer *buf_fname = mutt_buffer_pool_get();
414
415 mutt_buffer_quote_filename(buf_fname, fname, true);
416 cctx.fname = mutt_buffer_string(buf_fname);
417 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
418 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
419 if (c_pgp_sign_as)
420 cctx.signas = c_pgp_sign_as;
421 else
422 cctx.signas = c_pgp_default_key;
423
424 const char *const c_pgp_import_command = cs_subset_string(NeoMutt->sub, "pgp_import_command");
425 mutt_pgp_command(cmd, sizeof(cmd), &cctx, c_pgp_import_command);
426 if (mutt_system(cmd) != 0)
427 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", cmd);
428
429 mutt_buffer_pool_release(&buf_fname);
430}
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
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:907
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ 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:176
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:60
const char * signas
a
Definition: pgpinvoke.c:64
const char * fname
f
Definition: pgpinvoke.c:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function: