File: aycryption.c

package info (click to toggle)
ayttm 0.6.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,368 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 561; yacc: 294; lex: 53; sed: 16
file content (767 lines) | stat: -rw-r--r-- 19,982 bytes parent folder | download | duplicates (2)
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
/*
 * Aycryption (GPG support) module for Ayttm 
 *
 * Copyright (C) 2003, the Ayttm team
 * 
 * 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 __MINGW32__
#define __IN_PLUGIN__
#endif

#include "intl.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <gpgme.h>

#include "externs.h"
#include "plugin_api.h"
#include "prefs.h"
#include "util.h"
#include "messages.h"
#include "debug.h"
#include "platform_defs.h"
#include "select-keys.h"
#include <gtk/gtk.h>
#include "gtk/html_text_buffer.h"
#include <errno.h>

/*******************************************************************************
 *                             Begin Module Code
 ******************************************************************************/
/*  Module defines */
#ifndef USE_POSIX_DLOPEN
#define plugin_info aycryption_LTX_plugin_info
#define module_version aycryption_LTX_module_version
#endif

/* Function Prototypes */
static char *aycryption_out(Conversation *conv, const char *s);
static char *aycryption_in(Conversation *conv, const char *s);
static void set_gpg_key(ebmCallbackData *data);
static void show_gpg_log(ebmCallbackData *data);
void pgp_encrypt(gpgme_data_t plain, gpgme_data_t *cipher, gpgme_key_t *kset,
	int sign);

gpgme_error_t gpgmegtk_passphrase_cb(void *opaque,
	const char *desc,
	const char *passphrase_info, int prev_was_bad, int fd);

static int aycryption_init();
static int aycryption_finish();

struct passphrase_cb_info_s {
	gpgme_ctx_t c;
	int did_it;
};

static int store_passphrase = 0;
static char mykey[MAX_PREF_LEN] = "";

static int ref_count = 0;
static void *tag1 = NULL;
static void *tag2 = NULL;
static void *tag3 = NULL;
static void *tag4 = NULL;

static GtkWidget *gpg_log_window = NULL;
static GtkWidget *gpg_log_text = NULL;
static GtkWidget *gpg_log_swindow = NULL;
static int do_aycryption_debug = 0;
#define DBG_CRYPT do_aycryption_debug

/*  Module Exports */
PLUGIN_INFO plugin_info = {
	PLUGIN_FILTER,
	"Aycryption",
	"Encrypts messages with GPG.\n"
		"WARNING: Apparently MSN servers randomly truncates GPG signed/encrypted messages.",
	"$Revision: 1.27 $",
	"$Date: 2009/09/17 12:04:58 $",
	&ref_count,
	aycryption_init,
	aycryption_finish,
	NULL
};

/* End Module Exports */

unsigned int module_version()
{
	return CORE_VERSION;
}

static int aycryption_init()
{
	gpgme_check_version(NULL);

	input_list *il = g_new0(input_list, 1);
	plugin_info.prefs = il;

	il->widget.checkbox.value = &store_passphrase;
	il->name = "store_passphrase";
	il->label = strdup(_("Store passphrase in memory"));
	il->type = EB_INPUT_CHECKBOX;

	il->next = g_new0(input_list, 1);
	il = il->next;
	il->widget.entry.value = mykey;
	il->name = "mykey";
	il->label = strdup(_("Private key for signing:"));
	il->type = EB_INPUT_ENTRY;

	il->next = g_new0(input_list, 1);
	il = il->next;
	il->widget.checkbox.value = &do_aycryption_debug;
	il->name = "do_aycryption_debug";
	il->label = strdup(_("Enable debugging"));
	il->type = EB_INPUT_CHECKBOX;

	outgoing_message_filters_remote =
		l_list_append(outgoing_message_filters_remote, &aycryption_out);
	incoming_message_filters =
		l_list_append(incoming_message_filters, &aycryption_in);

	gpg_log_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gpg_log_text = gtk_text_view_new();
	gpg_log_swindow = gtk_scrolled_window_new(NULL, NULL);

	gtk_window_set_title(GTK_WINDOW(gpg_log_window),
		_("Aycryption - status"));
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(gpg_log_swindow),
		GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

	html_text_view_init(GTK_TEXT_VIEW(gpg_log_text), HTML_IGNORE_FONT);
	gtk_widget_set_size_request(gpg_log_text, 450, 150);

	gtk_container_add(GTK_CONTAINER(gpg_log_swindow), gpg_log_text);
	gtk_container_add(GTK_CONTAINER(gpg_log_window), gpg_log_swindow);
	gtk_widget_show(gpg_log_text);
	gtk_widget_show(gpg_log_swindow);

	g_signal_connect(gpg_log_window, "delete-event",
		G_CALLBACK(gtk_widget_hide_on_delete), NULL);

	gtk_widget_realize(gpg_log_window);
	gtk_widget_realize(gpg_log_swindow);
	gtk_widget_realize(gpg_log_text);

	tag1 = eb_add_menu_item(_("GPG settings..."), EB_CHAT_WINDOW_MENU,
		set_gpg_key, ebmCONTACTDATA, NULL);
	if (!tag1) {
		eb_debug(DBG_MOD,
			"Error!  Unable to add aycryption menu to chat window menu\n");
		return (-1);
	}
	tag2 = eb_add_menu_item(_("GPG settings..."), EB_CONTACT_MENU,
		set_gpg_key, ebmCONTACTDATA, NULL);
	if (!tag2) {
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag1);
		eb_debug(DBG_MOD,
			"Error!  Unable to add aycryption menu to contact menu\n");
		return (-1);
	}
	tag3 = eb_add_menu_item(_("GPG status..."), EB_CHAT_WINDOW_MENU,
		show_gpg_log, ebmCONTACTDATA, NULL);
	if (!tag3) {
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag1);
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag2);
		eb_debug(DBG_MOD,
			"Error!  Unable to add aycryption menu to chat window menu\n");
		return (-1);
	}
	tag4 = eb_add_menu_item(_("GPG status..."), EB_CONTACT_MENU,
		show_gpg_log, ebmCONTACTDATA, NULL);
	if (!tag4) {
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag1);
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag2);
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag3);
		eb_debug(DBG_MOD,
			"Error!  Unable to add aycryption menu to contact menu\n");
		return (-1);
	}

	return 0;
}

static int aycryption_finish()
{
	outgoing_message_filters_remote =
		l_list_remove(outgoing_message_filters_remote, &aycryption_out);
	incoming_message_filters =
		l_list_remove(incoming_message_filters, &aycryption_in);

	while (plugin_info.prefs) {
		input_list *il = plugin_info.prefs->next;
		free(plugin_info.prefs);
		plugin_info.prefs = il;
	}

	if (tag1)
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag1);
	if (tag2)
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag2);
	if (tag3)
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag3);
	if (tag4)
		eb_remove_menu_item(EB_CHAT_WINDOW_MENU, tag4);

	gtk_widget_destroy(gpg_log_window);
	return 0;
}

/*******************************************************************************
 *                             End Module Code
 ******************************************************************************/

/* removes the <br/> crap that kopete adds */
static void br_to_nl(char *text)
{
	int i, j;
	int visible = 1;
	for (i = 0, j = 0; text[i]; i++) {
		if (text[i] == '<') {
			if (!strncasecmp(text + i + 1, "br/", 3)) {
				/* Kopete compat */
				visible = 0;
				text[j++] = '\n';
			} else if (!strncasecmp(text + i + 1, "br", 2)) {
				/* Fire compat */
				visible = 0;
				text[j++] = '\n';
			}
		} else if (text[i] == '>') {
			if (!visible) {
				visible = 1;
				continue;
			}
		}
		if (visible)
			text[j++] = text[i];
	}
	text[j] = '\0';
}

static gpgme_error_t mygpgme_data_rewind(gpgme_data_t dh)
{
	return (gpgme_data_seek(dh, 0, SEEK_SET) == -1)
		? gpgme_error_from_errno(errno) : 0;
}

static void show_gpg_log(ebmCallbackData *data)
{
	gtk_widget_show(gpg_log_window);
	gtk_widget_show(gpg_log_swindow);
	gtk_widget_show(gpg_log_text);
	gdk_window_raise(gpg_log_window->window);
}

static void set_gpg_key(ebmCallbackData *data)
{
	ebmContactData *ecd = NULL;
	struct contact *ct = NULL;
	struct select_keys_s keys;
	GSList *recp_names = NULL;
	if (IS_ebmContactData(data))
		ecd = (ebmContactData *)data;

	if (ecd)
		ct = find_contact_by_nick(ecd->contact);

	if (!ct) {
		eb_debug(DBG_CRYPT, "contact is null !\n");
		return;
	}
	recp_names = g_slist_append(recp_names, strdup(ct->nick));
	if (ct->gpg_key && ct->gpg_key[0]) ;
	recp_names = g_slist_append(recp_names, strdup(ct->gpg_key));
	keys = gpgmegtk_recipient_selection(recp_names, ct->gpg_do_encryption,
		ct->gpg_do_signature);
	if (keys.kset && keys.key) {
		eb_debug(DBG_CRYPT, "got key %s\n", keys.key);
		strncpy(ct->gpg_key, keys.key, 48);

		ct->gpg_do_encryption = keys.do_crypt;
		ct->gpg_do_signature = keys.do_sign;
	} else {
		eb_debug(DBG_CRYPT, "no key\n");
		strncpy(ct->gpg_key, "", 48);

		ct->gpg_do_encryption = 0;
		ct->gpg_do_signature = keys.do_sign;
	}
	write_contact_list();

}
static char *logcolor[3] = { "#ffa8a8", "#a8a8a8", "#a8ffa8" };

typedef enum {
	LOG_ERR = 0,
	LOG_NORM,
	LOG_OK
} LogLevel;

static void log_action(const struct contact *ct, int loglevel,
	const char *string)
{

	char buf[1024];
	snprintf(buf, 1024, _("<font color=%s><b>%s</b>: %s</font><br>"),
		logcolor[loglevel], ct->nick, string);
	html_text_buffer_append(GTK_TEXT_VIEW(gpg_log_text), buf,
		HTML_IGNORE_NONE);
	if (loglevel == LOG_ERR) {
		show_gpg_log(NULL);
	}
}

void gpg_get_kset(struct contact *ct, gpgme_key_t **kset)
{
	int num_keys = 0;
	gpgme_ctx_t ctx;
	gpgme_error_t err;

	err = gpgme_new(&ctx);
	g_assert(!err);

	err = gpgme_op_keylist_start(ctx, ct->gpg_key, 0);
	if (err) {
		eb_debug(DBG_CRYPT, "err: %s\n", gpgme_strerror(err));
		*kset = NULL;
		return;
	}

	*kset = g_malloc(sizeof(gpgme_key_t));

	while (!(err = gpgme_op_keylist_next(ctx, &(*kset)[num_keys]))) {
		eb_debug(DBG_CRYPT, "found a key for %s with name %s\n",
			ct->gpg_key, (*kset)[num_keys]->uids->name);

		*kset = g_realloc(*kset, sizeof(gpgme_key_t) * (num_keys + 1));
		num_keys++;
	}

	gpgme_release(ctx);
}

static char *aycryption_out(Conversation *conv, const char *s)
{
	char *p = NULL;
	char buf[4096];

	gpgme_data_t plain = NULL;
	gpgme_data_t cipher = NULL;
	gpgme_error_t error;
	gpgme_key_t *kset = NULL;
	int err;

	struct contact *ct = conv->contact;

	if (!ct)
		return g_strdup(s);

	if ((!ct->gpg_do_encryption || !ct->gpg_key || ct->gpg_key == '\0')
		&& !ct->gpg_do_signature) {
		if (ct->gpg_do_encryption)
			log_action(ct, LOG_ERR, "Could not encrypt message.");
		return g_strdup(s);
	}

	if (ct->gpg_do_encryption && ct->gpg_key && ct->gpg_key[0])
		gpg_get_kset(ct, &kset);

	if (ct->gpg_do_encryption && ct->gpg_key && ct->gpg_key[0] && !kset) {
		eb_debug(DBG_CRYPT, "can't init outgoing crypt: %d %p %c\n",
			ct->gpg_do_encryption, ct->gpg_key, ct->gpg_key[0]);
		log_action(ct, LOG_ERR,
			"Could not encrypt message - you may have to set your contact's key.");
		return g_strdup(s);
	}

	error = gpgme_data_new(&plain);
	err = gpgme_data_write(plain, s, strlen(s));

	/* encrypt only */
	if (ct->gpg_do_encryption && kset && !ct->gpg_do_signature) {
		pgp_encrypt(plain, &cipher, kset, FALSE);
		gpgme_data_release(plain);
		plain = NULL;
		log_action(ct, LOG_NORM, "Sent encrypted, unsigned message.");
		/* sign only */
	} else if (!(ct->gpg_do_encryption && kset) && ct->gpg_do_signature) {
		pgp_encrypt(plain, &cipher, NULL, TRUE);
		gpgme_data_release(plain);
		plain = NULL;
		log_action(ct, LOG_NORM, "Sent uncrypted, signed message.");
		/* encrypt and sign */
	} else if (ct->gpg_do_encryption && kset && ct->gpg_do_signature) {
		pgp_encrypt(plain, &cipher, kset, TRUE);
		gpgme_data_release(plain);
		plain = NULL;
		log_action(ct, LOG_NORM, "Sent encrypted, signed message.");
	}
	err = mygpgme_data_rewind(cipher);
	if (err)
		eb_debug(DBG_CRYPT, "error: %s\n", gpgme_strerror(err));

	memset(buf, 0, sizeof(buf));

	while (gpgme_data_read(cipher, buf, 4096) > 0) {
		char tmp[4096];

		snprintf(tmp, sizeof(tmp), "%s%s", (p != NULL) ? p : "", buf);
		if (p)
			g_free(p);
		p = g_strdup(tmp);
		memset(buf, 0, sizeof(buf));
	}

	if (cipher)
		gpgme_data_release(cipher);

	return p;
}

