aboutsummaryrefslogtreecommitdiff
path: root/src/rfc2015.c
blob: 7496e38a1a2806900fb6b1eccd44c776e0d7d251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
/*
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 2001 Werner Koch (dd9jn)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#if USE_GPGME

#include "defs.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
#include <errno.h>

#include <gpgme.h>

#include "procmime.h"
#include "procheader.h"
#include "base64.h"
#include "uuencode.h"
#include "unmime.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
#include "passphrase.h"
#include "select-keys.h"
#include "sigstatus.h"
#include "rfc2015.h"

#define DIM(v)     (sizeof(v)/sizeof((v)[0]))

static char *content_names[] = {
    "Content-Type",
    "Content-Disposition",
    "Content-Transfer-Encoding",
    NULL
};

static char *mime_version_name[] = {
    "Mime-Version",
    NULL
};

#if 0
static void dump_mimeinfo (const char *text, MimeInfo *x)
{
    debug_print ("MimeInfo[%s] %p  level=%d\n",
               text, x, x? x->level:0 );
    if (!x)
        return;

    debug_print ("      enc=`%s' enc_type=%d mime_type=%d\n",
               x->encoding, x->encoding_type, x->mime_type );
    debug_print ("      cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
               x->content_type, x->charset, x->name, x->boundary );
    debug_print ("      cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
               x->content_disposition, x->filename, x->fpos, x->size,
               x->level );
    dump_mimeinfo (".main", x->main );
    dump_mimeinfo (".sub", x->sub );
    dump_mimeinfo (".next", x->next );
    debug_print ("MimeInfo[.parent] %p\n", x ); 
    dump_mimeinfo (".children", x->children );
    dump_mimeinfo (".plaintext", x->plaintext );
}

static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
{
    unsigned int size = mimeinfo->size;
    int c;

    if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
        debug_print ("dump_part: fseek error\n");
        return;
    }

    debug_print ("--- begin dump_part ----\n");
    while (size-- && (c = getc (fp)) != EOF) 
        putc (c, stderr);
    if (ferror (fp))
        debug_print ("dump_part: read error\n");
    debug_print ("--- end dump_part ----\n");
}
#endif

void
rfc2015_disable_all (void)
{
    /* FIXME: set a flag, so that we don't bother the user with failed
     * gpgme messages */
}


void
rfc2015_secure_remove (const char *fname)
{
    if (!fname)
        return;
    /* fixme: overwrite the file first */
    remove (fname);
}

static void
sig_status_for_key(GString *str, gpgme_ctx_t ctx, gpgme_signature_t sig)
{
	gpgme_key_t key;
	gpgme_user_id_t user;

	gpgme_get_key(ctx, sig->fpr, &key, 0);
	if (key == NULL || key->uids->uid == NULL) {
		g_string_sprintfa (str, "%s\n",
				   gpgmegtk_sig_status_to_string (sig, FALSE));
		if ((sig->fpr != NULL) && (*(sig->fpr) != '\0'))
			g_string_sprintfa
				(str, "Key fingerprint: %s\n", sig->fpr);
		g_string_append (str, _("Cannot find user ID for this key."));
		return;
	}
	user = key->uids;
	g_string_sprintfa
		(str, gpgmegtk_sig_status_to_string (sig, TRUE), user->uid);
	g_string_append (str, "\n");

	user = user->next;
	while (user) {
		g_string_sprintfa
			(str, _("		aka \"%s\"\n"), user->uid);
		user = user->next;
	}
}

static gchar *
sig_status_full (gpgme_ctx_t ctx, gpgme_verify_result_t result)
{
	GString *str;
	gpgme_signature_t sig;
	time_t created;
	struct tm *ctime_val;
	char ctime_str[80], ctime_str_utf8[80];
	gchar *retval;

	str = g_string_new ("");

	sig = result->signatures;
	while (sig != NULL) {
		if (sig->timestamp != 0) {
			created = sig->timestamp;
			ctime_val = localtime (&created);
			strftime (ctime_str, sizeof (ctime_str), "%c", 
				  ctime_val);
			conv_localetodisp (ctime_str_utf8,
					   sizeof (ctime_str_utf8), ctime_str);
			g_string_sprintfa (str,
					   _("Signature made at %s\n"),
					   ctime_str_utf8);
		}
		sig_status_for_key(str, ctx, sig);
		if (sig->next)
			g_string_append (str, "\n\n");
		sig = sig->next;
	}

	retval = str->str;
	g_string_free (str, FALSE);
	return retval;
}

static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
{
    gpgme_ctx_t ctx = NULL;
    gpgme_error_t err;
    gpgme_data_t sig = NULL, text = NULL;
    gpgme_verify_result_t verifyresult = NULL;
    GpgmegtkSigStatus statuswindow = NULL;
    const char *result = NULL;
    gchar *tmp_file;
    gint n_exclude_chars = 0;

    if (prefs_common.gpg_signature_popup)
	statuswindow = gpgmegtk_sig_status_create ();

    err = gpgme_new (&ctx);
    if (err) {
	debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
	goto leave;
    }

    /* don't include the last empty line.
       It does not belong to the signed text */
    if (mimeinfo->children->size > 0) {
	if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 1,
		  SEEK_SET) < 0) {
	    perror("fseek");
	    goto leave;
	}
	if (fgetc(fp) == '\n') {
	    n_exclude_chars++;
	    if (mimeinfo->children->size > 1) {
		if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 2,
			  SEEK_SET) < 0) {
		    perror("fseek");
		    goto leave;
		}
		if (fgetc(fp) == '\r')
		    n_exclude_chars++;
	    }
	}
    }

    /* canonicalize the file part. */
    tmp_file = get_tmp_file();
    if (copy_file_part(fp, mimeinfo->children->fpos,
		       mimeinfo->children->size - n_exclude_chars,
		       tmp_file) < 0) {
	g_free(tmp_file);
	goto leave;
    }
    if (canonicalize_file_replace(tmp_file) < 0) {
	unlink(tmp_file);
	g_free(tmp_file);
	goto leave;
    }

    err = gpgme_data_new_from_file(&text, tmp_file, 1);

    unlink(tmp_file);
    g_free(tmp_file);

    if (!err)
	err = gpgme_data_new_from_filepart (&sig, NULL, fp,
					    partinfo->fpos, partinfo->size);
    if (err) {
        debug_print ("gpgme_data_new_from_filepart failed: %s\n",
		   gpgme_strerror (err));
        goto leave;
    }

    err = gpgme_op_verify (ctx, sig, text, NULL);
    if (err)  {
        debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
        goto leave;
    }
    verifyresult = gpgme_op_verify_result(ctx);

    /* FIXME: check what the heck this sig_status_full stuff is.
     * Maybe it belongs in sigstatus.c 
     *
     * I think it belongs here as it is interfacing with gmime (Toshio). */
    g_free (partinfo->sigstatus_full);
    partinfo->sigstatus_full = sig_status_full (ctx, verifyresult);

leave:
    if (verifyresult) {
        result = gpgmegtk_sig_status_to_string(verifyresult->signatures, FALSE);
    } else {
        result = _("Error verifying the signature");
    }
    debug_print("verification status: %s\n", result);
    if (prefs_common.gpg_signature_popup)
	gpgmegtk_sig_status_update (statuswindow, ctx);

    g_free (partinfo->sigstatus);
    partinfo->sigstatus = g_strdup (result);

    gpgme_data_release (sig);
    gpgme_data_release (text);
    gpgme_release (ctx);
    if (prefs_common.gpg_signature_popup)
	gpgmegtk_sig_status_destroy (statuswindow);
}

/*
 * Copy a gpgme data object to a temporary file and
 * return this filename 
 */
#if 0
static char *
copy_gpgmedata_to_temp (GpgmeData data, guint *length)
{
    static int id;
    char *tmp;
    FILE *fp;
    char buf[100];
    size_t nread;
    GpgmeError err;
    
    tmp = g_strdup_printf("%s%cgpgtmp.%08x",
                          get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );

    if ((fp = fopen(tmp, "wb")) == NULL) {
        FILE_OP_ERROR(tmp, "fopen");
        g_free(tmp);
        return NULL;
    }

    err = gpgme_data_rewind ( data );
    if (err)
        debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));

    while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
        fwrite ( buf, nread, 1, fp );
    }

    if (err != GPGME_EOF)
        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));

    fclose (fp);
    *length = nread;

    return tmp;
}
#endif
static gpgme_data_t
pgp_decrypt (MimeInfo *partinfo, FILE *fp)
{
    gpgme_ctx_t ctx = NULL;
    gpgme_error_t err;
    gpgme_data_t cipher = NULL, plain = NULL;
    struct passphrase_cb_info_s info;

    memset (&info, 0, sizeof info);

    err = gpgme_new (&ctx);
    if (err) {
        debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
        goto leave;
    }

    err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
					partinfo->fpos, partinfo->size);
    if (err) {
        debug_print ("gpgme_data_new_from_filepart failed: %s\n",
                     gpgme_strerror (err));
        goto leave;
    }

    err = gpgme_data_new (&plain);
    if (err) {
        debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
        goto leave;
    }

    if (!getenv("GPG_AGENT_INFO")) {
        info.c = ctx;
        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
    } 

    err = gpgme_op_decrypt (ctx, cipher, plain);

leave:
    gpgme_data_release (cipher);
    if (err) {
        gpgmegtk_free_passphrase();
        debug_print ("decryption failed: %s\n", gpgme_strerror (err));
        gpgme_data_release (plain);
        plain = NULL;
    }
    else
        debug_print ("** decryption succeeded\n");

    gpgme_release (ctx);
    return plain;
}

MimeInfo ** rfc2015_find_signature (MimeInfo *mimeinfo)
{
    MimeInfo *partinfo;
    MimeInfo **signedinfo = NULL;
    int n = 0;

    if (!mimeinfo)
        return NULL;

    /* We could have a signature nested within multipart/mixed so
     * recurse to find it.
     */
    if (!g_strcasecmp (mimeinfo->content_type, "multipart/mixed")) {
        for (partinfo = mimeinfo->children; partinfo != NULL; 
                partinfo = partinfo->next) {
            signedinfo = rfc2015_find_signature (partinfo);
            if (signedinfo) {
                return signedinfo;
            }
        }
        return NULL;
    }
    if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
        return NULL;

    debug_print ("** multipart/signed encountered\n");

    /* check that we have at least 2 parts of the correct type */
    for (partinfo = mimeinfo->children;
         partinfo != NULL; partinfo = partinfo->next) {
        if (++n > 1  && !g_strcasecmp (partinfo->content_type,
				       "application/pgp-signature"))
            break;
    }

    if (partinfo) {
        signedinfo = g_malloc(sizeof(MimeInfo *) * 2);
        signedinfo[0] = mimeinfo;
        signedinfo[1] = partinfo;
    }
    /* This is NULL if partinfo was not set */
    return signedinfo;
}

gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
{
    return rfc2015_find_signature (mimeinfo) != NULL;
}

void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
{
    MimeInfo **signedinfo;

    signedinfo = rfc2015_find_signature (mimeinfo);
    if (!signedinfo)
        return;

#if 0
    g_message ("** yep, it is a pgp signature");
    dump_mimeinfo ("gpg-signature", partinfo );
    dump_part (partinfo, fp );
    dump_mimeinfo ("signed text", mimeinfo->children );
    dump_part (mimeinfo->children, fp);
#endif

    check_signature (signedinfo[0], signedinfo[1], fp);
    g_free(signedinfo);
}

int rfc2015_is_encrypted (MimeInfo *mimeinfo)
{
    if (!mimeinfo || mimeinfo->mime_type != MIME_MULTIPART)
        return 0;
    if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
        return 0;
    /* fixme: we should check the protocol parameter */
    return 1;
}

gboolean rfc2015_msg_is_encrypted (const gchar *file)
{
	FILE *fp;
	MimeInfo *mimeinfo;
	int ret;

	if ((fp = fopen(file, "rb")) == NULL)
		return FALSE;

	mimeinfo = procmime_scan_mime_header(fp);
	if(!mimeinfo) {
		fclose(fp);
		return FALSE;
	}

	ret = rfc2015_is_encrypted(mimeinfo);
	procmime_mimeinfo_free_all(mimeinfo);
	return ret != 0 ? TRUE : FALSE;
}

static int
name_cmp(const char *a, const char *b)
{
    for( ; *a && *b; a++, b++) {
        if(*a != *b
           && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
            return 1;
    }

    return *a != *b;
}

static int
headerp(char *p, char **names)
{
    int i, c;
    char *p2;

    p2 = strchr(p, ':');
    if(!p2 || p == p2) {
        return 0;
    }
    if(p2[-1] == ' ' || p2[-1] == '\t') {
        return 0;
    }

    if(!names[0])
        return 1;  

    c = *p2;
    *p2 = 0;
    for(i = 0 ; names[i] != NULL; i++) {
        if(!name_cmp (names[i], p))
            break;
    }
    *p2 = c;

    return names[i] != NULL;
}


#define DECRYPTION_ABORT() \
{ \
    procmime_mimeinfo_free_all(tmpinfo); \
    msginfo->decryption_failed = 1; \
    return; \
}

void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
{
    static int id;
    MimeInfo *tmpinfo, *partinfo;
    int ver_ok = 0;
    char *fname;
    gpgme_data_t plain;
    FILE *dstfp;
    ssize_t nread;
    char buf[BUFFSIZE];
    int in_cline;
    gpgme_error_t err;

    g_return_if_fail (msginfo != NULL);
    g_return_if_fail (mimeinfo != NULL);
    g_return_if_fail (fp != NULL);
    g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);

    debug_print ("** decrypting multipart/encrypted message\n");

    /* skip headers */
    if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
        perror("fseek");
    tmpinfo = procmime_scan_mime_header(fp);
    if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
        DECRYPTION_ABORT();
    }

    procmime_scan_multipart_message(tmpinfo, fp);

    /* check that we have the 2 parts */
    partinfo = tmpinfo->children;
    if (!partinfo || !partinfo->next) {
        DECRYPTION_ABORT();
    }
    if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
        /* Fixme: check that the version is 1 */
        ver_ok = 1;
    }
    partinfo = partinfo->next;
    if (ver_ok &&
        !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
        if (partinfo->next)
            g_warning ("oops: pgp_encrypted with more than 2 parts");
    }
    else {
        DECRYPTION_ABORT();
    }

    debug_print ("** yep, it is pgp encrypted\n");

    plain = pgp_decrypt (partinfo, fp);
    if (!plain) {
        DECRYPTION_ABORT();
    }

    fname = g_strdup_printf("%s%cplaintext.%08x",
			    get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);

    if ((dstfp = fopen(fname, "wb")) == NULL) {
        FILE_OP_ERROR(fname, "fopen");
        g_free(fname);
        DECRYPTION_ABORT();
    }

    /* write the orginal header to the new file */
    if (fseek(fp, tmpinfo->fpos, SEEK_SET) < 0)
        perror("fseek");

    in_cline = 0;
    while (fgets(buf, sizeof(buf), fp)) {
        if (headerp (buf, content_names)) {
            in_cline = 1;
            continue;
        }
        if (in_cline) {
            if (buf[0] == ' ' || buf[0] == '\t')
                continue;
            in_cline = 0;
        }
        if (buf[0] == '\r' || buf[0] == '\n')
            break;
        fputs (buf, dstfp);
    }

    err = (gpgme_data_seek (plain, 0, SEEK_SET) == -1) ?
	gpgme_error_from_errno(errno) : 0;
    if (err)
	debug_print ("gpgme_data_seek failed: %s\n", gpgme_strerror (err));

    nread = gpgme_data_read(plain, buf, sizeof(buf));
    while (nread > 0) {
        fwrite (buf, nread, 1, dstfp);
	nread = gpgme_data_read(plain, buf, sizeof(buf));
    }

    if (nread != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
    }

    fclose (dstfp);
    procmime_mimeinfo_free_all(tmpinfo);

    msginfo->plaintext_file = fname;
    msginfo->decryption_failed = 0;
}

#undef DECRYPTION_ABORT


/*
 * plain contains an entire mime object.
 * Encrypt it and return an GpgmeData object with the encrypted version of
 * the file or NULL in case of error.
 */
static gpgme_data_t
pgp_encrypt ( gpgme_data_t plain, gpgme_key_t kset[] )
{
    gpgme_ctx_t ctx = NULL;
    gpgme_error_t err;
    gpgme_data_t cipher = NULL;

    err = gpgme_new (&ctx);
    if (!err)
	err = gpgme_data_new (&cipher);
    if (!err) {
        gpgme_set_armor (ctx, 1);
    err = (gpgme_data_seek(plain, 0, SEEK_SET) == -1) ?
        gpgme_error_from_errno(errno) : 0;
        if (!err) {
            /*
             * Note -- it is currently the responsibility of select-keys.c::
             * gpgmegtk_recipient_selection() to prompt the user whether to
             * encrypt to recipients whose key is not trusted.
             */
            err = gpgme_op_encrypt (ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST,
                    plain, cipher);
        }
    }

    if (err) {
        debug_print ("encryption failed: %s\n", gpgme_strerror (err));
        gpgme_data_release (cipher);
        cipher = NULL;
    }
    else {
        debug_print ("** encryption succeeded\n");
    }

    gpgme_release (ctx);
    return cipher;
}

/*
 * Create and return a list of keys matching a key id
 */

GSList *rfc2015_create_signers_list (const char *keyid)
{
	GSList *key_list = NULL;
	gpgme_ctx_t list_ctx = NULL;
	GSList *p;
	gpgme_error_t err;
	gpgme_key_t key;

	err = gpgme_new (&list_ctx);
	if (err)
		goto leave;
	err = gpgme_op_keylist_start (list_ctx, keyid, 1);
	if (err)
		goto leave;
	while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
		key_list = g_slist_append (key_list, key);
	}
	if (gpgme_err_code(err) != GPG_ERR_EOF)
		goto leave;
	err = 0;
	if (key_list == NULL) {
		debug_print ("no keys found for keyid \"%s\"\n", keyid);
	}

