2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and
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 3 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, see <http://www.gnu.org/licenses/>.
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()
68 data = g_new0(PrivacyDataPGP, 1);
69 data->data.system = &pgpinline_system;
70 data->done_sigtest = FALSE;
71 data->is_signed = FALSE;
72 data->sigstatus = NULL;
73 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
74 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
81 static void pgpinline_free_privacydata(PrivacyData *_data)
83 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
84 gpgme_release(data->ctx);
88 static gchar *fp_read_noconv(FILE *fp)
97 array = g_byte_array_new();
99 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
100 if (n_read < sizeof(buf) && ferror(fp))
102 g_byte_array_append(array, buf, n_read);
106 FILE_OP_ERROR("file stream", "fread");
107 g_byte_array_free(array, TRUE);
112 g_byte_array_append(array, buf, 1);
113 result = (gchar *)array->data;
114 g_byte_array_free(array, FALSE);
119 static gchar *get_part_as_string(MimeInfo *mimeinfo)
121 gchar *textdata = NULL;
122 gchar *real_data = NULL;
123 g_return_val_if_fail(mimeinfo != NULL, 0);
124 procmime_decode_content(mimeinfo);
125 if (mimeinfo->content == MIMECONTENT_MEM)
126 textdata = g_strdup(mimeinfo->data.mem);
128 /* equals file_read_to_str but without conversion */
129 FILE *fp = fopen(mimeinfo->data.filename, "r");
132 textdata = fp_read_noconv(fp);
136 if (!g_utf8_validate(textdata, -1, NULL)) {
138 codeconv_set_strict(TRUE);
139 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
140 tmp = conv_codeset_strdup(textdata,
141 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
145 tmp = conv_codeset_strdup(textdata,
146 conv_get_locale_charset_str_no_utf8(),
149 codeconv_set_strict(FALSE);
151 tmp = conv_codeset_strdup(textdata,
152 conv_get_locale_charset_str_no_utf8(),
160 if (textdata && mimeinfo->offset &&
161 mimeinfo->offset+ mimeinfo->length <= strlen(textdata)) {
162 real_data = g_strdup(textdata + mimeinfo->offset);
163 real_data[mimeinfo->length] = '\0';
165 textdata = real_data;
166 } else if (textdata && mimeinfo->offset) {
167 debug_print("got data shorter than what it should be\n");
172 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
174 PrivacyDataPGP *data = NULL;
175 const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
176 gchar *textdata, *sigpos;
178 g_return_val_if_fail(mimeinfo != NULL, FALSE);
180 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
181 return FALSE; /* not parent */
183 if (mimeinfo->type != MIMETYPE_TEXT &&
184 (mimeinfo->type != MIMETYPE_APPLICATION ||
185 g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
188 /* Seal the deal. This has to be text/plain through and through. */
189 if (mimeinfo->type == MIMETYPE_APPLICATION)
191 mimeinfo->type = MIMETYPE_TEXT;
192 g_free(mimeinfo->subtype);
193 mimeinfo->subtype = g_strdup("plain");
196 if (mimeinfo->privacy != NULL) {
197 data = (PrivacyDataPGP *) mimeinfo->privacy;
198 if (data->done_sigtest)
199 return data->is_signed;
202 textdata = get_part_as_string(mimeinfo);
206 if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
211 if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
219 data = pgpinline_new_privacydata();
220 mimeinfo->privacy = (PrivacyData *) data;
222 data->done_sigtest = TRUE;
223 data->is_signed = TRUE;
228 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
230 PrivacyDataPGP *data = NULL;
231 gchar *textdata = NULL, *tmp = NULL;
232 gpgme_data_t plain = NULL, cipher = NULL;
235 g_return_val_if_fail(mimeinfo != NULL, 0);
237 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
238 return 0; /* not parent */
239 if (mimeinfo->type != MIMETYPE_TEXT)
242 g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
243 data = (PrivacyDataPGP *) mimeinfo->privacy;
245 textdata = get_part_as_string(mimeinfo);
249 privacy_set_error(_("Couldn't get text data."));
253 /* gtk2: convert back from utf8 */
254 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
255 procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
257 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
258 conv_get_locale_charset_str_no_utf8());
261 g_warning("Can't convert charset to anything sane\n");
262 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
267 privacy_set_error(_("Couldn't convert text data to any sane charset."));
270 textdata = g_strdup(tmp);
273 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
274 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
275 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
278 gpgme_set_textmode(data->ctx, 1);
279 gpgme_set_armor(data->ctx, 1);
281 gpgme_data_new_from_mem(&plain, textdata, (size_t)strlen(textdata), 1);
282 gpgme_data_new(&cipher);
284 data->sigstatus = sgpgme_verify_signature(data->ctx, plain, NULL, cipher);
286 gpgme_data_release(plain);
287 gpgme_data_release(cipher);
294 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
296 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
298 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
300 if (data->sigstatus == NULL &&
301 prefs_gpg_get_config()->auto_check_signatures)
302 pgpinline_check_signature(mimeinfo);
304 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
307 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
309 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
311 g_return_val_if_fail(data != NULL, g_strdup("Error"));
313 if (data->sigstatus == NULL &&
314 prefs_gpg_get_config()->auto_check_signatures)
315 pgpinline_check_signature(mimeinfo);
317 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
320 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
322 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
324 g_return_val_if_fail(data != NULL, g_strdup("Error"));
326 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
331 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
333 const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
336 g_return_val_if_fail(mimeinfo != NULL, FALSE);
338 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
339 return FALSE; /* not parent */
341 if (mimeinfo->type != MIMETYPE_TEXT &&
342 (mimeinfo->type != MIMETYPE_APPLICATION ||
343 g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
346 /* Seal the deal. This has to be text/plain through and through. */
347 if (mimeinfo->type == MIMETYPE_APPLICATION)
349 mimeinfo->type = MIMETYPE_TEXT;
350 g_free(mimeinfo->subtype);
351 mimeinfo->subtype = g_strdup("plain");
354 textdata = get_part_as_string(mimeinfo);
358 if (!strstr(textdata, enc_indicator)) {
368 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
370 MimeInfo *decinfo, *parseinfo;
371 gpgme_data_t cipher, plain;
374 gchar *textdata = NULL;
376 const gchar *src_codeset = NULL;
377 gpgme_verify_result_t sigstat = 0;
378 PrivacyDataPGP *data = NULL;
382 const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
383 const gchar *end_indicator = "-----END PGP MESSAGE-----";
386 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
389 gpgme_set_textmode(ctx, 1);
390 gpgme_set_armor(ctx, 1);
392 g_return_val_if_fail(mimeinfo != NULL, NULL);
393 g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
395 if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
396 mimeinfo->type != MIMETYPE_TEXT) {
398 privacy_set_error(_("Couldn't parse mime part."));
402 textdata = get_part_as_string(mimeinfo);
405 privacy_set_error(_("Couldn't get text data."));
409 debug_print("decrypting '%s'\n", textdata);
410 gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
412 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
413 if (sigstat && !sigstat->signatures)
416 gpgme_data_release(cipher);
423 fname = g_strdup_printf("%s%cplaintext.%08x",
424 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
426 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
427 FILE_OP_ERROR(fname, "fopen");
428 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
430 gpgme_data_release(plain);
435 src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
436 if (src_codeset == NULL)
437 src_codeset = CS_ISO_8859_1;
439 fprintf(dstfp, "MIME-Version: 1.0\r\n"
440 "Content-Type: text/plain; charset=%s\r\n"
441 "Content-Transfer-Encoding: 8bit\r\n"
445 /* Store any part before encrypted text */
446 pos = strstr(textdata, begin_indicator);
447 if (pos != NULL && (pos - textdata) > 0) {
448 fwrite(textdata, pos - textdata, 1, dstfp);
451 fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"),
452 strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")),
454 chars = sgpgme_data_release_and_get_mem(plain, &len);
456 fwrite(chars, len, 1, dstfp);
458 /* Store any part after encrypted text */
459 fwrite(_("--- End of PGP/Inline encrypted data ---\n"),
460 strlen(_("--- End of PGP/Inline encrypted data ---\n")),
463 pos = strstr(pos, end_indicator);
464 if (pos != NULL && *pos != '\0') {
465 pos += strlen(end_indicator);
466 fwrite(pos, strlen(pos), 1, dstfp);
472 parseinfo = procmime_scan_file(fname);
475 if (parseinfo == NULL) {
477 privacy_set_error(_("Couldn't scan decrypted file."));
480 decinfo = g_node_first_child(parseinfo->node) != NULL ?
481 g_node_first_child(parseinfo->node)->data : NULL;
483 if (decinfo == NULL) {
485 privacy_set_error(_("Couldn't scan decrypted file parts."));
489 g_node_unlink(decinfo->node);
490 procmime_mimeinfo_free_all(parseinfo);
494 if (sigstat != GPGME_SIG_STAT_NONE) {
495 if (decinfo->privacy != NULL) {
496 data = (PrivacyDataPGP *) decinfo->privacy;
498 data = pgpinline_new_privacydata();
499 decinfo->privacy = (PrivacyData *) data;
501 data->done_sigtest = TRUE;
502 data->is_signed = TRUE;
503 data->sigstatus = sigstat;
505 gpgme_release(data->ctx);
513 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
515 MimeInfo *msgcontent;
516 gchar *textstr, *tmp;
520 gpgme_data_t gpgtext, gpgsig;
523 struct passphrase_cb_info_s info;
524 gpgme_sign_result_t result = NULL;
526 memset (&info, 0, sizeof info);
528 /* get content node from message */
529 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
530 if (msgcontent->type == MIMETYPE_MULTIPART)
531 msgcontent = (MimeInfo *) msgcontent->node->children->data;
533 /* get rid of quoted-printable or anything */
534 procmime_decode_content(msgcontent);
538 perror("my_tmpfile");
539 privacy_set_error(_("Couldn't create temporary file."));
542 procmime_write_mimeinfo(msgcontent, fp);
545 /* read temporary file into memory */
546 textstr = fp_read_noconv(fp);
550 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
551 gpgme_data_new(&gpgsig);
552 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
553 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
554 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
557 gpgme_set_textmode(ctx, 1);
558 gpgme_set_armor(ctx, 1);
560 if (!sgpgme_setup_signers(ctx, account)) {
565 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
566 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
568 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
571 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
572 if (err != GPG_ERR_NO_ERROR) {
573 if (err == GPG_ERR_CANCELED) {
574 /* ignore cancelled signing */
575 privacy_reset_error();
576 debug_print("gpgme_op_sign cancelled\n");
578 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
579 debug_print("gpgme_op_sign error : %x\n", err);
584 result = gpgme_op_sign_result(ctx);
585 if (result && result->signatures) {
586 gpgme_new_signature_t sig = result->signatures;
588 debug_print("valid signature: %s\n", sig->fpr);
591 } else if (result && result->invalid_signers) {
592 gpgme_invalid_key_t invalid = result->invalid_signers;
594 g_warning("invalid signer: %s (%s)", invalid->fpr,
595 gpgme_strerror(invalid->reason));
596 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
597 gpgme_strerror(invalid->reason));
598 invalid = invalid->next;
603 /* can't get result (maybe no signing key?) */
604 debug_print("gpgme_op_sign_result error\n");
605 privacy_set_error(_("Data signing failed, no results."));
611 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
613 if (sigcontent == NULL || len <= 0) {
614 g_warning("sgpgme_data_release_and_get_mem failed");
615 privacy_set_error(_("Data signing failed, no contents."));
616 gpgme_data_release(gpgtext);
623 tmp = g_malloc(len+1);
624 g_memmove(tmp, sigcontent, len+1);
626 gpgme_data_release(gpgtext);
630 if (msgcontent->content == MIMECONTENT_FILE &&
631 msgcontent->data.filename != NULL) {
632 if (msgcontent->tmp == TRUE)
633 g_unlink(msgcontent->data.filename);
634 g_free(msgcontent->data.filename);
636 msgcontent->data.mem = g_strdup(tmp);
637 msgcontent->content = MIMECONTENT_MEM;
640 /* avoid all sorts of clear-signing problems with non ascii
643 procmime_encode_content(msgcontent, ENC_BASE64);
649 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
651 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
654 static const gchar *pgpinline_get_encrypt_warning(void)
656 if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
659 return _("Please note that attachments are not encrypted by "
660 "the PGP/Inline system, nor are email headers, like Subject.");
663 static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
666 prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
668 prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
671 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
673 MimeInfo *msgcontent;
677 gchar *textstr, *tmp;
678 gpgme_data_t gpgtext, gpgenc;
680 gpgme_key_t *kset = NULL;
681 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
685 while (fprs[i] && strlen(fprs[i])) {
689 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
690 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
691 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
692 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
693 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
697 while (fprs[i] && strlen(fprs[i])) {
699 err = gpgme_get_key(ctx, fprs[i], &key, 0);
701 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
702 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
705 debug_print("found %s at %d\n", fprs[i], i);
711 debug_print("Encrypting message content\n");
713 /* get content node from message */
714 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
715 if (msgcontent->type == MIMETYPE_MULTIPART)
716 msgcontent = (MimeInfo *) msgcontent->node->children->data;
718 /* get rid of quoted-printable or anything */
719 procmime_decode_content(msgcontent);
723 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
724 perror("my_tmpfile");
727 procmime_write_mimeinfo(msgcontent, fp);
730 /* read temporary file into memory */
731 textstr = fp_read_noconv(fp);
736 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
737 gpgme_data_new(&gpgenc);
738 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
739 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
740 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
743 gpgme_set_armor(ctx, 1);
745 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
747 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
749 if (enccontent == NULL || len <= 0) {
750 g_warning("sgpgme_data_release_and_get_mem failed");
751 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
752 gpgme_data_release(gpgtext);
758 tmp = g_malloc(len+1);
759 g_memmove(tmp, enccontent, len+1);
763 gpgme_data_release(gpgtext);
766 if (msgcontent->content == MIMECONTENT_FILE &&
767 msgcontent->data.filename != NULL) {
768 if (msgcontent->tmp == TRUE)
769 g_unlink(msgcontent->data.filename);
770 g_free(msgcontent->data.filename);
772 msgcontent->data.mem = g_strdup(tmp);
773 msgcontent->content = MIMECONTENT_MEM;
780 static PrivacySystem pgpinline_system = {
781 "pgpinline", /* id */
782 "PGP Inline", /* name */
784 pgpinline_free_privacydata, /* free_privacydata */
786 pgpinline_is_signed, /* is_signed(MimeInfo *) */
787 pgpinline_check_signature, /* check_signature(MimeInfo *) */
788 pgpinline_get_sig_status, /* get_sig_status(MimeInfo *) */
789 pgpinline_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
790 pgpinline_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
792 pgpinline_is_encrypted, /* is_encrypted(MimeInfo *) */
793 pgpinline_decrypt, /* decrypt(MimeInfo *) */
799 pgpinline_get_encrypt_data,
801 pgpinline_get_encrypt_warning,
802 pgpinline_inhibit_encrypt_warning,
805 void pgpinline_init()
807 privacy_register_system(&pgpinline_system);
810 void pgpinline_done()
812 privacy_unregister_system(&pgpinline_system);
815 struct PluginFeature *plugin_provides(void)
817 static struct PluginFeature features[] =
818 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
819 {PLUGIN_NOTHING, NULL}};
822 #endif /* USE_GPGME */