Rocco Rutte:
[apps/madmutt.git] / smime.c
diff --git a/smime.c b/smime.c
index abd65df..102732f 100644 (file)
--- a/smime.c
+++ b/smime.c
 #include "mime.h"
 #include "copy.h"
 
+#include "lib/mem.h"
+#include "lib/intl.h"
+#include "lib/str.h"
+#include "lib/debug.h"
+
 #include <sys/wait.h>
 #include <string.h>
 #include <stdlib.h>
@@ -245,12 +250,9 @@ static void mutt_smime_command (char *d, size_t dlen,
 {
   mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_smime_command,
                      (unsigned long) cctx, 0);
-  dprint (2, (debugfile, "mutt_smime_command: %s\n", d));
+  debug_print (2, ("%s\n", d));
 }
 
-
-
-
 static pid_t smime_invoke (FILE ** smimein, FILE ** smimeout,
                            FILE ** smimeerr, int smimeinfd, int smimeoutfd,
                            int smimeerrfd, const char *fname,
@@ -399,7 +401,7 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
         continue;
 
       /* Check if query matches this certificate */
-      if (!mutt_stristr (fields[0], qry) && !mutt_stristr (fields[2], qry))
+      if (!str_isstr (fields[0], qry) && !str_isstr (fields[2], qry))
         continue;
 
       Table[cur].hash = hash;
@@ -507,7 +509,7 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
     }
 
     while (fgets (buf, sizeof (buf) - 1, fp) != NULL)
