aboutsummaryrefslogtreecommitdiffstats
path: root/games/senso
ModeNameSize
-rw-r--r--Makefile721logstatsplainblame
-rw-r--r--distinfo170logstatsplainblame
-rw-r--r--pkg-descr165logstatsplainblame
ref='#n174'>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 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2002 Ximian, Inc. (www.ximian.com)
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include "camel-gpg-context.h"
#include "camel-stream-fs.h"
#include "camel-operation.h"

#define d(x) x

static void camel_gpg_context_class_init (CamelGpgContextClass *klass);
static void camel_gpg_context_init       (CamelGpgContext *obj);
static void camel_gpg_context_finalise   (CamelObject *obj);

static const char *gpg_hash_to_id (CamelCipherContext *context, CamelCipherHash hash);
static CamelCipherHash gpg_id_to_hash (CamelCipherContext *context, const char *id);

static int                  gpg_sign    (CamelCipherContext *ctx, const char *userid, CamelCipherHash hash,
                     CamelStream *istream, CamelStream *ostream, CamelException *ex);
static CamelCipherValidity *gpg_verify  (CamelCipherContext *context, CamelCipherHash hash,
                     CamelStream *istream, CamelStream *sigstream,
                     CamelException *ex);
static int                  gpg_encrypt (CamelCipherContext *context, gboolean sign, const char *userid,
                     GPtrArray *recipients, CamelStream *istream, CamelStream *ostream,
                     CamelException *ex);
static int                  gpg_decrypt (CamelCipherContext *context, CamelStream *istream,
                     CamelStream *ostream, CamelException *ex);
static int              gpg_import_keys (CamelCipherContext *context, CamelStream *istream,
                     CamelException *ex);
static int              gpg_export_keys (CamelCipherContext *context, GPtrArray *keys,
                     CamelStream *ostream, CamelException *ex);

static CamelCipherContextClass *parent_class = NULL;


CamelType
camel_gpg_context_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type = camel_type_register (camel_cipher_context_get_type (),
                        "CamelGpgContext",
                        sizeof (CamelGpgContext),
                        sizeof (CamelGpgContextClass),
                        (CamelObjectClassInitFunc) camel_gpg_context_class_init,
                        NULL,
                        (CamelObjectInitFunc) camel_gpg_context_init,
                        (CamelObjectFinalizeFunc) camel_gpg_context_finalise);
    }
    
    return type;
}

static void
camel_gpg_context_class_init (CamelGpgContextClass *klass)
{
    CamelCipherContextClass *cipher_class = CAMEL_CIPHER_CONTEXT_CLASS (klass);
    
    parent_class = CAMEL_CIPHER_CONTEXT_CLASS (camel_type_get_global_classfuncs (camel_cipher_context_get_type ()));
    
    cipher_class->hash_to_id = gpg_hash_to_id;
    cipher_class->id_to_hash = gpg_id_to_hash;
    cipher_class->sign = gpg_sign;
    cipher_class->verify = gpg_verify;
    cipher_class->encrypt = gpg_encrypt;
    cipher_class->decrypt = gpg_decrypt;
    cipher_class->import_keys = gpg_import_keys;
    cipher_class->export_keys = gpg_export_keys;
}

static void
camel_gpg_context_init (CamelGpgContext *context)
{
    CamelCipherContext *cipher = (CamelCipherContext *) context;
    
    context->always_trust = FALSE;
    
    cipher->sign_protocol = "application/pgp-signature";
    cipher->encrypt_protocol = "application/pgp-encrypted";
    cipher->key_protocol = "application/pgp-keys";
}

static void
camel_gpg_context_finalise (CamelObject *object)
{
    ;
}


/**
 * camel_gpg_context_new:
 * @session: session
 *
 * Creates a new gpg cipher context object.
 *
 * Returns a new gpg cipher context object.
 **/
CamelCipherContext *
camel_gpg_context_new (CamelSession *session)
{
    CamelCipherContext *cipher;
    CamelGpgContext *ctx;
    
    g_return_val_if_fail (CAMEL_IS_SESSION (session), NULL);
    
    ctx = (CamelGpgContext *) camel_object_new (camel_gpg_context_get_type ());
    
    cipher = (CamelCipherContext *) ctx;
    cipher->session = session;
    camel_object_ref (session);
    
    return cipher;
}


/**
 * camel_gpg_context_set_always_trust:
 * @ctx: gpg context
 * @always_trust always truct flag
 *
 * Sets the @always_trust flag on the gpg context which is used for
 * encryption.
 **/
void
camel_gpg_context_set_always_trust (CamelGpgContext *ctx, gboolean always_trust)
{
    g_return_if_fail (CAMEL_IS_GPG_CONTEXT (ctx));
    
    ctx->always_trust = always_trust;
}


static const char *
gpg_hash_to_id (CamelCipherContext *context, CamelCipherHash hash)
{
    switch (hash) {
    case CAMEL_CIPHER_HASH_MD2:
        return "pgp-md2";
    case CAMEL_CIPHER_HASH_MD5:
        return "pgp-md5";
    case CAMEL_CIPHER_HASH_SHA1:
    case CAMEL_CIPHER_HASH_DEFAULT:
        return "pgp-sha1";
    case CAMEL_CIPHER_HASH_RIPEMD160:
        return "pgp-ripemd160";
    case CAMEL_CIPHER_HASH_TIGER192:
        return "pgp-tiger192";
    case CAMEL_CIPHER_HASH_HAVAL5160:
        return "pgp-haval-5-160";
    }
    
    return NULL;
}

