replace deprecated g_memmove with memmove
[claws.git] / src / plugins / smime / smime.c
index 5f7068b5133311c5f90e54c04b41c3cc3651a121..0d83259384fdcbec8932c87ccb85a05bc1b1ab82 100644 (file)
@@ -1,7 +1,6 @@
 /* 
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
- * the Claws Mail team
+ * Copyright (C) 1999-2016 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
@@ -14,8 +13,7 @@
  * 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
@@ -45,6 +43,7 @@
 #include "prefs_common.h"
 #include "procmime.h"
 #include "plugin.h"
+#include "file-utils.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -68,7 +67,7 @@ static PrivacyDataPGP *smime_new_privacydata()
        gpgme_ctx_t     ctx;
 
        if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR) {
-               debug_print("gpgme_new failed");
+               debug_print("gpgme_new failed\n");
                return NULL;
        }
 
@@ -172,13 +171,13 @@ static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
 
        if (boundary) {
                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 (boundary && IS_BOUNDARY(buf, boundary, boundary_len))
@@ -231,7 +230,7 @@ static gint smime_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");
@@ -279,7 +278,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                        g_free(textstr);
                        cm_gpgme_data_rewind(cipher);
                        textstr = sgpgme_data_release_and_get_mem(cipher, &len);
-                       fclose(fp);
+                       claws_fclose(fp);
                        if (textstr && len > 0)
                                textstr[len-1]='\0';
 
@@ -287,16 +286,18 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                gchar *tmp_file = get_tmp_file();
                                MimeInfo *newinfo = NULL, *decinfo = NULL, *parentinfo = NULL;
 
-                               str_write_to_file(textstr, tmp_file);
+                               str_write_to_file(textstr, tmp_file, TRUE);
                                newinfo = procmime_scan_file(tmp_file);
                                decinfo = g_node_first_child(newinfo->node) != NULL ?
                                        g_node_first_child(newinfo->node)->data : NULL;
 
-                               if (decinfo == NULL)
+                               if (decinfo == NULL) {
+                                       g_free(textstr);
                                        return -1;
+                               }
 
                                g_node_unlink(decinfo->node);
-                               procmime_mimeinfo_free_all(newinfo);
+                               procmime_mimeinfo_free_all(&newinfo);
                                decinfo->tmp = TRUE;
                                parentinfo = procmime_mimeinfo_parent(mimeinfo);
 
@@ -313,9 +314,11 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                        }
                                }
                                g_node_prepend(parentinfo->node, decinfo->node);
+                               g_free(textstr);
                                return 0;
                        } else {
-                               g_free(textstr);
+                               if (textstr)
+                                       g_free(textstr);
                                return -1;
                        }
                }
@@ -340,7 +343,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
        gpgme_data_release(sigdata);
        gpgme_data_release(textdata);
        g_free(textstr);
-       fclose(fp);
+       claws_fclose(fp);
        
        return 0;
 }
@@ -442,8 +445,8 @@ static MimeInfo *smime_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, "g_fopen");
+       if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+               FILE_OP_ERROR(fname, "claws_fopen");
                g_free(fname);
                gpgme_data_release(plain);
                gpgme_release(ctx);
@@ -455,7 +458,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
        if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
                FILE_OP_ERROR(fname, "fprintf");
                g_free(fname);
-               fclose(dstfp);
+               claws_fclose(dstfp);
                gpgme_data_release(plain);
                gpgme_release(ctx);
                debug_print("can't close!\n");
@@ -466,9 +469,9 @@ static MimeInfo *smime_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");
-                       fclose(dstfp);
+               if (claws_fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "claws_fwrite");
+                       claws_fclose(dstfp);
                        g_free(fname);
                        g_free(chars);
                        gpgme_data_release(plain);
@@ -478,8 +481,8 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
                        return NULL;
                }
        }
-       if (fclose(dstfp) == EOF) {
-               FILE_OP_ERROR(fname, "fclose");
+       if (claws_safe_fclose(dstfp) == EOF) {
+               FILE_OP_ERROR(fname, "claws_fclose");
                g_free(fname);
                        g_free(chars);
                gpgme_data_release(plain);
@@ -506,7 +509,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
        }
 
        g_node_unlink(decinfo->node);
-       procmime_mimeinfo_free_all(parseinfo);
+       procmime_mimeinfo_free_all(&parseinfo);
 
        decinfo->tmp = TRUE;
 
@@ -561,7 +564,7 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
 
        /* read temporary file into memory */
        test_msg = file_read_stream_to_str(fp);
