2006-01-13 [paul] 1.9.100cvs144
[claws.git] / src / plugins / pgpinline / pgpinline.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  * This file (C) 2004 Colin Leroy <colin@colino.net>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 <gpgme.h>
30
31 #include "utils.h"
32 #include "privacy.h"
33 #include "procmime.h"
34 #include "pgpinline.h"
35 #include <plugins/pgpcore/sgpgme.h>
36 #include <plugins/pgpcore/prefs_gpg.h>
37 #include <plugins/pgpcore/passphrase.h>
38 #include "quoted-printable.h"
39 #include "base64.h"
40 #include "codeconv.h"
41
42 extern struct GPGConfig prefs_gpg;
43
44 typedef struct _PrivacyDataPGP PrivacyDataPGP;
45
46 struct _PrivacyDataPGP
47 {
48         PrivacyData     data;
49         
50         gboolean        done_sigtest;
51         gboolean        is_signed;
52         gpgme_verify_result_t   sigstatus;
53         gpgme_ctx_t     ctx;
54 };
55
56 static PrivacySystem pgpinline_system;
57
58 static gint pgpinline_check_signature(MimeInfo *mimeinfo);
59
60 static PrivacyDataPGP *pgpinline_new_privacydata()
61 {
62         PrivacyDataPGP *data;
63
64         data = g_new0(PrivacyDataPGP, 1);
65         data->data.system = &pgpinline_system;
66         data->done_sigtest = FALSE;
67         data->is_signed = FALSE;
68         data->sigstatus = NULL;
69         gpgme_new(&data->ctx);
70         
71         return data;
72 }
73
74 static void pgpinline_free_privacydata(PrivacyData *_data)
75 {
76         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
77         gpgme_release(data->ctx);
78         g_free(data);
79 }
80
81 static gchar *fp_read_noconv(FILE *fp)
82 {
83         GByteArray *array;
84         guchar buf[BUFSIZ];
85         gint n_read;
86         gchar *result = NULL;
87
88         if (!fp)
89                 return NULL;
90         array = g_byte_array_new();
91
92         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
93                 if (n_read < sizeof(buf) && ferror(fp))
94                         break;
95                 g_byte_array_append(array, buf, n_read);
96         }
97
98         if (ferror(fp)) {
99                 FILE_OP_ERROR("file stream", "fread");
100                 g_byte_array_free(array, TRUE);
101                 return NULL;
102         }
103
104         buf[0] = '\0';
105         g_byte_array_append(array, buf, 1);
106         result = (gchar *)array->data;
107         g_byte_array_free(array, FALSE);
108         
109         return result;
110 }
111
112 static gchar *get_part_as_string(MimeInfo *mimeinfo)
113 {
114         gchar *textdata = NULL;
115
116         g_return_val_if_fail(mimeinfo != NULL, 0);
117         procmime_decode_content(mimeinfo);
118         if (mimeinfo->content == MIMECONTENT_MEM)
119                 textdata = g_strdup(mimeinfo->data.mem);
120         else {
121                 /* equals file_read_to_str but without conversion */
122                 FILE *fp = fopen(mimeinfo->data.filename, "r");
123                 if (!fp)
124                         return NULL;
125                 textdata = fp_read_noconv(fp);
126                 fclose(fp);
127         }
128
129         if (!g_utf8_validate(textdata, -1, NULL)) {
130                 gchar *tmp = NULL;
131                 codeconv_set_strict(TRUE);
132                 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
133                         tmp = conv_codeset_strdup(textdata,
134                                 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
135                                 CS_UTF_8);
136                 }
137                 if (!tmp) {
138                         tmp = conv_codeset_strdup(textdata,
139                                 conv_get_locale_charset_str_no_utf8(), 
140                                 CS_UTF_8);
141                 }
142                 codeconv_set_strict(FALSE);
143                 if (!tmp) {
144                         tmp = conv_codeset_strdup(textdata,
145                                 conv_get_locale_charset_str_no_utf8(), 
146                                 CS_UTF_8);
147                 }
148                 if (tmp) {
149                         g_free(textdata);
150                         textdata = tmp;
151                 }
152         }
153
154         return textdata;        
155 }
156
157 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
158 {
159         PrivacyDataPGP *data = NULL;
160         const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
161         gchar *textdata, *sigpos;
162         
163         g_return_val_if_fail(mimeinfo != NULL, FALSE);
164         
165         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
166                 return FALSE; /* not parent */
167         
168         if (mimeinfo->type != MIMETYPE_TEXT &&
169                 (mimeinfo->type != MIMETYPE_APPLICATION ||
170                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
171                 return FALSE;
172
173         /* Seal the deal. This has to be text/plain through and through. */
174         if (mimeinfo->type == MIMETYPE_APPLICATION)
175         {
176                 mimeinfo->type = MIMETYPE_TEXT;
177                 g_free(mimeinfo->subtype);
178                 mimeinfo->subtype = g_strdup("plain");
179         }
180
181         if (mimeinfo->privacy != NULL) {
182                 data = (PrivacyDataPGP *) mimeinfo->privacy;
183                 if (data->done_sigtest)
184                         return data->is_signed;
185         }
186         
187         textdata = get_part_as_string(mimeinfo);
188         if (!textdata)
189                 return FALSE;
190         
191         if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
192                 g_free(textdata);
193                 return FALSE;
194         }
195
196         if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
197                 g_free(textdata);
198                 return FALSE;
199         }
200
201         g_free(textdata);
202
203         if (data == NULL) {
204                 data = pgpinline_new_privacydata();
205                 mimeinfo->privacy = (PrivacyData *) data;
206         }
207         data->done_sigtest = TRUE;
208         data->is_signed = TRUE;
209
210         return TRUE;
211 }
212
213 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
214 {
215         PrivacyDataPGP *data = NULL;
216         gchar *textdata = NULL, *tmp = NULL;
217         gpgme_data_t plain = NULL, cipher = NULL;
218         gpgme_ctx_t ctx;
219         
220         g_return_val_if_fail(mimeinfo != NULL, 0);
221
222         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
223                 return 0; /* not parent */
224         if (mimeinfo->type != MIMETYPE_TEXT)
225                 return 0;
226
227         g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
228         data = (PrivacyDataPGP *) mimeinfo->privacy;
229
230         textdata = get_part_as_string(mimeinfo);
231         
232         if (!textdata) {
233                 g_free(textdata);
234                 return 0;
235         }
236
237         /* gtk2: convert back from utf8 */
238         tmp = conv_codeset_strdup(textdata, CS_UTF_8,
239                         procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
240         if (!tmp) {
241                 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
242                         conv_get_locale_charset_str_no_utf8());
243         }
244         if (!tmp) {
245                 g_warning("Can't convert charset to anything sane\n");
246                 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
247         }
248         g_free(textdata);
249
250         if (!tmp)
251                 return 0;
252
253         textdata = g_strdup(tmp);
254         g_free(tmp);
255         
256         gpgme_new(&ctx);
257         gpgme_set_textmode(ctx, 1);
258         gpgme_set_armor(ctx, 1);
259         
260         gpgme_data_new_from_mem(&plain, textdata, strlen(textdata), 1);
261         gpgme_data_new(&cipher);
262
263         data->sigstatus = sgpgme_verify_signature(ctx, plain, NULL, cipher);
264         
265         gpgme_data_release(plain);
266         gpgme_data_release(cipher);
267         
268         g_free(textdata);
269         
270         return 0;
271 }
272
273 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
274 {
275         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
276         
277         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
278
279         if (data->sigstatus == NULL && 
280             prefs_gpg_get_config()->auto_check_signatures)
281                 pgpinline_check_signature(mimeinfo);
282
283         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
284 }
285
286 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
287 {
288         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
289         
290         g_return_val_if_fail(data != NULL, g_strdup("Error"));
291
292         if (data->sigstatus == NULL && 
293             prefs_gpg_get_config()->auto_check_signatures)
294                 pgpinline_check_signature(mimeinfo);
295         
296         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
297 }
298
299 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
300 {
301         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
302         
303         g_return_val_if_fail(data != NULL, g_strdup("Error"));
304
305         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
306 }
307
308
309
310 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
311 {
312         const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
313         gchar *textdata;
314         
315         g_return_val_if_fail(mimeinfo != NULL, FALSE);
316         
317         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
318                 return FALSE; /* not parent */
319         
320         if (mimeinfo->type != MIMETYPE_TEXT &&
321                 (mimeinfo->type != MIMETYPE_APPLICATION ||
322                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
323                 return FALSE;
324         
325         /* Seal the deal. This has to be text/plain through and through. */
326         if (mimeinfo->type == MIMETYPE_APPLICATION)
327         {
328                 mimeinfo->type = MIMETYPE_TEXT;
329                 g_free(mimeinfo->subtype);
330                 mimeinfo->subtype = g_strdup("plain");
331         }
332         
333         textdata = get_part_as_string(mimeinfo);
334         if (!textdata)
335                 return FALSE;
336         
337         if (!strstr(textdata, enc_indicator)) {
338                 g_free(textdata);
339                 return FALSE;
340         }
341
342         g_free(textdata);
343         return TRUE;
344 }
345
346 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
347 {
348         MimeInfo *decinfo, *parseinfo;
349         gpgme_data_t cipher, plain;
350         FILE *dstfp;
351         gchar *fname;
352         gchar *textdata = NULL;
353         static gint id = 0;
354         const gchar *src_codeset = NULL;
355         gpgme_verify_result_t sigstat = 0;
356         PrivacyDataPGP *data = NULL;
357         gpgme_ctx_t ctx;
358         gchar *chars;
359         size_t len;
360         
361         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
362                 return NULL;
363
364         gpgme_set_textmode(ctx, 1);
365         gpgme_set_armor(ctx, 1);
366
367         g_return_val_if_fail(mimeinfo != NULL, NULL);
368         g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
369         
370         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
371             mimeinfo->type != MIMETYPE_TEXT) {
372                 gpgme_release(ctx);
373                 return NULL;
374         }
375
376         textdata = get_part_as_string(mimeinfo);
377         if (!textdata) {
378                 gpgme_release(ctx);
379                 return NULL;
380         }
381
382         debug_print("decrypting '%s'\n", textdata);
383         gpgme_data_new_from_mem(&cipher, textdata, strlen(textdata), 1);
384
385         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
386         if (sigstat && !sigstat->signatures)
387                 sigstat = NULL;
388
389         gpgme_data_release(cipher);
390         
391         if (plain == NULL) {
392                 gpgme_release(ctx);
393                 return NULL;
394         }
395
396         fname = g_strdup_printf("%s%cplaintext.%08x",
397                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
398
399         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
400                 FILE_OP_ERROR(fname, "fopen");
401                 g_free(fname);
402                 gpgme_data_release(plain);
403                 gpgme_release(ctx);
404                 return NULL;
405         }
406
407         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
408         if (src_codeset == NULL)
409                 src_codeset = CS_ISO_8859_1;
410                 
411         fprintf(dstfp, "MIME-Version: 1.0\r\n"
412                         "Content-Type: text/plain; charset=%s\r\n"
413                         "Content-Transfer-Encoding: 8bit\r\n"
414                         "\r\n",
415                         src_codeset);
416         
417         chars = gpgme_data_release_and_get_mem(plain, &len);
418         if (len > 0)
419                 fwrite(chars, len, 1, dstfp);
420
421         fclose(dstfp);
422         
423         gpgme_data_release(plain);
424
425         parseinfo = procmime_scan_file(fname);
426         g_free(fname);
427         
428         if (parseinfo == NULL) {
429                 gpgme_release(ctx);
430                 return NULL;
431         }
432         decinfo = g_node_first_child(parseinfo->node) != NULL ?
433                 g_node_first_child(parseinfo->node)->data : NULL;
434                 
435         if (decinfo == NULL) {
436                 gpgme_release(ctx);
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
464 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
465 {
466         MimeInfo *msgcontent;
467         gchar *textstr, *tmp;
468         FILE *fp;
469         gchar *sigcontent;
470         gpgme_ctx_t ctx;
471         gpgme_data_t gpgtext, gpgsig;
472         guint len;
473         struct passphrase_cb_info_s info;
474
475         memset (&info, 0, sizeof info);
476
477         /* get content node from message */
478         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
479         if (msgcontent->type == MIMETYPE_MULTIPART)
480                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
481
482         /* get rid of quoted-printable or anything */
483         procmime_decode_content(msgcontent);
484
485         fp = my_tmpfile();
486         if (fp == NULL) {
487                 perror("my_tmpfile");
488                 return FALSE;
489         }
490         procmime_write_mimeinfo(msgcontent, fp);
491         rewind(fp);
492
493         /* read temporary file into memory */
494         textstr = fp_read_noconv(fp);
495         
496         fclose(fp);
497                 
498         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
499         gpgme_data_new(&gpgsig);
500         gpgme_new(&ctx);
501         gpgme_set_textmode(ctx, 1);
502         gpgme_set_armor(ctx, 1);
503
504         if (!sgpgme_setup_signers(ctx, account)) {
505                 gpgme_release(ctx);
506                 return FALSE;
507         }
508
509         if (!getenv("GPG_AGENT_INFO")) {
510                 info.c = ctx;
511                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
512         }
513
514         if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR) 
515             != GPG_ERR_NO_ERROR) {
516                 gpgme_release(ctx);
517                 return FALSE;
518         }
519
520         gpgme_release(ctx);
521         sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
522         tmp = g_malloc(len+1);
523         g_memmove(tmp, sigcontent, len+1);
524         tmp[len] = '\0';
525         gpgme_data_release(gpgtext);
526         g_free(textstr);
527         g_free(sigcontent);
528
529         if (msgcontent->content == MIMECONTENT_FILE &&
530             msgcontent->data.filename != NULL) {
531                 g_unlink(msgcontent->data.filename);
532                 g_free(msgcontent->data.filename);
533         }
534         msgcontent->data.mem = g_strdup(tmp);
535         msgcontent->content = MIMECONTENT_MEM;
536         g_free(tmp);
537
538         /* avoid all sorts of clear-signing problems with non ascii
539          * chars
540          */
541         procmime_encode_content(msgcontent, ENC_BASE64);
542                         
543         return TRUE;
544 }
545
546 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
547 {
548         return sgpgme_get_encrypt_data(recp_names);
549 }
550
551 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
552 {
553         MimeInfo *msgcontent;
554         FILE *fp;
555         gchar *enccontent;
556         guint len;
557         gchar *textstr, *tmp;
558         gpgme_data_t gpgtext, gpgenc;
559         gpgme_ctx_t ctx;
560         gpgme_key_t *kset = NULL;
561         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
562         gint i = 0;
563         while (fprs[i] && strlen(fprs[i])) {
564                 i++;
565         }
566         
567         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
568         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
569         gpgme_new(&ctx);
570         i = 0;
571         while (fprs[i] && strlen(fprs[i])) {
572                 gpgme_key_t key;
573                 gpgme_error_t err;
574                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
575                 if (err) {
576                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
577                         break;
578                 }
579                 debug_print("found %s at %d\n", fprs[i], i);
580                 kset[i] = key;
581                 i++;
582         }
583         
584
585         debug_print("Encrypting message content\n");
586
587         /* get content node from message */
588         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
589         if (msgcontent->type == MIMETYPE_MULTIPART)
590                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
591
592         /* get rid of quoted-printable or anything */
593         procmime_decode_content(msgcontent);
594
595         fp = my_tmpfile();
596         if (fp == NULL) {
597                 perror("my_tmpfile");
598                 return FALSE;
599         }
600         procmime_write_mimeinfo(msgcontent, fp);
601         rewind(fp);
602
603         /* read temporary file into memory */
604         textstr = fp_read_noconv(fp);
605         
606         fclose(fp);
607
608         /* encrypt data */
609         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
610         gpgme_data_new(&gpgenc);
611         gpgme_new(&ctx);
612         gpgme_set_armor(ctx, 1);
613
614         gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
615
616         gpgme_release(ctx);
617         enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
618
619         tmp = g_malloc(len+1);
620         g_memmove(tmp, enccontent, len+1);
621         tmp[len] = '\0';
622         g_free(enccontent);
623
624         gpgme_data_release(gpgtext);
625         g_free(textstr);
626
627         if (msgcontent->content == MIMECONTENT_FILE &&
628             msgcontent->data.filename != NULL) {
629                 g_unlink(msgcontent->data.filename);
630                 g_free(msgcontent->data.filename);
631         }
632         msgcontent->data.mem = g_strdup(tmp);
633         msgcontent->content = MIMECONTENT_MEM;
634         g_free(tmp);
635
636         return TRUE;
637 }
638
639 static PrivacySystem pgpinline_system = {
640         "pgpinline",                    /* id */
641         "PGP Inline",                   /* name */
642
643         pgpinline_free_privacydata,     /* free_privacydata */
644
645         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
646         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
647         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
648         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
649         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
650
651         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
652         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
653
654         TRUE,
655         pgpinline_sign,
656
657         TRUE,
658         pgpinline_get_encrypt_data,
659         pgpinline_encrypt,
660 };
661
662 void pgpinline_init()
663 {
664         privacy_register_system(&pgpinline_system);
665 }
666
667 void pgpinline_done()
668 {
669         privacy_unregister_system(&pgpinline_system);
670 }
671
672 #endif /* USE_GPGME */