NeoMutt  2025-01-09-117-gace867
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 "mutt_logging.h"
48#include "pgpkey.h"
49#include "protos.h"
50#ifdef CRYPT_BACKEND_CLASSIC_PGP
51#include "pgp.h"
52#endif
53
60static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx,
61 const struct Expando *exp)
62{
64 mutt_debug(LL_DEBUG2, "%s\n", buf_string(buf));
65}
66
86static pid_t pgp_invoke(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
87 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
88 bool need_passphrase, const char *fname, const char *sig_fname,
89 const char *ids, const struct Expando *exp)
90{
91 struct PgpCommandContext cctx = { 0 };
92
93 if (!exp)
94 return (pid_t) -1;
95
97 cctx.fname = fname;
98 cctx.sig_fname = sig_fname;
99 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
100 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
101 if (c_pgp_sign_as)
102 cctx.signas = c_pgp_sign_as;
103 else
104 cctx.signas = c_pgp_default_key;
105 cctx.ids = ids;
106
107 struct Buffer *cmd = buf_pool_get();
108 mutt_pgp_command(cmd, &cctx, exp);
109
110 pid_t pid = filter_create_fd(buf_string(cmd), fp_pgp_in, fp_pgp_out, fp_pgp_err,
111 fd_pgp_in, fd_pgp_out, fd_pgp_err, NeoMutt->env);
112 buf_pool_release(&cmd);
113 return pid;
114}
115
132pid_t pgp_invoke_decode(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
133 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
134 const char *fname, bool need_passphrase)
135{
136 const struct Expando *c_pgp_decode_command = cs_subset_expando(NeoMutt->sub, "pgp_decode_command");
137 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out, fd_pgp_err,
138 need_passphrase, fname, NULL, NULL, c_pgp_decode_command);
139}
140
157pid_t pgp_invoke_verify(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
158 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
159 const char *fname, const char *sig_fname)
160{
161 const struct Expando *c_pgp_verify_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_command");
162 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
163 fd_pgp_err, false, fname, sig_fname, NULL, c_pgp_verify_command);
164}
165
181pid_t pgp_invoke_decrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
182 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
183{
184 const struct Expando *c_pgp_decrypt_command = cs_subset_expando(NeoMutt->sub, "pgp_decrypt_command");
185 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
186 fd_pgp_err, true, fname, NULL, NULL, c_pgp_decrypt_command);
187}
188
204pid_t pgp_invoke_sign(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
205 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname)
206{
207 const struct Expando *c_pgp_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_sign_command");
208 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
209 fd_pgp_err, true, fname, NULL, NULL, c_pgp_sign_command);
210}
211
229pid_t pgp_invoke_encrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
230 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
231 const char *fname, const char *uids, bool sign)
232{
233 if (sign)
234 {
235 const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
236 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
237 fd_pgp_err, true, fname, NULL, uids, c_pgp_encrypt_sign_command);
238 }
239 else
240 {
241 const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
242 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
243 fd_pgp_err, false, fname, NULL, uids, c_pgp_encrypt_only_command);
244 }
245}
246
264pid_t pgp_invoke_traditional(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
265 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
266 const char *fname, const char *uids, SecurityFlags flags)
267{
268 if (flags & SEC_ENCRYPT)
269 {
270 const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
271 const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
272 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
273 fd_pgp_err, (flags & SEC_SIGN), fname, NULL, uids,
274 (flags & SEC_SIGN) ? c_pgp_encrypt_sign_command : c_pgp_encrypt_only_command);
275 }
276 else
277 {
278 const struct Expando *c_pgp_clear_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_clear_sign_command");
279 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
280 fd_pgp_err, true, fname, NULL, NULL, c_pgp_clear_sign_command);
281 }
282}
283
287void pgp_class_invoke_import(const char *fname)
288{
289 struct PgpCommandContext cctx = { 0 };
290
291 struct Buffer *buf_fname = buf_pool_get();
292 struct Buffer *cmd = buf_pool_get();
293
294 buf_quote_filename(buf_fname, fname, true);
295 cctx.fname = buf_string(buf_fname);
296 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
297 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
298 if (c_pgp_sign_as)
299 cctx.signas = c_pgp_sign_as;
300 else
301 cctx.signas = c_pgp_default_key;
302
303 const struct Expando *c_pgp_import_command = cs_subset_expando(NeoMutt->sub, "pgp_import_command");
304 mutt_pgp_command(cmd, &cctx, c_pgp_import_command);
305 if (mutt_system(buf_string(cmd)) != 0)
306 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
307
308 buf_pool_release(&buf_fname);
309 buf_pool_release(&cmd);
310}
311
316{
317 struct Buffer *personal = NULL;
318 struct PgpCommandContext cctx = { 0 };
319
320 const struct Expando *c_pgp_get_keys_command = cs_subset_expando(NeoMutt->sub, "pgp_get_keys_command");
321 if (!c_pgp_get_keys_command)
322 return;
323
324 struct Buffer *buf = buf_pool_get();
325 struct Buffer *cmd = buf_pool_get();
326 personal = addr->personal;
327 addr->personal = NULL;
328
329 struct Buffer *tmp = buf_pool_get();
330 mutt_addr_to_local(addr);
331 mutt_addr_write(tmp, addr, false);
332 buf_quote_filename(buf, buf_string(tmp), true);
333 buf_pool_release(&tmp);
334
335 addr->personal = personal;
336
337 cctx.ids = buf_string(buf);
338
339 mutt_pgp_command(cmd, &cctx, c_pgp_get_keys_command);
340
341 int fd_null = open("/dev/null", O_RDWR);
342
343 if (!isendwin())
344 mutt_message(_("Fetching PGP key..."));
345
346 if (mutt_system(buf_string(cmd)) != 0)
347 mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
348
349 if (!isendwin())
351
352 if (fd_null >= 0)
353 close(fd_null);
354
355 buf_pool_release(&buf);
356 buf_pool_release(&cmd);
357}
358
374pid_t pgp_invoke_export(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
375 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
376{
377 const struct Expando *c_pgp_export_command = cs_subset_expando(NeoMutt->sub, "pgp_export_command");
378 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
379 fd_pgp_err, false, NULL, NULL, uids, c_pgp_export_command);
380}
381
397pid_t pgp_invoke_verify_key(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
398 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
399{
400 const struct Expando *c_pgp_verify_key_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_key_command");
401 return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
402 fd_pgp_err, false, NULL, NULL, uids, c_pgp_verify_key_command);
403}
404
421pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
422 int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
423 enum PgpRing keyring, struct ListHead *hints)
424{
425 struct Buffer *uids = buf_pool_get();
426 struct Buffer *quoted = buf_pool_get();
427
428 struct ListNode *np = NULL;
429 STAILQ_FOREACH(np, hints, entries)
430 {
431 buf_quote_filename(quoted, (char *) np->data, true);
432 buf_addstr(uids, buf_string(quoted));
433 if (STAILQ_NEXT(np, entries))
434 buf_addch(uids, ' ');
435 }
436
437 const struct Expando *c_pgp_list_pubring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_pubring_command");
438 const struct Expando *c_pgp_list_secring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_secring_command");
439 pid_t rc = pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in,
440 fd_pgp_out, fd_pgp_err, 0, NULL, NULL, buf_string(uids),
441 (keyring == PGP_SECRING) ? c_pgp_list_secring_command :
442 c_pgp_list_pubring_command);
443
444 buf_pool_release(&uids);
445 buf_pool_release(&quoted);
446 return rc;
447}
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:361
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:810
void pgp_class_invoke_getkeys(struct Address *addr)
Run a command to download a PGP key - Implements CryptModuleSpecs::pgp_invoke_getkeys() -.
Definition: pgpinvoke.c:315
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:287
#define mutt_message(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
Convenience wrapper for the gui headers.
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:45
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
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:397
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:229
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:132
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:264
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:86
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:204
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:157
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:374
static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx, const struct Expando *exp)
Prepare a PGP Command.
Definition: pgpinvoke.c:60
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:421
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:181
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:390
#define STAILQ_NEXT(elm, field)
Definition: queue.h:439
#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:43
char ** env
Private copy of the environment variables.
Definition: neomutt.h:55
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:47
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