NeoMutt  2024-03-23-23-gec7045
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 2262 of file crypt_gpgme.c.

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

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

Definition at line 355 of file pgpinvoke.c.

356{
357 struct PgpCommandContext cctx = { 0 };
358
359 struct Buffer *buf_fname = buf_pool_get();
360 struct Buffer *cmd = buf_pool_get();
361
362 buf_quote_filename(buf_fname, fname, true);
363 cctx.fname = buf_string(buf_fname);
364 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
365 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
366 if (c_pgp_sign_as)
367 cctx.signas = c_pgp_sign_as;
368 else
369 cctx.signas = c_pgp_default_key;
370
371 const struct Expando *c_pgp_import_command = cs_subset_expando(NeoMutt->sub, "pgp_import_command");
372 mutt_pgp_command(cmd, &cctx, c_pgp_import_command);
373 if (mutt_system(buf_string(cmd)) != 0)
374 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
375
376 buf_pool_release(&buf_fname);
377 buf_pool_release(&cmd);
378}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:97
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:297
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:974
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx, const struct Expando *exp)
Prepare a PGP Command.
Definition: pgpinvoke.c:122
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
String manipulation buffer.
Definition: buffer.h:36
Parsed Expando trees.
Definition: expando.h:41
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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: