NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
dlg_pgp.c File Reference

PGP Key Selection Dialog. More...

#include "config.h"
#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "format_flags.h"
#include "keymap.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "opcodes.h"
#include "pgp.h"
#include "pgp_functions.h"
#include "pgpkey.h"
#include "pgplib.h"
+ Include dependency graph for dlg_pgp.c:

Go to the source code of this file.

Data Structures

struct  PgpEntry
 An entry in a PGP key menu. More...
 

Functions

static int pgp_compare_key_address (const void *a, const void *b)
 Compare Key addresses and IDs for sorting. More...
 
static int pgp_compare_address_qsort (const void *a, const void *b)
 Compare the addresses of two PGP keys. More...
 
static int pgp_compare_key_date (const void *a, const void *b)
 Compare Key dates for sorting. More...
 
static int pgp_compare_date_qsort (const void *a, const void *b)
 Compare the dates of two PGP keys. More...
 
static int pgp_compare_keyid (const void *a, const void *b)
 Compare Key IDs and addresses for sorting. More...
 
static int pgp_compare_keyid_qsort (const void *a, const void *b)
 Compare key IDs. More...
 
static int pgp_compare_key_trust (const void *a, const void *b)
 Compare the trust of keys for sorting. More...
 
static int pgp_compare_trust_qsort (const void *a, const void *b)
 Compare the trust levels of two PGP keys. More...
 
static char * pgp_key_abilities (KeyFlags flags)
 Turn PGP key abilities into a string. More...
 
static char pgp_flags (KeyFlags flags)
 Turn PGP key flags into a string. More...
 
static const char * pgp_entry_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Format an entry on the PGP key selection menu - Implements format_t -. More...
 
static void pgp_make_entry (struct Menu *menu, char *buf, size_t buflen, int line)
 Format a menu item for the pgp key list - Implements Menu::make_entry() -. More...
 
static void pgp_key_table_free (struct Menu *menu, void **ptr)
 Free the key table - Implements Menu::mdata_free() -. More...
 
static int pgp_key_config_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has changed - Implements observer_t -. More...
 
static int pgp_key_window_observer (struct NotifyCallback *nc)
 Notification that a Window has changed - Implements observer_t -. More...
 
struct PgpKeyInfodlg_select_pgp_key (struct PgpKeyInfo *keys, struct Address *p, const char *s)
 Let the user select a key to use. More...
 

Variables

static const struct Mapping PgpHelp []
 Help Bar for the PGP key selection dialog. More...
 
static const char trust_flags [] = "?- +"
 

Detailed Description

PGP Key Selection Dialog.

Authors
  • Richard Russon

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, see http://www.gnu.org/licenses/.

Definition in file dlg_pgp.c.

Function Documentation

◆ pgp_compare_key_address()

static int pgp_compare_key_address ( const void *  a,
const void *  b 
)
static

Compare Key addresses and IDs for sorting.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 123 of file dlg_pgp.c.

124{
125 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
126 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
127
128 int r = mutt_istr_cmp((*s)->addr, (*t)->addr);
129 if (r != 0)
130 return (r > 0);
131
132 return mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent)) > 0;
133}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:483
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:234
PGP User ID.
Definition: pgplib.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_address_qsort()

static int pgp_compare_address_qsort ( const void *  a,
const void *  b 
)
static

Compare the addresses of two PGP keys.

Parameters
aFirst address
bSecond address
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 143 of file dlg_pgp.c.

144{
145 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
146 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_address(a, b) :
148}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
static int pgp_compare_key_address(const void *a, const void *b)
Compare Key addresses and IDs for sorting.
Definition: dlg_pgp.c:123
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_key_date()

static int pgp_compare_key_date ( const void *  a,
const void *  b 
)
static

Compare Key dates for sorting.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 158 of file dlg_pgp.c.

