replace deprecated g_memmove with memmove
[claws.git] / src / plugins / pgpmime / pgpmime.c
index c97834d7ceca6d0194114dea8f95c24342457540..595fa92e9cc5e10250449e154e15e7de1fbbf7e7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2014 the Claws Mail team
+ * Copyright (C) 1999-2016 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
@@ -14,7 +14,6 @@
  *
  * 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
@@ -43,6 +42,7 @@
 #include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
+#include "file-utils.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -71,7 +71,7 @@ static PrivacyDataPGP *pgpmime_new_privacydata()
        data->is_signed = FALSE;
        data->sigstatus = NULL;
        if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
-               g_warning(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               g_warning("Couldn't initialize GPG context: %s", gpgme_strerror(err));
                return NULL;
        }
        
@@ -144,12 +144,12 @@ static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
        gchar buf[BUFFSIZE];
 
        boundary_len = strlen(boundary);
-       while (fgets(buf, sizeof(buf), fp) != NULL)
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL)
                if (IS_BOUNDARY(buf, boundary, boundary_len))
                        break;
 
        textbuffer = g_string_new("");
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                gchar *buf2;
 
                if (IS_BOUNDARY(buf, boundary, boundary_len))
@@ -180,7 +180,7 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        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));
+               debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return 0;
        }
@@ -196,13 +196,13 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        }
        parent = procmime_mimeinfo_parent(mimeinfo);
 
-       fp = g_fopen(parent->data.filename, "rb");
+       fp = claws_fopen(parent->data.filename, "rb");
        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);
+               claws_fclose(fp);
                return 0;
        }
        textstr = get_canonical_content(fp, boundary);
@@ -231,7 +231,7 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        gpgme_data_release(sigdata);
        gpgme_data_release(textdata);
        g_free(textstr);
-       fclose(fp);
+       claws_fclose(fp);
        
        return 0;
 }
@@ -293,7 +293,7 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
                return FALSE;
        
-       textdata = get_part_as_string(tmpinfo);
+       textdata = procmime_get_part_as_string(tmpinfo, TRUE);
        if (!textdata)
                return FALSE;
        
@@ -326,7 +326,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        gpgme_error_t err;
 
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return NULL;
        }
@@ -348,8 +348,8 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        fname = g_strdup_printf("%s%cplaintext.%08x",
                get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-       if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-               FILE_OP_ERROR(fname, "fopen");
+       if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+               FILE_OP_ERROR(fname, "claws_fopen");
                privacy_set_error(_("Couldn't open decrypted file %s"), fname);
                g_free(fname);
                gpgme_data_release(plain);
@@ -360,7 +360,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 
        if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
                FILE_OP_ERROR(fname, "fprintf");
-               fclose(dstfp);
+               claws_fclose(dstfp);
                privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
                g_free(fname);
                gpgme_data_release(plain);
@@ -371,10 +371,10 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 
        chars = sgpgme_data_release_and_get_mem(plain, &len);
        if (len > 0) {
-               if (fwrite(chars, 1, len, dstfp) < len) {
-                       FILE_OP_ERROR(fname, "fwrite");
+               if (claws_fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "claws_fwrite");
                        g_free(chars);
-                       fclose(dstfp);
+                       claws_fclose(dstfp);
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
                        g_free(fname);
                        gpgme_data_release(plain);
@@ -385,8 +385,8 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        }
        g_free(chars);
 
-       if (fclose(dstfp) == EOF) {
-               FILE_OP_ERROR(fname, "fclose");
+       if (claws_safe_fclose(dstfp) == EOF) {
+               FILE_OP_ERROR(fname, "claws_fclose");
                privacy_set_error(_("Couldn't close decrypted file %s"), fname);
                g_free(fname);
                gpgme_data_release(plain);
@@ -411,7 +411,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        }
 
        g_node_unlink(decinfo->node);
-       procmime_mimeinfo_free_all(parseinfo);
+       procmime_mimeinfo_free_all(&parseinfo);
 
        decinfo->tmp = TRUE;
 
@@ -453,7 +453,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        
        fp = my_tmpfile();
        if (fp == NULL) {
-               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
                return FALSE;
        }
        procmime_write_mimeinfo(mimeinfo, fp);
@@ -461,7 +462,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
 
        /* read temporary file into memory */
        test_msg = file_read_stream_to_str(fp);
-       fclose(fp);
+       claws_fclose(fp);
        
        memset (&info, 0, sizeof info);
 
@@ -492,7 +493,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        fp = my_tmpfile();
        if (fp == NULL) {
                perror("my_tmpfile");
-               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
                return FALSE;
        }
        procmime_write_mimeinfo(sigmultipart, fp);
@@ -501,12 +502,13 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        /* read temporary file into memory */
        textstr = get_canonical_content(fp, boundary);
 
-       fclose(fp);
+       g_free(boundary);
+       claws_fclose(fp);
 
        gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
        gpgme_data_new(&gpgsig);
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return FALSE;
        }
@@ -520,7 +522,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        }
 
        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) {
                debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
        } else {
                info.c = ctx;
@@ -544,8 +546,10 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        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", g_ascii_strdown(gpgme_hash_algo_name(
-                               result->signatures->hash_algo),-1));
+                       gchar *down_algo = g_ascii_strdown(gpgme_hash_algo_name(
+                               result->signatures->hash_algo), -1);
+                       micalg = g_strdup_printf("pgp-%s", down_algo);
+                       g_free(down_algo);
                } else {
                        micalg = g_strdup(gpgme_hash_algo_name(
                                result->signatures->hash_algo));
@@ -592,15 +596,12 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        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);
+       memmove(newinfo->data.mem, sigcontent, len);
        newinfo->data.mem[len] = '\0';
+       newinfo->tmp = TRUE;
        g_node_append(sigmultipart->node, newinfo->node);
 
        g_free(sigcontent);
@@ -651,7 +652,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
        memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                g_free(kset);
                return FALSE;
@@ -691,7 +692,8 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        /* write message content to temporary file */
        fp = my_tmpfile();
        if (fp == NULL) {
-               privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file, %s"), g_strerror(errno));
                g_free(kset);
                return FALSE;
        }
@@ -701,7 +703,8 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        /* read temporary file into memory */
        textstr = get_canonical_content(fp, boundary);
 
-       fclose(fp);
+       g_free(boundary);
+       claws_fclose(fp);
 
        /* encrypt data */
        gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
@@ -726,7 +729,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
        /* create encrypted multipart */
        g_node_unlink(msgcontent->node);
-       procmime_mimeinfo_free_all(msgcontent);
+       procmime_mimeinfo_free_all(&msgcontent);
        g_node_append(mimeinfo->node, encmultipart->node);
 
        newinfo = procmime_mimeinfo_new();
@@ -734,6 +737,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        newinfo->subtype = g_strdup("pgp-encrypted");
        newinfo->content = MIMECONTENT_MEM;
        newinfo->data.mem = g_strdup("Version: 1\n");
+       newinfo->tmp = TRUE;
        g_node_append(encmultipart->node, newinfo->node);
 
        newinfo = procmime_mimeinfo_new();
@@ -741,7 +745,8 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        newinfo->subtype = g_strdup("octet-stream");
        newinfo->content = MIMECONTENT_MEM;
        newinfo->data.mem = g_malloc(len + 1);
-       g_memmove(newinfo->data.mem, enccontent, len);
+       newinfo->tmp = TRUE;
+       memmove(newinfo->data.mem, enccontent, len);
        newinfo->data.mem[len] = '\0';
        g_node_append(encmultipart->node, newinfo->node);