/*
* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and
+ * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and
* the Claws Mail team
*
* This program is free software; you can redistribute it and/or modify
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
#ifdef USE_GPGME
static gchar *get_part_as_string(MimeInfo *mimeinfo)
{
gchar *textdata = NULL;
- gchar *real_data = NULL;
- g_return_val_if_fail(mimeinfo != NULL, 0);
+ gchar *filename = NULL;
+ FILE *fp;
+
+ cm_return_val_if_fail(mimeinfo != NULL, 0);
procmime_decode_content(mimeinfo);
+
if (mimeinfo->content == MIMECONTENT_MEM)
textdata = g_strdup(mimeinfo->data.mem);
else {
- /* equals file_read_to_str but without conversion */
- FILE *fp = fopen(mimeinfo->data.filename, "r");
- if (!fp)
+ filename = procmime_get_tmp_file_name(mimeinfo);
+ if (procmime_get_part(filename, mimeinfo) < 0) {
+ printf("error dumping file\n");
return NULL;
+ }
+ fp = g_fopen(filename,"rb");
+ if (!fp) {
+ printf("error reading file\n");
+ return NULL;
+ }
textdata = fp_read_noconv(fp);
fclose(fp);
+ g_unlink(filename);
+ g_free(filename);
}
if (!g_utf8_validate(textdata, -1, NULL)) {
}
}
- if (textdata && mimeinfo->offset &&
- mimeinfo->offset+mimeinfo->length == g_utf8_strlen(textdata, -1)) {
- /* textdata is OK */
- } else if (textdata && mimeinfo->offset &&
- mimeinfo->offset+ mimeinfo->length <= strlen(textdata)) {
- real_data = g_strdup(textdata + mimeinfo->offset);
- real_data[mimeinfo->length] = '\0';
- g_free(textdata);
- textdata = real_data;
- } else if (textdata && mimeinfo->offset) {
- debug_print("got data shorter than what it should be\n");
- }
return textdata;
}
+static gchar *pgpinline_locate_armor_header(gchar *textdata, const gchar *armor_header)
+{
+ gchar *pos;
+
+ pos = strstr(textdata, armor_header);
+ /*
+ * It's only a valid armor header if it's at the
+ * beginning of the buffer or a new line.
+ */
+ if (pos != NULL && (pos == textdata || *(pos-1) == '\n'))
+ {
+ return pos;
+ }
+ return NULL;
+}
+
static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
{
PrivacyDataPGP *data = NULL;
const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
gchar *textdata, *sigpos;
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
+ cm_return_val_if_fail(mimeinfo != NULL, FALSE);
if (procmime_mimeinfo_parent(mimeinfo) == NULL)
return FALSE; /* not parent */
gpgme_data_t plain = NULL, cipher = NULL;
gpgme_error_t err;
- g_return_val_if_fail(mimeinfo != NULL, 0);
+ cm_return_val_if_fail(mimeinfo != NULL, 0);
if (procmime_mimeinfo_parent(mimeinfo) == NULL) {
privacy_set_error(_("Incorrect part"));
debug_print("type %d\n", mimeinfo->type);
return 0;
}
- g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
+ cm_return_val_if_fail(mimeinfo->privacy != NULL, 0);
data = (PrivacyDataPGP *) mimeinfo->privacy;
textdata = get_part_as_string(mimeinfo);
if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+ g_free(textdata);
return 0;
}
gpgme_set_textmode(data->ctx, 1);
{
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)
{
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)
{
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"));
return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
}
const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
gchar *textdata;
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
+ cm_return_val_if_fail(mimeinfo != NULL, FALSE);
if (procmime_mimeinfo_parent(mimeinfo) == NULL)
return FALSE; /* not parent */
if (!textdata)
return FALSE;
- if (!strstr(textdata, enc_indicator)) {
+ if (!pgpinline_locate_armor_header(textdata, enc_indicator)) {
g_free(textdata);
return FALSE;
}
gpgme_set_textmode(ctx, 1);
gpgme_set_armor(ctx, 1);
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+ cm_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
mimeinfo->type != MIMETYPE_TEXT) {
src_codeset) < 0) {
FILE_OP_ERROR(fname, "fprintf");
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
/* Store any part before encrypted text */
- pos = strstr(textdata, begin_indicator);
+ pos = pgpinline_locate_armor_header(textdata, begin_indicator);
if (pos != NULL && (pos - textdata) > 0) {
if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
FILE_OP_ERROR(fname, "fwrite");
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
}
dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
FILE_OP_ERROR(fname, "fwrite");
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
chars = sgpgme_data_release_and_get_mem(plain, &len);
if (len > 0) {
if (fwrite(chars, 1, len, dstfp) < len) {
FILE_OP_ERROR(fname, "fwrite");
+ g_free(chars);
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
}
+ g_free(chars);
/* Store any part after encrypted text */
if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
strlen(_("--- End of PGP/Inline encrypted data ---\n")),
dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
FILE_OP_ERROR(fname, "fwrite");
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
if (pos != NULL) {
- pos = strstr(pos, end_indicator);
+ pos = pgpinline_locate_armor_header(pos, end_indicator);
if (pos != NULL && *pos != '\0') {
pos += strlen(end_indicator);
if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
FILE_OP_ERROR(fname, "fwrite");
privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
- g_free(fname);
- gpgme_data_release(plain);
- gpgme_release(ctx);
- return NULL;
+ goto FILE_ERROR;
}
}
}
gpgme_release(ctx);
return decinfo;
+
+FILE_ERROR:
+ fclose(dstfp);
+ g_free(fname);
+ gpgme_data_release(plain);
+ gpgme_release(ctx);
+ return NULL;
}
-static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
+static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
{
MimeInfo *msgcontent;
gchar *textstr, *tmp;
/* get content node from message */
msgcontent = (MimeInfo *) mimeinfo->node->children->data;
- if (msgcontent->type == MIMETYPE_MULTIPART)
+ if (msgcontent->type == MIMETYPE_MULTIPART) {
+ if (!msgcontent->node->children) {
+ debug_print("msgcontent->node->children NULL, bailing\n");
+ privacy_set_error(_("Malformed message"));
+ return FALSE;
+ }
msgcontent = (MimeInfo *) msgcontent->node->children->data;
-
+ }
/* get rid of quoted-printable or anything */
procmime_decode_content(msgcontent);
gpgme_set_textmode(ctx, 1);
gpgme_set_armor(ctx, 1);
- if (!sgpgme_setup_signers(ctx, account)) {
+ if (!sgpgme_setup_signers(ctx, account, from_addr)) {
gpgme_release(ctx);
return FALSE;
}
if (msgcontent->content == MIMECONTENT_FILE &&
msgcontent->data.filename != NULL) {
if (msgcontent->tmp == TRUE)
- g_unlink(msgcontent->data.filename);
+ claws_unlink(msgcontent->data.filename);
g_free(msgcontent->data.filename);
}
msgcontent->data.mem = g_strdup(tmp);
if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+ g_free(kset);
return FALSE;
}
i = 0;
if (err) {
debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
+ g_free(kset);
return FALSE;
}
debug_print("found %s at %d\n", fprs[i], i);
/* get content node from message */
msgcontent = (MimeInfo *) mimeinfo->node->children->data;
- if (msgcontent->type == MIMETYPE_MULTIPART)
+ if (msgcontent->type == MIMETYPE_MULTIPART) {
+ if (!msgcontent->node->children) {
+ debug_print("msgcontent->node->children NULL, bailing\n");
+ privacy_set_error(_("Malformed message"));
+ g_free(kset);
+ return FALSE;
+ }
msgcontent = (MimeInfo *) msgcontent->node->children->data;
-
+ }
/* get rid of quoted-printable or anything */
procmime_decode_content(msgcontent);
if (fp == NULL) {
privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
perror("my_tmpfile");
+ g_free(kset);
return FALSE;
}
procmime_write_mimeinfo(msgcontent, fp);
if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+ g_free(kset);
return FALSE;
}
gpgme_set_armor(ctx, 1);
err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
+ g_free(kset);
if (enccontent == NULL || len <= 0) {
g_warning("sgpgme_data_release_and_get_mem failed");
if (msgcontent->content == MIMECONTENT_FILE &&
msgcontent->data.filename != NULL) {
if (msgcontent->tmp == TRUE)
- g_unlink(msgcontent->data.filename);
+ claws_unlink(msgcontent->data.filename);
g_free(msgcontent->data.filename);
}
msgcontent->data.mem = g_strdup(tmp);