more constness.
[apps/madmutt.git] / smime.c
diff --git a/smime.c b/smime.c
index 1f25222..cba7100 100644 (file)
--- a/smime.c
+++ b/smime.c
@@ -1,21 +1,12 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 2001,2002 Oliver Ehli <elmy@acm.org>
  * Copyright (C) 2002 Mike Schiraldi <raldi@research.netsol.com>
  * Copyright (C) 2004 g10 Code 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
 #endif
 
 #include "mutt.h"
+#include "enter.h"
+#include "handler.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "smime.h"
 #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>
@@ -108,8 +106,8 @@ int smime_valid_passphrase (void)
 
   smime_void_passphrase ();
 
-  if (mutt_get_password
-      (_("Enter SMIME passphrase:"), SmimePass, sizeof (SmimePass)) == 0) {
+  if (mutt_get_field_unbuffered (_("Enter S/MIME passphrase:"), SmimePass,
+                                 sizeof (SmimePass), M_PASS) == 0) {
     SmimeExptime = time (NULL) + SmimeTimeout;
     return (1);
   }
@@ -254,12 +252,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,
@@ -308,7 +303,7 @@ static void smime_entry (char *s, size_t l, MUTTMENU * menu, int num)
 {
   smime_id *Table = (smime_id *) menu->data;
   smime_id this = Table[num];
-  char *truststate;
+  const char *truststate;
 
   switch (this.trust) {
   case 't':
@@ -394,7 +389,7 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
     }
     /* Read Entries */
     cur = 0;
-    Table = safe_calloc (cert_num, sizeof (smime_id));
+    Table = mem_calloc (cert_num, sizeof (smime_id));
     while (!feof (index)) {
       numFields =
         fscanf (index, MUTT_FORMAT (STRING) " %x.%i " MUTT_FORMAT (STRING),
@@ -408,7 +403,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;
@@ -460,14 +455,14 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
       }
     }
     if (hash) {
-      fname = safe_malloc (13); /* Hash + '.' + Suffix + \0 */
+      fname = mem_malloc (13); /* Hash + '.' + Suffix + \0 */
       sprintf (fname, "%.8x.%i", Table[cur].hash, Table[cur].suffix);
     }
     else
       fname = NULL;
 
     mutt_menuDestroy (&menu);
-    FREE (&Table);
+    mem_free (&Table);
     set_option (OPTNEEDREDRAW);
 
     if (fname)
@@ -493,8 +488,8 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
   if (!mailbox && !query)
     return (NULL);
 
-  addr_len = mailbox ? mutt_strlen (mailbox) : 0;
-  query_len = query ? mutt_strlen (query) : 0;
+  addr_len = mailbox ? str_len (mailbox) : 0;
+  query_len = query ? str_len (query) : 0;
 
   *key = '\0';
 
@@ -516,7 +511,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 && !(str_ncasecmp (mailbox, buf, addr_len))) {
         numFields = sscanf (buf,
                             MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) " "
                             MUTT_FORMAT (STRING) " " MUTT_FORMAT (STRING) " "
@@ -578,13 +573,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))) {
+            !(str_ncasecmp (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))) {
+                 !(str_ncasecmp (query, fields[1], query_len))) {
           ask = 0;
           strfcpy (key, fields[3], sizeof (key));
         }
@@ -626,8 +621,8 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
 
   }
 
-  /* Note: safe_strdup ("") returns NULL. */
-  return safe_strdup (key);
+  /* Note: str_dup ("") returns NULL. */
+  return str_dup (key);
 }
 
 
