NeoMutt  2025-12-11-980-ge38c27
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 = 0;
260
261 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NONE)))
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 = 0;
309
310 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NONE)))
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;
358 char *s = NULL;
359 size_t linelen = 0;
360 int inside_decrypt = 0;
361
362 const bool c_pgp_check_gpg_decrypt_status_fd = cs_subset_bool(NeoMutt->sub, "pgp_check_gpg_decrypt_status_fd");
363 if (!c_pgp_check_gpg_decrypt_status_fd)
365
366 while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NONE)))
367 {
368 size_t plen = mutt_str_startswith(line, "[GNUPG:] ");
369 if (plen == 0)
370 continue;
371 s = line + plen;
372 mutt_debug(LL_DEBUG2, "checking \"%s\"\n", line);
373 if (mutt_str_startswith(s, "BEGIN_DECRYPTION"))
374 {
375 inside_decrypt = 1;
376 }
377 else if (mutt_str_startswith(s, "END_DECRYPTION"))
378 {
379 inside_decrypt = 0;
380 }
381 else if (mutt_str_startswith(s, "PLAINTEXT"))
382 {
383 if (!inside_decrypt)
384 {
385 mutt_debug(LL_DEBUG2, " PLAINTEXT encountered outside of DECRYPTION\n");
386 rc = -2;
387 break;
388 }
389 }
390 else if (mutt_str_startswith(s, "DECRYPTION_FAILED"))
391 {
392 mutt_debug(LL_DEBUG2, " DECRYPTION_FAILED encountered. Failure\n");
393 rc = -3;
394 break;
395 }
396 else if (mutt_str_startswith(s, "DECRYPTION_OKAY"))
397 {
398 /* Don't break out because we still have to check for
399 * PLAINTEXT outside of the decryption boundaries. */
400 mutt_debug(LL_DEBUG2, " DECRYPTION_OKAY encountered\n");
401 rc = 0;
402 }
403 }
404 FREE(&line);
405
406 return rc;
407}
408
422static void pgp_copy_clearsigned(FILE *fp_in, struct State *state, char *charset)
423{
424 char buf[8192] = { 0 };
425 bool complete, armor_header;
426
427 rewind(fp_in);
428
429 /* fromcode comes from the MIME Content-Type charset label. It might
430 * be a wrong label, so we want the ability to do corrections via
431 * charset-hooks. Therefore we set flags to MUTT_ICONV_HOOK_FROM. */
432 struct FgetConv *fc = mutt_ch_fgetconv_open(fp_in, charset, cc_charset(), MUTT_ICONV_HOOK_FROM);
433
434 for (complete = true, armor_header = true;
435 mutt_ch_fgetconvs(buf, sizeof(buf), fc); complete = (strchr(buf, '\n')))
436 {
437 if (!complete)
438 {
439 if (!armor_header)
440 state_puts(state, buf);
441 continue;
442 }
443
444 if (mutt_str_equal(buf, "-----BEGIN PGP SIGNATURE-----\n"))
445 break;
446
447 if (armor_header)
448 {
449 char *p = mutt_str_skip_whitespace(buf);
450 if (*p == '\0')
451 armor_header = false;
452 continue;
453 }
454
455 if (state->prefix)
456 state_puts(state, state->prefix);
457
458 if ((buf[0] == '-') && (buf[1] == ' '))
459 state_puts(state, buf + 2);
460 else
461 state_puts(state, buf);
462 }
463
465}
466
470int pgp_class_application_handler(struct Body *b, struct State *state)
471{
473 bool could_not_decrypt = false;
474 int decrypt_okay_rc = 0;
475 int needpass = -1;
476 bool pgp_keyblock = false;
477 bool clearsign = false;
478 int rc = -1;
479 int c = 1;
480 long bytes;
481 LOFF_T last_pos;
482 LOFF_T offset;
483 char buf[8192] = { 0 };
484 FILE *fp_pgp_out = NULL;
485 FILE *fp_pgp_in = NULL;
486 FILE *fp_pgp_err = NULL;
487 FILE *fp_tmp = NULL;
488 pid_t pid;
489 struct Buffer *tempfile = buf_pool_get();
490
491 bool maybe_goodsig = true;
492 bool have_any_sigs = false;
493
494 char *gpgcharset = NULL;
495 char body_charset[256] = { 0 };
496 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
497
498 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
499 {
500 return -1;
501 }
502 last_pos = b->offset;
503
504 for (bytes = b->length; bytes > 0;)
505 {
506 if (!fgets(buf, sizeof(buf), state->fp_in))
507 break;
508
509 offset = ftello(state->fp_in);
510 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
511 last_pos = offset;
512
513 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
514 if (plen != 0)
515 {
516 needpass = false;
517 clearsign = false;
518 could_not_decrypt = false;
519 decrypt_okay_rc = 0;
520
521 if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
522 {
523 needpass = 1;
524 }
525 else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
526 {
527 clearsign = true;
528 }
529 else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
530 {
531 pgp_keyblock = true;
532 }
533 else
534 {
535 /* XXX we may wish to recode here */
536 if (state->prefix)
537 state_puts(state, state->prefix);
538 state_puts(state, buf);
539 continue;
540 }
541
542 have_any_sigs = have_any_sigs || (clearsign && (state->flags & STATE_VERIFY));
543
544 /* Copy PGP material to temporary file */
545 buf_mktemp(tempfile);
546 fp_tmp = mutt_file_fopen(buf_string(tempfile), "w+");
547 if (!fp_tmp)
548 {
549 mutt_perror("%s", buf_string(tempfile));
550 FREE(&gpgcharset);
551 goto out;
552 }
553
554 fputs(buf, fp_tmp);
555 while ((bytes > 0) && fgets(buf, sizeof(buf) - 1, state->fp_in))
556 {
557 offset = ftello(state->fp_in);
558 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
559 last_pos = offset;
560
561 fputs(buf, fp_tmp);
562
563 if ((needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf)) ||
564 (!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
565 mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf))))
566 {
567 break;
568 }
569 /* remember optional Charset: armor header as defined by RFC4880 */
570 if (mutt_str_startswith(buf, "Charset: "))
571 {
572 size_t l = 0;
573 FREE(&gpgcharset);
574 gpgcharset = mutt_str_dup(buf + 9);
575 l = mutt_str_len(gpgcharset);
576 if ((l > 0) && (gpgcharset[l - 1] == '\n'))
577 gpgcharset[l - 1] = 0;
578 if (!mutt_ch_check_charset(gpgcharset, false))
579 mutt_str_replace(&gpgcharset, "UTF-8");
580 }
581 }
582
583 /* leave fp_tmp open in case we still need it - but flush it! */
584 fflush(fp_tmp);
585
586 /* Invoke PGP if needed */
587 if (!clearsign || (state->flags & STATE_VERIFY))
588 {
589 fp_pgp_out = mutt_file_mkstemp();
590 if (!fp_pgp_out)
591 {
592 mutt_perror(_("Can't create temporary file"));
593 goto out;
594 }
595
596 fp_pgp_err = mutt_file_mkstemp();
597 if (!fp_pgp_err)
598 {
599 mutt_perror(_("Can't create temporary file"));
600 goto out;
601 }
602
603 pid = pgp_invoke_decode(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
604 fileno(fp_pgp_err), buf_string(tempfile),
605 (needpass != 0));
606 if (pid == -1)
607 {
608 mutt_file_fclose(&fp_pgp_out);
609 maybe_goodsig = false;
610 fp_pgp_in = NULL;
611 state_attach_puts(state, _("[-- Error: unable to create PGP subprocess --]\n"));
612 }
613 else /* PGP started successfully */
614 {
615 if (needpass)
616 {
619 if (pgp_use_gpg_agent())
620 *mod_data->pgp_pass = '\0';
621 fprintf(fp_pgp_in, "%s\n", mod_data->pgp_pass);
622 }
623
624 mutt_file_fclose(&fp_pgp_in);
625
626 int wait_filter_rc = filter_wait(pid);
627
628 fflush(fp_pgp_err);
629 /* If we are expecting an encrypted message, verify status fd output.
630 * Note that BEGIN PGP MESSAGE does not guarantee the content is encrypted,
631 * so we need to be more selective about the value of decrypt_okay_rc.
632 *
633 * -3 indicates we actively found a DECRYPTION_FAILED.
634 * -2 and -1 indicate part or all of the content was plaintext. */
635 if (needpass)
636 {
637 rewind(fp_pgp_err);
638 decrypt_okay_rc = pgp_check_decryption_okay(fp_pgp_err);
639 if (decrypt_okay_rc <= -3)
640 mutt_file_fclose(&fp_pgp_out);
641 }
642
643 if (state->flags & STATE_DISPLAY)
644 {
645 rewind(fp_pgp_err);
646 crypt_current_time(state, "PGP");
647 int checksig_rc = pgp_copy_checksig(fp_pgp_err, state->fp_out);
648
649 if (checksig_rc == 0)
650 have_any_sigs = true;
651 /* Sig is bad if
652 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
653 * Sig _is_ correct if
654 * gpg_good_sign="" && pgp_decode_command returned 0 */
655 if (checksig_rc == -1 || (wait_filter_rc != 0))
656 maybe_goodsig = false;
657
658 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
659 }
660 if (pgp_use_gpg_agent())
661 {
663 }
664 }
665
666 /* treat empty result as sign of failure */
667 /* TODO: maybe on failure neomutt should include the original undecoded text. */
668 if (fp_pgp_out)
669 {
670 rewind(fp_pgp_out);
671 c = fgetc(fp_pgp_out);
672 ungetc(c, fp_pgp_out);
673 }
674 if (!clearsign && (!fp_pgp_out || (c == EOF)))
675 {
676 could_not_decrypt = true;
678 }
679
680 if ((could_not_decrypt || (decrypt_okay_rc <= -3)) && !(state->flags & STATE_DISPLAY))
681 {
682 mutt_error(_("Could not decrypt PGP message"));
683 goto out;
684 }
685 }
686
687 /* Now, copy cleartext to the screen. */
688 if (state->flags & STATE_DISPLAY)
689 {
690 if (needpass)
691 state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
692 else if (pgp_keyblock)
693 state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
694 else
695 state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
696 }
697
698 if (clearsign)
699 {
700 rewind(fp_tmp);
701 pgp_copy_clearsigned(fp_tmp, state, body_charset);
702 }
703 else if (fp_pgp_out)
704 {
705 struct FgetConv *fc = NULL;
706 int ch;
707 char *expected_charset = (gpgcharset && *gpgcharset) ? gpgcharset : "utf-8";
708
709 mutt_debug(LL_DEBUG3, "pgp: recoding inline from [%s] to [%s]\n",
710 expected_charset, cc_charset());
711
712 rewind(fp_pgp_out);
713 state_set_prefix(state);
714 fc = mutt_ch_fgetconv_open(fp_pgp_out, expected_charset, cc_charset(),
716 while ((ch = mutt_ch_fgetconv(fc)) != EOF)
717 state_prefix_putc(state, ch);
719 }
720
721 /* Multiple PGP blocks can exist, so these need to be closed and
722 * unlinked inside the loop. */
723 mutt_file_fclose(&fp_tmp);
724 mutt_file_unlink(buf_string(tempfile));
725 mutt_file_fclose(&fp_pgp_out);
726 mutt_file_fclose(&fp_pgp_err);
727
728 if (state->flags & STATE_DISPLAY)
729 {
730 state_putc(state, '\n');
731 if (needpass)
732 {
733 state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
734 if (could_not_decrypt || (decrypt_okay_rc <= -3))
735 {
736 mutt_error(_("Could not decrypt PGP message"));
737 }
738 else if (decrypt_okay_rc < 0)
739 {
740 /* L10N: You will see this error message if (1) you are decrypting
741 (not encrypting) something and (2) it is a plaintext. So the
742 message does not mean "You failed to encrypt the message." */
743 mutt_error(_("PGP message is not encrypted"));
744 }
745 else
746 {
747 mutt_message(_("PGP message successfully decrypted"));
748 }
749 }
750 else if (pgp_keyblock)
751 {
752 state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
753 }
754 else
755 {
756 state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
757 }
758 }
759 }
760 else
761 {
762 /* A traditional PGP part may mix signed and unsigned content */
763 /* XXX we may wish to recode here */
764 if (state->prefix)
765 state_puts(state, state->prefix);
766 state_puts(state, buf);
767 }
768 }
769
770 rc = 0;
771
772out:
773 b->goodsig = (maybe_goodsig && have_any_sigs);
774
775 if (fp_tmp)
776 {
777 mutt_file_fclose(&fp_tmp);
778 mutt_file_unlink(buf_string(tempfile));
779 }
780 mutt_file_fclose(&fp_pgp_out);
781 mutt_file_fclose(&fp_pgp_err);
782
783 buf_pool_release(&tempfile);
784
785 FREE(&gpgcharset);
786
787 if (needpass == -1)
788 {
789 state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
790 return -1;
791 }
792
793 return rc;
794}
795
803static bool pgp_check_traditional_one_body(FILE *fp, struct Body *b)
804{
805 struct Buffer *tempfile = NULL;
806 char buf[8192] = { 0 };
807 bool rc = false;
808
809 bool sgn = false;
810 bool enc = false;
811 bool key = false;
812
813 if (b->type != TYPE_TEXT)
814 goto cleanup;
815
816 tempfile = buf_pool_get();
817 buf_mktemp(tempfile);
819 {
820 unlink(buf_string(tempfile));
821 goto cleanup;
822 }
823
824 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "r");
825 if (!fp_tmp)
826 {
827 unlink(buf_string(tempfile));
828 goto cleanup;
829 }
830
831 while (fgets(buf, sizeof(buf), fp_tmp))
832 {
833 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
834 if (plen != 0)
835 {
836 if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
837 enc = true;
838 else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
839 sgn = true;
840 else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
841 key = true;
842 }
843 }
844 mutt_file_fclose(&fp_tmp);
845 unlink(buf_string(tempfile));
846
847 if (!enc && !sgn && !key)
848 goto cleanup;
849
850 /* fix the content type */
851
852 mutt_param_set(&b->parameter, "format", "fixed");
853 if (enc)
854 mutt_param_set(&b->parameter, "x-action", "pgp-encrypted");
855 else if (sgn)
856 mutt_param_set(&b->parameter, "x-action", "pgp-signed");
857 else if (key)
858 mutt_param_set(&b->parameter, "x-action", "pgp-keys");
859
860 rc = true;
861
862cleanup:
863 buf_pool_release(&tempfile);
864 return rc;
865}
866
870bool pgp_class_check_traditional(FILE *fp, struct Body *b, bool just_one)
871{
872 bool rc = false;
873 int r;
874 for (; b; b = b->next)
875 {
876 if (!just_one && is_multipart(b))
877 {
878 rc = pgp_class_check_traditional(fp, b->parts, false) || rc;
879 }
880 else if (b->type == TYPE_TEXT)
881 {
883 if (r)
884 rc = rc || r;
885 else
886 rc = pgp_check_traditional_one_body(fp, b) || rc;
887 }
888
889 if (just_one)
890 break;
891 }
892
893 return rc;
894}
895
899int pgp_class_verify_one(struct Body *b, struct State *state, const char *tempfile)
900{
901 FILE *fp_pgp_out = NULL;
902 pid_t pid;
903 int badsig = -1;
904 struct Buffer *sigfile = buf_pool_get();
905
906 buf_printf(sigfile, "%s.asc", tempfile);
907
908 FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
909 if (!fp_sig)
910 {
911 mutt_perror("%s", buf_string(sigfile));
912 goto cleanup;
913 }
914
915 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
916 {
917 mutt_file_fclose(&fp_sig);
918 goto cleanup;
919 }
920 mutt_file_copy_bytes(state->fp_in, fp_sig, b->length);
921 mutt_file_fclose(&fp_sig);
922
923 FILE *fp_pgp_err = mutt_file_mkstemp();
924 if (!fp_pgp_err)
925 {
926 mutt_perror(_("Can't create temporary file"));
927 unlink(buf_string(sigfile));
928 goto cleanup;
929 }
930
931 crypt_current_time(state, "PGP");
932
933 pid = pgp_invoke_verify(NULL, &fp_pgp_out, NULL, -1, -1, fileno(fp_pgp_err),
934 tempfile, buf_string(sigfile));
935 if (pid != -1)
936 {
937 if (pgp_copy_checksig(fp_pgp_out, state->fp_out) >= 0)
938 badsig = 0;
939
940 mutt_file_fclose(&fp_pgp_out);
941 fflush(fp_pgp_err);
942 rewind(fp_pgp_err);
943
944 if (pgp_copy_checksig(fp_pgp_err, state->fp_out) >= 0)
945 badsig = 0;
946
947 const int rv = filter_wait(pid);
948 if (rv)
949 badsig = -1;
950
951 mutt_debug(LL_DEBUG1, "filter_wait returned %d\n", rv);
952 }
953
954 mutt_file_fclose(&fp_pgp_err);
955
956 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
957
959
960cleanup:
961 buf_pool_release(&sigfile);
962
963 mutt_debug(LL_DEBUG1, "returning %d\n", badsig);
964 return badsig;
965}
966
972static void pgp_extract_keys_from_attachment(FILE *fp, struct Body *b)
973{
974 struct State state = { 0 };
975 struct Buffer *tempfile = buf_pool_get();
976
977 buf_mktemp(tempfile);
978 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
979 if (!fp_tmp)
980 {
981 mutt_perror("%s", buf_string(tempfile));
982 goto cleanup;
983 }
984
985 state.fp_in = fp;
986 state.fp_out = fp_tmp;
987
988 mutt_body_handler(b, &state);
989
990 mutt_file_fclose(&fp_tmp);
991
994
995 mutt_file_unlink(buf_string(tempfile));
996
997cleanup:
998 buf_pool_release(&tempfile);
999}
1000
1005{
1006 if (!fp)
1007 {
1008 mutt_error(_("Internal error. Please submit a bug report."));
1009 return;
1010 }
1011
1012 mutt_endwin();
1013
1014 OptDontHandlePgpKeys = true;
1016 OptDontHandlePgpKeys = false;
1017}
1018
1027static struct Body *pgp_decrypt_part(struct Body *a, struct State *state,
1028 FILE *fp_out, struct Body *p)
1029{
1030 if (!a || !state || !fp_out || !p)
1031 return NULL;
1032
1034 char buf[1024] = { 0 };
1035 FILE *fp_pgp_in = NULL;
1036 FILE *fp_pgp_out = NULL;
1037 FILE *fp_pgp_tmp = NULL;
1038 struct Body *tattach = NULL;
1039 pid_t pid;
1040 int rv;
1041 struct Buffer *tempfile = buf_pool_get();
1042
1043 FILE *fp_pgp_err = mutt_file_mkstemp();
1044 if (!fp_pgp_err)
1045 {
1046 mutt_perror(_("Can't create temporary file"));
1047 goto cleanup;
1048 }
1049
1050 buf_mktemp(tempfile);
1051 fp_pgp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
1052 if (!fp_pgp_tmp)
1053 {
1054 mutt_perror("%s", buf_string(tempfile));
1055 mutt_file_fclose(&fp_pgp_err);
1056 goto cleanup;
1057 }
1058
1059 /* Position the stream at the beginning of the body, and send the data to
1060 * the temporary file. */
1061
1062 if (!mutt_file_seek(state->fp_in, a->offset, SEEK_SET))
1063 {
1064 mutt_file_fclose(&fp_pgp_tmp);
1065 mutt_file_fclose(&fp_pgp_err);
1066 goto cleanup;
1067 }
1068 mutt_file_copy_bytes(state->fp_in, fp_pgp_tmp, a->length);
1069 mutt_file_fclose(&fp_pgp_tmp);
1070
1071 pid = pgp_invoke_decrypt(&fp_pgp_in, &fp_pgp_out, NULL, -1, -1,
1072 fileno(fp_pgp_err), buf_string(tempfile));
1073 if (pid == -1)
1074 {
1075 mutt_file_fclose(&fp_pgp_err);
1076 unlink(buf_string(tempfile));
1077 if (state->flags & STATE_DISPLAY)
1078 {
1079 state_attach_puts(state, _("[-- Error: could not create a PGP subprocess --]\n\n"));
1080 }
1081 goto cleanup;
1082 }
1083
1084 /* send the PGP passphrase to the subprocess. Never do this if the agent is
1085 * active, because this might lead to a passphrase send as the message. */
1086 if (!pgp_use_gpg_agent())
1087 fputs(mod_data->pgp_pass, fp_pgp_in);
1088 fputc('\n', fp_pgp_in);
1089 mutt_file_fclose(&fp_pgp_in);
1090
1091 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
1092 * read_mime_header has a hard time parsing the message. */
1093 while (fgets(buf, sizeof(buf) - 1, fp_pgp_out))
1094 {
1095 size_t len = mutt_str_len(buf);
1096 if ((len > 1) && (buf[len - 2] == '\r'))
1097 strcpy(buf + len - 2, "\n");
1098 fputs(buf, fp_out);
1099 }
1100
1101 mutt_file_fclose(&fp_pgp_out);
1102
1103 rv = filter_wait(pid);
1104 const bool c_pgp_use_gpg_agent = cs_subset_bool(NeoMutt->sub, "pgp_use_gpg_agent");
1105 if (c_pgp_use_gpg_agent)
1107
1108 mutt_file_unlink(buf_string(tempfile));
1109
1110 fflush(fp_pgp_err);
1111 rewind(fp_pgp_err);
1112 if (pgp_check_decryption_okay(fp_pgp_err) < 0)
1113 {
1114 mutt_error(_("Decryption failed"));
1116 mutt_file_fclose(&fp_pgp_err);
1117 goto cleanup;
1118 }
1119
1120 if (state->flags & STATE_DISPLAY)
1121 {
1122 rewind(fp_pgp_err);
1123 if ((pgp_copy_checksig(fp_pgp_err, state->fp_out) == 0) && !rv)
1124 p->goodsig = true;
1125 else
1126 p->goodsig = false;
1127 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
1128 }
1129 mutt_file_fclose(&fp_pgp_err);
1130
1131 fflush(fp_out);
1132 rewind(fp_out);
1133
1134 if (fgetc(fp_out) == EOF)
1135 {
1136 mutt_error(_("Decryption failed"));
1138 goto cleanup;
1139 }
1140
1141 rewind(fp_out);
1142 const long size = mutt_file_get_size_fp(fp_out);
1143 if (size == 0)
1144 {
1145 goto cleanup;
1146 }
1147
1148 tattach = mutt_read_mime_header(fp_out, 0);
1149 if (tattach)
1150 {
1151 /* Need to set the length of this body part. */
1152 tattach->length = size - tattach->offset;
1153
1154 /* See if we need to recurse on this MIME part. */
1155 mutt_parse_part(fp_out, tattach);
1156 }
1157
1158cleanup:
1159 buf_pool_release(&tempfile);
1160 return tattach;
1161}
1162
1166int pgp_class_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
1167{
1168 struct State state = { 0 };
1169 struct Body *p = b;
1170 bool need_decode = false;
1171 LOFF_T saved_offset = 0;
1172 size_t saved_length = 0;
1173 FILE *fp_decoded = NULL;
1174 int rc = 0;
1175
1177 {
1178 b = b->parts->next;
1179 /* Some clients improperly encode the octetstream part. */
1180 if (b->encoding != ENC_7BIT)
1181 need_decode = true;
1182 }
1184 {
1185 b = b->parts->next->next;
1186 need_decode = true;
1187 }
1188 else
1189 {
1190 return -1;
1191 }
1192
1193 state.fp_in = fp_in;
1194
1195 if (need_decode)
1196 {
1197 saved_offset = b->offset;
1198 saved_length = b->length;
1199
1200 fp_decoded = mutt_file_mkstemp();
1201 if (!fp_decoded)
1202 {
1203 mutt_perror(_("Can't create temporary file"));
1204 return -1;
1205 }
1206
1207 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1208 {
1209 rc = -1;
1210 goto bail;
1211 }
1212 state.fp_out = fp_decoded;
1213
1214 mutt_decode_attachment(b, &state);
1215
1216 fflush(fp_decoded);
1217 b->length = ftello(fp_decoded);
1218 b->offset = 0;
1219 rewind(fp_decoded);
1220 state.fp_in = fp_decoded;
1221 state.fp_out = 0;
1222 }
1223
1224 *fp_out = mutt_file_mkstemp();
1225 if (!*fp_out)
1226 {
1227 mutt_perror(_("Can't create temporary file"));
1228 rc = -1;
1229 goto bail;
1230 }
1231
1232 *b_dec = pgp_decrypt_part(b, &state, *fp_out, p);
1233 if (!*b_dec)
1234 rc = -1;
1235 rewind(*fp_out);
1236
1237bail:
1238 if (need_decode)
1239 {
1240 b->length = saved_length;
1241 b->offset = saved_offset;
1242 mutt_file_fclose(&fp_decoded);
1243 }
1244
1245 return rc;
1246}
1247
1251int pgp_class_encrypted_handler(struct Body *b, struct State *state)
1252{
1253 FILE *fp_in = NULL;
1254 struct Body *tattach = NULL;
1255 int rc = 0;
1256
1257 FILE *fp_out = mutt_file_mkstemp();
1258 if (!fp_out)
1259 {
1260 mutt_perror(_("Can't create temporary file"));
1261 if (state->flags & STATE_DISPLAY)
1262 {
1263 state_attach_puts(state, _("[-- Error: could not create temporary file --]\n"));
1264 }
1265 return -1;
1266 }
1267
1268 if (state->flags & STATE_DISPLAY)
1269 crypt_current_time(state, "PGP");
1270
1271 tattach = pgp_decrypt_part(b, state, fp_out, b);
1272 if (tattach)
1273 {
1274 if (state->flags & STATE_DISPLAY)
1275 {
1276 state_attach_puts(state, _("[-- The following data is PGP/MIME encrypted --]\n"));
1277 mutt_protected_headers_handler(tattach, state);
1278 }
1279
1280 /* Store any protected headers in the parent so they can be
1281 * accessed for index updates after the handler recursion is done.
1282 * This is done before the handler to prevent a nested encrypted
1283 * handler from freeing the headers. */
1285 b->mime_headers = tattach->mime_headers;
1286 tattach->mime_headers = NULL;
1287
1288 fp_in = state->fp_in;
1289 state->fp_in = fp_out;
1290 rc = mutt_body_handler(tattach, state);
1291 state->fp_in = fp_in;
1292
1293 /* Embedded multipart signed protected headers override the
1294 * encrypted headers. We need to do this after the handler so
1295 * they can be printed in the pager. */
1296 if (mutt_is_multipart_signed(tattach) && tattach->parts && tattach->parts->mime_headers)
1297 {
1299 b->mime_headers = tattach->parts->mime_headers;
1300 tattach->parts->mime_headers = NULL;
1301 }
1302
1303 /* if a multipart/signed is the _only_ sub-part of a
1304 * multipart/encrypted, cache signature verification
1305 * status. */
1306 if (mutt_is_multipart_signed(tattach) && !tattach->next)
1307 b->goodsig |= tattach->goodsig;
1308
1309 if (state->flags & STATE_DISPLAY)
1310 state_attach_puts(state, _("[-- End of PGP/MIME encrypted data --]\n"));
1311
1312 mutt_body_free(&tattach);
1313 /* clear 'Invoking...' message, since there's no error */
1314 mutt_message(_("PGP message successfully decrypted"));
1315 }
1316 else
1317 {
1318 mutt_error(_("Could not decrypt PGP message"));
1319 /* void the passphrase, even if it's not necessarily the problem */
1321 rc = -1;
1322 }
1323
1324 mutt_file_fclose(&fp_out);
1325
1326 return rc;
1327}
1328
1329/* ----------------------------------------------------------------------------
1330 * Routines for sending PGP/MIME messages.
1331 */
1332
1336struct Body *pgp_class_sign_message(struct Body *b, const struct AddressList *from)
1337{
1339 struct Body *b_enc = NULL;
1340 struct Body *rv = NULL;
1341 char buf[1024] = { 0 };
1342 FILE *fp_pgp_in = NULL;
1343 FILE *fp_pgp_out = NULL;
1344 FILE *fp_pgp_err = NULL;
1345 FILE *fp_signed = NULL;
1346 bool err = false;
1347 bool empty = true;
1348 pid_t pid;
1349 struct Buffer *sigfile = buf_pool_get();
1350 struct Buffer *signedfile = buf_pool_get();
1351
1352 crypt_convert_to_7bit(b); /* Signed data _must_ be in 7-bit format. */
1353
1354 buf_mktemp(sigfile);
1355 FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
1356 if (!fp_sig)
1357 {
1358 goto cleanup;
1359 }
1360
1361 buf_mktemp(signedfile);
1362 fp_signed = mutt_file_fopen(buf_string(signedfile), "w");
1363 if (!fp_signed)
1364 {
1365 mutt_perror("%s", buf_string(signedfile));
1366 mutt_file_fclose(&fp_sig);
1367 unlink(buf_string(sigfile));
1368 goto cleanup;
1369 }
1370
1371 mutt_write_mime_header(b, fp_signed, NeoMutt->sub);
1372 fputc('\n', fp_signed);
1373 mutt_write_mime_body(b, fp_signed, NeoMutt->sub);
1374 mutt_file_fclose(&fp_signed);
1375
1376 pid = pgp_invoke_sign(&fp_pgp_in, &fp_pgp_out, &fp_pgp_err, -1, -1, -1,
1377 buf_string(signedfile));
1378 if (pid == -1)
1379 {
1380 mutt_perror(_("Can't open PGP subprocess"));
1381 mutt_file_fclose(&fp_sig);
1382 unlink(buf_string(sigfile));
1383 unlink(buf_string(signedfile));
1384 goto cleanup;
1385 }
1386
1387 if (!pgp_use_gpg_agent())
1388 fputs(mod_data->pgp_pass, fp_pgp_in);
1389 fputc('\n', fp_pgp_in);
1390 mutt_file_fclose(&fp_pgp_in);
1391
1392 /* Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1393 * recommended for future releases of PGP. */
1394 while (fgets(buf, sizeof(buf) - 1, fp_pgp_out))
1395 {
1396 if (mutt_str_equal("-----BEGIN PGP MESSAGE-----\n", buf))
1397 fputs("-----BEGIN PGP SIGNATURE-----\n", fp_sig);
1398 else if (mutt_str_equal("-----END PGP MESSAGE-----\n", buf))
1399 fputs("-----END PGP SIGNATURE-----\n", fp_sig);
1400 else
1401 fputs(buf, fp_sig);
1402 empty = false; /* got some output, so we're ok */
1403 }
1404
1405 /* check for errors from PGP */
1406 err = false;
1407 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1408 {
1409 err = true;
1410 fputs(buf, stdout);
1411 }
1412
1413 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1414 if (filter_wait(pid) && c_pgp_check_exit)
1415 empty = true;
1416
1417 mutt_file_fclose(&fp_pgp_err);
1418 mutt_file_fclose(&fp_pgp_out);
1419 unlink(buf_string(signedfile));
1420
1421 if (mutt_file_fclose(&fp_sig) != 0)
1422 {
1423 mutt_perror("fclose");
1424 unlink(buf_string(sigfile));
1425 goto cleanup;
1426 }
1427
1428 if (err)
1430 if (empty)
1431 {
1432 unlink(buf_string(sigfile));
1433 /* most likely error is a bad passphrase, so automatically forget it */
1435 goto cleanup; /* fatal error while signing */
1436 }
1437
1438 b_enc = mutt_body_new();
1439 b_enc->type = TYPE_MULTIPART;
1440 b_enc->subtype = mutt_str_dup("signed");
1441 b_enc->encoding = ENC_7BIT;
1442 b_enc->use_disp = false;
1443 b_enc->disposition = DISP_INLINE;
1444 rv = b_enc;
1445
1447 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-signature");
1448 mutt_param_set(&b_enc->parameter, "micalg", pgp_micalg(buf_string(sigfile)));
1449
1450 b_enc->parts = b;
1451
1452 b_enc->parts->next = mutt_body_new();
1453 b_enc = b_enc->parts->next;
1454 b_enc->type = TYPE_APPLICATION;
1455 b_enc->subtype = mutt_str_dup("pgp-signature");
1456 b_enc->filename = buf_strdup(sigfile);
1457 b_enc->use_disp = false;
1458 b_enc->disposition = DISP_NONE;
1459 b_enc->encoding = ENC_7BIT;
1460 b_enc->unlink = true; /* ok to remove this file after sending. */
1461 mutt_param_set(&b_enc->parameter, "name", "signature.asc");
1462
1463cleanup:
1464 buf_pool_release(&sigfile);
1465 buf_pool_release(&signedfile);
1466 return rv;
1467}
1468
1472char *pgp_class_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
1473{
1474 struct ListHead crypt_hook_list = STAILQ_HEAD_INITIALIZER(crypt_hook_list);
1475 struct ListNode *crypt_hook = NULL;
1476 const char *keyid = NULL;
1477 char *keylist = NULL;
1478 size_t keylist_size = 0;
1479 size_t keylist_used = 0;
1480 struct Address *p = NULL;
1481 struct PgpKeyInfo *k_info = NULL;
1482 const char *fqdn = mutt_fqdn(true, NeoMutt->sub);
1483 char buf[1024] = { 0 };
1484 bool key_selected;
1485 struct AddressList hookal = TAILQ_HEAD_INITIALIZER(hookal);
1486
1487 struct Address *a = NULL;
1488 const bool c_crypt_confirm_hook = cs_subset_bool(NeoMutt->sub, "crypt_confirm_hook");
1489 /* Iterate through each recipient address to find an encryption key */
1490 TAILQ_FOREACH(a, addrlist, entries)
1491 {
1492 key_selected = false;
1493 /* Check for crypt-hook overrides for this recipient */
1494 mutt_crypt_hook(&crypt_hook_list, a);
1495 crypt_hook = STAILQ_FIRST(&crypt_hook_list);
1496 do
1497 {
1498 p = a;
1499 k_info = NULL;
1500
1501 /* If a crypt-hook provides a key ID, confirm with the user unless
1502 * in opportunistic encryption mode */
1503 if (crypt_hook)
1504 {
1505 keyid = crypt_hook->data;
1506 enum QuadOption ans = MUTT_YES;
1507 if (!oppenc_mode && c_crypt_confirm_hook && isatty(STDIN_FILENO))
1508 {
1509 snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyid,
1510 buf_string(p->mailbox));
1511 ans = query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "crypt_confirm_hook");
1512 }
1513 if (ans == MUTT_YES)
1514 {
1515 if (crypt_is_numerical_keyid(keyid))
1516 {
1517 if (mutt_strn_equal(keyid, "0x", 2))
1518 keyid += 2;
1519 goto bypass_selection; /* you don't see this. */
1520 }
1521
1522 /* check for e-mail address */
1523 mutt_addrlist_clear(&hookal);
1524 if (strchr(keyid, '@') && (mutt_addrlist_parse(&hookal, keyid) != 0))
1525 {
1526 mutt_addrlist_qualify(&hookal, fqdn);
1527 p = TAILQ_FIRST(&hookal);
1528 }
1529 else if (!oppenc_mode)
1530 {
1532 }
1533 }
1534 else if (ans == MUTT_NO)
1535 {
1536 if (key_selected || STAILQ_NEXT(crypt_hook, entries))
1537 {
1538 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1539 continue;
1540 }
1541 }
1542 else if (ans == MUTT_ABORT)
1543 {
1544 FREE(&keylist);
1545 mutt_addrlist_clear(&hookal);
1546 mutt_list_free(&crypt_hook_list);
1547 return NULL;
1548 }
1549 }
1550
1551 /* If no key found yet, try looking up by address in the keyring */
1552 if (!k_info)
1553 {
1555 k_info = pgp_getkeybyaddr(p, KEYFLAG_CANENCRYPT, PGP_PUBRING, oppenc_mode);
1556 }
1557
1558 /* Last resort: prompt the user to enter a key ID interactively */
1559 if (!k_info && !oppenc_mode && isatty(STDIN_FILENO))
1560 {
1561 snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), buf_string(p->mailbox));
1563 }
1564
1565 if (!k_info)
1566 {
1567 FREE(&keylist);
1568 mutt_addrlist_clear(&hookal);
1569 mutt_list_free(&crypt_hook_list);
1570 return NULL;
1571 }
1572
1573 keyid = pgp_fpr_or_lkeyid(k_info);
1574
1575 bypass_selection:
1576 /* Append the selected key ID to the space-separated keylist string */
1577 keylist_size += mutt_str_len(keyid) + 4;
1578 MUTT_MEM_REALLOC(&keylist, keylist_size, char);
1579 sprintf(keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", keyid);
1580 keylist_used = mutt_str_len(keylist);
1581
1582 key_selected = true;
1583
1584 pgp_key_free(&k_info);
1585 mutt_addrlist_clear(&hookal);
1586
1587 if (crypt_hook)
1588 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1589
1590 } while (crypt_hook);
1591
1592 mutt_list_free(&crypt_hook_list);
1593 }
1594 return keylist;
1595}
1596
1603struct Body *pgp_class_encrypt_message(struct Body *b, char *keylist, bool sign,
1604 const struct AddressList *from)
1605{
1607 char buf[1024] = { 0 };
1608 FILE *fp_pgp_in = NULL;
1609 FILE *fp_tmp = NULL;
1610 struct Body *b_enc = NULL;
1611 bool err = false;
1612 bool empty = false;
1613 pid_t pid;
1614 struct Buffer *tempfile = buf_pool_get();
1615 struct Buffer *pgpinfile = buf_pool_get();
1616
1617 buf_mktemp(tempfile);
1618 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1619 if (!fp_out)
1620 {
1621 mutt_perror("%s", buf_string(tempfile));
1622 goto cleanup;
1623 }
1624
1625 FILE *fp_pgp_err = mutt_file_mkstemp();
1626 if (!fp_pgp_err)
1627 {
1628 mutt_perror(_("Can't create temporary file"));
1629 unlink(buf_string(tempfile));
1630 mutt_file_fclose(&fp_out);
1631 goto cleanup;
1632 }
1633
1634 buf_mktemp(pgpinfile);
1635 fp_tmp = mutt_file_fopen(buf_string(pgpinfile), "w");
1636 if (!fp_tmp)
1637 {
1638 mutt_perror("%s", buf_string(pgpinfile));
1639 unlink(buf_string(tempfile));
1640 mutt_file_fclose(&fp_out);
1641 mutt_file_fclose(&fp_pgp_err);
1642 goto cleanup;
1643 }
1644
1645 if (sign)
1647
1648 mutt_write_mime_header(b, fp_tmp, NeoMutt->sub);
1649 fputc('\n', fp_tmp);
1650 mutt_write_mime_body(b, fp_tmp, NeoMutt->sub);
1651 mutt_file_fclose(&fp_tmp);
1652
1653 pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out),
1654 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, sign);
1655 if (pid == -1)
1656 {
1657 mutt_file_fclose(&fp_out);
1658 mutt_file_fclose(&fp_pgp_err);
1659 unlink(buf_string(pgpinfile));
1660 goto cleanup;
1661 }
1662
1663 if (sign)
1664 {
1665 if (!pgp_use_gpg_agent())
1666 fputs(mod_data->pgp_pass, fp_pgp_in);
1667 fputc('\n', fp_pgp_in);
1668 }
1669 mutt_file_fclose(&fp_pgp_in);
1670
1671 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1672 if (filter_wait(pid) && c_pgp_check_exit)
1673 empty = true;
1674
1675 unlink(buf_string(pgpinfile));
1676
1677 fflush(fp_out);
1678 rewind(fp_out);
1679 if (!empty)
1680 empty = (fgetc(fp_out) == EOF);
1681 mutt_file_fclose(&fp_out);
1682
1683 fflush(fp_pgp_err);
1684 rewind(fp_pgp_err);
1685 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1686 {
1687 err = true;
1688 fputs(buf, stdout);
1689 }
1690 mutt_file_fclose(&fp_pgp_err);
1691
1692 /* pause if there is any error output from PGP */
1693 if (err)
1695
1696 if (empty)
1697 {
1698 /* fatal error while trying to encrypt message */
1699 if (sign)
1700 pgp_class_void_passphrase(); /* just in case */
1701 unlink(buf_string(tempfile));
1702 goto cleanup;
1703 }
1704
1705 b_enc = mutt_body_new();
1706 b_enc->type = TYPE_MULTIPART;
1707 b_enc->subtype = mutt_str_dup("encrypted");
1708 b_enc->encoding = ENC_7BIT;
1709 b_enc->use_disp = false;
1710 b_enc->disposition = DISP_INLINE;
1711
1713 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-encrypted");
1714
1715 b_enc->parts = mutt_body_new();
1716 b_enc->parts->type = TYPE_APPLICATION;
1717 b_enc->parts->subtype = mutt_str_dup("pgp-encrypted");
1718 b_enc->parts->encoding = ENC_7BIT;
1719
1720 b_enc->parts->next = mutt_body_new();
1721 b_enc->parts->next->type = TYPE_APPLICATION;
1722 b_enc->parts->next->subtype = mutt_str_dup("octet-stream");
1723 b_enc->parts->next->encoding = ENC_7BIT;
1724 b_enc->parts->next->filename = buf_strdup(tempfile);
1725 b_enc->parts->next->use_disp = true;
1726 b_enc->parts->next->disposition = DISP_ATTACH;
1727 b_enc->parts->next->unlink = true; /* delete after sending the message */
1728 b_enc->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1729
1730cleanup:
1731 buf_pool_release(&tempfile);
1732 buf_pool_release(&pgpinfile);
1733 return b_enc;
1734}
1735
1739struct Body *pgp_class_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
1740{
1742 struct Body *b_enc = NULL;
1743 char body_charset[256] = { 0 };
1744 const char *from_charset = NULL;
1745 const char *send_charset = NULL;
1746 bool empty = false;
1747 bool err;
1748 char buf[256] = { 0 };
1749 pid_t pid;
1750 struct Buffer *pgpinfile = buf_pool_get();
1751 struct Buffer *pgpoutfile = buf_pool_get();
1752
1753 if (b->type != TYPE_TEXT)
1754 goto cleanup;
1755 if (!mutt_istr_equal(b->subtype, "plain"))
1756 goto cleanup;
1757
1758 FILE *fp_body = mutt_file_fopen(b->filename, "r");
1759 if (!fp_body)
1760 {
1761 mutt_perror("%s", b->filename);
1762 goto cleanup;
1763 }
1764
1765 buf_mktemp(pgpinfile);
1766 FILE *fp_pgp_in = mutt_file_fopen(buf_string(pgpinfile), "w");
1767 if (!fp_pgp_in)
1768 {
1769 mutt_perror("%s", buf_string(pgpinfile));
1770 mutt_file_fclose(&fp_body);
1771 goto cleanup;
1772 }
1773
1774 /* The following code is really correct: If noconv is set,
1775 * b's charset parameter contains the on-disk character set, and
1776 * we have to convert from that to utf-8. If noconv is not set,
1777 * we have to convert from $charset to utf-8. */
1778
1779 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
1780 if (b->noconv)
1781 from_charset = body_charset;
1782 else
1783 from_charset = cc_charset();
1784
1785 if (mutt_ch_is_us_ascii(body_charset))
1786 {
1787 send_charset = "us-ascii";
1788 mutt_file_copy_stream(fp_body, fp_pgp_in);
1789 }
1790 else
1791 {
1792 int c;
1793 struct FgetConv *fc = NULL;
1794
1795 if (flags & SEC_ENCRYPT)
1796 send_charset = "us-ascii";
1797 else
1798 send_charset = "utf-8";
1799
1800 /* fromcode is assumed to be correct: we set flags to 0 */
1801 fc = mutt_ch_fgetconv_open(fp_body, from_charset, "utf-8", MUTT_ICONV_NONE);
1802 while ((c = mutt_ch_fgetconv(fc)) != EOF)
1803 fputc(c, fp_pgp_in);
1804
1806 }
1807 mutt_file_fclose(&fp_body);
1808 mutt_file_fclose(&fp_pgp_in);
1809
1810 buf_mktemp(pgpoutfile);
1811 FILE *fp_pgp_out = mutt_file_fopen(buf_string(pgpoutfile), "w+");
1812 FILE *fp_pgp_err = mutt_file_mkstemp();
1813 if (!fp_pgp_out || !fp_pgp_err)
1814 {
1815 mutt_perror("%s", fp_pgp_out ? "Can't create temporary file" : buf_string(pgpoutfile));
1816 unlink(buf_string(pgpinfile));
1817 if (fp_pgp_out)
1818 {
1819 mutt_file_fclose(&fp_pgp_out);
1820 unlink(buf_string(pgpoutfile));
1821 }
1822 mutt_file_fclose(&fp_pgp_err);
1823 goto cleanup;
1824 }
1825
1826 pid = pgp_invoke_traditional(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
1827 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, flags);
1828 if (pid == -1)
1829 {
1830 mutt_perror(_("Can't invoke PGP"));
1831 mutt_file_fclose(&fp_pgp_out);
1832 mutt_file_fclose(&fp_pgp_err);
1833 mutt_file_unlink(buf_string(pgpinfile));
1834 unlink(buf_string(pgpoutfile));
1835 goto cleanup;
1836 }
1837
1838 if (pgp_use_gpg_agent())
1839 *mod_data->pgp_pass = '\0';
1840 if (flags & SEC_SIGN)
1841 fprintf(fp_pgp_in, "%s\n", mod_data->pgp_pass);
1842 mutt_file_fclose(&fp_pgp_in);
1843
1844 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1845 if (filter_wait(pid) && c_pgp_check_exit)
1846 empty = true;
1847
1848 mutt_file_unlink(buf_string(pgpinfile));
1849
1850 fflush(fp_pgp_out);
1851 fflush(fp_pgp_err);
1852
1853 rewind(fp_pgp_out);
1854 rewind(fp_pgp_err);
1855
1856 if (!empty)
1857 empty = (fgetc(fp_pgp_out) == EOF);
1858 mutt_file_fclose(&fp_pgp_out);
1859
1860 err = false;
1861
1862 while (fgets(buf, sizeof(buf), fp_pgp_err))
1863 {
1864 err = true;
1865 fputs(buf, stdout);
1866 }
1867
1868 mutt_file_fclose(&fp_pgp_err);
1869
1870 if (err)
1872
1873 if (empty)
1874 {
1875 if (flags & SEC_SIGN)
1876 pgp_class_void_passphrase(); /* just in case */
1877 unlink(buf_string(pgpoutfile));
1878 goto cleanup;
1879 }
1880
1881 b_enc = mutt_body_new();
1882
1883 b_enc->encoding = ENC_7BIT;
1884
1885 b_enc->type = TYPE_TEXT;
1886 b_enc->subtype = mutt_str_dup("plain");
1887
1888 mutt_param_set(&b_enc->parameter, "x-action",
1889 (flags & SEC_ENCRYPT) ? "pgp-encrypted" : "pgp-signed");
1890 mutt_param_set(&b_enc->parameter, "charset", send_charset);
1891
1892 b_enc->filename = buf_strdup(pgpoutfile);
1893
1894 b_enc->disposition = DISP_NONE;
1895 b_enc->unlink = true;
1896
1897 b_enc->noconv = true;
1898 b_enc->use_disp = false;
1899
1900 if (!(flags & SEC_ENCRYPT))
1901 b_enc->encoding = b->encoding;
1902
1903cleanup:
1904 buf_pool_release(&pgpinfile);
1905 buf_pool_release(&pgpoutfile);
1906 return b_enc;
1907}
1908
1913{
1914 struct PgpKeyInfo *p = NULL;
1915 const char *prompt = NULL;
1916 const char *letters = NULL;
1917 const char *choices = NULL;
1918 char promptbuf[1024] = { 0 };
1919 int choice;
1920
1921 if (!(WithCrypto & APPLICATION_PGP))
1922 return e->security;
1923
1924 /* If autoinline and no crypto options set, then set inline. */
1925 const bool c_pgp_auto_inline = cs_subset_bool(NeoMutt->sub, "pgp_auto_inline");
1926 if (c_pgp_auto_inline &&
1927 !((e->security & APPLICATION_PGP) && (e->security & (SEC_SIGN | SEC_ENCRYPT))))
1928 {
1929 e->security |= SEC_INLINE;
1930 }
1931
1933
1934 char *mime_inline = NULL;
1935 if (e->security & SEC_INLINE)
1936 {
1937 /* L10N: The next string MUST have the same highlighted letter
1938 One of them will appear in each of the three strings marked "(inline"), below. */
1939 mime_inline = _("PGP/M(i)ME");
1940 }
1941 else
1942 {
1943 /* L10N: The previous string MUST have the same highlighted letter
1944 One of them will appear in each of the three strings marked "(inline"), below. */
1945 mime_inline = _("(i)nline");
1946 }
1947 /* Opportunistic encrypt is controlling encryption. Allow to toggle
1948 * between inline and mime, but not turn encryption on or off.
1949 * NOTE: "Signing" and "Clearing" only adjust the sign bit, so we have different
1950 * letter choices for those. */
1951 const bool c_crypt_opportunistic_encrypt = cs_subset_bool(NeoMutt->sub, "crypt_opportunistic_encrypt");
1952 if (c_crypt_opportunistic_encrypt && (e->security & SEC_OPPENCRYPT))
1953 {
1954 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
1955 {
1956 snprintf(promptbuf, sizeof(promptbuf),
1957 /* L10N: PGP options (inline) (opportunistic encryption is on) */
1958 _("PGP (s)ign, sign (a)s, %s format, (c)lear, or (o)ppenc mode off?"),
1959 mime_inline);
1960 prompt = promptbuf;
1961 /* L10N: PGP options (inline) (opportunistic encryption is on)
1962 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
1963 letters = _("saico");
1964 choices = "SaiCo";
1965 }
1966 else
1967 {
1968 /* L10N: PGP options (opportunistic encryption is on) */
1969 prompt = _("PGP (s)ign, sign (a)s, (c)lear, or (o)ppenc mode off?");
1970 /* L10N: PGP options (opportunistic encryption is on) */
1971 letters = _("saco");
1972 choices = "SaCo";
1973 }
1974 }
1975 else if (c_crypt_opportunistic_encrypt)
1976 {
1977 /* Opportunistic encryption option is set, but is toggled off
1978 * for this message. */
1979 /* When the message is not selected for signing or encryption, the toggle
1980 * between PGP/MIME and Traditional doesn't make sense. */
1981 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
1982 {
1983 snprintf(promptbuf, sizeof(promptbuf),
1984 /* L10N: PGP options (inline) (opportunistic encryption is off) */
1985 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s format, (c)lear, or (o)ppenc mode?"),
1986 mime_inline);
1987 prompt = promptbuf;
1988 /* L10N: PGP options (inline) (opportunistic encryption is off)
1989 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
1990 letters = _("esabico");
1991 choices = "esabicO";
1992 }
1993 else
1994 {
1995 /* L10N: PGP options (opportunistic encryption is off) */
1996 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, (c)lear, or (o)ppenc mode?");
1997 /* L10N: PGP options (opportunistic encryption is off) */
1998 letters = _("esabco");
1999 choices = "esabcO";
2000 }
2001 }
2002 else
2003 {
2004 /* Opportunistic encryption is unset */
2005 if (e->security & (SEC_ENCRYPT | SEC_SIGN))
2006 {
2007 snprintf(promptbuf, sizeof(promptbuf),
2008 /* L10N: PGP options (inline) */
2009 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s format, or (c)lear?"),
2010 mime_inline);
2011 prompt = promptbuf;
2012 /* L10N: PGP options (inline)
2013 The 'i' is from the "PGP/M(i)ME" or "(i)nline", above. */
2014 letters = _("esabic");
2015 choices = "esabic";
2016 }
2017 else
2018 {
2019 /* L10N: PGP options */
2020 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, or (c)lear?");
2021 /* L10N: PGP options */
2022 letters = _("esabc");
2023 choices = "esabc";
2024 }
2025 }
2026
2027 choice = mw_multi_choice(prompt, letters);
2028 if (choice > 0)
2029 {
2030 switch (choices[choice - 1])
2031 {
2032 case 'a': /* sign (a)s */
2033 OptPgpCheckTrust = false;
2034
2035 p = pgp_ask_for_key(_("Sign as: "), NULL, KEYFLAG_NONE, PGP_SECRING);
2036 if (p)
2037 {
2038 char input_signas[128] = { 0 };
2039 snprintf(input_signas, sizeof(input_signas), "0x%s", pgp_fpr_or_lkeyid(p));
2040 cs_subset_str_string_set(NeoMutt->sub, "pgp_sign_as", input_signas, NULL);
2041 pgp_key_free(&p);
2042
2043 e->security |= SEC_SIGN;
2044
2045 crypt_pgp_void_passphrase(); /* probably need a different passphrase */
2046 }
2047 break;
2048
2049 case 'b': /* (b)oth */
2050 e->security |= (SEC_ENCRYPT | SEC_SIGN);
2051 break;
2052
2053 case 'C':
2054 e->security &= ~SEC_SIGN;
2055 break;
2056
2057 case 'c': /* (c)lear */
2058 e->security &= ~(SEC_ENCRYPT | SEC_SIGN);
2059 break;
2060
2061 case 'e': /* (e)ncrypt */
2062 e->security |= SEC_ENCRYPT;
2063 e->security &= ~SEC_SIGN;
2064 break;
2065
2066 case 'i': /* toggle (i)nline */
2067 e->security ^= SEC_INLINE;
2068 break;
2069
2070 case 'O': /* oppenc mode on */
2073 break;
2074
2075 case 'o': /* oppenc mode off */
2077 break;
2078
2079 case 'S': /* (s)ign in oppenc mode */
2080 e->security |= SEC_SIGN;
2081 break;
2082
2083 case 's': /* (s)ign */
2084 e->security &= ~SEC_ENCRYPT;
2085 e->security |= SEC_SIGN;
2086 break;
2087 }
2088 }
2089
2090 return e->security;
2091}
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition address.c:687
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1475
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition address.c:481
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:168
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:577
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:1051
bool crypt_is_numerical_keyid(const char *s)
Is this a numerical keyid.
Definition crypt.c:1489
SecurityFlags mutt_is_multipart_signed(struct Body *b)
Is a message signed?
Definition crypt.c:409
int mutt_is_valid_multipart_pgp_encrypted(struct Body *b)
Is this a valid multi-part encrypted message?
Definition crypt.c:468
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition crypt.c:505
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:815
SecurityFlags mutt_is_application_pgp(const struct Body *b)
Does the message use PGP?
Definition crypt.c:549
Signing/encryption multiplexor.
void crypt_pgp_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition cryptglue.c:211
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:174
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition curs_lib.c:101
void mutt_endwin(void)
Shutdown curses.
Definition curs_lib.c:152
Edit a string.
@ MUTT_COMP_UNBUFFERED
Ignore macro buffer.
Definition wdata.h:49
@ MUTT_COMP_PASS
Password mode (no echo)
Definition wdata.h:48
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:134
Structs that make up an email.
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition parse.c:1949
struct Body * mutt_read_mime_header(FILE *fp, bool digest)
Parse a MIME header.
Definition parse.c:1484
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:1433
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:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
@ MUTT_RL_NONE
No flags are set.
Definition file.h:43
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:470
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:1166
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:1251
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:1472
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:870
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:1603
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:1004
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:1739
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:1912
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:1336
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:899
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:502
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:1123
#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:1668
void mutt_decode_attachment(const struct Body *b, struct State *state)
Decode an email's attachment.
Definition handler.c:1943
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition lib.h:61
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:82
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:882
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
Definition charset.c:968
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:921
char * mutt_ch_fgetconvs(char *buf, size_t buflen, struct FgetConv *fc)
Convert a file's charset into a string buffer.
Definition charset.c:1030
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
Definition charset.c:950
#define MUTT_ICONV_NONE
No flags are set.
Definition charset.h:66
#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
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:896
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:459
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:231
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:618
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:64
#define state_set_prefix(state)
Definition state.h:62
#define state_putc(STATE, STR)
Definition state.h:65
@ STATE_NONE
No flags are set.
Definition state.h:36
@ STATE_VERIFY
Perform signature verification.
Definition state.h:38
@ STATE_DISPLAY
Output is displayed to the user.
Definition state.h:37
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
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:666
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:557
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:587
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_NONE
Overwrite existing file (the default)
Definition mutt_attach.h:59
API for encryption/signing of emails.
uint16_t SecurityFlags
Definition lib.h:104
@ SEC_OPPENCRYPT
Opportunistic encrypt mode.
Definition lib.h:100
@ SEC_SIGN
Email is signed.
Definition lib.h:93
@ SEC_INLINE
Email has an inline signature.
Definition lib.h:99
@ SEC_ENCRYPT
Email is encrypted.
Definition lib.h:92
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:106
#define PGP_ENCRYPT
Email is PGP encrypted.
Definition lib.h:112
@ KEYFLAG_NONE
No flags are set.
Definition lib.h:146
@ KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition lib.h:148
@ KEYFLAG_SUBKEY
Key is a subkey.
Definition lib.h:154
#define WithCrypto
Definition lib.h:132
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:666
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:1027
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:422
static void pgp_extract_keys_from_attachment(FILE *fp, struct Body *b)
Extract pgp keys from messages/attachments.
Definition pgp.c:972
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:803
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:202
struct PgpKeyInfo * pgp_getkeybyaddr(struct Address *a, KeyFlags abilities, enum PgpRing keyring, bool oppenc_mode)
Find a PGP key by address.
Definition pgpkey.c:382
struct PgpKeyInfo * pgp_getkeybystr(const char *cp, KeyFlags abilities, enum PgpRing keyring)
Find a PGP key by string.
Definition pgpkey.c:523
PGP key management routines.
@ PGP_SECRING
Secret keys.
Definition pgpkey.h:41
@ PGP_PUBRING
Public keys.
Definition pgpkey.h:40
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition pgplib.c:204
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:304
int mutt_write_mime_header(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition header.c:763
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:717
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:39
char pgp_pass[1024]
Cached PGP Passphrase.
Definition module_data.h:51
time_t pgp_exptime
Unix time when pgp_pass expires.
Definition module_data.h:52
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:54
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition state.h:58
FILE * fp_out
File to write to.
Definition state.h:56
FILE * fp_in
File to read from.
Definition state.h:55
const char * prefix
String to add to the beginning of each output line.
Definition state.h:57
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