-       fclose(fp);
+       claws_fclose(fp);
        
        memset (&info, 0, sizeof info);
 
@@ -603,7 +606,7 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
 
        g_free(boundary);
 
-       fclose(fp);
+       claws_fclose(fp);
 
        gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
        gpgme_data_new(&gpgsig);
@@ -688,7 +691,8 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
        g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
                            g_strdup("smime.p7s"));
        newinfo->data.mem = g_malloc(len + 1);
-       g_memmove(newinfo->data.mem, real_content, len);
+       newinfo->tmp = TRUE;
+       memmove(newinfo->data.mem, real_content, len);
        newinfo->data.mem[len] = '\0';
        newinfo->encoding_type = ENC_BASE64;
        g_node_append(sigmultipart->node, newinfo->node);
@@ -793,29 +797,29 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
        /* write message content to temporary file */
        tmpfile = get_tmp_file();
-       fp = g_fopen(tmpfile, "wb");
+       fp = claws_fopen(tmpfile, "wb");
        if (fp == NULL) {
-               perror("get_tmp_file");
+               FILE_OP_ERROR(tmpfile, "create");
                g_free(kset);
                return FALSE;
        }
        procmime_decode_content(msgcontent);
        procmime_write_mime_header(msgcontent, fp);
        procmime_write_mimeinfo(msgcontent, fp);
-       fclose(fp);
+       claws_safe_fclose(fp);
        canonicalize_file_replace(tmpfile);
-       fp = g_fopen(tmpfile, "rb");
+       fp = claws_fopen(tmpfile, "rb");
        if (fp == NULL) {
-               perror("get_tmp_file");
+               FILE_OP_ERROR(tmpfile, "open");
                g_free(kset);
                return FALSE;
        }
        g_free(tmpfile);
 
        /* read temporary file into memory */
-       textstr = fp_read_noconv(fp);
+       textstr = file_read_stream_to_str_no_recode(fp);
 
-       fclose(fp);
+       claws_fclose(fp);
 
        /* encrypt data */
        gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
@@ -829,28 +833,30 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
 
        if (!enccontent) {
-               g_warning("no enccontent\n");
+               g_warning("no enccontent");
                return FALSE;
        }
 
        tmpfile = get_tmp_file();
-       fp = g_fopen(tmpfile, "wb");
+       fp = claws_fopen(tmpfile, "wb");
        if (fp) {
-               if (fwrite(enccontent, 1, len, fp) < len) {
-                       FILE_OP_ERROR(tmpfile, "fwrite");
-                       fclose(fp);
+               if (claws_fwrite(enccontent, 1, len, fp) < len) {
+                       FILE_OP_ERROR(tmpfile, "claws_fwrite");
+                       claws_fclose(fp);
                        claws_unlink(tmpfile);
                        g_free(tmpfile);
+                       g_free(enccontent);
                        return FALSE;
                }
-               if (fclose(fp) == EOF) {
-                       FILE_OP_ERROR(tmpfile, "fclose");
+               if (claws_safe_fclose(fp) == EOF) {
+                       FILE_OP_ERROR(tmpfile, "claws_fclose");
                        claws_unlink(tmpfile);
                        g_free(tmpfile);
+                       g_free(enccontent);
                        return FALSE;
                }
        } else {
-               perror("get_tmp_file");
+               FILE_OP_ERROR(tmpfile, "create");
                g_free(tmpfile);
                g_free(enccontent);
                return FALSE;
@@ -859,11 +865,12 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        g_free(textstr);
 
        /* create encrypted multipart */
-       procmime_mimeinfo_free_all(msgcontent);
+       procmime_mimeinfo_free_all(&msgcontent);
        g_node_append(mimeinfo->node, encmultipart->node);
 
        encmultipart->content = MIMECONTENT_FILE;
        encmultipart->data.filename = tmpfile;
+       encmultipart->tmp = TRUE;
        procmime_encode_content(encmultipart, ENC_BASE64);
 
        g_free(enccontent);