NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgpinvoke.c
Go to the documentation of this file.
1
33#include "config.h"
34#include <fcntl.h>
35#include <stdbool.h>
36#include <stdio.h>
37#include <unistd.h>
38#include "mutt/lib.h"
39#include "address/lib.h"
40#include "config/lib.h"
41#include "core/lib.h"
42#include "gui/lib.h"
43#include "pgpinvoke.h"
44#include "lib.h"
45#include "expando/lib.h"
46#include "expando_command.h"
47#include "globals.h"
48#include "mutt_logging.h"
49#include "pgpkey.h"
50#include "protos.h"
51#ifdef CRYPT_BACKEND_CLASSIC_PGP
52#include "pgp.h"
53#endif
54
61static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx,
62 const struct Expando *exp)
63{
65 mutt_debug(LL_DEBUG2, "%s\n", buf_string(buf));
66}
67
87static pid_t pgp_invoke(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
88 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
89 bool need_passphrase, const char *fname, const char *sig_fname,
90 const char *ids, const struct Expando *exp)
91{
92 struct PgpCommandContext cctx = { 0 };
93
94 if (!exp)
95 return (pid_t) -1;
96
98 cctx.fname = fname;
99 cctx.sig_fname = sig_fname;
100 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
101 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
102 if (c_pgp_sign_as)
103 cctx.signas = c_pgp_sign_as;
104 else
105 cctx.signas = c_pgp_default_key;
106 cctx.ids = ids;
107
108 struct Buffer *cmd = buf_pool_get();
109 mutt_pgp_command(cmd, &cctx, exp);
110
111 pid_t pid = filter_create_fd(buf_string(cmd), fp_pgp_in, fp_pgp_out, fp_pgp_err,
112 fd_pgp_in, fd_pgp_out, fd_pgp_err, EnvList);
113 buf_pool_release(&cmd);
114 return pid;
115}
116
133pid_t pgp_invoke_decode(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
134 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
135 const char *fname, bool need_passphrase)
136{
137 const struct Expando *c_pgp_decode_command = cs_subset_expando(NeoMutt->sub, "pgp_decode_command");
138 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out, fd_pgp_err,
139 need_passphrase, fname, NULL, NULL, c_pgp_decode_command);
140}
141
158pid_t pgp_invoke_verify(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
159 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
160 const char *fname, const char *sig_fname)
161{
162 const struct Expando *c_pgp_verify_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_command");
163 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
164 fd_pgp_err, false, fname, sig_fname, NULL, c_pgp_verify_command);
165}
166
182pid_t pgp_invoke_decrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
183 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
184{
185 const struct Expando *c_pgp_decrypt_command = cs_subset_expando(NeoMutt->sub, "pgp_decrypt_command");
186 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
187 fd_pgp_err, true, fname, NULL, NULL, c_pgp_decrypt_command);
188}
189
205pid_t pgp_invoke_sign(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
206 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
207{
208 const struct Expando *c_pgp_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_sign_command");
209 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
210 fd_pgp_err, true, fname, NULL, NULL, c_pgp_sign_command);
211}
212
230pid_t pgp_invoke_encrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
231 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
232 const char *fname, const char *uids, bool sign)
233{
234 if (sign)
235 {
236 const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
237 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
238 fd_pgp_err, true, fname, NULL, uids, c_pgp_encrypt_sign_command);
239 }
240 else
241 {
242 const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
243 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
244 fd_pgp_err, false, fname, NULL, uids, c_pgp_encrypt_only_command);
245 }
246}
247
265pid_t pgp_invoke_traditional(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
266 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
267 const char *fname, const char *uids, SecurityFlags flags)
268{
269 if (flags & SEC_ENCRYPT)
270 {
271 const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
272 const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
273 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
274 fd_pgp_err, (flags & SEC_SIGN), fname, NULL, uids,
275 (flags & SEC_SIGN) ? c_pgp_encrypt_sign_command : c_pgp_encrypt_only_command);
276 }
277 else
278 {
279 const struct Expando *c_pgp_clear_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_clear_sign_command");
280 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
281 fd_pgp_err, true, fname, NULL, NULL, c_pgp_clear_sign_command);
282 }
283}
284
288void pgp_class_invoke_import(const char *fname)
289{
290 struct PgpCommandContext cctx = { 0 };
291
292 struct Buffer *buf_fname = buf_pool_get();
293 struct Buffer *cmd = buf_pool_get();
294
295 buf_quote_filename(buf_fname, fname, true);
296 cctx.fname = buf_string(buf_fname);
297 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
298 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
299 if (c_pgp_sign_as)
300 cctx.signas = c_pgp_sign_as;
301 else
302 cctx.signas = c_pgp_default_key;
303
304 const struct Expando *c_pgp_import_command = cs_subset_expando(NeoMutt->sub, "pgp_import_command");
305 mutt_pgp_command(cmd, &cctx, c_pgp_import_command);
306 if (mutt_system(buf_string(cmd)) != 0)
307 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
308
309 buf_pool_release(&buf_fname);
310 buf_pool_release(&cmd);
311}
312
317{
318 struct Buffer *personal = NULL;
319 struct PgpCommandContext cctx = { 0 };
320
321 const struct Expando *c_pgp_get_keys_command = cs_subset_expando(NeoMutt->sub, "pgp_get_keys_command");
322 if (!c_pgp_get_keys_command)
323 return;
324
325 struct Buffer *buf = buf_pool_get();
326 struct Buffer *cmd = buf_pool_get();
327 personal = addr->personal;
328 addr->personal = NULL;
329
330 struct Buffer *tmp = buf_pool_get();
331 mutt_addr_to_local(addr);
332 mutt_addr_write(tmp, addr, false);
333 buf_quote_filename(buf, buf_string(tmp), true);
334 buf_pool_release(&tmp);
335
336 addr->personal = personal;
337
338 cctx.ids = buf_string(buf);
339
340 mutt_pgp_command(cmd, &cctx, c_pgp_get_keys_command);
341
342 int fd_null = open("/dev/null", O_RDWR);
343
344 if (!isendwin())
345 mutt_message(_("Fetching PGP key..."));
346
347 if (mutt_system(buf_string(cmd)) != 0)
348 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
349
350 if (!isendwin())
352
353 if (fd_null >= 0)
354 close(fd_null);
355
356 buf_pool_release(&buf);
357 buf_pool_release(&cmd);
358}
359
375pid_t pgp_invoke_export(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
376 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
377{
378 const struct Expando *c_pgp_export_command = cs_subset_expando(NeoMutt->sub, "pgp_export_command");
379 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
380 fd_pgp_err, false, NULL, NULL, uids, c_pgp_export_command);
381}
382
398pid_t pgp_invoke_verify_key(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
399 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
400{
401 const struct Expando *c_pgp_verify_key_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_key_command");
402 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
403 fd_pgp_err, false, NULL, NULL, uids, c_pgp_verify_key_command);
404}
405
422pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
423 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
424 enum PgpRing keyring, struct ListHead *hints)
425{
426 struct Buffer *uids = buf_pool_get();
427 struct Buffer *quoted = buf_pool_get();
428
429 struct ListNode *np = NULL;
430 STAILQ_FOREACH(np, hints, entries)
431 {
432 buf_quote_filename(quoted, (char *) np->data, true);
433 buf_addstr(uids, buf_string(quoted));
434 if (STAILQ_NEXT(np, entries))
435 buf_addch(uids, ' ');
436 }
437
438 const struct Expando *c_pgp_list_pubring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_pubring_command");
439 const struct Expando *c_pgp_list_secring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_secring_command");
440 pid_t rc = pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in,
441 fd_pgp_out, fd_pgp_err, 0, NULL, NULL, buf_string(uids),
442 (keyring == PGP_SECRING) ? c_pgp_list_secring_command :
443 c_pgp_list_pubring_command);
444
445 buf_pool_release(&uids);
446 buf_pool_release(&quoted);
447 return rc;
448}
size_t mutt_addr_write(struct Buffer *buf, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1050
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1340
Email Address Handling.
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
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.
Definition: config_type.c:357
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
int expando_render(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando + data into a string.
Definition: expando.c:118
Parse Expando string.
const struct ExpandoRenderCallback PgpCommandRenderCallbacks[]
Callbacks for PGP Command Expandos.
Ncrypt PGP Expando definitions.
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:933
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:75
void pgp_class_invoke_getkeys(struct Address *addr)
Run a command to download a PGP key - Implements CryptModuleSpecs::pgp_invoke_getkeys() -.
Definition: pgpinvoke.c:316
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...
Definition: pgpinvoke.c:288
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
Convenience wrapper for the gui headers.
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
pid_t filter_create_fd(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, int fdin, int fdout, int fderr, char **envlist)
Run a command on a pipe (optionally connect stdin/stdout)
Definition: filter.c:62
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: lib.h:82
#define SEC_ENCRYPT
Email is encrypted.
Definition: lib.h:84
#define SEC_SIGN
Email is signed.
Definition: lib.h:85
PGP sign, encrypt, check routines.
pid_t pgp_invoke_verify_key(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
Use PGP to verify a key.
Definition: pgpinvoke.c:398
pid_t pgp_invoke_encrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, const char *uids, bool sign)
Use PGP to encrypt a file.
Definition: pgpinvoke.c:230
pid_t pgp_invoke_decode(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, bool need_passphrase)
Use PGP to decode a message.
Definition: pgpinvoke.c:133
pid_t pgp_invoke_traditional(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, const char *uids, SecurityFlags flags)
Use PGP to create in inline-signed message.
Definition: pgpinvoke.c:265
static pid_t pgp_invoke(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, bool need_passphrase, const char *fname, const char *sig_fname, const char *ids, const struct Expando *exp)
Run a PGP command.
Definition: pgpinvoke.c:87
pid_t pgp_invoke_sign(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
Use PGP to sign a file.
Definition: pgpinvoke.c:205
pid_t pgp_invoke_verify(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, const char *sig_fname)
Use PGP to verify a message.
Definition: pgpinvoke.c:158
pid_t pgp_invoke_export(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
Use PGP to export a key from the user's keyring.
Definition: pgpinvoke.c:375
static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx, const struct Expando *exp)
Prepare a PGP Command.
Definition: pgpinvoke.c:61
pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, enum PgpRing keyring, struct ListHead *hints)
Find matching PGP Keys.
Definition: pgpinvoke.c:422
pid_t pgp_invoke_decrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
Use PGP to decrypt a file.
Definition: pgpinvoke.c:182
Wrapper around calls to external PGP program.
PGP key management routines.
PgpRing
PGP ring type.
Definition: pgpkey.h:38
@ PGP_SECRING
Secret keys.
Definition: pgpkey.h:40
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
Prototypes for many functions.
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: render.h:33
Key value store.
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
String manipulation buffer.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:39
Parsed Expando trees.
Definition: expando.h:41
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
Data for a PGP command.
Definition: pgp.h:43
bool need_passphrase
p
Definition: pgp.h:44
const char * signas
a
Definition: pgp.h:47
const char * fname
f
Definition: pgp.h:45
const char * ids
r
Definition: pgp.h:48
const char * sig_fname
s
Definition: pgp.h:46