2005-07-19 [colin] 1.9.12cvs84
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *get_part_as_string(MimeInfo *mimeinfo)
82 {
83         gchar *textdata = NULL;
84
85         g_return_val_if_fail(mimeinfo != NULL, 0);
86         procmime_decode_content(mimeinfo);
87         if (mimeinfo->content == MIMECONTENT_MEM)
88                 textdata = g_strdup(mimeinfo->data.mem);
89         else
90                 textdata = file_read_to_str(mimeinfo->data.filename);
91
92         return textdata;        
93 }
94
95 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
96 {
97         PrivacyDataPGP *data = NULL;
98         const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
99         gchar *textdata, *sigpos;
100         
101         g_return_val_if_fail(mimeinfo != NULL, FALSE);
102         
103         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
104                 return FALSE; /* not parent */
105
106         if (mimeinfo->type != MIMETYPE_TEXT)
107                 return FALSE;
108         
109         if (mimeinfo->privacy != NULL) {
110                 data = (PrivacyDataPGP *) mimeinfo->privacy;
111                 if (data->done_sigtest)
112                         return data->is_signed;
113         }
114         
115         textdata = get_part_as_string(mimeinfo);
116         if (!textdata)
117                 return FALSE;
118         
119         if (data == NULL) {
120                 data = pgpinline_new_privacydata();
121                 mimeinfo->privacy = (PrivacyData *) data;
122         }
123         data->done_sigtest = TRUE;
124         data->is_signed = FALSE;
125
126         if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
127                 g_free(textdata);
128                 return FALSE;
129         }
130
131         if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
132                 g_free(textdata);
133                 return FALSE;
134         }
135
136         g_free(textdata);
137
138         data->is_signed = TRUE;
139
140         return TRUE;
141 }
142
143 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
144 {
145         PrivacyDataPGP *data = NULL;
146         gchar *textdata = NULL, *tmp = NULL;
147         gpgme_data_t plain = NULL, cipher = NULL;
148         gpgme_ctx_t ctx;
149         
150         g_return_val_if_fail(mimeinfo != NULL, 0);
151
152         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
153                 return 0; /* not parent */
154         if (mimeinfo->type != MIMETYPE_TEXT)
155                 return 0;
156
157         g_return_val_if_fail(mimeinfo->privacy != NULL, 0);
158         data = (PrivacyDataPGP *) mimeinfo->privacy;
159
160         textdata = get_part_as_string(mimeinfo);
161         
162         if (!textdata) {
163                 g_free(textdata);
164                 return 0;
165         }
166
167         /* gtk2: convert back from utf8 */
168         tmp = conv_codeset_strdup(textdata, CS_UTF_8,
169                         procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
170         g_free(textdata);
171         textdata = g_strdup(tmp);
172         g_free(tmp);
173         
174         gpgme_new(&ctx);
175         gpgme_set_textmode(ctx, 1);
176         gpgme_set_armor(ctx, 1);
177         
178         gpgme_data_new_from_mem(&plain, textdata, strlen(textdata), 1);
179         gpgme_data_new(&cipher);
180
181         data->sigstatus = sgpgme_verify_signature(ctx, plain, NULL, cipher);
182         
183         gpgme_data_release(plain);
184         gpgme_data_release(cipher);
185         
186         g_free(textdata);
187         
188         return 0;
189 }
190
191 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
192 {
193         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
194         
195         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
196
197         if (data->sigstatus == NULL && 
198             prefs_gpg_get_config()->auto_check_signatures)
199                 pgpinline_check_signature(mimeinfo);
200
201         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
202 }
203
204 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
205 {
206         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
207         
208         g_return_val_if_fail(data != NULL, g_strdup("Error"));
209
210         if (data->sigstatus == NULL && 
211             prefs_gpg_get_config()->auto_check_signatures)
212                 pgpinline_check_signature(mimeinfo);
213         
214         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
215 }
216
217 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
218 {
219         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
220         
221         g_return_val_if_fail(data != NULL, g_strdup("Error"));
222
223         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
224 }
225
226
227
228 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
229 {
230         const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
231         gchar *textdata;
232         
233         g_return_val_if_fail(mimeinfo != NULL, FALSE);
234         
235         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
236                 return FALSE; /* not parent */
237         
238         if (mimeinfo->type != MIMETYPE_TEXT)
239                 return FALSE;
240         
241         textdata = get_part_as_string(mimeinfo);
242         if (!textdata)
243                 return FALSE;
244         
245         if (!strstr(textdata, enc_indicator)) {
246                 g_free(textdata);
247                 return FALSE;
248         }
249
250         g_free(textdata);
251         return TRUE;
252 }
253
254 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
255 {
256         MimeInfo *decinfo, *parseinfo;
257         gpgme_data_t cipher, plain;
258         FILE *dstfp;
259         gchar *fname;
260         gchar *textdata = NULL;
261         static gint id = 0;
262         const gchar *src_codeset = NULL;
263         gpgme_verify_result_t sigstat = 0;
264         PrivacyDataPGP *data = NULL;
265         gpgme_ctx_t ctx;
266         gchar *chars;
267         int len;
268         
269         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
270                 return NULL;
271
272         gpgme_set_textmode(ctx, 1);
273         gpgme_set_armor(ctx, 1);
274
275         g_return_val_if_fail(mimeinfo != NULL, NULL);
276         g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
277         
278         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
279             mimeinfo->type != MIMETYPE_TEXT) {
280                 gpgme_release(ctx);
281                 return NULL;
282         }
283
284         textdata = get_part_as_string(mimeinfo);
285         if (!textdata) {
286                 gpgme_release(ctx);
287                 return NULL;
288         }
289
290         debug_print("decrypting '%s'\n", textdata);
291         gpgme_data_new_from_mem(&cipher, textdata, strlen(textdata), 1);
292
293         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
294         if (sigstat && !sigstat->signatures)
295                 sigstat = NULL;
296
297         gpgme_data_release(cipher);
298         
299         if (plain == NULL) {
300                 gpgme_release(ctx);
301                 return NULL;
302         }
303
304         fname = g_strdup_printf("%s%cplaintext.%08x",
305                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
306
307         if ((dstfp = fopen(fname, "wb")) == NULL) {
308                 FILE_OP_ERROR(fname, "fopen");
309                 g_free(fname);
310                 gpgme_data_release(plain);
311                 gpgme_release(ctx);
312                 return NULL;
313         }
314
315         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
316         if (src_codeset == NULL)
317                 src_codeset = CS_ISO_8859_1;
318                 
319         fprintf(dstfp, "MIME-Version: 1.0\r\n"
320                         "Content-Type: text/plain; charset=%s\r\n"
321                         "Content-Transfer-Encoding: 8bit\r\n"
322                         "\r\n",
323                         src_codeset);
324         
325         chars = gpgme_data_release_and_get_mem(plain, &len);
326         if (len > 0)
327                 fwrite(chars, len, 1, dstfp);
328
329         fclose(dstfp);
330         
331         gpgme_data_release(plain);
332
333         parseinfo = procmime_scan_file(fname);
334         g_free(fname);
335         
336         if (parseinfo == NULL) {
337                 gpgme_release(ctx);
338                 return NULL;
339         }
340         decinfo = g_node_first_child(parseinfo->node) != NULL ?
341                 g_node_first_child(parseinfo->node)->data : NULL;
342                 
343         if (decinfo == NULL) {
344                 gpgme_release(ctx);
345                 return NULL;
346         }
347
348         g_node_unlink(decinfo->node);
349         procmime_mimeinfo_free_all(parseinfo);
350
351         decinfo->tmp = TRUE;
352
353         if (sigstat != GPGME_SIG_STAT_NONE) {
354                 if (decinfo->privacy != NULL) {
355                         data = (PrivacyDataPGP *) decinfo->privacy;
356                 } else {
357                         data = pgpinline_new_privacydata();
358                         decinfo->privacy = (PrivacyData *) data;        
359                 }
360                 data->done_sigtest = TRUE;
361                 data->is_signed = TRUE;
362                 data->sigstatus = sigstat;
363                 if (data->ctx)
364                         gpgme_release(data->ctx);
365                 data->ctx = ctx;
366         } else
367                 gpgme_release(ctx);
368
369         return decinfo;
370 }
371
372 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
373 {
374         MimeInfo *msgcontent;
375         gchar *textstr, *tmp;
376         FILE *fp;
377         gchar *sigcontent;
378         gpgme_ctx_t ctx;
379         gpgme_data_t gpgtext, gpgsig;
380         guint len;
381         struct passphrase_cb_info_s info;
382
383         memset (&info, 0, sizeof info);
384
385         /* get content node from message */
386         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
387         if (msgcontent->type == MIMETYPE_MULTIPART)
388                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
389
390         /* get rid of quoted-printable or anything */
391         procmime_decode_content(msgcontent);
392
393         fp = my_tmpfile();
394         procmime_write_mimeinfo(msgcontent, fp);
395         rewind(fp);
396
397         /* read temporary file into memory */
398         textstr = file_read_stream_to_str(fp);
399         
400         /* gtk2: convert back from utf8 */
401         tmp = conv_codeset_strdup(textstr, CS_UTF_8, 
402                         procmime_mimeinfo_get_parameter(msgcontent, "charset"));
403         g_free(textstr);
404         textstr = g_strdup(tmp);
405         g_free(tmp);
406
407         fclose(fp);
408                 
409         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
410         gpgme_data_new(&gpgsig);
411         gpgme_new(&ctx);
412         gpgme_set_textmode(ctx, 1);
413         gpgme_set_armor(ctx, 1);
414
415         if (!sgpgme_setup_signers(ctx, account)) {
416                 gpgme_release(ctx);
417                 return FALSE;
418         }
419
420         if (!getenv("GPG_AGENT_INFO")) {
421                 info.c = ctx;
422                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
423         }
424
425         if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR) 
426             != GPG_ERR_NO_ERROR) {
427                 gpgme_release(ctx);
428                 return FALSE;
429         }
430
431         gpgme_release(ctx);
432         sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
433         tmp = g_malloc(len+1);
434         g_memmove(tmp, sigcontent, len+1);
435         tmp[len] = '\0';
436         gpgme_data_release(gpgtext);
437         g_free(textstr);
438         g_free(sigcontent);
439
440         if (msgcontent->content == MIMECONTENT_FILE &&
441             msgcontent->data.filename != NULL) {
442                 unlink(msgcontent->data.filename);
443                 g_free(msgcontent->data.filename);
444         }
445         msgcontent->data.mem = g_strdup(tmp);
446         msgcontent->content = MIMECONTENT_MEM;
447         g_free(tmp);
448
449         /* avoid all sorts of clear-signing problems with non ascii
450          * chars
451          */
452         procmime_encode_content(msgcontent, ENC_BASE64);
453                         
454         return TRUE;
455 }
456
457 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
458 {
459         return sgpgme_get_encrypt_data(recp_names);
460 }
461
462 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
463 {
464         MimeInfo *msgcontent;
465         FILE *fp;
466         gchar *enccontent;
467         guint len;
468         gchar *textstr, *tmp;
469         gpgme_data_t gpgtext, gpgenc;
470         gpgme_ctx_t ctx;
471         gpgme_key_t *kset = NULL;
472         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
473         gint i = 0;
474         while (fprs[i] && strlen(fprs[i])) {
475                 i++;
476         }
477         
478         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
479         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
480         gpgme_new(&ctx);
481         i = 0;
482         while (fprs[i] && strlen(fprs[i])) {
483                 gpgme_key_t key;
484                 gpgme_error_t err;
485                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
486                 if (err) {
487                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
488                         break;
489                 }
490                 debug_print("found %s at %d\n", fprs[i], i);
491                 kset[i] = key;
492                 i++;
493         }
494         
495
496         debug_print("Encrypting message content\n");
497
498         /* get content node from message */
499         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
500         if (msgcontent->type == MIMETYPE_MULTIPART)
501                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
502
503         /* get rid of quoted-printable or anything */
504         procmime_decode_content(msgcontent);
505
506         fp = my_tmpfile();
507         procmime_write_mimeinfo(msgcontent, fp);
508         rewind(fp);
509
510         /* read temporary file into memory */
511         textstr = file_read_stream_to_str(fp);
512         
513         /* gtk2: convert back from utf8 */
514         tmp = conv_codeset_strdup(textstr, CS_UTF_8, 
515                         procmime_mimeinfo_get_parameter(msgcontent, "charset"));
516         g_free(textstr);
517         textstr = g_strdup(tmp);
518         g_free(tmp);
519         fclose(fp);
520
521         /* encrypt data */
522         gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
523         gpgme_data_new(&gpgenc);
524         gpgme_new(&ctx);
525         gpgme_set_armor(ctx, 1);
526
527         gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
528
529         gpgme_release(ctx);
530         enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
531
532         tmp = g_malloc(len+1);
533         g_memmove(tmp, enccontent, len+1);
534         tmp[len] = '\0';
535         g_free(enccontent);
536
537         gpgme_data_release(gpgtext);
538         g_free(textstr);
539
540         if (msgcontent->content == MIMECONTENT_FILE &&
541             msgcontent->data.filename != NULL) {
542                 unlink(msgcontent->data.filename);
543                 g_free(msgcontent->data.filename);
544         }
545         msgcontent->data.mem = g_strdup(tmp);
546         msgcontent->content = MIMECONTENT_MEM;
547         g_free(tmp);
548
549         return TRUE;
550 }
551
552 static PrivacySystem pgpinline_system = {
553         "pgpinline",                    /* id */
554         "PGP Inline",                   /* name */
555
556         pgpinline_free_privacydata,     /* free_privacydata */
557
558         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
559         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
560         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
561         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
562         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
563
564         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
565         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
566
567         TRUE,
568         pgpinline_sign,
569
570         TRUE,
571         pgpinline_get_encrypt_data,
572         pgpinline_encrypt,
573 };
574
575 void pgpinline_init()
576 {
577         privacy_register_system(&pgpinline_system);
578 }
579
580 void pgpinline_done()
581 {
582         privacy_unregister_system(&pgpinline_system);
583 }
584
585 #endif /* USE_GPGME */