Remove unused code
[apps/madmutt.git] / lib-crypt / smime.c
index 1683a87..5906f68 100644 (file)
@@ -9,48 +9,26 @@
  * please see the file GPL in the top level source directory.
  */
 
-#if HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include <lib-lib/lib-lib.h>
 
-#include <lib-lib/mem.h>
-#include <lib-lib/str.h>
-#include <lib-lib/macros.h>
-#include <lib-lib/file.h>
-#include <lib-lib/debug.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 "mutt.h"
+#include "alias.h"
 #include "handler.h"
 #include "copy.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 "alias.h"
 
 #include "crypt.h"
+#include "smime.h"
 
 struct smime_command_context {
   const char *key;              /* %k */
@@ -134,15 +112,11 @@ int smime_valid_passphrase (void)
 
 /* This is almost identical to ppgp's invoking interface. */
 
-static const char *_mutt_fmt_smime_command (char *dest,
-                                            size_t destlen,
-                                            char op,
-                                            const char *src,
-                                            const char *prefix,
-                                            const char *ifstring,
-                                            const char *elsestring,
-                                            unsigned long data,
-                                            format_flag flags)
+static const char *
+_mutt_fmt_smime_command (char *dest, ssize_t destlen, char op,
+                         const char *src, const char *prefix,
+                         const char *ifstring, const char *elsestring,
+                         unsigned long data, format_flag flags)
 {
   char fmt[16];
   struct smime_command_context *cctx = (struct smime_command_context *) data;
@@ -256,13 +230,12 @@ static const char *_mutt_fmt_smime_command (char *dest,
 
 
 
-static void mutt_smime_command (char *d, size_t dlen,
+static void mutt_smime_command (char *d, ssize_t dlen,
                                 struct smime_command_context *cctx,
                                 const char *fmt)
 {
   mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_smime_command,
                      (unsigned long) cctx, 0);
-  debug_print (2, ("%s\n", d));
 }
 
 static pid_t smime_invoke (FILE ** smimein, FILE ** smimeout,
@@ -309,7 +282,7 @@ static pid_t smime_invoke (FILE ** smimein, FILE ** smimeout,
    return certificate file name.
 */
 
-static void smime_entry (char *s, size_t l, MUTTMENU * menu, int num)
+static void smime_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
 {
   smime_id *Table = (smime_id *) menu->data;
   smime_id this = Table[num];
@@ -349,14 +322,15 @@ static void smime_entry (char *s, size_t l, MUTTMENU * menu, int num)
 
 
 
-char *smime_ask_for_key (char *prompt, char *mailbox, short public)
+char *smime_ask_for_key (char *prompt, char *mailbox __attribute__((unused)),
+                         short public)
 {
   char *fname;
   smime_id *Table;
   long cert_num;                /* Will contain the number of certificates.
                                  * To be able to get it, the .index file will be read twice... */
   char index_file[_POSIX_PATH_MAX];
-  FILE *index;
+  FILE *idx;
   char buf[LONG_STRING];
   char fields[5][STRING];
   int numFields, hash_suffix, done, cur;        /* The current entry */
@@ -371,18 +345,18 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
   snprintf (index_file, sizeof (index_file), "%s/.index",
             public ? NONULL (SmimeCertificates) : NONULL (SmimeKeys));
 
-  index = fopen (index_file, "r");
-  if (index == NULL) {
+  idx = fopen (index_file, "r");
+  if (idx == NULL) {
     mutt_perror (index_file);
     return NULL;
   }
   /* Count Lines */
   cert_num = 0;
-  while (!feof (index)) {
-    if (fgets (buf, sizeof (buf), index))
+  while (!feof (idx)) {
+    if (fgets (buf, sizeof (buf), idx))
       cert_num++;
   }
-  fclose (index);
+  m_fclose(&idx);
 
   for (;;) {
     *qry = 0;
@@ -392,20 +366,20 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
               _("S/MIME certificates matching \"%s\"."), qry);
 
 
-    index = fopen (index_file, "r");
-    if (index == NULL) {
+    idx = fopen (index_file, "r");
+    if (idx == NULL) {
       mutt_perror (index_file);
       return NULL;
     }
     /* Read Entries */
     cur = 0;
     Table = p_new(smime_id, cert_num);
-    while (!feof (index)) {
+    while (!feof (idx)) {
       numFields =
-        fscanf (index, MUTT_FORMAT (STRING) " %x.%i " MUTT_FORMAT (STRING),
+        fscanf (idx, MUTT_FORMAT (STRING) " %x.%i " MUTT_FORMAT (STRING),
                 fields[0], &hash, &hash_suffix, fields[2]);
       if (public)
-        fscanf (index, MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) "\n",
+        fscanf (idx, MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) "\n",
                 fields[3], fields[4]);
 
       /* 0=email 1=name 2=nick 3=intermediate 4=trust */
@@ -425,7 +399,7 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
 
       cur++;
     }
-    fclose (index);
+    m_fclose(&idx);
 
     /* Make Helpstring */
     helpstr[0] = 0;
@@ -595,7 +569,7 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
         }
       }
 
-    safe_fclose (&fp);
+    m_fclose(&fp);
 
     if (ask) {
       if (public && *fields[4] == 'u')
@@ -635,15 +609,11 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
   return m_strdup(key);
 }
 
-
-
-
 /* 
    This sets the '*ToUse' variables for an upcoming decryption, where
    the reuquired key is different from SmimeDefaultKey.
 */
-
-void _smime_getkeys (char *mailbox)
+static void _smime_getkeys (char *mailbox)
 {
   char *k = NULL;
   char buf[STRING];
@@ -731,8 +701,8 @@ void smime_getkeys (ENVELOPE * env)
 char *smime_findKeys (address_t * to, address_t * cc, address_t * bcc)
 {
   char *keyID, *keylist = NULL;
-  size_t keylist_size = 0;
-  size_t keylist_used = 0;
+  ssize_t keylist_size = 0;
+  ssize_t keylist_used = 0;
   address_t *tmp = NULL, *addr = NULL;
   address_t **last = &tmp;
   address_t *p, *q;
@@ -763,7 +733,7 @@ char *smime_findKeys (address_t * to, address_t * cc, address_t * bcc)
   if (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];
@@ -808,16 +778,16 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
   int ret = -1, count = 0;
   pid_t thepid;
 
-  mutt_mktemp (tmpfname);
-  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL) {
+  fperr = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fperr) {
     mutt_perror (tmpfname);
     return 1;
   }
   mutt_unlink (tmpfname);
 
-  mutt_mktemp (tmpfname);
-  if ((fpout = safe_fopen (tmpfname, "w+")) == NULL) {
-    fclose (fperr);
+  fpout = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    m_fclose(&fperr);
     mutt_perror (tmpfname);
     return 1;
   }
@@ -828,8 +798,8 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
                               certificate, NULL, NULL, NULL, NULL, NULL,
                               SmimeGetCertEmailCommand)) == -1) {
     mutt_message (_("Error: unable to create OpenSSL subprocess!"));
-    fclose (fperr);
-    fclose (fpout);
+    m_fclose(&fperr);
+    m_fclose(&fpout);
     return 1;
   }
 
@@ -877,8 +847,8 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
   else if (copy)
     ret = 2;
 
-  fclose (fpout);
-  fclose (fperr);
+  m_fclose(&fpout);
+  m_fclose(&fperr);
 
   return ret;
 }
@@ -894,16 +864,16 @@ static char *smime_extract_certificate (char *infile)
   int empty;
 
 
-  mutt_mktemp (tmpfname);
-  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL) {
+  fperr = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fperr) {
     mutt_perror (tmpfname);
     return NULL;
   }
   mutt_unlink (tmpfname);
 
-  mutt_mktemp (pk7out);
-  if ((fpout = safe_fopen (pk7out, "w+")) == NULL) {
-    fclose (fperr);
+  fpout = m_tempfile (pk7out, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    m_fclose(&fperr);
     mutt_perror (pk7out);
     return NULL;
   }
@@ -917,8 +887,8 @@ static char *smime_extract_certificate (char *infile)
                               SmimePk7outCommand)) == -1) {
     mutt_any_key_to_continue (_
                               ("Error: unable to create OpenSSL subprocess!"));
-    fclose (fperr);
-    fclose (fpout);
+    m_fclose(&fperr);
+    m_fclose(&fpout);
     mutt_unlink (pk7out);
     return NULL;
   }
@@ -930,22 +900,22 @@ static char *smime_extract_certificate (char *infile)
   rewind (fpout);
   rewind (fperr);
   fflush (fperr);
+
   empty = (fgetc (fpout) == EOF);
+
+  m_fclose(&fpout);
+
   if (empty) {
     mutt_perror (pk7out);
     mutt_copy_stream (fperr, stdout);
-    fclose (fpout);
-    fclose (fperr);
+    m_fclose(&fperr);
     mutt_unlink (pk7out);
     return NULL;
-
   }
 
-
-  fclose (fpout);
-  mutt_mktemp (certfile);
-  if ((fpout = safe_fopen (certfile, "w+")) == NULL) {
-    fclose (fperr);
+  fpout = m_tempfile (certfile, sizeof(certfile), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    m_fclose(&fperr);
     mutt_unlink (pk7out);
     mutt_perror (certfile);
     return NULL;
@@ -959,8 +929,8 @@ static char *smime_extract_certificate (char *infile)
                               SmimeGetCertCommand)) == -1) {
     mutt_any_key_to_continue (_
                               ("Error: unable to create OpenSSL subprocess!"));
-    fclose (fperr);
-    fclose (fpout);
+    m_fclose(&fperr);
+    m_fclose(&fpout);
     mutt_unlink (pk7out);
     mutt_unlink (certfile);
     return NULL;
@@ -977,14 +947,14 @@ static char *smime_extract_certificate (char *infile)
   empty = (fgetc (fpout) == EOF);
   if (empty) {
     mutt_copy_stream (fperr, stdout);
-    fclose (fpout);
-    fclose (fperr);
+    m_fclose(&fpout);
+    m_fclose(&fperr);
     mutt_unlink (certfile);
     return NULL;
   }
 
-  fclose (fpout);
-  fclose (fperr);
+  m_fclose(&fpout);
+  m_fclose(&fperr);
 
   return m_strdup(certfile);
 }
@@ -997,18 +967,16 @@ static char *smime_extract_signer_certificate (char *infile)
   pid_t thepid;
   int empty;
 
-
-  mutt_mktemp (tmpfname);
-  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL) {
+  fperr = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fperr) {
     mutt_perror (tmpfname);
     return NULL;
   }
   mutt_unlink (tmpfname);
 
-
-  mutt_mktemp (certfile);
-  if ((fpout = safe_fopen (certfile, "w+")) == NULL) {
-    fclose (fperr);
+  m_tempfile (certfile, sizeof(certfile), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    m_fclose(&fperr);
     mutt_perror (certfile);
     return NULL;
   }
@@ -1021,8 +989,8 @@ static char *smime_extract_signer_certificate (char *infile)
                               SmimeGetSignerCertCommand)) == -1) {
     mutt_any_key_to_continue (_
                               ("Error: unable to create OpenSSL subprocess!"));
-    fclose (fperr);
-    fclose (fpout);
+    m_fclose(&fperr);
+    m_fclose(&fpout);
     mutt_unlink (pk7out);
     mutt_unlink (certfile);
     return NULL;
@@ -1035,49 +1003,45 @@ static char *smime_extract_signer_certificate (char *infile)
   rewind (fperr);
   fflush (fperr);
   empty = (fgetc (fpout) == EOF);
+  m_fclose(&fpout);
+
   if (empty) {
     mutt_endwin (NULL);
     mutt_copy_stream (fperr, stdout);
     mutt_any_key_to_continue (NULL);
-    fclose (fpout);
-    fclose (fperr);
+    m_fclose(&fperr);
     mutt_unlink (certfile);
     return NULL;
   }
 
-  fclose (fpout);
-  fclose (fperr);
+  m_fclose(&fperr);
 
   return m_strdup(certfile);
 }
 