static CamelCipherHash
gpg_id_to_hash (CamelCipherContext *context, const char *id)
{
    if (id) {
        if (!strcmp (id, "pgp-md2"))
            return CAMEL_CIPHER_HASH_MD2;
        else if (!strcmp (id, "pgp-md5"))
            return CAMEL_CIPHER_HASH_MD5;
        else if (!strcmp (id, "pgp-sha1"))
            return CAMEL_CIPHER_HASH_SHA1;
        else if (!strcmp (id, "pgp-ripemd160"))
            return CAMEL_CIPHER_HASH_RIPEMD160;
        else if (!strcmp (id, "tiger192"))
            return CAMEL_CIPHER_HASH_TIGER192;
        else if (!strcmp (id, "haval-5-160"))
            return CAMEL_CIPHER_HASH_HAVAL5160;
    }
    
    return CAMEL_CIPHER_HASH_DEFAULT;
}


enum _GpgCtxMode {
    GPG_CTX_MODE_SIGN,
    GPG_CTX_MODE_VERIFY,
    GPG_CTX_MODE_ENCRYPT,
    GPG_CTX_MODE_DECRYPT,
    GPG_CTX_MODE_IMPORT,
    GPG_CTX_MODE_EXPORT,
};

enum _GpgTrustMetric {
    GPG_TRUST_UNKNOWN,
    GPG_TRUST_NEVER,
    GPG_TRUST_MARGINAL,
    GPG_TRUST_FULLY,
    GPG_TRUST_ULTIMATE
};

struct _GpgCtx {
    enum _GpgCtxMode mode;
    CamelSession *session;
    GHashTable *userid_hint;
    pid_t pid;
    
    char *userid;
    char *sigfile;
    GPtrArray *recipients;
    CamelCipherHash hash;
    
    int stdin_fd;
    int stdout_fd;
    int stderr_fd;
    int status_fd;
    int passwd_fd;  /* only needed for sign/decrypt */
    
    /* status-fd buffer */
    unsigned char *statusbuf;
    unsigned char *statusptr;
    unsigned int statusleft;
    
    char *passwd;
    
    CamelStream *istream;
    CamelStream *ostream;
    
    GByteArray *diagnostics;
    
    int exit_status;
    
    unsigned int exited:1;
    unsigned int complete:1;
    unsigned int seen_eof1:1;
    unsigned int seen_eof2:1;
    unsigned int always_trust:1;
    unsigned int armor:1;
    unsigned int need_passwd:1;
    unsigned int send_passwd:1;
    
    unsigned int bad_passwds:2;
    
    unsigned int validsig:1;
    unsigned int trust:3;
    
    unsigned int padding:18;
};

static struct _GpgCtx *
gpg_ctx_new (CamelSession *session)
{
    struct _GpgCtx *gpg;
    
    gpg = g_new (struct _GpgCtx, 1);
    gpg->mode = GPG_CTX_MODE_SIGN;
    gpg->session = session;
    camel_object_ref (CAMEL_OBJECT (session));
    gpg->userid_hint = g_hash_table_new (g_str_hash, g_str_equal);
    gpg->complete = FALSE;
    gpg->seen_eof1 = FALSE;
    gpg->seen_eof2 = FALSE;
    gpg->pid = (pid_t) -1;
    gpg->exit_status = 0;
    gpg->exited = FALSE;
    
    gpg->userid = NULL;
    gpg->sigfile = NULL;
    gpg->recipients = NULL;
    gpg->hash = CAMEL_CIPHER_HASH_DEFAULT;
    gpg->always_trust = FALSE;
    gpg->armor = FALSE;
    
    gpg->stdin_fd = -1;
    gpg->stdout_fd = -1;
    gpg->stderr_fd = -1;
    gpg->status_fd = -1;
    gpg->passwd_fd = -1;
    
    gpg->statusbuf = g_malloc (128);
    gpg->statusptr = gpg->statusbuf;
    gpg->statusleft = 128;
    
    gpg->bad_passwds = 0;
    gpg->need_passwd = FALSE;
    gpg->send_passwd = FALSE;
    gpg->passwd = NULL;
    
    gpg->validsig = FALSE;
    gpg->trust = GPG_TRUST_UNKNOWN;
    
    gpg->istream = NULL;
    gpg->ostream = NULL;
    
    gpg->diagnostics = g_byte_array_new ();
    
    return gpg;
}

static void
gpg_ctx_set_mode (struct _GpgCtx *gpg, enum _GpgCtxMode mode)
{
    gpg->mode = mode;
    gpg->need_passwd = ((gpg->mode == GPG_CTX_MODE_SIGN) || (gpg->mode == GPG_CTX_MODE_DECRYPT));
}

static void
gpg_ctx_set_hash (struct _GpgCtx *gpg, CamelCipherHash hash)
{
    gpg->hash = hash;
}

static void
gpg_ctx_set_always_trust (struct _GpgCtx *gpg, gboolean trust)
{
    gpg->always_trust = trust;
}

static void
gpg_ctx_set_userid (struct _GpgCtx *gpg, const char *userid)
{
    g_free (gpg->userid);
    gpg->userid = g_strdup (userid);
}

static void
gpg_ctx_add_recipient (struct _GpgCtx *gpg, const char *keyid)
{
    if (gpg->mode != GPG_CTX_MODE_ENCRYPT && gpg->mode != GPG_CTX_MODE_EXPORT)
        return;
    
    if (!gpg->recipients)
        gpg->recipients = g_ptr_array_new ();
    
    g_ptr_array_add (gpg->recipients, g_strdup (keyid));
}

static void
gpg_ctx_set_sigfile (struct _GpgCtx *gpg, const char *sigfile)
{
    g_free (gpg->sigfile);
    gpg->sigfile = g_strdup (sigfile);
}

static void
gpg_ctx_set_armor (struct _GpgCtx *gpg, gboolean armor)
{
    gpg->armor = armor;
}

