Cosmetic: use always g_getenv instead of getenv
[claws.git] / src / plugins / pgpinline / pgpinline.c
index f4784bb58a8761cbbffaff328dc4e8253c1dbb0d..7dc15027fb499efcd9ec2937a21afd7042b1e4e4 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Colin Leroy <colin@colino.net> and 
- * the Claws Mail team
+ * Copyright (C) 1999-2015 Colin Leroy and the Claws Mail team
  *
  * 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_GPGME
@@ -37,8 +36,8 @@
 #include <plugins/pgpcore/sgpgme.h>
 #include <plugins/pgpcore/prefs_gpg.h>
 #include <plugins/pgpcore/passphrase.h>
+#include <plugins/pgpcore/pgp_utils.h>
 #include "quoted-printable.h"
-#include "base64.h"
 #include "codeconv.h"
 #include "plugin.h"
 
@@ -85,93 +84,6 @@ 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;
-       gchar *filename = NULL;
-       FILE *fp;
-
-       cm_return_val_if_fail(mimeinfo != NULL, 0);
-       procmime_decode_content(mimeinfo);
-       
-       if (mimeinfo->content == MIMECONTENT_MEM)
-               textdata = g_strdup(mimeinfo->data.mem);
-       else {
-               filename = procmime_get_tmp_file_name(mimeinfo);
-               if (procmime_get_part(filename, mimeinfo) < 0) {
-                       printf("error dumping file\n");
-                       return NULL;
-               }
-               fp = g_fopen(filename,"rb");
-               if (!fp) {
-                       printf("error reading file\n");
-                       return NULL;
-               }
-               textdata = fp_read_noconv(fp);
-               fclose(fp);
-               g_unlink(filename);
-               g_free(filename);
-       }
-
-       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;        
-}
-
 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = NULL;
@@ -222,8 +134,10 @@ static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
                data = pgpinline_new_privacydata();
                mimeinfo->privacy = (PrivacyData *) data;
        }
-       data->done_sigtest = TRUE;
-       data->is_signed = TRUE;
+       if (data != NULL) {
+               data->done_sigtest = TRUE;
+               data->is_signed = TRUE;
+       }
 
        return TRUE;
 }
@@ -265,7 +179,7 @@ static gint pgpinline_check_signature(MimeInfo *mimeinfo)
                        conv_get_locale_charset_str_no_utf8());
        }
        if (!tmp) {
-               g_warning("Can't convert charset to anything sane\n");
+               g_warning("Can't convert charset to anything sane");
                tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
        }
        g_free(textdata);
@@ -280,6 +194,7 @@ static gint pgpinline_check_signature(MimeInfo *mimeinfo)
        if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
                debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               g_free(textdata);
                return 0;
        }
        gpgme_set_textmode(data->ctx, 1);
@@ -301,12 +216,8 @@ static gint pgpinline_check_signature(MimeInfo *mimeinfo)
 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
-       cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
 
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpinline_check_signature(mimeinfo);
+       cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
 
        return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
 }
@@ -314,13 +225,9 @@ static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
+
        cm_return_val_if_fail(data != NULL, g_strdup("Error"));
 
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpinline_check_signature(mimeinfo);
-       
        return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
 }
 
@@ -333,11 +240,10 @@ static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
        return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
 }
 
-
-
 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
 {
-       const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
+       const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
+       const gchar *end_indicator = "-----END PGP MESSAGE-----";
        gchar *textdata;
        
        cm_return_val_if_fail(mimeinfo != NULL, FALSE);
@@ -357,12 +263,16 @@ static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
                g_free(mimeinfo->subtype);
                mimeinfo->subtype = g_strdup("plain");
        }
-       
+
        textdata = get_part_as_string(mimeinfo);
        if (!textdata)
                return FALSE;
-       
-       if (!strstr(textdata, enc_indicator)) {
+
+       if (!pgp_locate_armor_header(textdata, begin_indicator)) {
+               g_free(textdata);
+               return FALSE;
+       }
+       if (!pgp_locate_armor_header(textdata, end_indicator)) {
                g_free(textdata);
                return FALSE;
        }
@@ -450,22 +360,16 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                        src_codeset) < 0) {
                FILE_OP_ERROR(fname, "fprintf");
                privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
-               return NULL;
+               goto FILE_ERROR;
        }
 
        /* Store any part before encrypted text */
-       pos = strstr(textdata, begin_indicator);
+       pos = pgp_locate_armor_header(textdata, begin_indicator);
        if (pos != NULL && (pos - textdata) > 0) {
            if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
                FILE_OP_ERROR(fname, "fwrite");
                privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
-               return NULL;
+               goto FILE_ERROR;
            }
        }
        
@@ -474,44 +378,34 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
                FILE_OP_ERROR(fname, "fwrite");
                privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