-
-
-
 /* Add a certificate and update index file (externally). */
 
-void smime_invoke_import (char *infile, char *mailbox)
+void smime_invoke_import (char *infile, char *mailbox __attribute__ ((unused)))
 {
   char tmpfname[_POSIX_PATH_MAX], *certfile = NULL, buf[STRING];
   FILE *smimein = NULL, *fpout = NULL, *fperr = NULL;
   pid_t thepid = -1;
 
 mutt_mktemp (tmpfname);
-  if ((fperr = safe_fopen (tmpfname, "w+")) == NULL) {
fperr = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fperr) {
     mutt_perror (tmpfname);
     return;
   }
   mutt_unlink (tmpfname);
 
-  mutt_mktemp (tmpfname);
-  if ((fpout = safe_fopen (tmpfname, "w+")) == NULL) {
-    fclose (fperr);
+  fpout = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    m_fclose(&fperr);
     mutt_perror (tmpfname);
     return;
   }
   mutt_unlink (tmpfname);
 
-
   buf[0] = '\0';
   if (option (OPTASKCERTLABEL))
     mutt_get_field ("Label for certificate:", buf, sizeof (buf), 0);
@@ -1095,7 +1059,7 @@ void smime_invoke_import (char *infile, char *mailbox)
     }
     fputs (buf, smimein);
     fputc ('\n', smimein);
