2005-04-15 [colin] 1.9.6cvs36
[claws.git] / src / plugins / pgpmime / pgpmime.c
index 947c5f94f2e5986170a680d47cc39c02bcb08066..a2487fd6368001720dbd3083534f2850b8016304 100644 (file)
@@ -37,8 +37,6 @@
 #include "prefs_gpg.h"
 #include "passphrase.h"
 
-extern struct GPGConfig prefs_gpg;
-
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
 struct _PrivacyDataPGP
@@ -72,7 +70,7 @@ static PrivacyDataPGP *pgpmime_new_privacydata()
 static void pgpmime_free_privacydata(PrivacyData *_data)
 {
        PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
-       
+       gpgme_release(data->ctx);
        g_free(data);
 }
 
@@ -95,10 +93,10 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        if (parent == NULL)
                return FALSE;
        if ((parent->type != MIMETYPE_MULTIPART) ||
-           g_strcasecmp(parent->subtype, "signed"))
+           g_ascii_strcasecmp(parent->subtype, "signed"))
                return FALSE;
        protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
-       if ((protocol == NULL) || g_strcasecmp(protocol, "application/pgp-signature"))
+       if ((protocol == NULL) || g_ascii_strcasecmp(protocol, "application/pgp-signature"))
                return FALSE;
 
        /* check if mimeinfo is the first child */
@@ -111,7 +109,7 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        if (signature == NULL)
                return FALSE;
        if ((signature->type != MIMETYPE_APPLICATION) ||
-           g_strcasecmp(signature->subtype, "pgp-signature"))
+           g_ascii_strcasecmp(signature->subtype, "pgp-signature"))
                return FALSE;
 
        if (data == NULL) {
@@ -171,7 +169,7 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        debug_print("Checking PGP/MIME signature\n");
        parent = procmime_mimeinfo_parent(mimeinfo);
 
-       fp = fopen(parent->filename, "rb");
+       fp = fopen(parent->data.filename, "rb");
        g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
        
        boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
@@ -202,7 +200,7 @@ static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
        g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
 
        if (data->sigstatus == GPGME_SIG_STAT_NONE && 
-           prefs_gpg.auto_check_signatures)
+           prefs_gpg_get_config()->auto_check_signatures)
                pgpmime_check_signature(mimeinfo);
        
        return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
@@ -215,7 +213,7 @@ static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
        g_return_val_if_fail(data != NULL, g_strdup("Error"));
 
        if (data->sigstatus == GPGME_SIG_STAT_NONE && 
-           prefs_gpg.auto_check_signatures)
+           prefs_gpg_get_config()->auto_check_signatures)
                pgpmime_check_signature(mimeinfo);
        
        return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
@@ -228,7 +226,7 @@ static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
        g_return_val_if_fail(data != NULL, g_strdup("Error"));
 
        if (data->sigstatus == GPGME_SIG_STAT_NONE && 
-           prefs_gpg.auto_check_signatures)
+           prefs_gpg_get_config()->auto_check_signatures)
                pgpmime_check_signature(mimeinfo);
        
        return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
@@ -241,10 +239,10 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        
        if (mimeinfo->type != MIMETYPE_MULTIPART)
                return FALSE;
-       if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
+       if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
                return FALSE;
        tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
-       if ((tmpstr == NULL) || g_strcasecmp(tmpstr, "application/pgp-encrypted"))
+       if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
                return FALSE;
        if (g_node_n_children(mimeinfo->node) != 2)
                return FALSE;
@@ -252,13 +250,13 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
        if (tmpinfo->type != MIMETYPE_APPLICATION)
                return FALSE;
-       if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
+       if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
                return FALSE;
        
        tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
        if (tmpinfo->type != MIMETYPE_APPLICATION)
                return FALSE;
-       if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
+       if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
                return FALSE;
        
        return TRUE;
@@ -289,9 +287,11 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
 
        gpgme_data_release(cipher);
-       if (plain == NULL)
+       if (plain == NULL) {
+               gpgme_release(ctx);
                return NULL;
-       
+       }
+
        fname = g_strdup_printf("%s%cplaintext.%08x",
                get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
@@ -299,6 +299,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                FILE_OP_ERROR(fname, "fopen");
                g_free(fname);
                gpgme_data_release(plain);
+               gpgme_release(ctx);
                return NULL;
        }
 
@@ -313,12 +314,16 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 
        parseinfo = procmime_scan_file(fname);
        g_free(fname);
