move hash.[hc] into lib-lib/
[apps/madmutt.git] / smime.c
diff --git a/smime.c b/smime.c
index 0116684..be9b6b4 100644 (file)
--- a/smime.c
+++ b/smime.c
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/macros.h>
+
 #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>
@@ -90,7 +93,7 @@ static char SmimeIntermediateToUse[_POSIX_PATH_MAX];
 
 void smime_void_passphrase (void)
 {
-  memset (SmimePass, 0, sizeof (SmimePass));
+  p_clear(SmimePass, sizeof(SmimePass));
   SmimeExptime = 0;
 }
 
@@ -104,7 +107,7 @@ int smime_valid_passphrase (void)
 
   smime_void_passphrase ();
 
-  if (mutt_get_field_unbuffered (_("Enter SMIME passphrase:"), SmimePass,
+  if (mutt_get_field_unbuffered (_("Enter S/MIME passphrase:"), SmimePass,
                                  sizeof (SmimePass), M_PASS) == 0) {
     SmimeExptime = time (NULL) + SmimeTimeout;
     return (1);
@@ -263,7 +266,7 @@ static pid_t smime_invoke (FILE ** smimein, FILE ** smimeout,
   struct smime_command_context cctx;
   char cmd[HUGE_STRING];
 
-  memset (&cctx, 0, sizeof (cctx));
+  p_clear(&cctx, 1);
 
   if (!format || !*format)
     return (pid_t) - 1;
@@ -301,7 +304,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':
@@ -387,7 +390,7 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
     }
     /* Read Entries */
     cur = 0;
-    Table = mem_calloc (cert_num, sizeof (smime_id));
+    Table = p_new(smime_id, cert_num);
     while (!feof (index)) {
       numFields =
         fscanf (index, MUTT_FORMAT (STRING) " %x.%i " MUTT_FORMAT (STRING),
@@ -453,14 +456,14 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public)
       }
     }
     if (hash) {
-      fname = mem_malloc (13); /* Hash + '.' + Suffix + \0 */
+      fname = p_new(char, 13); /* Hash + '.' + Suffix + \0 */
       sprintf (fname, "%.8x.%i", Table[cur].hash, Table[cur].suffix);
     }
     else
       fname = NULL;
 
     mutt_menuDestroy (&menu);
-    mem_free (&Table);
+    p_delete(&Table);
     set_option (OPTNEEDREDRAW);
 
     if (fname)
@@ -486,8 +489,8 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
   if (!mailbox && !query)
     return (NULL);
 
-  addr_len = mailbox ? str_len (mailbox) : 0;
-  query_len = query ? str_len (query) : 0;
+  addr_len = mailbox ? m_strlen(mailbox) : 0;
+  query_len = query ? m_strlen(query) : 0;
 
   *key = '\0';
 
@@ -619,8 +622,8 @@ char *smime_get_field_from_db (char *mailbox, char *query, short public,
 
   }
 
-  /* Note: str_dup ("") returns NULL. */
-  return str_dup (key);
+  /* Note: m_strdup("") returns NULL. */
+  return m_strdup(key);
 }
 
 
@@ -646,8 +649,8 @@ void _smime_getkeys (char *mailbox)
   if (k) {
     /* the key used last time. */
     if (*SmimeKeyToUse &&
-        !str_casecmp (k, SmimeKeyToUse + str_len (SmimeKeys) + 1)) {
-      mem_free (&k);
+        !str_casecmp (k, SmimeKeyToUse + m_strlen(SmimeKeys) + 1)) {
+      p_delete(&k);
       return;
     }
     else
@@ -662,13 +665,13 @@ void _smime_getkeys (char *mailbox)
     if (str_casecmp (k, SmimeDefaultKey))
       smime_void_passphrase ();
 
-    mem_free (&k);
+    p_delete(&k);
     return;
   }
 
   if (*SmimeKeyToUse) {
     if (!str_casecmp (SmimeDefaultKey,
-                          SmimeKeyToUse + str_len (SmimeKeys) + 1))
+                          SmimeKeyToUse + m_strlen(SmimeKeys) + 1))
       return;
 
     smime_void_passphrase ();
@@ -764,16 +767,16 @@ char *smime_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
     }
     if (!keyID) {
       mutt_message (_("No (valid) certificate found for %s."), q->mailbox);
-      mem_free (&keylist);
+      p_delete(&keylist);
       rfc822_free_address (&tmp);
       rfc822_free_address (&addr);
       return NULL;
     }
 
-    keylist_size += str_len (keyID) + 2;
-    mem_realloc (&keylist, keylist_size);
+    keylist_size += m_strlen(keyID) + 2;
+    p_realloc(&keylist, keylist_size);
     sprintf (keylist + keylist_used, "%s\n", keyID);    /* __SPRINTF_CHECKED__ */
-    keylist_used = str_len (keylist);
+    keylist_used = m_strlen(keylist);
 
     rfc822_free_address (&addr);
 
@@ -830,8 +833,8 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
 
   while ((fgets (email, sizeof (email), fpout))) {
-    *(email + str_len (email) - 1) = '\0';
-    if (str_ncasecmp (email, mailbox, str_len (mailbox)) == 0)
+    *(email + m_strlen(email) - 1) = '\0';
+    if (str_ncasecmp (email, mailbox, m_strlen(mailbox)) == 0)
       ret = 1;
 
     ret = ret < 0 ? 0 : ret;
@@ -852,14 +855,13 @@ static int smime_handle_cert_email (char *certificate, char *mailbox,
 
   if (copy && buffer && num) {
     (*num) = count;
-    *buffer = mem_calloc (sizeof (char *), count);
+    *buffer = p_new(char *, count);
     count = 0;
 
     rewind (fpout);
     while ((fgets (email, sizeof (email), fpout))) {
-      *(email + str_len (email) - 1) = '\0';
-      (*buffer)[count] = mem_calloc (1, str_len (email) + 1);
-      strncpy ((*buffer)[count], email, str_len (email));
+      *(email + m_strlen(email) - 1) = '\0';
+      (*buffer)[count] = p_dupstr(email, m_strlen(email));
       count++;
     }
   }
@@ -975,7 +977,7 @@ static char *smime_extract_certificate (char *infile)
   fclose (fpout);
   fclose (fperr);
 
-  return str_dup (certfile);
+  return m_strdup(certfile);
 }
 
 static char *smime_extract_signer_certificate (char *infile)
@@ -1037,7 +1039,7 @@ static char *smime_extract_signer_certificate (char *infile)
   fclose (fpout);
   fclose (fperr);
 
-  return str_dup (certfile);
+  return m_strdup(certfile);
 }
 
 
@@ -1089,7 +1091,7 @@ void smime_invoke_import (char *infile, char *mailbox)
     mutt_wait_filter (thepid);
 
     mutt_unlink (certfile);
-    mem_free (&certfile);
+    p_delete(&certfile);
   }
 
   fflush (fpout);
@@ -1148,7 +1150,7 @@ int smime_verify_sender (HEADER * h)
       else
         retval = 0;
       mutt_unlink (certfile);
-      mem_free (&certfile);
+      p_delete(&certfile);
     }
     else
       mutt_any_key_to_continue (_("no certfile"));
@@ -1239,7 +1241,7 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
 
   *certfile = '\0';
   while (1) {
-    int off = str_len (certfile);
+    int off = m_strlen(certfile);
 
     while (*++cert_end && *cert_end != '\n');
     if (!*cert_end)
@@ -1302,14 +1304,14 @@ BODY *smime_build_smime_entity (BODY * a, char *certlist)
 
   t = mutt_new_body ();
   t->type = TYPEAPPLICATION;
-  t->subtype = str_dup ("x-pkcs7-mime");
+  t->subtype = m_strdup("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 = str_dup ("smime.p7m");
-  t->filename = str_dup (tempfile);
+  t->d_filename = m_strdup("smime.p7m");
+  t->filename = m_strdup(tempfile);
   t->unlink = 1;                /*delete after sending the message */
   t->parts = 0;
   t->next = 0;
@@ -1418,7 +1420,7 @@ BODY *smime_sign_message (BODY * a)
 
   t = mutt_new_body ();
   t->type = TYPEMULTIPART;
-  t->subtype = str_dup ("signed");
+  t->subtype = m_strdup("signed");
   t->encoding = ENC7BIT;
   t->use_disp = 0;
   t->disposition = DISPINLINE;
@@ -1435,9 +1437,9 @@ BODY *smime_sign_message (BODY * a)
   t->parts->next = mutt_new_body ();
   t = t->parts->next;
   t->type = TYPEAPPLICATION;
-  t->subtype = str_dup ("x-pkcs7-signature");
-  t->filename = str_dup (signedfile);
-  t->d_filename = str_dup ("smime.p7s");
+  t->subtype = m_strdup("x-pkcs7-signature");
+  t->filename = m_strdup(signedfile);
+  t->d_filename = m_strdup("smime.p7s");
   t->use_disp = 1;
   t->disposition = DISPATTACH;
   t->encoding = ENCBASE64;
@@ -1521,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);
 
@@ -1565,7 +1567,7 @@ int smime_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
       if (linelen && !str_casecmp (line, "verification successful"))
         badsig = 0;
 
-      mem_free (&line);
+      p_delete(&line);
     }
   }
 
@@ -1644,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);
@@ -1725,7 +1727,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
       }
     }
     while (fgets (buf, sizeof (buf) - 1, smimeout) != NULL) {
-      len = str_len (buf);
+      len = m_strlen(buf);
       if (len > 1 && buf[len - 2] == '\r') {
         buf[len - 2] = '\n';
         buf[len - 1] = '\0';
@@ -1778,7 +1780,7 @@ static BODY *smime_handle_entity (BODY * m, STATE * s, FILE * outFile)
     line = mutt_read_line (line, &linelen, smimeerr, &lineno);
     if (linelen && !str_casecmp (line, "verification successful"))
       m->goodsig = 1;
-    mem_free (&line);
+    p_delete(&line);
   }
   else {
     m->goodsig = p->goodsig;
@@ -1811,9 +1813,9 @@ int smime_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
   if (b->parts)
     return -1;
 
-  memset (&s, 0, sizeof (s));
+  p_clear(&s, 1);
   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) {
@@ -1825,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;
@@ -1859,11 +1861,9 @@ bail:
 }
 
 
-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)
@@ -1883,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:
-      str_replace (&SmimeCryptAlg, "des");
-      break;
-    case 2:
-      str_replace (&SmimeCryptAlg, "des3");
-      break;
-    case 3:
-      str_replace (&SmimeCryptAlg, "rc2-40");
-      break;
-    case 4:
-      str_replace (&SmimeCryptAlg, "rc2-64");
-      break;
-    case 5:
-      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 */
+            p_delete(&SmimeCryptAlg);
+            /* fallback */
+          case -1: /* Ctrl-G or Enter */
+            choice = 0;
+            break;
+        }
+      } while (choice == -1);
     }
     break;
 
@@ -1921,7 +1959,6 @@ int smime_send_menu (HEADER * msg, int *redraw)
   case 4:                      /* sign (a)s */
 
     if ((p = smime_ask_for_key (_("Sign as: "), NULL, 0))) {
-      p[str_len (p) - 1] = '\0';
       str_replace (&SmimeDefaultKey, p);
 
       msg->security |= SIGN;