cbd3bb02dcb7149487b97e1254f456eb1f94fe33
[claws.git] / src / plugins / pgpinline / pgpinline.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
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.
10  *
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.
15  *
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/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #ifdef USE_GPGME
27
28 #include "defs.h"
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <errno.h>
32 #include <gpgme.h>
33
34 #include "utils.h"
35 #include "privacy.h"
36 #include "procmime.h"
37 #include "pgpinline.h"
38 #include <plugins/pgpcore/sgpgme.h>
39 #include <plugins/pgpcore/prefs_gpg.h>
40 #include <plugins/pgpcore/passphrase.h>
41 #include <plugins/pgpcore/pgp_utils.h>
42 #include "quoted-printable.h"
43 #include "base64.h"
44 #include "codeconv.h"
45 #include "plugin.h"
46
47 extern struct GPGConfig prefs_gpg;
48
49 typedef struct _PrivacyDataPGP PrivacyDataPGP;
50
51 struct _PrivacyDataPGP
52 {
53         PrivacyData     data;
54         
55         gboolean        done_sigtest;
56         gboolean        is_signed;
57         gpgme_verify_result_t   sigstatus;
58         gpgme_ctx_t     ctx;
59 };
60
61 static PrivacySystem pgpinline_system;
62
63 static gint pgpinline_check_signature(MimeInfo *mimeinfo);
64
65 static PrivacyDataPGP *pgpinline_new_privacydata()
66 {
67         PrivacyDataPGP *data;
68         gpgme_error_t err;
69
70         data = g_new0(PrivacyDataPGP, 1);
71         data->data.system = &pgpinline_system;
72         data->done_sigtest = FALSE;
73         data->is_signed = FALSE;
74         data->sigstatus = NULL;
75         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
76                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
77                 return NULL;
78         }
79         
80         return data;
81 }
82
83 static void pgpinline_free_privacydata(PrivacyData *_data)
84 {
85         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
86         gpgme_release(data->ctx);
87         g_free(data);
88 }
89
90 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
91 {
92         PrivacyDataPGP *data = NULL;
93         const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
94         gchar *textdata, *sigpos;
95         
96         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
97         
98         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
99                 return FALSE; /* not parent */
100         
101         if (mimeinfo->type != MIMETYPE_TEXT &&
102                 (mimeinfo->type != MIMETYPE_APPLICATION ||
103                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
104                 return FALSE;
105
106         /* Seal the deal. This has to be text/plain through and through. */
107         if (mimeinfo->type == MIMETYPE_APPLICATION)
108         {
109                 mimeinfo->type = MIMETYPE_TEXT;
110                 g_free(mimeinfo->subtype);
111                 mimeinfo->subtype = g_strdup("plain");
112         }
113
114         if (mimeinfo->privacy != NULL) {
115                 data = (PrivacyDataPGP *) mimeinfo->privacy;
116                 if (data->done_sigtest)
117                         return data->is_signed;
118         }
119         
120         textdata = get_part_as_string(mimeinfo);
121         if (!textdata)
122                 return FALSE;
123         
124         if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
125                 g_free(textdata);
126                 return FALSE;
127         }
128
129         if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
130                 g_free(textdata);
131                 return FALSE;
132         }
133
134         g_free(textdata);
135
136         if (data == NULL) {
137                 data = pgpinline_new_privacydata();
138                 mimeinfo->privacy = (PrivacyData *) data;
139         }
140         data->done_sigtest = TRUE;
141         data->is_signed = TRUE;
142
143         return TRUE;
144 }
145
146 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
147 {
148         PrivacyDataPGP *data = NULL;
149         gchar *textdata = NULL, *tmp = NULL;
150         gpgme_data_t plain = NULL, cipher = NULL;
151         gpgme_error_t err;
152
153         cm_return_val_if_fail(mimeinfo != NULL, 0);
154
155         if (procmime_mimeinfo_parent(mimeinfo) == NULL) {
156                 privacy_set_error(_("Incorrect part"));
157                 return 0; /* not parent */
158         }
159         if (mimeinfo->type != MIMETYPE_TEXT) {
160                 privacy_set_error(_("Not a text part"));
161                 debug_print("type %d\n", mimeinfo->type);
162                 return 0;
163         }
164         cm_return_val_if_fail(mimeinfo->privacy != NULL, 0);
165         data = (PrivacyDataPGP *) mimeinfo->privacy;
166
167         textdata = get_part_as_string(mimeinfo);
168
169         if (!textdata) {
170                 g_free(textdata);
171                 privacy_set_error(_("Couldn't get text data."));
172                 return 0;
173         }
174
175         /* gtk2: convert back from utf8 */
176         tmp = conv_codeset_strdup(textdata, CS_UTF_8,
177                         procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
178         if (!tmp) {
179                 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
180                         conv_get_locale_charset_str_no_utf8());
181         }
182         if (!tmp) {
183                 g_warning("Can't convert charset to anything sane\n");
184                 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
185         }
186         g_free(textdata);
187
188         if (!tmp) {
189                 privacy_set_error(_("Couldn't convert text data to any sane charset."));
190                 return 0;
191         }
192         textdata = g_strdup(tmp);
193         g_free(tmp);
194         
195         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
196                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
197                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
198                 g_free(textdata);
199                 return 0;
200         }
201         gpgme_set_textmode(data->ctx, 1);
202         gpgme_set_armor(data->ctx, 1);
203         
204         gpgme_data_new_from_mem(&plain, textdata, (size_t)strlen(textdata), 1);
205         gpgme_data_new(&cipher);
206
207         data->sigstatus = sgpgme_verify_signature(data->ctx, plain, NULL, cipher);
208         
209         gpgme_data_release(plain);
210         gpgme_data_release(cipher);
211         
212         g_free(textdata);
213         
214         return 0;
215 }
216
217 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
218 {
219         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
220         
221         cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
222
223         if (data->sigstatus == NULL && 
224             prefs_gpg_get_config()->auto_check_signatures)
225                 pgpinline_check_signature(mimeinfo);
226
227         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
228 }
229
230 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
231 {
232         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
233         
234         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
235
236         if (data->sigstatus == NULL && 
237             prefs_gpg_get_config()->auto_check_signatures)
238                 pgpinline_check_signature(mimeinfo);
239         
240         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
241 }
242
243 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
244 {
245         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
246         
247         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
248
249         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
250 }
251
252 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
253 {
254         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
255         const gchar *end_indicator = "-----END PGP MESSAGE-----";
256         gchar *textdata;
257         
258         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
259         
260         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
261                 return FALSE; /* not parent */
262         
263         if (mimeinfo->type != MIMETYPE_TEXT &&
264                 (mimeinfo->type != MIMETYPE_APPLICATION ||
265                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
266                 return FALSE;
267         
268         /* Seal the deal. This has to be text/plain through and through. */
269         if (mimeinfo->type == MIMETYPE_APPLICATION)
270         {
271                 mimeinfo->type = MIMETYPE_TEXT;
272                 g_free(mimeinfo->subtype);
273                 mimeinfo->subtype = g_strdup("plain");
274         }
275
276         textdata = get_part_as_string(mimeinfo);
277         if (!textdata)
278                 return FALSE;
279
280         if (!pgp_locate_armor_header(textdata, begin_indicator)) {
281                 g_free(textdata);
282                 return FALSE;
283         }
284         if (!pgp_locate_armor_header(textdata, end_indicator)) {
285                 g_free(textdata);
286                 return FALSE;
287         }
288
289         g_free(textdata);
290
291         return TRUE;
292 }
293
294 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
295 {
296         MimeInfo *decinfo, *parseinfo;
297         gpgme_data_t cipher, plain;
298         FILE *dstfp;
299         gchar *fname;
300         gchar *textdata = NULL;
301         static gint id = 0;
302         const gchar *src_codeset = NULL;
303         gpgme_verify_result_t sigstat = 0;
304         PrivacyDataPGP *data = NULL;
305         gpgme_ctx_t ctx;
306         gchar *chars;
307         size_t len;
308         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
309         const gchar *end_indicator = "-----END PGP MESSAGE-----";
310         gchar *pos;
311         
312         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
313                 return NULL;
314
315         gpgme_set_textmode(ctx, 1);
316         gpgme_set_armor(ctx, 1);
317
318         cm_return_val_if_fail(mimeinfo != NULL, NULL);
319         cm_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
320         
321         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
322             mimeinfo->type != MIMETYPE_TEXT) {
323                 gpgme_release(ctx);
324                 privacy_set_error(_("Couldn't parse mime part."));
325                 return NULL;
326         }
327
328         textdata = get_part_as_string(mimeinfo);
329         if (!textdata) {
330                 gpgme_release(ctx);
331                 privacy_set_error(_("Couldn't get text data."));
332                 return NULL;
333         }
334
335         debug_print("decrypting '%s'\n", textdata);
336         gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
337
338         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
339         if (sigstat && !sigstat->signatures)
340                 sigstat = NULL;
341
342         gpgme_data_release(cipher);
343         
344         if (plain == NULL) {
345                 gpgme_release(ctx);
346                 return NULL;
347         }
348
349         fname = g_strdup_printf("%s%cplaintext.%08x",
350                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
351
352         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
353                 FILE_OP_ERROR(fname, "fopen");
354                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
355                 g_free(fname);
356                 gpgme_data_release(plain);
357                 gpgme_release(ctx);
358                 return NULL;
359         }
360
361         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
362         if (src_codeset == NULL)
363                 src_codeset = CS_ISO_8859_1;
364                 
365         if (fprintf(dstfp, "MIME-Version: 1.0\r\n"
366                         "Content-Type: text/plain; charset=%s\r\n"
367                         "Content-Transfer-Encoding: 8bit\r\n"
368                         "\r\n",
369                         src_codeset) < 0) {
370                 FILE_OP_ERROR(fname, "fprintf");
371                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
372                 goto FILE_ERROR;
373         }
374
375         /* Store any part before encrypted text */
376         pos = pgp_locate_armor_header(textdata, begin_indicator);
377         if (pos != NULL && (pos - textdata) > 0) {
378             if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
379                 FILE_OP_ERROR(fname, "fwrite");
380                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
381                 goto FILE_ERROR;
382             }
383         }
384         
385         if (fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
386                 strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
387                 dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
388                 FILE_OP_ERROR(fname, "fwrite");
389                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
390                 goto FILE_ERROR;
391         }
392         chars = sgpgme_data_release_and_get_mem(plain, &len);
393         if (len > 0) {
394                 if (fwrite(chars, 1, len, dstfp) < len) {
395                         FILE_OP_ERROR(fname, "fwrite");
396                         g_free(chars);
397                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
398                         goto FILE_ERROR;
399                 }
400         }
401         g_free(chars);
402         /* Store any part after encrypted text */
403         if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
404                 strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
405                 dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
406                         FILE_OP_ERROR(fname, "fwrite");
407                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
408                         goto FILE_ERROR;
409         }
410         if (pos != NULL) {
411             pos = pgp_locate_armor_header(pos, end_indicator);
412             if (pos != NULL && *pos != '\0') {
413                 pos += strlen(end_indicator);
414                 if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
415                         FILE_OP_ERROR(fname, "fwrite");
416                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
417                         goto FILE_ERROR;
418                 }
419             }
420         }
421
422         if (fclose(dstfp) == EOF) {
423                 FILE_OP_ERROR(fname, "fclose");
424                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
425                 g_free(fname);
426                 gpgme_data_release(plain);
427                 gpgme_release(ctx);
428                 return NULL;
429         }
430         
431         parseinfo = procmime_scan_file(fname);
432         g_free(fname);
433         
434         if (parseinfo == NULL) {
435                 gpgme_release(ctx);
436                 privacy_set_error(_("Couldn't scan decrypted file."));
437                 return NULL;
438         }
439         decinfo = g_node_first_child(parseinfo->node) != NULL ?
440                 g_node_first_child(parseinfo->node)->data : NULL;
441                 
442         if (decinfo == NULL) {
443                 gpgme_release(ctx);
444                 privacy_set_error(_("Couldn't scan decrypted file parts."));
445                 return NULL;
446         }
447
448         g_node_unlink(decinfo->node);
449         procmime_mimeinfo_free_all(parseinfo);
450
451         decinfo->tmp = TRUE;
452
453         if (sigstat != GPGME_SIG_STAT_NONE) {
454                 if (decinfo->privacy != NULL) {
455                         data = (PrivacyDataPGP *) decinfo->privacy;
456                 } else {
457                         data = pgpinline_new_privacydata();
458                         decinfo->privacy = (PrivacyData *) data;        
459                 }
460                 data->done_sigtest = TRUE;
461                 data->is_signed = TRUE;
462                 data->sigstatus = sigstat;
463                 if (data->ctx)
464                         gpgme_release(data->ctx);
465                 data->ctx = ctx;
466         } else
467                 gpgme_release(ctx);
468
469         return decinfo;
470
471 FILE_ERROR:
472         fclose(dstfp);
473         g_free(fname);
474         gpgme_data_release(plain);
475         gpgme_release(ctx);
476         return NULL;
477 }
478
479 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
480 {
481         MimeInfo *msgcontent;
482         gchar *textstr, *tmp;
483         FILE *fp;
484         gchar *sigcontent;
485         gpgme_ctx_t ctx;
486         gpgme_data_t gpgtext, gpgsig;
487         size_t len;
488         gpgme_error_t err;
489         struct passphrase_cb_info_s info;
490         gpgme_sign_result_t result = NULL;
491
492         memset (&info, 0, sizeof info);
493
494         /* get content node from message */
495         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
496         if (msgcontent->type == MIMETYPE_MULTIPART) {
497                 if (!msgcontent->node->children) {
498                         debug_print("msgcontent->node->children NULL, bailing\n");
499                         privacy_set_error(_("Malformed message"));
500                         return FALSE;
501                 }
502                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
503         }
504         /* get rid of quoted-printable or anything */
505         procmime_decode_content(msgcontent);
506
507         fp = my_tmpfile();
508         if (fp == NULL) {
509                 perror("my_tmpfile");
510                 privacy_set_error(_("Couldn't create temporary file."));
511                 return FALSE;
512         }
513         procmime_write_mimeinfo(msgcontent, fp);
514         rewind(fp);
515
516         /* read temporary file into memory */
517         textstr = fp_read_noconv(fp);
518         
519         fclose(fp);
520                 
521         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
522         gpgme_data_new(&gpgsig);
523         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
524                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
525                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
526                 return FALSE;
527         }
528         gpgme_set_textmode(ctx, 1);
529         gpgme_set_armor(ctx, 1);
530
531         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
532                 gpgme_release(ctx);
533                 return FALSE;
534         }
535
536         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
537         if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
538                 info.c = ctx;
539                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
540         }
541
542         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
543         if (err != GPG_ERR_NO_ERROR) {
544                 if (err == GPG_ERR_CANCELED) {
545                         /* ignore cancelled signing */
546                         privacy_reset_error();
547                         debug_print("gpgme_op_sign cancelled\n");
548                 } else {
549                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
550                         debug_print("gpgme_op_sign error : %x\n", err);
551                 }
552                 gpgme_release(ctx);
553                 return FALSE;
554         }
555         result = gpgme_op_sign_result(ctx);
556         if (result && result->signatures) {
557                 gpgme_new_signature_t sig = result->signatures;
558                 while (sig) {
559                         debug_print("valid signature: %s\n", sig->fpr);
560                         sig = sig->next;
561                 }
562         } else if (result && result->invalid_signers) {
563                 gpgme_invalid_key_t invalid = result->invalid_signers;
564                 while (invalid) {
565                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
566                                 gpgme_strerror(invalid->reason));
567                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
568                                 gpgme_strerror(invalid->reason));
569                         invalid = invalid->next;
570                 }
571                 gpgme_release(ctx);
572                 return FALSE;
573         } else {
574                 /* can't get result (maybe no signing key?) */
575                 debug_print("gpgme_op_sign_result error\n");
576                 privacy_set_error(_("Data signing failed, no results."));
577                 gpgme_release(ctx);
578                 return FALSE;
579         }
580
581
582         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
583         
584         if (sigcontent == NULL || len <= 0) {
585                 g_warning("sgpgme_data_release_and_get_mem failed");
586                 privacy_set_error(_("Data signing failed, no contents."));
587                 gpgme_data_release(gpgtext);
588                 g_free(textstr);
589                 g_free(sigcontent);
590                 gpgme_release(ctx);
591                 return FALSE;
592         }
593
594         tmp = g_malloc(len+1);
595         g_memmove(tmp, sigcontent, len+1);
596         tmp[len] = '\0';
597         gpgme_data_release(gpgtext);
598         g_free(textstr);
599         g_free(sigcontent);
600
601         if (msgcontent->content == MIMECONTENT_FILE &&
602             msgcontent->data.filename != NULL) {
603                 if (msgcontent->tmp == TRUE)
604                         claws_unlink(msgcontent->data.filename);
605                 g_free(msgcontent->data.filename);
606         }
607         msgcontent->data.mem = g_strdup(tmp);
608         msgcontent->content = MIMECONTENT_MEM;
609         g_free(tmp);
610
611         /* avoid all sorts of clear-signing problems with non ascii
612          * chars
613          */
614         procmime_encode_content(msgcontent, ENC_BASE64);
615         gpgme_release(ctx);
616
617         return TRUE;
618 }
619
620 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
621 {
622         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
623 }
624
625 static const gchar *pgpinline_get_encrypt_warning(void)
626 {
627         if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
628                 return NULL;
629         else
630                 return _("Please note that attachments are not encrypted by "
631                  "the PGP/Inline system, nor are email headers, like Subject.");
632 }
633
634 static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
635 {
636         if (inhibit)
637                 prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
638         else
639                 prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
640 }
641
642 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
643 {
644         MimeInfo *msgcontent;
645         FILE *fp;
646         gchar *enccontent;
647         size_t len;
648         gchar *textstr, *tmp;
649         gpgme_data_t gpgtext, gpgenc;
650         gpgme_ctx_t ctx;
651         gpgme_key_t *kset = NULL;
652         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
653         gpgme_error_t err;
654         gint i = 0;
655
656         while (fprs[i] && strlen(fprs[i])) {
657                 i++;
658         }
659         
660         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
661         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
662         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
663                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
664                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
665                 g_free(kset);
666                 return FALSE;
667         }
668         i = 0;
669         while (fprs[i] && strlen(fprs[i])) {
670                 gpgme_key_t key;
671                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
672                 if (err) {
673                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
674                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
675                         g_free(kset);
676                         return FALSE;
677                 }
678                 debug_print("found %s at %d\n", fprs[i], i);
679                 kset[i] = key;
680                 i++;
681         }
682         
683
684         debug_print("Encrypting message content\n");
685
686         /* get content node from message */
687         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
688         if (msgcontent->type == MIMETYPE_MULTIPART) {
689                 if (!msgcontent->node->children) {
690                         debug_print("msgcontent->node->children NULL, bailing\n");
691                         privacy_set_error(_("Malformed message"));
692                         g_free(kset);
693                         return FALSE;
694                 }
695                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
696         }
697         /* get rid of quoted-printable or anything */
698         procmime_decode_content(msgcontent);
699
700         fp = my_tmpfile();
701         if (fp == NULL) {
702                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
703                 perror("my_tmpfile");
704                 g_free(kset);
705                 return FALSE;
706         }
707         procmime_write_mimeinfo(msgcontent, fp);
708         rewind(fp);
709
710         /* read temporary file into memory */
711         textstr = fp_read_noconv(fp);
712         
713         fclose(fp);
714
715         /* encrypt data */
716         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
717         gpgme_data_new(&gpgenc);
718         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
719                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
720                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
721                 g_free(kset);
722                 return FALSE;
723         }
724         gpgme_set_armor(ctx, 1);
725
726         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
727
728         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
729         g_free(kset);
730
731         if (enccontent == NULL || len <= 0) {
732                 g_warning("sgpgme_data_release_and_get_mem failed");
733                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
734                 gpgme_data_release(gpgtext);
735                 g_free(textstr);
736                 gpgme_release(ctx);
737                 return FALSE;
738         }
739
740         tmp = g_malloc(len+1);
741         g_memmove(tmp, enccontent, len+1);
742         tmp[len] = '\0';
743         g_free(enccontent);
744
745         gpgme_data_release(gpgtext);
746         g_free(textstr);
747
748         if (msgcontent->content == MIMECONTENT_FILE &&
749             msgcontent->data.filename != NULL) {
750                 if (msgcontent->tmp == TRUE)
751                         claws_unlink(msgcontent->data.filename);
752                 g_free(msgcontent->data.filename);
753         }
754         msgcontent->data.mem = g_strdup(tmp);
755         msgcontent->content = MIMECONTENT_MEM;
756         g_free(tmp);
757         gpgme_release(ctx);
758
759         return TRUE;
760 }
761
762 static PrivacySystem pgpinline_system = {
763         "pgpinline",                    /* id */
764         "PGP Inline",                   /* name */
765
766         pgpinline_free_privacydata,     /* free_privacydata */
767
768         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
769         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
770         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
771         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
772         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
773
774         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
775         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
776
777         TRUE,
778         pgpinline_sign,
779
780         TRUE,
781         pgpinline_get_encrypt_data,
782         pgpinline_encrypt,
783         pgpinline_get_encrypt_warning,
784         pgpinline_inhibit_encrypt_warning,
785 };
786
787 void pgpinline_init()
788 {
789         privacy_register_system(&pgpinline_system);
790 }
791
792 void pgpinline_done()
793 {
794         privacy_unregister_system(&pgpinline_system);
795 }
796
797 struct PluginFeature *plugin_provides(void)
798 {
799         static struct PluginFeature features[] = 
800                 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
801                   {PLUGIN_NOTHING, NULL}};
802         return features;
803 }
804 #endif /* USE_GPGME */