static void
gpg_ctx_set_istream (struct _GpgCtx *gpg, CamelStream *istream)
{
    camel_object_ref (CAMEL_OBJECT (istream));
    if (gpg->istream)
        camel_object_unref (CAMEL_OBJECT (gpg->istream));
    gpg->istream = istream;
}

static void
gpg_ctx_set_ostream (struct _GpgCtx *gpg, CamelStream *ostream)
{
    camel_object_ref (CAMEL_OBJECT (ostream));
    if (gpg->ostream)
        camel_object_unref (CAMEL_OBJECT (gpg->ostream));
    gpg->ostream = ostream;
}

static char *
gpg_ctx_get_diagnostics (struct _GpgCtx *gpg)
{
    return g_strndup (gpg->diagnostics->data, gpg->diagnostics->len);
}

static void
userid_hint_free (gpointer key, gpointer value, gpointer user_data)
{
    g_free (key);
    g_free (value);
}

static void
gpg_ctx_free (struct _GpgCtx *gpg)
{
    int i;
    
    if (gpg->session)
        camel_object_unref (CAMEL_OBJECT (gpg->session));
    
    g_hash_table_foreach (gpg->userid_hint, userid_hint_free, NULL);
    g_hash_table_destroy (gpg->userid_hint);
    
    g_free (gpg->userid);
    
    g_free (gpg->sigfile);
    
    if (gpg->recipients) {
        for (i = 0; i < gpg->recipients->len; i++)
            g_free (gpg->recipients->pdata[i]);
    
        g_ptr_array_free (gpg->recipients, TRUE);
    }
    
    if (gpg->stdin_fd != -1)
        close (gpg->stdin_fd);
    if (gpg->stdout_fd != -1)
        close (gpg->stdout_fd);
    if (gpg->stderr_fd != -1)
        close (gpg->stderr_fd);
    if (gpg->status_fd != -1)
        close (gpg->status_fd);
    if (gpg->passwd_fd != -1)
        close (gpg->passwd_fd);
    
    g_free (gpg->statusbuf);
    
    if (gpg->passwd)
        g_free (gpg->passwd);
    
    if (gpg->istream)
        camel_object_unref (CAMEL_OBJECT (gpg->istream));
    
    if (gpg->ostream)
        camel_object_unref (CAMEL_OBJECT (gpg->ostream));
    
    g_byte_array_free (gpg->diagnostics, TRUE);
    
    g_free (gpg);
}

static const char *
gpg_hash_str (CamelCipherHash hash)
{
    switch (hash) {
    case CAMEL_CIPHER_HASH_MD2:
        return "--digest-algo=MD2";
    case CAMEL_CIPHER_HASH_MD5:
        return "--digest-algo=MD5";
    case CAMEL_CIPHER_HASH_SHA1:
        return "--digest-algo=SHA1";
    case CAMEL_CIPHER_HASH_RIPEMD160:
        return "--digest-algo=RIPEMD160";
    default:
        return NULL;
    }
}

static GPtrArray *
gpg_ctx_get_argv (struct _GpgCtx *gpg, int status_fd, char **sfd, int passwd_fd, char **pfd)
{
    const char *hash_str;
    GPtrArray *argv;
    char *buf;
    int i;
    
    argv = g_ptr_array_new ();
    g_ptr_array_add (argv, "gpg");
    
    g_ptr_array_add (argv, "--verbose");
    g_ptr_array_add (argv, "--no-secmem-warning");
    g_ptr_array_add (argv, "--no-greeting");
    g_ptr_array_add (argv, "--no-tty");
    if (passwd_fd == -1) {
        /* only use batch mode if we don't intend on using the
                   interactive --command-fd option */
        g_ptr_array_add (argv, "--batch");
        g_ptr_array_add (argv, "--yes");
    }
    
    *sfd = buf = g_strdup_printf ("--status-fd=%d", status_fd);
    g_ptr_array_add (argv, buf);
    
    if (passwd_fd != -1) {
        *pfd = buf = g_strdup_printf ("--command-fd=%d", passwd_fd);
        g_ptr_array_add (argv, buf);
    }
    
    switch (gpg->mode) {
    case GPG_CTX_MODE_SIGN:
        g_ptr_array_add (argv, "--sign");
        g_ptr_array_add (argv, "--detach");
        if (gpg->armor)
            g_ptr_array_add (argv, "--armor");
        hash_str = gpg_hash_str (gpg->hash);
        if (hash_str)
            g_ptr_array_add (argv, (char *) hash_str);
        if (gpg->userid) {
            g_ptr_array_add (argv, "-u");
            g_ptr_array_add (argv, (char *) gpg->userid);
        }
        g_ptr_array_add (argv, "--output");
        g_ptr_array_add (argv, "-");
        break;
    case GPG_CTX_MODE_VERIFY:
        if (!camel_session_is_online (gpg->session)) {
            /* this is a deprecated flag to gpg since 1.0.7 */
            /*g_ptr_array_add (argv, "--no-auto-key-retrieve");*/
            g_ptr_array_add (argv, "--keyserver-options");
            g_ptr_array_add (argv, "no-auto-key-retrieve");
        }
        g_ptr_array_add (argv, "--verify");
        if (gpg->sigfile)
            g_ptr_array_add (argv, gpg->sigfile);
        g_ptr_array_add (argv, "-");
        break;
    case GPG_CTX_MODE_ENCRYPT:
        g_ptr_array_add (argv,  "--encrypt");
        if (gpg->armor)
            g_ptr_array_add (argv, "--armor");
        if (gpg->always_trust)
            g_ptr_array_add (argv, "--always-trust");
        if (gpg->userid) {
            g_ptr_array_add (argv, "-u");
            g_ptr_array_add (argv, (char *) gpg->userid);
        }
        if (gpg->recipients) {
            for (i = 0; i < gpg->recipients->len; i++) {
                g_ptr_array_add (argv, "-r");
                g_ptr_array_add (argv, gpg->recipients->pdata[i]);
            }
        }
        g_ptr_array_add (argv, "--output");
        g_ptr_array_add (argv, "-");
        break;
    case GPG_CTX_MODE_DECRYPT:
        g_ptr_array_add (argv, "--decrypt");
        g_ptr_array_add (argv, "--output");
        g_ptr_array_add (argv, "-");
        break;
    case GPG_CTX_MODE_IMPORT:
        g_ptr_array_add (argv, "--import");
        g_ptr_array_add (argv, "-");
        break;
    case GPG_CTX_MODE_EXPORT:
        if (gpg->armor)
            g_ptr_array_add (argv, "--armor");
        g_ptr_array_add (argv, "--export");
        for (i = 0; i < gpg->recipients->len; i++)
            g_ptr_array_add (argv, gpg->recipients->pdata[i]);
        break;
    }
    
    g_ptr_array_add (argv, NULL);
    
    return argv;
}

