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 *ifstr, const char *elstr,
103 anytype data, format_flag flags)
110 int optional = (flags & M_FORMAT_OPTIONAL);
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 */
238 if (flags & M_FORMAT_OPTIONAL)
239 m_strformat(dest, destlen, 0, optional ? ifstr : elstr,
240 mutt_attach_fmt, data, 0);
244 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
246 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
249 entry.uid = KeyTable[num];
252 m_strformat(s, l, COLS - SW, PgpEntryFormat, pgp_entry_fmt, &entry,
253 option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
256 static int _pgp_compare_address (const void *a, const void *b)
260 pgp_uid_t **s = (pgp_uid_t **) a;
261 pgp_uid_t **t = (pgp_uid_t **) b;
263 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
266 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
267 _pgp_keyid ((*t)->parent)) > 0);
270 static int pgp_compare_address (const void *a, const void *b)
272 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
273 : _pgp_compare_address (a, b));
278 static int _pgp_compare_keyid (const void *a, const void *b)
282 pgp_uid_t **s = (pgp_uid_t **) a;
283 pgp_uid_t **t = (pgp_uid_t **) b;
285 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
286 _pgp_keyid ((*t)->parent))))
289 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
292 static int pgp_compare_keyid (const void *a, const void *b)
294 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
295 : _pgp_compare_keyid (a, b));
298 static int _pgp_compare_date (const void *a, const void *b)
301 pgp_uid_t **s = (pgp_uid_t **) a;
302 pgp_uid_t **t = (pgp_uid_t **) b;
304 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
306 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
309 static int pgp_compare_date (const void *a, const void *b)
311 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
312 : _pgp_compare_date (a, b));
315 static int _pgp_compare_trust (const void *a, const void *b)
319 pgp_uid_t **s = (pgp_uid_t **) a;
320 pgp_uid_t **t = (pgp_uid_t **) b;
322 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
323 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
325 if ((r = ((*s)->trust - (*t)->trust)))
327 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
329 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
331 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
333 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
334 _pgp_keyid ((*t)->parent))) > 0;
337 static int pgp_compare_trust (const void *a, const void *b)
339 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
340 : _pgp_compare_trust (a, b));
343 static int pgp_key_is_valid (pgp_key_t k)
345 pgp_key_t pk = pgp_principal_key (k);
347 if (k->flags & KEYFLAG_CANTUSE)
349 if (pk->flags & KEYFLAG_CANTUSE)
355 static int pgp_id_is_strong (pgp_uid_t * uid)
357 if ((uid->trust & 3) < 3)
363 static int pgp_id_is_valid (pgp_uid_t * uid)
365 if (!pgp_key_is_valid (uid->parent))
367 if (uid->flags & KEYFLAG_CANTUSE)
373 #define PGP_KV_VALID 1
374 #define PGP_KV_ADDR 2
375 #define PGP_KV_STRING 4
376 #define PGP_KV_STRONGID 8
378 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
380 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
385 if (pgp_id_is_valid (uid))
388 if (pgp_id_is_strong (uid))
389 rv |= PGP_KV_STRONGID;
391 if (addr->mailbox && u_addr->mailbox
392 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
395 if (addr->personal && u_addr->personal
396 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
402 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
405 pgp_uid_t **KeyTable;
408 char helpstr[STRING], buf[LONG_STRING], tmpbuf[STRING];
409 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
414 int (*f) (const void *, const void *);
421 for (i = 0, kp = keys; kp; kp = kp->next) {
422 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
427 for (a = kp->address; a; a = a->next) {
428 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
435 p_realloc(&KeyTable, keymax);
442 if (!i && unusable) {
443 mutt_error _("All matching keys are expired, revoked, or disabled.");
449 switch (PgpSortKeys & SORT_MASK) {
451 f = pgp_compare_date;
454 f = pgp_compare_keyid;
457 f = pgp_compare_address;
461 f = pgp_compare_trust;
464 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
467 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
468 m_strcat(helpstr, sizeof(helpstr), buf);
469 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
470 OP_GENERIC_SELECT_ENTRY);
471 m_strcat(helpstr, sizeof(helpstr), buf);
472 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
474 m_strcat(helpstr, sizeof(helpstr), buf);
475 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
476 m_strcat(helpstr, sizeof(helpstr), buf);
478 menu = mutt_new_menu ();
480 menu->make_entry = pgp_entry;
481 menu->menu = MENU_PGP;
482 menu->help = helpstr;
483 menu->data = KeyTable;
486 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
488 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
498 switch (mutt_menuLoop (menu)) {
502 if ((devnull = fopen("/dev/null", "w")) == NULL) {
503 mutt_perror (_("Can't open /dev/null"));
508 fp = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
511 mutt_perror (_("Can't create temporary file"));
516 mutt_message _("Invoking PGP...");
518 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
519 pgp_keyid (pgp_principal_key
520 (KeyTable[menu->current]->parent)));
522 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
523 fileno (fp), fileno (devnull),
525 mutt_perror (_("Can't create filter"));
532 mutt_wait_filter (thepid);
536 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
537 pgp_keyid (pgp_principal_key
538 (KeyTable[menu->current]->parent)));
539 mutt_do_pager (cmd, tempfile, 0, NULL);
540 menu->redraw = REDRAW_FULL;
546 mutt_message ("%s", KeyTable[menu->current]->addr);
549 case OP_GENERIC_SELECT_ENTRY:
552 /* XXX make error reporting more verbose */
554 if (option (OPTPGPCHECKTRUST))
555 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
556 mutt_error _("This key can't be used: expired/disabled/revoked.");
561 if (option (OPTPGPCHECKTRUST) &&
562 (!pgp_id_is_valid (KeyTable[menu->current])
563 || !pgp_id_is_strong (KeyTable[menu->current]))) {
565 char buff[LONG_STRING];
567 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
568 q = N_("ID is expired/disabled/revoked.");
570 switch (KeyTable[menu->current]->trust & 0x03) {
572 q = N_("ID has undefined validity.");
575 q = N_("ID is not valid.");
578 q = N_("ID is only marginally valid.");
582 snprintf (buff, sizeof (buff),
583 _("%s Do you really want to use the key?"), _(q));
585 if (mutt_yesorno (buff, M_NO) != M_YES) {
592 kp = pgp_principal_key (KeyTable[menu->current]->parent);
594 kp = KeyTable[menu->current]->parent;
607 mutt_menuDestroy (&menu);
610 set_option (OPTNEEDREDRAW);
615 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
616 short abilities, pgp_ring_t keyring)
620 struct pgp_cache *l = NULL;
627 for (l = id_defaults; l; l = l->next)
628 if (!m_strcasecmp(whatfor, l->what)) {
629 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
637 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
642 m_strreplace(&l->dflt, resp);
644 l = p_new(struct pgp_cache, 1);
645 l->next = id_defaults;
647 l->what = m_strdup(whatfor);
648 l->dflt = m_strdup(resp);
652 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
660 /* generate a public key attachment */
662 BODY *pgp_make_key_attachment (char *tempf)
665 char buff[LONG_STRING];
666 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
673 unset_option (OPTPGPCHECKTRUST);
676 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
681 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
685 tempfp = m_tempfile (tempfb, sizeof(tempfb), NONULL(Tempdir), NULL);
688 tempfp = safe_fopen(tempf, "a");
692 mutt_perror (_("Can't create temporary file"));
696 if ((devnull = fopen("/dev/null", "w")) == NULL) {
697 mutt_perror (_("Can't open /dev/null"));
705 mutt_message _("Invoking pgp...");
709 pgp_invoke_export (NULL, NULL, NULL, -1,
710 fileno (tempfp), fileno (devnull), tmp)) == -1) {
711 mutt_perror (_("Can't create filter"));
719 mutt_wait_filter (thepid);
725 att->filename = m_strdup(tempf);
728 att->type = TYPEAPPLICATION;
729 att->subtype = m_strdup("pgp-keys");
730 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
731 att->description = m_strdup(buff);
732 mutt_update_encoding (att);
735 att->length = sb.st_size;
740 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
745 if ((scratch = m_strdup(str)) == NULL)
748 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
749 t = strtok (NULL, " ,.:\"()<>\n")) {
751 hints = mutt_add_list (hints, t);
758 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
760 for (; p; p = p->next)
767 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
770 string_list_t *hints = NULL;
775 int this_key_has_strong;
776 int this_key_has_weak;
777 int this_key_has_invalid;
780 pgp_key_t keys, k, kn;
781 pgp_key_t the_valid_key = NULL;
782 pgp_key_t matches = NULL;
783 pgp_key_t *last = &matches;
787 hints = pgp_add_string_to_hints (hints, a->mailbox);
788 if (a && a->personal)
789 hints = pgp_add_string_to_hints (hints, a->personal);
791 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
792 keys = pgp_get_candidates (keyring, hints);
794 string_list_wipe(&hints);
799 for (k = keys; k; k = kn) {
802 if (abilities && !(k->flags & abilities)) {
806 this_key_has_weak = 0; /* weak but valid match */
807 this_key_has_invalid = 0; /* invalid match */
808 this_key_has_strong = 0; /* strong and valid match */
809 match = 0; /* any match */
811 for (q = k->address; q; q = q->next) {
812 r = rfc822_parse_adrlist (NULL, q->addr);
814 for (p = r; p; p = p->next) {
815 int validity = pgp_id_matches_addr (a, p, q);
817 if (validity & PGP_KV_MATCH) /* something matches */
820 /* is this key a strong candidate? */
821 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
822 && (validity & PGP_KV_ADDR)) {
823 if (the_valid_key && the_valid_key != k)
826 this_key_has_strong = 1;
828 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
829 this_key_has_invalid = 1;
830 else if ((validity & PGP_KV_MATCH)
831 && (!(validity & PGP_KV_STRONGID)
832 || !(validity & PGP_KV_ADDR)))
833 this_key_has_weak = 1;
836 address_list_wipe(&r);
839 if (match && !this_key_has_strong && this_key_has_invalid)
841 if (match && !this_key_has_strong && this_key_has_weak)
845 *last = pgp_principal_key (k);
846 kn = pgp_remove_key (&keys, *last);
847 last = pgp_get_lastp (k);
851 pgp_free_key (&keys);
854 if (the_valid_key && !multi /* && !weak
855 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
857 * There was precisely one strong match on a valid ID.
859 * Proceed without asking the user.
861 pgp_remove_key (&matches, the_valid_key);
862 pgp_free_key (&matches);
867 * Else: Ask the user.
869 if ((k = pgp_select_key (matches, a, NULL)))
870 pgp_remove_key (&matches, k);
871 pgp_free_key (&matches);
880 pgp_key_t pgp_getkeybystr (const char *p, short abilities, pgp_ring_t keyring)
882 string_list_t *hints = NULL;
884 pgp_key_t matches = NULL;
885 pgp_key_t *last = &matches;
890 mutt_message (_("Looking for keys matching \"%s\"..."), p);
892 hints = pgp_add_string_to_hints (hints, p);
893 keys = pgp_get_candidates (keyring, hints);
894 string_list_wipe(&hints);
900 for (k = keys; k; k = kn) {
902 if (abilities && !(k->flags & abilities))
907 for (a = k->address; a; a = a->next) {
908 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
909 || (!m_strncasecmp(p, "0x", 2)
910 && !m_strcasecmp(p + 2, pgp_keyid (k)))
911 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
912 && !m_strcasecmp(p + 2, k->keyid + 8))
913 || m_stristr(a->addr, p)) {
920 *last = pgp_principal_key (k);
921 kn = pgp_remove_key (&keys, *last);
922 last = pgp_get_lastp (k);
926 pgp_free_key (&keys);
929 if ((k = pgp_select_key (matches, NULL, p)))
930 pgp_remove_key (&matches, k);
932 pgp_free_key (&matches);