static char *aycryption_in(Conversation *conv, const char *s)
{
	char *p = NULL, *res = NULL, *s_nohtml = NULL;
	gpgme_data_t plain = NULL, cipher = NULL;
	gpgme_key_t key = NULL;
	int err;
	char buf[4096];

	gpgme_ctx_t ctx = NULL;
	gpgme_data_t sigstat = NULL;
	char s_sigstat[1024];
	int was_crypted = 1;
	int curloglevel = 0;
	gpgme_verify_result_t verify_result;

	int sig_code = 0;

	struct contact *ct = conv->contact;

	if (!ct)
		return g_strdup(s);

	memset(buf, 0, 4096);
	if (strncmp(s, "-----BEGIN PGP ", strlen("-----BEGIN PGP "))) {
		eb_debug(DBG_CRYPT, "Incoming message isn't PGP formatted\n");
		return g_strdup(s);
	}

	err = gpgme_new(&ctx);

	if (err) {
		eb_debug(DBG_CRYPT, "gpgme_new failed: %s\n",
			gpgme_strerror(err));
		log_action(ct, LOG_ERR, "Memory error.");
		return g_strdup(s);
	}
	gpgme_data_new(&plain);
	gpgme_data_new(&cipher);

	/* Clean out kopete HTML crap
	 * < vdanen> so like KDE to just bloat stuff for the hell of it
	 */
	s_nohtml = g_strdup(s);
	if (!s_nohtml) {
		eb_debug(DBG_CRYPT, "Couldn't copy message to strip html");
		log_action(ct, LOG_ERR, "Memory error while stripping html.");
		return g_strdup(s);
	}
	br_to_nl(s_nohtml);
	eb_debug(DBG_CRYPT, "html stripped: %s\n", s_nohtml);

	err = gpgme_data_write(cipher, s_nohtml, strlen(s_nohtml));

	if (err == -1)
		perror("cipher write error");

	g_free(s_nohtml);

	mygpgme_data_rewind(cipher);
	mygpgme_data_rewind(plain);

	if (!getenv("GPG_AGENT_INFO")) {
		gpgme_set_passphrase_cb(ctx, gpgmegtk_passphrase_cb, NULL);
	}
	err = gpgme_op_decrypt_verify(ctx, cipher, plain);

	if (err && gpg_err_code(err) != GPG_ERR_NO_DATA) {
		log_action(ct, LOG_ERR,
			"Cannot decrypt message - maybe your contact uses an incorrect key.");
		return g_strdup(s);
	} else if (gpg_err_code(err) == GPG_ERR_NO_DATA) {	/*plaintext signed */
		was_crypted = 0;
		mygpgme_data_rewind(cipher);
		mygpgme_data_rewind(plain);
		err = gpgme_op_verify(ctx, cipher, sigstat, plain);
		if (err)
			eb_debug(DBG_CRYPT, "plaintext err: %s\n",
				gpgme_strerror(err));
	}

	verify_result = gpgme_op_verify_result(ctx);

	if (verify_result && verify_result->signatures) {
		err = gpgme_get_key(ctx, verify_result->signatures->fpr, &key,
			0);
		if (err) {
			eb_debug(DBG_CRYPT, "getkey err %s\n",
				gpgme_strerror(err));
			key = NULL;
		}
	} else
		key = NULL;

	err = mygpgme_data_rewind(plain);
	if (err)
		eb_debug(DBG_CRYPT, "rewind err %d\n", err);

	memset(buf, 0, sizeof(buf));
	while (gpgme_data_read(plain, buf, 4096) > 0) {
		char tmp[4096];
		memset(tmp, 0, 4096);

		snprintf(tmp, sizeof(tmp), "%s%s", (p != NULL) ? p : "", buf);
		if (p)
			g_free(p);
		p = g_strdup(tmp);
		memset(buf, 0, sizeof(buf));
	}

	if (p) {
		while (p[strlen(p) - 1] == '\n' || p[strlen(p) - 1] == '\r')
			p[strlen(p) - 1] = '\0';
	}

	gpgme_release(ctx);

	if (verify_result && verify_result->signatures) {

		sig_code = gpg_err_code(verify_result->signatures->status);

		switch (sig_code) {
		case GPG_ERR_NO_DATA:
			curloglevel = LOG_NORM;
			break;
		case GPG_ERR_NO_ERROR:
			curloglevel = LOG_OK;
			break;
		default:
			curloglevel = LOG_ERR;
			break;
		}

		strcpy(s_sigstat, _("Got an "));
		strcat(s_sigstat,
			was_crypted ? _("encrypted") : _("unencrypted"));

		switch (sig_code) {
		case GPG_ERR_NO_DATA:
			strcat(s_sigstat, _(", unsigned message."));
			break;
		case GPG_ERR_NO_ERROR:
			strcat(s_sigstat, _(", correctly signed (by "));
			strcat(s_sigstat, key->uids->email);
			strcat(s_sigstat, ") message.");
			break;
		case GPG_ERR_SIG_EXPIRED:
			strcat(s_sigstat, _(", badly signed (by "));
			strcat(s_sigstat, key->uids->email);
			strcat(s_sigstat, ") message.");
			break;
		case GPG_ERR_NO_PUBKEY:
			strcat(s_sigstat, _(" message with no key."));
			break;
		case GPG_ERR_CERT_REVOKED:
			strcat(s_sigstat,
				_
				(" message ; signature is valid but  the signing key has been revoked"));
			break;
		case GPG_ERR_BAD_SIGNATURE:
			strcat(s_sigstat, _(" correctly signed (by "));
			strcat(s_sigstat, key->uids->email);
			strcat(s_sigstat,
				_(") message, but signature has expired."));
			break;
		case GPG_ERR_KEY_EXPIRED:
			strcat(s_sigstat, _(" correctly signed (by "));
			strcat(s_sigstat, key->uids->email);
			strcat(s_sigstat, _(") message, but key has expired."));
			break;
		case GPG_ERR_GENERAL:
			strcat(s_sigstat,
				_
				(") message, but there was an error verifying the signature"));
			break;
		default:
			strcat(s_sigstat,
				_
				(" message - Unknown signature status (file a bugreport)!"));
			break;
		}

		if (curloglevel == LOG_ERR) {
			res = g_strdup(s);
			g_free(p);
			p = res;
		}
		log_action(ct, curloglevel, s_sigstat);
	}

	return p;
}