static int
gpg_ctx_op_start (struct _GpgCtx *gpg)
{
    char *status_fd = NULL, *passwd_fd = NULL;
    int i, maxfd, errnosave, fds[10];
    GPtrArray *argv;
    
    for (i = 0; i < 10; i++)
        fds[i] = -1;
    
    maxfd = gpg->need_passwd ? 10 : 8;
    for (i = 0; i < maxfd; i += 2) {
        if (pipe (fds + i) == -1)
            goto exception;
    }
    
    argv = gpg_ctx_get_argv (gpg, fds[7], &status_fd, fds[8], &passwd_fd);
    
    if (!(gpg->pid = fork ())) {
        /* child process */
        
        if ((dup2 (fds[0], STDIN_FILENO) < 0 ) ||
            (dup2 (fds[3], STDOUT_FILENO) < 0 ) ||
            (dup2 (fds[5], STDERR_FILENO) < 0 )) {
            _exit (255);
        }
        
        /* Dissociate from camel's controlling terminal so
         * that gpg won't be able to read from it.
         */
        setsid ();
        
        maxfd = sysconf (_SC_OPEN_MAX);
        if (maxfd > 0) {
            /* Loop over all fds. */
            for (i = 0; i < maxfd; i++) {
                if ((i != STDIN_FILENO) &&
                    (i != STDOUT_FILENO) &&
                    (i != STDERR_FILENO) &&
                    (i != fds[7]) &&  /* status fd */
                    (i != fds[8]))    /* passwd fd */
                    close (i);
            }
        }
        
        /* run gpg */
        execvp ("gpg", (char **) argv->pdata);
        _exit (255);
    } else if (gpg->pid < 0) {
        g_ptr_array_free (argv, TRUE);
        g_free (status_fd);
        g_free (passwd_fd);
        goto exception;
    }
    
    g_ptr_array_free (argv, TRUE);
    g_free (status_fd);
    g_free (passwd_fd);
    
    /* Parent */
    close (fds[0]);
    gpg->stdin_fd = fds[1];
    gpg->stdout_fd = fds[2];
    close (fds[3]);
    gpg->stderr_fd = fds[4];
    close (fds[5]);
    gpg->status_fd = fds[6];
    close (fds[7]);
    if (gpg->need_passwd) {
        close (fds[8]);
        gpg->passwd_fd = fds[9];
        fcntl (gpg->passwd_fd, F_SETFL, O_NONBLOCK);
    }
    
    fcntl (gpg->stdin_fd, F_SETFL, O_NONBLOCK);
    fcntl (gpg->stdout_fd, F_SETFL, O_NONBLOCK);
    fcntl (gpg->stderr_fd, F_SETFL, O_NONBLOCK);
    fcntl (gpg->status_fd, F_SETFL, O_NONBLOCK);
    
    return 0;
    
 exception:
    
    errnosave = errno;
    
    for (i = 0; i < 10; i++) {
        if (fds[i] != -1)
            close (fds[i]);
    }
    
    errno = errnosave;
    
    return -1;
}

static const char *
next_token (const char *in, char **token)
{
    const char *start, *inptr = in;
    
    while (*inptr == ' ')
        inptr++;
    
    if (*inptr == '\0' || *inptr == '\n') {
        if (token)
            *token = NULL;
        return inptr;
    }
    
    start = inptr;
    while (*inptr && *inptr != ' ' && *inptr != '\n')
        inptr++;
    
    if (token)
        *token = g_strndup (start, inptr - start);
    
    return inptr;
}

