add support for import key from gpgme as well !
[apps/madmutt.git] / lib-crypt / pgp.c
index 408bf33..8db2a5d 100644 (file)
  * a message.
  */
 
-#if HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include <lib-lib/lib-lib.h>
 
-#include <lib-lib/mem.h>
-#include <lib-lib/ascii.h>
-#include <lib-lib/str.h>
-#include <lib-lib/macros.h>
-#include <lib-lib/file.h>
+#ifdef HAVE_SYS_RESOURCE_H
+# include <sys/resource.h>
+#endif
 
 #include <lib-mime/mime.h>
+#include <lib-sys/unix.h>
 
 #include <lib-ui/curses.h>
 #include <lib-ui/enter.h>
 #include <lib-ui/menu.h>
+#include <lib-mx/mx.h>
 
-#include "mutt.h"
 #include "handler.h"
 #include "pgp.h"
 #include "copy.h"
 #include "attach.h"
 
-
-#include <sys/wait.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <ctype.h>
-
-#ifdef HAVE_LOCALE_H
-#include <locale.h>
-#endif
-
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
-
-#ifdef HAVE_SYS_RESOURCE_H
-# include <sys/resource.h>
-#endif
-
 #include "crypt.h"
 
 
@@ -154,7 +130,7 @@ static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
         rv = 0;
       }
 
-      if (strncmp (line, "[GNUPG:] ", 9) == 0)
+      if (m_strncmp (line, "[GNUPG:] ", 9) == 0)
         continue;
       fputs (line, fpout);
       fputc ('\n', fpout);
@@ -191,7 +167,7 @@ static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
 
   rewind (fpin);
 
-  fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
+  fc = fgetconv_open (fpin, charset, MCharset.charset, M_ICONV_HOOK_FROM);
 
   for (complete = 1, armor_header = 1;
        fgetconvs (buf, sizeof (buf), fc) != NULL;
@@ -290,8 +266,8 @@ int pgp_application_pgp_handler (BODY * m, STATE * s)
       have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
 
       /* Copy PGP material to temporary file */
-      mutt_mktemp (tmpfname);
-      if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
+      tmpfp = m_tempfile(tmpfname, sizeof(tmpfname), NONULL(MCore.tmpdir), NULL);
+      if (tmpfp == NULL) {
         mutt_perror (tmpfname);
         return (-1);
       }
@@ -319,16 +295,16 @@ int pgp_application_pgp_handler (BODY * m, STATE * s)
 
       /* Invoke PGP if needed */
       if (!clearsign || (s->flags & M_VERIFY)) {
-        mutt_mktemp (outfile);
-        if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
-          mutt_perror (tmpfname);
+        pgpout = m_tempfile(outfile, sizeof(outfile), NONULL(MCore.tmpdir), NULL);
+        if (pgpout == NULL) {
+          mutt_perror (outfile);
           return (-1);
         }
 
         if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
                                          fileno (pgpout), -1, tmpfname,
                                          needpass)) == -1) {
-          safe_fclose (&pgpout);
+          m_fclose(&pgpout);
           maybe_goodsig = 0;
           pgpin = NULL;
           pgperr = NULL;
@@ -346,14 +322,14 @@ int pgp_application_pgp_handler (BODY * m, STATE * s)
             fprintf (pgpin, "%s\n", PgpPass);
           }
 
-          safe_fclose (&pgpin);
+          m_fclose(&pgpin);
 
           if (s->flags & M_DISPLAY) {
             crypt_current_time (s, "PGP");
             rc = pgp_copy_checksig (pgperr, s->fpout);
           }
 
