forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlg_smime.c
260 lines (237 loc) · 7.93 KB
/
dlg_smime.c
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
/**
* @file
* SMIME Key Selection Dialog
*
* @authors
* Copyright (C) 2020 Richard Russon <[email protected]>
*
* @copyright
* 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, see <http://www.gnu.org/licenses/>.
*/
/**
* @page crypt_dlg_smime SMIME Key Selection Dialog
*
* The SMIME Key Selection Dialog lets the user select a SMIME key.
*
* This is a @ref gui_simple
*
* ## Windows
*
* | Name | Type | See Also |
* | :------------------------- | :----------- | :--------------------- |
* | SMIME Key Selection Dialog | WT_DLG_SMIME | dlg_select_smime_key() |
*
* **Parent**
* - @ref gui_dialog
*
* **Children**
* - See: @ref gui_simple
*
* ## Data
* - #Menu
* - #Menu::mdata
* - #SmimeKey
*
* The @ref gui_simple holds a Menu. The SMIME Key Selection Dialog stores its
* data (#SmimeKey) in Menu::mdata.
*
* ## Events
*
* None. The dialog is not affected by any config or colours and doesn't
* support sorting. Once constructed, the events are handled by the Menu (part
* of the @ref gui_simple).
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "keymap.h"
#include "mutt_logging.h"
#include "opcodes.h"
#include "smime.h"
#include "smime_functions.h"
/// Help Bar for the Smime key selection dialog
static const struct Mapping SmimeHelp[] = {
// clang-format off
{ N_("Exit"), OP_EXIT },
{ N_("Select"), OP_GENERIC_SELECT_ENTRY },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
// clang-format on
};
/**
* smime_key_flags - Turn SMIME key flags into a string
* @param flags Flags, see #KeyFlags
* @retval ptr Flag string
*
* @note The string is statically allocated
*/
static char *smime_key_flags(KeyFlags flags)
{
static char buf[3];
if (!(flags & KEYFLAG_CANENCRYPT))
buf[0] = '-';
else
buf[0] = 'e';
if (!(flags & KEYFLAG_CANSIGN))
buf[1] = '-';
else
buf[1] = 's';
buf[2] = '\0';
return buf;
}
/**
* smime_make_entry - Format a menu item for the smime key list - Implements Menu::make_entry() - @ingroup menu_make_entry
*/
static void smime_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
{
struct SmimeKey **table = menu->mdata;
struct SmimeKey *key = table[line];
char *truststate = NULL;
switch (key->trust)
{
case 'e':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Expired ");
break;
case 'i':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Invalid ");
break;
case 'r':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Revoked ");
break;
case 't':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Trusted ");
break;
case 'u':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Unverified");
break;
case 'v':
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Verified ");
break;
default:
/* L10N: Describes the trust state of a S/MIME key.
This translation must be padded with spaces to the right such that it
has the same length as the other translations.
The translation strings which need to be padded are:
Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
truststate = _("Unknown ");
}
snprintf(buf, buflen, " 0x%s %s %s %-35.35s %s", key->hash,
smime_key_flags(key->flags), truststate, key->email, key->label);
}
/**
* smime_key_table_free - Free the key table - Implements Menu::mdata_free() - @ingroup menu_mdata_free
*
* @note The keys are owned by the caller of the dialog
*/
static void smime_key_table_free(struct Menu *menu, void **ptr)
{
FREE(ptr);
}
/**
* dlg_select_smime_key - Get the user to select a key
* @param keys List of keys to select from
* @param query String to match
* @retval ptr Key selected by user
*/
struct SmimeKey *dlg_select_smime_key(struct SmimeKey *keys, const char *query)
{
struct SmimeKey **table = NULL;
int table_size = 0;
int table_index = 0;
struct SmimeKey *key = NULL;
for (table_index = 0, key = keys; key; key = key->next)
{
if (table_index == table_size)
{
table_size += 5;
mutt_mem_realloc(&table, sizeof(struct SmimeKey *) * table_size);
}
table[table_index++] = key;
}
struct MuttWindow *dlg = simple_dialog_new(MENU_SMIME, WT_DLG_SMIME, SmimeHelp);
struct Menu *menu = dlg->wdata;
menu->max = table_index;
menu->make_entry = smime_make_entry;
menu->mdata = table;
menu->mdata_free = smime_key_table_free;
/* sorting keys might be done later - TODO */
struct SmimeData sd = { false, menu, table, NULL };
dlg->wdata = &sd;
char title[256] = { 0 };
struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
snprintf(title, sizeof(title), _("S/MIME certificates matching \"%s\""), query);
sbar_set_title(sbar, title);
mutt_clear_error();
// ---------------------------------------------------------------------------
// Event Loop
int op = OP_NULL;
do
{
menu_tagging_dispatcher(menu->win, op);
window_redraw(NULL);
op = km_dokey(MENU_SMIME);
mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
if (op < 0)
continue;
if (op == OP_NULL)
{
km_error_key(MENU_SMIME);
continue;
}
mutt_clear_error();
int rc = smime_function_dispatcher(dlg, op);
if (rc == FR_UNKNOWN)
rc = menu_function_dispatcher(menu->win, op);
if (rc == FR_UNKNOWN)
rc = global_function_dispatcher(NULL, op);
} while (!sd.done);
simple_dialog_free(&dlg);
return sd.key;
}