-      if (mailbox && !(mutt_strncasecmp (mailbox, buf, addr_len))) {
+      if (mailbox && !(safe_strncasecmp (mailbox, buf, addr_len))) {
         numFields = sscanf (buf,
                             MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) " "
                             MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) " "
@@ -569,13 +571,13 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
 
         /* query = label: return certificate. */
         if (numFields >= 3 &&
-            !(mutt_strncasecmp (query, fields[2], query_len))) {
+            !(safe_strncasecmp (query, fields[2], query_len))) {
           ask = 0;
           strfcpy (key, fields[1], sizeof (key));
         }
         /* query = certificate: return intermediate certificate. */
         else if (numFields >= 4 &&
-                 !(mutt_strncasecmp (query, fields[1], query_len))) {
+                 !(safe_strncasecmp (query, fields[1], query_len))) {
           ask = 0;
           strfcpy (key, fields[3], sizeof (key));
         }
@@ -644,7 +646,7 @@ void _smime_getkeys (char *mailbox)
   if (k) {
     /* the key used last time. */
     if (*SmimeKeyToUse &&
-        !mutt_strcasecmp (k, SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1)) {
+        !safe_strcasecmp (k, SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1)) {
       FREE (&k);
       return;
     }
@@ -657,7 +659,7 @@ void _smime_getkeys (char *mailbox)
     snprintf (SmimeCertToUse, sizeof (SmimeCertToUse), "%s/%s",
               NONULL (SmimeCertificates), k);
 
-    if (mutt_strcasecmp (k, SmimeDefaultKey))
+    if (safe_strcasecmp (k, SmimeDefaultKey))
       smime_void_passphrase ();
 
     FREE (&k);
@@ -665,7 +667,7 @@ void _smime_getkeys (char *mailbox)
   }
 
   if (*SmimeKeyToUse) {
-    if (!mutt_strcasecmp (SmimeDefaultKey,
+    if (!safe_strcasecmp (SmimeDefaultKey,
                           SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1))
       return;
 
@@ -829,7 +831,7 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
   while ((fgets (email, sizeof (email), fpout))) {
     *(email + mutt_strlen (email) - 1) = '\0';
-    if (mutt_strncasecmp (email, mailbox, mutt_strlen (mailbox)) == 0)
+    if (safe_strncasecmp (email, mailbox, mutt_strlen (mailbox)) == 0)
       ret = 1;
 
     ret = ret < 0 ? 0 : ret;
@@ -1371,7 +1373,7 @@ BODY *smime_sign_message (BODY * a)
   if ((thepid = smime_invoke_sign (&smimein, NULL, &smimeerr,
                                    -1, fileno (smimeout), -1,
                                    filetosign)) == -1) {
-    mutt_perror _("Can't open OpenSSL subprocess!");
+    mutt_perror (_("Can't open OpenSSL subprocess!"));
 
     fclose (smimeout);
     mutt_unlink (signedfile);
@@ -1560,7 +1562,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
       rewind (smimeerr);
 
       line = mutt_read_line (line, &linelen, smimeerr, &lineno);
-      if (linelen && !mutt_strcasecmp (line, "verification successful"))
+      if (linelen && !safe_strcasecmp (line, "verification successful"))
         badsig = 0;
 
       FREE (&line);
@@ -1657,9 +1659,8 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     fclose (smimeout);
     smimeout = NULL;
     mutt_unlink (tmpfname);
-    state_attach_puts (_
-                       ("[-- Error: unable to create OpenSSL subprocess! --]\n"),
-                       s);
+    if (s->flags & M_DISPLAY)
+      state_attach_puts (_("[-- Error: unable to create OpenSSL subprocess! --]\n"), s);
     return NULL;
   }
   else if ((type & SIGNOPAQUE) &&
@@ -1670,9 +1671,8 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     fclose (smimeout);
     smimeout = NULL;
     mutt_unlink (tmpfname);
-    state_attach_puts (_
-                       ("[-- Error: unable to create OpenSSL subprocess! --]\n"),
-                       s);
+    if (s->flags & M_DISPLAY)
+      state_attach_puts (_("[-- Error: unable to create OpenSSL subprocess! --]\n"), s);
     return NULL;
   }
 
@@ -1776,7 +1776,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     rewind (smimeerr);
 
     line = mutt_read_line (line, &linelen, smimeerr, &lineno);
-    if (linelen && !mutt_strcasecmp (line, "verification successful"))
+    if (linelen && !safe_strcasecmp (line, "verification successful"))
       m->goodsig = 1;
     FREE (&line);
   }
@@ -1803,6 +1803,7 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   size_t tmplength = b->length;
   int origType = b->type;
   FILE *tmpfp = NULL;
+  int rv = 0;
 
   if (!mutt_is_application_smime (b))
     return -1;
@@ -1833,21 +1834,28 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   mutt_mktemp (tempfile);
   if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
     mutt_perror (tempfile);
-    return (-1);
+    rv = -1;
+    goto bail;
   }
   mutt_unlink (tempfile);
 
-  *cur = smime_handle_entity (b, &s, *fpout);
+  if (!(*cur = smime_handle_entity (b, &s, *fpout))) {
+    rv = -1;
+    goto bail;
+  }
+
   (*cur)->goodsig = b->goodsig;
-  (*cur)->badsig = b->badsig;
+  (*cur)->badsig  = b->badsig;
+
+bail:
   b->type = origType;
   b->length = tmplength;
   b->offset = tmpoffset;
-  fclose (tmpfp);
-
-  rewind (*fpout);
-  return (0);
 
+  safe_fclose (&tmpfp);
+  if (*fpout)
+    rewind (*fpout);
+  return (rv);
 }
 
 
@@ -1880,19 +1888,19 @@ int smime_send_menu (HEADER * msg, int *redraw)
                                  " 4: RC2-64, 5: RC2-128, or (f)orget it? "),
                                _("12345f"))) {
     case 1:
-      mutt_str_replace (&SmimeCryptAlg, "des");
+      str_replace (&SmimeCryptAlg, "des");
       break;
     case 2:
-      mutt_str_replace (&SmimeCryptAlg, "des3");
+      str_replace (&SmimeCryptAlg, "des3");
       break;
     case 3:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-40");
+      str_replace (&SmimeCryptAlg, "rc2-40");
       break;
     case 4:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-64");
+      str_replace (&SmimeCryptAlg, "rc2-64");
       break;
     case 5:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-128");
+      str_replace (&SmimeCryptAlg, "rc2-128");
       break;
     case 6:                    /* forget it */
       break;
@@ -1914,7 +1922,7 @@ int smime_send_menu (HEADER * msg, int *redraw)
 
     if ((p = smime_ask_for_key (_("Sign as: "), NULL, 0))) {
       p[mutt_strlen (p) - 1] = '\0';
-      mutt_str_replace (&SmimeDefaultKey, p);
+      str_replace (&SmimeDefaultKey, p);
 
       msg->security |= SIGN;