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
|
/*
* Copyright (C) 1996-1997,2007 Michael R. Elkins <me@mutt.org>
* Copyright (c) 1998-2003 Thomas Roessler <roessler@does-not-exist.org>
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "mutt.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mime.h"
#include "pgp.h"
#include "pager.h"
#include "sort.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <locale.h>
#ifdef CRYPT_BACKEND_CLASSIC_PGP
struct pgp_cache
{
char *what;
char *dflt;
struct pgp_cache *next;
};
static struct pgp_cache *id_defaults = NULL;
static const char trust_flags[] = "?- +";
static char *pgp_key_abilities (int flags)
{
static char buff[3];
if (!(flags & KEYFLAG_CANENCRYPT))
buff[0] = '-';
else if (flags & KEYFLAG_PREFER_SIGNING)
buff[0] = '.';
else
buff[0] = 'e';
if (!(flags & KEYFLAG_CANSIGN))
buff[1] = '-';
else if (flags & KEYFLAG_PREFER_ENCRYPTION)
buff[1] = '.';
else
buff[1] = 's';
buff[2] = '\0';
return buff;
}
static char pgp_flags (int flags)
{
if (flags & KEYFLAG_REVOKED)
return 'R';
else if (flags & KEYFLAG_EXPIRED)
return 'X';
else if (flags & KEYFLAG_DISABLED)
return 'd';
else if (flags & KEYFLAG_CRITICAL)
return 'c';
else
return ' ';
}
static pgp_key_t pgp_principal_key (pgp_key_t key)
{
if (key->flags & KEYFLAG_SUBKEY && key->parent)
return key->parent;
else
return key;
}
/*
* Format an entry on the PGP key selection menu.
*
* %n number
* %k key id %K key id of the principal key
* %u user id
* %a algorithm %A algorithm of the princ. key
* %l length %L length of the princ. key
* %f flags %F flags of the princ. key
* %c capabilities %C capabilities of the princ. key
* %t trust/validity of the key-uid association
* %[...] date of key using strftime(3)
*/
typedef struct pgp_entry
{
size_t num;
pgp_uid_t *uid;
} pgp_entry_t;
static const char *pgp_entry_fmt (char *dest,
size_t destlen,
size_t col,
int cols,
char op,
const char *src,
const char *prefix,
const char *ifstring,
const char *elsestring,
void *data,
format_flag flags)
{
char fmt[16];
pgp_entry_t *entry;
pgp_uid_t *uid;
pgp_key_t key, pkey;
int kflags = 0;
int optional = (flags & MUTT_FORMAT_OPTIONAL);
entry = (pgp_entry_t *) data;
uid = entry->uid;
key = uid->parent;
pkey = pgp_principal_key (key);
if (isupper ((unsigned char) op))
key = pkey;
kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
| uid->flags;
switch (ascii_tolower (op))
{
case '[':
{
const char *cp;
char buf2[SHORT_STRING], *p;
int do_locales;
struct tm *tm;
size_t len;
p = dest;
cp = src;
if (*cp == '!')
{
do_locales = 0;
cp++;
}
else
do_locales = 1;
len = destlen - 1;
while (len > 0 && *cp != ']')
{
if (*cp == '%')
{
cp++;
if (len >= 2)
{
*p++ = '%';
*p++ = *cp;
len -= 2;
}
else
break; /* not enough space */
cp++;
}
else
{
*p++ = *cp++;
len--;
}
}
*p = 0;
tm = localtime (&key->gen_time);
if (!do_locales)
setlocale (LC_TIME, "C");
strftime (buf2, sizeof (buf2), dest, tm);
if (!do_locales)
setlocale (LC_TIME, "");
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, buf2);
if (len > 0)
src = cp + 1;
}
break;
case 'n':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
snprintf (dest, destlen, fmt, entry->num);
}
break;
case 'k':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, _pgp_keyid (key));
}
break;
case 'u':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, NONULL (uid->addr));
}
break;
case 'a':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, key->algorithm);
}
break;
case 'l':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
snprintf (dest, destlen, fmt, key->keylen);
}
break;
case 'f':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
snprintf (dest, destlen, fmt, pgp_flags (kflags));
}
else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
optional = 0;
break;
case 'c':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
}
else if (!(kflags & (KEYFLAG_ABILITIES)))
optional = 0;
break;
case 't':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
}
else if (!(uid->trust & 0x03))
/* undefined trust */
optional = 0;
break;
default:
*dest = '\0';
}
if (optional)
mutt_FormatString (dest, destlen, col, cols, ifstring, pgp_entry_fmt, data, 0);
else if (flags & MUTT_FORMAT_OPTIONAL)
mutt_FormatString (dest, destlen, col, cols, elsestring, pgp_entry_fmt, data, 0);
return (src);
}
static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
{
pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
pgp_entry_t entry;
entry.uid = KeyTable[num];
entry.num = num + 1;
mutt_FormatString (s, l, 0, MuttIndexWindow->cols, NONULL (PgpEntryFormat), pgp_entry_fmt,
&entry, MUTT_FORMAT_ARROWCURSOR);
}
static int _pgp_compare_address (const void *a, const void *b)
{
int r;
pgp_uid_t **s = (pgp_uid_t **) a;
pgp_uid_t **t = (pgp_uid_t **) b;
if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
return r;
else
return mutt_strcasecmp (pgp_fpr_or_lkeyid ((*s)->parent),
pgp_fpr_or_lkeyid ((*t)->parent));
}
static int pgp_compare_address (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
: _pgp_compare_address (a, b));
}
static int _pgp_compare_keyid (const void *a, const void *b)
{
int r;
pgp_uid_t **s = (pgp_uid_t **) a;
pgp_uid_t **t = (pgp_uid_t **) b;
if ((r = mutt_strcasecmp (pgp_fpr_or_lkeyid ((*s)->parent),
pgp_fpr_or_lkeyid ((*t)->parent))))
return r;
else
return (mutt_strcasecmp ((*s)->addr, (*t)->addr));
}
static int pgp_compare_keyid (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
: _pgp_compare_keyid (a, b));
}
static int _pgp_compare_date (const void *a, const void *b)
{
int r;
pgp_uid_t **s = (pgp_uid_t **) a;
pgp_uid_t **t = (pgp_uid_t **) b;
if ((r = mutt_numeric_cmp ((*s)->parent->gen_time, (*t)->parent->gen_time)))
return r;
return (mutt_strcasecmp ((*s)->addr, (*t)->addr));
}
static int pgp_compare_date (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
: _pgp_compare_date (a, b));
}
static int _pgp_compare_trust (const void *a, const void *b)
{
int r;
pgp_uid_t **s = (pgp_uid_t **) a;
pgp_uid_t **t = (pgp_uid_t **) b;
if ((r = mutt_numeric_cmp (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS)),
((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
return r;
/* Note: reversed */
if ((r = mutt_numeric_cmp ((*t)->trust, (*s)->trust)))
return r;
/* Note: reversed */
if ((r = mutt_numeric_cmp ((*t)->parent->keylen, (*s)->parent->keylen)))
return r;
/* Note: reversed */
if ((r = mutt_numeric_cmp ((*t)->parent->gen_time, (*s)->parent->gen_time)))
return r;
if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
return r;
return (mutt_strcasecmp (pgp_fpr_or_lkeyid ((*s)->parent),
pgp_fpr_or_lkeyid ((*t)->parent)));
}
static int pgp_compare_trust (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
: _pgp_compare_trust (a, b));
}
static int pgp_key_is_valid (pgp_key_t k)
{
pgp_key_t pk = pgp_principal_key (k);
if (k->flags & KEYFLAG_CANTUSE)
return 0;
if (pk->flags & KEYFLAG_CANTUSE)
return 0;
return 1;
}
static int pgp_id_is_strong (pgp_uid_t *uid)
{
if ((uid->trust & 3) < 3)
return 0;
/* else */
return 1;
}
static int pgp_id_is_valid (pgp_uid_t *uid)
{
if (!pgp_key_is_valid (uid->parent))
return 0;
if (uid->flags & KEYFLAG_CANTUSE)
return 0;
/* else */
return 1;
}
#define PGP_KV_VALID 1
#define PGP_KV_ADDR 2
#define PGP_KV_STRING 4
#define PGP_KV_STRONGID 8
#define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
static int pgp_id_matches_addr (ADDRESS *addr, ADDRESS *u_addr, pgp_uid_t *uid)
{
int rv = 0;
if (pgp_id_is_valid (uid))
rv |= PGP_KV_VALID;
if (pgp_id_is_strong (uid))
rv |= PGP_KV_STRONGID;
if (addr->mailbox && u_addr->mailbox
&& mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
rv |= PGP_KV_ADDR;
if (addr->personal && u_addr->personal
&& mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
rv |= PGP_KV_STRING;
return rv;
}
static pgp_key_t pgp_select_key (pgp_key_t keys,
ADDRESS * p, const char *s)
{
int keymax;
pgp_uid_t **KeyTable;
MUTTMENU *menu;
int i, done = 0;
char helpstr[LONG_STRING], buf[LONG_STRING], tmpbuf[STRING];
char cmd[LONG_STRING];
BUFFER *tempfile;
FILE *fp, *devnull;
pid_t thepid;
pgp_key_t kp;
pgp_uid_t *a;
int (*f) (const void *, const void *);
int unusable = 0;
keymax = 0;
KeyTable = NULL;
for (i = 0, kp = keys; kp; kp = kp->next)
{
if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE))
{
unusable = 1;
continue;
}
for (a = kp->address; a; a = a->next)
{
if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE))
{
unusable = 1;
continue;
}
if (i == keymax)
{
keymax += 5;
safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
}
KeyTable[i++] = a;
}
}
if (!i && unusable)
{
mutt_error _("All matching keys are expired, revoked, or disabled.");
mutt_sleep (1);
return NULL;
}
switch (PgpSortKeys & SORT_MASK)
{
case SORT_DATE:
f = pgp_compare_date;
break;
case SORT_KEYID:
f = pgp_compare_keyid;
break;
case SORT_ADDRESS:
f = pgp_compare_address;
break;
case SORT_TRUST:
default:
f = pgp_compare_trust;
break;
}
qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
helpstr[0] = 0;
mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
OP_GENERIC_SELECT_ENTRY);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP, OP_VERIFY_KEY);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
menu = mutt_new_menu (MENU_PGP);
menu->max = i;
menu->make_entry = pgp_entry;
menu->help = helpstr;
menu->data = KeyTable;
mutt_push_current_menu (menu);
if (p)
snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
else
snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
menu->title = buf;
kp = NULL;
mutt_clear_error ();
while (!done)
{
switch (mutt_menuLoop (menu))
{
case OP_VERIFY_KEY:
if ((devnull = fopen ("/dev/null", "w")) == NULL) /* __FOPEN_CHECKED__ */
{
mutt_perror _("Can't open /dev/null");
break;
}
tempfile = mutt_buffer_pool_get ();
mutt_buffer_mktemp (tempfile);
if ((fp = safe_fopen (mutt_b2s (tempfile), "w")) == NULL)
{
mutt_perror _("Can't create temporary file");
safe_fclose (&devnull);
mutt_buffer_pool_release (&tempfile);
break;
}
mutt_message _("Invoking PGP...");
snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
pgp_fpr_or_lkeyid (pgp_principal_key (KeyTable[menu->current]->parent)));
if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
fileno (fp), fileno (devnull), tmpbuf)) == -1)
{
mutt_perror _("Can't create filter");
unlink (mutt_b2s (tempfile));
mutt_buffer_pool_release (&tempfile);
safe_fclose (&fp);
safe_fclose (&devnull);
break;
}
mutt_wait_filter (thepid);
safe_fclose (&fp);
safe_fclose (&devnull);
mutt_clear_error ();
snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
pgp_keyid (pgp_principal_key (KeyTable[menu->current]->parent)));
mutt_do_pager (cmd, mutt_b2s (tempfile), 0, NULL);
mutt_buffer_pool_release (&tempfile);
menu->redraw = REDRAW_FULL;
break;
case OP_VIEW_ID:
mutt_message ("%s", NONULL (KeyTable[menu->current]->addr));
break;
case OP_GENERIC_SELECT_ENTRY:
/* XXX make error reporting more verbose */
if (option (OPTPGPCHECKTRUST))
if (!pgp_key_is_valid (KeyTable[menu->current]->parent))
{
mutt_error _("This key can't be used: expired/disabled/revoked.");
break;
}
if (option (OPTPGPCHECKTRUST) &&
(!pgp_id_is_valid (KeyTable[menu->current])
|| !pgp_id_is_strong (KeyTable[menu->current])))
{
char *s = "";
char buff[LONG_STRING];
if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
s = N_("ID is expired/disabled/revoked.");
else switch (KeyTable[menu->current]->trust & 0x03)
{
case 0:
s = N_("ID has undefined validity.");
break;
case 1:
s = N_("ID is not valid.");
break;
case 2:
s = N_("ID is only marginally valid.");
break;
}
snprintf (buff, sizeof (buff), _("%s Do you really want to use the key?"),
_(s));
if (mutt_yesorno (buff, MUTT_NO) != MUTT_YES)
{
mutt_clear_error ();
break;
}
}
# if 0
kp = pgp_principal_key (KeyTable[menu->current]->parent);
# else
kp = KeyTable[menu->current]->parent;
# endif
done = 1;
break;
case OP_EXIT:
kp = NULL;
done = 1;
break;
}
}
mutt_pop_current_menu (menu);
mutt_menuDestroy (&menu);
FREE (&KeyTable);
return (kp);
}
pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
short abilities, pgp_ring_t keyring)
{
pgp_key_t key;
char resp[SHORT_STRING];
struct pgp_cache *l = NULL;
mutt_clear_error ();
resp[0] = 0;
if (whatfor)
{
for (l = id_defaults; l; l = l->next)
if (!mutt_strcasecmp (whatfor, l->what))
{
strfcpy (resp, NONULL (l->dflt), sizeof (resp));
break;
}
}
FOREVER
{
resp[0] = 0;
if (mutt_get_field (tag, resp, sizeof (resp), MUTT_CLEAR) != 0)
return NULL;
if (whatfor)
{
if (l)
mutt_str_replace (&l->dflt, resp);
else
{
l = safe_malloc (sizeof (struct pgp_cache));
l->next = id_defaults;
id_defaults = l;
l->what = safe_strdup (whatfor);
l->dflt = safe_strdup (resp);
}
}
if ((key = pgp_getkeybystr (resp, abilities, keyring)))
return key;
BEEP ();
}
/* not reached */
}
/* generate a public key attachment */
BODY *pgp_make_key_attachment (void)
{
BODY *att = NULL;
char buff[LONG_STRING], tmp[STRING];
BUFFER *tempf = NULL;
FILE *tempfp;
FILE *devnull;
struct stat sb;
pid_t thepid;
pgp_key_t key;
unset_option (OPTPGPCHECKTRUST);
key = pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
if (!key)
return NULL;
snprintf (tmp, sizeof (tmp), "0x%s", pgp_fpr_or_lkeyid (pgp_principal_key (key)));
pgp_free_key (&key);
tempf = mutt_buffer_pool_get ();
mutt_buffer_mktemp (tempf);
if ((tempfp = safe_fopen (mutt_b2s (tempf), "w")) == NULL)
{
mutt_perror _("Can't create temporary file");
goto cleanup;
}
if ((devnull = fopen ("/dev/null", "w")) == NULL) /* __FOPEN_CHECKED__ */
{
mutt_perror _("Can't open /dev/null");
safe_fclose (&tempfp);
unlink (mutt_b2s (tempf));
goto cleanup;
}
mutt_message _("Invoking PGP...");
if ((thepid =
pgp_invoke_export (NULL, NULL, NULL, -1,
fileno (tempfp), fileno (devnull), tmp)) == -1)
{
mutt_perror _("Can't create filter");
safe_fclose (&tempfp);
unlink (mutt_b2s (tempf));
safe_fclose (&devnull);
goto cleanup;
}
mutt_wait_filter (thepid);
safe_fclose (&tempfp);
safe_fclose (&devnull);
att = mutt_new_body ();
att->filename = safe_strdup (mutt_b2s (tempf));
att->unlink = 1;
att->use_disp = 0;
att->type = TYPEAPPLICATION;
att->subtype = safe_strdup ("pgp-keys");
snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
att->description = safe_strdup (buff);
mutt_update_encoding (att);
stat (mutt_b2s (tempf), &sb);
att->length = sb.st_size;
cleanup:
mutt_buffer_pool_release (&tempf);
return att;
}
static LIST *pgp_add_string_to_hints (LIST *hints, const char *str)
{
char *scratch;
char *t;
if ((scratch = safe_strdup (str)) == NULL)
return hints;
for (t = strtok (scratch, " ,.:\"()<>\n"); t;
t = strtok (NULL, " ,.:\"()<>\n"))
{
if (strlen (t) > 3)
hints = mutt_add_list (hints, t);
}
FREE (&scratch);
return hints;
}
static pgp_key_t *pgp_get_lastp (pgp_key_t p)
{
for (; p; p = p->next)
if (!p->next)
return &p->next;
return NULL;
}
pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring,
int oppenc_mode)
{
ADDRESS *r, *p;
LIST *hints = NULL;
int multi = 0;
int match;
pgp_key_t keys, k, kn;
pgp_key_t the_strong_valid_key = NULL;
pgp_key_t a_valid_addrmatch_key = NULL;
pgp_key_t matches = NULL;
pgp_key_t *last = &matches;
pgp_uid_t *q;
if (a && a->mailbox)
hints = mutt_add_list (hints, a->mailbox);
if (a && a->personal)
hints = pgp_add_string_to_hints (hints, a->personal);
if (! oppenc_mode )
mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
keys = pgp_get_candidates (keyring, hints);
mutt_free_list (&hints);
if (!keys)
return NULL;
dprint (5, (debugfile, "pgp_getkeybyaddr: looking for %s <%s>.\n",
NONULL (a->personal), NONULL (a->mailbox)));
for (k = keys; k; k = kn)
{
kn = k->next;
dprint (5, (debugfile, " looking at key: %s\n",
pgp_keyid (k)));
if (abilities && !(k->flags & abilities))
{
dprint (5, (debugfile, " insufficient abilities: Has %x, want %x\n",
k->flags, abilities));
continue;
}
match = 0; /* any match */
for (q = k->address; q; q = q->next)
{
r = rfc822_parse_adrlist (NULL, NONULL (q->addr));
for (p = r; p; p = p->next)
{
int validity = pgp_id_matches_addr (a, p, q);
if (validity & PGP_KV_MATCH) /* something matches */
match = 1;
if ((validity & PGP_KV_VALID)
&& (validity & PGP_KV_ADDR))
{
if (validity & PGP_KV_STRONGID)
{
if (the_strong_valid_key && the_strong_valid_key != k)
multi = 1;
the_strong_valid_key = k;
}
else
{
a_valid_addrmatch_key = k;
}
}
}
rfc822_free_address (&r);
}
if (match)
{
*last = pgp_principal_key (k);
kn = pgp_remove_key (&keys, *last);
last = pgp_get_lastp (k);
}
}
pgp_free_key (&keys);
if (matches)
{
if (oppenc_mode)
{
if (the_strong_valid_key)
{
pgp_remove_key (&matches, the_strong_valid_key);
k = the_strong_valid_key;
}
else if (a_valid_addrmatch_key &&
!option (OPTCRYPTOPPENCSTRONGKEYS))
{
pgp_remove_key (&matches, a_valid_addrmatch_key);
k = a_valid_addrmatch_key;
}
else
k = NULL;
}
else if (the_strong_valid_key && !multi)
{
/*
* There was precisely one strong match on a valid ID.
*
* Proceed without asking the user.
*/
pgp_remove_key (&matches, the_strong_valid_key);
k = the_strong_valid_key;
}
else
{
/*
* Else: Ask the user.
*/
if ((k = pgp_select_key (matches, a, NULL)))
pgp_remove_key (&matches, k);
}
pgp_free_key (&matches);
return k;
}
return NULL;
}
pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
{
LIST *hints = NULL;
pgp_key_t keys;
pgp_key_t matches = NULL;
pgp_key_t *last = &matches;
pgp_key_t k, kn;
pgp_uid_t *a;
short match;
size_t l;
const char *ps, *pl, *pfcopy, *phint;
if ((l = mutt_strlen (p)) && p[l-1] == '!')
p[l-1] = 0;
mutt_message (_("Looking for keys matching \"%s\"..."), p);
pfcopy = crypt_get_fingerprint_or_id (p, &phint, &pl, &ps);
hints = pgp_add_string_to_hints (hints, phint);
keys = pgp_get_candidates (keyring, hints);
mutt_free_list (&hints);
if (!keys)
goto out;
for (k = keys; k; k = kn)
{
kn = k->next;
if (abilities && !(k->flags & abilities))
continue;
/* This shouldn't happen, but keys without any addresses aren't selectable
* in pgp_select_key().
*/
if (!k->address)
continue;
match = 0;
dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s:\n",
p, pgp_long_keyid (k)));
if (!*p ||
(pfcopy && mutt_strcasecmp (pfcopy, k->fingerprint) == 0) ||
(pl && mutt_strcasecmp (pl, pgp_long_keyid (k)) == 0) ||
(ps && mutt_strcasecmp (ps, pgp_short_keyid (k)) == 0))
{
dprint (5, (debugfile, "\t\tmatch.\n"));
match = 1;
}
else
{
for (a = k->address; a; a = a->next)
{
dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\":\n",
p, pgp_long_keyid (k), NONULL (a->addr)));
if (mutt_stristr (a->addr, p))
{
dprint (5, (debugfile, "\t\tmatch.\n"));
match = 1;
break;
}
}
}
if (match)
{
*last = pgp_principal_key (k);
kn = pgp_remove_key (&keys, *last);
last = pgp_get_lastp (k);
}
}
pgp_free_key (&keys);
if (matches)
{
if ((k = pgp_select_key (matches, NULL, p)))
pgp_remove_key (&matches, k);
pgp_free_key (&matches);
FREE (&pfcopy);
if (l && !p[l-1])
p[l-1] = '!';
return k;
}
out:
FREE (&pfcopy);
if (l && !p[l-1])
p[l-1] = '!';
return NULL;
}
#endif /* CRYPT_BACKEND_CLASSIC_PGP */
|