2009-06-18 [colin] 3.7.1cvs76
[claws.git] / src / plugins / smime / smime.c
index b48faf3c57467cfa7fa33397670f4eb4506dcad2..f837a9b56f6df8ed8d56d4b39bd6f8db97dac462 100644 (file)
@@ -1,6 +1,6 @@
 /* 
  * 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
@@ -88,7 +88,7 @@ static gboolean smime_is_signed(MimeInfo *mimeinfo)
        const gchar *protocol, *tmpstr;
        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)
@@ -194,8 +194,8 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
        const gchar *tmpstr;
        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;
        gpgme_new(&data->ctx);
        EncodingType oldenc = ENC_BINARY;
@@ -211,7 +211,7 @@ static gint smime_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) {
@@ -236,7 +236,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
        } else {
                textstr = get_canonical_content(fp, boundary);
        }
-       err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
+       err = gpgme_data_new_from_mem(&textdata, textstr, textstr?strlen(textstr):0, 0);
        
        if (err) {
                debug_print ("gpgme_data_new_from_mem failed: %s\n",
@@ -256,7 +256,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                sgpgme_verify_signature (data->ctx, textdata, NULL, cipher);
                        gpgme_data_release(textdata);
                        g_free(textstr);
-                       gpgme_data_rewind(cipher);
+                       cm_gpgme_data_rewind(cipher);
                        textstr = sgpgme_data_release_and_get_mem(cipher, &len);
                        fclose(fp);
                        if (textstr && len > 0)
@@ -271,6 +271,10 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                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)
+                                       return -1;
+
                                g_node_unlink(decinfo->node);
                                procmime_mimeinfo_free_all(newinfo);
                                decinfo->tmp = TRUE;
@@ -324,7 +328,7 @@ static SignatureStatus smime_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)
@@ -337,7 +341,7 @@ static gchar *smime_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)
@@ -350,7 +354,7 @@ static gchar *smime_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"));
 
        if (data->sigstatus == NULL && 
            prefs_gpg_get_config()->auto_check_signatures)
@@ -396,7 +400,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
        gchar *chars;
        size_t len;
 
-       g_return_val_if_fail(smime_is_encrypted(mimeinfo), NULL);
+       cm_return_val_if_fail(smime_is_encrypted(mimeinfo), NULL);
        
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
@@ -430,7 +434,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
                get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
        if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-               FILE_OP_ERROR(fname, "fopen");
+               FILE_OP_ERROR(fname, "g_fopen");
                g_free(fname);
                gpgme_data_release(plain);
                gpgme_release(ctx);
@@ -442,6 +446,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);
                gpgme_data_release(plain);
                gpgme_release(ctx);
                debug_print("can't close!\n");
@@ -586,7 +591,7 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
 
        fclose(fp);
 
-       gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
+       gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
        gpgme_data_new(&gpgsig);
        gpgme_new(&ctx);
        gpgme_set_armor(ctx, TRUE);
@@ -795,7 +800,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
        /* write message content to temporary file */
        tmpfile = get_tmp_file();
-       fp = fopen(tmpfile, "wb");
+       fp = g_fopen(tmpfile, "wb");
        if (fp == NULL) {
                perror("get_tmp_file");
                return FALSE;
@@ -805,7 +810,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        procmime_write_mimeinfo(msgcontent, fp);
        fclose(fp);
        canonicalize_file_replace(tmpfile);
-       fp = fopen(tmpfile, "rb");
+       fp = g_fopen(tmpfile, "rb");
        if (fp == NULL) {
                perror("get_tmp_file");
                return FALSE;
@@ -818,9 +823,9 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        fclose(fp);
 
        /* encrypt data */
-       gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
+       gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
        gpgme_data_new(&gpgenc);
-       gpgme_data_rewind(gpgtext);
+       cm_gpgme_data_rewind(gpgtext);
        
        gpgme_data_set_encoding(gpgenc, GPGME_DATA_ENCODING_BASE64);
        gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
@@ -834,7 +839,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        }
 
        tmpfile = get_tmp_file();
-       fp = fopen(tmpfile, "wb");
+       fp = g_fopen(tmpfile, "wb");
        if (fp) {
                if (fwrite(enccontent, 1, len, fp) < len) {
                        FILE_OP_ERROR(tmpfile, "fwrite");