159{
160 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
161 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
162
163 int r = ((*s)->parent->gen_time - (*t)->parent->gen_time);
164 if (r != 0)
165 return r > 0;
166 return mutt_istr_cmp((*s)->addr, (*t)->addr) > 0;
167}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_date_qsort()

static int pgp_compare_date_qsort ( const void *  a,
const void *  b 
)
static

Compare the dates of two PGP keys.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 177 of file dlg_pgp.c.

178{
179 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
180 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_date(a, b) :
182}
static int pgp_compare_key_date(const void *a, const void *b)
Compare Key dates for sorting.
Definition: dlg_pgp.c:158
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_keyid()

static int pgp_compare_keyid ( const void *  a,
const void *  b 
)
static

Compare Key IDs and addresses for sorting.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 192 of file dlg_pgp.c.

193{
194 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
195 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
196
197 int r = mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent));
198 if (r != 0)
199 return (r > 0);
200 return mutt_istr_cmp((*s)->addr, (*t)->addr) > 0;
201}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_keyid_qsort()

static int pgp_compare_keyid_qsort ( const void *  a,
const void *  b 
)
static

Compare key IDs.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 211 of file dlg_pgp.c.

212{
213 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
214 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_keyid(a, b) :
215 pgp_compare_keyid(a, b);
216}
static int pgp_compare_keyid(const void *a, const void *b)
Compare Key IDs and addresses for sorting.
Definition: dlg_pgp.c:192
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_key_trust()

static int pgp_compare_key_trust ( const void *  a,
const void *  b 
)
static

Compare the trust of keys for sorting.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Compare two trust values, the key length, the creation dates. the addresses and the key IDs.

Definition at line 229 of file dlg_pgp.c.

230{
231 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
232 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
233
234 int r = (((*s)->parent->flags & KEYFLAG_RESTRICTIONS) -
235 ((*t)->parent->flags & KEYFLAG_RESTRICTIONS));
236 if (r != 0)
237 return r > 0;
238 r = ((*s)->trust - (*t)->trust);
239 if (r != 0)
240 return r < 0;
241 r = ((*s)->parent->keylen - (*t)->parent->keylen);
242 if (r != 0)
243 return r < 0;
244 r = ((*s)->parent->gen_time - (*t)->parent->gen_time);
245 if (r != 0)
246 return r < 0;
247 r = mutt_istr_cmp((*s)->addr, (*t)->addr);
248 if (r != 0)
249 return r > 0;
250 return mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent)) > 0;
251}
#define KEYFLAG_RESTRICTIONS
Definition: lib.h:140
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_trust_qsort()

static int pgp_compare_trust_qsort ( const void *  a,
const void *  b 
)
static

Compare the trust levels of two PGP keys.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 261 of file dlg_pgp.c.

262{
263 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
264 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_trust(a, b) :
266}
static int pgp_compare_key_trust(const void *a, const void *b)
Compare the trust of keys for sorting.
Definition: dlg_pgp.c:229
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_key_abilities()

static char * pgp_key_abilities ( KeyFlags  flags)
static

Turn PGP key abilities into a string.

Parameters
flagsFlags, see KeyFlags
Return values
ptrAbilities string
Note
This returns a pointer to a static buffer

Definition at line 275 of file dlg_pgp.c.

276{
277 static char buf[3];
278
279 if (!(flags & KEYFLAG_CANENCRYPT))
280 buf[0] = '-';
281 else if (flags & KEYFLAG_PREFER_SIGNING)
282 buf[0] = '.';
283 else
284 buf[0] = 'e';
285
286 if (!(flags & KEYFLAG_CANSIGN))
287 buf[1] = '-';
288 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
289 buf[1] = '.';
290 else
291 buf[1] = 's';
292
293 buf[2] = '\0';
294
295 return buf;
296}
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
#define KEYFLAG_PREFER_SIGNING
Key's owner prefers signing.
Definition: lib.h:137
#define KEYFLAG_PREFER_ENCRYPTION
Key's owner prefers encryption.
Definition: lib.h:136
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:127
+ Here is the caller graph for this function:

◆ pgp_flags()

static char pgp_flags ( KeyFlags  flags)
static

Turn PGP key flags into a string.

Parameters
flagsFlags, see KeyFlags
Return values
charFlag character

Definition at line 303 of file dlg_pgp.c.

304{
305 if (flags & KEYFLAG_REVOKED)
306 return 'R';
307 if (flags & KEYFLAG_EXPIRED)
308 return 'X';
309 if (flags & KEYFLAG_DISABLED)
310 return 'd';
311 if (flags & KEYFLAG_CRITICAL)
312 return 'c';
313
314 return ' ';
315}
#define KEYFLAG_EXPIRED
Key is expired.
Definition: lib.h:131
#define KEYFLAG_CRITICAL
Key is marked critical.
Definition: lib.h:135
#define KEYFLAG_DISABLED
Key is marked disabled.
Definition: lib.h:133
#define KEYFLAG_REVOKED
Key is revoked.
Definition: lib.h:132
+ Here is the caller graph for this function:

◆ dlg_select_pgp_key()

struct PgpKeyInfo * dlg_select_pgp_key ( struct PgpKeyInfo keys,
struct Address p,
const char *  s 
)

Let the user select a key to use.

Parameters
keysList of PGP keys
pAddress to match
sString to match
Return values
ptrSelected PGP key

Definition at line 586 of file dlg_pgp.c.

588{
589 struct PgpUid **key_table = NULL;
590 struct Menu *menu = NULL;
591 char buf[1024] = { 0 };
592 struct PgpUid *a = NULL;
593 bool unusable = false;
594 int keymax = 0;
595
596 const bool c_pgp_show_unusable = cs_subset_bool(NeoMutt->sub, "pgp_show_unusable");
597 int i = 0;
598 for (struct PgpKeyInfo *kp = keys; kp; kp = kp->next)
599 {
600 if (!c_pgp_show_unusable && (kp->flags & KEYFLAG_CANTUSE))
601 {
602 unusable = true;
603 continue;
604 }
605
606 for (a = kp->address; a; a = a->next)
607 {
608 if (!c_pgp_show_unusable && (a->flags & KEYFLAG_CANTUSE))
609 {
610 unusable = true;
611 continue;
612 }
613
614 if (i == keymax)
615 {
616 keymax += 5;
617 mutt_mem_realloc(&key_table, sizeof(struct PgpUid *) * keymax);
618 }
619
620 key_table[i++] = a;
621 }
622 }
623
624 if ((i == 0) && unusable)
625 {
626 mutt_error(_("All matching keys are expired, revoked, or disabled"));
627 return NULL;
628 }
629
630 int (*f)(const void *, const void *);
631 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
632 switch (c_pgp_sort_keys & SORT_MASK)
633 {
634 case SORT_ADDRESS:
636 break;
637 case SORT_DATE:
639 break;
640 case SORT_KEYID:
642 break;
643 case SORT_TRUST:
644 default:
646 break;
647 }
648 qsort(key_table, i, sizeof(struct PgpUid *), f);
649
651
652 menu = dlg->wdata;
653 menu->max = i;
655 menu->mdata = key_table;
657
658 struct PgpData pd = { false, menu, key_table, NULL };
659 dlg->wdata = &pd;
660
661 // NT_COLOR is handled by the SimpleDialog
664
665 if (p)
666 snprintf(buf, sizeof(buf), _("PGP keys matching <%s>"), p->mailbox);
667 else
668 snprintf(buf, sizeof(buf), _("PGP keys matching \"%s\""), s);
669
670 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
671 sbar_set_title(sbar, buf);
672
674
675 // ---------------------------------------------------------------------------
676 // Event Loop
677 int op = OP_NULL;
678 do
679 {
680 menu_tagging_dispatcher(menu->win, op);
681 window_redraw(NULL);
682
683 op = km_dokey(MENU_PGP);
684 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
685 if (op < 0)
686 continue;
687 if (op == OP_NULL)
688 {
690 continue;
691 }
693
694 int rc = pgp_function_dispatcher(dlg, op);
695
696 if (rc == FR_UNKNOWN)
697 rc = menu_function_dispatcher(menu->win, op);
698 if (rc == FR_UNKNOWN)
699 rc = global_function_dispatcher(NULL, op);
700 } while (!pd.done);
701 // ---------------------------------------------------------------------------
702
703 simple_dialog_free(&dlg);
704 return pd.key;
705}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static int pgp_compare_address_qsort(const void *a, const void *b)
Compare the addresses of two PGP keys.
Definition: dlg_pgp.c:143
static int pgp_compare_keyid_qsort(const void *a, const void *b)
Compare key IDs.
Definition: dlg_pgp.c:211
static int pgp_compare_date_qsort(const void *a, const void *b)
Compare the dates of two PGP keys.
Definition: dlg_pgp.c:177
static int pgp_compare_trust_qsort(const void *a, const void *b)
Compare the trust levels of two PGP keys.
Definition: dlg_pgp.c:261
static const struct Mapping PgpHelp[]
Help Bar for the PGP key selection dialog.
Definition: dlg_pgp.c:94
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int pgp_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pgp function - Implements function_dispatcher_t -.
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:320
#define mutt_error(...)
Definition: logging.h:87
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static void pgp_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the pgp key list - Implements Menu::make_entry() -.
Definition: dlg_pgp.c:500
static void pgp_key_table_free(struct Menu *menu, void **ptr)
Free the key table - Implements Menu::mdata_free() -.
Definition: dlg_pgp.c:519
static int pgp_key_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pgp.c:556
static int pgp_key_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pgp.c:527
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
int km_dokey(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:795
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1061
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define _(a)
Definition: message.h:28
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:604
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:522
@ WT_DLG_PGP
Pgp Dialog, dlg_select_pgp_key()
Definition: mutt_window.h:88
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
#define KEYFLAG_CANTUSE
Definition: lib.h:139
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:46
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
@ SORT_TRUST
Sort by encryption key's trust level.
Definition: sort2.h:56
@ SORT_KEYID
Sort by the encryption key's ID.
Definition: sort2.h:55
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:43
@ SORT_ADDRESS
Sort by email address.
Definition: sort2.h:54
char * mailbox
Mailbox and host address.
Definition: address.h:38
Definition: lib.h:69
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:76
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:96
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:71
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Data to pass to the Pgp Functions.
Definition: pgp_functions.h:34
struct PgpUid ** key_table
Array of Keys.
Definition: pgp_functions.h:37
struct Menu * menu
Pgp Menu.
Definition: pgp_functions.h:36
bool done
Should we close the Dialog?
Definition: pgp_functions.h:35
struct PgpKeyInfo * key
Selected Key.
Definition: pgp_functions.h:38
Information about a PGP key.
Definition: pgplib.h:47
struct PgpKeyInfo * next
Definition: pgplib.h:57
int flags
Definition: pgplib.h:38
struct PgpUid * next
Linked list.
Definition: pgplib.h:40
@ MENU_PGP
PGP encryption menu.
Definition: type.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ PgpHelp

const struct Mapping PgpHelp[]
static
Initial value:
= {
{ N_("Exit"), OP_EXIT },
{ N_("Select"), OP_GENERIC_SELECT_ENTRY },
{ N_("Check key"), OP_VERIFY_KEY },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
}
#define N_(a)
Definition: message.h:32

Help Bar for the PGP key selection dialog.

Definition at line 94 of file dlg_pgp.c.

◆ trust_flags

const char trust_flags[] = "?- +"
static

Definition at line 113 of file dlg_pgp.c.