2011-01-06 [colin] 3.7.8cvs30
[claws.git] / src / plugins / pgpinline / pgpinline.c
index 3c171d770017e6842c8dce8d20ac6882710778bb..52df2610fbc673bcaac340c69c4184af1378f731 100644 (file)
@@ -1,11 +1,11 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and 
+ * Copyright (C) 1999-2009 Colin Leroy <colin@colino.net> 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -14,8 +14,8 @@
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -119,18 +119,29 @@ static gchar *fp_read_noconv(FILE *fp)
 static gchar *get_part_as_string(MimeInfo *mimeinfo)
 {
        gchar *textdata = NULL;
-       gchar *real_data = NULL;
-       g_return_val_if_fail(mimeinfo != NULL, 0);
+       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 {
-               /* equals file_read_to_str but without conversion */
-               FILE *fp = fopen(mimeinfo->data.filename, "r");
-               if (!fp)
+               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)) {
@@ -157,15 +168,7 @@ static gchar *get_part_as_string(MimeInfo *mimeinfo)
                        textdata = tmp;
                }
        }
-       if (textdata && mimeinfo->offset && 
-           mimeinfo->offset+ mimeinfo->length < strlen(textdata)) {
-               real_data = g_strdup(textdata + mimeinfo->offset);
-               real_data[mimeinfo->length] = '\0';
-               g_free(textdata);
-               textdata = real_data;
-       } else if (textdata && mimeinfo->offset) {
-               debug_print("got data shorter than what it should be\n");
-       }
+
        return textdata;        
 }
 
@@ -175,7 +178,7 @@ static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
        const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
        gchar *textdata, *sigpos;
        
-       g_return_val_if_fail(mimeinfo != NULL, FALSE);
+       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
        
        if (procmime_mimeinfo_parent(mimeinfo) == NULL)
                return FALSE; /* not parent */
@@ -232,18 +235,22 @@ static gint pgpinline_check_signature(MimeInfo *mimeinfo)
        gpgme_data_t plain = NULL, cipher = NULL;
        gpgme_error_t err;
 
-       g_return_val_if_fail(mimeinfo != NULL, 0);
+       cm_return_val_if_fail(mimeinfo != NULL, 0);
 
-       if (procmime_mimeinfo_parent(mimeinfo) == NULL)
+       if (procmime_mimeinfo_parent(mimeinfo) == NULL) {
+               privacy_set_error(_("Incorrect part"));
                return 0; /* not parent */
-       if (mimeinfo->type != MIMETYPE_TEXT)
+       }
+       if (mimeinfo->type != MIMETYPE_TEXT) {
+               privacy_set_error(_("Not a text part"));
+               debug_print("type %d\n", mimeinfo->type);
                return 0;
-
-       g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
+       }
+       cm_return_val_if_fail(mimeinfo->privacy != NULL, 0);
        data = (PrivacyDataPGP *) mimeinfo->privacy;
 
        textdata = get_part_as_string(mimeinfo);
-       
+
        if (!textdata) {
                g_free(textdata);
                privacy_set_error(_("Couldn't get text data."));
@@ -273,6 +280,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);
@@ -295,7 +303,7 @@ static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
        
-       g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
+       cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
 
        if (data->sigstatus == NULL && 
            prefs_gpg_get_config()->auto_check_signatures)
@@ -308,7 +316,7 @@ static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
        
-       g_return_val_if_fail(data != NULL, g_strdup("Error"));
+       cm_return_val_if_fail(data != NULL, g_strdup("Error"));
 
        if (data->sigstatus == NULL && 
            prefs_gpg_get_config()->auto_check_signatures)
@@ -321,7 +329,7 @@ static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
        
-       g_return_val_if_fail(data != NULL, g_strdup("Error"));
+       cm_return_val_if_fail(data != NULL, g_strdup("Error"));
 
        return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
 }
@@ -333,7 +341,7 @@ static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
        const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
        gchar *textdata;
        
-       g_return_val_if_fail(mimeinfo != NULL, FALSE);
+       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
        
        if (procmime_mimeinfo_parent(mimeinfo) == NULL)
                return FALSE; /* not parent */
@@ -379,6 +387,9 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        gpgme_ctx_t ctx;
        gchar *chars;
        size_t len;
+       const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
+       const gchar *end_indicator = "-----END PGP MESSAGE-----";
+       gchar *pos;
        
        if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
                return NULL;
@@ -386,8 +397,8 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        gpgme_set_textmode(ctx, 1);
        gpgme_set_armor(ctx, 1);
 