-    fclose (smimein);
+    m_fclose(&smimein);
 
     mutt_wait_filter (thepid);
 
@@ -1111,22 +1075,19 @@ void smime_invoke_import (char *infile, char *mailbox)
   mutt_copy_stream (fpout, stdout);
   mutt_copy_stream (fperr, stdout);
 
-  fclose (fpout);
-  fclose (fperr);
-
+  m_fclose(&fpout);
+  m_fclose(&fperr);
 }
 
-
-
 int smime_verify_sender (HEADER * h)
 {
   char *mbox = NULL, *certfile, tempfname[_POSIX_PATH_MAX];
   FILE *fpout;
   int retval = 1;
 
-  mutt_mktemp (tempfname);
-  if (!(fpout = safe_fopen (tempfname, "w"))) {
-    mutt_perror (tempfname);
+  fpout = m_tempfile (tempfname, sizeof(tempfname), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    mutt_perror (_("Can't create temporary file"));
     return 1;
   }
 
@@ -1138,7 +1099,7 @@ int smime_verify_sender (HEADER * h)
     mutt_copy_message (fpout, Context, h, 0, 0);
 
   fflush (fpout);
-  fclose (fpout);
+  m_fclose(&fpout);
 
   if (h->env->from) {
     h->env->from = mutt_expand_aliases (h->env->from);
@@ -1224,27 +1185,27 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
   int err = 0, empty;
   pid_t thepid;
 
-  mutt_mktemp (tempfile);
-  if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
-    mutt_perror (tempfile);
-    return (NULL);
+  fpout = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
+  if (!fpout) {
+    mutt_perror (_("Can't create temporary file"));
+    return NULL;
   }
 
-  mutt_mktemp (smimeerrfile);
-  if ((smimeerr = safe_fopen (smimeerrfile, "w+")) == NULL) {
+  smimeerr = m_tempfile (smimeerrfile, sizeof(smimeerrfile), NONULL(Tempdir), NULL);
+  if (!smimeerr) {
     mutt_perror (smimeerrfile);
-    fclose (fpout);
+    m_fclose(&fpout);
     mutt_unlink (tempfile);
     return NULL;
   }
   mutt_unlink (smimeerrfile);
 
-  mutt_mktemp (smimeinfile);
-  if ((fptmp = safe_fopen (smimeinfile, "w+")) == NULL) {
+  fptmp = m_tempfile (smimeinfile, sizeof(smimeinfile), NONULL(Tempdir), NULL);
+  if (!fptmp) {
     mutt_perror (smimeinfile);
     mutt_unlink (tempfile);
-    fclose (fpout);
-    fclose (smimeerr);
+    m_fclose(&fpout);
+    m_fclose(&smimeerr);
     return NULL;
   }
 
@@ -1267,19 +1228,19 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
   mutt_write_mime_header (a, fptmp);
   fputc ('\n', fptmp);
   mutt_write_mime_body (a, fptmp);
-  fclose (fptmp);
+  m_fclose(&fptmp);
 
   if ((thepid =
        smime_invoke_encrypt (&smimein, NULL, NULL, -1,
                              fileno (fpout), fileno (smimeerr),
                              smimeinfile, certfile)) == -1) {
-    fclose (smimeerr);
+    m_fclose(&smimeerr);
     mutt_unlink (smimeinfile);
     mutt_unlink (certfile);
     return (NULL);
   }
 
-  fclose (smimein);
+  m_fclose(&smimein);
 
   mutt_wait_filter (thepid);
   mutt_unlink (smimeinfile);
@@ -1288,7 +1249,7 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
   fflush (fpout);
   rewind (fpout);
   empty = (fgetc (fpout) == EOF);
-  fclose (fpout);
+  m_fclose(&fpout);
 
   fflush (smimeerr);
   rewind (smimeerr);
@@ -1296,7 +1257,7 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
     err = 1;
     fputs (buf, stdout);
   }
-  fclose (smimeerr);
+  m_fclose(&smimeerr);
 
   /* pause if there is any error output from SMIME */
   if (err)
@@ -1311,11 +1272,11 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
     return (NULL);
   }
 
-  t = mutt_new_body ();
+  t = body_new();
   t->type = TYPEAPPLICATION;
   t->subtype = m_strdup("x-pkcs7-mime");
-  mutt_set_parameter ("name", "smime.p7m", &t->parameter);
-  mutt_set_parameter ("smime-type", "enveloped-data", &t->parameter);
+  parameter_setval(&t->parameter, "name", "smime.p7m");
+  parameter_setval(&t->parameter, "smime-type", "enveloped-data");
   t->encoding = ENCBASE64;      /* The output of OpenSSL SHOULD be binary */
   t->use_disp = 1;
   t->disposition = DISPATTACH;
@@ -1349,16 +1310,16 @@ BODY *smime_sign_message (BODY * a)
 
   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
 
-  mutt_mktemp (filetosign);
-  if ((sfp = safe_fopen (filetosign, "w+")) == NULL) {
+  sfp = m_tempfile (filetosign, sizeof(filetosign), NONULL(Tempdir), NULL);
+  if (!sfp) {
     mutt_perror (filetosign);
     return NULL;
   }
 
-  mutt_mktemp (signedfile);
-  if ((smimeout = safe_fopen (signedfile, "w+")) == NULL) {
+  smimeout = m_tempfile (signedfile, sizeof(signedfile), NONULL(Tempdir), NULL);
+  if (!smimeout) {
     mutt_perror (signedfile);
-    fclose (sfp);
+    m_fclose(&sfp);
     mutt_unlink (filetosign);
     return NULL;
   }
@@ -1366,7 +1327,7 @@ BODY *smime_sign_message (BODY * a)
   mutt_write_mime_header (a, sfp);
   fputc ('\n', sfp);
   mutt_write_mime_body (a, sfp);
-  fclose (sfp);
+  m_fclose(&sfp);
 
 
 
@@ -1386,14 +1347,14 @@ BODY *smime_sign_message (BODY * a)
                                    filetosign)) == -1) {
     mutt_perror (_("Can't open OpenSSL subprocess!"));
 
-    fclose (smimeout);
+    m_fclose(&smimeout);
     mutt_unlink (signedfile);
     mutt_unlink (filetosign);
     return NULL;
   }
   fputs (SmimePass, smimein);
   fputc ('\n', smimein);
-  fclose (smimein);
+  m_fclose(&smimein);
 
 
   mutt_wait_filter (thepid);
@@ -1406,13 +1367,13 @@ BODY *smime_sign_message (BODY * a)
     err = 1;
     fputs (buffer, stdout);
   }
-  fclose (smimeerr);
+  m_fclose(&smimeerr);
 
 
   fflush (smimeout);
   rewind (smimeout);
   empty = (fgetc (smimeout) == EOF);
-  fclose (smimeout);
+  m_fclose(&smimeout);
 
   mutt_unlink (filetosign);
 
@@ -1427,23 +1388,23 @@ BODY *smime_sign_message (BODY * a)
     return (NULL);              /* fatal error while signing */
   }
 
