2a60946fdbf5096bc267f0b898b749d9636e039b
[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         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
224 }
225
226 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
227 {
228         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
229
230         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
231
232         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
233 }
234
235 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
236 {
237         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
238         
239         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
240
241         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
242 }
243
244 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
245 {
246         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
247         const gchar *end_indicator = "-----END PGP MESSAGE-----";
248         gchar *textdata;
249         
250         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
251         
252         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
253                 return FALSE; /* not parent */
254         
255         if (mimeinfo->type != MIMETYPE_TEXT &&
256                 (mimeinfo->type != MIMETYPE_APPLICATION ||
257                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
258                 return FALSE;
259         
260         /* Seal the deal. This has to be text/plain through and through. */
261         if (mimeinfo->type == MIMETYPE_APPLICATION)
262         {
263                 mimeinfo->type = MIMETYPE_TEXT;
264                 g_free(mimeinfo->subtype);
265                 mimeinfo->subtype = g_strdup("plain");
266         }
267
268         textdata = get_part_as_string(mimeinfo);
269         if (!textdata)
270                 return FALSE;
271
272         if (!pgp_locate_armor_header(textdata, begin_indicator)) {
273                 g_free(textdata);
274                 return FALSE;
275         }
276         if (!pgp_locate_armor_header(textdata, end_indicator)) {
277                 g_free(textdata);
278                 return FALSE;
279         }
280
281         g_free(textdata);
282
283         return TRUE;
284 }
285
286 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
287 {
288         MimeInfo *decinfo, *parseinfo;
289         gpgme_data_t cipher, plain;
290         FILE *dstfp;
291         gchar *fname;
292         gchar *textdata = NULL;
293         static gint id = 0;
294         const gchar *src_codeset = NULL;
295         gpgme_verify_result_t sigstat = 0;
296         PrivacyDataPGP *data = NULL;
297         gpgme_ctx_t ctx;
298         gchar *chars;
299         size_t len;
300         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
301         const gchar *end_indicator = "-----END PGP MESSAGE-----";
302         gchar *pos;
303         
304         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
305                 return NULL;
306
307         gpgme_set_textmode(ctx, 1);
308         gpgme_set_armor(ctx, 1);
309
310         cm_return_val_if_fail(mimeinfo != NULL, NULL);
311         cm_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
312         
313         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
314             mimeinfo->type != MIMETYPE_TEXT) {
315                 gpgme_release(ctx);
316                 privacy_set_error(_("Couldn't parse mime part."));
317                 return NULL;
318         }
319
320         textdata = get_part_as_string(mimeinfo);
321         if (!textdata) {
322                 gpgme_release(ctx);
323                 privacy_set_error(_("Couldn't get text data."));
324                 return NULL;
325         }
326
327         debug_print("decrypting '%s'\n", textdata);
328         gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
329
330         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
331         if (sigstat && !sigstat->signatures)
332                 sigstat = NULL;
333
334         gpgme_data_release(cipher);
335         
336         if (plain == NULL) {
337                 gpgme_release(ctx);
338                 return NULL;
339         }
340
341         fname = g_strdup_printf("%s%cplaintext.%08x",
342                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
343
344         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
345                 FILE_OP_ERROR(fname, "fopen");
346                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
347                 g_free(fname);
348                 gpgme_data_release(plain);
349                 gpgme_release(ctx);
350                 return NULL;
351         }
352
353         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
354         if (src_codeset == NULL)
355                 src_codeset = CS_ISO_8859_1;
356                 
357         if (fprintf(dstfp, "MIME-Version: 1.0\r\n"
358                         "Content-Type: text/plain; charset=%s\r\n"
359                         "Content-Transfer-Encoding: 8bit\r\n"
360                         "\r\n",
361                         src_codeset) < 0) {
362                 FILE_OP_ERROR(fname, "fprintf");
363                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
364                 goto FILE_ERROR;
365         }
366
367         /* Store any part before encrypted text */
368         pos = pgp_locate_armor_header(textdata, begin_indicator);
369         if (pos != NULL && (pos - textdata) > 0) {
370             if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
371                 FILE_OP_ERROR(fname, "fwrite");
372                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
373                 goto FILE_ERROR;
374             }
375         }
376         
377         if (fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
378                 strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
379                 dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
380                 FILE_OP_ERROR(fname, "fwrite");
381                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
382                 goto FILE_ERROR;
383         }
384         chars = sgpgme_data_release_and_get_mem(plain, &len);
385         if (len > 0) {
386                 if (fwrite(chars, 1, len, dstfp) < len) {
387                         FILE_OP_ERROR(fname, "fwrite");
388                         g_free(chars);
389                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
390                         goto FILE_ERROR;
391                 }
392         }
393         g_free(chars);
394         /* Store any part after encrypted text */
395         if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
396                 strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
397                 dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
398                         FILE_OP_ERROR(fname, "fwrite");
399                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
400                         goto FILE_ERROR;
401         }
402         if (pos != NULL) {
403             pos = pgp_locate_armor_header(pos, end_indicator);
404             if (pos != NULL && *pos != '\0') {
405                 pos += strlen(end_indicator);
406                 if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
407                         FILE_OP_ERROR(fname, "fwrite");
408                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
409                         goto FILE_ERROR;
410                 }
411             }
412         }
413
414         if (fclose(dstfp) == EOF) {
415                 FILE_OP_ERROR(fname, "fclose");
416                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
417                 g_free(fname);
418                 gpgme_data_release(plain);
419                 gpgme_release(ctx);
420                 return NULL;
421         }
422         
423         parseinfo = procmime_scan_file(fname);
424         g_free(fname);
425         
426         if (parseinfo == NULL) {
427                 gpgme_release(ctx);
428                 privacy_set_error(_("Couldn't scan decrypted file."));
429                 return NULL;
430         }
431         decinfo = g_node_first_child(parseinfo->node) != NULL ?
432                 g_node_first_child(parseinfo->node)->data : NULL;
433                 
434         if (decinfo == NULL) {
435                 gpgme_release(ctx);
436                 privacy_set_error(_("Couldn't scan decrypted file parts."));
437                 return NULL;
438         }
439
440         g_node_unlink(decinfo->node);
441         procmime_mimeinfo_free_all(parseinfo);
442
443         decinfo->tmp = TRUE;
444
445         if (sigstat != GPGME_SIG_STAT_NONE) {
446                 if (decinfo->privacy != NULL) {
447                         data = (PrivacyDataPGP *) decinfo->privacy;
448                 } else {
449                         data = pgpinline_new_privacydata();
450                         decinfo->privacy = (PrivacyData *) data;        
451                 }
452                 data->done_sigtest = TRUE;
453                 data->is_signed = TRUE;
454                 data->sigstatus = sigstat;
455                 if (data->ctx)
456                         gpgme_release(data->ctx);
457                 data->ctx = ctx;
458         } else
459                 gpgme_release(ctx);
460
461         return decinfo;
462
463 FILE_ERROR:
464         fclose(dstfp);
465         g_free(fname);
466         gpgme_data_release(plain);
467         gpgme_release(ctx);
468         return NULL;
469 }
470
471 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
472 {
473         MimeInfo *msgcontent;
474         gchar *textstr, *tmp;
475         FILE *fp;
476         gchar *sigcontent;
477         gpgme_ctx_t ctx;
478         gpgme_data_t gpgtext, gpgsig;
479         size_t len;
480         gpgme_error_t err;
481         struct passphrase_cb_info_s info;
482         gpgme_sign_result_t result = NULL;
483
484         memset (&info, 0, sizeof info);
485
486         /* get content node from message */
487         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
488         if (msgcontent->type == MIMETYPE_MULTIPART) {
489                 if (!msgcontent->node->children) {
490                         debug_print("msgcontent->node->children NULL, bailing\n");
491                         privacy_set_error(_("Malformed message"));
492                         return FALSE;
493                 }
494                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
495         }
496         /* get rid of quoted-printable or anything */
497         procmime_decode_content(msgcontent);
498
499         fp = my_tmpfile();
500         if (fp == NULL) {
501                 perror("my_tmpfile");
502                 privacy_set_error(_("Couldn't create temporary file."));
503                 return FALSE;
504         }
505         procmime_write_mimeinfo(msgcontent, fp);
506         rewind(fp);
507
508         /* read temporary file into memory */
509         textstr = fp_read_noconv(fp);
510         
511         fclose(fp);
512                 
513         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
514         gpgme_data_new(&gpgsig);
515         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
516                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
517                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
518                 return FALSE;
519         }
520         gpgme_set_textmode(ctx, 1);
521         gpgme_set_armor(ctx, 1);
522
523         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
524                 gpgme_release(ctx);
525                 return FALSE;
526         }
527
528         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
529         if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
530                 info.c = ctx;
531                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
532         }
533
534         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
535         if (err != GPG_ERR_NO_ERROR) {
536                 if (err == GPG_ERR_CANCELED) {
537                         /* ignore cancelled signing */
538                         privacy_reset_error();
539                         debug_print("gpgme_op_sign cancelled\n");
540                 } else {
541                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
542                         debug_print("gpgme_op_sign error : %x\n", err);
543                 }
544                 gpgme_release(ctx);
545                 return FALSE;
546         }
547         result = gpgme_op_sign_result(ctx);
548         if (result && result->signatures) {
549                 gpgme_new_signature_t sig = result->signatures;
550                 while (sig) {
551                         debug_print("valid signature: %s\n", sig->fpr);
552                         sig = sig->next;
553                 }
554         } else if (result && result->invalid_signers) {
555                 gpgme_invalid_key_t invalid = result->invalid_signers;
556                 while (invalid) {
557                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
558                                 gpgme_strerror(invalid->reason));
559                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
560                                 gpgme_strerror(invalid->reason));
561                         invalid = invalid->next;
562                 }
563                 gpgme_release(ctx);
564                 return FALSE;
565         } else {
566                 /* can't get result (maybe no signing key?) */
567                 debug_print("gpgme_op_sign_result error\n");
568                 privacy_set_error(_("Data signing failed, no results."));
569                 gpgme_release(ctx);
570                 return FALSE;
571         }
572
573
574         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
575         
576         if (sigcontent == NULL || len <= 0) {
577                 g_warning("sgpgme_data_release_and_get_mem failed");
578                 privacy_set_error(_("Data signing failed, no contents."));
579                 gpgme_data_release(gpgtext);
580                 g_free(textstr);
581                 g_free(sigcontent);
582                 gpgme_release(ctx);
583                 return FALSE;
584         }
585
586         tmp = g_malloc(len+1);
587         g_memmove(tmp, sigcontent, len+1);
588         tmp[len] = '\0';
589         gpgme_data_release(gpgtext);
590         g_free(textstr);
591         g_free(sigcontent);
592
593         if (msgcontent->content == MIMECONTENT_FILE &&
594             msgcontent->data.filename != NULL) {
595                 if (msgcontent->tmp == TRUE)
596                         claws_unlink(msgcontent->data.filename);
597                 g_free(msgcontent->data.filename);
598         }
599         msgcontent->data.mem = g_strdup(tmp);
600         msgcontent->content = MIMECONTENT_MEM;
601         g_free(tmp);
602
603         /* avoid all sorts of clear-signing problems with non ascii
604          * chars
605          */
606         procmime_encode_content(msgcontent, ENC_BASE64);
607         gpgme_release(ctx);
608
609         return TRUE;
610 }
611
612 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
613 {
614         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
615 }
616
617 static const gchar *pgpinline_get_encrypt_warning(void)
618 {
619         if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
620                 return NULL;
621         else
622                 return _("Please note that attachments are not encrypted by "
623                  "the PGP/Inline system, nor are email headers, like Subject.");
624 }
625
626 static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
627 {
628         if (inhibit)
629                 prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
630         else
631                 prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
632 }
633
634 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
635 {
636         MimeInfo *msgcontent;
637         FILE *fp;
638         gchar *enccontent;
639         size_t len;
640         gchar *textstr, *tmp;
641         gpgme_data_t gpgtext, gpgenc;
642         gpgme_ctx_t ctx;
643         gpgme_key_t *kset = NULL;
644         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
645         gpgme_error_t err;
646         gint i = 0;
647
648         while (fprs[i] && strlen(fprs[i])) {
649                 i++;
650         }
651         
652         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
653         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
654         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
655                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
656                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
657                 g_free(kset);
658                 return FALSE;
659         }
660         i = 0;
661         while (fprs[i] && strlen(fprs[i])) {
662                 gpgme_key_t key;
663                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
664                 if (err) {
665                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
666                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
667                         g_free(kset);
668                         return FALSE;
669                 }
670                 debug_print("found %s at %d\n", fprs[i], i);
671                 kset[i] = key;
672                 i++;
673         }
674         
675
676         debug_print("Encrypting message content\n");
677
678         /* get content node from message */
679         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
680         if (msgcontent->type == MIMETYPE_MULTIPART) {
681                 if (!msgcontent->node->children) {
682                         debug_print("msgcontent->node->children NULL, bailing\n");
683                         privacy_set_error(_("Malformed message"));
684                         g_free(kset);
685                         return FALSE;
686                 }
687                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
688         }
689         /* get rid of quoted-printable or anything */
690         procmime_decode_content(msgcontent);
691
692         fp = my_tmpfile();
693         if (fp == NULL) {
694                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
695                 perror("my_tmpfile");
696                 g_free(kset);
697                 return FALSE;
698         }
699         procmime_write_mimeinfo(msgcontent, fp);
700         rewind(fp);
701
702         /* read temporary file into memory */
703         textstr = fp_read_noconv(fp);
704         
705         fclose(fp);
706
707         /* encrypt data */
708         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
709         gpgme_data_new(&gpgenc);
710         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
711                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
712                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
713                 g_free(kset);
714                 return FALSE;
715         }
716         gpgme_set_armor(ctx, 1);
717
718         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
719
720         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
721         g_free(kset);
722
723         if (enccontent == NULL || len <= 0) {
724                 g_warning("sgpgme_data_release_and_get_mem failed");
725                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
726                 gpgme_data_release(gpgtext);
727                 g_free(textstr);
728                 gpgme_release(ctx);
729                 return FALSE;
730         }
731
732         tmp = g_malloc(len+1);
733         g_memmove(tmp, enccontent, len+1);
734         tmp[len] = '\0';
735         g_free(enccontent);
736
737         gpgme_data_release(gpgtext);
738         g_free(textstr);
739
740         if (msgcontent->content == MIMECONTENT_FILE &&
741             msgcontent->data.filename != NULL) {
742                 if (msgcontent->tmp == TRUE)
743                         claws_unlink(msgcontent->data.filename);
744                 g_free(msgcontent->data.filename);
745         }
746         msgcontent->data.mem = g_strdup(tmp);
747         msgcontent->content = MIMECONTENT_MEM;
748         g_free(tmp);
749         gpgme_release(ctx);
750
751         return TRUE;
752 }
753
754 static PrivacySystem pgpinline_system = {
755         "pgpinline",                    /* id */
756         "PGP Inline",                   /* name */
757
758         pgpinline_free_privacydata,     /* free_privacydata */
759
760         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
761         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
762         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
763         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
764         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
765
766         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
767         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
768
769         TRUE,
770         pgpinline_sign,
771
772         TRUE,
773         pgpinline_get_encrypt_data,
774         pgpinline_encrypt,
775         pgpinline_get_encrypt_warning,
776         pgpinline_inhibit_encrypt_warning,
777         prefs_gpg_auto_check_signatures,
778 };
779
780 void pgpinline_init()
781 {
782         privacy_register_system(&pgpinline_system);
783 }
784
785 void pgpinline_done()
786 {
787         privacy_unregister_system(&pgpinline_system);
788 }
789
790 struct PluginFeature *plugin_provides(void)
791 {
792         static struct PluginFeature features[] = 
793                 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
794                   {PLUGIN_NOTHING, NULL}};
795         return features;
796 }
797 #endif /* USE_GPGME */