-          safe_fclose (&pgperr);
+          m_fclose(&pgperr);
           rv = mutt_wait_filter (thepid);
 
           if (s->flags & M_DISPLAY) {
@@ -418,7 +394,7 @@ int pgp_application_pgp_handler (BODY * m, STATE * s)
 
         rewind (pgpout);
         state_set_prefix (s);
-        fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
+        fc = fgetconv_open (pgpout, "utf-8", MCharset.charset, 0);
         while ((c = fgetconv (fc)) != EOF)
           state_prefix_putc (c, s);
         fgetconv_close (&fc);
@@ -454,11 +430,11 @@ out:
   m->goodsig = (maybe_goodsig && have_any_sigs);
 
   if (tmpfp) {
-    safe_fclose (&tmpfp);
+    m_fclose(&tmpfp);
     mutt_unlink (tmpfname);
   }
   if (pgpout) {
-    safe_fclose (&pgpout);
+    m_fclose(&pgpout);
     mutt_unlink (outfile);
   }
 
@@ -478,6 +454,7 @@ static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
   char tempfile[_POSIX_PATH_MAX];
   char buf[HUGE_STRING];
   FILE *tfp;
+  int tempfd;
 
   short sgn = 0;
   short enc = 0;
@@ -489,14 +466,14 @@ static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
   if (tagged_only && !b->tagged)
     return 0;
 
-  mutt_mktemp (tempfile);
-  if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
+  tempfd = m_tempfd(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
+  if (mutt_decode_save_attachment (fp, b, tempfd, 0) != 0) {
     unlink (tempfile);
     return 0;
   }
 
-  if ((tfp = fopen (tempfile, "r")) == NULL) {
-    unlink (tempfile);
+  if (!(tfp = fopen(tempfile, "r"))) {
+    unlink(tempfile);
     return 0;
   }
 
@@ -510,7 +487,7 @@ static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
         key = 1;
     }
   }
-  safe_fclose (&tfp);
+  m_fclose(&tfp);
   unlink (tempfile);
 
   if (!enc && !sgn && !key)
@@ -569,10 +546,10 @@ int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
 
   fseeko (s->fpin, sigbdy->offset, 0);
   mutt_copy_bytes (s->fpin, fp, sigbdy->length);
-  fclose (fp);
+  m_fclose(&fp);
 
-  mutt_mktemp (pgperrfile);
-  if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
+  pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(MCore.tmpdir), NULL);
+  if (pgperr == NULL) {
     mutt_perror (pgperrfile);
     unlink (sigfile);
     return -1;
@@ -587,7 +564,7 @@ int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
       badsig = 0;
 
 
-    safe_fclose (&pgpout);
+    m_fclose(&pgpout);
     fflush (pgperr);
     rewind (pgperr);
 
@@ -598,7 +575,7 @@ int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
       badsig = -1;
   }
 
-  safe_fclose (&pgperr);
+  m_fclose(&pgperr);
 
   state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
 
@@ -611,70 +588,15 @@ int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
 
 /* Extract pgp public keys from messages or attachments */
 
-void pgp_extract_keys_from_messages (HEADER * h)
-{
-  int i;
-  char tempfname[_POSIX_PATH_MAX];
-  FILE *fpout;
-
-  if (h) {
-    mutt_parse_mime_message (Context, h);
-    if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
-      return;
-  }
-
-  mutt_mktemp (tempfname);
-  if (!(fpout = safe_fopen (tempfname, "w"))) {
-    mutt_perror (tempfname);
-    return;
-  }
-
-  set_option (OPTDONTHANDLEPGPKEYS);
-
-  if (!h) {
-    for (i = 0; i < Context->vcount; i++) {
-      if (Context->hdrs[Context->v2r[i]]->tagged) {
-        mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
-        if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
-            && !pgp_valid_passphrase ()) {
-          fclose (fpout);
-          goto bailout;
-        }
-        mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
-                           M_CM_DECODE | M_CM_CHARCONV, 0);
-      }
-    }
-  }
-  else {
-    mutt_parse_mime_message (Context, h);
-    if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
-      fclose (fpout);
-      goto bailout;
-    }
-    mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
-  }
-
-  fclose (fpout);
-  mutt_endwin (NULL);
-  pgp_invoke_import (tempfname);
-  mutt_any_key_to_continue (NULL);
-
-bailout:
-
-  mutt_unlink (tempfname);
-  unset_option (OPTDONTHANDLEPGPKEYS);
-
-}
-
 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
 {
   STATE s;
   FILE *tempfp;
   char tempfname[_POSIX_PATH_MAX];
 
-  mutt_mktemp (tempfname);
-  if (!(tempfp = safe_fopen (tempfname, "w"))) {
-    mutt_perror (tempfname);
+  tempfp = m_tempfile(tempfname, sizeof(tempfname), NONULL(MCore.tmpdir), NULL);
+  if (tempfp == NULL) {
+    mutt_perror (_("Can't create temporary file"));
     return;
   }
 
@@ -685,10 +607,12 @@ static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
 
   mutt_body_handler (top, &s);
 
-  fclose (tempfp);
+  m_fclose(&tempfp);
 
-  pgp_invoke_import (tempfname);
-  mutt_any_key_to_continue (NULL);
+  if (CRYPT_MOD_CALL_CHECK (PGP, pgp_invoke_import)) {
+    (CRYPT_MOD_CALL (PGP, pgp_invoke_import)) (fname);
+    mutt_any_key_to_continue (NULL);
+  }
 
   mutt_unlink (tempfname);
 }
@@ -727,17 +651,17 @@ BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
   pid_t thepid;
   int rv;
 
-  mutt_mktemp (pgperrfile);
-  if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
+  pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(MCore.tmpdir), NULL);
+  if (!pgperr) {
     mutt_perror (pgperrfile);
     return NULL;
   }
   unlink (pgperrfile);
 
-  mutt_mktemp (pgptmpfile);
-  if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
+  pgptmp = m_tempfile(pgptmpfile, sizeof(pgptmpfile), NONULL(MCore.tmpdir), NULL);
+  if (!pgptmp) {
     mutt_perror (pgptmpfile);
-    fclose (pgperr);
+    m_fclose(&pgperr);
     return NULL;
   }
 
@@ -747,11 +671,11 @@ BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
 
   fseeko (s->fpin, a->offset, 0);
   mutt_copy_bytes (s->fpin, pgptmp, a->length);
-  fclose (pgptmp);
+  m_fclose(&pgptmp);
 
   if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
                                     fileno (pgperr), pgptmpfile)) == -1) {
-    fclose (pgperr);
+    m_fclose(&pgperr);
     unlink (pgptmpfile);
     if (s->flags & M_DISPLAY)
       state_attach_puts (_
@@ -766,7 +690,7 @@ BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
   if (!pgp_use_gpg_agent ())
     fputs (PgpPass, pgpin);
   fputc ('\n', pgpin);
-  fclose (pgpin);
+  m_fclose(&pgpin);
 
   /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
    * read_mime_header has a hard time parsing the message.
@@ -774,11 +698,11 @@ BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
   while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
     len = m_strlen(buf);
     if (len > 1 && buf[len - 2] == '\r')
-      strcpy (buf + len - 2, "\n");     /* __STRCPY_CHECKED__ */
+      m_strcpy(buf + len - 2, len - 2,  "\n");
     fputs (buf, fpout);
   }
 
-  fclose (pgpout);
+  m_fclose(&pgpout);
   rv = mutt_wait_filter (thepid);
   mutt_unlink (pgptmpfile);
 
@@ -791,7 +715,7 @@ BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
       p->goodsig = 0;
     state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
   }
-  fclose (pgperr);
+  m_fclose(&pgperr);
 
   fflush (fpout);
   rewind (fpout);
@@ -835,9 +759,9 @@ int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
 
   p_clear(&s, 1);
   s.fpin = fpin;
-  mutt_mktemp (tempfile);
-  if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
-    mutt_perror (tempfile);
+  *fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
+  if (*fpout == NULL) {
+    mutt_perror (_("Can't create temporary file"));
     return (-1);
   }
   unlink (tempfile);
@@ -876,8 +800,8 @@ int pgp_encrypted_handler (BODY * a, STATE * s)
    */
   a = a->next;
 
