NeoMutt  2023-05-17-33-gce4425
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 2264 of file crypt_gpgme.c.

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

419{
420 char cmd[STR_COMMAND] = { 0 };
421 struct PgpCommandContext cctx = { 0 };
422
423 struct Buffer *buf_fname = buf_pool_get();
424
425 buf_quote_filename(buf_fname, fname, true);
426 cctx.fname = buf_string(buf_fname);
427 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
428 const char *const c_pgp_default_key = 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 = cs_subset_string(NeoMutt->sub, "pgp_import_command");
435 mutt_pgp_command(cmd, sizeof(cmd), &cctx, c_pgp_import_command);
436 if (mutt_system(cmd) != 0)
437 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", cmd);
438
439 buf_pool_release(&buf_fname);
440}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
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:911
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.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:186
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:106
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:119
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52
#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: