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