2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2004 g10 Code GmbH
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
13 * This file contains all of the PGP routines necessary to sign, encrypt,
14 * verify and decrypt PGP messages in either the new PGP/MIME format, or
15 * in the older Application/Pgp format. It also contains some code to
16 * cache the user's passphrase for repeat use when decrypting or signing
20 #include <lib-lib/lib-lib.h>
22 #ifdef HAVE_SYS_RESOURCE_H
23 # include <sys/resource.h>
26 #include <lib-mime/mime.h>
27 #include <lib-sys/unix.h>
29 #include <lib-ui/curses.h>
30 #include <lib-ui/enter.h>
31 #include <lib-ui/menu.h>
32 #include <lib-mx/mx.h>
42 char PgpPass[LONG_STRING];
43 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
45 void pgp_void_passphrase (void)
47 p_clear(PgpPass, countof(PgpPass));
51 int pgp_valid_passphrase (void)
53 time_t now = time (NULL);
55 if (pgp_use_gpg_agent ()) {
57 return 1; /* handled by gpg-agent */
61 /* Use cached copy. */
64 pgp_void_passphrase ();
66 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
67 sizeof (PgpPass), M_PASS) == 0) {
68 PgpExptime = time (NULL) + PgpTimeout;
77 void pgp_forget_passphrase (void)
79 pgp_void_passphrase ();
80 mutt_message _("PGP passphrase forgotten.");
83 int pgp_use_gpg_agent (void) {
86 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
89 if ((tty = ttyname(0)))
90 setenv ("GPG_TTY", tty, 0);
95 char *pgp_keyid (pgp_key_t k)
97 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
100 return _pgp_keyid (k);
103 char *_pgp_keyid (pgp_key_t k)
105 if (option (OPTPGPLONGIDS))
108 return (k->keyid + 8);
111 /* ----------------------------------------------------------------------------
112 * Routines for handing PGP input.
117 /* Copy PGP output messages and look for signs of a good signature */
119 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
123 if (PgpGoodSign.pattern) {
128 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
129 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
133 if (m_strncmp (line, "[GNUPG:] ", 9) == 0)
141 mutt_copy_stream (fpin, fpout);
149 * Copy a clearsigned message, and strip the signature and PGP's
152 * XXX - charset handling: We assume that it is safe to do
153 * character set decoding first, dash decoding second here, while
154 * we do it the other way around in the main handler.
156 * (Note that we aren't worse than Outlook &c in this, and also
157 * note that we can successfully handle anything produced by any
158 * existing versions of mutt.)
161 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
163 char buf[HUGE_STRING];
164 short complete, armor_header;
170 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
172 for (complete = 1, armor_header = 1;
173 fgetconvs (buf, sizeof (buf), fc) != NULL;
174 complete = strchr (buf, '\n') != NULL) {
181 if (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
185 char *p = vskipspaces(buf);
193 state_puts (s->prefix, s);
195 if (buf[0] == '-' && buf[1] == ' ')
196 state_puts (buf + 2, s);
201 fgetconv_close (&fc);
205 /* Support for the Application/PGP Content Type. */
207 int pgp_application_pgp_handler (BODY * m, STATE * s)
209 int could_not_decrypt = 0;
210 int needpass = -1, pgp_keyblock = 0;
212 int clearsign = 0, rv, rc;
215 off_t last_pos, offset;
216 char buf[HUGE_STRING];
217 char outfile[_POSIX_PATH_MAX];
218 char tmpfname[_POSIX_PATH_MAX];
219 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
223 short maybe_goodsig = 1;
224 short have_any_sigs = 0;
226 char body_charset[STRING];
228 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
230 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
232 fseeko (s->fpin, m->offset, 0);
233 last_pos = m->offset;
235 for (bytes = m->length; bytes > 0;) {
236 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
239 offset = ftello (s->fpin);
240 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
243 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
245 start_pos = last_pos;
247 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
249 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
253 else if (!option (OPTDONTHANDLEPGPKEYS) &&
254 m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
259 /* XXX - we may wish to recode here */
261 state_puts (s->prefix, s);
266 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
268 /* Copy PGP material to temporary file */
269 tmpfp = m_tempfile(tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
271 mutt_perror (tmpfname);
276 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
277 offset = ftello (s->fpin);
278 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
284 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
286 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
287 || m_strcmp("-----END PGP PUBLIC KEY BLOCK-----\n",
292 /* leave tmpfp open in case we still need it - but flush it! */
296 /* Invoke PGP if needed */
297 if (!clearsign || (s->flags & M_VERIFY)) {
298 pgpout = m_tempfile(outfile, sizeof(outfile), NONULL(Tempdir), NULL);
299 if (pgpout == NULL) {
300 mutt_perror (outfile);
304 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
305 fileno (pgpout), -1, tmpfname,
312 ("[-- Error: unable to create PGP subprocess! --]\n"),
315 else { /* PGP started successfully */
318 if (!pgp_valid_passphrase ())
319 pgp_void_passphrase ();
320 if (pgp_use_gpg_agent ())
322 fprintf (pgpin, "%s\n", PgpPass);
327 if (s->flags & M_DISPLAY) {
328 crypt_current_time (s, "PGP");
329 rc = pgp_copy_checksig (pgperr, s->fpout);
333 rv = mutt_wait_filter (thepid);
335 if (s->flags & M_DISPLAY) {
341 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
342 * Sig _is_ correct if
343 * gpg_good_sign="" && pgp_decode_command returned 0
348 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
352 /* treat empty result as sign of failure */
353 /* TODO: maybe on failure mutt should include the original undecoded text. */
359 if (!clearsign && (!pgpout || c == EOF)) {
360 could_not_decrypt = 1;
361 pgp_void_passphrase ();
364 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
365 mutt_error _("Could not decrypt PGP message");
373 * Now, copy cleartext to the screen. NOTE - we expect that PGP
374 * outputs utf-8 cleartext. This may not always be true, but it
375 * seems to be a reasonable guess.
378 if (s->flags & M_DISPLAY) {
380 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
381 else if (pgp_keyblock)
382 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
384 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
390 pgp_copy_clearsigned (tmpfp, s, body_charset);
396 state_set_prefix (s);
397 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
398 while ((c = fgetconv (fc)) != EOF)
399 state_prefix_putc (c, s);
400 fgetconv_close (&fc);
403 if (s->flags & M_DISPLAY) {
404 state_putc ('\n', s);
406 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
407 if (could_not_decrypt)
408 mutt_error _("Could not decrypt PGP message.");
410 mutt_message _("PGP message successfully decrypted.");
412 else if (pgp_keyblock)
413 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
415 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
420 /* XXX - we may wish to recode here */
422 state_puts (s->prefix, s);
430 m->goodsig = (maybe_goodsig && have_any_sigs);
434 mutt_unlink (tmpfname);
438 mutt_unlink (outfile);
441 if (needpass == -1) {
443 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
451 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
454 char tempfile[_POSIX_PATH_MAX];
455 char buf[HUGE_STRING];
463 if (b->type != TYPETEXT)
466 if (tagged_only && !b->tagged)
469 tempfd = m_tempfd(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
470 if (mutt_decode_save_attachment (fp, b, tempfd, 0) != 0) {
475 if (!(tfp = fopen(tempfile, "r"))) {
480 while (fgets (buf, sizeof (buf), tfp)) {
481 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
482 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
484 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
486 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
493 if (!enc && !sgn && !key)
496 /* fix the content type */
498 parameter_setval(&b->parameter, "format", "fixed");
500 parameter_setval(&b->parameter, "x-action", "pgp-encrypted");
502 parameter_setval(&b->parameter, "x-action", "pgp-signed");
504 parameter_setval(&b->parameter, "x-action", "pgp-keys");
509 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
514 for (; b; b = b->next) {
515 if (is_multipart (b))
516 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
517 else if (b->type == TYPETEXT) {
518 if ((r = mutt_is_application_pgp (b)))
521 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
532 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
534 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
535 FILE *fp, *pgpout, *pgperr;
540 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
542 if (!(fp = safe_fopen (sigfile, "w"))) {
543 mutt_perror (sigfile);
547 fseeko (s->fpin, sigbdy->offset, 0);
548 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
551 pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(Tempdir), NULL);
552 if (pgperr == NULL) {
553 mutt_perror (pgperrfile);
558 crypt_current_time (s, "PGP");
560 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
561 -1, -1, fileno (pgperr),
562 tempfile, sigfile)) != -1) {
563 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
571 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
574 if ((rv = mutt_wait_filter (thepid)))
580 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
582 mutt_unlink (sigfile);
583 mutt_unlink (pgperrfile);
589 /* Extract pgp public keys from messages or attachments */
591 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
595 char tempfname[_POSIX_PATH_MAX];
597 tempfp = m_tempfile(tempfname, sizeof(tempfname), NONULL(Tempdir), NULL);
598 if (tempfp == NULL) {
599 mutt_perror (_("Can't create temporary file"));
608 mutt_body_handler (top, &s);
612 pgp_invoke_import (tempfname);
613 mutt_any_key_to_continue (NULL);
615 mutt_unlink (tempfname);
618 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
621 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
627 set_option (OPTDONTHANDLEPGPKEYS);
629 for (; top; top = top->next) {
630 if (!tag || top->tagged)
631 pgp_extract_keys_from_attachment (fp, top);
637 unset_option (OPTDONTHANDLEPGPKEYS);
640 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
642 char buf[LONG_STRING];
643 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
647 char pgperrfile[_POSIX_PATH_MAX];
648 char pgptmpfile[_POSIX_PATH_MAX];
652 pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(Tempdir), NULL);
654 mutt_perror (pgperrfile);
659 pgptmp = m_tempfile(pgptmpfile, sizeof(pgptmpfile), NONULL(Tempdir), NULL);
661 mutt_perror (pgptmpfile);
666 /* Position the stream at the beginning of the body, and send the data to
667 * the temporary file.
670 fseeko (s->fpin, a->offset, 0);
671 mutt_copy_bytes (s->fpin, pgptmp, a->length);
674 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
675 fileno (pgperr), pgptmpfile)) == -1) {
678 if (s->flags & M_DISPLAY)
680 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
685 /* send the PGP passphrase to the subprocess. Never do this if the
686 agent is active, because this might lead to a passphrase send as
688 if (!pgp_use_gpg_agent ())
689 fputs (PgpPass, pgpin);
693 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
694 * read_mime_header has a hard time parsing the message.
696 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
698 if (len > 1 && buf[len - 2] == '\r')
699 m_strcpy(buf + len - 2, len - 2, "\n");
704 rv = mutt_wait_filter (thepid);
705 mutt_unlink (pgptmpfile);
707 if (s->flags & M_DISPLAY) {
710 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
714 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
721 if (fgetc (fpout) == EOF) {
722 mutt_error (_("Decryption failed."));
723 pgp_void_passphrase ();
729 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
731 * Need to set the length of this body part.
733 fstat (fileno (fpout), &info);
734 tattach->length = info.st_size - tattach->offset;
736 /* See if we need to recurse on this MIME part. */
738 mutt_parse_part (fpout, tattach);
744 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
746 char tempfile[_POSIX_PATH_MAX];
750 if (!mutt_is_multipart_encrypted (b))
753 if (!b->parts || !b->parts->next)
760 *fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
761 if (*fpout == NULL) {
762 mutt_perror (_("Can't create temporary file"));
767 *cur = pgp_decrypt_part (b, &s, *fpout, p);
777 int pgp_encrypted_handler (BODY * a, STATE * s)
779 char tempfile[_POSIX_PATH_MAX];
786 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
787 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
788 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
789 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
790 if (s->flags & M_DISPLAY)
791 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
797 * Move forward to the application/pgp-encrypted body.
801 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
803 if (s->flags & M_DISPLAY)
805 ("[-- Error: could not create temporary file! --]\n"),
810 if (s->flags & M_DISPLAY)
811 crypt_current_time (s, "PGP");
813 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
814 if (s->flags & M_DISPLAY)
816 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
821 rc = mutt_body_handler (tattach, s);
825 * if a multipart/signed is the _only_ sub-part of a
826 * multipart/encrypted, cache signature verification
831 if (mutt_is_multipart_signed (tattach) && !tattach->next)
832 p->goodsig |= tattach->goodsig;
834 if (s->flags & M_DISPLAY) {
835 state_puts ("\n", s);
836 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
839 body_list_wipe(&tattach);
840 /* clear 'Invoking...' message, since there's no error */
841 mutt_message _("PGP message successfully decrypted.");
843 mutt_error _("Could not decrypt PGP message");
844 pgp_void_passphrase ();
849 mutt_unlink (tempfile);
854 /* ----------------------------------------------------------------------------
855 * Routines for sending PGP/MIME messages.
859 BODY *pgp_sign_message (BODY * a)
862 char buffer[LONG_STRING];
863 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
864 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
869 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
871 fp = m_tempfile(sigfile, sizeof(sigfile), NONULL(Tempdir), NULL);
876 sfp = m_tempfile(signedfile, sizeof(signedfile), NONULL(Tempdir), NULL);
878 mutt_perror (signedfile);
884 mutt_write_mime_header (a, sfp);
886 mutt_write_mime_body (a, sfp);
889 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
890 -1, -1, -1, signedfile)) == -1) {
891 mutt_perror (_("Can't open PGP subprocess!"));
899 if (!pgp_use_gpg_agent ())
900 fputs (PgpPass, pgpin);
905 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
906 * recommended for future releases of PGP.
908 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
909 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
910 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
911 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
912 fputs ("-----END PGP SIGNATURE-----\n", fp);
915 empty = 0; /* got some output, so we're ok */
918 /* check for errors from PGP */
920 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
922 fputs (buffer, stdout);
925 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
932 if (m_fclose(&fp) != 0) {
933 mutt_perror ("fclose");
939 pgp_void_passphrase ();
940 mutt_any_key_to_continue (NULL);
945 return (NULL); /* fatal error while signing */
949 t->type = TYPEMULTIPART;
950 t->subtype = m_strdup("signed");
951 t->encoding = ENC7BIT;
953 t->disposition = DISPINLINE;
955 parameter_set_boundary(&t->parameter);
956 parameter_setval(&t->parameter, "protocol", "application/pgp-signature");
957 parameter_setval(&t->parameter, "micalg", pgp_micalg (sigfile));
962 t->parts->next = body_new();
964 t->type = TYPEAPPLICATION;
965 t->subtype = m_strdup("pgp-signature");
966 t->filename = m_strdup(sigfile);
968 t->disposition = DISPINLINE;
969 t->encoding = ENC7BIT;
970 t->unlink = 1; /* ok to remove this file after sending. */
975 static short is_numerical_keyid (const char *s)
977 /* or should we require the "0x"? */
978 if (m_strncmp (s, "0x", 2) == 0)
983 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
989 /* This routine attempts to find the keyids of the recipients of a message.
990 * It returns NULL if any of the keys can not be found.
992 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
994 char *keylist = NULL, *t;
996 size_t keylist_size = 0;
997 size_t keylist_used = 0;
998 address_t *tmp = NULL, *addr = NULL;
999 address_t **last = &tmp;
1002 pgp_key_t k_info = NULL, key = NULL;
1004 const char *fqdn = mutt_fqdn (1);
1006 for (i = 0; i < 3; i++) {
1021 *last = address_list_dup (p);
1023 last = &((*last)->next);
1026 rfc822_qualify (tmp, fqdn);
1028 address_list_uniq(tmp);
1030 for (p = tmp; p; p = p->next) {
1031 char buf[LONG_STRING];
1036 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1039 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1041 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1042 if (is_numerical_keyid (keyID)) {
1043 if (m_strncmp (keyID, "0x", 2) == 0)
1045 goto bypass_selection; /* you don't see this. */
1048 /* check for e-mail address */
1049 if ((t = strchr (keyID, '@')) &&
1050 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1051 rfc822_qualify (addr, fqdn);
1055 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1059 address_list_wipe(&tmp);
1060 address_list_wipe(&addr);
1066 pgp_invoke_getkeys (q);
1070 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1071 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1073 if ((key = pgp_ask_for_key (buf, q->mailbox,
1074 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1076 address_list_wipe(&tmp);
1077 address_list_wipe(&addr);
1084 keyID = pgp_keyid (key);
1087 keylist_size += m_strlen(keyID) + 4;
1088 p_realloc(&keylist, keylist_size);
1089 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "",
1091 keylist_used = m_strlen(keylist);
1093 pgp_free_key (&key);
1094 address_list_wipe(&addr);
1097 address_list_wipe(&tmp);
1101 /* Warning: "a" is no longer freed in this routine, you need
1102 * to free it later. This is necessary for $fcc_attach. */
1104 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1106 char buf[LONG_STRING];
1107 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1108 char pgpinfile[_POSIX_PATH_MAX];
1109 FILE *pgpin, *pgperr, *fpout, *fptmp;
1115 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
1116 if (fpout == NULL) {
1117 mutt_perror (_("Can't create temporary file"));
1121 pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(Tempdir), NULL);
1122 if (pgperr == NULL) {
1123 mutt_perror (pgperrfile);
1128 unlink (pgperrfile);
1130 fptmp = m_tempfile(pgpinfile, sizeof(pgpinfile), NONULL(Tempdir), NULL);
1131 if (fptmp == NULL) {
1132 mutt_perror (pgpinfile);
1136 unlink (pgperrfile);
1141 convert_to_7bit (a);
1143 mutt_write_mime_header (a, fptmp);
1144 fputc ('\n', fptmp);
1145 mutt_write_mime_body (a, fptmp);
1148 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1149 fileno (fpout), fileno (pgperr),
1150 pgpinfile, keylist, sign)) == -1) {
1157 if (!pgp_use_gpg_agent ())
1158 fputs (PgpPass, pgpin);
1159 fputc ('\n', pgpin);
1163 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1171 empty = (fgetc (fpout) == EOF);
1176 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1178 fputs (buf, stdout);
1182 /* pause if there is any error output from PGP */
1184 mutt_any_key_to_continue (NULL);
1187 /* fatal error while trying to encrypt message */
1189 pgp_void_passphrase (); /* just in case */
1195 t->type = TYPEMULTIPART;
1196 t->subtype = m_strdup("encrypted");
1197 t->encoding = ENC7BIT;
1199 t->disposition = DISPINLINE;
1201 parameter_set_boundary(&t->parameter);
1202 parameter_setval(&t->parameter, "protocol", "application/pgp-encrypted");
1204 t->parts = body_new();
1205 t->parts->type = TYPEAPPLICATION;
1206 t->parts->subtype = m_strdup("pgp-encrypted");
1207 t->parts->encoding = ENC7BIT;
1209 t->parts->next = body_new();
1210 t->parts->next->type = TYPEAPPLICATION;
1211 t->parts->next->subtype = m_strdup("octet-stream");
1212 t->parts->next->encoding = ENC7BIT;
1213 t->parts->next->filename = m_strdup(tempfile);
1214 t->parts->next->use_disp = 1;
1215 t->parts->next->disposition = DISPINLINE;
1216 t->parts->next->unlink = 1; /* delete after sending the message */
1217 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1222 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1226 char pgpoutfile[_POSIX_PATH_MAX];
1227 char pgperrfile[_POSIX_PATH_MAX];
1228 char pgpinfile[_POSIX_PATH_MAX];
1230 char body_charset[STRING];
1232 const char *send_charset;
1234 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1244 if (a->type != TYPETEXT)
1246 if (ascii_strcasecmp (a->subtype, "plain"))
1249 if ((fp = fopen (a->filename, "r")) == NULL) {
1250 mutt_perror (a->filename);
1254 pgpin = m_tempfile(pgpinfile, sizeof(pgpinfile), NONULL(Tempdir), NULL);
1255 if (pgpin == NULL) {
1256 mutt_perror (pgpinfile);
1261 /* The following code is really correct: If noconv is set,
1262 * a's charset parameter contains the on-disk character set, and
1263 * we have to convert from that to utf-8. If noconv is not set,
1264 * we have to convert from $charset to utf-8.
1267 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1269 from_charset = body_charset;
1271 from_charset = Charset;
1273 if (!charset_is_us_ascii (body_charset)) {
1277 if (flags & ENCRYPT)
1278 send_charset = "us-ascii";
1280 send_charset = "utf-8";
1282 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1283 while ((c = fgetconv (fc)) != EOF)
1286 fgetconv_close (&fc);
1289 send_charset = "us-ascii";
1290 mutt_copy_stream (fp, pgpin);
1295 pgpout = m_tempfile(pgpoutfile, sizeof(pgpoutfile), NONULL(Tempdir), NULL);
1296 pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(Tempdir), NULL);
1297 if (pgpout == NULL || pgperr == NULL) {
1298 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1302 unlink (pgpoutfile);
1308 unlink (pgperrfile);
1310 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1311 -1, fileno (pgpout), fileno (pgperr),
1312 pgpinfile, keylist, flags)) == -1) {
1313 mutt_perror (_("Can't invoke PGP"));
1317 mutt_unlink (pgpinfile);
1318 unlink (pgpoutfile);
1322 if (pgp_use_gpg_agent ())
1325 fprintf (pgpin, "%s\n", PgpPass);
1328 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1331 mutt_unlink (pgpinfile);
1340 empty = (fgetc (pgpout) == EOF);
1345 while (fgets (buff, sizeof (buff), pgperr)) {
1347 fputs (buff, stdout);
1353 mutt_any_key_to_continue (NULL);
1357 pgp_void_passphrase (); /* just in case */
1358 unlink (pgpoutfile);
1364 b->encoding = ENC7BIT;
1367 b->subtype = m_strdup("plain");
1369 parameter_setval(&b->parameter, "x-action",
1370 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed");
1371 parameter_setval(&b->parameter, "charset", send_charset);
1373 b->filename = m_strdup(pgpoutfile);
1375 b->disposition = DISPINLINE;
1381 if (!(flags & ENCRYPT))
1382 b->encoding = a->encoding;
1387 int pgp_send_menu (HEADER * msg, int *redraw)
1390 char input_signas[STRING];
1392 char prompt[LONG_STRING];
1394 /* If autoinline and no crypto options set, then set inline. */
1395 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1396 && (msg->security & (SIGN | ENCRYPT))))
1397 msg->security |= INLINE;
1399 snprintf (prompt, sizeof (prompt),
1400 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1401 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1403 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1404 case 1: /* (e)ncrypt */
1405 msg->security |= ENCRYPT;
1406 msg->security &= ~SIGN;
1409 case 2: /* (s)ign */
1410 msg->security |= SIGN;
1411 msg->security &= ~ENCRYPT;
1414 case 3: /* sign (a)s */
1415 unset_option (OPTPGPCHECKTRUST);
1418 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1420 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1421 m_strreplace(&PgpSignAs, input_signas);
1424 msg->security |= SIGN;
1426 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1429 *redraw = REDRAW_FULL;
1432 case 4: /* (b)oth */
1433 msg->security |= (ENCRYPT | SIGN);
1436 case 5: /* (i)nline */
1437 if ((msg->security & (ENCRYPT | SIGN)))
1438 msg->security ^= INLINE;
1440 msg->security &= ~INLINE;
1443 case 6: /* (f)orget it */
1444 case 7: /* (c)lear */
1449 if (msg->security) {
1450 if (!(msg->security & (ENCRYPT | SIGN)))
1453 msg->security |= APPLICATION_PGP;
1456 return (msg->security);