-  mutt_mktemp (tempfile);
-  if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
+  fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
+  if (fpout == NULL) {
     if (s->flags & M_DISPLAY)
       state_attach_puts (_
                          ("[-- Error: could not create temporary file! --]\n"),
@@ -923,7 +847,7 @@ int pgp_encrypted_handler (BODY * a, STATE * s)
     rc = -1;
   }
 
-  fclose (fpout);
+  m_fclose(&fpout);
   mutt_unlink (tempfile);
 
   return (rc);
@@ -946,15 +870,15 @@ BODY *pgp_sign_message (BODY * a)
 
   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
 
-  mutt_mktemp (sigfile);
-  if ((fp = safe_fopen (sigfile, "w")) == NULL) {
+  fp = m_tempfile(sigfile, sizeof(sigfile), NONULL(MCore.tmpdir), NULL);
+  if (fp == NULL) {
     return (NULL);
   }
 
-  mutt_mktemp (signedfile);
-  if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
+  sfp = m_tempfile(signedfile, sizeof(signedfile), NONULL(MCore.tmpdir), NULL);
+  if (sfp == NULL) {
     mutt_perror (signedfile);
-    fclose (fp);
+    m_fclose(&fp);
     unlink (sigfile);
     return NULL;
   }
@@ -962,13 +886,13 @@ BODY *pgp_sign_message (BODY * a)
   mutt_write_mime_header (a, sfp);
   fputc ('\n', sfp);
   mutt_write_mime_body (a, sfp);
-  fclose (sfp);
+  m_fclose(&sfp);
 
   if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
                                  -1, -1, -1, signedfile)) == -1) {
     mutt_perror (_("Can't open PGP subprocess!"));
 
-    fclose (fp);
+    m_fclose(&fp);
     unlink (sigfile);
     unlink (signedfile);
     return NULL;
@@ -977,7 +901,7 @@ BODY *pgp_sign_message (BODY * a)
   if (!pgp_use_gpg_agent ())
     fputs (PgpPass, pgpin);
   fputc ('\n', pgpin);
-  fclose (pgpin);
+  m_fclose(&pgpin);
 
   /*
    * Read back the PGP signature.  Also, change MESSAGE=>SIGNATURE as
@@ -1003,11 +927,11 @@ BODY *pgp_sign_message (BODY * a)
   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
     empty = 1;
 
-  fclose (pgperr);
-  fclose (pgpout);
+  m_fclose(&pgperr);
+  m_fclose(&pgpout);
   unlink (signedfile);
 
-  if (fclose (fp) != 0) {
+  if (m_fclose(&fp) != 0) {
     mutt_perror ("fclose");
     unlink (sigfile);
     return (NULL);
@@ -1053,7 +977,7 @@ BODY *pgp_sign_message (BODY * a)
 static short is_numerical_keyid (const char *s)
 {
   /* or should we require the "0x"? */
-  if (strncmp (s, "0x", 2) == 0)
+  if (m_strncmp (s, "0x", 2) == 0)
     s += 2;
   if (m_strlen(s) % 8)
     return 0;
@@ -1069,7 +993,8 @@ static short is_numerical_keyid (const char *s)
  */
 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
 {
-  char *keyID, *keylist = NULL, *t;
+  char *keylist = NULL, *t;
+  const char *keyID;
   size_t keylist_size = 0;
   size_t keylist_used = 0;
   address_t *tmp = NULL, *addr = NULL;
@@ -1100,10 +1025,9 @@ char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
       last = &((*last)->next);
   }
 
-  if (fqdn)
-    rfc822_qualify (tmp, fqdn);
+  rfc822_qualify (tmp, fqdn);
 
-  tmp = mutt_remove_duplicates (tmp);
+  address_list_uniq(tmp);
 
   for (p = tmp; p; p = p->next) {
     char buf[LONG_STRING];
@@ -1118,7 +1042,7 @@ char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
                 p->mailbox);
       if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
         if (is_numerical_keyid (keyID)) {
-          if (strncmp (keyID, "0x", 2) == 0)
+          if (m_strncmp (keyID, "0x", 2) == 0)
             keyID += 2;
           goto bypass_selection;        /* you don't see this. */
         }
@@ -1126,8 +1050,7 @@ char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
         /* check for e-mail address */
         if ((t = strchr (keyID, '@')) &&
             (addr = rfc822_parse_adrlist (NULL, keyID))) {
-          if (fqdn)
-            rfc822_qualify (addr, fqdn);
+          rfc822_qualify (addr, fqdn);
           q = addr;
         }
         else
@@ -1165,7 +1088,7 @@ char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
   bypass_selection:
     keylist_size += m_strlen(keyID) + 4;
     p_realloc(&keylist, keylist_size);
-    sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
+    sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "",
              keyID);
     keylist_used = m_strlen(keylist);
 
@@ -1191,27 +1114,28 @@ BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
   int empty = 0;
   pid_t thepid;
 
-  mutt_mktemp (tempfile);
-  if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
-    mutt_perror (tempfile);
+  fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
+  if (fpout == NULL) {
+    mutt_perror (_("Can't create temporary file"));
     return (NULL);
   }
 
-  mutt_mktemp (pgperrfile);
-  if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
+  pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(MCore.tmpdir), NULL);
+  if (pgperr == NULL) {
     mutt_perror (pgperrfile);
+    m_fclose(&fpout);
     unlink (tempfile);
-    fclose (fpout);
     return NULL;
   }
   unlink (pgperrfile);
 
-  mutt_mktemp (pgpinfile);
-  if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
+  fptmp = m_tempfile(pgpinfile, sizeof(pgpinfile), NONULL(MCore.tmpdir), NULL);
+  if (fptmp == NULL) {
     mutt_perror (pgpinfile);
+    m_fclose(&fpout);
     unlink (tempfile);
-    fclose (fpout);
-    fclose (pgperr);
+    m_fclose(&pgperr);
+    unlink (pgperrfile);
     return NULL;
   }
 
@@ -1221,12 +1145,12 @@ BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
   mutt_write_mime_header (a, fptmp);
   fputc ('\n', fptmp);
   mutt_write_mime_body (a, fptmp);
