always build imap as well.
[apps/madmutt.git] / crypt.c
diff --git a/crypt.c b/crypt.c
index 402a497..bc44714 100644 (file)
--- a/crypt.c
+++ b/crypt.c
 # include "config.h"
 #endif
 
+#include <lib-lib/str.h>
+#include <lib-lib/file.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/mem.h>
+#include <lib-lib/macros.h>
+
+#include <lib-mime/mime.h>
+
 #include "mutt.h"
-#include "ascii.h"
 #include "handler.h"
 #include "mutt_curses.h"
-#include "mime.h"
 #include "copy.h"
 #include "mutt_crypt.h"
 #include "pgp.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
 
 #include <sys/wait.h>
 #include <string.h>
 
 
 /* print the current time to avoid spoofing of the signature output */
-void crypt_current_time (STATE * s, char *app_name)
+void crypt_current_time (STATE * s, const char *app_name)
 {
   time_t t;
   char p[STRING], tmp[STRING];
 
-  if (!WithCrypto)
-    return;
-
   if (option (OPTCRYPTTIMESTAMP)) {
     t = time (NULL);
     setlocale (LC_TIME, "");
@@ -77,13 +77,8 @@ void crypt_current_time (STATE * s, char *app_name)
 
 void crypt_forget_passphrase (void)
 {
-  if ((WithCrypto & APPLICATION_PGP))
     crypt_pgp_void_passphrase ();
-
-  if ((WithCrypto & APPLICATION_SMIME))
     crypt_smime_void_passphrase ();
-
-  if (WithCrypto)
     mutt_message _("Passphrase(s) forgotten.");
 }
 
@@ -112,10 +107,10 @@ int crypt_valid_passphrase (int flags)
   disable_coredumps ();
 # endif
 
-  if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
+  if (flags & APPLICATION_PGP)
     ret = crypt_pgp_valid_passphrase ();
 
-  if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
+  if (flags & APPLICATION_SMIME)
     ret = crypt_smime_valid_passphrase ();
 
   return ret;
@@ -128,17 +123,13 @@ int mutt_protect (HEADER * msg, char *keylist)
   BODY *pbody = NULL, *tmp_pbody = NULL;
   BODY *tmp_smime_pbody = NULL;
   BODY *tmp_pgp_pbody = NULL;
-  int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
+  int flags = msg->security;
   int i;
 
-  if (!WithCrypto)
-    return -1;
-
   if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
     return (-1);
 
-  if ((WithCrypto & APPLICATION_PGP)
-      && ((msg->security & PGPINLINE) == PGPINLINE)) {
+  if ((msg->security & PGPINLINE) == PGPINLINE) {
     /* they really want to send it inline... go for it */
     if (!isendwin ())
       mutt_endwin _("Invoking PGP...");
@@ -166,21 +157,17 @@ int mutt_protect (HEADER * msg, char *keylist)
   if (!isendwin ())
     mutt_endwin (NULL);
 
-  if ((WithCrypto & APPLICATION_SMIME))
-    tmp_smime_pbody = msg->content;
-  if ((WithCrypto & APPLICATION_PGP))
-    tmp_pgp_pbody = msg->content;
+  tmp_smime_pbody = msg->content;
+  tmp_pgp_pbody = msg->content;
 
   if (msg->security & SIGN) {
-    if ((WithCrypto & APPLICATION_SMIME)
-        && (msg->security & APPLICATION_SMIME)) {
+    if (msg->security & APPLICATION_SMIME) {
       if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
         return -1;
       pbody = tmp_smime_pbody = tmp_pbody;
     }
 
-    if ((WithCrypto & APPLICATION_PGP)
-        && (msg->security & APPLICATION_PGP)
+    if ((msg->security & APPLICATION_PGP)
         && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
       if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
         return -1;
@@ -189,7 +176,7 @@ int mutt_protect (HEADER * msg, char *keylist)
       pbody = tmp_pgp_pbody = tmp_pbody;
     }
 
-    if (WithCrypto && (msg->security & APPLICATION_SMIME)
+    if ((msg->security & APPLICATION_SMIME)
         && (msg->security & APPLICATION_PGP)) {
       /* here comes the draft ;-) */
     }
@@ -197,8 +184,7 @@ int mutt_protect (HEADER * msg, char *keylist)
 
 
   if (msg->security & ENCRYPT) {
-    if ((WithCrypto & APPLICATION_SMIME)
-        && (msg->security & APPLICATION_SMIME)) {
+    if ((msg->security & APPLICATION_SMIME)) {
       if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
                                                         keylist))) {
         /* signed ? free it! */
@@ -215,8 +201,7 @@ int mutt_protect (HEADER * msg, char *keylist)
       pbody = tmp_pbody;
     }
 
-    if ((WithCrypto & APPLICATION_PGP)
-        && (msg->security & APPLICATION_PGP)) {
+    if ((msg->security & APPLICATION_PGP)) {
       if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
                                                flags & SIGN))) {
 
@@ -265,15 +250,12 @@ int mutt_is_multipart_signed (BODY * b)
   if (!(ascii_strcasecmp (p, "multipart/mixed")))
     return SIGN;
 
-  if ((WithCrypto & APPLICATION_PGP)
-      && !(ascii_strcasecmp (p, "application/pgp-signature")))
+  if (!(ascii_strcasecmp (p, "application/pgp-signature")))
     return PGPSIGN;
 
-  if ((WithCrypto & APPLICATION_SMIME)
-      && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
+  if (!(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
     return SMIMESIGN;
-  if ((WithCrypto & APPLICATION_SMIME)
-      && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
+  if (!(ascii_strcasecmp (p, "application/pkcs7-signature")))
     return SMIMESIGN;
 
   return 0;
@@ -282,19 +264,15 @@ int mutt_is_multipart_signed (BODY * b)
 
 int mutt_is_multipart_encrypted (BODY * b)
 {
-  if ((WithCrypto & APPLICATION_PGP)) {
-    char *p;
-
-    if (!b || b->type != TYPEMULTIPART ||
-        !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
-        !(p = mutt_get_parameter ("protocol", b->parameter)) ||
-        ascii_strcasecmp (p, "application/pgp-encrypted"))
-      return 0;
+  char *p;
 
-    return PGPENCRYPT;
-  }
+  if (!b || b->type != TYPEMULTIPART ||
+      !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
+      !(p = mutt_get_parameter ("protocol", b->parameter)) ||
+      ascii_strcasecmp (p, "application/pgp-encrypted"))
+    return 0;
 
-  return 0;
+  return PGPENCRYPT;
 }
 
 
@@ -388,7 +366,7 @@ int mutt_is_application_smime (BODY * m)
 
     /* no .p7c, .p10 support yet. */
 
-    len = str_len (t) - 4;
+    len = m_strlen(t) - 4;
     if (len > 0 && *(t + len) == '.') {
       len++;
       if (!ascii_strcasecmp ((t + len), "p7m"))
@@ -416,25 +394,19 @@ int crypt_query (BODY * m)
 {
   int t = 0;
 
-  if (!WithCrypto)
-    return 0;
-
   if (!m)
     return 0;
 
   if (m->type == TYPEAPPLICATION) {
-    if ((WithCrypto & APPLICATION_PGP))
-      t |= mutt_is_application_pgp (m);
-
-    if ((WithCrypto & APPLICATION_SMIME)) {
-      t |= mutt_is_application_smime (m);
-      if (t && m->goodsig)
-        t |= GOODSIGN;
-      if (t && m->badsig)
-        t |= BADSIGN;
-    }
+    t |= mutt_is_application_pgp (m);
+
+    t |= mutt_is_application_smime (m);
+    if (t && m->goodsig)
+      t |= GOODSIGN;
+    if (t && m->badsig)
+      t |= BADSIGN;
   }
-  else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
+  else if (m->type == TYPETEXT) {
     t |= mutt_is_application_pgp (m);
     if (t && m->goodsig)
       t |= GOODSIGN;
@@ -479,15 +451,12 @@ int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
   short hadcr;
   size_t bytes;
 
-  if (!WithCrypto)
-    return -1;
-
   if (!(fp = safe_fopen (tempfile, "w"))) {
     mutt_perror (tempfile);
     return -1;
   }
 
-  fseek (s->fpin, a->hdr_offset, 0);
+  fseeko (s->fpin, a->hdr_offset, 0);
   bytes = a->length + a->offset - a->hdr_offset;
   hadcr = 0;
   while (bytes > 0) {
@@ -517,20 +486,17 @@ int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
 
 void convert_to_7bit (BODY * a)
 {
-  if (!WithCrypto)
-    return;
-
   while (a) {
     if (a->type == TYPEMULTIPART) {
       if (a->encoding != ENC7BIT) {
         a->encoding = ENC7BIT;
         convert_to_7bit (a->parts);
       }
-      else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
+      else if (option (OPTPGPSTRICTENC))
         convert_to_7bit (a->parts);
     }
     else if (a->type == TYPEMESSAGE &&
-             str_casecmp (a->subtype, "delivery-status")) {
+             m_strcasecmp(a->subtype, "delivery-status")) {
       if (a->encoding != ENC7BIT)
         mutt_message_to_7bit (a, NULL);
     }
@@ -553,20 +519,16 @@ void crypt_extract_keys_from_messages (HEADER * h)
 {
   int i;
   char tempfname[_POSIX_PATH_MAX], *mbox;
-  ADDRESS *tmp = NULL;
+  address_t *tmp = NULL;
   FILE *fpout;
 
-  if (!WithCrypto)
-    return;
-
   mutt_mktemp (tempfname);
   if (!(fpout = safe_fopen (tempfname, "w"))) {
     mutt_perror (tempfname);
     return;
   }
 
-  if ((WithCrypto & APPLICATION_PGP))
-    set_option (OPTDONTHANDLEPGPKEYS);
+  set_option (OPTDONTHANDLEPGPKEYS);
 
   if (!h) {
     for (i = 0; i < Context->vcount; i++) {
@@ -579,8 +541,7 @@ void crypt_extract_keys_from_messages (HEADER * h)
           break;
         }
 
-        if ((WithCrypto & APPLICATION_PGP)
-            && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
+        if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
           mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
                              M_CM_DECODE | M_CM_CHARCONV, 0);
           fflush (fpout);
@@ -589,8 +550,7 @@ void crypt_extract_keys_from_messages (HEADER * h)
           crypt_pgp_invoke_import (tempfname);
         }
 
-        if ((WithCrypto & APPLICATION_SMIME)
-            && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
+        if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
           if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
             mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
                                M_CM_NOHEADER | M_CM_DECODE_CRYPT
@@ -620,16 +580,14 @@ void crypt_extract_keys_from_messages (HEADER * h)
   else {
     mutt_parse_mime_message (Context, h);
     if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
-      if ((WithCrypto & APPLICATION_PGP)
-          && (h->security & APPLICATION_PGP)) {
+      if (h->security & APPLICATION_PGP) {
         mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
         fflush (fpout);
         mutt_endwin (_("Trying to extract PGP keys...\n"));
         crypt_pgp_invoke_import (tempfname);
       }
 
-      if ((WithCrypto & APPLICATION_SMIME)
-          && (h->security & APPLICATION_SMIME)) {
+      if (h->security & APPLICATION_SMIME) {
         if (h->security & ENCRYPT)
           mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
                              | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
@@ -656,8 +614,7 @@ void crypt_extract_keys_from_messages (HEADER * h)
 
   mutt_unlink (tempfname);
 
-  if ((WithCrypto & APPLICATION_PGP))
-    unset_option (OPTDONTHANDLEPGPKEYS);
+  unset_option (OPTDONTHANDLEPGPKEYS);
 }
 
 
@@ -668,24 +625,18 @@ int crypt_get_keys (HEADER * msg, char **keylist)
    * keys if the user has requested this service.
    */
 
-  if (!WithCrypto)
-    return 0;
-
-  if ((WithCrypto & APPLICATION_PGP))
-    set_option (OPTPGPCHECKTRUST);
+  set_option (OPTPGPCHECKTRUST);
 
   *keylist = NULL;
 
   if (msg->security & ENCRYPT) {
-    if ((WithCrypto & APPLICATION_PGP)
-        && (msg->security & APPLICATION_PGP)) {
+    if (msg->security & APPLICATION_PGP) {
       if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
                                           msg->env->bcc)) == NULL)
         return (-1);
       unset_option (OPTPGPCHECKTRUST);
     }
-    if ((WithCrypto & APPLICATION_SMIME)
-        && (msg->security & APPLICATION_SMIME)) {
+    if (msg->security & APPLICATION_SMIME) {
       if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
                                             msg->env->bcc)) == NULL)
         return (-1);
@@ -697,17 +648,14 @@ int crypt_get_keys (HEADER * msg, char **keylist)
 
 
 
-static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
+static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
 {
-  if (!WithCrypto)
-    return;
-
   for (; a; a = a->next) {
     if (a->type == TYPEMULTIPART)
       crypt_fetch_signatures (signatures, a->parts, n);
     else {
       if ((*n % 5) == 0)
-        mem_realloc (signatures, (*n + 6) * sizeof (BODY **));
+        p_realloc(signatures, *n + 6);
 
       (*signatures)[(*n)++] = a;
     }
@@ -733,9 +681,6 @@ int mutt_signed_handler (BODY * a, STATE * s)
   short goodsig = 1;
   int rc = 0;
 
-  if (!WithCrypto)
-    return (-1);
-
   protocol = mutt_get_parameter ("protocol", a->parameter);
   a = a->parts;
 
@@ -748,7 +693,7 @@ int mutt_signed_handler (BODY * a, STATE * s)
     if ((protocol_minor = strchr (protocol, '/')))
       protocol_minor++;
 
-    strfcpy (major, protocol, sizeof (major));
+    m_strcpy(major, sizeof(major), protocol);
     if ((t = strchr (major, '/')))
       *t = '\0';
 
@@ -758,7 +703,7 @@ int mutt_signed_handler (BODY * a, STATE * s)
   /* consistency check */
 
   if (!(a && a->next && a->next->type == protocol_major &&
-        !str_casecmp (a->next->subtype, protocol_minor))) {
+        !m_strcasecmp(a->next->subtype, protocol_minor))) {
     state_attach_puts (_("[-- Error: "
                          "Inconsistent multipart/signed structure! --]\n\n"),
                        s);
@@ -766,15 +711,13 @@ int mutt_signed_handler (BODY * a, STATE * s)
   }
 
 
-  if ((WithCrypto & APPLICATION_PGP)
-      && protocol_major == TYPEAPPLICATION
-      && !str_casecmp (protocol_minor, "pgp-signature"));
-  else if ((WithCrypto & APPLICATION_SMIME)
-           && protocol_major == TYPEAPPLICATION
-           && !(str_casecmp (protocol_minor, "x-pkcs7-signature")
-                && str_casecmp (protocol_minor, "pkcs7-signature")));
+  if (protocol_major == TYPEAPPLICATION
+      && !m_strcasecmp(protocol_minor, "pgp-signature"));
+  else if (protocol_major == TYPEAPPLICATION
+           && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
+                && m_strcasecmp(protocol_minor, "pkcs7-signature")));
   else if (protocol_major == TYPEMULTIPART
-           && !str_casecmp (protocol_minor, "mixed"));
+           && !m_strcasecmp(protocol_minor, "mixed"));
   else {
     state_printf (s, _("[-- Error: "
                        "Unknown multipart/signed protocol %s! --]\n\n"),
@@ -790,21 +733,18 @@ int mutt_signed_handler (BODY * a, STATE * s)
       mutt_mktemp (tempfile);
       if (crypt_write_signed (a, s, tempfile) == 0) {
         for (i = 0; i < sigcnt; i++) {
-          if ((WithCrypto & APPLICATION_PGP)
-              && signatures[i]->type == TYPEAPPLICATION
-              && !str_casecmp (signatures[i]->subtype, "pgp-signature")) {
+          if (signatures[i]->type == TYPEAPPLICATION
+              && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
             if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
               goodsig = 0;
 
             continue;
           }
 
-          if ((WithCrypto & APPLICATION_SMIME)
-              && signatures[i]->type == TYPEAPPLICATION
-              &&
-              (!str_casecmp (signatures[i]->subtype, "x-pkcs7-signature")
-               || !str_casecmp (signatures[i]->subtype,
-                                    "pkcs7-signature"))) {
+          if (signatures[i]->type == TYPEAPPLICATION
+          && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
+               || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
+          {
             if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
               goodsig = 0;
 
@@ -826,7 +766,7 @@ int mutt_signed_handler (BODY * a, STATE * s)
       state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
 
 
-      mem_free (&signatures);
+      p_delete(&signatures);
     }
     else
       state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),