implement RFE 3239, 'Remove "Content-Disposition: attachment" from PGP/MIME signed...
[claws.git] / src / plugins / pgpmime / pgpmime.c
index 30d7f95f7ba74e7b8363684612ff68df1dd603af..b9a3d7dfef5ebda13768ee662e833214da36d350 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 the Claws Mail team
+ * Copyright (C) 1999-2014 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_GPGME
@@ -39,6 +40,7 @@
 #include <plugins/pgpcore/sgpgme.h>
 #include <plugins/pgpcore/prefs_gpg.h>
 #include <plugins/pgpcore/passphrase.h>
+#include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
 
@@ -90,7 +92,7 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        const gchar *protocol;
        PrivacyDataPGP *data = NULL;
        
-       g_return_val_if_fail(mimeinfo != NULL, FALSE);
+       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
        if (mimeinfo->privacy != NULL) {
                data = (PrivacyDataPGP *) mimeinfo->privacy;
                if (data->done_sigtest)
@@ -126,9 +128,10 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
                data = pgpmime_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;
 }
@@ -173,8 +176,8 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        gchar *textstr;
        gpgme_data_t sigdata = NULL, textdata = NULL;
        gpgme_error_t err;
-       g_return_val_if_fail(mimeinfo != NULL, -1);
-       g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
+       cm_return_val_if_fail(mimeinfo != NULL, -1);
+       cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
        data = (PrivacyDataPGP *) mimeinfo->privacy;
        if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
                debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
@@ -194,11 +197,12 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        parent = procmime_mimeinfo_parent(mimeinfo);
 
        fp = g_fopen(parent->data.filename, "rb");
-       g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
+       cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
        
        boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
        if (!boundary) {
                privacy_set_error(_("Signature boundary not found."));
+               fclose(fp);
                return 0;
        }
        textstr = get_canonical_content(fp, boundary);
@@ -236,12 +240,8 @@ static SignatureStatus pgpmime_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)
-               pgpmime_check_signature(mimeinfo);
-       
        return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
 }
 
@@ -249,25 +249,17 @@ static gchar *pgpmime_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)
-               pgpmime_check_signature(mimeinfo);
-       
        return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
 }
 
 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
-       g_return_val_if_fail(data != NULL, g_strdup("Error"));
 
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpmime_check_signature(mimeinfo);
-       
+       cm_return_val_if_fail(data != NULL, g_strdup("Error"));
+
        return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
 }
 
@@ -275,7 +267,10 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
 {
        MimeInfo *tmpinfo;
        const gchar *tmpstr;
-       
+       const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
+       const gchar *end_indicator = "-----END PGP MESSAGE-----";
+       gchar *textdata;
+
        if (mimeinfo->type != MIMETYPE_MULTIPART)
                return FALSE;
        if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
@@ -298,6 +293,21 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
                return FALSE;
        
+       textdata = get_part_as_string(tmpinfo);
+       if (!textdata)
+               return FALSE;
+       
+       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;
+       }
+
+       g_free(textdata);
+
        return TRUE;
 }
 
@@ -321,7 +331,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                return NULL;
        }
        
-       g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
+       cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
        
        encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
 
@@ -348,12 +358,42 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                return NULL;
        }
 
-       fprintf(dstfp, "MIME-Version: 1.0\n");
+       if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
+               FILE_OP_ERROR(fname, "fprintf");
+               fclose(dstfp);
+               privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+               g_free(fname);
+               gpgme_data_release(plain);
+               gpgme_release(ctx);
+               debug_print("can't open!\n");
+               return NULL;
+       }
 
        chars = sgpgme_data_release_and_get_mem(plain, &len);
-       if (len > 0)
-               fwrite(chars, len, 1, dstfp);
-       fclose(dstfp);
+       if (len > 0) {
+               if (fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "fwrite");
+                       g_free(chars);
+                       fclose(dstfp);
+                       privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+                       g_free(fname);
+                       gpgme_data_release(plain);
+                       gpgme_release(ctx);
+                       debug_print("can't open!\n");
+                       return NULL;
+               }
+       }
+       g_free(chars);
+
+       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);
+               debug_print("can't open!\n");
+               return NULL;
+       }
 
        parseinfo = procmime_scan_file(fname);
        g_free(fname);
@@ -382,22 +422,24 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                        data = pgpmime_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;
 }
 
-gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
+gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
 {
        MimeInfo *msgcontent, *sigmultipart, *newinfo;
-       gchar *textstr, *micalg;
+       gchar *textstr, *micalg = NULL;
        FILE *fp;
        gchar *boundary = NULL;
        gchar *sigcontent;
@@ -472,7 +514,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        gpgme_set_armor(ctx, 1);
        gpgme_signers_clear (ctx);
 
-       if (!sgpgme_setup_signers(ctx, account)) {
+       if (!sgpgme_setup_signers(ctx, account, from_addr)) {
                gpgme_release(ctx);
                return FALSE;
        }
@@ -502,8 +544,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        if (result && result->signatures) {
                gpgme_new_signature_t sig = result->signatures;
                if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
-                       micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
-                               result->signatures->hash_algo));
+                       micalg = g_strdup_printf("pgp-%s", g_ascii_strdown(gpgme_hash_algo_name(
+                               result->signatures->hash_algo),-1));
                } else {
                        micalg = g_strdup(gpgme_hash_algo_name(
                                result->signatures->hash_algo));
@@ -538,6 +580,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        if (sigcontent == NULL || len <= 0) {
                g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Data signing failed, no contents."));
+               g_free(micalg);
+               g_free(sigcontent);
                return FALSE;
        }
 
@@ -548,12 +592,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        newinfo = procmime_mimeinfo_new();
        newinfo->type = MIMETYPE_APPLICATION;
        newinfo->subtype = g_strdup("pgp-signature");
-       g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
-                            g_strdup("signature.asc"));
+       newinfo->description = g_strdup(_("OpenPGP digital signature"));
        newinfo->content = MIMECONTENT_MEM;
-       newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
-       g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
-                           g_strdup("signature.asc"));
        newinfo->data.mem = g_malloc(len + 1);
        g_memmove(newinfo->data.mem, sigcontent, len);
        newinfo->data.mem[len] = '\0';
@@ -609,6 +649,7 @@ gboolean pgpmime_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;
@@ -618,6 +659,7 @@ gboolean pgpmime_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);
@@ -646,6 +688,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        fp = my_tmpfile();
        if (fp == NULL) {
                privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
+               g_free(kset);
                return FALSE;
        }
        procmime_write_mimeinfo(encmultipart, fp);
@@ -660,18 +703,20 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
        gpgme_data_new(&gpgenc);
        gpgme_set_armor(ctx, 1);
-       gpgme_data_rewind(gpgtext);
+       cm_gpgme_data_rewind(gpgtext);
        
        err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
 
        enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
        gpgme_data_release(gpgtext);
        g_free(textstr);
+       g_free(kset);
 
        if (enccontent == NULL || len <= 0) {
                g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
                gpgme_release(ctx);
+               g_free(enccontent);
                return FALSE;
        }
 
@@ -725,6 +770,7 @@ static PrivacySystem pgpmime_system = {
        pgpmime_encrypt,
        pgpmime_get_encrypt_warning,
        pgpmime_inhibit_encrypt_warning,
+       prefs_gpg_auto_check_signatures,
 };
 
 void pgpmime_init()