static GSList *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 (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
		goto leave;
	}
	err = gpgme_op_keylist_start(list_ctx, keyid, 1);
	if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
		goto leave;
	}

	err = gpgme_op_keylist_next(list_ctx, &key);
	while (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
		key_list = g_slist_append(key_list, key);
		err = gpgme_op_keylist_next(list_ctx, &key);
	}
	if (gpg_err_code(err) != GPG_ERR_EOF) {
		goto leave;
	}
	err = 0;
	if (key_list == NULL) {
		eb_debug(DBG_CRYPT, "no keys found for keyid \"%s\"\n", keyid);
	}

 leave:
	if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
		eb_debug(DBG_CRYPT, "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;
}

/*
 * plain contains an entire mime object.
 * Encrypt it and return an gpgme_data_t object with the encrypted version of
 * the file or NULL in case of error.
 */
void
pgp_encrypt(gpgme_data_t plain, gpgme_data_t *cipher, gpgme_key_t *kset,
	int sign)
{
	gpgme_ctx_t ctx = NULL;
	gpgme_error_t err;
	GSList *p;
	GSList *key_list = NULL;

	if (sign && mykey[0]) {
		key_list = create_signers_list(mykey);
	}

	err = gpgme_new(&ctx);
	if (gpg_err_code(err) == GPG_ERR_NO_ERROR)
		err = gpgme_data_new(cipher);

	if (gpg_err_code(err) == GPG_ERR_NO_ERROR && sign) {

		if (!getenv("GPG_AGENT_INFO")) {
			gpgme_set_passphrase_cb(ctx, gpgmegtk_passphrase_cb,
				NULL);
		}
		if (kset != NULL) {
			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 (kset != NULL) {
			mygpgme_data_rewind(plain);
			err = gpgme_op_encrypt_sign(ctx, kset, 0, plain,
				*cipher);
		} else {
			mygpgme_data_rewind(plain);
			err = gpgme_op_sign(ctx, plain, *cipher,
				GPGME_SIG_MODE_CLEAR);
		}
		for (p = key_list; p != NULL; p = p->next)
			gpgme_key_unref((gpgme_key_t) p->data);
		g_slist_free(key_list);
	} else if (gpg_err_code(err) == GPG_ERR_NO_ERROR) {
		gpgme_set_armor(ctx, 1);
		mygpgme_data_rewind(plain);
		err = gpgme_op_encrypt(ctx, kset, 0, plain, *cipher);
	}

	if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
		eb_debug(DBG_CRYPT, "pgp_encrypt failed: %s\n",
			gpgme_strerror(err));
		gpgme_data_release(*cipher);
		*cipher = NULL;
	}

	gpgme_release(ctx);
}

gpgme_error_t
gpgmegtk_passphrase_cb(void *opaque, const char *desc,
	const char *passphrase_info, int prev_was_bad, int fd)
{
	const char *pass;

	if (store_passphrase && aycrypt_last_pass != NULL && !prev_was_bad) {
		write(fd, aycrypt_last_pass, strlen(aycrypt_last_pass));
		write(fd, "\n", 1);
		return GPG_ERR_NO_ERROR;
	}

	pass = passphrase_mbox(desc, prev_was_bad);
	if (!pass) {
		eb_debug(DBG_CRYPT, "Cancelled passphrase entry\n");
		write(fd, "\n", 1);
		return GPG_ERR_CANCELED;
	} else {
		if (store_passphrase) {
			if (aycrypt_last_pass)
				g_free(aycrypt_last_pass);
			aycrypt_last_pass = g_strdup(pass);
		}
	}
	write(fd, pass, strlen(pass));
	write(fd, "\n", 1);
	return GPG_ERR_NO_ERROR;
}