-               return NULL;
+               goto FILE_ERROR;
        }
        chars = sgpgme_data_release_and_get_mem(plain, &len);
        if (len > 0) {
                if (fwrite(chars, 1, len, dstfp) < len) {
                        FILE_OP_ERROR(fname, "fwrite");
+                       g_free(chars);
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-                       g_free(fname);
-                       gpgme_data_release(plain);
-                       gpgme_release(ctx);
-                       return NULL;
+                       goto FILE_ERROR;
                }
        }
+       g_free(chars);
        /* Store any part after encrypted text */
        if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
                strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
                dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
                        FILE_OP_ERROR(fname, "fwrite");
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-                       g_free(fname);
-                       gpgme_data_release(plain);
-                       gpgme_release(ctx);
-                       return NULL;
+                       goto FILE_ERROR;
        }
        if (pos != NULL) {
-           pos = strstr(pos, end_indicator);
+           pos = pgp_locate_armor_header(pos, end_indicator);
            if (pos != NULL && *pos != '\0') {
                pos += strlen(end_indicator);
                if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
                        FILE_OP_ERROR(fname, "fwrite");
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-                       g_free(fname);
-                       gpgme_data_release(plain);
-                       gpgme_release(ctx);
-                       return NULL;
+                       goto FILE_ERROR;
                }
            }
        }
@@ -554,16 +448,25 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                        data = pgpinline_new_privacydata();
                        decinfo->privacy = (PrivacyData *) data;        
                }
-               data->done_sigtest = TRUE;
-               data->is_signed = TRUE;
-               data->sigstatus = sigstat;
-               if (data->ctx)
-                       gpgme_release(data->ctx);
-               data->ctx = ctx;
+               if (data != NULL) {
+                       data->done_sigtest = TRUE;
+                       data->is_signed = TRUE;
+                       data->sigstatus = sigstat;
+                       if (data->ctx)
+                               gpgme_release(data->ctx);
+                       data->ctx = ctx;
+               }
        } else
                gpgme_release(ctx);
 
        return decinfo;
+
+FILE_ERROR:
+       fclose(dstfp);
+       g_free(fname);
+       gpgme_data_release(plain);
+       gpgme_release(ctx);
+       return NULL;
 }
 
 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
@@ -583,9 +486,14 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const
 
        /* get content node from message */
        msgcontent = (MimeInfo *) mimeinfo->node->children->data;
-       if (msgcontent->type == MIMETYPE_MULTIPART)
+       if (msgcontent->type == MIMETYPE_MULTIPART) {
+               if (!msgcontent->node->children) {
+                       debug_print("msgcontent->node->children NULL, bailing\n");
+                       privacy_set_error(_("Malformed message"));
+                       return FALSE;
+               }
                msgcontent = (MimeInfo *) msgcontent->node->children->data;
-
+       }
        /* get rid of quoted-printable or anything */
        procmime_decode_content(msgcontent);
 
@@ -619,7 +527,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const
        }
 
        prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
-       if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
+       if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
                info.c = ctx;
                gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
        }
@@ -747,6 +655,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
                debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               g_free(kset);
                return FALSE;
        }
        i = 0;
@@ -756,6 +665,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
                if (err) {
                        debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
                        privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
+                       g_free(kset);
                        return FALSE;
                }
                debug_print("found %s at %d\n", fprs[i], i);
@@ -768,16 +678,23 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
        /* get content node from message */
        msgcontent = (MimeInfo *) mimeinfo->node->children->data;
-       if (msgcontent->type == MIMETYPE_MULTIPART)
+       if (msgcontent->type == MIMETYPE_MULTIPART) {
+               if (!msgcontent->node->children) {
+                       debug_print("msgcontent->node->children NULL, bailing\n");
+                       privacy_set_error(_("Malformed message"));
+                       g_free(kset);
+                       return FALSE;
+               }
                msgcontent = (MimeInfo *) msgcontent->node->children->data;
-
+       }
        /* get rid of quoted-printable or anything */
        procmime_decode_content(msgcontent);
 
        fp = my_tmpfile();
        if (fp == NULL) {
-               privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
+               privacy_set_error(_("Couldn't create temporary file, %s"), g_strerror(errno));
                perror("my_tmpfile");
+               g_free(kset);
                return FALSE;
        }
        procmime_write_mimeinfo(msgcontent, fp);
@@ -794,6 +711,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
                debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               g_free(kset);
                return FALSE;
        }
        gpgme_set_armor(ctx, 1);
@@ -801,6 +719,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
 
        enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
+       g_free(kset);
 
        if (enccontent == NULL || len <= 0) {
                g_warning("sgpgme_data_release_and_get_mem failed");
@@ -808,6 +727,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
                gpgme_data_release(gpgtext);
                g_free(textstr);
                gpgme_release(ctx);
+               g_free(enccontent);
                return FALSE;
        }
 
@@ -856,6 +776,7 @@ static PrivacySystem pgpinline_system = {
        pgpinline_encrypt,
        pgpinline_get_encrypt_warning,
        pgpinline_inhibit_encrypt_warning,
+       prefs_gpg_auto_check_signatures,
 };
 
 void pgpinline_init()