leave:
	if (err) {
		debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
		for (p = key_list; p != NULL; p = p->next)
			gpgme_key_unref ((gpgme_key_t) p->data);
		g_slist_free (key_list);
	}
	if (list_ctx)
		gpgme_release (list_ctx);
	return err ? NULL : key_list;
}

/*
 * Encrypt the file by extracting all recipients and finding the
 * encryption keys for all of them.  The file content is then replaced
 * by the encrypted one.  */
int
rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
{
    FILE *fp = NULL;
    char buf[BUFFSIZE];
    int i, clineidx, saved_last;
    char *clines[3] = {NULL};
    gpgme_error_t err;
    gpgme_data_t header = NULL;
    gpgme_data_t plain = NULL;
    gpgme_data_t cipher = NULL;
    gpgme_key_t *kset = NULL;
    ssize_t bytesRW = 0;
    int mime_version_seen = 0;
    char *boundary;

    boundary = generate_mime_boundary ("Encrypt");

    /* Create the list of recipients */
    kset = gpgmegtk_recipient_selection (recp_list);
    if (!kset) {
        debug_print ("error creating recipient list\n" );
        goto failure;
    }

    /* Open the source file */
    if ((fp = fopen(file, "rb")) == NULL) {
        FILE_OP_ERROR(file, "fopen");
        goto failure;
    }

    err = gpgme_data_new (&header);
    if (!err)
	err = gpgme_data_new (&plain);
    if (err) {
        debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
        goto failure;
    }

    /* get the content header lines from the source */
    clineidx = 0;
    saved_last = 0;
    while (!err && fgets(buf, sizeof(buf), fp)) {
        /* fixme: check for overlong lines */
        if (headerp (buf, content_names)) {
            if (clineidx >= DIM (clines)) {
                debug_print ("rfc2015_encrypt: too many content lines\n");
                goto failure;
            }
            clines[clineidx++] = g_strdup (buf);
            saved_last = 1;
            continue;
        }
        if (saved_last) {
            if (*buf == ' ' || *buf == '\t') {
                char *last = clines[clineidx - 1];
                clines[clineidx - 1] = g_strconcat (last, buf, NULL);
                g_free (last);
                continue;
            }
            saved_last = 0;
        }

        if (headerp (buf, mime_version_name)) 
            mime_version_seen = 1;

        if (buf[0] == '\r' || buf[0] == '\n')
            break;
	bytesRW = gpgme_data_write (header, buf, strlen (buf));
    }
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fgets");
        goto failure;
    }

    /* write them to the temp data and add the rest of the message */
    for (i = 0; (bytesRW != -1) && i < clineidx; i++) {
        debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
	bytesRW = gpgme_data_write (plain, clines[i], strlen (clines[i]));
    }
    if (bytesRW != -1)
	bytesRW = gpgme_data_write (plain, "\r\n", 2);
    while ((bytesRW != -1) && fgets(buf, sizeof(buf), fp)) {
	bytesRW = gpgme_data_write (plain, buf, strlen (buf));
    }
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fgets");
        goto failure;
    }
    if (bytesRW == -1) {
	debug_print ("gpgme_data_write failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    cipher = pgp_encrypt (plain, kset);
    gpgme_data_release (plain); plain = NULL;
    i = 0;
    while (kset[i] != NULL) {
	gpgme_key_unref(kset[i]);
	i++;
    }
    g_free(kset);
    kset = NULL;
    if (!cipher)
        goto failure;

    /* we have the encrypted message available in cipher and now we
     * are going to rewrite the source file. To be sure that file has
     * been truncated we use an approach which should work everywhere:
     * close the file and then reopen it for writing. It is important
     * that this works, otherwise it may happen that parts of the
     * plaintext are still in the file (The encrypted stuff is, due to
     * compression, usually shorter than the plaintext). 
     * 
     * Yes, there is a race condition here, but everyone, who is so
     * stupid to store the temp file with the plaintext in a public
     * directory has to live with this anyway. */
    if (fclose (fp)) {
        FILE_OP_ERROR(file, "fclose");
        goto failure;
    }
    if ((fp = fopen(file, "wb")) == NULL) {
        FILE_OP_ERROR(file, "fopen");
        goto failure;
    }

    /* Write the header, append new content lines, part 1 and part 2 header */
    err = (gpgme_data_seek(header, 0 , SEEK_SET) == -1) ?
	gpgme_error_from_errno(errno) : 0;
    if (err) {
	debug_print ("gpgme_data_seek failed: %s\n", gpgme_strerror (err));
        goto failure;
    }
    bytesRW = gpgme_data_read(header, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);
	bytesRW = gpgme_data_read(header, buf, BUFFSIZE);
    }

    if (bytesRW != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fwrite");
        goto failure;
    }
    gpgme_data_release (header); header = NULL;
    
    if (!mime_version_seen) 
        fputs ("MIME-Version: 1\r\n", fp);

    if (ascii_armored) {
        fprintf(fp, 
            "Content-Type: text/plain; charset=US-ASCII\r\n"
            "Content-Transfer-Encoding: 7bit\r\n"  
            "\r\n");
    } else {
        fprintf (fp,
	        "Content-Type: multipart/encrypted;"
	        " protocol=\"application/pgp-encrypted\";\r\n"
	        " boundary=\"%s\"\r\n"
	        "\r\n"
	        "--%s\r\n"
	        "Content-Type: application/pgp-encrypted\r\n"
	        "\r\n"
	        "Version: 1\r\n"
	        "\r\n"
	        "--%s\r\n"
	        "Content-Type: application/octet-stream\r\n"
	        "\r\n",
	        boundary, boundary, boundary);
    }

    /* append the encrypted stuff */
    err = (gpgme_data_seek(cipher, 0 , SEEK_SET) == -1) ?
	gpgme_error_from_errno(errno) : 0;
    if (err) {
	debug_print ("** gpgme_data_seek on cipher failed: %s\n",
                   gpgme_strerror (err));
	debug_print ("gpgme_data_seek failed: %s\n", gpgme_strerror (err));
        goto failure;
    }

    bytesRW = gpgme_data_read(cipher, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);
	bytesRW = gpgme_data_read(cipher, buf, BUFFSIZE);
    }

    if (bytesRW != 0) {
	debug_print ("** gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    /* and the final boundary */
    if (!ascii_armored) {
        fprintf (fp,
		 "\r\n"
		 "--%s--\r\n",
	         boundary);
    }
    fflush (fp);
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fwrite");
        goto failure;
    }
    fclose (fp);
    gpgme_data_release (cipher);
    return 0;

failure:
    if (fp) 
        fclose (fp);
    gpgme_data_release (header);
    gpgme_data_release (plain);
    gpgme_data_release (cipher);

    if (kset != NULL) {
	i = 0;
	while (kset[i] != NULL) {
	    gpgme_key_unref(kset[i]);
	    i++;
	}
	g_free(kset);
    }
    g_free (boundary);
    return -1; /* error */
}

