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