static int
gpg_ctx_parse_status (struct _GpgCtx *gpg, CamelException *ex)
{
    register unsigned char *inptr;
    const unsigned char *status;
    int len;
    
 parse:
    
    inptr = gpg->statusbuf;
    while (inptr < gpg->statusptr && *inptr != '\n')
        inptr++;
    
    if (*inptr != '\n') {
        /* we don't have enough data buffered to parse this status line */
        return 0;
    }
    
    *inptr++ = '\0';
    status = gpg->statusbuf;
    
    d(printf ("status: %s\n", status));
    
    if (strncmp (status, "[GNUPG:] ", 9) != 0) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Unexpected GnuPG status message encountered:\n\n%s"),
                      status);
        return -1;
    }
    
    status += 9;
    
    if (!strncmp (status, "USERID_HINT ", 12)) {
        char *hint, *user;
        
        status += 12;
        status = next_token (status, &hint);
        if (!hint) {
            camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Failed to parse gpg userid hint."));
            return -1;
        }
        
        if (g_hash_table_lookup (gpg->userid_hint, hint)) {
            /* we already have this userid hint... */
            g_free (hint);
            goto recycle;
        }
        
        user = g_strdup (status);
        g_strstrip (user);
        
        g_hash_table_insert (gpg->userid_hint, hint, user);
    } else if (!strncmp (status, "NEED_PASSPHRASE ", 16)) {
        char *prompt, *userid, *passwd;
        const char *name;
        
        status += 16;
        
        status = next_token (status, &userid);
        if (!userid) {
            camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Failed to parse gpg passphrase request."));
            return -1;
        }
        
        name = g_hash_table_lookup (gpg->userid_hint, userid);
        if (!name)
            name = userid;
        
        prompt = g_strdup_printf (_("You need a passphrase to unlock the key for\n"
                        "user: \"%s\""), name);
        
        passwd = camel_session_get_password (gpg->session, prompt, TRUE, NULL, userid, ex);
        g_free (prompt);
        
        g_free (gpg->userid);
        gpg->userid = userid;
        
        if (passwd == NULL) {
            if (!camel_exception_is_set (ex))
                camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, _("Cancelled."));
            return -1;
        }
        
        gpg->passwd = g_strdup_printf ("%s\n", passwd);
        memset (passwd, 0, strlen (passwd));
        g_free (passwd);
        
        gpg->send_passwd = TRUE;
    } else if (!strncmp (status, "GOOD_PASSPHRASE", 15)) {
        gpg->bad_passwds = 0;
    } else if (!strncmp (status, "BAD_PASSPHRASE", 14)) {
        gpg->bad_passwds++;
        
        camel_session_forget_password (gpg->session, NULL, gpg->userid, ex);
        
        if (gpg->bad_passwds == 3) {
            camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                         _("Failed to unlock secret key: 3 bad passphrases given."));
            return -1;
        }
    } else if (!strncmp (status, "UNEXPECTED ", 11)) {
        /* this is an error */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Unexpected response from GnuPG: %s"),
                      status + 11);
        return -1;
    } else if (!strncmp (status, "NODATA", 6)) {
        /* this is an error */
        if (gpg->diagnostics->len)
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "%.*s",
                          gpg->diagnostics->len, gpg->diagnostics->data);
        else
            camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                         _("No data provided"));
        return -1;
    } else {
        /* check to see if we are complete */
        switch (gpg->mode) {
        case GPG_CTX_MODE_SIGN:
            if (!strncmp (status, "SIG_CREATED ", 12)) {
                /* FIXME: save this state? */
            }
            break;
        case GPG_CTX_MODE_VERIFY:
            if (!strncmp (status, "TRUST_", 6)) {
                status += 6;
                if (!strncmp (status, "NEVER", 5)) {
                    gpg->trust = GPG_TRUST_NEVER;
                } else if (!strncmp (status, "MARGINAL", 8)) {
                    gpg->trust = GPG_TRUST_MARGINAL;
                } else if (!strncmp (status, "FULLY", 5)) {
                    gpg->trust = GPG_TRUST_FULLY;
                } else if (!strncmp (status, "ULTIMATE", 8)) {
                    gpg->trust = GPG_TRUST_ULTIMATE;
                }
                
                /* Since verifying a signature will never produce output
                   on gpg's stdout descriptor, we use this EOF bit for
                   making sure that we get a TRUST metric. */
                gpg->seen_eof1 = TRUE;
            } else if (!strncmp (status, "VALIDSIG", 8)) {
                gpg->validsig = TRUE;
            } else if (!strncmp (status, "BADSIG", 6)) {
                gpg->validsig = FALSE;
            } else if (!strncmp (status, "ERRSIG", 6)) {
                /* Note: NO_PUBKEY often comes after an ERRSIG, but do we really care? */
                gpg->validsig = FALSE;
            }
            break;
        case GPG_CTX_MODE_ENCRYPT:
            if (!strncmp (status, "BEGIN_ENCRYPTION", 16)) {
                /* nothing to do... but we know to expect data on stdout soon */
            } else if (!strncmp (status, "END_ENCRYPTION", 14)) {
                /* nothing to do, but we know the end is near? */
            } else if (!strncmp (status, "NO_RECP", 7)) {
                camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                             _("Failed to encrypt: No valid recipients specified."));
                return -1;
            }
            break;
        case GPG_CTX_MODE_DECRYPT:
            if (!strncmp (status, "BEGIN_DECRYPTION", 16)) {
                /* nothing to do... but we know to expect data on stdout soon */
            } else if (!strncmp (status, "END_DECRYPTION", 14)) {
                /* nothing to do, but we know the end is near? */
            }
            break;
        case GPG_CTX_MODE_IMPORT:
            /* hack to work around the fact that gpg
                           doesn't write anything to stdout when
                           importing keys */
            if (!strncmp (status, "IMPORT_RES", 10))
                gpg->seen_eof1 = TRUE;
            break;
        }
    }
    
 recycle:
    
    /* recycle our statusbuf by moving inptr to the beginning of statusbuf */
    len = gpg->statusptr - inptr;
    memmove (gpg->statusbuf, inptr, len);
    
    len = inptr - gpg->statusbuf;
    gpg->statusleft += len;
    gpg->statusptr -= len;
    
    /* if we have more data, try parsing the next line? */
    if (gpg->statusptr > gpg->statusbuf)
        goto parse;
    
    return 0;
}

#define status_backup(gpg, start, len) G_STMT_START {                     \
    if (gpg->statusleft <= len) {                                     \
        unsigned int slen, soff;                                  \
                                                                  \
        slen = soff = gpg->statusptr - gpg->statusbuf;            \
        slen = slen ? slen : 1;                                   \
                                                                  \
        while (slen < soff + len)                                 \
            slen <<= 1;                                       \
                                                                  \
        gpg->statusbuf = g_realloc (gpg->statusbuf, slen + 1);    \
        gpg->statusptr = gpg->statusbuf + soff;                   \
        gpg->statusleft = slen - soff;                            \
    }                                                                 \
                                                                      \
    memcpy (gpg->statusptr, start, len);                              \
    gpg->statusptr += len;                                            \
    gpg->statusleft -= len;                                           \
} G_STMT_END

static int
gpg_ctx_op_step (struct _GpgCtx *gpg, CamelException *ex)
{
    fd_set rdset, wrset, *wrsetp = NULL;
    struct timeval timeout;
    const char *mode;
    int maxfd = 0;
    int ready;
    
 retry:
    FD_ZERO (&rdset);
    FD_SET (gpg->stdout_fd, &rdset);
    FD_SET (gpg->stderr_fd, &rdset);
    FD_SET (gpg->status_fd, &rdset);
    
    maxfd = MAX (gpg->stdout_fd, gpg->stderr_fd);
    maxfd = MAX (maxfd, gpg->status_fd);
    
    if (gpg->stdin_fd != -1 || gpg->passwd_fd != -1) {
        FD_ZERO (&wrset);
        if (gpg->stdin_fd != -1) {
            FD_SET (gpg->stdin_fd, &wrset);
            maxfd = MAX (maxfd, gpg->stdin_fd);
        }
        if (gpg->passwd_fd != -1) {
            FD_SET (gpg->passwd_fd, &wrset);
            maxfd = MAX (maxfd, gpg->passwd_fd);
        }
        
        wrsetp = &wrset;
    }
    
    timeout.tv_sec = 10; /* timeout in seconds */
    timeout.tv_usec = 0;
    
    if ((ready = select (maxfd + 1, &rdset, wrsetp, NULL, &timeout)) == 0)
        return 0;
    
    if (ready < 0) {
        if (errno == EINTR)
            goto retry;
        
        d(printf ("select() failed: %s\n", strerror (errno)));
        
        return -1;
    }
    
    /* Test each and every file descriptor to see if it's 'ready',
       and if so - do what we can with it and then drop through to
       the next file descriptor and so on until we've done what we
       can to all of them. If one fails along the way, return
       -1. */
    
    if (FD_ISSET (gpg->status_fd, &rdset)) {
        /* read the status message and decide what to do... */
        char buffer[4096];
        ssize_t nread;
        
        d(printf ("reading from gpg's status-fd...\n"));
        
        do {
            nread = read (gpg->status_fd, buffer, sizeof (buffer));
        } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
        if (nread == -1)
            goto exception;
        
        if (nread > 0) {
            status_backup (gpg, buffer, nread);
            
            if (gpg_ctx_parse_status (gpg, ex) == -1)
                return -1;
        } else {
            gpg->complete = TRUE;
        }
    }
    
    if (FD_ISSET (gpg->stdout_fd, &rdset) && gpg->ostream) {
        char buffer[4096];
        ssize_t nread;
        
        d(printf ("reading gpg's stdout...\n"));
        
        do {
            nread = read (gpg->stdout_fd, buffer, sizeof (buffer));
        } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
        if (nread == -1)
            goto exception;
        
        if (nread > 0) {
            if (camel_stream_write (gpg->ostream, buffer, (size_t) nread) == -1)
                goto exception;
        } else {
            gpg->seen_eof1 = TRUE;
        }
    }
    
    if (FD_ISSET (gpg->stderr_fd, &rdset)) {
        char buffer[4096];
        ssize_t nread;
        
        d(printf ("reading gpg's stderr...\n"));
        
        do {
            nread = read (gpg->stderr_fd, buffer, sizeof (buffer));
        } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
        if (nread == -1)
            goto exception;
        
        if (nread > 0) {
            g_byte_array_append (gpg->diagnostics, buffer, nread);
        } else {
            gpg->seen_eof2 = TRUE;
        }
    }
    
    if (wrsetp && gpg->passwd_fd != -1 && FD_ISSET (gpg->passwd_fd, &wrset) && gpg->need_passwd && gpg->send_passwd) {
        ssize_t w, nwritten = 0;
        size_t n;
        
        d(printf ("sending gpg our passphrase...\n"));
        
        /* send the passphrase to gpg */
        n = strlen (gpg->passwd);
        do {
            do {
                w = write (gpg->passwd_fd, gpg->passwd + nwritten, n - nwritten);
            } while (w == -1 && (errno == EINTR || errno == EAGAIN));
            
            if (w > 0)
                nwritten += w;
        } while (nwritten < n && w != -1);
        
        /* zero and free our passwd buffer */
        memset (gpg->passwd, 0, n);
        g_free (gpg->passwd);
        gpg->passwd = NULL;
        
        if (w == -1)
            goto exception;
        
        gpg->send_passwd = FALSE;
    }
    
    if (gpg->istream && wrsetp && gpg->stdin_fd != -1 && FD_ISSET (gpg->stdin_fd, &wrset)) {
        char buffer[4096];
        ssize_t nread;
        
        d(printf ("writing to gpg's stdin...\n"));
        
        /* write our stream to gpg's stdin */
        nread = camel_stream_read (gpg->istream, buffer, sizeof (buffer));
        if (nread > 0) {
            ssize_t w, nwritten = 0;
            
            do {
                do {
                    w = write (gpg->stdin_fd, buffer + nwritten, nread - nwritten);
                } while (w == -1 && (errno == EINTR || errno == EAGAIN));
                
                if (w > 0)
                    nwritten += w;
            } while (nwritten < nread && w != -1);
            
            d(printf ("wrote %d (out of %d) bytes to gpg's stdin\n", nwritten, nread));
            
            if (w == -1)
                goto exception;
        }
        
        if (camel_stream_eos (gpg->istream)) {
            d(printf ("closing gpg's stdin\n"));
            close (gpg->stdin_fd);
            gpg->stdin_fd = -1;
        }
    }
    
    return 0;
    
 exception:
    
    switch (gpg->mode) {
    case GPG_CTX_MODE_SIGN:
        mode = "sign";
        break;
    case GPG_CTX_MODE_VERIFY:
        mode = "verify";
        break;
    case GPG_CTX_MODE_ENCRYPT:
        mode = "encrypt";
        break;
    case GPG_CTX_MODE_DECRYPT:
        mode = "decrypt";
        break;
    case GPG_CTX_MODE_IMPORT:
        mode = "import keys";
        break;
    case GPG_CTX_MODE_EXPORT:
        mode = "export keys";
        break;
    default:
        g_assert_not_reached ();
        mode = NULL;
        break;
    }
    
    if (gpg->diagnostics->len) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to GPG %s: %s\n\n%.*s"),
                      mode, g_strerror (errno),
                      gpg->diagnostics->len,
                      gpg->diagnostics->data);
    } else {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to GPG %s: %s\n"),
                      mode, g_strerror (errno));
    }
    
    return -1;
}

