2006-06-15 [colin] 2.3.0cvs16
[claws.git] / src / plugins / pgpinline / pgpinline.c
index 81e29c3..354230b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
  * This file (C) 2004 Colin Leroy <colin@colino.net>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -15,7 +15,7 @@
  *
  * 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-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -26,6 +26,8 @@
 
 #include "defs.h"
 #include <glib.h>
+#include <glib/gi18n.h>
+#include <errno.h>
 #include <gpgme.h>
 
 #include "utils.h"
@@ -78,6 +80,37 @@ static void pgpinline_free_privacydata(PrivacyData *_data)
        g_free(data);
 }
 
+static gchar *fp_read_noconv(FILE *fp)
+{
+       GByteArray *array;
+       guchar buf[BUFSIZ];
+       gint n_read;
+       gchar *result = NULL;
+
+       if (!fp)
+               return NULL;
+       array = g_byte_array_new();
+
+       while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+               if (n_read < sizeof(buf) && ferror(fp))
+                       break;
+               g_byte_array_append(array, buf, n_read);
+       }
+
+       if (ferror(fp)) {
+               FILE_OP_ERROR("file stream", "fread");
+               g_byte_array_free(array, TRUE);
+               return NULL;
+       }
+
+       buf[0] = '\0';
+       g_byte_array_append(array, buf, 1);
+       result = (gchar *)array->data;
+       g_byte_array_free(array, FALSE);
+       
+       return result;
+}
+
 static gchar *get_part_as_string(MimeInfo *mimeinfo)
 {
        gchar *textdata = NULL;
@@ -86,8 +119,39 @@ static gchar *get_part_as_string(MimeInfo *mimeinfo)
        procmime_decode_content(mimeinfo);
        if (mimeinfo->content == MIMECONTENT_MEM)
                textdata = g_strdup(mimeinfo->data.mem);
-       else
-               textdata = file_read_to_str(mimeinfo->data.filename);
+       else {
+               /* equals file_read_to_str but without conversion */
+               FILE *fp = fopen(mimeinfo->data.filename, "r");
+               if (!fp)
+                       return NULL;
+               textdata = fp_read_noconv(fp);
+               fclose(fp);
+       }
+
+       if (!g_utf8_validate(textdata, -1, NULL)) {
+               gchar *tmp = NULL;
+               codeconv_set_strict(TRUE);
+               if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
+                       tmp = conv_codeset_strdup(textdata,
+                               procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
+                               CS_UTF_8);
+               }
+               if (!tmp) {
+                       tmp = conv_codeset_strdup(textdata,
+                               conv_get_locale_charset_str_no_utf8(), 
+                               CS_UTF_8);
+               }
+               codeconv_set_strict(FALSE);
+               if (!tmp) {
+                       tmp = conv_codeset_strdup(textdata,
+                               conv_get_locale_charset_str_no_utf8(), 
+                               CS_UTF_8);
+               }
+               if (tmp) {
+                       g_free(textdata);
+                       textdata = tmp;
+               }
+       }
 
        return textdata;        
 }
@@ -102,10 +166,20 @@ static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
        
        if (procmime_mimeinfo_parent(mimeinfo) == NULL)
                return FALSE; /* not parent */
-
-       if (mimeinfo->type != MIMETYPE_TEXT)
-               return FALSE;
        
