Rocco Rutte:
[apps/madmutt.git] / smime.c
diff --git a/smime.c b/smime.c
index 6f5c0f2..102732f 100644 (file)
--- a/smime.c
+++ b/smime.c
@@ -23,6 +23,7 @@
 #include "lib/mem.h"
 #include "lib/intl.h"
 #include "lib/str.h"
+#include "lib/debug.h"
 
 #include <sys/wait.h>
 #include <string.h>
@@ -249,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,
@@ -488,8 +486,8 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
   if (!mailbox && !query)
     return (NULL);
 
-  addr_len = mailbox ? safe_strlen (mailbox) : 0;
-  query_len = query ? safe_strlen (query) : 0;
+  addr_len = mailbox ? mutt_strlen (mailbox) : 0;
+  query_len = query ? mutt_strlen (query) : 0;
 
   *key = '\0';
 
@@ -648,7 +646,7 @@ void _smime_getkeys (char *mailbox)
   if (k) {
     /* the key used last time. */
     if (*SmimeKeyToUse &&
-        !safe_strcasecmp (k, SmimeKeyToUse + safe_strlen (SmimeKeys) + 1)) {
+        !safe_strcasecmp (k, SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1)) {
       FREE (&k);
       return;
     }
@@ -670,7 +668,7 @@ void _smime_getkeys (char *mailbox)
 
   if (*SmimeKeyToUse) {
     if (!safe_strcasecmp (SmimeDefaultKey,
-                          SmimeKeyToUse + safe_strlen (SmimeKeys) + 1))
+                          SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1))
       return;
 
     smime_void_passphrase ();
@@ -772,10 +770,10 @@ char *smime_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
       return NULL;
     }
 
-    keylist_size += safe_strlen (keyID) + 2;
+    keylist_size += mutt_strlen (keyID) + 2;
     safe_realloc (&keylist, keylist_size);
     sprintf (keylist + keylist_used, "%s\n", keyID);    /* __SPRINTF_CHECKED__ */
-    keylist_used = safe_strlen (keylist);
+    keylist_used = mutt_strlen (keylist);
 
     rfc822_free_address (&addr);
 
@@ -832,8 +830,8 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
 
   while ((fgets (email, sizeof (email), fpout))) {
-    *(email + safe_strlen (email) - 1) = '\0';
-    if (safe_strncasecmp (email, mailbox, safe_strlen (mailbox)) == 0)
+    *(email + mutt_strlen (email) - 1) = '\0';
+    if (safe_strncasecmp (email, mailbox, mutt_strlen (mailbox)) == 0)
       ret = 1;
 
     ret = ret < 0 ? 0 : ret;
@@ -859,9 +857,9 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
     rewind (fpout);
     while ((fgets (email, sizeof (email), fpout))) {
-      *(email + safe_strlen (email) - 1) = '\0';
-      (*buffer)[count] = safe_calloc (1, safe_strlen (email) + 1);
-      strncpy ((*buffer)[count], email, safe_strlen (email));
+      *(email + mutt_strlen (email) - 1) = '\0';
+      (*buffer)[count] = safe_calloc (1, mutt_strlen (email) + 1);
+      strncpy ((*buffer)[count], email, mutt_strlen (email));
       count++;
     }
   }
@@ -1241,7 +1239,7 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
 
   *certfile = '\0';
   while (1) {
-    int off = safe_strlen (certfile);
+    int off = mutt_strlen (certfile);
 
     while (*++cert_end && *cert_end != '\n');
     if (!*cert_end)
@@ -1375,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);
@@ -1661,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) &&
@@ -1674,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;
   }
 
@@ -1729,7 +1725,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
       }
     }
     while (fgets (buf, sizeof (buf) - 1, smimeout) != NULL) {
-      len = safe_strlen (buf);
+      len = mutt_strlen (buf);
       if (len > 1 && buf[len - 2] == '\r') {
         buf[len - 2] = '\n';
         buf[len - 1] = '\0';
@@ -1807,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;
@@ -1837,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);
 }
 
 
@@ -1917,7 +1921,7 @@ int smime_send_menu (HEADER * msg, int *redraw)
   case 4:                      /* sign (a)s */
 
     if ((p = smime_ask_for_key (_("Sign as: "), NULL, 0))) {
-      p[safe_strlen (p) - 1] = '\0';
+      p[mutt_strlen (p) - 1] = '\0';
       str_replace (&SmimeDefaultKey, p);
 
       msg->security |= SIGN;