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 ()) {
80 return 1; /* handled by gpg-agent */
84 /* Use cached copy. */
87 pgp_void_passphrase ();
90 (_("Enter PGP passphrase:"), PgpPass, sizeof (PgpPass)) == 0) {
91 PgpExptime = time (NULL) + PgpTimeout;
100 void pgp_forget_passphrase (void)
102 pgp_void_passphrase ();
103 mutt_message _("PGP passphrase forgotten.");
106 int pgp_use_gpg_agent (void)
108 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
109 && 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) {
145 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
146 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
147 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" matches regexp.\n",
154 "pgp_copy_checksig: \"%s\" doesn't match regexp.\n", line));
156 if (strncmp (line, "[GNUPG:] ", 9) == 0)
164 dprint (2, (debugfile, "pgp_copy_checksig: No pattern.\n"));
165 mutt_copy_stream (fpin, fpout);
173 * Copy a clearsigned message, and strip the signature and PGP's
176 * XXX - charset handling: We assume that it is safe to do
177 * character set decoding first, dash decoding second here, while
178 * we do it the other way around in the main handler.
180 * (Note that we aren't worse than Outlook &c in this, and also
181 * note that we can successfully handle anything produced by any
182 * existing versions of mutt.)
185 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
187 char buf[HUGE_STRING];
188 short complete, armor_header;
194 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
196 for (complete = 1, armor_header = 1;
197 fgetconvs (buf, sizeof (buf), fc) != NULL;
198 complete = strchr (buf, '\n') != NULL) {
205 if (mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
209 char *p = mutt_skip_whitespace (buf);
217 state_puts (s->prefix, s);
219 if (buf[0] == '-' && buf[1] == ' ')
220 state_puts (buf + 2, s);
225 fgetconv_close (&fc);
229 /* Support for the Application/PGP Content Type. */
231 void pgp_application_pgp_handler (BODY * m, STATE * s)
233 int needpass = -1, pgp_keyblock = 0;
234 int clearsign = 0, rv, rc;
236 long bytes, last_pos, offset;
237 char buf[HUGE_STRING];
238 char outfile[_POSIX_PATH_MAX];
239 char tmpfname[_POSIX_PATH_MAX];
240 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
244 short maybe_goodsig = 1;
245 short have_any_sigs = 0;
247 char body_charset[STRING];
249 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
251 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
253 fseek (s->fpin, m->offset, 0);
254 last_pos = m->offset;
256 for (bytes = m->length; bytes > 0;) {
257 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
260 offset = ftell (s->fpin);
261 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
264 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
266 start_pos = last_pos;
268 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
270 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
274 else if (!option (OPTDONTHANDLEPGPKEYS) &&
275 mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
280 /* XXX - we may wish to recode here */
282 state_puts (s->prefix, s);
287 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
289 /* Copy PGP material to temporary file */
290 mutt_mktemp (tmpfname);
291 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
292 mutt_perror (tmpfname);
297 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
298 offset = ftell (s->fpin);
299 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
305 && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0)
307 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
308 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
313 /* leave tmpfp open in case we still need it - but flush it! */
317 /* Invoke PGP if needed */
318 if (!clearsign || (s->flags & M_VERIFY)) {
319 mutt_mktemp (outfile);
320 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
321 mutt_perror (tmpfname);
325 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
326 fileno (pgpout), -1, tmpfname,
328 safe_fclose (&pgpout);
333 ("[-- Error: unable to create PGP subprocess! --]\n"),
336 else { /* PGP started successfully */
339 if (!pgp_valid_passphrase ())
340 pgp_void_passphrase ();
341 if (pgp_use_gpg_agent ())
343 fprintf (pgpin, "%s\n", PgpPass);
346 safe_fclose (&pgpin);
348 if (s->flags & M_DISPLAY) {
349 crypt_current_time (s, "PGP");
350 rc = pgp_copy_checksig (pgperr, s->fpout);
353 safe_fclose (&pgperr);
354 rv = mutt_wait_filter (thepid);
356 if (s->flags & M_DISPLAY) {
361 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
362 * Sig _is_ correct if
363 * gpg_good_sign="" && pgp_decode_command returned 0
368 state_putc ('\n', s);
369 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
376 * Now, copy cleartext to the screen. NOTE - we expect that PGP
377 * outputs utf-8 cleartext. This may not always be true, but it
378 * seems to be a reasonable guess.
381 if (s->flags & M_DISPLAY) {
383 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
384 else if (pgp_keyblock)
385 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
387 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
393 pgp_copy_clearsigned (tmpfp, s, body_charset);
400 state_set_prefix (s);
401 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
402 while ((c = fgetconv (fc)) != EOF)
403 state_prefix_putc (c, s);
404 fgetconv_close (&fc);
407 if (s->flags & M_DISPLAY) {
408 state_putc ('\n', s);
410 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
411 else if (pgp_keyblock)
412 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
414 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
418 safe_fclose (&tmpfp);
419 mutt_unlink (tmpfname);
422 safe_fclose (&pgpout);
423 mutt_unlink (outfile);
427 /* XXX - we may wish to recode here */
429 state_puts (s->prefix, s);
434 m->goodsig = (maybe_goodsig && have_any_sigs);
436 if (needpass == -1) {
438 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
444 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
447 char tempfile[_POSIX_PATH_MAX];
448 char buf[HUGE_STRING];
455 if (b->type != TYPETEXT)
458 if (tagged_only && !b->tagged)
461 mutt_mktemp (tempfile);
462 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
467 if ((tfp = fopen (tempfile, "r")) == NULL) {
472 while (fgets (buf, sizeof (buf), tfp)) {
473 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
474 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
476 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
478 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
485 if (!enc && !sgn && !key)
488 /* fix the content type */
490 mutt_set_parameter ("format", "fixed", &b->parameter);
492 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
494 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
496 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
501 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
506 for (; b; b = b->next) {
507 if (is_multipart (b))
508 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
509 else if (b->type == TYPETEXT) {
510 if ((r = mutt_is_application_pgp (b)))
513 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
524 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
526 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
527 FILE *fp, *pgpout, *pgperr;
532 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
534 if (!(fp = safe_fopen (sigfile, "w"))) {
535 mutt_perror (sigfile);
539 fseek (s->fpin, sigbdy->offset, 0);
540 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
543 mutt_mktemp (pgperrfile);
544 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
545 mutt_perror (pgperrfile);
550 crypt_current_time (s, "PGP");
552 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
553 -1, -1, fileno (pgperr),
554 tempfile, sigfile)) != -1) {
555 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
559 safe_fclose (&pgpout);
563 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
566 if ((rv = mutt_wait_filter (thepid)))
570 (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n",
574 safe_fclose (&pgperr);
576 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
578 mutt_unlink (sigfile);
579 mutt_unlink (pgperrfile);
581 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
587 /* Extract pgp public keys from messages or attachments */
589 void pgp_extract_keys_from_messages (HEADER * h)
592 char tempfname[_POSIX_PATH_MAX];
596 mutt_parse_mime_message (Context, h);
597 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
601 mutt_mktemp (tempfname);
602 if (!(fpout = safe_fopen (tempfname, "w"))) {
603 mutt_perror (tempfname);
607 set_option (OPTDONTHANDLEPGPKEYS);
610 for (i = 0; i < Context->vcount; i++) {
611 if (Context->hdrs[Context->v2r[i]]->tagged) {
612 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
613 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
614 && !pgp_valid_passphrase ()) {
618 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
619 M_CM_DECODE | M_CM_CHARCONV, 0);
624 mutt_parse_mime_message (Context, h);
625 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
629 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
634 pgp_invoke_import (tempfname);
635 mutt_any_key_to_continue (NULL);
639 mutt_unlink (tempfname);
640 unset_option (OPTDONTHANDLEPGPKEYS);
644 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
648 char tempfname[_POSIX_PATH_MAX];
650 mutt_mktemp (tempfname);
651 if (!(tempfp = safe_fopen (tempfname, "w"))) {
652 mutt_perror (tempfname);
656 memset (&s, 0, sizeof (STATE));
661 mutt_body_handler (top, &s);
665 pgp_invoke_import (tempfname);
666 mutt_any_key_to_continue (NULL);
668 mutt_unlink (tempfname);
671 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
674 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
680 set_option (OPTDONTHANDLEPGPKEYS);
682 for (; top; top = top->next) {
683 if (!tag || top->tagged)
684 pgp_extract_keys_from_attachment (fp, top);
690 unset_option (OPTDONTHANDLEPGPKEYS);
693 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
695 char buf[LONG_STRING];
696 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
700 char pgperrfile[_POSIX_PATH_MAX];
701 char pgptmpfile[_POSIX_PATH_MAX];
704 mutt_mktemp (pgperrfile);
705 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
706 mutt_perror (pgperrfile);
711 mutt_mktemp (pgptmpfile);
712 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
713 mutt_perror (pgptmpfile);
718 /* Position the stream at the beginning of the body, and send the data to
719 * the temporary file.
722 fseek (s->fpin, a->offset, 0);
723 mutt_copy_bytes (s->fpin, pgptmp, a->length);
726 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
727 fileno (pgperr), pgptmpfile)) == -1) {
730 if (s->flags & M_DISPLAY)
732 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
737 /* send the PGP passphrase to the subprocess. Never do this if the
738 agent is active, because this might lead to a passphrase send as
740 if (!pgp_use_gpg_agent ())
741 fputs (PgpPass, pgpin);
745 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
746 * read_mime_header has a hard time parsing the message.
748 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
749 len = mutt_strlen (buf);
750 if (len > 1 && buf[len - 2] == '\r')
751 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
756 mutt_wait_filter (thepid);
757 mutt_unlink (pgptmpfile);
759 if (s->flags & M_DISPLAY) {
762 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && p)
764 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
771 if (fgetc (fpout) == EOF)
776 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
778 * Need to set the length of this body part.
780 fstat (fileno (fpout), &info);
781 tattach->length = info.st_size - tattach->offset;
783 /* See if we need to recurse on this MIME part. */
785 mutt_parse_part (fpout, tattach);
791 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
793 char tempfile[_POSIX_PATH_MAX];
797 if (!mutt_is_multipart_encrypted (b))
800 if (!b->parts || !b->parts->next)
805 memset (&s, 0, sizeof (s));
807 mutt_mktemp (tempfile);
808 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
809 mutt_perror (tempfile);
814 *cur = pgp_decrypt_part (b, &s, *fpout, p);
824 void pgp_encrypted_handler (BODY * a, STATE * s)
826 char tempfile[_POSIX_PATH_MAX];
832 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
833 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
834 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
835 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
836 if (s->flags & M_DISPLAY)
837 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
843 * Move forward to the application/pgp-encrypted body.
847 mutt_mktemp (tempfile);
848 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
849 if (s->flags & M_DISPLAY)
851 ("[-- Error: could not create temporary file! --]\n"),
856 if (s->flags & M_DISPLAY)
857 crypt_current_time (s, "PGP");
859 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
860 if (s->flags & M_DISPLAY)
862 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
867 mutt_body_handler (tattach, s);
871 * if a multipart/signed is the _only_ sub-part of a
872 * multipart/encrypted, cache signature verification
877 if (mutt_is_multipart_signed (tattach) && !tattach->next)
878 p->goodsig |= tattach->goodsig;
880 if (s->flags & M_DISPLAY) {
881 state_puts ("\n", s);
882 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
885 mutt_free_body (&tattach);
889 mutt_unlink (tempfile);
892 /* ----------------------------------------------------------------------------
893 * Routines for sending PGP/MIME messages.
897 BODY *pgp_sign_message (BODY * a)
900 char buffer[LONG_STRING];
901 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
902 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
907 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
909 mutt_mktemp (sigfile);
910 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
914 mutt_mktemp (signedfile);
915 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
916 mutt_perror (signedfile);
922 mutt_write_mime_header (a, sfp);
924 mutt_write_mime_body (a, sfp);
927 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
928 -1, -1, -1, signedfile)) == -1) {
929 mutt_perror _("Can't open PGP subprocess!");
937 if (!pgp_use_gpg_agent ())
938 fputs (PgpPass, pgpin);
943 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
944 * recommended for future releases of PGP.
946 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
947 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
948 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
949 else if (mutt_strcmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
950 fputs ("-----END PGP SIGNATURE-----\n", fp);
953 empty = 0; /* got some output, so we're ok */
956 /* check for errors from PGP */
958 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
960 fputs (buffer, stdout);
963 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
970 if (fclose (fp) != 0) {
971 mutt_perror ("fclose");
977 pgp_void_passphrase ();
978 mutt_any_key_to_continue (NULL);
983 return (NULL); /* fatal error while signing */
986 t = mutt_new_body ();
987 t->type = TYPEMULTIPART;
988 t->subtype = safe_strdup ("signed");
989 t->encoding = ENC7BIT;
991 t->disposition = DISPINLINE;
993 mutt_generate_boundary (&t->parameter);
994 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
995 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1000 t->parts->next = mutt_new_body ();
1002 t->type = TYPEAPPLICATION;
1003 t->subtype = safe_strdup ("pgp-signature");
1004 t->filename = safe_strdup (sigfile);
1006 t->disposition = DISPINLINE;
1007 t->encoding = ENC7BIT;
1008 t->unlink = 1; /* ok to remove this file after sending. */
1013 static short is_numerical_keyid (const char *s)
1015 /* or should we require the "0x"? */
1016 if (strncmp (s, "0x", 2) == 0)
1021 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1027 /* This routine attempts to find the keyids of the recipients of a message.
1028 * It returns NULL if any of the keys can not be found.
1030 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1032 char *keyID, *keylist = NULL, *t;
1033 size_t keylist_size = 0;
1034 size_t keylist_used = 0;
1035 ADDRESS *tmp = NULL, *addr = NULL;
1036 ADDRESS **last = &tmp;
1039 pgp_key_t k_info = NULL, key = NULL;
1041 const char *fqdn = mutt_fqdn (1);
1043 for (i = 0; i < 3; i++) {
1058 *last = rfc822_cpy_adr (p);
1060 last = &((*last)->next);
1064 rfc822_qualify (tmp, fqdn);
1066 tmp = mutt_remove_duplicates (tmp);
1068 for (p = tmp; p; p = p->next) {
1069 char buf[LONG_STRING];
1074 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1077 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1079 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1080 if (is_numerical_keyid (keyID)) {
1081 if (strncmp (keyID, "0x", 2) == 0)
1083 goto bypass_selection; /* you don't see this. */
1086 /* check for e-mail address */
1087 if ((t = strchr (keyID, '@')) &&
1088 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1090 rfc822_qualify (addr, fqdn);
1094 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1098 rfc822_free_address (&tmp);
1099 rfc822_free_address (&addr);
1105 pgp_invoke_getkeys (q);
1109 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1110 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1112 if ((key = pgp_ask_for_key (buf, q->mailbox,
1113 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1115 rfc822_free_address (&tmp);
1116 rfc822_free_address (&addr);
1123 keyID = pgp_keyid (key);
1126 keylist_size += mutt_strlen (keyID) + 4;
1127 safe_realloc (&keylist, keylist_size);
1128 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1130 keylist_used = mutt_strlen (keylist);
1132 pgp_free_key (&key);
1133 rfc822_free_address (&addr);
1136 rfc822_free_address (&tmp);
1140 /* Warning: "a" is no longer freed in this routine, you need
1141 * to free it later. This is necessary for $fcc_attach. */
1143 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1145 char buf[LONG_STRING];
1146 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1147 char pgpinfile[_POSIX_PATH_MAX];
1148 FILE *pgpin, *pgperr, *fpout, *fptmp;
1154 mutt_mktemp (tempfile);
1155 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1156 mutt_perror (tempfile);
1160 mutt_mktemp (pgperrfile);
1161 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1162 mutt_perror (pgperrfile);
1167 unlink (pgperrfile);
1169 mutt_mktemp (pgpinfile);
1170 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1171 mutt_perror (pgpinfile);
1179 convert_to_7bit (a);
1181 mutt_write_mime_header (a, fptmp);
1182 fputc ('\n', fptmp);
1183 mutt_write_mime_body (a, fptmp);
1186 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1187 fileno (fpout), fileno (pgperr),
1188 pgpinfile, keylist, sign)) == -1) {
1195 if (!pgp_use_gpg_agent ())
1196 fputs (PgpPass, pgpin);
1197 fputc ('\n', pgpin);
1201 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1209 empty = (fgetc (fpout) == EOF);
1214 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1216 fputs (buf, stdout);
1220 /* pause if there is any error output from PGP */
1222 mutt_any_key_to_continue (NULL);
1225 /* fatal error while trying to encrypt message */
1230 t = mutt_new_body ();
1231 t->type = TYPEMULTIPART;
1232 t->subtype = safe_strdup ("encrypted");
1233 t->encoding = ENC7BIT;
1235 t->disposition = DISPINLINE;
1237 mutt_generate_boundary (&t->parameter);
1238 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1240 t->parts = mutt_new_body ();
1241 t->parts->type = TYPEAPPLICATION;
1242 t->parts->subtype = safe_strdup ("pgp-encrypted");
1243 t->parts->encoding = ENC7BIT;
1245 t->parts->next = mutt_new_body ();
1246 t->parts->next->type = TYPEAPPLICATION;
1247 t->parts->next->subtype = safe_strdup ("octet-stream");
1248 t->parts->next->encoding = ENC7BIT;
1249 t->parts->next->filename = safe_strdup (tempfile);
1250 t->parts->next->use_disp = 1;
1251 t->parts->next->disposition = DISPINLINE;
1252 t->parts->next->unlink = 1; /* delete after sending the message */
1253 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1258 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1262 char pgpoutfile[_POSIX_PATH_MAX];
1263 char pgperrfile[_POSIX_PATH_MAX];
1264 char pgpinfile[_POSIX_PATH_MAX];
1266 char body_charset[STRING];
1268 const char *send_charset;
1270 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1280 if (a->type != TYPETEXT)
1282 if (ascii_strcasecmp (a->subtype, "plain"))
1285 if ((fp = fopen (a->filename, "r")) == NULL) {
1286 mutt_perror (a->filename);
1290 mutt_mktemp (pgpinfile);
1291 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1292 mutt_perror (pgpinfile);
1297 /* The following code is really correct: If noconv is set,
1298 * a's charset parameter contains the on-disk character set, and
1299 * we have to convert from that to utf-8. If noconv is not set,
1300 * we have to convert from $charset to utf-8.
1303 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1305 from_charset = body_charset;
1307 from_charset = Charset;
1309 if (!mutt_is_us_ascii (body_charset)) {
1313 if (flags & ENCRYPT)
1314 send_charset = "us-ascii";
1316 send_charset = "utf-8";
1318 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1319 while ((c = fgetconv (fc)) != EOF)
1322 fgetconv_close (&fc);
1325 send_charset = "us-ascii";
1326 mutt_copy_stream (fp, pgpin);
1331 mutt_mktemp (pgpoutfile);
1332 mutt_mktemp (pgperrfile);
1333 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1334 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1335 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1339 unlink (pgpoutfile);
1344 unlink (pgperrfile);
1346 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1347 -1, fileno (pgpout), fileno (pgperr),
1348 pgpinfile, keylist, flags)) == -1) {
1349 mutt_perror _("Can't invoke PGP");
1353 mutt_unlink (pgpinfile);
1354 unlink (pgpoutfile);
1358 if (pgp_use_gpg_agent ())
1361 fprintf (pgpin, "%s\n", PgpPass);
1364 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1367 mutt_unlink (pgpinfile);
1376 empty = (fgetc (pgpout) == EOF);
1381 while (fgets (buff, sizeof (buff), pgperr)) {
1383 fputs (buff, stdout);
1389 mutt_any_key_to_continue (NULL);
1392 unlink (pgpoutfile);
1396 b = mutt_new_body ();
1398 b->encoding = ENC7BIT;
1401 b->subtype = safe_strdup ("plain");
1403 mutt_set_parameter ("x-action",
1404 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1406 mutt_set_parameter ("charset", send_charset, &b->parameter);
1408 b->filename = safe_strdup (pgpoutfile);
1411 /* The following is intended to give a clue to some completely brain-dead
1412 * "mail environments" which are typically used by large corporations.
1415 b->d_filename = safe_strdup ("msg.pgp");
1420 b->disposition = DISPINLINE;
1426 if (!(flags & ENCRYPT))
1427 b->encoding = a->encoding;
1432 int pgp_send_menu (HEADER * msg, int *redraw)
1435 char input_signas[SHORT_STRING];
1437 char prompt[LONG_STRING];
1439 if (!(WithCrypto & APPLICATION_PGP))
1440 return msg->security;
1442 /* If autoinline and no crypto options set, then set inline. */
1443 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1444 && (msg->security & (SIGN | ENCRYPT))))
1445 msg->security |= INLINE;
1447 snprintf (prompt, sizeof (prompt),
1448 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1449 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1451 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1452 case 1: /* (e)ncrypt */
1453 msg->security |= ENCRYPT;
1454 msg->security &= ~SIGN;
1457 case 2: /* (s)ign */
1458 msg->security |= SIGN;
1459 msg->security &= ~ENCRYPT;
1462 case 3: /* sign (a)s */
1463 unset_option (OPTPGPCHECKTRUST);
1466 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1468 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1469 mutt_str_replace (&PgpSignAs, input_signas);
1472 msg->security |= SIGN;
1474 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1478 msg->security &= ~SIGN;
1482 *redraw = REDRAW_FULL;
1485 case 4: /* (b)oth */
1486 msg->security |= (ENCRYPT | SIGN);
1489 case 5: /* (i)nline */
1490 if ((msg->security & (ENCRYPT | SIGN)))
1491 msg->security ^= INLINE;
1493 msg->security &= ~INLINE;
1496 case 6: /* (f)orget it */
1497 case 7: /* (c)lear */
1502 if (msg->security) {
1503 if (!(msg->security & (ENCRYPT | SIGN)))
1506 msg->security |= APPLICATION_PGP;
1509 return (msg->security);
1513 #endif /* CRYPT_BACKEND_CLASSIC_PGP */