-       if (parseinfo == NULL)
+       if (parseinfo == NULL) {
+               gpgme_release(ctx);
                return NULL;
+       }
        decinfo = g_node_first_child(parseinfo->node) != NULL ?
                g_node_first_child(parseinfo->node)->data : NULL;
-       if (decinfo == NULL)
+       if (decinfo == NULL) {
+               gpgme_release(ctx);
                return NULL;
+       }
 
        g_node_unlink(decinfo->node);
        procmime_mimeinfo_free_all(parseinfo);
@@ -404,7 +409,7 @@ extract_micalg (char *xml)
     return NULL;
 }
 
-gboolean pgpmime_sign(MimeInfo *mimeinfo)
+gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 {
        MimeInfo *msgcontent, *sigmultipart, *newinfo;
        gchar *textstr, *opinfo, *micalg;
@@ -412,9 +417,9 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo)
        gchar *boundary, *sigcontent;
        GpgmeCtx ctx;
        GpgmeData gpgtext, gpgsig;
-       guint len;
+       size_t len;
        struct passphrase_cb_info_s info;
-  
+
        memset (&info, 0, sizeof info);
 
        /* remove content node from message */
@@ -448,15 +453,21 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo)
        gpgme_new(&ctx);
        gpgme_set_textmode(ctx, 1);
        gpgme_set_armor(ctx, 1);
-       gpgme_signers_clear(ctx);
+
+       if (!sgpgme_setup_signers(ctx, account)) {
+               gpgme_release(ctx);
+               return FALSE;
+       }
 
        if (!getenv("GPG_AGENT_INFO")) {
                info.c = ctx;
                gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
-           }
+       }
 
-       if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPGME_No_Error)
+       if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPGME_No_Error) {
+               gpgme_release(ctx);
                return FALSE;
+       }
        opinfo = gpgme_get_op_info(ctx, 0);
        micalg = extract_micalg(opinfo);
        g_free(opinfo);
@@ -474,8 +485,9 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo)
        newinfo->type = MIMETYPE_APPLICATION;
        newinfo->subtype = g_strdup("pgp-signature");
        newinfo->content = MIMECONTENT_MEM;
-       newinfo->data = g_memdup(sigcontent, len + 1);
-       newinfo->data[len] = '\0';
+       newinfo->data.mem = g_malloc(len + 1);
+       g_memmove(newinfo->data.mem, sigcontent, len);
+       newinfo->data.mem[len] = '\0';
        g_node_append(sigmultipart->node, newinfo->node);
 
        g_free(sigcontent);
@@ -493,7 +505,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        MimeInfo *msgcontent, *encmultipart, *newinfo;
        FILE *fp;
        gchar *boundary, *enccontent;
-       guint len;
+       size_t len;
        gchar *textstr;
        GpgmeData gpgtext, gpgenc;
        gchar **recipients, **nextrecp;
@@ -504,7 +516,6 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        recipients = g_strsplit(encrypt_data, " ", 0);
        gpgme_recipients_new(&recp);
        for (nextrecp = recipients; *nextrecp != NULL; nextrecp++) {
-               printf("%s\n", *nextrecp);
                gpgme_recipients_add_name_with_validity(recp, *nextrecp,
                                                        GPGME_VALIDITY_FULL);
        }
@@ -560,15 +571,16 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        newinfo->type = MIMETYPE_APPLICATION;
        newinfo->subtype = g_strdup("pgp-encrypted");
        newinfo->content = MIMECONTENT_MEM;
-       newinfo->data = g_strdup("Version: 1\n");
+       newinfo->data.mem = g_strdup("Version: 1\n");
        g_node_append(encmultipart->node, newinfo->node);
 
        newinfo = procmime_mimeinfo_new();
        newinfo->type = MIMETYPE_APPLICATION;
        newinfo->subtype = g_strdup("octet-stream");
        newinfo->content = MIMECONTENT_MEM;
-       newinfo->data = g_memdup(enccontent, len + 1);
-       newinfo->data[len] = '\0';
+       newinfo->data.mem = g_malloc(len + 1);
+       g_memmove(newinfo->data.mem, enccontent, len);
+       newinfo->data.mem[len] = '\0';
        g_node_append(encmultipart->node, newinfo->node);
 
        g_free(enccontent);
@@ -578,7 +590,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
 static PrivacySystem pgpmime_system = {
        "pgpmime",                      /* id */
-       "PGP Mime",                     /* name */
+       "PGP/MIME",                     /* name */
 
        pgpmime_free_privacydata,       /* free_privacydata */