+       if (mimeinfo->type != MIMETYPE_TEXT &&
+               (mimeinfo->type != MIMETYPE_APPLICATION ||
+                g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
+               return FALSE;
+
+       /* Seal the deal. This has to be text/plain through and through. */
+       if (mimeinfo->type == MIMETYPE_APPLICATION)
+       {
+               mimeinfo->type = MIMETYPE_TEXT;
+               g_free(mimeinfo->subtype);
+               mimeinfo->subtype = g_strdup("plain");
+       }
+
        if (mimeinfo->privacy != NULL) {
                data = (PrivacyDataPGP *) mimeinfo->privacy;
                if (data->done_sigtest)
@@ -159,13 +233,27 @@ static gint pgpinline_check_signature(MimeInfo *mimeinfo)
        
        if (!textdata) {
                g_free(textdata);
+               privacy_set_error(_("Couldn't get text data."));
                return 0;
        }
 
        /* gtk2: convert back from utf8 */
        tmp = conv_codeset_strdup(textdata, CS_UTF_8,
                        procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
+       if (!tmp) {
+               tmp = conv_codeset_strdup(textdata, CS_UTF_8,
+                       conv_get_locale_charset_str_no_utf8());
+       }
+       if (!tmp) {
+               g_warning("Can't convert charset to anything sane\n");
+               tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
+       }
        g_free(textdata);
+
+       if (!tmp) {
+               privacy_set_error(_("Couldn't convert text data to any sane charset."));
+               return 0;
+       }
        textdata = g_strdup(tmp);
        g_free(tmp);
        
@@ -233,9 +321,19 @@ static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
        if (procmime_mimeinfo_parent(mimeinfo) == NULL)
                return FALSE; /* not parent */
        
-       if (mimeinfo->type != MIMETYPE_TEXT)
+       if (mimeinfo->type != MIMETYPE_TEXT &&
+               (mimeinfo->type != MIMETYPE_APPLICATION ||
+                g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
                return FALSE;
        
+       /* Seal the deal. This has to be text/plain through and through. */
+       if (mimeinfo->type == MIMETYPE_APPLICATION)
+       {
+               mimeinfo->type = MIMETYPE_TEXT;
+               g_free(mimeinfo->subtype);
+               mimeinfo->subtype = g_strdup("plain");
+       }
+       
        textdata = get_part_as_string(mimeinfo);
        if (!textdata)
                return FALSE;
@@ -276,12 +374,14 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
            mimeinfo->type != MIMETYPE_TEXT) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't parse mime part."));
                return NULL;
        }
 
        textdata = get_part_as_string(mimeinfo);
        if (!textdata) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't get text data."));
                return NULL;
        }
 
@@ -304,6 +404,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 
        if ((dstfp = g_fopen(fname, "wb")) == NULL) {
                FILE_OP_ERROR(fname, "fopen");
+               privacy_set_error(_("Couldn't open decrypted file %s"), fname);
                g_free(fname);
                gpgme_data_release(plain);
                gpgme_release(ctx);
@@ -333,6 +434,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        
        if (parseinfo == NULL) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't scan decrypted file."));
                return NULL;
        }
        decinfo = g_node_first_child(parseinfo->node) != NULL ?
@@ -340,6 +442,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                
        if (decinfo == NULL) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't scan decrypted file parts."));
                return NULL;
        }
 
@@ -376,7 +479,9 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        gpgme_ctx_t ctx;
        gpgme_data_t gpgtext, gpgsig;
        guint len;
+       gpgme_error_t err;
        struct passphrase_cb_info_s info;
+       gpgme_sign_result_t result = NULL;
 
        memset (&info, 0, sizeof info);
 
@@ -389,19 +494,17 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        procmime_decode_content(msgcontent);
 
        fp = my_tmpfile();
+       if (fp == NULL) {
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file."));
+               return FALSE;
+       }
        procmime_write_mimeinfo(msgcontent, fp);
        rewind(fp);
 
        /* read temporary file into memory */
-       textstr = file_read_stream_to_str(fp);
+       textstr = fp_read_noconv(fp);
        
-       /* gtk2: convert back from utf8 */
-       tmp = conv_codeset_strdup(textstr, CS_UTF_8, 
-                       procmime_mimeinfo_get_parameter(msgcontent, "charset"));
-       g_free(textstr);
-       textstr = g_strdup(tmp);
-       g_free(tmp);
-
        fclose(fp);
                
        gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
@@ -412,6 +515,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 
        if (!sgpgme_setup_signers(ctx, account)) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't find private key."));
                return FALSE;
        }
 
@@ -420,14 +524,52 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
                gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
        }
 
