Fix a leak, CID #1220437.
[claws.git] / src / plugins / smime / smime.c
index 465704504a782e5cc4f849d9b63f08a107f427ec..823ba373216bd0d95281f8f6a830ce85311a2020 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
@@ -65,13 +63,19 @@ static gint smime_check_signature(MimeInfo *mimeinfo);
 static PrivacyDataPGP *smime_new_privacydata()
 {
        PrivacyDataPGP *data;
+       gpgme_ctx_t     ctx;
+
+       if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR) {
+               debug_print("gpgme_new failed\n");
+               return NULL;
+       }
 
        data = g_new0(PrivacyDataPGP, 1);
        data->data.system = &smime_system;
        data->done_sigtest = FALSE;
        data->is_signed = FALSE;
        data->sigstatus = NULL;
-       gpgme_new(&data->ctx);
+       data->ctx = ctx;
        
        return data;
 }
@@ -103,6 +107,8 @@ static gboolean smime_is_signed(MimeInfo *mimeinfo)
                if (tmpstr && !g_ascii_strcasecmp(tmpstr, "signed-data")) {
                        if (data == NULL) {
                                data = smime_new_privacydata();
+                               if (!data)
+                                       return FALSE;
                                mimeinfo->privacy = (PrivacyData *) data;
                        }
 
@@ -144,6 +150,8 @@ static gboolean smime_is_signed(MimeInfo *mimeinfo)
 
        if (data == NULL) {
                data = smime_new_privacydata();
+               if (!data)
+                       return FALSE;
                mimeinfo->privacy = (PrivacyData *) data;
        }
        
@@ -196,12 +204,21 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
        const gchar *tmpstr;
        gpgme_data_t sigdata = NULL, textdata = NULL;
        gpgme_error_t err;
+       EncodingType oldenc = ENC_BINARY;
+
        cm_return_val_if_fail(mimeinfo != NULL, -1);
        cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
+
        data = (PrivacyDataPGP *) mimeinfo->privacy;
-       gpgme_new(&data->ctx);
-       EncodingType oldenc = ENC_BINARY;
-       
+
+       if (!data->ctx) {
+               if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
+                       debug_print("gpgme_new failed: %s\n",
+                               gpgme_strerror(err));
+                       return -1;
+               }
+       }
+
        debug_print("Checking S/MIME signature\n");
 
        err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_CMS);
@@ -273,11 +290,13 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                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);
 
@@ -296,6 +315,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                g_node_prepend(parentinfo->node, decinfo->node);
                                return 0;
                        } else {
+                               g_free(textstr);
                                return -1;
                        }
                }
@@ -486,7 +506,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;
 
@@ -495,6 +515,10 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
                        data = (PrivacyDataPGP *) decinfo->privacy;
                } else {
                        data = smime_new_privacydata();
+                       if (!data) {
+                               gpgme_release(ctx);
+                               return NULL;
+                       }
                        decinfo->privacy = (PrivacyData *) data;        
                }
                data->done_sigtest = TRUE;
@@ -618,8 +642,10 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
        result = gpgme_op_sign_result(ctx);
        if (result && 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));
@@ -711,15 +737,18 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        while (fprs[i] && strlen(fprs[i])) {
                i++;
        }
-       
-       gpgme_new(&ctx);
+
+       if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
+               debug_print ("gpgme_new failed: %s\n", gpgme_strerror(err));
+               return FALSE;
+       }
 
        err = gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
 
        if (err) {
                debug_print ("gpgme_set_protocol failed: %s\n",
                    gpgme_strerror (err));
-               return FALSE;   
+               return FALSE;
        }
 
        kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
@@ -766,7 +795,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        tmpfile = get_tmp_file();
        fp = g_fopen(tmpfile, "wb");
        if (fp == NULL) {
-               perror("get_tmp_file");
+               FILE_OP_ERROR(tmpfile, "create");
                g_free(kset);
                return FALSE;
        }
@@ -777,7 +806,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        canonicalize_file_replace(tmpfile);
        fp = g_fopen(tmpfile, "rb");
        if (fp == NULL) {
-               perror("get_tmp_file");
+               FILE_OP_ERROR(tmpfile, "open");
                g_free(kset);
                return FALSE;
        }
@@ -800,7 +829,7 @@ 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;
        }
 
@@ -812,24 +841,27 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
                        fclose(fp);
                        claws_unlink(tmpfile);
                        g_free(tmpfile);
+                       g_free(enccontent);
                        return FALSE;
                }
                if (fclose(fp) == EOF) {
                        FILE_OP_ERROR(tmpfile, "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;
        }
        gpgme_data_release(gpgtext);
        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;