2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (c) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
11 #include <lib-lib/lib-lib.h>
13 #include <lib-mime/mime.h>
14 #include <lib-sys/unix.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
20 #include "recvattach.h"
28 struct pgp_cache *next;
31 static struct pgp_cache *id_defaults = NULL;
33 static char trust_flags[] = "?- +";
35 static char *pgp_key_abilities (int flags)
39 if (!(flags & KEYFLAG_CANENCRYPT))
41 else if (flags & KEYFLAG_PREFER_SIGNING)
46 if (!(flags & KEYFLAG_CANSIGN))
48 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
58 static char pgp_flags (int flags)
60 if (flags & KEYFLAG_REVOKED)
62 else if (flags & KEYFLAG_EXPIRED)
64 else if (flags & KEYFLAG_DISABLED)
66 else if (flags & KEYFLAG_CRITICAL)
72 static pgp_key_t pgp_principal_key (pgp_key_t key)
74 if (key->flags & KEYFLAG_SUBKEY && key->parent)
81 * Format an entry on the PGP key selection menu.
84 * %k key id %K key id of the principal key
86 * %a algorithm %A algorithm of the princ. key
87 * %l length %L length of the princ. key
88 * %f flags %F flags of the princ. key
89 * %c capabilities %C capabilities of the princ. key
90 * %t trust/validity of the key-uid association
91 * %[...] date of key using strftime(3)
94 typedef struct pgp_entry {
100 pgp_entry_fmt (char *dest, ssize_t destlen, char op,
101 const char *src, const char *prefix,
102 const char *ifstring, const char *elsestring,
103 unsigned long data, format_flag flags)
110 int optional = (flags & M_FORMAT_OPTIONAL);
112 entry = (pgp_entry_t *) data;
115 pkey = pgp_principal_key (key);
117 if (isupper ((unsigned char) op))
120 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
123 switch (ascii_tolower (op)) {
128 char buf2[STRING], *p;
144 while (len > 0 && *cp != ']') {
153 break; /* not enough space */
163 if (do_locales && Locale)
164 setlocale (LC_TIME, Locale);
166 tm = localtime (&key->gen_time);
168 strftime (buf2, sizeof (buf2), dest, tm);
171 setlocale (LC_TIME, "C");
173 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
174 snprintf (dest, destlen, fmt, buf2);
181 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
182 snprintf (dest, destlen, fmt, entry->num);
187 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
188 snprintf (dest, destlen, fmt, _pgp_keyid (key));
193 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
194 snprintf (dest, destlen, fmt, uid->addr);
199 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
200 snprintf (dest, destlen, fmt, key->algorithm);
205 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
206 snprintf (dest, destlen, fmt, key->keylen);
211 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
212 snprintf (dest, destlen, fmt, pgp_flags (kflags));
214 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
219 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
220 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
222 else if (!(kflags & (KEYFLAG_ABILITIES)))
227 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
228 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
230 else if (!(uid->trust & 0x03))
231 /* undefined trust */
239 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
240 else if (flags & M_FORMAT_OPTIONAL)
241 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
245 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
247 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
250 entry.uid = KeyTable[num];
253 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
254 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
257 static int _pgp_compare_address (const void *a, const void *b)
261 pgp_uid_t **s = (pgp_uid_t **) a;
262 pgp_uid_t **t = (pgp_uid_t **) b;
264 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
267 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
268 _pgp_keyid ((*t)->parent)) > 0);
271 static int pgp_compare_address (const void *a, const void *b)
273 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
274 : _pgp_compare_address (a, b));
279 static int _pgp_compare_keyid (const void *a, const void *b)
283 pgp_uid_t **s = (pgp_uid_t **) a;
284 pgp_uid_t **t = (pgp_uid_t **) b;
286 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
287 _pgp_keyid ((*t)->parent))))
290 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
293 static int pgp_compare_keyid (const void *a, const void *b)
295 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
296 : _pgp_compare_keyid (a, b));
299 static int _pgp_compare_date (const void *a, const void *b)
302 pgp_uid_t **s = (pgp_uid_t **) a;
303 pgp_uid_t **t = (pgp_uid_t **) b;
305 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
307 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
310 static int pgp_compare_date (const void *a, const void *b)
312 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
313 : _pgp_compare_date (a, b));
316 static int _pgp_compare_trust (const void *a, const void *b)
320 pgp_uid_t **s = (pgp_uid_t **) a;
321 pgp_uid_t **t = (pgp_uid_t **) b;
323 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
324 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
326 if ((r = ((*s)->trust - (*t)->trust)))
328 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
330 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
332 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
334 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
335 _pgp_keyid ((*t)->parent))) > 0;
338 static int pgp_compare_trust (const void *a, const void *b)
340 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
341 : _pgp_compare_trust (a, b));
344 static int pgp_key_is_valid (pgp_key_t k)
346 pgp_key_t pk = pgp_principal_key (k);
348 if (k->flags & KEYFLAG_CANTUSE)
350 if (pk->flags & KEYFLAG_CANTUSE)
356 static int pgp_id_is_strong (pgp_uid_t * uid)
358 if ((uid->trust & 3) < 3)
364 static int pgp_id_is_valid (pgp_uid_t * uid)
366 if (!pgp_key_is_valid (uid->parent))
368 if (uid->flags & KEYFLAG_CANTUSE)
374 #define PGP_KV_VALID 1
375 #define PGP_KV_ADDR 2
376 #define PGP_KV_STRING 4
377 #define PGP_KV_STRONGID 8
379 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
381 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
386 if (pgp_id_is_valid (uid))
389 if (pgp_id_is_strong (uid))
390 rv |= PGP_KV_STRONGID;
392 if (addr->mailbox && u_addr->mailbox
393 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
396 if (addr->personal && u_addr->personal
397 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
403 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
406 pgp_uid_t **KeyTable;
409 char helpstr[STRING], buf[LONG_STRING], tmpbuf[STRING];
410 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
415 int (*f) (const void *, const void *);
422 for (i = 0, kp = keys; kp; kp = kp->next) {
423 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
428 for (a = kp->address; a; a = a->next) {
429 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
436 p_realloc(&KeyTable, keymax);
443 if (!i && unusable) {
444 mutt_error _("All matching keys are expired, revoked, or disabled.");
450 switch (PgpSortKeys & SORT_MASK) {
452 f = pgp_compare_date;
455 f = pgp_compare_keyid;
458 f = pgp_compare_address;
462 f = pgp_compare_trust;
465 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
468 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
469 m_strcat(helpstr, sizeof(helpstr), buf);
470 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
471 OP_GENERIC_SELECT_ENTRY);
472 m_strcat(helpstr, sizeof(helpstr), buf);
473 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
475 m_strcat(helpstr, sizeof(helpstr), buf);
476 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
477 m_strcat(helpstr, sizeof(helpstr), buf);
479 menu = mutt_new_menu ();
481 menu->make_entry = pgp_entry;
482 menu->menu = MENU_PGP;
483 menu->help = helpstr;
484 menu->data = KeyTable;
487 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
489 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
499 switch (mutt_menuLoop (menu)) {
503 if ((devnull = fopen("/dev/null", "w")) == NULL) {
504 mutt_perror (_("Can't open /dev/null"));
509 fp = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
512 mutt_perror (_("Can't create temporary file"));
517 mutt_message _("Invoking PGP...");
519 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
520 pgp_keyid (pgp_principal_key
521 (KeyTable[menu->current]->parent)));
523 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
524 fileno (fp), fileno (devnull),
526 mutt_perror (_("Can't create filter"));
533 mutt_wait_filter (thepid);
537 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
538 pgp_keyid (pgp_principal_key
539 (KeyTable[menu->current]->parent)));
540 mutt_do_pager (cmd, tempfile, 0, NULL);
541 menu->redraw = REDRAW_FULL;
547 mutt_message ("%s", KeyTable[menu->current]->addr);
550 case OP_GENERIC_SELECT_ENTRY:
553 /* XXX make error reporting more verbose */
555 if (option (OPTPGPCHECKTRUST))
556 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
557 mutt_error _("This key can't be used: expired/disabled/revoked.");
562 if (option (OPTPGPCHECKTRUST) &&
563 (!pgp_id_is_valid (KeyTable[menu->current])
564 || !pgp_id_is_strong (KeyTable[menu->current]))) {
566 char buff[LONG_STRING];
568 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
569 q = N_("ID is expired/disabled/revoked.");
571 switch (KeyTable[menu->current]->trust & 0x03) {
573 q = N_("ID has undefined validity.");
576 q = N_("ID is not valid.");
579 q = N_("ID is only marginally valid.");
583 snprintf (buff, sizeof (buff),
584 _("%s Do you really want to use the key?"), _(q));
586 if (mutt_yesorno (buff, M_NO) != M_YES) {
593 kp = pgp_principal_key (KeyTable[menu->current]->parent);
595 kp = KeyTable[menu->current]->parent;
608 mutt_menuDestroy (&menu);
611 set_option (OPTNEEDREDRAW);
616 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
617 short abilities, pgp_ring_t keyring)
621 struct pgp_cache *l = NULL;
628 for (l = id_defaults; l; l = l->next)
629 if (!m_strcasecmp(whatfor, l->what)) {
630 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
638 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
643 m_strreplace(&l->dflt, resp);
645 l = p_new(struct pgp_cache, 1);
646 l->next = id_defaults;
648 l->what = m_strdup(whatfor);
649 l->dflt = m_strdup(resp);
653 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
661 /* generate a public key attachment */
663 BODY *pgp_make_key_attachment (char *tempf)
666 char buff[LONG_STRING];
667 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
674 unset_option (OPTPGPCHECKTRUST);
677 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
682 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
686 tempfp = m_tempfile (tempfb, sizeof(tempfb), NONULL(Tempdir), NULL);
689 tempfp = safe_fopen(tempf, "a");
693 mutt_perror (_("Can't create temporary file"));
697 if ((devnull = fopen("/dev/null", "w")) == NULL) {
698 mutt_perror (_("Can't open /dev/null"));
706 mutt_message _("Invoking pgp...");
710 pgp_invoke_export (NULL, NULL, NULL, -1,
711 fileno (tempfp), fileno (devnull), tmp)) == -1) {
712 mutt_perror (_("Can't create filter"));
720 mutt_wait_filter (thepid);
726 att->filename = m_strdup(tempf);
729 att->type = TYPEAPPLICATION;
730 att->subtype = m_strdup("pgp-keys");
731 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
732 att->description = m_strdup(buff);
733 mutt_update_encoding (att);
736 att->length = sb.st_size;
741 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
746 if ((scratch = m_strdup(str)) == NULL)
749 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
750 t = strtok (NULL, " ,.:\"()<>\n")) {
752 hints = mutt_add_list (hints, t);
759 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
761 for (; p; p = p->next)
768 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
771 string_list_t *hints = NULL;
776 int this_key_has_strong;
777 int this_key_has_weak;
778 int this_key_has_invalid;
781 pgp_key_t keys, k, kn;
782 pgp_key_t the_valid_key = NULL;
783 pgp_key_t matches = NULL;
784 pgp_key_t *last = &matches;
788 hints = pgp_add_string_to_hints (hints, a->mailbox);
789 if (a && a->personal)
790 hints = pgp_add_string_to_hints (hints, a->personal);
792 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
793 keys = pgp_get_candidates (keyring, hints);
795 string_list_wipe(&hints);
800 for (k = keys; k; k = kn) {
803 if (abilities && !(k->flags & abilities)) {
807 this_key_has_weak = 0; /* weak but valid match */
808 this_key_has_invalid = 0; /* invalid match */
809 this_key_has_strong = 0; /* strong and valid match */
810 match = 0; /* any match */
812 for (q = k->address; q; q = q->next) {
813 r = rfc822_parse_adrlist (NULL, q->addr);
815 for (p = r; p; p = p->next) {
816 int validity = pgp_id_matches_addr (a, p, q);
818 if (validity & PGP_KV_MATCH) /* something matches */
821 /* is this key a strong candidate? */
822 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
823 && (validity & PGP_KV_ADDR)) {
824 if (the_valid_key && the_valid_key != k)
827 this_key_has_strong = 1;
829 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
830 this_key_has_invalid = 1;
831 else if ((validity & PGP_KV_MATCH)
832 && (!(validity & PGP_KV_STRONGID)
833 || !(validity & PGP_KV_ADDR)))
834 this_key_has_weak = 1;
837 address_list_wipe(&r);
840 if (match && !this_key_has_strong && this_key_has_invalid)
842 if (match && !this_key_has_strong && this_key_has_weak)
846 *last = pgp_principal_key (k);
847 kn = pgp_remove_key (&keys, *last);
848 last = pgp_get_lastp (k);
852 pgp_free_key (&keys);
855 if (the_valid_key && !multi /* && !weak
856 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
858 * There was precisely one strong match on a valid ID.
860 * Proceed without asking the user.
862 pgp_remove_key (&matches, the_valid_key);
863 pgp_free_key (&matches);
868 * Else: Ask the user.
870 if ((k = pgp_select_key (matches, a, NULL)))
871 pgp_remove_key (&matches, k);
872 pgp_free_key (&matches);
881 pgp_key_t pgp_getkeybystr (const char *p, short abilities, pgp_ring_t keyring)
883 string_list_t *hints = NULL;
885 pgp_key_t matches = NULL;
886 pgp_key_t *last = &matches;
891 mutt_message (_("Looking for keys matching \"%s\"..."), p);
893 hints = pgp_add_string_to_hints (hints, p);
894 keys = pgp_get_candidates (keyring, hints);
895 string_list_wipe(&hints);
901 for (k = keys; k; k = kn) {
903 if (abilities && !(k->flags & abilities))
908 for (a = k->address; a; a = a->next) {
909 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
910 || (!m_strncasecmp(p, "0x", 2)
911 && !m_strcasecmp(p + 2, pgp_keyid (k)))
912 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
913 && !m_strcasecmp(p + 2, k->keyid + 8))
914 || m_stristr(a->addr, p)) {
921 *last = pgp_principal_key (k);
922 kn = pgp_remove_key (&keys, *last);
923 last = pgp_get_lastp (k);
927 pgp_free_key (&keys);
930 if ((k = pgp_select_key (matches, NULL, p)))
931 pgp_remove_key (&matches, k);
933 pgp_free_key (&matches);