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