2 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 * This file contains all of the PGP routines necessary to sign, encrypt,
23 * verify and decrypt PGP messages in either the new PGP/MIME format, or
24 * in the older Application/Pgp format. It also contains some code to
25 * cache the user's passphrase for repeat use when decrypting or signing
34 #include "mutt_curses.h"
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
55 #ifdef HAVE_SYS_RESOURCE_H
56 # include <sys/resource.h>
59 #ifdef CRYPT_BACKEND_CLASSIC_PGP
61 #include "mutt_crypt.h"
62 #include "mutt_menu.h"
66 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
68 void pgp_void_passphrase (void)
70 memset (PgpPass, 0, sizeof (PgpPass));
74 int pgp_valid_passphrase (void)
76 time_t now = time (NULL);
78 if (pgp_use_gpg_agent())
81 return 1; /* handled by gpg-agent */
85 /* Use cached copy. */
88 pgp_void_passphrase ();
90 if (mutt_get_password (_("Enter PGP passphrase:"), PgpPass, sizeof (PgpPass)) == 0)
92 PgpExptime = time (NULL) + PgpTimeout;
101 void pgp_forget_passphrase (void)
103 pgp_void_passphrase ();
104 mutt_message _("PGP passphrase forgotten.");
107 int pgp_use_gpg_agent (void)
109 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY") && getenv ("GPG_AGENT_INFO");
112 char *pgp_keyid(pgp_key_t k)
114 if((k->flags & KEYFLAG_SUBKEY) && k->parent && option(OPTPGPIGNORESUB))
117 return _pgp_keyid(k);
120 char *_pgp_keyid(pgp_key_t k)
122 if(option(OPTPGPLONGIDS))
125 return (k->keyid + 8);
128 /* ----------------------------------------------------------------------------
129 * Routines for handing PGP input.
134 /* Copy PGP output messages and look for signs of a good signature */
136 static int pgp_copy_checksig (FILE *fpin, FILE *fpout)
140 if (PgpGoodSign.pattern)
146 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL)
148 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0)
150 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" matches regexp.\n",
155 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" doesn't match regexp.\n",
158 if (strncmp (line, "[GNUPG:] ", 9) == 0)
167 dprint (2, (debugfile, "pgp_copy_checksig: No pattern.\n"));
168 mutt_copy_stream (fpin, fpout);
176 * Copy a clearsigned message, and strip the signature and PGP's
179 * XXX - charset handling: We assume that it is safe to do
180 * character set decoding first, dash decoding second here, while
181 * we do it the other way around in the main handler.
183 * (Note that we aren't worse than Outlook &c in this, and also
184 * note that we can successfully handle anything produced by any
185 * existing versions of mutt.)
188 static void pgp_copy_clearsigned (FILE *fpin, STATE *s, char *charset)
190 char buf[HUGE_STRING];
191 short complete, armor_header;
197 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
199 for (complete = 1, armor_header = 1;
200 fgetconvs (buf, sizeof (buf), fc) != NULL;
201 complete = strchr (buf, '\n') != NULL)
210 if (mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
215 char *p = mutt_skip_whitespace (buf);
222 state_puts (s->prefix, s);
224 if (buf[0] == '-' && buf[1] == ' ')
225 state_puts (buf + 2, s);
230 fgetconv_close (&fc);
234 /* Support for the Application/PGP Content Type. */
236 void pgp_application_pgp_handler (BODY *m, STATE *s)
238 int needpass = -1, pgp_keyblock = 0;
239 int clearsign = 0, rv, rc;
241 long bytes, last_pos, offset;
242 char buf[HUGE_STRING];
243 char outfile[_POSIX_PATH_MAX];
244 char tmpfname[_POSIX_PATH_MAX];
245 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
249 short maybe_goodsig = 1;
250 short have_any_sigs = 0;
252 char body_charset[STRING];
253 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
255 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
257 fseek (s->fpin, m->offset, 0);
258 last_pos = m->offset;
260 for (bytes = m->length; bytes > 0;)
262 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
265 offset = ftell (s->fpin);
266 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
269 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
272 start_pos = last_pos;
274 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
276 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
281 else if (!option (OPTDONTHANDLEPGPKEYS) &&
282 mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
289 /* XXX - we may wish to recode here */
291 state_puts (s->prefix, s);
296 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
298 /* Copy PGP material to temporary file */
299 mutt_mktemp (tmpfname);
300 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL)
302 mutt_perror (tmpfname);
307 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL)
309 offset = ftell (s->fpin);
310 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
315 if ((needpass && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0) ||
317 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
318 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",buf) == 0)))
322 /* leave tmpfp open in case we still need it - but flush it! */
326 /* Invoke PGP if needed */
327 if (!clearsign || (s->flags & M_VERIFY))
329 mutt_mktemp (outfile);
330 if ((pgpout = safe_fopen (outfile, "w+")) == NULL)
332 mutt_perror (tmpfname);
336 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
337 fileno (pgpout), -1, tmpfname,
340 safe_fclose (&pgpout);
344 state_attach_puts (_("[-- Error: unable to create PGP subprocess! --]\n"), s);
346 else /* PGP started successfully */
350 if (!pgp_valid_passphrase ()) pgp_void_passphrase();
351 if (pgp_use_gpg_agent())
353 fprintf (pgpin, "%s\n", PgpPass);
356 safe_fclose (&pgpin);
358 if (s->flags & M_DISPLAY)
360 crypt_current_time (s, "PGP");
361 rc = pgp_copy_checksig (pgperr, s->fpout);
364 safe_fclose (&pgperr);
365 rv = mutt_wait_filter (thepid);
367 if (s->flags & M_DISPLAY)
369 if (rc == 0) have_any_sigs = 1;
372 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
373 * Sig _is_ correct if
374 * gpg_good_sign="" && pgp_decode_command returned 0
376 if (rc == -1 || rv) maybe_goodsig = 0;
378 state_putc ('\n', s);
379 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
386 * Now, copy cleartext to the screen. NOTE - we expect that PGP
387 * outputs utf-8 cleartext. This may not always be true, but it
388 * seems to be a reasonable guess.
391 if(s->flags & M_DISPLAY)
394 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
395 else if (pgp_keyblock)
396 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
398 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
405 pgp_copy_clearsigned (tmpfp, s, body_charset);
412 state_set_prefix (s);
413 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
414 while ((c = fgetconv (fc)) != EOF)
415 state_prefix_putc (c, s);
416 fgetconv_close (&fc);
419 if (s->flags & M_DISPLAY)
421 state_putc ('\n', s);
423 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
424 else if (pgp_keyblock)
425 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
427 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
432 safe_fclose (&tmpfp);
433 mutt_unlink (tmpfname);
437 safe_fclose (&pgpout);
438 mutt_unlink (outfile);
443 /* XXX - we may wish to recode here */
445 state_puts (s->prefix, s);
450 m->goodsig = (maybe_goodsig && have_any_sigs);
454 state_attach_puts (_("[-- Error: could not find beginning of PGP message! --]\n\n"), s);
459 static int pgp_check_traditional_one_body (FILE *fp, BODY *b, int tagged_only)
461 char tempfile[_POSIX_PATH_MAX];
462 char buf[HUGE_STRING];
469 if (b->type != TYPETEXT)
472 if (tagged_only && !b->tagged)
475 mutt_mktemp (tempfile);
476 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0)
482 if ((tfp = fopen (tempfile, "r")) == NULL)
488 while (fgets (buf, sizeof (buf), tfp))
490 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
492 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
494 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
496 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
503 if (!enc && !sgn && !key)
506 /* fix the content type */
508 mutt_set_parameter ("format", "fixed", &b->parameter);
510 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
512 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
514 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
519 int pgp_check_traditional (FILE *fp, BODY *b, int tagged_only)
523 for (; b; b = b->next)
525 if (is_multipart (b))
526 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
527 else if (b->type == TYPETEXT)
529 if ((r = mutt_is_application_pgp (b)))
532 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
543 int pgp_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
545 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
546 FILE *fp, *pgpout, *pgperr;
551 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
553 if(!(fp = safe_fopen (sigfile, "w")))
555 mutt_perror(sigfile);
559 fseek (s->fpin, sigbdy->offset, 0);
560 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
563 mutt_mktemp(pgperrfile);
564 if(!(pgperr = safe_fopen(pgperrfile, "w+")))
566 mutt_perror(pgperrfile);
571 crypt_current_time (s, "PGP");
573 if((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
574 -1, -1, fileno(pgperr),
575 tempfile, sigfile)) != -1)
577 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
581 safe_fclose (&pgpout);
585 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
588 if ((rv = mutt_wait_filter (thepid)))
591 dprint (1, (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n", rv));
594 safe_fclose (&pgperr);
596 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
598 mutt_unlink (sigfile);
599 mutt_unlink (pgperrfile);
601 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
607 /* Extract pgp public keys from messages or attachments */
609 void pgp_extract_keys_from_messages (HEADER *h)
612 char tempfname[_POSIX_PATH_MAX];
617 mutt_parse_mime_message (Context, h);
618 if(h->security & PGPENCRYPT && !pgp_valid_passphrase ())
622 mutt_mktemp (tempfname);
623 if (!(fpout = safe_fopen (tempfname, "w")))
625 mutt_perror (tempfname);
629 set_option (OPTDONTHANDLEPGPKEYS);
633 for (i = 0; i < Context->vcount; i++)
635 if (Context->hdrs[Context->v2r[i]]->tagged)
637 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
638 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
639 && !pgp_valid_passphrase())
644 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
645 M_CM_DECODE|M_CM_CHARCONV, 0);
651 mutt_parse_mime_message (Context, h);
652 if (h->security & PGPENCRYPT && !pgp_valid_passphrase())
657 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
662 pgp_invoke_import (tempfname);
663 mutt_any_key_to_continue (NULL);
667 mutt_unlink (tempfname);
668 unset_option (OPTDONTHANDLEPGPKEYS);
672 static void pgp_extract_keys_from_attachment (FILE *fp, BODY *top)
676 char tempfname[_POSIX_PATH_MAX];
678 mutt_mktemp (tempfname);
679 if (!(tempfp = safe_fopen (tempfname, "w")))
681 mutt_perror (tempfname);
685 memset (&s, 0, sizeof (STATE));
690 mutt_body_handler (top, &s);
694 pgp_invoke_import (tempfname);
695 mutt_any_key_to_continue (NULL);
697 mutt_unlink (tempfname);
700 void pgp_extract_keys_from_attachment_list (FILE *fp, int tag, BODY *top)
704 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
709 set_option(OPTDONTHANDLEPGPKEYS);
711 for(; top; top = top->next)
713 if(!tag || top->tagged)
714 pgp_extract_keys_from_attachment (fp, top);
720 unset_option(OPTDONTHANDLEPGPKEYS);
723 BODY *pgp_decrypt_part (BODY *a, STATE *s, FILE *fpout, BODY *p)
725 char buf[LONG_STRING];
726 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
730 char pgperrfile[_POSIX_PATH_MAX];
731 char pgptmpfile[_POSIX_PATH_MAX];
734 mutt_mktemp (pgperrfile);
735 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
737 mutt_perror (pgperrfile);
742 mutt_mktemp (pgptmpfile);
743 if((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL)
745 mutt_perror (pgptmpfile);
750 /* Position the stream at the beginning of the body, and send the data to
751 * the temporary file.
754 fseek (s->fpin, a->offset, 0);
755 mutt_copy_bytes (s->fpin, pgptmp, a->length);
758 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
759 fileno (pgperr), pgptmpfile)) == -1)
763 if (s->flags & M_DISPLAY)
764 state_attach_puts (_("[-- Error: could not create a PGP subprocess! --]\n\n"), s);
768 /* send the PGP passphrase to the subprocess. Never do this if the
769 agent is active, because this might lead to a passphrase send as
771 if (!pgp_use_gpg_agent())
772 fputs (PgpPass, pgpin);
776 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
777 * read_mime_header has a hard time parsing the message.
779 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL)
781 len = mutt_strlen (buf);
782 if (len > 1 && buf[len - 2] == '\r')
783 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
788 mutt_wait_filter (thepid);
789 mutt_unlink(pgptmpfile);
791 if (s->flags & M_DISPLAY)
795 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && p)
797 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
804 if (fgetc (fpout) == EOF)
809 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL)
812 * Need to set the length of this body part.
814 fstat (fileno (fpout), &info);
815 tattach->length = info.st_size - tattach->offset;
817 /* See if we need to recurse on this MIME part. */
819 mutt_parse_part (fpout, tattach);
825 int pgp_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
827 char tempfile[_POSIX_PATH_MAX];
831 if(!mutt_is_multipart_encrypted(b))
834 if(!b->parts || !b->parts->next)
839 memset (&s, 0, sizeof (s));
841 mutt_mktemp (tempfile);
842 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL)
844 mutt_perror (tempfile);
849 *cur = pgp_decrypt_part (b, &s, *fpout, p);
859 void pgp_encrypted_handler (BODY *a, STATE *s)
861 char tempfile[_POSIX_PATH_MAX];
867 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
868 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
869 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
870 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0)
872 if (s->flags & M_DISPLAY)
873 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"), s);
878 * Move forward to the application/pgp-encrypted body.
882 mutt_mktemp (tempfile);
883 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
885 if (s->flags & M_DISPLAY)
886 state_attach_puts (_("[-- Error: could not create temporary file! --]\n"), s);
890 if (s->flags & M_DISPLAY) crypt_current_time (s, "PGP");
892 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL)
894 if (s->flags & M_DISPLAY)
895 state_attach_puts (_("[-- The following data is PGP/MIME encrypted --]\n\n"), s);
899 mutt_body_handler (tattach, s);
903 * if a multipart/signed is the _only_ sub-part of a
904 * multipart/encrypted, cache signature verification
909 if (mutt_is_multipart_signed (tattach) && !tattach->next)
910 p->goodsig |= tattach->goodsig;
912 if (s->flags & M_DISPLAY)
914 state_puts ("\n", s);
915 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
918 mutt_free_body (&tattach);
922 mutt_unlink(tempfile);
925 /* ----------------------------------------------------------------------------
926 * Routines for sending PGP/MIME messages.
930 BODY *pgp_sign_message (BODY *a)
933 char buffer[LONG_STRING];
934 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
935 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
940 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
942 mutt_mktemp (sigfile);
943 if ((fp = safe_fopen (sigfile, "w")) == NULL)
948 mutt_mktemp (signedfile);
949 if ((sfp = safe_fopen(signedfile, "w")) == NULL)
951 mutt_perror(signedfile);
957 mutt_write_mime_header (a, sfp);
959 mutt_write_mime_body (a, sfp);
962 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
963 -1, -1, -1, signedfile)) == -1)
965 mutt_perror _("Can't open PGP subprocess!");
972 if (!pgp_use_gpg_agent())
973 fputs(PgpPass, pgpin);
978 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
979 * recommended for future releases of PGP.
981 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL)
983 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
984 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
985 else if (mutt_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
986 fputs ("-----END PGP SIGNATURE-----\n", fp);
989 empty = 0; /* got some output, so we're ok */
992 /* check for errors from PGP */
994 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL)
997 fputs (buffer, stdout);
1000 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1005 unlink (signedfile);
1007 if (fclose (fp) != 0)
1009 mutt_perror ("fclose");
1016 pgp_void_passphrase();
1017 mutt_any_key_to_continue (NULL);
1023 return (NULL); /* fatal error while signing */
1026 t = mutt_new_body ();
1027 t->type = TYPEMULTIPART;
1028 t->subtype = safe_strdup ("signed");
1029 t->encoding = ENC7BIT;
1031 t->disposition = DISPINLINE;
1033 mutt_generate_boundary (&t->parameter);
1034 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1035 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1040 t->parts->next = mutt_new_body ();
1042 t->type = TYPEAPPLICATION;
1043 t->subtype = safe_strdup ("pgp-signature");
1044 t->filename = safe_strdup (sigfile);
1046 t->disposition = DISPINLINE;
1047 t->encoding = ENC7BIT;
1048 t->unlink = 1; /* ok to remove this file after sending. */
1053 static short is_numerical_keyid (const char *s)
1055 /* or should we require the "0x"? */
1056 if (strncmp (s, "0x", 2) == 0)
1061 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1067 /* This routine attempts to find the keyids of the recipients of a message.
1068 * It returns NULL if any of the keys can not be found.
1070 char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
1072 char *keyID, *keylist = NULL, *t;
1073 size_t keylist_size = 0;
1074 size_t keylist_used = 0;
1075 ADDRESS *tmp = NULL, *addr = NULL;
1076 ADDRESS **last = &tmp;
1079 pgp_key_t k_info = NULL, key = NULL;
1081 const char *fqdn = mutt_fqdn (1);
1083 for (i = 0; i < 3; i++)
1087 case 0: p = to; break;
1088 case 1: p = cc; break;
1089 case 2: p = bcc; break;
1093 *last = rfc822_cpy_adr (p);
1095 last = &((*last)->next);
1099 rfc822_qualify (tmp, fqdn);
1101 tmp = mutt_remove_duplicates (tmp);
1103 for (p = tmp; p ; p = p->next)
1105 char buf[LONG_STRING];
1110 if ((keyID = mutt_crypt_hook (p)) != NULL)
1113 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID, p->mailbox);
1114 if ((r = mutt_yesorno (buf, M_YES)) == M_YES)
1116 if (is_numerical_keyid (keyID))
1118 if (strncmp (keyID, "0x", 2) == 0)
1120 goto bypass_selection; /* you don't see this. */
1123 /* check for e-mail address */
1124 if ((t = strchr (keyID, '@')) &&
1125 (addr = rfc822_parse_adrlist (NULL, keyID)))
1127 if (fqdn) rfc822_qualify (addr, fqdn);
1131 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1136 rfc822_free_address (&tmp);
1137 rfc822_free_address (&addr);
1143 pgp_invoke_getkeys (q);
1145 if (k_info == NULL && (k_info = pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1147 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1149 if ((key = pgp_ask_for_key (buf, q->mailbox,
1150 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1153 rfc822_free_address (&tmp);
1154 rfc822_free_address (&addr);
1161 keyID = pgp_keyid (key);
1164 keylist_size += mutt_strlen (keyID) + 4;
1165 safe_realloc (&keylist, keylist_size);
1166 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1168 keylist_used = mutt_strlen (keylist);
1170 pgp_free_key (&key);
1171 rfc822_free_address (&addr);
1174 rfc822_free_address (&tmp);
1178 /* Warning: "a" is no longer freed in this routine, you need
1179 * to free it later. This is necessary for $fcc_attach. */
1181 BODY *pgp_encrypt_message (BODY *a, char *keylist, int sign)
1183 char buf[LONG_STRING];
1184 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1185 char pgpinfile[_POSIX_PATH_MAX];
1186 FILE *pgpin, *pgperr, *fpout, *fptmp;
1192 mutt_mktemp (tempfile);
1193 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
1195 mutt_perror (tempfile);
1199 mutt_mktemp (pgperrfile);
1200 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1202 mutt_perror (pgperrfile);
1207 unlink (pgperrfile);
1209 mutt_mktemp(pgpinfile);
1210 if((fptmp = safe_fopen(pgpinfile, "w")) == NULL)
1212 mutt_perror(pgpinfile);
1220 convert_to_7bit (a);
1222 mutt_write_mime_header (a, fptmp);
1223 fputc ('\n', fptmp);
1224 mutt_write_mime_body (a, fptmp);
1227 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1228 fileno (fpout), fileno (pgperr),
1229 pgpinfile, keylist, sign)) == -1)
1238 if (!pgp_use_gpg_agent())
1239 fputs (PgpPass, pgpin);
1240 fputc ('\n', pgpin);
1244 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1252 empty = (fgetc (fpout) == EOF);
1257 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL)
1260 fputs (buf, stdout);
1264 /* pause if there is any error output from PGP */
1266 mutt_any_key_to_continue (NULL);
1270 /* fatal error while trying to encrypt message */
1275 t = mutt_new_body ();
1276 t->type = TYPEMULTIPART;
1277 t->subtype = safe_strdup ("encrypted");
1278 t->encoding = ENC7BIT;
1280 t->disposition = DISPINLINE;
1282 mutt_generate_boundary(&t->parameter);
1283 mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
1285 t->parts = mutt_new_body ();
1286 t->parts->type = TYPEAPPLICATION;
1287 t->parts->subtype = safe_strdup ("pgp-encrypted");
1288 t->parts->encoding = ENC7BIT;
1290 t->parts->next = mutt_new_body ();
1291 t->parts->next->type = TYPEAPPLICATION;
1292 t->parts->next->subtype = safe_strdup ("octet-stream");
1293 t->parts->next->encoding = ENC7BIT;
1294 t->parts->next->filename = safe_strdup (tempfile);
1295 t->parts->next->use_disp = 1;
1296 t->parts->next->disposition = DISPINLINE;
1297 t->parts->next->unlink = 1; /* delete after sending the message */
1298 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1303 BODY *pgp_traditional_encryptsign (BODY *a, int flags, char *keylist)
1307 char pgpoutfile[_POSIX_PATH_MAX];
1308 char pgperrfile[_POSIX_PATH_MAX];
1309 char pgpinfile[_POSIX_PATH_MAX];
1311 char body_charset[STRING];
1313 const char *send_charset;
1315 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1325 if (a->type != TYPETEXT)
1327 if (ascii_strcasecmp (a->subtype, "plain"))
1330 if ((fp = fopen (a->filename, "r")) == NULL)
1332 mutt_perror (a->filename);
1336 mutt_mktemp (pgpinfile);
1337 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL)
1339 mutt_perror (pgpinfile);
1344 /* The following code is really correct: If noconv is set,
1345 * a's charset parameter contains the on-disk character set, and
1346 * we have to convert from that to utf-8. If noconv is not set,
1347 * we have to convert from $charset to utf-8.
1350 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1352 from_charset = body_charset;
1354 from_charset = Charset;
1356 if (!mutt_is_us_ascii (body_charset))
1361 if (flags & ENCRYPT)
1362 send_charset = "us-ascii";
1364 send_charset = "utf-8";
1366 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1367 while ((c = fgetconv (fc)) != EOF)
1370 fgetconv_close (&fc);
1374 send_charset = "us-ascii";
1375 mutt_copy_stream (fp, pgpin);
1380 mutt_mktemp (pgpoutfile);
1381 mutt_mktemp (pgperrfile);
1382 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1383 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1385 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1390 unlink (pgpoutfile);
1395 unlink (pgperrfile);
1397 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1398 -1, fileno (pgpout), fileno (pgperr),
1399 pgpinfile, keylist, flags)) == -1)
1401 mutt_perror _("Can't invoke PGP");
1404 mutt_unlink (pgpinfile);
1405 unlink (pgpoutfile);
1409 if (pgp_use_gpg_agent())
1412 fprintf (pgpin, "%s\n", PgpPass);
1415 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1418 mutt_unlink (pgpinfile);
1427 empty = (fgetc (pgpout) == EOF);
1432 while (fgets (buff, sizeof (buff), pgperr))
1435 fputs (buff, stdout);
1441 mutt_any_key_to_continue (NULL);
1445 unlink (pgpoutfile);
1449 b = mutt_new_body ();
1451 b->encoding = ENC7BIT;
1454 b->subtype = safe_strdup ("plain");
1456 mutt_set_parameter ("x-action", flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1458 mutt_set_parameter ("charset", send_charset, &b->parameter);
1460 b->filename = safe_strdup (pgpoutfile);
1463 /* The following is intended to give a clue to some completely brain-dead
1464 * "mail environments" which are typically used by large corporations.
1467 b->d_filename = safe_strdup ("msg.pgp");
1472 b->disposition = DISPINLINE;
1478 if (!(flags & ENCRYPT))
1479 b->encoding = a->encoding;
1484 int pgp_send_menu (HEADER *msg, int *redraw)
1487 char input_signas[SHORT_STRING];
1489 char prompt[LONG_STRING];
1491 if (!(WithCrypto & APPLICATION_PGP))
1492 return msg->security;
1494 /* If autoinline and no crypto options set, then set inline. */
1495 if (option (OPTPGPAUTOINLINE) &&
1496 !((msg->security & APPLICATION_PGP) && (msg->security & (SIGN|ENCRYPT))))
1497 msg->security |= INLINE;
1499 snprintf (prompt, sizeof (prompt),
1500 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1501 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1503 switch (mutt_multi_choice (prompt, _("esabifc")))
1505 case 1: /* (e)ncrypt */
1506 msg->security |= ENCRYPT;
1507 msg->security &= ~SIGN;
1510 case 2: /* (s)ign */
1511 msg->security |= SIGN;
1512 msg->security &= ~ENCRYPT;
1515 case 3: /* sign (a)s */
1516 unset_option(OPTPGPCHECKTRUST);
1518 if ((p = pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN, PGP_PUBRING)))
1520 snprintf (input_signas, sizeof (input_signas), "0x%s",
1522 mutt_str_replace (&PgpSignAs, input_signas);
1525 msg->security |= SIGN;
1527 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1532 msg->security &= ~SIGN;
1536 *redraw = REDRAW_FULL;
1539 case 4: /* (b)oth */
1540 msg->security |= (ENCRYPT | SIGN);
1543 case 5: /* (i)nline */
1544 if ((msg->security & (ENCRYPT | SIGN)))
1545 msg->security ^= INLINE;
1547 msg->security &= ~INLINE;
1550 case 6: /* (f)orget it */
1551 case 7: /* (c)lear */
1558 if (! (msg->security & (ENCRYPT | SIGN)))
1561 msg->security |= APPLICATION_PGP;
1564 return (msg->security);
1568 #endif /* CRYPT_BACKEND_CLASSIC_PGP */