static gboolean
gpg_ctx_op_complete (struct _GpgCtx *gpg)
{
    return gpg->complete && gpg->seen_eof1 && gpg->seen_eof2;
}

static gboolean
gpg_ctx_op_exited (struct _GpgCtx *gpg)
{
    pid_t retval;
    int status;
    
    if (gpg->exited)
        return TRUE;
    
    retval = waitpid (gpg->pid, &status, WNOHANG);
    if (retval == gpg->pid) {
        gpg->exit_status = status;
        gpg->exited = TRUE;
        return TRUE;
    }
    
    return FALSE;
}

static void
gpg_ctx_op_cancel (struct _GpgCtx *gpg)
{
    pid_t retval;
    int status;
    
    if (gpg->exited)
        return;
    
    kill (gpg->pid, SIGTERM);
    sleep (1);
    retval = waitpid (gpg->pid, &status, WNOHANG);
    if (retval == (pid_t) 0) {
        /* no more mr nice guy... */
        kill (gpg->pid, SIGKILL);
        sleep (1);
        waitpid (gpg->pid, &status, WNOHANG);
    }
}

static int
gpg_ctx_op_wait (struct _GpgCtx *gpg)
{
    sigset_t mask, omask;
    pid_t retval;
    int status;
    
    if (!gpg->exited) {
        sigemptyset (&mask);
        sigaddset (&mask, SIGALRM);
        sigprocmask (SIG_BLOCK, &mask, &omask);
        alarm (1);
        retval = waitpid (gpg->pid, &status, 0);
        alarm (0);
        sigprocmask (SIG_SETMASK, &omask, NULL);
        
        if (retval == (pid_t) -1 && errno == EINTR) {
            /* The child is hanging: send a friendly reminder. */
            kill (gpg->pid, SIGTERM);
            sleep (1);
            retval = waitpid (gpg->pid, &status, WNOHANG);
            if (retval == (pid_t) 0) {
                /* Still hanging; use brute force. */
                kill (gpg->pid, SIGKILL);
                sleep (1);
                retval = waitpid (gpg->pid, &status, WNOHANG);
            }
        }
    } else {
        status = gpg->exit_status;
        retval = gpg->pid;
    }
    
    if (retval != (pid_t) -1 && WIFEXITED (status))
        return WEXITSTATUS (status);
    else
        return -1;
}



static int
gpg_sign (CamelCipherContext *context, const char *userid, CamelCipherHash hash,
      CamelStream *istream, CamelStream *ostream, CamelException *ex)
{
    struct _GpgCtx *gpg;
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_SIGN);
    gpg_ctx_set_hash (gpg, hash);
    gpg_ctx_set_armor (gpg, TRUE);
    gpg_ctx_set_userid (gpg, userid);
    gpg_ctx_set_istream (gpg, istream);
    gpg_ctx_set_ostream (gpg, ostream);
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to execute gpg: %s"), g_strerror (errno));
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    while (!gpg_ctx_op_complete (gpg)) {
        if (camel_operation_cancel_check (NULL)) {
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                         _("Cancelled."));
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
        
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
    }
    
    if (gpg_ctx_op_wait (gpg) != 0) {
        char *diagnostics;
        
        diagnostics = gpg_ctx_get_diagnostics (gpg);
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     diagnostics && *diagnostics ? diagnostics :
                     _("Failed to execute gpg."));
        g_free (diagnostics);
        
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    gpg_ctx_free (gpg);
    
    return 0;
}


static char *
swrite (CamelStream *istream)
{
    CamelStream *ostream;
    char *template;
    int fd, ret;
    
    template = g_strdup ("/tmp/evolution-pgp.XXXXXX");
    fd = mkstemp (template);
    if (fd == -1) {
        g_free (template);
        return NULL;
    }
    
    ostream = camel_stream_fs_new_with_fd (fd);
    ret = camel_stream_write_to_stream (istream, ostream);
    if (ret != -1) {
        ret = camel_stream_flush (ostream);
        if (ret != -1)
            ret = camel_stream_close (ostream);
    }
    camel_object_unref (CAMEL_OBJECT (ostream));
    
    if (ret == -1) {
        unlink (template);
        g_free (template);
        return NULL;
    }
    
    return template;
}

