always build imap as well.
[apps/madmutt.git] / crypt.c
diff --git a/crypt.c b/crypt.c
index 39fead1..bc44714 100644 (file)
--- a/crypt.c
+++ b/crypt.c
@@ -1,4 +1,5 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
  * Copyright (C) 2001  Thomas Roessler <roessler@does-not-exist.org>
@@ -6,32 +7,31 @@
  * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
  * Copyright (C) 2004 g10code GmbH
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- * 
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
  */
 
 #if HAVE_CONFIG_H
 # 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 "handler.h"
 #include "mutt_curses.h"
-#include "mime.h"
 #include "copy.h"
 #include "mutt_crypt.h"
 #include "pgp.h"
 
+
 #include <sys/wait.h>
 #include <string.h>
 #include <stdlib.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, "");
@@ -80,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.");
 }
 
@@ -115,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;
@@ -131,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...");
@@ -169,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;
@@ -192,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 ;-) */
     }
@@ -200,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! */
@@ -218,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))) {
 
@@ -268,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;
@@ -285,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;
 }
 
 
@@ -391,7 +366,7 @@ int mutt_is_application_smime (BODY * m)
 
     /* no .p7c, .p10 support yet. */
 
-    len = mutt_strlen (t) - 4;
+    len = m_strlen(t) - 4;
     if (len > 0 && *(t + len) == '.') {
       len++;
       if (!ascii_strcasecmp ((t + len), "p7m"))
@@ -419,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;
@@ -482,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) {
@@ -520,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 &&
-             mutt_strcasecmp (a->subtype, "delivery-status")) {
+             m_strcasecmp(a->subtype, "delivery-status")) {
       if (a->encoding != ENC7BIT)
         mutt_message_to_7bit (a, NULL);
     }
@@ -556,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++) {
@@ -582,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);
@@ -592,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
@@ -623,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);
@@ -659,8 +614,7 @@ void crypt_extract_keys_from_messages (HEADER * h)
 
   mutt_unlink (tempfname);
 
-  if ((WithCrypto & APPLICATION_PGP))
-    unset_option (OPTDONTHANDLEPGPKEYS);
+  unset_option (OPTDONTHANDLEPGPKEYS);
 }
 
 
@@ -671,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);
@@ -700,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)
-        safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
+        p_realloc(signatures, *n + 6);
 
       (*signatures)[(*n)++] = a;
     }
@@ -722,7 +667,7 @@ static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
  * This routine verifies a  "multipart/signed"  body.
  */
 
-void mutt_signed_handler (BODY * a, STATE * s)
+int mutt_signed_handler (BODY * a, STATE * s)
 {
   char tempfile[_POSIX_PATH_MAX];
   char *protocol;
@@ -734,9 +679,7 @@ void mutt_signed_handler (BODY * a, STATE * s)
   int sigcnt = 0;
   int i;
   short goodsig = 1;
-
-  if (!WithCrypto)
-    return;
+  int rc = 0;
 
   protocol = mutt_get_parameter ("protocol", a->parameter);
   a = a->parts;
@@ -750,7 +693,7 @@ void 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';
 
@@ -760,30 +703,26 @@ void mutt_signed_handler (BODY * a, STATE * s)
   /* consistency check */
 
   if (!(a && a->next && a->next->type == protocol_major &&
-        !mutt_strcasecmp (a->next->subtype, protocol_minor))) {
+        !m_strcasecmp(a->next->subtype, protocol_minor))) {
     state_attach_puts (_("[-- Error: "
                          "Inconsistent multipart/signed structure! --]\n\n"),
                        s);
-    mutt_body_handler (a, s);
-    return;
+    return mutt_body_handler (a, s);
   }
 
 
-  if ((WithCrypto & APPLICATION_PGP)
-      && protocol_major == TYPEAPPLICATION
-      && !mutt_strcasecmp (protocol_minor, "pgp-signature"));
-  else if ((WithCrypto & APPLICATION_SMIME)
-           && protocol_major == TYPEAPPLICATION
-           && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
-                && mutt_strcasecmp (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
-           && !mutt_strcasecmp (protocol_minor, "mixed"));
+           && !m_strcasecmp(protocol_minor, "mixed"));
   else {
     state_printf (s, _("[-- Error: "
                        "Unknown multipart/signed protocol %s! --]\n\n"),
                   protocol);
-    mutt_body_handler (a, s);
-    return;
+    return mutt_body_handler (a, s);
   }
 
   if (s->flags & M_DISPLAY) {
@@ -794,21 +733,18 @@ void 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
-              && !mutt_strcasecmp (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
-              &&
-              (!mutt_strcasecmp (signatures[i]->subtype, "x-pkcs7-signature")
-               || !mutt_strcasecmp (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;
 
@@ -830,15 +766,17 @@ void mutt_signed_handler (BODY * a, STATE * s)
       state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
 
 
-      FREE (&signatures);
+      p_delete(&signatures);
     }
     else
       state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
                          s);
   }
 
-  mutt_body_handler (a, s);
+  rc = mutt_body_handler (a, s);
 
   if (s->flags & M_DISPLAY && sigcnt)
     state_attach_puts (_("\n[-- End of signed data --]\n"), s);
+
+  return (rc);
 }