/* 
 * plain contains an entire mime object.  Sign it and return an
 * GpgmeData object with the signature of it or NULL in case of error.
 * micalg returns the micalg information about the signature.
 */
static gpgme_data_t
pgp_sign (gpgme_data_t plain, GSList *key_list, gboolean clearsign,
	  char **micalg)
{
    GSList *p;
    gpgme_ctx_t ctx = NULL;
    gpgme_error_t err;
    gpgme_data_t sig = NULL;
    gpgme_sign_result_t result = NULL;
    struct passphrase_cb_info_s info;

    *micalg = NULL;
    memset (&info, 0, sizeof info);

    err = gpgme_new (&ctx);
    if (err)
        goto leave;
    err = gpgme_data_new (&sig);
    if (err)
        goto leave;

    if (!getenv("GPG_AGENT_INFO")) {
        info.c = ctx;
        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
    }
    gpgme_set_textmode (ctx, 1);
    gpgme_set_armor (ctx, 1);
    gpgme_signers_clear (ctx);
    for (p = key_list; p != NULL; p = p->next) {
    err = gpgme_signers_add (ctx, (gpgme_key_t) p->data);
	if (err)
	    goto leave;
    }
    for (p = key_list; p != NULL; p = p->next)
	gpgme_key_unref ((gpgme_key_t) p->data);
    g_slist_free (key_list);

    err = (gpgme_data_seek(plain, 0, SEEK_SET) == -1) ?
        gpgme_error_from_errno(errno) : 0;
    if (!err) {
        err = gpgme_op_sign (ctx, plain, sig,
	 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
    }
    if (!err)
	result = gpgme_op_sign_result(ctx);
	if (result) {
	    if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
		*micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
			    result->signatures->hash_algo));
	    } else {
		*micalg = g_strdup(gpgme_hash_algo_name(
			    result->signatures->hash_algo));
	    }
	}