-       if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR
+       if ((err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR)
            != GPG_ERR_NO_ERROR) {
                gpgme_release(ctx);
+               privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
+               return FALSE;
+       }
+       result = gpgme_op_sign_result(ctx);
+       if (result && result->signatures) {
+               gpgme_new_signature_t sig = result->signatures;
+               while (sig) {
+                       debug_print("valid signature: %s\n", sig->fpr);
+                       sig = sig->next;
+               }
+       } else if (result && result->invalid_signers) {
+               gpgme_invalid_key_t invalid = result->invalid_signers;
+               while (invalid) {
+                       g_warning("invalid signer: %s (%s)", invalid->fpr, 
+                               gpgme_strerror(invalid->reason));
+                       privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
+                               gpgme_strerror(invalid->reason));
+                       invalid = invalid->next;
+               }
+               gpgme_release(ctx);
+               return FALSE;
+       } else {
+               /* can't get result (maybe no signing key?) */
+               debug_print("gpgme_op_sign_result error\n");
+               privacy_set_error(_("Data signing failed, no results."));
+               gpgme_release(ctx);
                return FALSE;
        }
 
-       gpgme_release(ctx);
+
        sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
+       
+       gpgme_release(ctx);
+       
+       if (sigcontent == NULL || len <= 0) {
+               g_warning("gpgme_data_release_and_get_mem failed");
+               privacy_set_error(_("Data signing failed, no contents."));
+               gpgme_data_release(gpgtext);
+               g_free(textstr);
+               g_free(sigcontent);
+               return FALSE;
+       }
+
        tmp = g_malloc(len+1);
        g_memmove(tmp, sigcontent, len+1);
        tmp[len] = '\0';
@@ -454,7 +596,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 
 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
 {
-       return sgpgme_get_encrypt_data(recp_names);
+       return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
 }
 
 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
@@ -468,7 +610,9 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        gpgme_ctx_t ctx;
        gpgme_key_t *kset = NULL;
        gchar **fprs = g_strsplit(encrypt_data, " ", -1);
+       gpgme_error_t err;
        gint i = 0;
+
        while (fprs[i] && strlen(fprs[i])) {
                i++;
        }
@@ -479,11 +623,11 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        i = 0;
        while (fprs[i] && strlen(fprs[i])) {
                gpgme_key_t key;
-               gpgme_error_t err;
                err = gpgme_get_key(ctx, fprs[i], &key, 0);
                if (err) {
                        debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
-                       break;
+                       privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
+                       return FALSE;
                }
                debug_print("found %s at %d\n", fprs[i], i);
                kset[i] = key;
@@ -502,18 +646,17 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        procmime_decode_content(msgcontent);
 
        fp = my_tmpfile();
+       if (fp == NULL) {
+               privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
+               perror("my_tmpfile");
+               return FALSE;
+       }
        procmime_write_mimeinfo(msgcontent, fp);
        rewind(fp);
 
        /* read temporary file into memory */
-       textstr = file_read_stream_to_str(fp);
+       textstr = fp_read_noconv(fp);
        
-       /* gtk2: convert back from utf8 */
-       tmp = conv_codeset_strdup(textstr, CS_UTF_8, 
-                       procmime_mimeinfo_get_parameter(msgcontent, "charset"));
-       g_free(textstr);
-       textstr = g_strdup(tmp);
-       g_free(tmp);
        fclose(fp);
 
        /* encrypt data */
@@ -522,11 +665,19 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        gpgme_new(&ctx);
        gpgme_set_armor(ctx, 1);
 
-       gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
+       err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
 
        gpgme_release(ctx);
        enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
 
+       if (enccontent == NULL || len <= 0) {
+               g_warning("gpgme_data_release_and_get_mem failed");
+               privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
+               gpgme_data_release(gpgtext);
+               g_free(textstr);
+               return FALSE;
+       }
+
        tmp = g_malloc(len+1);
        g_memmove(tmp, enccontent, len+1);
        tmp[len] = '\0';