2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
4 * This file (C) 2004 Colin Leroy <colin@colino.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <glib/gi18n.h>
36 #include "pgpinline.h"
37 #include <plugins/pgpcore/sgpgme.h>
38 #include <plugins/pgpcore/prefs_gpg.h>
39 #include <plugins/pgpcore/passphrase.h>
40 #include "quoted-printable.h"
45 extern struct GPGConfig prefs_gpg;
47 typedef struct _PrivacyDataPGP PrivacyDataPGP;
49 struct _PrivacyDataPGP
53 gboolean done_sigtest;
55 gpgme_verify_result_t sigstatus;
59 static PrivacySystem pgpinline_system;
61 static gint pgpinline_check_signature(MimeInfo *mimeinfo);
63 static PrivacyDataPGP *pgpinline_new_privacydata()
67 data = g_new0(PrivacyDataPGP, 1);
68 data->data.system = &pgpinline_system;
69 data->done_sigtest = FALSE;
70 data->is_signed = FALSE;
71 data->sigstatus = NULL;
72 gpgme_new(&data->ctx);
77 static void pgpinline_free_privacydata(PrivacyData *_data)
79 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
80 gpgme_release(data->ctx);
84 static gchar *fp_read_noconv(FILE *fp)
93 array = g_byte_array_new();
95 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
96 if (n_read < sizeof(buf) && ferror(fp))
98 g_byte_array_append(array, buf, n_read);
102 FILE_OP_ERROR("file stream", "fread");
103 g_byte_array_free(array, TRUE);
108 g_byte_array_append(array, buf, 1);
109 result = (gchar *)array->data;
110 g_byte_array_free(array, FALSE);
115 static gchar *get_part_as_string(MimeInfo *mimeinfo)
117 gchar *textdata = NULL;
119 g_return_val_if_fail(mimeinfo != NULL, 0);
120 procmime_decode_content(mimeinfo);
121 if (mimeinfo->content == MIMECONTENT_MEM)
122 textdata = g_strdup(mimeinfo->data.mem);
124 /* equals file_read_to_str but without conversion */
125 FILE *fp = fopen(mimeinfo->data.filename, "r");
128 textdata = fp_read_noconv(fp);
132 if (!g_utf8_validate(textdata, -1, NULL)) {
134 codeconv_set_strict(TRUE);
135 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
136 tmp = conv_codeset_strdup(textdata,
137 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
141 tmp = conv_codeset_strdup(textdata,
142 conv_get_locale_charset_str_no_utf8(),
145 codeconv_set_strict(FALSE);
147 tmp = conv_codeset_strdup(textdata,
148 conv_get_locale_charset_str_no_utf8(),
160 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
162 PrivacyDataPGP *data = NULL;
163 const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
164 gchar *textdata, *sigpos;
166 g_return_val_if_fail(mimeinfo != NULL, FALSE);
168 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
169 return FALSE; /* not parent */
171 if (mimeinfo->type != MIMETYPE_TEXT &&
172 (mimeinfo->type != MIMETYPE_APPLICATION ||
173 g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
176 /* Seal the deal. This has to be text/plain through and through. */
177 if (mimeinfo->type == MIMETYPE_APPLICATION)
179 mimeinfo->type = MIMETYPE_TEXT;
180 g_free(mimeinfo->subtype);
181 mimeinfo->subtype = g_strdup("plain");
184 if (mimeinfo->privacy != NULL) {
185 data = (PrivacyDataPGP *) mimeinfo->privacy;
186 if (data->done_sigtest)
187 return data->is_signed;
190 textdata = get_part_as_string(mimeinfo);
194 if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
199 if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
207 data = pgpinline_new_privacydata();
208 mimeinfo->privacy = (PrivacyData *) data;
210 data->done_sigtest = TRUE;
211 data->is_signed = TRUE;
216 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
218 PrivacyDataPGP *data = NULL;
219 gchar *textdata = NULL, *tmp = NULL;
220 gpgme_data_t plain = NULL, cipher = NULL;
223 g_return_val_if_fail(mimeinfo != NULL, 0);
225 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
226 return 0; /* not parent */
227 if (mimeinfo->type != MIMETYPE_TEXT)
230 g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
231 data = (PrivacyDataPGP *) mimeinfo->privacy;
233 textdata = get_part_as_string(mimeinfo);
237 privacy_set_error(_("Couldn't get text data."));
241 /* gtk2: convert back from utf8 */
242 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
243 procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
245 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
246 conv_get_locale_charset_str_no_utf8());
249 g_warning("Can't convert charset to anything sane\n");
250 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
255 privacy_set_error(_("Couldn't convert text data to any sane charset."));
258 textdata = g_strdup(tmp);
262 gpgme_set_textmode(ctx, 1);
263 gpgme_set_armor(ctx, 1);
265 gpgme_data_new_from_mem(&plain, textdata, strlen(textdata), 1);
266 gpgme_data_new(&cipher);
268 data->sigstatus = sgpgme_verify_signature(ctx, plain, NULL, cipher);
270 gpgme_data_release(plain);
271 gpgme_data_release(cipher);
278 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
280 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
282 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
284 if (data->sigstatus == NULL &&
285 prefs_gpg_get_config()->auto_check_signatures)
286 pgpinline_check_signature(mimeinfo);
288 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
291 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
293 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
295 g_return_val_if_fail(data != NULL, g_strdup("Error"));
297 if (data->sigstatus == NULL &&
298 prefs_gpg_get_config()->auto_check_signatures)
299 pgpinline_check_signature(mimeinfo);
301 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
304 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
306 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
308 g_return_val_if_fail(data != NULL, g_strdup("Error"));
310 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
315 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
317 const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
320 g_return_val_if_fail(mimeinfo != NULL, FALSE);
322 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
323 return FALSE; /* not parent */
325 if (mimeinfo->type != MIMETYPE_TEXT &&
326 (mimeinfo->type != MIMETYPE_APPLICATION ||
327 g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
330 /* Seal the deal. This has to be text/plain through and through. */
331 if (mimeinfo->type == MIMETYPE_APPLICATION)
333 mimeinfo->type = MIMETYPE_TEXT;
334 g_free(mimeinfo->subtype);
335 mimeinfo->subtype = g_strdup("plain");
338 textdata = get_part_as_string(mimeinfo);
342 if (!strstr(textdata, enc_indicator)) {
351 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
353 MimeInfo *decinfo, *parseinfo;
354 gpgme_data_t cipher, plain;
357 gchar *textdata = NULL;
359 const gchar *src_codeset = NULL;
360 gpgme_verify_result_t sigstat = 0;
361 PrivacyDataPGP *data = NULL;
366 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
369 gpgme_set_textmode(ctx, 1);
370 gpgme_set_armor(ctx, 1);
372 g_return_val_if_fail(mimeinfo != NULL, NULL);
373 g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
375 if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
376 mimeinfo->type != MIMETYPE_TEXT) {
378 privacy_set_error(_("Couldn't parse mime part."));
382 textdata = get_part_as_string(mimeinfo);
385 privacy_set_error(_("Couldn't get text data."));
389 debug_print("decrypting '%s'\n", textdata);
390 gpgme_data_new_from_mem(&cipher, textdata, strlen(textdata), 1);
392 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
393 if (sigstat && !sigstat->signatures)
396 gpgme_data_release(cipher);
403 fname = g_strdup_printf("%s%cplaintext.%08x",
404 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
406 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
407 FILE_OP_ERROR(fname, "fopen");
408 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
410 gpgme_data_release(plain);
415 src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
416 if (src_codeset == NULL)
417 src_codeset = CS_ISO_8859_1;
419 fprintf(dstfp, "MIME-Version: 1.0\r\n"
420 "Content-Type: text/plain; charset=%s\r\n"
421 "Content-Transfer-Encoding: 8bit\r\n"
425 chars = gpgme_data_release_and_get_mem(plain, &len);
427 fwrite(chars, len, 1, dstfp);
431 gpgme_data_release(plain);
433 parseinfo = procmime_scan_file(fname);
436 if (parseinfo == NULL) {
438 privacy_set_error(_("Couldn't scan decrypted file."));
441 decinfo = g_node_first_child(parseinfo->node) != NULL ?
442 g_node_first_child(parseinfo->node)->data : NULL;
444 if (decinfo == NULL) {
446 privacy_set_error(_("Couldn't scan decrypted file parts."));
450 g_node_unlink(decinfo->node);
451 procmime_mimeinfo_free_all(parseinfo);
455 if (sigstat != GPGME_SIG_STAT_NONE) {
456 if (decinfo->privacy != NULL) {
457 data = (PrivacyDataPGP *) decinfo->privacy;
459 data = pgpinline_new_privacydata();
460 decinfo->privacy = (PrivacyData *) data;
462 data->done_sigtest = TRUE;
463 data->is_signed = TRUE;
464 data->sigstatus = sigstat;
466 gpgme_release(data->ctx);
474 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
476 MimeInfo *msgcontent;
477 gchar *textstr, *tmp;
481 gpgme_data_t gpgtext, gpgsig;
484 struct passphrase_cb_info_s info;
485 gpgme_sign_result_t result = NULL;
487 memset (&info, 0, sizeof info);
489 /* get content node from message */
490 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
491 if (msgcontent->type == MIMETYPE_MULTIPART)
492 msgcontent = (MimeInfo *) msgcontent->node->children->data;
494 /* get rid of quoted-printable or anything */
495 procmime_decode_content(msgcontent);
499 perror("my_tmpfile");
500 privacy_set_error(_("Couldn't create temporary file."));
503 procmime_write_mimeinfo(msgcontent, fp);
506 /* read temporary file into memory */
507 textstr = fp_read_noconv(fp);
511 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
512 gpgme_data_new(&gpgsig);
514 gpgme_set_textmode(ctx, 1);
515 gpgme_set_armor(ctx, 1);
517 if (!sgpgme_setup_signers(ctx, account)) {
522 if (!getenv("GPG_AGENT_INFO")) {
524 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
527 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
528 if (err != GPG_ERR_NO_ERROR) {
529 if (err == GPG_ERR_CANCELED) {
530 /* ignore cancelled signing */
531 privacy_reset_error();
532 debug_print("gpgme_op_sign cancelled\n");
534 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
535 debug_print("gpgme_op_sign error : %x\n", err);
540 result = gpgme_op_sign_result(ctx);
541 if (result && result->signatures) {
542 gpgme_new_signature_t sig = result->signatures;
544 debug_print("valid signature: %s\n", sig->fpr);
547 } else if (result && result->invalid_signers) {
548 gpgme_invalid_key_t invalid = result->invalid_signers;
550 g_warning("invalid signer: %s (%s)", invalid->fpr,
551 gpgme_strerror(invalid->reason));
552 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
553 gpgme_strerror(invalid->reason));
554 invalid = invalid->next;
559 /* can't get result (maybe no signing key?) */
560 debug_print("gpgme_op_sign_result error\n");
561 privacy_set_error(_("Data signing failed, no results."));
567 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
571 if (sigcontent == NULL || len <= 0) {
572 g_warning("gpgme_data_release_and_get_mem failed");
573 privacy_set_error(_("Data signing failed, no contents."));
574 gpgme_data_release(gpgtext);
580 tmp = g_malloc(len+1);
581 g_memmove(tmp, sigcontent, len+1);
583 gpgme_data_release(gpgtext);
587 if (msgcontent->content == MIMECONTENT_FILE &&
588 msgcontent->data.filename != NULL) {
589 if (msgcontent->tmp == TRUE)
590 g_unlink(msgcontent->data.filename);
591 g_free(msgcontent->data.filename);
593 msgcontent->data.mem = g_strdup(tmp);
594 msgcontent->content = MIMECONTENT_MEM;
597 /* avoid all sorts of clear-signing problems with non ascii
600 procmime_encode_content(msgcontent, ENC_BASE64);
605 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
607 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
610 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
612 MimeInfo *msgcontent;
616 gchar *textstr, *tmp;
617 gpgme_data_t gpgtext, gpgenc;
619 gpgme_key_t *kset = NULL;
620 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
624 while (fprs[i] && strlen(fprs[i])) {
628 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
629 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
632 while (fprs[i] && strlen(fprs[i])) {
634 err = gpgme_get_key(ctx, fprs[i], &key, 0);
636 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
637 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
640 debug_print("found %s at %d\n", fprs[i], i);
646 debug_print("Encrypting message content\n");
648 /* get content node from message */
649 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
650 if (msgcontent->type == MIMETYPE_MULTIPART)
651 msgcontent = (MimeInfo *) msgcontent->node->children->data;
653 /* get rid of quoted-printable or anything */
654 procmime_decode_content(msgcontent);
658 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
659 perror("my_tmpfile");
662 procmime_write_mimeinfo(msgcontent, fp);
665 /* read temporary file into memory */
666 textstr = fp_read_noconv(fp);
671 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
672 gpgme_data_new(&gpgenc);
674 gpgme_set_armor(ctx, 1);
676 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
679 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
681 if (enccontent == NULL || len <= 0) {
682 g_warning("gpgme_data_release_and_get_mem failed");
683 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
684 gpgme_data_release(gpgtext);
689 tmp = g_malloc(len+1);
690 g_memmove(tmp, enccontent, len+1);
694 gpgme_data_release(gpgtext);
697 if (msgcontent->content == MIMECONTENT_FILE &&
698 msgcontent->data.filename != NULL) {
699 if (msgcontent->tmp == TRUE)
700 g_unlink(msgcontent->data.filename);
701 g_free(msgcontent->data.filename);
703 msgcontent->data.mem = g_strdup(tmp);
704 msgcontent->content = MIMECONTENT_MEM;
710 static PrivacySystem pgpinline_system = {
711 "pgpinline", /* id */
712 "PGP Inline", /* name */
714 pgpinline_free_privacydata, /* free_privacydata */
716 pgpinline_is_signed, /* is_signed(MimeInfo *) */
717 pgpinline_check_signature, /* check_signature(MimeInfo *) */
718 pgpinline_get_sig_status, /* get_sig_status(MimeInfo *) */
719 pgpinline_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
720 pgpinline_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
722 pgpinline_is_encrypted, /* is_encrypted(MimeInfo *) */
723 pgpinline_decrypt, /* decrypt(MimeInfo *) */
729 pgpinline_get_encrypt_data,
733 void pgpinline_init()
735 privacy_register_system(&pgpinline_system);
738 void pgpinline_done()
740 privacy_unregister_system(&pgpinline_system);
743 struct PluginFeature *plugin_provides(void)
745 static struct PluginFeature features[] =
746 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
747 {PLUGIN_NOTHING, NULL}};
750 #endif /* USE_GPGME */