@@ -653,8 +648,8 @@ void _smime_getkeys (char *mailbox)
   if (k) {
     /* the key used last time. */
     if (*SmimeKeyToUse &&
-        !mutt_strcasecmp (k, SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1)) {
-      FREE (&k);
+        !str_casecmp (k, SmimeKeyToUse + str_len (SmimeKeys) + 1)) {
+      mem_free (&k);
       return;
     }
     else
@@ -666,16 +661,16 @@ void _smime_getkeys (char *mailbox)
     snprintf (SmimeCertToUse, sizeof (SmimeCertToUse), "%s/%s",
               NONULL (SmimeCertificates), k);
 
-    if (mutt_strcasecmp (k, SmimeDefaultKey))
+    if (str_casecmp (k, SmimeDefaultKey))
       smime_void_passphrase ();
 
-    FREE (&k);
+    mem_free (&k);
     return;
   }
 
   if (*SmimeKeyToUse) {
-    if (!mutt_strcasecmp (SmimeDefaultKey,
-                          SmimeKeyToUse + mutt_strlen (SmimeKeys) + 1))
+    if (!str_casecmp (SmimeDefaultKey,
+                          SmimeKeyToUse + str_len (SmimeKeys) + 1))
       return;
 
     smime_void_passphrase ();
@@ -771,16 +766,16 @@ char *smime_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
     }
     if (!keyID) {
       mutt_message (_("No (valid) certificate found for %s."), q->mailbox);
-      FREE (&keylist);
+      mem_free (&keylist);
       rfc822_free_address (&tmp);
       rfc822_free_address (&addr);
       return NULL;
     }
 
-    keylist_size += mutt_strlen (keyID) + 2;
-    safe_realloc (&keylist, keylist_size);
+    keylist_size += str_len (keyID) + 2;
+    mem_realloc (&keylist, keylist_size);
     sprintf (keylist + keylist_used, "%s\n", keyID);    /* __SPRINTF_CHECKED__ */
-    keylist_used = mutt_strlen (keylist);
+    keylist_used = str_len (keylist);
 
     rfc822_free_address (&addr);
 
@@ -837,8 +832,8 @@ 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)
+    *(email + str_len (email) - 1) = '\0';
+    if (str_ncasecmp (email, mailbox, str_len (mailbox)) == 0)
       ret = 1;
 
     ret = ret < 0 ? 0 : ret;
@@ -859,14 +854,14 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
   if (copy && buffer && num) {
     (*num) = count;
-    *buffer = safe_calloc (sizeof (char *), count);
+    *buffer = mem_calloc (sizeof (char *), count);
     count = 0;
 
     rewind (fpout);
     while ((fgets (email, sizeof (email), fpout))) {
-      *(email + mutt_strlen (email) - 1) = '\0';
-      (*buffer)[count] = safe_calloc (1, mutt_strlen (email) + 1);
-      strncpy ((*buffer)[count], email, mutt_strlen (email));
+      *(email + str_len (email) - 1) = '\0';
+      (*buffer)[count] = mem_calloc (1, str_len (email) + 1);
+      strncpy ((*buffer)[count], email, str_len (email));
       count++;
     }
   }
@@ -982,7 +977,7 @@ static char *smime_extract_certificate (char *infile)
   fclose (fpout);
   fclose (fperr);
 
-  return safe_strdup (certfile);
+  return str_dup (certfile);
 }
 
 static char *smime_extract_signer_certificate (char *infile)
@@ -1044,7 +1039,7 @@ static char *smime_extract_signer_certificate (char *infile)
   fclose (fpout);
   fclose (fperr);
 
-  return safe_strdup (certfile);
+  return str_dup (certfile);
 }
 
 
@@ -1096,7 +1091,7 @@ void smime_invoke_import (char *infile, char *mailbox)
     mutt_wait_filter (thepid);
 
     mutt_unlink (certfile);
-    FREE (&certfile);
+    mem_free (&certfile);
   }
 
   fflush (fpout);
@@ -1155,7 +1150,7 @@ int smime_verify_sender (HEADER * h)
       else
         retval = 0;
       mutt_unlink (certfile);
-      FREE (&certfile);
+      mem_free (&certfile);
     }
     else
       mutt_any_key_to_continue (_("no certfile"));
@@ -1246,7 +1241,7 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
 
   *certfile = '\0';
   while (1) {
-    int off = mutt_strlen (certfile);
+    int off = str_len (certfile);
 
     while (*++cert_end && *cert_end != '\n');
     if (!*cert_end)
@@ -1309,14 +1304,14 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
 
   t = mutt_new_body ();
   t->type = TYPEAPPLICATION;
-  t->subtype = safe_strdup ("x-pkcs7-mime");
+  t->subtype = str_dup ("x-pkcs7-mime");
   mutt_set_parameter ("name", "smime.p7m", &t->parameter);
   mutt_set_parameter ("smime-type", "enveloped-data", &t->parameter);
   t->encoding = ENCBASE64;      /* The output of OpenSSL SHOULD be binary */
   t->use_disp = 1;
   t->disposition = DISPATTACH;
-  t->d_filename = safe_strdup ("smime.p7m");
-  t->filename = safe_strdup (tempfile);
+  t->d_filename = str_dup ("smime.p7m");
+  t->filename = str_dup (tempfile);
   t->unlink = 1;                /*delete after sending the message */
   t->parts = 0;
   t->next = 0;
@@ -1380,7 +1375,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);
@@ -1425,7 +1420,7 @@ BODY *smime_sign_message (BODY * a)
 
   t = mutt_new_body ();
   t->type = TYPEMULTIPART;
-  t->subtype = safe_strdup ("signed");
+  t->subtype = str_dup ("signed");
   t->encoding = ENC7BIT;
   t->use_disp = 0;
   t->disposition = DISPINLINE;
@@ -1442,9 +1437,9 @@ BODY *smime_sign_message (BODY * a)
   t->parts->next = mutt_new_body ();
   t = t->parts->next;
   t->type = TYPEAPPLICATION;
-  t->subtype = safe_strdup ("x-pkcs7-signature");
-  t->filename = safe_strdup (signedfile);
-  t->d_filename = safe_strdup ("smime.p7s");
+  t->subtype = str_dup ("x-pkcs7-signature");
+  t->filename = str_dup (signedfile);
+  t->d_filename = str_dup ("smime.p7s");
   t->use_disp = 1;
   t->disposition = DISPATTACH;
   t->encoding = ENCBASE64;
@@ -1528,7 +1523,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
 
   mutt_decode_attachment (sigbdy, s);
 
-  sigbdy->length = ftell (s->fpout);
+  sigbdy->length = ftello (s->fpout);
   sigbdy->offset = 0;
   fclose (s->fpout);
 
@@ -1569,10 +1564,10 @@ 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 && !str_casecmp (line, "verification successful"))
         badsig = 0;
 
-      FREE (&line);
+      mem_free (&line);
     }
   }
 
@@ -1651,7 +1646,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     return NULL;
   }
 
-  fseek (s->fpin, m->offset, 0);
+  fseeko (s->fpin, m->offset, 0);
   last_pos = m->offset;
 
   mutt_copy_bytes (s->fpin, tmpfp, m->length);
@@ -1666,9 +1661,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) &&
@@ -1679,9 +1673,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;
   }
 
@@ -1734,7 +1727,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
       }
     }
     while (fgets (buf, sizeof (buf) - 1, smimeout) != NULL) {
-      len = mutt_strlen (buf);
+      len = str_len (buf);
       if (len > 1 && buf[len - 2] == '\r') {
         buf[len - 2] = '\n';
         buf[len - 1] = '\0';
@@ -1785,9 +1778,9 @@ 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 && !str_casecmp (line, "verification successful"))
       m->goodsig = 1;
-    FREE (&line);
+    mem_free (&line);
   }
   else {
     m->goodsig = p->goodsig;
@@ -1812,6 +1805,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;
@@ -1821,7 +1815,7 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
 
   memset (&s, 0, sizeof (s));
   s.fpin = fpin;
-  fseek (s.fpin, b->offset, 0);
+  fseeko (s.fpin, b->offset, 0);
 
   mutt_mktemp (tempfile);
   if ((tmpfp = safe_fopen (tempfile, "w+")) == NULL) {
@@ -1833,7 +1827,7 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   s.fpout = tmpfp;
   mutt_decode_attachment (b, &s);
   fflush (tmpfp);
-  b->length = ftell (s.fpout);
+  b->length = ftello (s.fpout);
   b->offset = 0;
   rewind (tmpfp);
   s.fpin = tmpfp;
@@ -1842,29 +1836,34 @@ 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);
 }
 
 