static CamelCipherValidity *
gpg_verify (CamelCipherContext *context, CamelCipherHash hash,
        CamelStream *istream, CamelStream *sigstream,
        CamelException *ex)
{
    CamelCipherValidity *validity;
    char *diagnostics = NULL;
    struct _GpgCtx *gpg;
    char *sigfile = NULL;
    gboolean valid;
    
    if (sigstream != NULL) {
        /* We are going to verify a detached signature so save
           the signature to a temp file. */
        sigfile = swrite (sigstream);
        if (sigfile == NULL) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("Cannot verify message signature: "
                        "could not create temp file: %s"),
                          g_strerror (errno));
            return NULL;
        }
    }
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_VERIFY);
    gpg_ctx_set_hash (gpg, hash);
    gpg_ctx_set_sigfile (gpg, sigfile);
    gpg_ctx_set_istream (gpg, istream);
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Failed to execute gpg."));
        gpg_ctx_free (gpg);
        goto exception;
    }
    
    while (!gpg_ctx_op_complete (gpg) && !gpg_ctx_op_exited (gpg)) {
        if (camel_operation_cancel_check (NULL)) {
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                         _("Cancelled."));
            gpg_ctx_op_cancel (gpg);
            goto exception;
        }
        
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            goto exception;
        }
    }
    
    diagnostics = gpg_ctx_get_diagnostics (gpg);
    
    valid = gpg_ctx_op_wait (gpg) == 0;
    gpg_ctx_free (gpg);
    
    validity = camel_cipher_validity_new ();
    camel_cipher_validity_set_valid (validity, valid);
    camel_cipher_validity_set_description (validity, diagnostics);
    g_free (diagnostics);
    
    if (sigfile) {
        unlink (sigfile);
        g_free (sigfile);
    }
    
    return validity;
    
 exception:
    
    diagnostics = gpg_ctx_get_diagnostics (gpg);
    
    gpg_ctx_free (gpg);
        
    if (sigfile) {
        unlink (sigfile);
        g_free (sigfile);
    }
    
    return NULL;
}


static int
gpg_encrypt (CamelCipherContext *context, gboolean sign, const char *userid,
         GPtrArray *recipients, CamelStream *istream, CamelStream *ostream,
         CamelException *ex)
{
    CamelGpgContext *ctx = (CamelGpgContext *) context;
    struct _GpgCtx *gpg;
    int i;
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_ENCRYPT);
    gpg_ctx_set_armor (gpg, TRUE);
    gpg_ctx_set_userid (gpg, userid);
    gpg_ctx_set_istream (gpg, istream);
    gpg_ctx_set_ostream (gpg, ostream);
    gpg_ctx_set_always_trust (gpg, ctx->always_trust);
    
    for (i = 0; i < recipients->len; i++) {
        gpg_ctx_add_recipient (gpg, recipients->pdata[i]);
    }
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Failed to execute gpg."));
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    while (!gpg_ctx_op_complete (gpg)) {
        if (camel_operation_cancel_check (NULL)) {
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                         _("Cancelled."));
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
        
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
    }
    
    if (gpg_ctx_op_wait (gpg) != 0) {
        char *diagnostics;
        
        diagnostics = gpg_ctx_get_diagnostics (gpg);
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     diagnostics && *diagnostics ? diagnostics :
                     _("Failed to execute gpg."));
        g_free (diagnostics);
        
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    gpg_ctx_free (gpg);
    
    return 0;
}


static int
gpg_decrypt (CamelCipherContext *context, CamelStream *istream,
         CamelStream *ostream, CamelException *ex)
{
    struct _GpgCtx *gpg;
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_DECRYPT);
    gpg_ctx_set_istream (gpg, istream);
    gpg_ctx_set_ostream (gpg, ostream);
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Failed to execute gpg."));
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    while (!gpg_ctx_op_complete (gpg)) {
        if (camel_operation_cancel_check (NULL)) {
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                         _("Cancelled."));
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
        
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
    }
    
    if (gpg_ctx_op_wait (gpg) != 0) {
        char *diagnostics;
        
        diagnostics = gpg_ctx_get_diagnostics (gpg);
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                     diagnostics && *diagnostics ? diagnostics :
                     _("Failed to execute gpg."));
        g_free (diagnostics);
        
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    gpg_ctx_free (gpg);
    
    return 0;
}

static int
gpg_import_keys (CamelCipherContext *context, CamelStream *istream, CamelException *ex)
{
    struct _GpgCtx *gpg;
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_IMPORT);
    gpg_ctx_set_istream (gpg, istream);
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to execute gpg: %s"),
                      errno ? g_strerror (errno) : _("Unknown"));
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    while (!gpg_ctx_op_complete (gpg)) {
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
    }
    
    if (gpg_ctx_op_wait (gpg) != 0) {
        char *diagnostics;
        
        diagnostics = gpg_ctx_get_diagnostics (gpg);
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, diagnostics);
        g_free (diagnostics);
        
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    gpg_ctx_free (gpg);
    
    return 0;
}

static int
gpg_export_keys (CamelCipherContext *context, GPtrArray *keys, CamelStream *ostream, CamelException *ex)
{
    struct _GpgCtx *gpg;
    int i;
    
    gpg = gpg_ctx_new (context->session);
    gpg_ctx_set_mode (gpg, GPG_CTX_MODE_EXPORT);
    gpg_ctx_set_armor (gpg, TRUE);
    gpg_ctx_set_ostream (gpg, ostream);
    
    for (i = 0; i < keys->len; i++) {
        gpg_ctx_add_recipient (gpg, keys->pdata[i]);
    }
    
    if (gpg_ctx_op_start (gpg) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to execute gpg: %s"),
                      errno ? g_strerror (errno) : _("Unknown"));
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    while (!gpg_ctx_op_complete (gpg)) {
        if (gpg_ctx_op_step (gpg, ex) == -1) {
            gpg_ctx_op_cancel (gpg);
            gpg_ctx_free (gpg);
            
            return -1;
        }
    }
    
    if (gpg_ctx_op_wait (gpg) != 0) {
        char *diagnostics;
        
        diagnostics = gpg_ctx_get_diagnostics (gpg);
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, diagnostics);
        g_free (diagnostics);
        
        gpg_ctx_free (gpg);
        
        return -1;
    }
    
    gpg_ctx_free (gpg);
    
    return 0;
}