-  fclose (fptmp);
+  m_fclose(&fptmp);
 
   if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
                                     fileno (fpout), fileno (pgperr),
                                     pgpinfile, keylist, sign)) == -1) {
-    fclose (pgperr);
+    m_fclose(&pgperr);
     unlink (pgpinfile);
     return (NULL);
   }
@@ -1236,7 +1160,7 @@ BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
       fputs (PgpPass, pgpin);
     fputc ('\n', pgpin);
   }
-  fclose (pgpin);
+  m_fclose(&pgpin);
 
   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
     empty = 1;
@@ -1247,7 +1171,7 @@ BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
   rewind (fpout);
   if (!empty)
     empty = (fgetc (fpout) == EOF);
-  fclose (fpout);
+  m_fclose(&fpout);
 
   fflush (pgperr);
   rewind (pgperr);
@@ -1255,7 +1179,7 @@ BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
     err = 1;
     fputs (buf, stdout);
   }
-  fclose (pgperr);
+  m_fclose(&pgperr);
 
   /* pause if there is any error output from PGP */
   if (err)
@@ -1329,10 +1253,10 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
     return NULL;
   }
 
-  mutt_mktemp (pgpinfile);
-  if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
+  pgpin = m_tempfile(pgpinfile, sizeof(pgpinfile), NONULL(MCore.tmpdir), NULL);
+  if (pgpin == NULL) {
     mutt_perror (pgpinfile);
-    fclose (fp);
+    m_fclose(&fp);
     return NULL;
   }
 
@@ -1346,7 +1270,7 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
   if (a->noconv)
     from_charset = body_charset;
   else
-    from_charset = Charset;
+    from_charset = MCharset.charset;
 
   if (!charset_is_us_ascii (body_charset)) {
     int c;
@@ -1367,19 +1291,19 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
     send_charset = "us-ascii";
     mutt_copy_stream (fp, pgpin);
   }
-  safe_fclose (&fp);
-  fclose (pgpin);
+  m_fclose(&fp);
+  m_fclose(&pgpin);
 
-  mutt_mktemp (pgpoutfile);
-  mutt_mktemp (pgperrfile);
-  if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
-      (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
+  pgpout = m_tempfile(pgpoutfile, sizeof(pgpoutfile), NONULL(MCore.tmpdir), NULL);
+  pgperr = m_tempfile(pgperrfile, sizeof(pgperrfile), NONULL(MCore.tmpdir), NULL);
+  if (pgpout == NULL || pgperr == NULL) {
     mutt_perror (pgpout ? pgperrfile : pgpoutfile);
+    m_fclose(&pgpin);
     unlink (pgpinfile);
-    if (pgpout) {
-      fclose (pgpout);
-      unlink (pgpoutfile);
-    }
+    m_fclose(&pgpout);
+    unlink (pgpoutfile);
+    m_fclose(&pgperr);
+    unlink(pgperrfile);
     return NULL;
   }
 
@@ -1390,8 +1314,8 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
                                         pgpinfile, keylist, flags)) == -1) {
     mutt_perror (_("Can't invoke PGP"));
 
-    fclose (pgpout);
-    fclose (pgperr);
+    m_fclose(&pgpout);
+    m_fclose(&pgperr);
     mutt_unlink (pgpinfile);
     unlink (pgpoutfile);
     return NULL;
@@ -1401,7 +1325,7 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
     *PgpPass = 0;
   if (flags & SIGN)
     fprintf (pgpin, "%s\n", PgpPass);
-  fclose (pgpin);
+  m_fclose(&pgpin);
 
   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
     empty = 1;
@@ -1416,7 +1340,7 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
 
   if (!empty)
     empty = (fgetc (pgpout) == EOF);
-  fclose (pgpout);
+  m_fclose(&pgpout);
 
   err = 0;
 
@@ -1425,7 +1349,7 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
     fputs (buff, stdout);
   }
 
-  fclose (pgperr);
+  m_fclose(&pgperr);
 
   if (err)
     mutt_any_key_to_continue (NULL);
@@ -1465,7 +1389,7 @@ BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
 int pgp_send_menu (HEADER * msg, int *redraw)
 {
   pgp_key_t p;
-  char input_signas[SHORT_STRING];
+  char input_signas[STRING];
 
   char prompt[LONG_STRING];
 
@@ -1503,11 +1427,6 @@ int pgp_send_menu (HEADER * msg, int *redraw)
 
       crypt_pgp_void_passphrase ();     /* probably need a different passphrase */
     }
-#if 0
-    else {
-      msg->security &= ~SIGN;
-    }
-#endif
 
     *redraw = REDRAW_FULL;
     break;