-  t = mutt_new_body ();
+  t = body_new();
   t->type = TYPEMULTIPART;
   t->subtype = m_strdup("signed");
   t->encoding = ENC7BIT;
   t->use_disp = 0;
   t->disposition = DISPINLINE;
 
-  mutt_generate_boundary (&t->parameter);
+  parameter_set_boundary(&t->parameter);
   /* check if this can be extracted from private key somehow.... */
-  mutt_set_parameter ("micalg", "sha1", &t->parameter);
-  mutt_set_parameter ("protocol", "application/x-pkcs7-signature",
-                      &t->parameter);
+  parameter_setval(&t->parameter, "micalg", "sha1");
+  parameter_setval(&t->parameter, "protocol",
+                   "application/x-pkcs7-signature");
 
   t->parts = a;
   a = t;
 
-  t->parts->next = mutt_new_body ();
+  t->parts->next = body_new();
   t = t->parts->next;
   t->type = TYPEAPPLICATION;
   t->subtype = m_strdup("x-pkcs7-signature");
@@ -1459,19 +1420,11 @@ BODY *smime_sign_message (BODY * a)
 }
 
 
-
-
-
-
 /*
  *    Handling S/MIME - bodies.
  */
 
 
-
-
-
-
 static
 pid_t smime_invoke_verify (FILE ** smimein, FILE ** smimeout,
                            FILE ** smimeerr, int smimeinfd, int smimeoutfd,
@@ -1505,7 +1458,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
   int badsig = -1;
 
   long tmpoffset = 0;
-  size_t tmplength = 0;
+  ssize_t tmplength = 0;
   int origType = sigbdy->type;
   char *savePrefix = NULL;
 
@@ -1534,7 +1487,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
 
   sigbdy->length = ftello (s->fpout);
   sigbdy->offset = 0;
-  fclose (s->fpout);
+  m_fclose(&s->fpout);
 
   /* restore final destination and substitute the tempfile for input */
   s->fpout = fp;
@@ -1546,9 +1499,8 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
 
   sigbdy->type = origType;
 
-
-  mutt_mktemp (smimeerrfile);
-  if (!(smimeerr = safe_fopen (smimeerrfile, "w+"))) {
+  smimeerr = m_tempfile(smimeerrfile, sizeof(smimeerrfile), NONULL(Tempdir), NULL);
+  if (!smimeerr) {
     mutt_perror (smimeerrfile);
     mutt_unlink (signedfile);
     return -1;
@@ -1559,15 +1511,14 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
   if ((thepid = smime_invoke_verify (NULL, &smimeout, NULL,
                                      -1, -1, fileno (smimeerr),
                                      tempfile, signedfile, 0)) != -1) {
-    fflush (smimeout);
-    fclose (smimeout);
+    m_fclose(&smimeout);
 
     if (mutt_wait_filter (thepid))
       badsig = -1;
     else {
       char *line = NULL;
       int lineno = 0;
-      size_t linelen;
+      ssize_t linelen;
 
       fflush (smimeerr);
       rewind (smimeerr);
@@ -1583,7 +1534,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
   fflush (smimeerr);
   rewind (smimeerr);
   mutt_copy_stream (smimeerr, s->fpout);
-  fclose (smimeerr);
+  m_fclose(&smimeerr);
 
   state_attach_puts (_("[-- End of OpenSSL output --]\n\n"), s);
 
@@ -1594,7 +1545,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
   sigbdy->offset = tmpoffset;
 
   /* restore the original source stream */
-  fclose (s->fpin);
+  m_fclose(&s->fpin);
   s->fpin = fp;
 
 
@@ -1629,29 +1580,25 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
   if (!(type & APPLICATION_SMIME))
     return NULL;
 
-  mutt_mktemp (outfile);
-  if ((smimeout = safe_fopen (outfile, "w+")) == NULL) {
+  smimeout = m_tempfile (outfile, sizeof(outfile), NONULL(Tempdir), NULL);
+  if (!smimeout) {
     mutt_perror (outfile);
     return NULL;
   }
 
-  mutt_mktemp (errfile);
-  if ((smimeerr = safe_fopen (errfile, "w+")) == NULL) {
+  smimeerr = m_tempfile(errfile, sizeof(errfile), NONULL(Tempdir), NULL);
+  if (!smimeerr) {
     mutt_perror (errfile);
-    fclose (smimeout);
-    smimeout = NULL;
+    m_fclose(&smimeout);
     return NULL;
   }
   mutt_unlink (errfile);
 
-
-  mutt_mktemp (tmpfname);
-  if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
+  tmpfp = m_tempfile (tmpfname, sizeof(tmpfname), NONULL(Tempdir), NULL);
+  if (!tmpfp) {
     mutt_perror (tmpfname);
-    fclose (smimeout);
-    smimeout = NULL;
-    fclose (smimeerr);
-    smimeerr = NULL;
+    m_fclose(&smimeout);
+    m_fclose(&smimeerr);
     return NULL;
   }
 
@@ -1659,16 +1606,13 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
   last_pos = m->offset;
 
   mutt_copy_bytes (s->fpin, tmpfp, m->length);
-
-  fflush (tmpfp);
-  fclose (tmpfp);
+  m_fclose(&tmpfp);
 
   if ((type & ENCRYPT) &&
       (thepid = smime_invoke_decrypt (&smimein, NULL, NULL, -1,
                                       fileno (smimeout), fileno (smimeerr),
                                       tmpfname)) == -1) {
-    fclose (smimeout);
-    smimeout = NULL;
+    m_fclose(&smimeout);
     mutt_unlink (tmpfname);
     if (s->flags & M_DISPLAY)
       state_attach_puts (_("[-- Error: unable to create OpenSSL subprocess! --]\n"), s);
@@ -1679,8 +1623,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
                                           fileno (smimeout),
                                           fileno (smimeerr), NULL, tmpfname,
                                           SIGNOPAQUE)) == -1) {
-    fclose (smimeout);
-    smimeout = NULL;
+    m_fclose(&smimeout);
     mutt_unlink (tmpfname);
     if (s->flags & M_DISPLAY)
       state_attach_puts (_("[-- Error: unable to create OpenSSL subprocess! --]\n"), s);
@@ -1695,7 +1638,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     fputc ('\n', smimein);
   }
 
-  fclose (smimein);
+  m_fclose(&smimein);
 
   mutt_wait_filter (thepid);
   mutt_unlink (tmpfname);
@@ -1727,11 +1670,10 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     if (outFile)
       fpout = outFile;
     else {
-      mutt_mktemp (tmptmpfname);
-      if ((fpout = safe_fopen (tmptmpfname, "w+")) == NULL) {
+      fpout = m_tempfile (tmptmpfname, sizeof(tmptmpfname), NONULL(Tempdir), NULL);
+      if (!fpout) {
         mutt_perror (tmptmpfname);
-        fclose (smimeout);
-        smimeout = NULL;
+        m_fclose(&smimeout);
         return NULL;
       }
     }
@@ -1761,12 +1703,11 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
       }
 
     }
-    fclose (smimeout);
-    smimeout = NULL;
+    m_fclose(&smimeout);
     mutt_unlink (outfile);
 
     if (!outFile) {
-      fclose (fpout);
+      m_fclose(&fpout);
       mutt_unlink (tmptmpfname);
     }
     fpout = NULL;
@@ -1782,7 +1723,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
   if (type & SIGNOPAQUE) {
     char *line = NULL;
     int lineno = 0;
-    size_t linelen;
+    ssize_t linelen;
 
     rewind (smimeerr);
 
@@ -1795,7 +1736,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     m->goodsig = p->goodsig;
     m->badsig = p->badsig;
   }
-  fclose (smimeerr);
+  m_fclose(&smimeerr);
 
   return (p);
 }
@@ -1811,7 +1752,7 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   char tempfile[_POSIX_PATH_MAX];
   STATE s;
   long tmpoffset = b->offset;
-  size_t tmplength = b->length;
+  ssize_t tmplength = b->length;
   int origType = b->type;
   FILE *tmpfp = NULL;
   int rv = 0;
@@ -1826,9 +1767,9 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   s.fpin = fpin;
   fseeko (s.fpin, b->offset, 0);
 
-  mutt_mktemp (tempfile);
-  if ((tmpfp = safe_fopen (tempfile, "w+")) == NULL) {
-    mutt_perror (tempfile);
+  tmpfp = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
+  if (!tmpfp) {
+    mutt_perror (_("Can't create temporary file"));
     return (-1);
   }
 
@@ -1842,9 +1783,9 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   s.fpin = tmpfp;
   s.fpout = 0;
 
-  mutt_mktemp (tempfile);
-  if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
-    mutt_perror (tempfile);
+  *fpout = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
+  if (!*fpout) {
+    mutt_perror (_("Can't create temporary file"));
     rv = -1;
     goto bail;
   }
@@ -1863,7 +1804,7 @@ bail:
   b->length = tmplength;
   b->offset = tmpoffset;
 
-  safe_fclose (&tmpfp);
+  m_fclose(&tmpfp);
   if (*fpout)
     rewind (*fpout);
   return (rv);
@@ -1971,10 +1912,6 @@ int smime_send_menu (HEADER * msg, int *redraw)
       /* probably need a different passphrase */
       crypt_smime_void_passphrase ();
     }
-#if 0
-    else
-      msg->security &= ~SIGN;
-#endif
 
     *redraw = REDRAW_FULL;
     break;