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