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 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()
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;
383 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
386 gpgme_set_textmode(ctx, 1);
387 gpgme_set_armor(ctx, 1);
389 g_return_val_if_fail(mimeinfo != NULL, NULL);
390 g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
392 if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
393 mimeinfo->type != MIMETYPE_TEXT) {
395 privacy_set_error(_("Couldn't parse mime part."));
399 textdata = get_part_as_string(mimeinfo);
402 privacy_set_error(_("Couldn't get text data."));
406 debug_print("decrypting '%s'\n", textdata);
407 gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
409 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
410 if (sigstat && !sigstat->signatures)
413 gpgme_data_release(cipher);
420 fname = g_strdup_printf("%s%cplaintext.%08x",
421 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
423 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
424 FILE_OP_ERROR(fname, "fopen");
425 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
427 gpgme_data_release(plain);
432 src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
433 if (src_codeset == NULL)
434 src_codeset = CS_ISO_8859_1;
436 fprintf(dstfp, "MIME-Version: 1.0\r\n"
437 "Content-Type: text/plain; charset=%s\r\n"
438 "Content-Transfer-Encoding: 8bit\r\n"
442 chars = sgpgme_data_release_and_get_mem(plain, &len);
444 fwrite(chars, len, 1, dstfp);
448 parseinfo = procmime_scan_file(fname);
451 if (parseinfo == NULL) {
453 privacy_set_error(_("Couldn't scan decrypted file."));
456 decinfo = g_node_first_child(parseinfo->node) != NULL ?
457 g_node_first_child(parseinfo->node)->data : NULL;
459 if (decinfo == NULL) {
461 privacy_set_error(_("Couldn't scan decrypted file parts."));
465 g_node_unlink(decinfo->node);
466 procmime_mimeinfo_free_all(parseinfo);
470 if (sigstat != GPGME_SIG_STAT_NONE) {
471 if (decinfo->privacy != NULL) {
472 data = (PrivacyDataPGP *) decinfo->privacy;
474 data = pgpinline_new_privacydata();
475 decinfo->privacy = (PrivacyData *) data;
477 data->done_sigtest = TRUE;
478 data->is_signed = TRUE;
479 data->sigstatus = sigstat;
481 gpgme_release(data->ctx);
489 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
491 MimeInfo *msgcontent;
492 gchar *textstr, *tmp;
496 gpgme_data_t gpgtext, gpgsig;
499 struct passphrase_cb_info_s info;
500 gpgme_sign_result_t result = NULL;
502 memset (&info, 0, sizeof info);
504 /* get content node from message */
505 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
506 if (msgcontent->type == MIMETYPE_MULTIPART)
507 msgcontent = (MimeInfo *) msgcontent->node->children->data;
509 /* get rid of quoted-printable or anything */
510 procmime_decode_content(msgcontent);
514 perror("my_tmpfile");
515 privacy_set_error(_("Couldn't create temporary file."));
518 procmime_write_mimeinfo(msgcontent, fp);
521 /* read temporary file into memory */
522 textstr = fp_read_noconv(fp);
526 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
527 gpgme_data_new(&gpgsig);
528 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
529 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
530 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
533 gpgme_set_textmode(ctx, 1);
534 gpgme_set_armor(ctx, 1);
536 if (!sgpgme_setup_signers(ctx, account)) {
541 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
542 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
544 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
547 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
548 if (err != GPG_ERR_NO_ERROR) {
549 if (err == GPG_ERR_CANCELED) {
550 /* ignore cancelled signing */
551 privacy_reset_error();
552 debug_print("gpgme_op_sign cancelled\n");
554 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
555 debug_print("gpgme_op_sign error : %x\n", err);
560 result = gpgme_op_sign_result(ctx);
561 if (result && result->signatures) {
562 gpgme_new_signature_t sig = result->signatures;
564 debug_print("valid signature: %s\n", sig->fpr);
567 } else if (result && result->invalid_signers) {
568 gpgme_invalid_key_t invalid = result->invalid_signers;
570 g_warning("invalid signer: %s (%s)", invalid->fpr,
571 gpgme_strerror(invalid->reason));
572 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
573 gpgme_strerror(invalid->reason));
574 invalid = invalid->next;
579 /* can't get result (maybe no signing key?) */
580 debug_print("gpgme_op_sign_result error\n");
581 privacy_set_error(_("Data signing failed, no results."));
587 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
589 if (sigcontent == NULL || len <= 0) {
590 g_warning("sgpgme_data_release_and_get_mem failed");
591 privacy_set_error(_("Data signing failed, no contents."));
592 gpgme_data_release(gpgtext);
599 tmp = g_malloc(len+1);
600 g_memmove(tmp, sigcontent, len+1);
602 gpgme_data_release(gpgtext);
606 if (msgcontent->content == MIMECONTENT_FILE &&
607 msgcontent->data.filename != NULL) {
608 if (msgcontent->tmp == TRUE)
609 g_unlink(msgcontent->data.filename);
610 g_free(msgcontent->data.filename);
612 msgcontent->data.mem = g_strdup(tmp);
613 msgcontent->content = MIMECONTENT_MEM;
616 /* avoid all sorts of clear-signing problems with non ascii
619 procmime_encode_content(msgcontent, ENC_BASE64);
625 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
627 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
630 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
632 MimeInfo *msgcontent;
636 gchar *textstr, *tmp;
637 gpgme_data_t gpgtext, gpgenc;
639 gpgme_key_t *kset = NULL;
640 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
644 while (fprs[i] && strlen(fprs[i])) {
648 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
649 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
650 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
651 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
652 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
656 while (fprs[i] && strlen(fprs[i])) {
658 err = gpgme_get_key(ctx, fprs[i], &key, 0);
660 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
661 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
664 debug_print("found %s at %d\n", fprs[i], i);
670 debug_print("Encrypting message content\n");
672 /* get content node from message */
673 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
674 if (msgcontent->type == MIMETYPE_MULTIPART)
675 msgcontent = (MimeInfo *) msgcontent->node->children->data;
677 /* get rid of quoted-printable or anything */
678 procmime_decode_content(msgcontent);
682 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
683 perror("my_tmpfile");
686 procmime_write_mimeinfo(msgcontent, fp);
689 /* read temporary file into memory */
690 textstr = fp_read_noconv(fp);
695 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
696 gpgme_data_new(&gpgenc);
697 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
698 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
699 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
702 gpgme_set_armor(ctx, 1);
704 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
706 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
708 if (enccontent == NULL || len <= 0) {
709 g_warning("sgpgme_data_release_and_get_mem failed");
710 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
711 gpgme_data_release(gpgtext);
717 tmp = g_malloc(len+1);
718 g_memmove(tmp, enccontent, len+1);
722 gpgme_data_release(gpgtext);
725 if (msgcontent->content == MIMECONTENT_FILE &&
726 msgcontent->data.filename != NULL) {
727 if (msgcontent->tmp == TRUE)
728 g_unlink(msgcontent->data.filename);
729 g_free(msgcontent->data.filename);
731 msgcontent->data.mem = g_strdup(tmp);
732 msgcontent->content = MIMECONTENT_MEM;
739 static PrivacySystem pgpinline_system = {
740 "pgpinline", /* id */
741 "PGP Inline", /* name */
743 pgpinline_free_privacydata, /* free_privacydata */
745 pgpinline_is_signed, /* is_signed(MimeInfo *) */
746 pgpinline_check_signature, /* check_signature(MimeInfo *) */
747 pgpinline_get_sig_status, /* get_sig_status(MimeInfo *) */
748 pgpinline_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
749 pgpinline_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
751 pgpinline_is_encrypted, /* is_encrypted(MimeInfo *) */
752 pgpinline_decrypt, /* decrypt(MimeInfo *) */
758 pgpinline_get_encrypt_data,
762 void pgpinline_init()
764 privacy_register_system(&pgpinline_system);
767 void pgpinline_done()
769 privacy_unregister_system(&pgpinline_system);
772 struct PluginFeature *plugin_provides(void)
774 static struct PluginFeature features[] =
775 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
776 {PLUGIN_NOTHING, NULL}};
779 #endif /* USE_GPGME */