-       g_return_val_if_fail(mimeinfo != NULL, NULL);
-       g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+       cm_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
        
        if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
            mimeinfo->type != MIMETYPE_TEXT) {
@@ -433,20 +444,70 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        if (src_codeset == NULL)
                src_codeset = CS_ISO_8859_1;
                
-       fprintf(dstfp, "MIME-Version: 1.0\r\n"
+       if (fprintf(dstfp, "MIME-Version: 1.0\r\n"
                        "Content-Type: text/plain; charset=%s\r\n"
                        "Content-Transfer-Encoding: 8bit\r\n"
                        "\r\n",
-                       src_codeset);
+                       src_codeset) < 0) {
+               FILE_OP_ERROR(fname, "fprintf");
+               privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+               goto FILE_ERROR;
+       }
+
+       /* Store any part before encrypted text */
+       pos = strstr(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);
+               goto FILE_ERROR;
+           }
+       }
        
+       if (fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
+               strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
+               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);
+               goto FILE_ERROR;
+       }
        chars = sgpgme_data_release_and_get_mem(plain, &len);
-       if (len > 0)
-               fwrite(chars, len, 1, dstfp);
+       if (len > 0) {
+               if (fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "fwrite");
+                       privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+                       goto FILE_ERROR;
+               }
+       }
+       /* 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);
+                       goto FILE_ERROR;
+       }
+       if (pos != NULL) {
+           pos = strstr(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);
+                       goto FILE_ERROR;
+               }
+           }
+       }
 
-       fclose(dstfp);
+       if (fclose(dstfp) == EOF) {
+               FILE_OP_ERROR(fname, "fclose");
+               privacy_set_error(_("Couldn't close decrypted file %s"), fname);
+               g_free(fname);
+               gpgme_data_release(plain);
+               gpgme_release(ctx);
+               return NULL;
+       }
        
-       gpgme_data_release(plain);
-
        parseinfo = procmime_scan_file(fname);
        g_free(fname);
        
@@ -486,9 +547,16 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                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)
+static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
 {
        MimeInfo *msgcontent;
        gchar *textstr, *tmp;
@@ -505,9 +573,14 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 
        /* 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);
 
@@ -535,7 +608,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        gpgme_set_textmode(ctx, 1);
        gpgme_set_armor(ctx, 1);
 
-       if (!sgpgme_setup_signers(ctx, account)) {
+       if (!sgpgme_setup_signers(ctx, account, from_addr)) {
                gpgme_release(ctx);
                return FALSE;
        }
@@ -608,7 +681,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        if (msgcontent->content == MIMECONTENT_FILE &&
            msgcontent->data.filename != NULL) {
                if (msgcontent->tmp == TRUE)
-                       g_unlink(msgcontent->data.filename);
+                       claws_unlink(msgcontent->data.filename);
                g_free(msgcontent->data.filename);
        }
        msgcontent->data.mem = g_strdup(tmp);
@@ -629,6 +702,23 @@ static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
        return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
 }
 
+static const gchar *pgpinline_get_encrypt_warning(void)
+{
+       if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
+               return NULL;
+       else
+               return _("Please note that attachments are not encrypted by "
+                "the PGP/Inline system, nor are email headers, like Subject.");
+}
+
+static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
+{
+       if (inhibit)
+               prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
+       else
+               prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
+}
+
 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 {
        MimeInfo *msgcontent;
@@ -652,6 +742,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;
@@ -661,6 +752,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);
@@ -673,9 +765,15 @@ 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);
 
@@ -683,6 +781,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        if (fp == NULL) {
                privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
                perror("my_tmpfile");
+               g_free(kset);
                return FALSE;
        }
        procmime_write_mimeinfo(msgcontent, fp);
@@ -699,6 +798,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);
@@ -706,6 +806,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");
@@ -727,7 +828,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        if (msgcontent->content == MIMECONTENT_FILE &&
            msgcontent->data.filename != NULL) {
                if (msgcontent->tmp == TRUE)
-                       g_unlink(msgcontent->data.filename);
+                       claws_unlink(msgcontent->data.filename);
                g_free(msgcontent->data.filename);
        }
        msgcontent->data.mem = g_strdup(tmp);
@@ -759,6 +860,8 @@ static PrivacySystem pgpinline_system = {
        TRUE,
        pgpinline_get_encrypt_data,
        pgpinline_encrypt,
+       pgpinline_get_encrypt_warning,
+       pgpinline_inhibit_encrypt_warning,
 };
 
 void pgpinline_init()