-void smime_application_smime_handler (BODY * m, STATE * s)
+int smime_application_smime_handler (BODY * m, STATE * s)
 {
-
-  smime_handle_entity (m, s, NULL);
-
+  return smime_handle_entity (m, s, NULL) ? 0 : -1;
 }
 
 int smime_send_menu (HEADER * msg, int *redraw)
@@ -1884,27 +1883,65 @@ int smime_send_menu (HEADER * msg, int *redraw)
     break;
 
   case 3:                      /* encrypt (w)ith */
-    msg->security |= ENCRYPT;
-    switch (mutt_multi_choice (_("1: DES, 2: Triple-DES, 3: RC2-40,"
-                                 " 4: RC2-64, 5: RC2-128, or (f)orget it? "),
-                               _("12345f"))) {
-    case 1:
-      mutt_str_replace (&SmimeCryptAlg, "des");
-      break;
-    case 2:
-      mutt_str_replace (&SmimeCryptAlg, "des3");
-      break;
-    case 3:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-40");
-      break;
-    case 4:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-64");
-      break;
-    case 5:
-      mutt_str_replace (&SmimeCryptAlg, "rc2-128");
-      break;
-    case 6:                    /* forget it */
-      break;
+    {
+      int choice = 0;
+      msg->security |= ENCRYPT;
+
+      do {
+        /* I use "dra" because "123" is recognized anyway */
+        switch (mutt_multi_choice (_("Choose algorithm family:"
+                                     " 1: DES, 2: RC2, 3: AES,"
+                                     " or (c)lear? "), _("drac"))) {
+          case 1:
+            switch (choice = mutt_multi_choice (_("1: DES, 2: Triple-DES "),
+                                                _("dt"))) {
+              case 1:
+                str_replace (&SmimeCryptAlg, "des");
+                break;
+              case 2:
+                str_replace (&SmimeCryptAlg, "des3");
+                break;
+            }
+            break;
+
+          case 2:
+            switch (choice = mutt_multi_choice (_("1: RC2-40, 2: RC2-64, 3: RC2-128 "),
+                                                _("468"))) {
+              case 1:
+                str_replace (&SmimeCryptAlg, "rc2-40");
+                break;
+              case 2:
+                str_replace (&SmimeCryptAlg, "rc2-64");
+                break;
+              case 3:
+                str_replace (&SmimeCryptAlg, "rc2-128");
+                break;
+            }
+            break;
+
+          case 3:
+            switch (choice = mutt_multi_choice (_("1: AES128, 2: AES192, 3: AES256 "),
+                                                _("895"))) {
+              case 1:
+                str_replace (&SmimeCryptAlg, "aes128");
+                break;
+              case 2:
+                str_replace (&SmimeCryptAlg, "aes192");
+                break;
+              case 3:
+                str_replace (&SmimeCryptAlg, "aes256");
+                break;
+            }
+            break;
+
+          case 4: /* (c)lear */
+            mem_free (&SmimeCryptAlg);
+            /* fallback */
+          case -1: /* Ctrl-G or Enter */
+            choice = 0;
+            break;
+        }
+      } while (choice == -1);
     }
     break;
 
@@ -1922,8 +1959,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[mutt_strlen (p) - 1] = '\0';
-      mutt_str_replace (&SmimeDefaultKey, p);
+      str_replace (&SmimeDefaultKey, p);
 
       msg->security |= SIGN;