NeoMutt  2025-12-11-769-g906513
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp.c
Go to the documentation of this file.
1
24
34
35#include "config.h"
36#include <stdbool.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/types.h>
41#include <unistd.h>
42#include "mutt/lib.h"
43#include "address/lib.h"
44#include "config/lib.h"
45#include "email/lib.h"
46#include "core/lib.h"
47#include "gui/lib.h"
48#include "mutt.h"
49#include "lib.h"
50#include "attach/lib.h"
51#include "editor/lib.h"
52#include "history/lib.h"
53#include "hooks/lib.h"
54#include "question/lib.h"
55#include "send/lib.h"
56#include "crypt.h"
57#include "cryptglue.h"
58#include "globals.h"
59#include "module_data.h"
60#include "pgpinvoke.h"
61#include "pgpkey.h"
62#include "pgpmicalg.h"
63#ifdef CRYPT_BACKEND_CLASSIC_PGP
64#include "pgp.h"
65#include "pgplib.h"
66#endif
67
72{
74 memset(mod_data->pgp_pass, 0, sizeof(mod_data->pgp_pass));
75 mod_data->pgp_exptime = 0;
76}
77
82{
85 {
86 *mod_data->pgp_pass = '\0';
87 return true; /* handled by gpg-agent */
88 }
89
90 if (mutt_date_now() < mod_data->pgp_exptime)
91 {
92 /* Use cached copy. */
93 return true;
94 }
95
97
98 struct Buffer *buf = buf_pool_get();
99 const int rc = mw_get_field(_("Enter PGP passphrase:"), buf,
101 mutt_str_copy(mod_data->pgp_pass, buf_string(buf), sizeof(mod_data->pgp_pass));
102 buf_pool_release(&buf);
103
104 if (rc == 0)
105 {
106 const long c_pgp_timeout = cs_subset_long(NeoMutt->sub, "pgp_timeout");
107 mod_data->pgp_exptime = mutt_date_add_timeout(mutt_date_now(), c_pgp_timeout);
108 return true;
109 }
110 else
111 {
112 mod_data->pgp_exptime = 0;
113 }
114
115 return false;
116}
117
125{
126 char *tty = NULL;
127
128 /* GnuPG 2.1 no longer exports GPG_AGENT_INFO */
129 const bool c_pgp_use_gpg_agent = cs_subset_bool(NeoMutt->sub, "pgp_use_gpg_agent");
130 if (!c_pgp_use_gpg_agent)
131 return false;
132
133 tty = ttyname(0);
134 if (tty)
135 {
136 setenv("GPG_TTY", tty, 0);
137 envlist_set(&NeoMutt->env, "GPG_TTY", tty, false);
138 }
139
140 return true;
141}
142
148static struct PgpKeyInfo *key_parent(struct PgpKeyInfo *k)
149{
150 const bool c_pgp_ignore_subkeys = cs_subset_bool(NeoMutt->sub, "pgp_ignore_subkeys");
151 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && c_pgp_ignore_subkeys)
152 k = k->parent;
153
154 return k;
155}
156
163{
164 k = key_parent(k);
165
166 return k->keyid;
167}
168
175{
176 k = key_parent(k);
177
178 return k->keyid + 8;
179}
180
189{
190 const bool c_pgp_long_ids = cs_subset_bool(NeoMutt->sub, "pgp_long_ids");
191 if (c_pgp_long_ids)
192 return k->keyid;
193 return k->keyid + 8;
194}
195
201char *pgp_keyid(struct PgpKeyInfo *k)
202{
203 k = key_parent(k);
204
205 return pgp_this_keyid(k);
206}
207
213static char *pgp_fingerprint(struct PgpKeyInfo *k)
214{
215 k = key_parent(k);
216
217 return k->fingerprint;
218}
219
232{
233 char *fingerprint = pgp_fingerprint(k);
235}
236
237/* ----------------------------------------------------------------------------
238 * Routines for handing PGP input.
239 */
240
248static int pgp_copy_checksig(FILE *fp_in, FILE *fp_out)
249{
250 if (!fp_in || !fp_out)
251 return -1;
252
253 int rc = -1;
254
255 const struct Regex *c_pgp_good_sign = cs_subset_regex(NeoMutt->sub, "pgp_good_sign");
256 if (c_pgp_good_sign && c_pgp_good_sign->regex)
257 {
258 char *line = NULL;
259 size_t linelen;
260
261 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
262 {
263 if (mutt_regex_match(c_pgp_good_sign, line))
264 {
265 mutt_debug(LL_DEBUG2, "\"%s\" matches regex\n", line);
266 rc = 0;
267 }
268 else
269 {
270 mutt_debug(LL_DEBUG2, "\"%s\" doesn't match regex\n", line);
271 }
272
273 if (mutt_strn_equal(line, "[GNUPG:] ", 9))
274 continue;
275 fputs(line, fp_out);
276 fputc('\n', fp_out);
277 }
278 FREE(&line);
279 }
280 else
281 {
282 mutt_debug(LL_DEBUG2, "No pattern\n");
283 mutt_file_copy_stream(fp_in, fp_out);
284 rc = 1;
285 }
286
287 return rc;
288}
289
301{
302 int rc = -1;
303
304 const struct Regex *c_pgp_decryption_okay = cs_subset_regex(NeoMutt->sub, "pgp_decryption_okay");
305 if (c_pgp_decryption_okay && c_pgp_decryption_okay->regex)
306 {
307 char *line = NULL;
308 size_t linelen;
309
310 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
311 {
312 if (mutt_regex_match(c_pgp_decryption_okay, line))
313 {
314 mutt_debug(LL_DEBUG2, "\"%s\" matches regex\n", line);
315 rc = 0;
316 break;
317 }
318 else
319 {
320 mutt_debug(LL_DEBUG2, "\"%s\" doesn't match regex\n", line);
321 }
322 }
323 FREE(&line);
324 }
325 else
326 {
327 mutt_debug(LL_DEBUG2, "No pattern\n");
328 rc = 1;
329 }
330
331 return rc;
332}
333
354static int pgp_check_decryption_okay(FILE *fp_in)
355{
356 int rc = -1;
357 char *line = NULL, *s = NULL;
358 size_t linelen;
359 int inside_decrypt = 0;
360
361 const bool c_pgp_check_gpg_decrypt_status_fd = cs_subset_bool(NeoMutt->sub, "pgp_check_gpg_decrypt_status_fd");
362 if (!c_pgp_check_gpg_decrypt_status_fd)
364
365 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
366 {
367 size_t plen = mutt_str_startswith(line, "[GNUPG:] ");
368 if (plen == 0)
369 continue;
370 s = line + plen;
371 mutt_debug(LL_DEBUG2, "checking \"%s\"\n", line);
372 if (mutt_str_startswith(s, "BEGIN_DECRYPTION"))
373 {
374 inside_decrypt = 1;
375 }
376 else if (mutt_str_startswith(s, "END_DECRYPTION"))
377 {
378 inside_decrypt = 0;
379 }
380 else if (mutt_str_startswith(s, "PLAINTEXT"))
381 {
382 if (!inside_decrypt)
383 {
384 mutt_debug(LL_DEBUG2, " PLAINTEXT encountered outside of DECRYPTION\n");
385 rc = -2;
386 break;
387 }
388 }
389 else if (mutt_str_startswith(s, "DECRYPTION_FAILED"))
390 {
391 mutt_debug(LL_DEBUG2, " DECRYPTION_FAILED encountered. Failure\n");
392 rc = -3;
393 break;
394 }
395 else if (mutt_str_startswith(s, "DECRYPTION_OKAY"))
396 {
397 /* Don't break out because we still have to check for
398 * PLAINTEXT outside of the decryption boundaries. */
399 mutt_debug(LL_DEBUG2, " DECRYPTION_OKAY encountered\n");
400 rc = 0;
401 }
402 }
403 FREE(&line);
404
405 return rc;
406}
407
421static void pgp_copy_clearsigned(FILE *fp_in, struct State *state, char *charset)
422{
423 char buf[8192] = { 0 };
424 bool complete, armor_header;
425
426 rewind(fp_in);
427
428 /* fromcode comes from the MIME Content-Type charset label. It might
429 * be a wrong label, so we want the ability to do corrections via
430 * charset-hooks. Therefore we set flags to MUTT_ICONV_HOOK_FROM. */
431 struct FgetConv *fc = mutt_ch_fgetconv_open(fp_in, charset, cc_charset(), MUTT_ICONV_HOOK_FROM);
432
433 for (complete = true, armor_header = true;
434 mutt_ch_fgetconvs(buf, sizeof(buf), fc); complete = (strchr(buf, '\n')))
435 {
436 if (!complete)
437 {
438 if (!armor_header)
439 state_puts(state, buf);
440 continue;
441 }
442
443 if (mutt_str_equal(buf, "-----BEGIN PGP SIGNATURE-----\n"))
444 break;
445
446 if (armor_header)
447 {
448 char *p = mutt_str_skip_whitespace(buf);
449 if (*p == '\0')
450 armor_header = false;
451 continue;
452 }
453
454 if (state->prefix)
455 state_puts(state, state->prefix);
456
457 if ((buf[0] == '-') && (buf[1] == ' '))
458 state_puts(state, buf + 2);
459 else
460 state_puts(state, buf);
461 }
462
464}
465
469int pgp_class_application_handler(struct Body *b, struct State *state)
470{
472 bool could_not_decrypt = false;
473 int decrypt_okay_rc = 0;
474 int needpass = -1;
475 bool pgp_keyblock = false;
476 bool clearsign = false;
477 int rc = -1;
478 int c = 1;
479 long bytes;
480 LOFF_T last_pos, offset;
481 char buf[8192] = { 0 };
482 FILE *fp_pgp_out = NULL, *fp_pgp_in = NULL, *fp_pgp_err = NULL;
483 FILE *fp_tmp = NULL;
484 pid_t pid;
485 struct Buffer *tempfile = buf_pool_get();
486
487 bool maybe_goodsig = true;
488 bool have_any_sigs = false;
489
490 char *gpgcharset = NULL;
491 char body_charset[256] = { 0 };
492 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
493
494 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
495 {
496 return -1;
497 }
498 last_pos = b->offset;
499
500 for (bytes = b->length; bytes > 0;)
501 {
502 if (!fgets(buf, sizeof(buf), state->fp_in))
503 break;
504
505 offset = ftello(state->fp_in);
506 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
507 last_pos = offset;
508
509 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
510 if (plen != 0)
511 {
512 needpass = false;
513 clearsign = false;
514 could_not_decrypt = false;
515 decrypt_okay_rc = 0;
516
517 if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
518 {
519 needpass = 1;
520 }
521 else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
522 {
523 clearsign = true;
524 }
525 else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
526 {
527 pgp_keyblock = true;
528 }
529 else
530 {
531 /* XXX we may wish to recode here */
532 if (state->prefix)
533 state_puts(state, state->prefix);
534 state_puts(state, buf);
535 continue;
536 }
537
538 have_any_sigs = have_any_sigs || (clearsign && (state->flags & STATE_VERIFY));
539
540 /* Copy PGP material to temporary file */
541 buf_mktemp(tempfile);
542 fp_tmp = mutt_file_fopen(buf_string(tempfile), "w+");
543 if (!fp_tmp)
544 {
545 mutt_perror("%s", buf_string(tempfile));
546 FREE(&gpgcharset);
547 goto out;
548 }
549
550 fputs(buf, fp_tmp);
551 while ((bytes > 0) && fgets(buf, sizeof(buf) - 1, state->fp_in))
552 {
553 offset = ftello(state->fp_in);
554 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
555 last_pos = offset;
556
557 fputs(buf, fp_tmp);
558
559 if ((needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf)) ||
560 (!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
561 mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf))))
562 {
563 break;
564 }
565 /* remember optional Charset: armor header as defined by RFC4880 */
566 if (mutt_str_startswith(buf, "Charset: "))
567 {
568 size_t l = 0;
569 FREE(&gpgcharset);
570 gpgcharset = mutt_str_dup(buf + 9);
571 l = mutt_str_len(gpgcharset);
572 if ((l > 0) && (gpgcharset[l - 1] == '\n'))
573 gpgcharset[l - 1] = 0;
574 if (!mutt_ch_check_charset(gpgcharset, false))
575 mutt_str_replace(&gpgcharset, "UTF-8");
576 }
577 }
578
579 /* leave fp_tmp open in case we still need it - but flush it! */
580 fflush(fp_tmp);
581
582 /* Invoke PGP if needed */
583 if (!clearsign || (state->flags & STATE_VERIFY))
584 {
585 fp_pgp_out = mutt_file_mkstemp();
586 if (!fp_pgp_out)
587 {
588 mutt_perror(_("Can't create temporary file"));
589 goto out;
590 }
591
592 fp_pgp_err = mutt_file_mkstemp();
593 if (!fp_pgp_err)
594 {
595 mutt_perror(_("Can't create temporary file"));
596 goto out;
597 }
598
599 pid = pgp_invoke_decode(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
600 fileno(fp_pgp_err), buf_string(tempfile),
601 (needpass != 0));
602 if (pid == -1)
603 {
604 mutt_file_fclose(&fp_pgp_out);
605 maybe_goodsig = false;
606 fp_pgp_in = NULL;
607 state_attach_puts(state, _("[-- Error: unable to create PGP subprocess --]\n"));
608 }
609 else /* PGP started successfully */
610 {
611 if (needpass)
612 {
615 if (pgp_use_gpg_agent())
616 *mod_data->pgp_pass = '\0';
617 fprintf(fp_pgp_in, "%s\n", mod_data->pgp_pass);
618 }
619
620 mutt_file_fclose(&fp_pgp_in);
621
622 int wait_filter_rc = filter_wait(pid);
623
624 fflush(fp_pgp_err);
625 /* If we are expecting an encrypted message, verify status fd output.
626 * Note that BEGIN PGP MESSAGE does not guarantee the content is encrypted,
627 * so we need to be more selective about the value of decrypt_okay_rc.
628 *
629 * -3 indicates we actively found a DECRYPTION_FAILED.
630 * -2 and -1 indicate part or all of the content was plaintext. */
631 if (needpass)
632 {
633 rewind(fp_pgp_err);
634 decrypt_okay_rc = pgp_check_decryption_okay(fp_pgp_err);
635 if (decrypt_okay_rc <= -3)
636 mutt_file_fclose(&fp_pgp_out);
637 }
638
639 if (state->flags & STATE_DISPLAY)
640 {
641 rewind(fp_pgp_err);
642 crypt_current_time(state, "PGP");
643 int checksig_rc = pgp_copy_checksig(fp_pgp_err, state->fp_out);
644
645 if (checksig_rc == 0)
646 have_any_sigs = true;
647 /* Sig is bad if
648 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
649 * Sig _is_ correct if
650 * gpg_good_sign="" && pgp_decode_command returned 0 */
651 if (checksig_rc == -1 || (wait_filter_rc != 0))
652 maybe_goodsig = false;
653
654 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
655 }
656 if (pgp_use_gpg_agent())
657 {
659 }
660 }
661
662 /* treat empty result as sign of failure */
663 /* TODO: maybe on failure neomutt should include the original undecoded text. */
664 if (fp_pgp_out)
665 {
666 rewind(fp_pgp_out);
667 c = fgetc(fp_pgp_out);
668 ungetc(c, fp_pgp_out);
669 }
670 if (!clearsign && (!fp_pgp_out || (c == EOF)))
671 {
672 could_not_decrypt = true;
674 }
675
676 if ((could_not_decrypt || (decrypt_okay_rc <= -3)) && !(state->flags & STATE_DISPLAY))
677 {
678 mutt_error(_("Could not decrypt PGP message"));
679 goto out;
680 }
681 }
682
683 /* Now, copy cleartext to the screen. */
684 if (state->flags & STATE_DISPLAY)
685 {
686 if (needpass)
687 state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
688 else if (pgp_keyblock)
689 state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
690 else
691 state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
692 }
693
694 if (clearsign)
695 {
696 rewind(fp_tmp);
697 pgp_copy_clearsigned(fp_tmp, state, body_charset);
698 }
699 else if (fp_pgp_out)
700 {
701 struct FgetConv *fc = NULL;
702 int ch;
703 char *expected_charset = (gpgcharset && *gpgcharset) ? gpgcharset : "utf-8";
704
705 mutt_debug(LL_DEBUG3, "pgp: recoding inline from [%s] to [%s]\n",
706 expected_charset, cc_charset());
707
708 rewind(fp_pgp_out);
709 state_set_prefix(state);
710 fc = mutt_ch_fgetconv_open(fp_pgp_out, expected_charset, cc_charset(),
712 while ((ch = mutt_ch_fgetconv(fc)) != EOF)
713 state_prefix_putc(state, ch);
715 }
716
717 /* Multiple PGP blocks can exist, so these need to be closed and
718 * unlinked inside the loop. */
719 mutt_file_fclose(&fp_tmp);
720 mutt_file_unlink(buf_string(tempfile));
721 mutt_file_fclose(&fp_pgp_out);
722 mutt_file_fclose(&fp_pgp_err);
723
724 if (state->flags & STATE_DISPLAY)
725 {
726 state_putc(state, '\n');
727 if (needpass)
728 {
729 state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
730 if (could_not_decrypt || (decrypt_okay_rc <= -3))
731 {
732 mutt_error(_("Could not decrypt PGP message"));
733 }
734 else if (decrypt_okay_rc < 0)
735 {
736 /* L10N: You will see this error message if (1) you are decrypting
737 (not encrypting) something and (2) it is a plaintext. So the
738 message does not mean "You failed to encrypt the message." */
739 mutt_error(_("PGP message is not encrypted"));
740 }
741 else
742 {
743 mutt_message(_("PGP message successfully decrypted"));
744 }
745 }
746 else if (pgp_keyblock)
747 {
748 state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
749 }
750 else
751 {
752 state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
753 }
754 }
755 }
756 else
757 {
758 /* A traditional PGP part may mix signed and unsigned content */
759 /* XXX we may wish to recode here */
760 if (state->prefix)
761 state_puts(state, state->prefix);
762 state_puts(state, buf);
763 }
764 }
765
766 rc = 0;
767
768out:
769 b->goodsig = (maybe_goodsig && have_any_sigs);
770
771 if (fp_tmp)
772 {
773 mutt_file_fclose(&fp_tmp);
774 mutt_file_unlink(buf_string(tempfile));
775 }
776 mutt_file_fclose(&fp_pgp_out);
777 mutt_file_fclose(&fp_pgp_err);
778
779 buf_pool_release(&tempfile);
780
781 FREE(&gpgcharset);
782
783 if (needpass == -1)
784 {
785 state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
786 return -1;
787 }
788
789 return rc;
790}
791
799static bool pgp_check_traditional_one_body(FILE *fp, struct Body *b)
800{
801 struct Buffer *tempfile = NULL;
802 char buf[8192] = { 0 };
803 bool rc = false;
804
805 bool sgn = false;
806 bool enc = false;
807 bool key = false;
808
809 if (b->type != TYPE_TEXT)
810 goto cleanup;
811
812 tempfile = buf_pool_get();
813 buf_mktemp(tempfile);
815 MUTT_SAVE_NO_FLAGS) != 0)
816 {
817 unlink(buf_string(tempfile));
818 goto cleanup;
819 }
820
821 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "r");
822 if (!fp_tmp)
823 {
824 unlink(buf_string(tempfile));
825 goto cleanup;
826 }
827
828 while (fgets(buf, sizeof(buf), fp_tmp))
829 {
830 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
831 if (plen != 0)
832 {
833 if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
834 enc = true;
835 else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
836 sgn = true;
837 else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
838 key = true;
839 }
840 }
841 mutt_file_fclose(&fp_tmp);
842 unlink(buf_string(tempfile));
843
844 if (!enc && !sgn && !key)
845 goto cleanup;
846
847 /* fix the content type */
848
849 mutt_param_set(&b->parameter, "format", "fixed");
850 if (enc)
851 mutt_param_set(&b->parameter, "x-action", "pgp-encrypted");
852 else if (sgn)
853 mutt_param_set(&b->parameter, "x-action", "pgp-signed");
854 else if (key)
855 mutt_param_set(&b->parameter, "x-action", "pgp-keys");
856
857 rc = true;
858
859cleanup:
860 buf_pool_release(&tempfile);
861 return rc;
862}
863
867bool pgp_class_check_traditional(FILE *fp, struct Body *b, bool just_one)
868{
869 bool rc = false;
870 int r;
871 for (; b; b = b->next)
872 {
873 if (!just_one && is_multipart(b))
874 {
875 rc = pgp_class_check_traditional(fp, b->parts, false) || rc;
876 }
877 else if (b->type == TYPE_TEXT)
878 {
880 if (r)
881 rc = rc || r;
882 else
883 rc = pgp_check_traditional_one_body(fp, b) || rc;
884 }
885
886 if (just_one)
887 break;
888 }
889
890 return rc;
891}
892
896int pgp_class_verify_one(struct Body *b, struct State *state, const char *tempfile)
897{
898 FILE *fp_pgp_out = NULL;
899 pid_t pid;
900 int badsig = -1;
901 struct Buffer *sigfile = buf_pool_get();
902
903 buf_printf(sigfile, "%s.asc", tempfile);
904
905 FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
906 if (!fp_sig)
907 {
908 mutt_perror("%s", buf_string(sigfile));
909 goto cleanup;
910 }
911
912 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
913 {
914 mutt_file_fclose(&fp_sig);
915 goto cleanup;
916 }
917 mutt_file_copy_bytes(state->fp_in, fp_sig, b->length);
918 mutt_file_fclose(&fp_sig);
919
920 FILE *fp_pgp_err = mutt_file_mkstemp();
921 if (!fp_pgp_err)
922 {
923 mutt_perror(_("Can't create temporary file"));
924 unlink(buf_string(sigfile));
925 goto cleanup;
926 }
927
928 crypt_current_time(state, "PGP");
929
930 pid = pgp_invoke_verify(NULL, &fp_pgp_out, NULL, -1, -1, fileno(fp_pgp_err),
931 tempfile, buf_string(sigfile));
932 if (pid != -1)
933 {
934 if (pgp_copy_checksig(fp_pgp_out, state->fp_out) >= 0)
935 badsig = 0;
936
937 mutt_file_fclose(&fp_pgp_out);
938 fflush(fp_pgp_err);
939 rewind(fp_pgp_err);
940
941 if (pgp_copy_checksig(fp_pgp_err, state->fp_out) >= 0)
942 badsig = 0;
943
944 const int rv = filter_wait(pid);
945 if (rv)
946 badsig = -1;
947
948 mutt_debug(LL_DEBUG1, "filter_wait returned %d\n", rv);
949 }
950
951 mutt_file_fclose(&fp_pgp_err);
952
953 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
954
956
957cleanup:
958 buf_pool_release(&sigfile);
959
960 mutt_debug(LL_DEBUG1, "returning %d\n", badsig);
961 return badsig;
962}
963
969static void pgp_extract_keys_from_attachment(FILE *fp, struct Body *b)
970{
971 struct State state = { 0 };
972 struct Buffer *tempfile = buf_pool_get();
973
974 buf_mktemp(tempfile);
975 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
976 if (!fp_tmp)
977 {
978 mutt_perror("%s", buf_string(tempfile));
979 goto cleanup;
980 }
981
982 state.fp_in = fp;
983 state.fp_out = fp_tmp;
984
985 mutt_body_handler(b, &state);
986
987 mutt_file_fclose(&fp_tmp);
988
991
992 mutt_file_unlink(buf_string(tempfile));
993
994cleanup:
995 buf_pool_release(&tempfile);
996}
997
1002{
1003 if (!fp)
1004 {
1005 mutt_error(_("Internal error. Please submit a bug report."));
1006 return;
1007 }
1008
1009 mutt_endwin();
1010
1011 OptDontHandlePgpKeys = true;
1013 OptDontHandlePgpKeys = false;
1014}
1015
1024static struct Body *pgp_decrypt_part(struct Body *a, struct State *state,
1025 FILE *fp_out, struct Body *p)
1026{
1027 if (!a || !state || !fp_out || !p)
1028 return NULL;
1029
1031 char buf[1024] = { 0 };
1032 FILE *fp_pgp_in = NULL, *fp_pgp_out = NULL, *fp_pgp_tmp = NULL;
1033 struct Body *tattach = NULL;
1034 pid_t pid;
1035 int rv;
1036 struct Buffer *tempfile = buf_pool_get();
1037
1038 FILE *fp_pgp_err = mutt_file_mkstemp();
1039 if (!fp_pgp_err)
1040 {
1041 mutt_perror(_("Can't create temporary file"));
1042 goto cleanup;
1043 }
1044
1045 buf_mktemp(tempfile);
1046 fp_pgp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
1047 if (!fp_pgp_tmp)
1048 {
1049 mutt_perror("%s", buf_string(tempfile));
1050 mutt_file_fclose(&fp_pgp_err);
1051 goto cleanup;
1052 }
1053
1054 /* Position the stream at the beginning of the body, and send the data to
1055 * the temporary file. */
1056
1057 if (!mutt_file_seek(state->fp_in, a->offset, SEEK_SET))
1058 {
1059 mutt_file_fclose(&fp_pgp_tmp);
1060 mutt_file_fclose(&fp_pgp_err);
1061 goto cleanup;
1062 }
1063 mutt_file_copy_bytes(state->fp_in, fp_pgp_tmp, a->length);
1064 mutt_file_fclose(&fp_pgp_tmp);
1065
1066 pid = pgp_invoke_decrypt(&fp_pgp_in, &fp_pgp_out, NULL, -1, -1,
1067 fileno(fp_pgp_err), buf_string(tempfile));
1068 if (pid == -1)
1069 {
1070 mutt_file_fclose(&fp_pgp_err);
1071 unlink(buf_string(tempfile));
1072 if (state->flags & STATE_DISPLAY)
1073 {
1074 state_attach_puts(state, _("[-- Error: could not create a PGP subprocess --]\n\n"));
1075 }
1076 goto cleanup;
1077 }
1078
1079 /* send the PGP passphrase to the subprocess. Never do this if the agent is
1080 * active, because this might lead to a passphrase send as the message. */
1081 if (!pgp_use_gpg_agent())
1082 fputs(mod_data->pgp_pass, fp_pgp_in);
1083 fputc('\n', fp_pgp_in);
1084 mutt_file_fclose(&fp_pgp_in);
1085
1086 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
1087 * read_mime_header has a hard time parsing the message. */
1088 while (fgets(buf, sizeof(buf) - 1, fp_pgp_out))
1089 {
1090 size_t len = mutt_str_len(buf);
1091 if ((len > 1) && (buf[len - 2] == '\r'))
1092 strcpy(buf + len - 2, "\n");
1093 fputs(buf, fp_out);
1094 }
1095
1096 mutt_file_fclose(&fp_pgp_out);
1097
1098 rv = filter_wait(pid);
1099 const bool c_pgp_use_gpg_agent = cs_subset_bool(NeoMutt->sub, "pgp_use_gpg_agent");
1100 if (c_pgp_use_gpg_agent)
1102
1103 mutt_file_unlink(buf_string(tempfile));
1104
1105 fflush(fp_pgp_err);
1106 rewind(fp_pgp_err);
1107 if (pgp_check_decryption_okay(fp_pgp_err) < 0)
1108 {
1109 mutt_error(_("Decryption failed"));
1111 mutt_file_fclose(&fp_pgp_err);
1112 goto cleanup;
1113 }
1114
1115 if (state->flags & STATE_DISPLAY)
1116 {
1117 rewind(fp_pgp_err);
1118 if ((pgp_copy_checksig(fp_pgp_err, state->fp_out) == 0) && !rv)
1119 p->goodsig = true;
1120 else
1121 p->goodsig = false;
1122 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
1123 }
1124 mutt_file_fclose(&fp_pgp_err);
1125
1126 fflush(fp_out);
1127 rewind(fp_out);
1128
1129 if (fgetc(fp_out) == EOF)
1130 {
1131 mutt_error(_("Decryption failed"));
1133 goto cleanup;
1134 }
1135
1136 rewind(fp_out);
1137 const long size = mutt_file_get_size_fp(fp_out);
1138 if (size == 0)
1139 {
1140 goto cleanup;
1141 }
1142
1143 tattach = mutt_read_mime_header(fp_out, 0);
1144 if (tattach)
1145 {
1146 /* Need to set the length of this body part. */
1147 tattach->length = size - tattach->offset;
1148
1149 /* See if we need to recurse on this MIME part. */
1150 mutt_parse_part(fp_out, tattach);
1151 }
1152
1153cleanup:
1154 buf_pool_release(&tempfile);
1155 return tattach;
1156}
1157
1161int pgp_class_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
1162{
1163 struct State state = { 0 };
1164 struct Body *p = b;
1165 bool need_decode = false;
1166 LOFF_T saved_offset = 0;
1167 size_t saved_length = 0;
1168 FILE *fp_decoded = NULL;
1169 int rc = 0;
1170
1172 {
1173 b = b->parts->next;
1174 /* Some clients improperly encode the octetstream part. */
1175 if (b->encoding != ENC_7BIT)
1176 need_decode = true;
1177 }
1179 {
1180 b = b->parts->next->next;
1181 need_decode = true;
1182 }
1183 else
1184 {
1185 return -1;
1186 }
1187
1188 state.fp_in = fp_in;
1189
1190 if (need_decode)
1191 {
1192 saved_offset = b->offset;
1193 saved_length = b->length;
1194
1195 fp_decoded = mutt_file_mkstemp();
1196 if (!fp_decoded)
1197 {
1198 mutt_perror(_("Can't create temporary file"));
1199 return -1;
1200 }
1201
1202 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1203 {
1204 rc = -1;
1205 goto bail;
1206 }
1207 state.fp_out = fp_decoded;
1208
1209 mutt_decode_attachment(b, &state);
1210
1211 fflush(fp_decoded);
1212 b->length = ftello(fp_decoded);
1213 b->offset = 0;
1214 rewind(fp_decoded);
1215 state.fp_in = fp_decoded;
1216 state.fp_out = 0;
1217 }
1218
1219 *fp_out = mutt_file_mkstemp();
1220 if (!*fp_out)
1221 {
1222 mutt_perror(_("Can't create temporary file"));
1223 rc = -1;
1224 goto bail;
1225 }
1226
1227 *b_dec = pgp_decrypt_part(b, &state, *fp_out, p);
1228 if (!*b_dec)
1229 rc = -1;
1230 rewind(*fp_out);
1231
1232bail:
1233 if (need_decode)
1234 {
1235 b->length = saved_length;
1236 b->offset = saved_offset;
1237 mutt_file_fclose(&fp_decoded);
1238 }
1239
1240 return rc;
1241}
1242
1246int pgp_class_encrypted_handler(struct Body *b, struct State *state)
1247{
1248 FILE *fp_in = NULL;
1249 struct Body *tattach = NULL;
1250 int rc = 0;
1251
1252 FILE *fp_out = mutt_file_mkstemp();
1253 if (!fp_out)
1254 {
1255 mutt_perror(_("Can't create temporary file"));
1256 if (state->flags & STATE_DISPLAY)
1257 {
1258 state_attach_puts(state, _("[-- Error: could not create temporary file --]\n"));
1259 }
1260 return -1;
1261 }
1262
1263 if (state->flags & STATE_DISPLAY)
1264 crypt_current_time(state, "PGP");
1265
1266 tattach = pgp_decrypt_part(b, state, fp_out, b);
1267 if (tattach)
1268 {
1269 if (state->flags & STATE_DISPLAY)
1270 {
1271 state_attach_puts(state, _("[-- The following data is PGP/MIME encrypted --]\n"));
1272 mutt_protected_headers_handler(tattach, state);
1273 }
1274
1275 /* Store any protected headers in the parent so they can be
1276 * accessed for index updates after the handler recursion is done.
1277 * This is done before the handler to prevent a nested encrypted
1278 * handler from freeing the headers. */
1280 b->mime_headers = tattach->mime_headers;
1281 tattach->mime_headers = NULL;
1282
1283 fp_in = state->fp_in;
1284 state->fp_in = fp_out;
1285 rc = mutt_body_handler(tattach, state);
1286 state->fp_in = fp_in;
1287
1288 /* Embedded multipart signed protected headers override the
1289 * encrypted headers. We need to do this after the handler so
1290 * they can be printed in the pager. */
1291 if (mutt_is_multipart_signed(tattach) && tattach->parts && tattach->parts->mime_headers)
1292 {
1294 b->mime_headers = tattach->parts->mime_headers;
1295 tattach->parts->mime_headers = NULL;
1296 }
1297
1298 /* if a multipart/signed is the _only_ sub-part of a
1299 * multipart/encrypted, cache signature verification
1300 * status. */
1301 if (mutt_is_multipart_signed(tattach) && !tattach->next)
1302 b->goodsig |= tattach->goodsig;
1303
1304 if (state->flags & STATE_DISPLAY)
1305 state_attach_puts(state, _("[-- End of PGP/MIME encrypted data --]\n"));
1306
1307 mutt_body_free(&tattach);
1308 /* clear 'Invoking...' message, since there's no error */
1309 mutt_message(_("PGP message successfully decrypted"));
1310 }
1311 else
1312 {
1313 mutt_error(_("Could not decrypt PGP message"));
1314 /* void the passphrase, even if it's not necessarily the problem */
1316 rc = -1;
1317 }
1318
1319 mutt_file_fclose(&fp_out);
1320
1321 return rc;
1322}
1323
1324/* ----------------------------------------------------------------------------
1325 * Routines for sending PGP/MIME messages.
1326 */
1327
1331struct Body *pgp_class_sign_message(struct Body *b, const struct AddressList *from)
1332{
1334 struct Body *b_enc = NULL, *rv = NULL;
1335 char buf[1024] = { 0 };
1336 FILE *fp_pgp_in = NULL, *fp_pgp_out = NULL, *fp_pgp_err = NULL, *fp_signed = NULL;
1337 bool err = false;
1338 bool empty = true;
1339 pid_t pid;
1340 struct Buffer *sigfile = buf_pool_get();
1341 struct Buffer *signedfile = buf_pool_get();
1342
1343 crypt_convert_to_7bit(b); /* Signed data _must_ be in 7-bit format. */
1344
1345 buf_mktemp(sigfile);
1346 FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
1347 if (!fp_sig)
1348 {
1349 goto cleanup;
1350 }
1351
1352 buf_mktemp(signedfile);
1353 fp_signed = mutt_file_fopen(buf_string(signedfile), "w");
1354 if (!fp_signed)
1355 {
1356 mutt_perror("%s", buf_string(signedfile));
1357 mutt_file_fclose(&fp_sig);
1358 unlink(buf_string(sigfile));
1359 goto cleanup;
1360 }
1361
1362 mutt_write_mime_header(b, fp_signed, NeoMutt->sub);
1363 fputc('\n', fp_signed);
1364 mutt_write_mime_body(b, fp_signed, NeoMutt->sub);
1365 mutt_file_fclose(&fp_signed);
1366
1367 pid = pgp_invoke_sign(&fp_pgp_in, &fp_pgp_out, &fp_pgp_err, -1, -1, -1,
1368 buf_string(signedfile));
1369 if (pid == -1)
1370 {
1371 mutt_perror(_("Can't open PGP subprocess"));
1372 mutt_file_fclose(&fp_sig);
1373 unlink(buf_string(sigfile));
1374 unlink(buf_string(signedfile));
1375 goto cleanup;
1376 }
1377
1378 if (!pgp_use_gpg_agent())
1379 fputs(mod_data->pgp_pass, fp_pgp_in);
1380 fputc('\n', fp_pgp_in);
1381 mutt_file_fclose(&fp_pgp_in);
1382
1383 /* Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1384 * recommended for future releases of PGP. */
1385 while (fgets(buf, sizeof(buf) - 1, fp_pgp_out))
1386 {
1387 if (mutt_str_equal("-----BEGIN PGP MESSAGE-----\n", buf))
1388 fputs("-----BEGIN PGP SIGNATURE-----\n", fp_sig);
1389 else if (mutt_str_equal("-----END PGP MESSAGE-----\n", buf))
1390 fputs("-----END PGP SIGNATURE-----\n", fp_sig);
1391 else
1392 fputs(buf, fp_sig);
1393 empty = false; /* got some output, so we're ok */
1394 }
1395
1396 /* check for errors from PGP */
1397 err = false;
1398 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1399 {
1400 err = true;
1401 fputs(buf, stdout);
1402 }
1403
1404 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1405 if (filter_wait(pid) && c_pgp_check_exit)
1406 empty = true;
1407
1408 mutt_file_fclose(&fp_pgp_err);
1409 mutt_file_fclose(&fp_pgp_out);
1410 unlink(buf_string(signedfile));
1411
1412 if (mutt_file_fclose(&fp_sig) != 0)
1413 {
1414 mutt_perror("fclose");
1415 unlink(buf_string(sigfile));
1416 goto cleanup;
1417 }
1418
1419 if (err)
1421 if (empty)
1422 {
1423 unlink(buf_string(sigfile));
1424 /* most likely error is a bad passphrase, so automatically forget it */
1426 goto cleanup; /* fatal error while signing */
1427 }
1428
1429 b_enc = mutt_body_new();
1430 b_enc->type = TYPE_MULTIPART;
1431 b_enc->subtype = mutt_str_dup("signed");
1432 b_enc->encoding = ENC_7BIT;
1433 b_enc->use_disp = false;
1434 b_enc->disposition = DISP_INLINE;
1435 rv = b_enc;
1436
1438 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-signature");
1439 mutt_param_set(&b_enc->parameter, "micalg", pgp_micalg(buf_string(sigfile)));
1440
1441 b_enc->parts = b;
1442
1443 b_enc->parts->next = mutt_body_new();
1444 b_enc = b_enc->parts->next;
1445 b_enc->type = TYPE_APPLICATION;
1446 b_enc->subtype = mutt_str_dup("pgp-signature");
1447 b_enc->filename = buf_strdup(sigfile);
1448 b_enc->use_disp = false;
1449 b_enc->disposition = DISP_NONE;
1450 b_enc->encoding = ENC_7BIT;
1451 b_enc->unlink = true; /* ok to remove this file after sending. */
1452 mutt_param_set(&b_enc->parameter, "name", "signature.asc");
1453
1454cleanup:
1455 buf_pool_release(&sigfile);
1456 buf_pool_release(&signedfile);
1457 return rv;
1458}
1459
1463char *pgp_class_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
1464{
1465 struct ListHead crypt_hook_list = STAILQ_HEAD_INITIALIZER(crypt_hook_list);
1466 struct ListNode *crypt_hook = NULL;
1467 const char *keyid = NULL;
1468 char *keylist = NULL;
1469 size_t keylist_size = 0;
1470 size_t keylist_used = 0;
1471 struct Address *p = NULL;
1472 struct PgpKeyInfo *k_info = NULL;
1473 const char *fqdn = mutt_fqdn(true, NeoMutt->sub);
1474 char buf[1024] = { 0 };
1475 bool key_selected;
1476 struct AddressList hookal = TAILQ_HEAD_INITIALIZER(hookal);
1477
1478 struct Address *a = NULL;
1479 const bool c_crypt_confirm_hook = cs_subset_bool(NeoMutt->sub, "crypt_confirm_hook");
1480 /* Iterate through each recipient address to find an encryption key */
1481 TAILQ_FOREACH(a, addrlist, entries)
1482 {
1483 key_selected = false;
1484 /* Check for crypt-hook overrides for this recipient */
1485 mutt_crypt_hook(&crypt_hook_list, a);
1486 crypt_hook = STAILQ_FIRST(&crypt_hook_list);
1487 do
1488 {
1489 p = a;
1490 k_info = NULL;
1491
1492 /* If a crypt-hook provides a key ID, confirm with the user unless
1493 * in opportunistic encryption mode */
1494 if (crypt_hook)
1495 {
1496 keyid = crypt_hook->data;
1497 enum QuadOption ans = MUTT_YES;
1498 if (!oppenc_mode && c_crypt_confirm_hook && isatty(STDIN_FILENO))
1499 {
1500 snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyid,
1501 buf_string(p->mailbox));
1502 ans = query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "crypt_confirm_hook");
1503 }
1504 if (ans == MUTT_YES)
1505 {
1506 if (crypt_is_numerical_keyid(keyid))
1507 {
1508 if (mutt_strn_equal(keyid, "0x", 2))
1509 keyid += 2;
1510 goto bypass_selection; /* you don't see this. */
1511 }
1512
1513 /* check for e-mail address */
1514 mutt_addrlist_clear(&hookal);
1515 if (strchr(keyid, '@') && (mutt_addrlist_parse(&hookal, keyid) != 0))
1516 {
1517 mutt_addrlist_qualify(&hookal, fqdn);
1518 p = TAILQ_FIRST(&hookal);
1519 }
1520 else if (!oppenc_mode)
1521 {
1523 }
1524 }
1525 else if (ans == MUTT_NO)
1526 {
1527 if (key_selected || STAILQ_NEXT(crypt_hook, entries))
1528 {
1529 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1530 continue;
1531 }
1532 }
1533 else if (ans == MUTT_ABORT)
1534 {
1535 FREE(&keylist);
1536 mutt_addrlist_clear(&hookal);
1537 mutt_list_free(&crypt_hook_list);
1538 return NULL;
1539 }
1540 }
1541
1542 /* If no key found yet, try looking up by address in the keyring */
1543 if (!k_info)
1544 {
1546 k_info = pgp_getkeybyaddr(p, KEYFLAG_CANENCRYPT, PGP_PUBRING, oppenc_mode);
1547 }
1548
1549 /* Last resort: prompt the user to enter a key ID interactively */
1550 if (!k_info && !oppenc_mode && isatty(STDIN_FILENO))
1551 {
1552 snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), buf_string(p->mailbox));
1554 }
1555
1556 if (!k_info)
1557 {
1558 FREE(&keylist);
1559 mutt_addrlist_clear(&hookal);
1560 mutt_list_free(&crypt_hook_list);
1561 return NULL;
1562 }
1563
1564 keyid = pgp_fpr_or_lkeyid(k_info);
1565
1566 bypass_selection:
1567 /* Append the selected key ID to the space-separated keylist string */
1568 keylist_size += mutt_str_len(keyid) + 4;
1569 MUTT_MEM_REALLOC(&keylist, keylist_size, char);
1570 sprintf(keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", keyid);
1571 keylist_used = mutt_str_len(keylist);
1572
1573 key_selected = true;
1574
1575 pgp_key_free(&k_info);
1576 mutt_addrlist_clear(&hookal);
1577
1578 if (crypt_hook)
1579 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1580
1581 } while (crypt_hook);
1582
1583 mutt_list_free(&crypt_hook_list);
1584 }
1585 return keylist;
1586}
1587
1594struct Body *pgp_class_encrypt_message(struct Body *b, char *keylist, bool sign,
1595 const struct AddressList *from)
1596{
1598 char buf[1024] = { 0 };
1599 FILE *fp_pgp_in = NULL, *fp_tmp = NULL;
1600 struct Body *b_enc = NULL;
1601 bool err = false;
1602 bool empty = false;
1603 pid_t pid;
1604 struct Buffer *tempfile = buf_pool_get();
1605 struct Buffer *pgpinfile = buf_pool_get();
1606
1607 buf_mktemp(tempfile);
1608 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1609 if (!fp_out)
1610 {
1611 mutt_perror("%s", buf_string(tempfile));
1612 goto cleanup;
1613 }
1614
1615 FILE *fp_pgp_err = mutt_file_mkstemp();
1616 if (!fp_pgp_err)
1617 {
1618 mutt_perror(_("Can't create temporary file"));
1619 unlink(buf_string(tempfile));
1620 mutt_file_fclose(&fp_out);
1621 goto cleanup;
1622 }
1623
1624 buf_mktemp(pgpinfile);
1625 fp_tmp = mutt_file_fopen(buf_string(pgpinfile), "w");
1626 if (!fp_tmp)
1627 {
1628 mutt_perror("%s", buf_string(pgpinfile));
1629 unlink(buf_string(tempfile));
1630 mutt_file_fclose(&fp_out);
1631 mutt_file_fclose(&fp_pgp_err);
1632 goto cleanup;
1633 }
1634
1635 if (sign)
1637
1638 mutt_write_mime_header(b, fp_tmp, NeoMutt->sub);
1639 fputc('\n', fp_tmp);
1640 mutt_write_mime_body(b, fp_tmp, NeoMutt->sub);
1641 mutt_file_fclose(&fp_tmp);
1642
1643 pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out),
1644 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, sign);
1645 if (pid == -1)
1646 {
1647 mutt_file_fclose(&fp_out);
1648 mutt_file_fclose(&fp_pgp_err);
1649 unlink(buf_string(pgpinfile));
1650 goto cleanup;
1651 }
1652
1653 if (sign)
1654 {
1655 if (!pgp_use_gpg_agent())
1656 fputs(mod_data->pgp_pass, fp_pgp_in);
1657 fputc('\n', fp_pgp_in);
1658 }
1659 mutt_file_fclose(&fp_pgp_in);
1660
1661 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1662 if (filter_wait(pid) && c_pgp_check_exit)
1663 empty = true;
1664
1665 unlink(buf_string(pgpinfile));
1666
1667 fflush(fp_out);
1668 rewind(fp_out);
1669 if (!empty)
1670 empty = (fgetc(fp_out) == EOF);
1671 mutt_file_fclose(&fp_out);
1672
1673 fflush(fp_pgp_err);
1674 rewind(fp_pgp_err);
1675 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1676 {
1677 err = true;
1678 fputs(buf, stdout);
1679 }
1680 mutt_file_fclose(&fp_pgp_err);
1681
1682 /* pause if there is any error output from PGP */
1683 if (err)
1685
1686 if (empty)
1687 {
1688 /* fatal error while trying to encrypt message */
1689 if (sign)
1690 pgp_class_void_passphrase(); /* just in case */
1691 unlink(buf_string(tempfile));
1692 goto cleanup;
1693 }
1694
1695 b_enc = mutt_body_new();
1696 b_enc->type = TYPE_MULTIPART;
1697 b_enc->subtype = mutt_str_dup("encrypted");
1698 b_enc->encoding = ENC_7BIT;
1699 b_enc->use_disp = false;
1700 b_enc->disposition = DISP_INLINE;
1701
1703 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-encrypted");
1704
1705 b_enc->parts = mutt_body_new();
1706 b_enc->parts->type = TYPE_APPLICATION;
1707 b_enc->parts->subtype = mutt_str_dup("pgp-encrypted");
1708 b_enc->parts->encoding = ENC_7BIT;
1709
1710 b_enc->parts->next = mutt_body_new();
1711 b_enc->parts->next->type = TYPE_APPLICATION;
1712 b_enc->parts->next->subtype = mutt_str_dup("octet-stream");
1713 b_enc->parts->next->encoding = ENC_7BIT;
1714 b_enc->parts->next->filename = buf_strdup(tempfile);
1715 b_enc->parts->next->use_disp = true;
1716 b_enc->parts->next->disposition = DISP_ATTACH;
1717 b_enc->parts->next->unlink = true; /* delete after sending the message */
1718 b_enc->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1719
1720cleanup:
1721 buf_pool_release(&tempfile);
1722 buf_pool_release(&pgpinfile);
1723 return b_enc;
1724}
1725
1729struct Body *pgp_class_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
1730{
1732 struct Body *b_enc = NULL;
1733 char body_charset[256] = { 0 };
1734 const char *from_charset = NULL;
1735 const char *send_charset = NULL;
1736 bool empty = false;
1737 bool err;
1738 char buf[256] = { 0 };
1739 pid_t pid;
1740 struct Buffer *pgpinfile = buf_pool_get();
1741 struct Buffer *pgpoutfile = buf_pool_get();
1742
1743 if (b->type != TYPE_TEXT)
1744 goto cleanup;
1745 if (!mutt_istr_equal(b->subtype, "plain"))
1746 goto cleanup;
1747
1748 FILE *fp_body = mutt_file_fopen(b->filename, "r");
1749 if (!fp_body)
1750 {
1751 mutt_perror("%s", b->filename);
1752 goto cleanup;
1753 }
1754
1755 buf_mktemp(pgpinfile);
1756 FILE *fp_pgp_in = mutt_file_fopen(buf_string(pgpinfile), "w");
1757 if (!fp_pgp_in)
1758 {
1759 mutt_perror("%s", buf_string(pgpinfile));
1760 mutt_file_fclose(&fp_body);
1761 goto cleanup;
1762 }
1763
1764 /* The following code is really correct: If noconv is set,
1765 * b's charset parameter contains the on-disk character set, and
1766 * we have to convert from that to utf-8. If noconv is not set,
1767 * we have to convert from $charset to utf-8. */
1768
1769 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
1770 if (b->noconv)
1771 from_charset = body_charset;
1772 else
1773 from_charset = cc_charset();
1774
1775 if (mutt_ch_is_us_ascii(body_charset))
1776 {
1777 send_charset = "us-ascii";
1778 mutt_file_copy_stream(fp_body, fp_pgp_in);
1779 }
1780 else
1781 {
1782 int c;
1783 struct FgetConv *fc = NULL;
1784
1785 if (flags & SEC_ENCRYPT)
1786 send_charset = "us-ascii";
1787 else
1788 send_charset = "utf-8";
1789
1790 /* fromcode is assumed to be correct: we set flags to 0 */
1791 fc = mutt_ch_fgetconv_open(fp_body, from_charset, "utf-8", MUTT_ICONV_NO_FLAGS);
1792 while ((c = mutt_ch_fgetconv(fc)) != EOF)
1793 fputc(c, fp_pgp_in);
1794
1796 }
1797 mutt_file_fclose(&fp_body);
1798 mutt_file_fclose(&fp_pgp_in);
1799
1800 buf_mktemp(pgpoutfile);
1801 FILE *fp_pgp_out = mutt_file_fopen(buf_string(pgpoutfile), "w+");
1802 FILE *fp_pgp_err = mutt_file_mkstemp();
1803 if (!fp_pgp_out || !fp_pgp_err)
1804 {
1805 mutt_perror("%s", fp_pgp_out ? "Can't create temporary file" : buf_string(pgpoutfile));
1806 unlink(buf_string(pgpinfile));
1807 if (fp_pgp_out)
1808 {
1809 mutt_file_fclose(&fp_pgp_out);
1810 unlink(buf_string(pgpoutfile));
1811 }
1812 mutt_file_fclose(&fp_pgp_err);
1813 goto cleanup;
1814 }
1815
1816 pid = pgp_invoke_traditional(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
1817 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, flags);
1818 if (pid == -1)
1819 {
1820 mutt_perror(_("Can't invoke PGP"));
1821 mutt_file_fclose(&fp_pgp_out);
1822 mutt_file_fclose(&fp_pgp_err);
1823 mutt_file_unlink(buf_string(pgpinfile));
1824 unlink(buf_string(pgpoutfile));
1825 goto cleanup;
1826 }
1827
1828 if (pgp_use_gpg_agent())
1829 *mod_data->pgp_pass = '\0';
1830 if (flags & SEC_SIGN)
1831 fprintf(fp_pgp_in, "%s\n", mod_data->pgp_pass);
1832 mutt_file_fclose(&fp_pgp_in);
1833
1834 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1835 if (filter_wait(pid) && c_pgp_check_exit)
1836 empty = true;
1837
1838 mutt_file_unlink(buf_string(pgpinfile));
1839
1840 fflush(fp_pgp_out);
1841 fflush(fp_pgp_err);
1842
1843 rewind(fp_pgp_out);
1844 rewind(fp_pgp_err);
1845
1846 if (!empty)
1847 empty = (fgetc(fp_pgp_out) == EOF);
1848 mutt_file_fclose(&fp_pgp_out);
1849
1850 err = false;
1851
1852 while (fgets(buf, sizeof(buf), fp_pgp_err))
1853 {
1854 err = true;
1855 fputs(buf, stdout);
1856 }
1857
1858 mutt_file_fclose(&fp_pgp_err);
1859
1860 if (err)
1862
1863 if (empty)
1864 {
1865 if (flags & SEC_SIGN)
1866 pgp_class_void_passphrase(); /* just in case */
1867 unlink(buf_string(pgpoutfile));
1868 goto cleanup;
1869 }
1870
1871 b_enc = mutt_body_new();
1872
1873 b_enc->encoding = ENC_7BIT;
1874
1875 b_enc->type = TYPE_TEXT;
1876 b_enc->subtype = mutt_str_dup("plain");
1877
1878 mutt_param_set(&b_enc->parameter, "x-action",
1879 (flags & SEC_ENCRYPT) ? "pgp-encrypted" : "pgp-signed");
1880 mutt_param_set(&b_enc->parameter, "charset", send_charset);
1881
1882 b_enc->filename = buf_strdup(pgpoutfile);
1883
1884 b_enc->disposition = DISP_NONE;
1885 b_enc->unlink = true;
1886
1887 b_enc->noconv = true;
1888 b_enc->use_disp = false;
1889
1890 if (!(flags & SEC_ENCRYPT))
1891 b_enc->encoding = b->encoding;
1892
1893cleanup:
1894 buf_pool_release(&pgpinfile);
1895 buf_pool_release(&pgpoutfile);
1896 return b_enc;
1897}
1898
1903{
1904 struct PgpKeyInfo *p = NULL;
1905 const char *prompt = NULL;
1906 const char *letters = NULL;
1907 const char *choices = NULL;
1908 char promptbuf[1024] = { 0 };
1909 int choice;
1910
1911 if (!(WithCrypto & APPLICATION_PGP))
1912 return e->security;
1913
1914 /* If autoinline and no crypto options set, then set inline. */
1915 const bool c_pgp_auto_inline = cs_subset_bool(NeoMutt->sub, "pgp_auto_inline");
1916 if (c_pgp_auto_inline &&
1917 !((e->security & APPLICATION_PGP) && (e->security & (SEC_SIGN | SEC_ENCRYPT))))
1918 {
1919 e->security |= SEC_INLINE;
1920 }
1921
1923
1924 char *mime_inline = NULL;
1925 if (e->security & SEC_INLINE)
1926 {
1927 /* L10N: The next string MUST have the same highlighted letter
1928 One of them will appear in each of the three strings marked "(inline"), below. */
1929 mime_inline = _("PGP/M(i)ME");
1930 }
1931 else
1932 {
1933 /* L10N: The previous string MUST have the same highlighted letter
1934 One of them will appear in each of the three strings marked "(inline"), below. */
1935 mime_inline = _("(i)nline");
1936 }
1937 /* Opportunistic encrypt is controlling encryption. Allow to toggle
1938 * between inline and mime, but not turn encryption on or off.
1939 * NOTE: "Signing" and "Clearing" only adjust the sign bit, so we have different
1940 * letter choices for those. */
1941 const bool c_crypt_opportunistic_encrypt = cs_subset_bool(NeoMutt->sub, "crypt_opportunistic_encrypt");
1942 if (c_crypt_opportunistic_encrypt && (e->security & SEC_OPPENCRYPT))
1943 {
1944 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
1945 {
1946 snprintf(promptbuf, sizeof(promptbuf),
1947 /* L10N: PGP options (inline) (opportunistic encryption is on) */
1948 _("PGP (s)ign, sign (a)s, %s format, (c)lear, or (o)ppenc mode off?"),
1949 mime_inline);
1950 prompt = promptbuf;
1951 /* L10N: PGP options (inline) (opportunistic encryption is on)
1952 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
1953 letters = _("saico");
1954 choices = "SaiCo";
1955 }
1956 else
1957 {
1958 /* L10N: PGP options (opportunistic encryption is on) */
1959 prompt = _("PGP (s)ign, sign (a)s, (c)lear, or (o)ppenc mode off?");
1960 /* L10N: PGP options (opportunistic encryption is on) */
1961 letters = _("saco");
1962 choices = "SaCo";
1963 }
1964 }
1965 else if (c_crypt_opportunistic_encrypt)
1966 {
1967 /* Opportunistic encryption option is set, but is toggled off
1968 * for this message. */
1969 /* When the message is not selected for signing or encryption, the toggle
1970 * between PGP/MIME and Traditional doesn't make sense. */
1971 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
1972 {
1973 snprintf(promptbuf, sizeof(promptbuf),
1974 /* L10N: PGP options (inline) (opportunistic encryption is off) */
1975 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s format, (c)lear, or (o)ppenc mode?"),
1976 mime_inline);
1977 prompt = promptbuf;
1978 /* L10N: PGP options (inline) (opportunistic encryption is off)
1979 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
1980 letters = _("esabico");
1981 choices = "esabicO";
1982 }
1983 else
1984 {
1985 /* L10N: PGP options (opportunistic encryption is off) */
1986 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, (c)lear, or (o)ppenc mode?");
1987 /* L10N: PGP options (opportunistic encryption is off) */
1988 letters = _("esabco");
1989 choices = "esabcO";
1990 }
1991 }
1992 else
1993 {
1994 /* Opportunistic encryption is unset */
1995 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
1996 {
1997 snprintf(promptbuf, sizeof(promptbuf),
1998 /* L10N: PGP options (inline) */
1999 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s format, or (c)lear?"),
2000 mime_inline);
2001 prompt = promptbuf;
2002 /* L10N: PGP options (inline)
2003 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
2004 letters = _("esabic");
2005 choices = "esabic";
2006 }
2007 else
2008 {
2009 /* L10N: PGP options */
2010 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, or (c)lear?");
2011 /* L10N: PGP options */
2012 letters = _("esabc");
2013 choices = "esabc";
2014 }
2015 }
2016
2017 choice = mw_multi_choice(prompt, letters);
2018 if (choice > 0)
2019 {
2020 switch (choices[choice - 1])
2021 {
2022 case 'a': /* sign (a)s */
2023 OptPgpCheckTrust = false;
2024
2025 p = pgp_ask_for_key(_("Sign as: "), NULL, KEYFLAG_NO_FLAGS, PGP_SECRING);
2026 if (p)
2027 {
2028 char input_signas[128] = { 0 };
2029 snprintf(input_signas, sizeof(input_signas), "0x%s", pgp_fpr_or_lkeyid(p));
2030 cs_subset_str_string_set(NeoMutt->sub, "pgp_sign_as", input_signas, NULL);
2031 pgp_key_free(&p);
2032
2033 e->security |= SEC_SIGN;
2034
2035 crypt_pgp_void_passphrase(); /* probably need a different passphrase */
2036 }
2037 break;
2038
2039 case 'b': /* (b)oth */
2040 e->security |= (SEC_ENCRYPT | SEC_SIGN);
2041 break;
2042
2043 case 'C':
2044 e->security &= ~SEC_SIGN;
2045 break;
2046
2047 case 'c': /* (c)lear */
2048 e->security &= ~(SEC_ENCRYPT | SEC_SIGN);
2049 break;
2050
2051 case 'e': /* (e)ncrypt */
2052 e->security |= SEC_ENCRYPT;
2053 e->security &= ~SEC_SIGN;
2054 break;
2055
2056 case 'i': /* toggle (i)nline */
2057 e->security ^= SEC_INLINE;
2058 break;
2059
2060 case 'O': /* oppenc mode on */
2063 break;
2064
2065 case 'o': /* oppenc mode off */
2067 break;
2068
2069 case 'S': /* (s)ign in oppenc mode */
2070 e->security |= SEC_SIGN;
2071 break;
2072
2073 case 's': /* (s)ign */
2074 e->security &= ~SEC_ENCRYPT;
2075 e->security |= SEC_SIGN;
2076 break;
2077 }
2078 }
2079
2080 return e->security;
2081}
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition address.c:685
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1469
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition address.c:480
Email Address Handling.
GUI display the mailboxes in a side panel.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const struct Regex * cs_subset_regex(const struct ConfigSubset *sub, const char *name)
Get a regex config item by name.
Definition helpers.c:217
long cs_subset_long(const struct ConfigSubset *sub, const char *name)
Get a long config item by name.
Definition helpers.c:95
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
const char * cc_charset(void)
Get the cached value of $charset.
Convenience wrapper for the core headers.
void crypt_opportunistic_encrypt(struct Email *e)
Can all recipients be determined.
Definition crypt.c:1050
bool crypt_is_numerical_keyid(const char *s)
Is this a numerical keyid.
Definition crypt.c:1484
SecurityFlags mutt_is_multipart_signed(struct Body *b)
Is a message signed?
Definition crypt.c:408
int mutt_is_valid_multipart_pgp_encrypted(struct Body *b)
Is this a valid multi-part encrypted message?
Definition crypt.c:467
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition crypt.c:504
void crypt_current_time(struct State *state, const char *app_name)
Print the current time.
Definition crypt.c:64
void crypt_convert_to_7bit(struct Body *b)
Convert an email to 7bit encoding.
Definition crypt.c:814
SecurityFlags mutt_is_application_pgp(const struct Body *b)
Does the message use PGP?
Definition crypt.c:548
Signing/encryption multiplexor.
void crypt_pgp_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition cryptglue.c:203
Wrapper around crypto functions.
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition curs_lib.c:175
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition curs_lib.c:102
void mutt_endwin(void)
Shutdown curses.
Definition curs_lib.c:153
Edit a string.
#define MUTT_COMP_PASS
Password mode (no echo)
Definition wdata.h:44
#define MUTT_COMP_UNBUFFERED
Ignore macro buffer.
Definition wdata.h:45
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition body.c:58
struct Body * mutt_body_new(void)
Create a new Body.
Definition body.c:44
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
Definition body.c:133
Structs that make up an email.
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition parse.c:1794
struct Body * mutt_read_mime_header(FILE *fp, bool digest)
Parse a MIME header.
Definition parse.c:1331
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition envelope.c:125
bool envlist_set(char ***envp, const char *name, const char *value, bool overwrite)
Set an environment variable.
Definition envlist.c:88
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition file.c:224
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition file.c:678
int mutt_file_copy_bytes(FILE *fp_in, FILE *fp_out, size_t size)
Copy some content from one file to another.
Definition file.c:192
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
Definition file.c:1432
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:648
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition file.c:156
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition file.h:40
bool OptDontHandlePgpKeys
(pseudo) used to extract PGP keys
Definition globals.c:46
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition globals.c:55
Global variables.
int pgp_class_application_handler(struct Body *b, struct State *state)
Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::applicat...
Definition pgp.c:469
int pgp_class_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
Definition pgp.c:1161
int pgp_class_encrypted_handler(struct Body *b, struct State *state)
Manage a PGP or S/MIME encrypted MIME part - Implements CryptModuleSpecs::encrypted_handler() -.
Definition pgp.c:1246
char * pgp_class_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
Definition pgp.c:1463
bool pgp_class_check_traditional(FILE *fp, struct Body *b, bool just_one)
Look for inline (non-MIME) PGP content - Implements CryptModuleSpecs::pgp_check_traditional() -.
Definition pgp.c:867
struct Body * pgp_class_encrypt_message(struct Body *b, char *keylist, bool sign, const struct AddressList *from)
PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.
Definition pgp.c:1594
void pgp_class_extract_key_from_attachment(FILE *fp, struct Body *b)
Extract PGP key from an attachment - Implements CryptModuleSpecs::pgp_extract_key_from_attachment() -...
Definition pgp.c:1001
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
struct Body * pgp_class_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
Create an inline PGP encrypted, signed email - Implements CryptModuleSpecs::pgp_traditional_encryptsi...
Definition pgp.c:1729
SecurityFlags pgp_class_send_menu(struct Email *e)
Ask the user whether to sign and/or encrypt the email - Implements CryptModuleSpecs::send_menu() -.
Definition pgp.c:1902
struct Body * pgp_class_sign_message(struct Body *b, const struct AddressList *from)
Cryptographically sign the Body of a message - Implements CryptModuleSpecs::sign_message() -.
Definition pgp.c:1331
bool pgp_class_valid_passphrase(void)
Ensure we have a valid passphrase - Implements CryptModuleSpecs::valid_passphrase() -.
Definition pgp.c:81
int pgp_class_verify_one(struct Body *b, struct State *state, const char *tempfile)
Check a signed MIME part against a signature - Implements CryptModuleSpecs::verify_one() -.
Definition pgp.c:896
void pgp_class_void_passphrase(void)
Forget the cached passphrase - Implements CryptModuleSpecs::void_passphrase() -.
Definition pgp.c:71
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:467
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition question.c:62
int mutt_protected_headers_handler(struct Body *b_email, struct State *state)
Handler for protected headers - Implements handler_t -.
Definition crypt.c:1122
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
#define mutt_perror(...)
Definition logging2.h:95
Convenience wrapper for the gui headers.
int mutt_body_handler(struct Body *b, struct State *state)
Handler for the Body of an email.
Definition handler.c:1664
void mutt_decode_attachment(const struct Body *b, struct State *state)
Decode an email's attachment.
Definition handler.c:1938
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition lib.h:60
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition exec.c:319
Hook Commands.
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
@ ENC_7BIT
7-bit text
Definition mime.h:49
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition mime.h:37
@ TYPE_APPLICATION
Type: 'application/*'.
Definition mime.h:33
@ TYPE_TEXT
Type: 'text/*'.
Definition mime.h:38
@ DISP_ATTACH
Content is attached.
Definition mime.h:63
@ DISP_INLINE
Content is inline.
Definition mime.h:62
@ DISP_NONE
No preferred disposition.
Definition mime.h:65
#define is_multipart(body)
Check if a body part is multipart or a message container.
Definition mime.h:85
@ MODULE_ID_NCRYPT
ModuleNcrypt, Ncrypt
Definition module_api.h:80
void mutt_generate_boundary(struct ParameterList *pl)
Create a unique boundary id for a MIME part.
Definition multipart.c:93
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
Definition charset.c:880
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
Definition charset.c:966
struct FgetConv * mutt_ch_fgetconv_open(FILE *fp, const char *from, const char *to, uint8_t flags)
Prepare a file for charset conversion.
Definition charset.c:919
char * mutt_ch_fgetconvs(char *buf, size_t buflen, struct FgetConv *fc)
Convert a file's charset into a string buffer.
Definition charset.c:1028
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
Definition charset.c:948
#define MUTT_ICONV_HOOK_FROM
apply charset-hooks to fromcode
Definition charset.h:67
#define mutt_ch_is_us_ascii(str)
Definition charset.h:108
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition charset.h:66
time_t mutt_date_add_timeout(time_t now, time_t timeout)
Safely add a timeout to a given time_t value.
Definition date.c:891
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:228
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition regex.c:614
void state_attach_puts(struct State *state, const char *t)
Write a string to the state.
Definition state.c:104
void state_prefix_putc(struct State *state, char c)
Write a prefixed character to the state.
Definition state.c:168
#define state_puts(STATE, STR)
Definition state.h:58
#define state_set_prefix(state)
Definition state.h:56
#define STATE_DISPLAY
Output is displayed to the user.
Definition state.h:33
#define state_putc(STATE, STR)
Definition state.h:59
#define STATE_VERIFY
Perform signature verification.
Definition state.h:34
#define STATE_NO_FLAGS
No flags are set.
Definition state.h:32
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:429
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
char * mutt_str_skip_whitespace(const char *p)
Find the first non-whitespace character in a string.
Definition string.c:556
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:586
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
Many unsorted constants and some structs.
int mutt_decode_save_attachment(FILE *fp, struct Body *b, const char *path, StateFlags flags, enum SaveAttach opt)
Decode, then save an attachment.
@ MUTT_SAVE_NO_FLAGS
Overwrite existing file (the default)
Definition mutt_attach.h:58
API for encryption/signing of emails.
#define SEC_INLINE
Email has an inline signature.
Definition lib.h:94
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition lib.h:85
#define SEC_OPPENCRYPT
Opportunistic encrypt mode.
Definition lib.h:95
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:99
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition lib.h:137
#define KEYFLAG_SUBKEY
Key is a subkey.
Definition lib.h:143
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition lib.h:135
#define SEC_ENCRYPT
Email is encrypted.
Definition lib.h:87
#define WithCrypto
Definition lib.h:125
#define SEC_SIGN
Email is signed.
Definition lib.h:88
Ncrypt private Module data.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:665
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition parameter.c:111
char * pgp_long_keyid(struct PgpKeyInfo *k)
Get a key's long id.
Definition pgp.c:162
char * pgp_this_keyid(struct PgpKeyInfo *k)
Get the ID of this key.
Definition pgp.c:188
static struct Body * pgp_decrypt_part(struct Body *a, struct State *state, FILE *fp_out, struct Body *p)
Decrypt part of a PGP message.
Definition pgp.c:1024
static char * pgp_fingerprint(struct PgpKeyInfo *k)
Get the key's fingerprint.
Definition pgp.c:213
static int pgp_check_pgp_decryption_okay_regex(FILE *fp_in)
Check PGP output to look for successful outcome.
Definition pgp.c:300
char * pgp_keyid(struct PgpKeyInfo *k)
Get the ID of the main (parent) key.
Definition pgp.c:201
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition pgp.c:231
static struct PgpKeyInfo * key_parent(struct PgpKeyInfo *k)
Find a key's parent (if it's a subkey)
Definition pgp.c:148
static int pgp_copy_checksig(FILE *fp_in, FILE *fp_out)
Copy PGP output and look for signs of a good signature.
Definition pgp.c:248
char * pgp_short_keyid(struct PgpKeyInfo *k)
Get a key's short id.
Definition pgp.c:174
static void pgp_copy_clearsigned(FILE *fp_in, struct State *state, char *charset)
Copy a clearsigned message, stripping the signature.
Definition pgp.c:421
static void pgp_extract_keys_from_attachment(FILE *fp, struct Body *b)
Extract pgp keys from messages/attachments.
Definition pgp.c:969
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition pgp.c:124
static int pgp_check_decryption_okay(FILE *fp_in)
Check GPG output for status codes.
Definition pgp.c:354
static bool pgp_check_traditional_one_body(FILE *fp, struct Body *b)
Check the body of an inline PGP message.
Definition pgp.c:799
PGP sign, encrypt, check routines.
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
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_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.
struct PgpKeyInfo * pgp_ask_for_key(char *tag, const char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition pgpkey.c:196
struct PgpKeyInfo * pgp_getkeybyaddr(struct Address *a, KeyFlags abilities, enum PgpRing keyring, bool oppenc_mode)
Find a PGP key by address.
Definition pgpkey.c:373
struct PgpKeyInfo * pgp_getkeybystr(const char *cp, KeyFlags abilities, enum PgpRing keyring)
Find a PGP key by string.
Definition pgpkey.c:512
PGP key management routines.
@ PGP_SECRING
Secret keys.
Definition pgpkey.h:40
@ PGP_PUBRING
Public keys.
Definition pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition pgplib.c:201
Misc PGP helper routines.
const char * pgp_micalg(const char *fname)
Find the hash algorithm of a file.
Definition pgpmicalg.c:228
Identify the hash algorithm from a PGP signature.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
QuadOption
Possible values for a quad-option.
Definition quad.h:36
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition quad.h:37
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
Ask the user a question.
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition question.c:357
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define STAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define STAILQ_FIRST(head)
Definition queue.h:388
#define TAILQ_FIRST(head)
Definition queue.h:780
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
int mutt_write_mime_body(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition body.c:302
int mutt_write_mime_header(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition header.c:757
Convenience wrapper for the send headers.
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition sendlib.c:713
An email address.
Definition address.h:35
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
The body of an email.
Definition body.h:36
char * d_filename
filename to be used for the content-disposition header If NULL, filename is used instead.
Definition body.h:56
struct Body * parts
parts of a multipart or message/rfc822
Definition body.h:73
LOFF_T offset
offset where the actual data begins
Definition body.h:52
bool noconv
Don't do character set conversion.
Definition body.h:46
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition body.h:68
struct Envelope * mime_headers
Memory hole protected headers.
Definition body.h:76
LOFF_T length
length (in bytes) of attachment
Definition body.h:53
struct ParameterList parameter
Parameters of the content-type.
Definition body.h:63
bool use_disp
Content-Disposition uses filename= ?
Definition body.h:47
unsigned int disposition
content-disposition, ContentDisposition
Definition body.h:42
struct Body * next
next attachment in the list
Definition body.h:72
char * subtype
content-type subtype
Definition body.h:61
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition body.h:41
bool goodsig
Good cryptographic signature.
Definition body.h:45
unsigned int type
content-type primary type, ContentType
Definition body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition body.h:59
String manipulation buffer.
Definition buffer.h:36
The envelope/body of an email.
Definition email.h:39
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition email.h:43
Cursor for converting a file's encoding.
Definition charset.h:45
FILE * fp
File to read from.
Definition charset.h:46
char * p
Current position in output buffer.
Definition charset.h:50
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
Ncrypt private Module data.
Definition module_data.h:38
char pgp_pass[1024]
Cached PGP Passphrase.
Definition module_data.h:50
time_t pgp_exptime
Unix time when pgp_pass expires.
Definition module_data.h:51
Container for Accounts, Notifications.
Definition neomutt.h:41
char ** env
Private copy of the environment variables.
Definition neomutt.h:57
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Information about a PGP key.
Definition pgplib.h:49
char * keyid
Key ID.
Definition pgplib.h:50
KeyFlags flags
Key flags.
Definition pgplib.h:53
char * fingerprint
Key fingerprint.
Definition pgplib.h:51
struct PgpKeyInfo * parent
Parent key.
Definition pgplib.h:58
Cached regular expression.
Definition regex3.h:85
regex_t * regex
compiled expression
Definition regex3.h:87
Keep track when processing files.
Definition state.h:48
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition state.h:52
FILE * fp_out
File to write to.
Definition state.h:50
FILE * fp_in
File to read from.
Definition state.h:49
const char * prefix
String to add to the beginning of each output line.
Definition state.h:51
int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Set a config item by string.
Definition subset.c:392
#define buf_mktemp(buf)
Definition tmp.h:33
#define mutt_file_mkstemp()
Definition tmp.h:36