leave:
    if (err) {
        gpgmegtk_free_passphrase();
        debug_print ("signing failed: %s\n", gpgme_strerror (err));
        gpgme_data_release (sig);
        sig = NULL;
    }
    else {
        debug_print ("signing succeeded\n");
    }

    gpgme_release (ctx);
    return sig;
}


/*
 * Sign the file and replace its content with the signed one.
 */
int
rfc2015_sign (const char *file, GSList *key_list)
{
    FILE *fp = NULL;
    char buf[BUFFSIZE];
    int i, clineidx, saved_last;
    char *clines[3] = {NULL};
    gpgme_error_t err;
    gpgme_data_t header = NULL;
    gpgme_data_t plain = NULL;
    gpgme_data_t sigdata = NULL;
    ssize_t bytesRW = -1;
    int mime_version_seen = 0;
    char *boundary;
    char *micalg = NULL;

    boundary = generate_mime_boundary ("Signature");

    /* Open the source file */
    if ((fp = fopen(file, "rb")) == NULL) {
        FILE_OP_ERROR(file, "fopen");
        goto failure;
    }

    err = gpgme_data_new (&header);
    if (!err)
        err = gpgme_data_new (&plain);
    if (err) {
        debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
        goto failure;
    }

    /* get the content header lines from the source */
    clineidx = 0;
    saved_last = 0;
    while (!err && fgets(buf, sizeof(buf), fp)) {
        /* fixme: check for overlong lines */
        if (headerp (buf, content_names)) {
            if (clineidx >= DIM (clines)) {
                debug_print ("rfc2015_sign: too many content lines\n");
                goto failure;
            }
            clines[clineidx++] = g_strdup (buf);
            saved_last = 1;
            continue;
        }
        if (saved_last) {
            if (*buf == ' ' || *buf == '\t') {
                char *last = clines[clineidx - 1];
                clines[clineidx - 1] = g_strconcat (last, buf, NULL);
                g_free (last);
                continue;
            }
            saved_last = 0;
        }

        if (headerp (buf, mime_version_name)) 
            mime_version_seen = 1;

        if (buf[0] == '\r' || buf[0] == '\n')
            break;
	 bytesRW = gpgme_data_write (header, buf, strlen (buf));
    }
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fgets");
        goto failure;
    }

    /* write them to the temp data and add the rest of the message */
    for (i = 0; (bytesRW != -1) && i < clineidx; i++) {
	bytesRW = gpgme_data_write (plain, clines[i], strlen (clines[i]));
    }
    if (bytesRW != -1)
	bytesRW = gpgme_data_write (plain, "\r\n", 2 );
    while ((bytesRW != -1) && fgets(buf, sizeof(buf), fp)) {
	bytesRW = gpgme_data_write (plain, buf, strlen (buf));
    }
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fgets");
        goto failure;
    }
    if (bytesRW == -1) {
	debug_print ("gpgme_data_write failed: %s\n", 
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    sigdata = pgp_sign (plain, key_list, FALSE, &micalg); 
    if (!sigdata) 
        goto failure;

    /* we have the signed message available in sigdata and now we are
     * going to rewrite the original file. To be sure that file has
     * been truncated we use an approach which should work everywhere:
     * close the file and then reopen it for writing. */
    if (fclose (fp)) {
        FILE_OP_ERROR(file, "fclose");
        goto failure;
    }
    if ((fp = fopen(file, "wb")) == NULL) {
        FILE_OP_ERROR(file, "fopen");
        goto failure;
    }

    /* Write the rfc822 header and add new content lines */
    err = (gpgme_data_seek (header, 0, SEEK_SET) == -1) ?
	    gpgme_error_from_errno(errno) : 0;
    if (err)
	debug_print ("gpgme_data_seek failed: %s\n", gpgme_strerror (err));
    bytesRW = gpgme_data_read (header, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);
	bytesRW = gpgme_data_read (header, buf, BUFFSIZE);
    }
    if (bytesRW != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fwrite");
        goto failure;
    }
    gpgme_data_release (header);
    header = NULL;

    if (!mime_version_seen) 
        fputs ("MIME-Version: 1.0\r\n", fp);
    fprintf (fp, "Content-Type: multipart/signed; "
             "protocol=\"application/pgp-signature\";\r\n");
    if (micalg)
        fprintf (fp, " micalg=\"%s\";\r\n", micalg);
    fprintf (fp, " boundary=\"%s\"\r\n", boundary);

    /* Part 1: signed material */
    fprintf (fp, "\r\n"
                 "--%s\r\n",
                 boundary);
    err = (gpgme_data_seek (plain, 0, SEEK_SET) == -1) ?
	    gpgme_error_from_errno(errno) : 0;
    if (err) {
	debug_print ("gpgme_data_seek on plain failed: %s\n",
                   gpgme_strerror (err));
        goto failure;
    }
    bytesRW = gpgme_data_read (plain, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);   
	bytesRW = gpgme_data_read (plain, buf, BUFFSIZE);
    }
    if (bytesRW != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    /* Part 2: signature */
    fprintf (fp, "\r\n"
                 "--%s\r\n",
                 boundary);
    fputs ("Content-Type: application/pgp-signature\r\n"
	   "\r\n", fp);

    err = (gpgme_data_seek (sigdata, 0, SEEK_SET) == -1) ?
	    gpgme_error_from_errno(errno) : 0;
    if (err) {
	debug_print ("gpgme_data_seek on sigdata failed: %s\n",
		   gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    bytesRW = gpgme_data_read (sigdata, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);
	bytesRW = gpgme_data_read (sigdata, buf, BUFFSIZE);
    }
    if (bytesRW != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
        goto failure;
    }

    /* Final boundary */
    fprintf (fp, "\r\n"
                 "--%s--\r\n",
                 boundary);
    fflush (fp);
    if (ferror (fp)) {
        FILE_OP_ERROR (file, "fwrite");
        goto failure;
    }
    fclose (fp);
    gpgme_data_release (header);
    gpgme_data_release (plain);
    gpgme_data_release (sigdata);
    g_free (boundary);
    g_free (micalg);
    return 0;

failure:
    if (fp) 
        fclose (fp);
    gpgme_data_release (header);
    gpgme_data_release (plain);
    gpgme_data_release (sigdata);
    g_free (boundary);
    g_free (micalg);
    return -1; /* error */
}


/*
 * Sign the file with clear text and replace its content with the signed one.
 */
gint
rfc2015_clearsign (const gchar *file, GSList *key_list)
{
    FILE *fp;
    gchar buf[BUFFSIZE];
    gpgme_error_t err;
    gpgme_data_t text = NULL;
    gpgme_data_t sigdata = NULL;
    ssize_t bytesRW = 0;
    gchar *micalg;

    if ((fp = fopen(file, "rb")) == NULL) {
	FILE_OP_ERROR(file, "fopen");
	goto failure;
    }

    err = gpgme_data_new(&text);
    if (err) {
	debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
	goto failure;
    }

    while ((bytesRW != -1) && fgets(buf, sizeof(buf), fp)) {
	bytesRW = gpgme_data_write(text, buf, strlen(buf));
    }
    if (ferror(fp)) {
	FILE_OP_ERROR(file, "fgets");
	goto failure;
    }
    if (bytesRW == -1) {
	debug_print("gpgme_data_write failed: %s\n",
		gpgme_strerror(gpgme_error_from_errno(errno)));
	goto failure;
    }

    sigdata = pgp_sign(text, key_list, TRUE, &micalg);
    if (micalg) {
	g_free(micalg);
    }
    if (!sigdata)
	goto failure;

    if (fclose(fp) == EOF) {
	FILE_OP_ERROR(file, "fclose");
	fp = NULL;
	goto failure;
    }
    if ((fp = fopen(file, "wb")) == NULL) {
	FILE_OP_ERROR(file, "fopen");
	goto failure;
    }

    err = (gpgme_data_seek (sigdata, 0, SEEK_SET) == -1) ?
	    gpgme_error_from_errno(errno) : 0;
    if (err) {
	debug_print("gpgme_data_seek on sigdata failed: %s\n",
		    gpgme_strerror(err));
	goto failure;
    }

    bytesRW = gpgme_data_read (sigdata, buf, BUFFSIZE);
    while (bytesRW > 0) {
	fwrite (buf, bytesRW, 1, fp);
	bytesRW = gpgme_data_read (sigdata, buf, BUFFSIZE);
    }
    if (bytesRW != 0) {
	debug_print ("gpgme_data_read failed: %s\n",
		gpgme_strerror (gpgme_error_from_errno(errno)));
	goto failure;
    }

    if (fclose(fp) == EOF) {
	FILE_OP_ERROR(file, "fclose");
	fp = NULL;
	goto failure;
    }
    gpgme_data_release(text);
    gpgme_data_release(sigdata);
    return 0;

failure:
    if (fp)
	fclose(fp);
    gpgme_data_release(text);
    gpgme_data_release(sigdata);
    return -1;
}

#endif /* USE_GPGME */