2005-10-27 [paul] 1.9.15cvs116
[claws.git] / src / plugins / pgpinline / pgpinline.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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                 return FALSE;
170         
171         if (mimeinfo->privacy != NULL) {
172                 data = (PrivacyDataPGP *) mimeinfo->privacy;
173                 if (data->done_sigtest)
174                         return data->is_signed;
175         }
176         
177         textdata = get_part_as_string(mimeinfo);
178         if (!textdata)
179                 return FALSE;
180         
181         if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
182                 g_free(textdata);
183                 return FALSE;
184         }
185
186         if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
187                 g_free(textdata);
188                 return FALSE;
189         }
190
191         g_free(textdata);
192
193         if (data == NULL) {
194                 data = pgpinline_new_privacydata();
195                 mimeinfo->privacy = (PrivacyData *) data;
196         }
197         data->done_sigtest = TRUE;
198         data->is_signed = TRUE;
199
200         return TRUE;
201 }
202
203 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
204 {
205         PrivacyDataPGP *data = NULL;
206         gchar *textdata = NULL, *tmp = NULL;
207         gpgme_data_t plain = NULL, cipher = NULL;
208         gpgme_ctx_t ctx;
209         
210         g_return_val_if_fail(mimeinfo != NULL, 0);
211
212         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
213                 return 0; /* not parent */
214         if (mimeinfo->type != MIMETYPE_TEXT)
215                 return 0;
216
217         g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
218         data = (PrivacyDataPGP *) mimeinfo->privacy;
219
220         textdata = get_part_as_string(mimeinfo);
221         
222         if (!textdata) {
223                 g_free(textdata);
224                 return 0;
225         }
226
227         /* gtk2: convert back from utf8 */
228         tmp = conv_codeset_strdup(textdata, CS_UTF_8,
229                         procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
230         if (!tmp) {
231                 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
232                         conv_get_locale_charset_str_no_utf8());
233         }
234         if (!tmp) {
235                 g_warning("Can't convert charset to anything sane\n");
236                 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
237         }
238         g_free(textdata);
239
240         if (!tmp)
241                 return 0;
242
243         textdata = g_strdup(tmp);
244         g_free(tmp);
245         
246         gpgme_new(&ctx);
247         gpgme_set_textmode(ctx, 1);
248         gpgme_set_armor(ctx, 1);
249         
250         gpgme_data_new_from_mem(&plain, textdata, strlen(textdata), 1);
251         gpgme_data_new(&cipher);
252
253         data->sigstatus = sgpgme_verify_signature(ctx, plain, NULL, cipher);
254         
255         gpgme_data_release(plain);
256         gpgme_data_release(cipher);
257         
258         g_free(textdata);
259         
260         return 0;
261 }
262
263 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
264 {
265         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
266         
267         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
268
269         if (data->sigstatus == NULL && 
270             prefs_gpg_get_config()->auto_check_signatures)
271                 pgpinline_check_signature(mimeinfo);
272
273         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
274 }
275
276 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
277 {
278         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
279         
280         g_return_val_if_fail(data != NULL, g_strdup("Error"));
281
282         if (data->sigstatus == NULL && 
283             prefs_gpg_get_config()->auto_check_signatures)
284                 pgpinline_check_signature(mimeinfo);
285         
286         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
287 }
288
289 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
290 {
291         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
292         
293         g_return_val_if_fail(data != NULL, g_strdup("Error"));
294
295         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
296 }
297
298
299
300 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
301 {
302         const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
303         gchar *textdata;
304         
305         g_return_val_if_fail(mimeinfo != NULL, FALSE);
306         
307         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
308                 return FALSE; /* not parent */
309         
310         if (mimeinfo->type != MIMETYPE_TEXT)
311                 return FALSE;
312         
313         textdata = get_part_as_string(mimeinfo);
314         if (!textdata)
315                 return FALSE;
316         
317         if (!strstr(textdata, enc_indicator)) {
318                 g_free(textdata);
319                 return FALSE;
320         }
321
322         g_free(textdata);
323         return TRUE;
324 }
325
326 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
327 {
328         MimeInfo *decinfo, *parseinfo;
329         gpgme_data_t cipher, plain;
330         FILE *dstfp;
331         gchar *fname;
332         gchar *textdata = NULL;
333         static gint id = 0;
334         const gchar *src_codeset = NULL;
335         gpgme_verify_result_t sigstat = 0;
336         PrivacyDataPGP *data = NULL;
337         gpgme_ctx_t ctx;
338         gchar *chars;
339         size_t len;
340         
341         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
342                 return NULL;
343
344         gpgme_set_textmode(ctx, 1);
345         gpgme_set_armor(ctx, 1);
346
347         g_return_val_if_fail(mimeinfo != NULL, NULL);
348         g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
349         
350         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
351             mimeinfo->type != MIMETYPE_TEXT) {
352                 gpgme_release(ctx);
353                 return NULL;
354         }
355
356         textdata = get_part_as_string(mimeinfo);
357         if (!textdata) {
358                 gpgme_release(ctx);
359                 return NULL;
360         }
361
362         debug_print("decrypting '%s'\n", textdata);
363         gpgme_data_new_from_mem(&cipher, textdata, strlen(textdata), 1);
364
365         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
366         if (sigstat && !sigstat->signatures)
367                 sigstat = NULL;
368
369         gpgme_data_release(cipher);
370         
371         if (plain == NULL) {
372                 gpgme_release(ctx);
373                 return NULL;
374         }
375
376         fname = g_strdup_printf("%s%cplaintext.%08x",
377                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
378
379         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
380                 FILE_OP_ERROR(fname, "fopen");
381                 g_free(fname);
382                 gpgme_data_release(plain);
383                 gpgme_release(ctx);
384                 return NULL;
385         }
386
387         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
388         if (src_codeset == NULL)
389                 src_codeset = CS_ISO_8859_1;
390                 
391         fprintf(dstfp, "MIME-Version: 1.0\r\n"
392                         "Content-Type: text/plain; charset=%s\r\n"
393                         "Content-Transfer-Encoding: 8bit\r\n"
394                         "\r\n",
395                         src_codeset);
396         
397         chars = gpgme_data_release_and_get_mem(plain, &len);
398         if (len > 0)
399                 fwrite(chars, len, 1, dstfp);
400
401         fclose(dstfp);
402         
403         gpgme_data_release(plain);
404
405         parseinfo = procmime_scan_file(fname);
406         g_free(fname);
407         
408         if (parseinfo == NULL) {
409                 gpgme_release(ctx);
410                 return NULL;
411         }
412         decinfo = g_node_first_child(parseinfo->node) != NULL ?
413                 g_node_first_child(parseinfo->node)->data : NULL;
414                 
415         if (decinfo == NULL) {
416                 gpgme_release(ctx);
417                 return NULL;
418         }
419
420         g_node_unlink(decinfo->node);
421         procmime_mimeinfo_free_all(parseinfo);
422
423         decinfo->tmp = TRUE;
424
425         if (sigstat != GPGME_SIG_STAT_NONE) {
426                 if (decinfo->privacy != NULL) {
427                         data = (PrivacyDataPGP *) decinfo->privacy;
428                 } else {
429                         data = pgpinline_new_privacydata();
430                         decinfo->privacy = (PrivacyData *) data;        
431                 }
432                 data->done_sigtest = TRUE;
433                 data->is_signed = TRUE;
434                 data->sigstatus = sigstat;
435                 if (data->ctx)
436                         gpgme_release(data->ctx);
437                 data->ctx = ctx;
438         } else
439                 gpgme_release(ctx);
440
441         return decinfo;
442 }
443
444 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
445 {
446         MimeInfo *msgcontent;
447         gchar *textstr, *tmp;
448         FILE *fp;
449         gchar *sigcontent;
450         gpgme_ctx_t ctx;
451         gpgme_data_t gpgtext, gpgsig;
452         guint len;
453         struct passphrase_cb_info_s info;
454
455         memset (&info, 0, sizeof info);
456
457         /* get content node from message */
458         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
459         if (msgcontent->type == MIMETYPE_MULTIPART)
460                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
461
462         /* get rid of quoted-printable or anything */
463         procmime_decode_content(msgcontent);
464
465         fp = my_tmpfile();
466         if (fp == NULL) {
467                 perror("my_tmpfile");
468                 return FALSE;
469         }
470         procmime_write_mimeinfo(msgcontent, fp);
471         rewind(fp);
472
473         /* read temporary file into memory */
474         textstr = fp_read_noconv(fp);
475         
476         fclose(fp);
477                 
478         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
479         gpgme_data_new(&gpgsig);
480         gpgme_new(&ctx);
481         gpgme_set_textmode(ctx, 1);
482         gpgme_set_armor(ctx, 1);
483
484         if (!sgpgme_setup_signers(ctx, account)) {
485                 gpgme_release(ctx);
486                 return FALSE;
487         }
488
489         if (!getenv("GPG_AGENT_INFO")) {
490                 info.c = ctx;
491                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
492         }
493
494         if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR) 
495             != GPG_ERR_NO_ERROR) {
496                 gpgme_release(ctx);
497                 return FALSE;
498         }
499
500         gpgme_release(ctx);
501         sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
502         tmp = g_malloc(len+1);
503         g_memmove(tmp, sigcontent, len+1);
504         tmp[len] = '\0';
505         gpgme_data_release(gpgtext);
506         g_free(textstr);
507         g_free(sigcontent);
508
509         if (msgcontent->content == MIMECONTENT_FILE &&
510             msgcontent->data.filename != NULL) {
511                 g_unlink(msgcontent->data.filename);
512                 g_free(msgcontent->data.filename);
513         }
514         msgcontent->data.mem = g_strdup(tmp);
515         msgcontent->content = MIMECONTENT_MEM;
516         g_free(tmp);
517
518         /* avoid all sorts of clear-signing problems with non ascii
519          * chars
520          */
521         procmime_encode_content(msgcontent, ENC_BASE64);
522                         
523         return TRUE;
524 }
525
526 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
527 {
528         return sgpgme_get_encrypt_data(recp_names);
529 }
530
531 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
532 {
533         MimeInfo *msgcontent;
534         FILE *fp;
535         gchar *enccontent;
536         guint len;
537         gchar *textstr, *tmp;
538         gpgme_data_t gpgtext, gpgenc;
539         gpgme_ctx_t ctx;
540         gpgme_key_t *kset = NULL;
541         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
542         gint i = 0;
543         while (fprs[i] && strlen(fprs[i])) {
544                 i++;
545         }
546         
547         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
548         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
549         gpgme_new(&ctx);
550         i = 0;
551         while (fprs[i] && strlen(fprs[i])) {
552                 gpgme_key_t key;
553                 gpgme_error_t err;
554                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
555                 if (err) {
556                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
557                         break;
558                 }
559                 debug_print("found %s at %d\n", fprs[i], i);
560                 kset[i] = key;
561                 i++;
562         }
563         
564
565         debug_print("Encrypting message content\n");
566
567         /* get content node from message */
568         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
569         if (msgcontent->type == MIMETYPE_MULTIPART)
570                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
571
572         /* get rid of quoted-printable or anything */
573         procmime_decode_content(msgcontent);
574
575         fp = my_tmpfile();
576         if (fp == NULL) {
577                 perror("my_tmpfile");
578                 return FALSE;
579         }
580         procmime_write_mimeinfo(msgcontent, fp);
581         rewind(fp);
582
583         /* read temporary file into memory */
584         textstr = fp_read_noconv(fp);
585         
586         fclose(fp);
587
588         /* encrypt data */
589         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
590         gpgme_data_new(&gpgenc);
591         gpgme_new(&ctx);
592         gpgme_set_armor(ctx, 1);
593
594         gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
595
596         gpgme_release(ctx);
597         enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
598
599         tmp = g_malloc(len+1);
600         g_memmove(tmp, enccontent, len+1);
601         tmp[len] = '\0';
602         g_free(enccontent);
603
604         gpgme_data_release(gpgtext);
605         g_free(textstr);
606
607         if (msgcontent->content == MIMECONTENT_FILE &&
608             msgcontent->data.filename != NULL) {
609                 g_unlink(msgcontent->data.filename);
610                 g_free(msgcontent->data.filename);
611         }
612         msgcontent->data.mem = g_strdup(tmp);
613         msgcontent->content = MIMECONTENT_MEM;
614         g_free(tmp);
615
616         return TRUE;
617 }
618
619 static PrivacySystem pgpinline_system = {
620         "pgpinline",                    /* id */
621         "PGP Inline",                   /* name */
622
623         pgpinline_free_privacydata,     /* free_privacydata */
624
625         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
626         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
627         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
628         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
629         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
630
631         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
632         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
633
634         TRUE,
635         pgpinline_sign,
636
637         TRUE,
638         pgpinline_get_encrypt_data,
639         pgpinline_encrypt,
640 };
641
642 void pgpinline_init()
643 {
644         privacy_register_system(&pgpinline_system);
645 }
646
647 void pgpinline_done()
648 {
649         privacy_unregister_system(&pgpinline_system);
650 }
651
652 #endif /* USE_GPGME */