NeoMutt  2025-12-11-949-g4870ee
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions_pgp.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include <sys/types.h>
33#include <unistd.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "gui/lib.h"
38#include "lib.h"
39#include "key/lib.h"
40#include "menu/lib.h"
41#include "pager/lib.h"
42#include "question/lib.h"
43#include "globals.h"
44#include "mutt_logging.h"
45#include "pgp.h"
46#include "pgp_functions.h"
47#include "pgpinvoke.h"
48#include "pgpkey.h"
49#include "pgplib.h"
50
54static int op_quit(struct PgpData *pd, const struct KeyEvent *event)
55{
56 pd->done = true;
57 return FR_SUCCESS;
58}
59
63static int op_generic_select_entry(struct PgpData *pd, const struct KeyEvent *event)
64{
65 /* XXX make error reporting more verbose */
66
67 const int index = menu_get_index(pd->menu);
68 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
69 if (!pkey)
70 return FR_ERROR;
71
73 {
74 if (!pgp_key_is_valid((*pkey)->parent))
75 {
76 mutt_error(_("This key can't be used: expired/disabled/revoked"));
77 return FR_ERROR;
78 }
79 }
80
81 if (OptPgpCheckTrust && (!pgp_id_is_valid((*pkey)) || !pgp_id_is_strong((*pkey))))
82 {
83 const char *str = "";
84 char buf2[1024] = { 0 };
85
86 if ((*pkey)->flags & KEYFLAG_CANTUSE)
87 {
88 str = _("ID is expired/disabled/revoked. Do you really want to use the key?");
89 }
90 else
91 {
92 switch ((*pkey)->trust & 0x03)
93 {
94 case 0:
95 str = _("ID has undefined validity. Do you really want to use the key?");
96 break;
97 case 1:
98 str = _("ID is not valid. Do you really want to use the key?");
99 break;
100 case 2:
101 str = _("ID is only marginally valid. Do you really want to use the key?");
102 break;
103 }
104 }
105
106 snprintf(buf2, sizeof(buf2), "%s", str);
107
108 if (query_yesorno(buf2, MUTT_NO) != MUTT_YES)
109 {
111 return FR_NO_ACTION;
112 }
113 }
114
115 pd->key = (*pkey)->parent;
116 pd->done = true;
117 return FR_SUCCESS;
118}
119
123static int op_verify_key(struct PgpData *pd, const struct KeyEvent *event)
124{
125 FILE *fp_null = mutt_file_fopen("/dev/null", "w");
126 if (!fp_null)
127 {
128 mutt_perror(_("Can't open /dev/null"));
129 return FR_ERROR;
130 }
131 struct Buffer *tempfile = NULL;
132 tempfile = buf_pool_get();
133 buf_mktemp(tempfile);
134 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
135 if (!fp_tmp)
136 {
137 mutt_perror(_("Can't create temporary file"));
138 mutt_file_fclose(&fp_null);
139 buf_pool_release(&tempfile);
140 return FR_ERROR;
141 }
142
143 mutt_message(_("Invoking PGP..."));
144
145 const int index = menu_get_index(pd->menu);
146 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
147 if (!pkey)
148 {
149 mutt_file_fclose(&fp_tmp);
150 mutt_file_fclose(&fp_null);
151 buf_pool_release(&tempfile);
152 return FR_ERROR;
153 }
154
155 char tmpbuf[256] = { 0 };
156 snprintf(tmpbuf, sizeof(tmpbuf), "0x%s",
157 pgp_fpr_or_lkeyid(pgp_principal_key((*pkey)->parent)));
158
159 pid_t pid = pgp_invoke_verify_key(NULL, NULL, NULL, -1, fileno(fp_tmp),
160 fileno(fp_null), tmpbuf);
161 if (pid == -1)
162 {
163 mutt_perror(_("Can't create filter"));
164 unlink(buf_string(tempfile));
165 mutt_file_fclose(&fp_tmp);
166 mutt_file_fclose(&fp_null);
167 buf_pool_release(&tempfile);
168 return FR_ERROR;
169 }
170
171 filter_wait(pid);
172 mutt_file_fclose(&fp_tmp);
173 mutt_file_fclose(&fp_null);
175 char title[1024] = { 0 };
176 snprintf(title, sizeof(title), _("Key ID: 0x%s"),
177 pgp_keyid(pgp_principal_key((*pkey)->parent)));
178
179 struct PagerData pdata = { 0 };
180 struct PagerView pview = { &pdata };
181
182 pdata.fname = buf_string(tempfile);
183
184 pview.banner = title;
185 pview.flags = MUTT_PAGER_NONE;
186 pview.mode = PAGER_MODE_OTHER;
187
188 mutt_do_pager(&pview, NULL);
189 buf_pool_release(&tempfile);
191 return FR_SUCCESS;
192}
193
197static int op_view_id(struct PgpData *pd, const struct KeyEvent *event)
198{
199 const int index = menu_get_index(pd->menu);
200 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
201 if (!pkey)
202 return FR_ERROR;
203
204 mutt_message("%s", NONULL((*pkey)->addr));
205 return FR_SUCCESS;
206}
207
208// -----------------------------------------------------------------------------
209
213static const struct PgpFunction PgpFunctions[] = {
214 // clang-format off
215 { OP_EXIT, op_quit },
216 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
217 { OP_QUIT, op_quit },
218 { OP_VERIFY_KEY, op_verify_key },
219 { OP_VIEW_ID, op_view_id },
220 { 0, NULL },
221 // clang-format on
222};
223
227int pgp_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
228{
229 // The Dispatcher may be called on any Window in the Dialog
230 struct MuttWindow *dlg = dialog_find(win);
231 if (!event || !dlg || !dlg->wdata)
232 {
234 return FR_ERROR;
235 }
236
237 const int op = event->op;
238 struct Menu *menu = dlg->wdata;
239 struct PgpData *pd = menu->mdata;
240 if (!pd)
241 {
243 return FR_ERROR;
244 }
245
246 int rc = FR_UNKNOWN;
247 for (size_t i = 0; PgpFunctions[i].op != OP_NULL; i++)
248 {
249 const struct PgpFunction *fn = &PgpFunctions[i];
250 if (fn->op == op)
251 {
252 rc = fn->function(pd, event);
253 break;
254 }
255 }
256
257 if (rc == FR_UNKNOWN) // Not our function
258 return rc;
259
260 const char *result = dispatcher_get_retval_name(rc);
261 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
262
264 return rc;
265}
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
struct MuttWindow * dialog_find(struct MuttWindow *win)
Find the parent Dialog of a Window.
Definition dialog.c:89
const char * dispatcher_get_retval_name(int rv)
Get the name of a return value.
Definition dispatcher.c:55
void dispatcher_flush_on_error(int rv)
Flush pending keys after a dispatch error.
Definition dispatcher.c:65
@ FR_SUCCESS
Valid function - successfully performed.
Definition dispatcher.h:40
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
@ FR_ERROR
Valid function - error occurred.
Definition dispatcher.h:39
@ FR_NO_ACTION
Valid function - no action performed.
Definition dispatcher.h:38
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition do_pager.c:122
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
static const struct PgpFunction PgpFunctions[]
All the NeoMutt functions that the Pgp supports.
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition globals.c:55
Global variables.
static int op_quit(struct AliasFunctionData *fdata, const struct KeyEvent *event)
Save changes and exit this dialog - Implements alias_function_t -.
Definition functions.c:308
static int op_generic_select_entry(struct AliasFunctionData *fdata, const struct KeyEvent *event)
select the current entry - Implements alias_function_t -
Definition functions.c:388
int pgp_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Pgp function - Implements function_dispatcher_t -.
static int op_verify_key(struct GpgmeData *gd, const struct KeyEvent *event)
Verify a PGP public key - Implements gpgme_function_t -.
static int op_view_id(struct GpgmeData *gd, const struct KeyEvent *event)
View the key's user id - Implements gpgme_function_t -.
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
#define mutt_perror(...)
Definition logging2.h:95
static int op_verify_key(struct PgpData *pd, const struct KeyEvent *event)
Verify a PGP public key - Implements pgp_function_t -.
static int op_generic_select_entry(struct PgpData *pd, const struct KeyEvent *event)
Select the current entry - Implements pgp_function_t -.
static int op_quit(struct PgpData *pd, const struct KeyEvent *event)
Save changes and exit this dialog - Implements pgp_function_t -.
static int op_view_id(struct PgpData *pd, const struct KeyEvent *event)
View the key's user id - Implements pgp_function_t -.
Convenience wrapper for the gui headers.
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
GUI present the user with a selectable list.
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition menu.c:177
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition menu.c:153
@ MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:64
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:228
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
NeoMutt Logging.
API for encryption/signing of emails.
#define KEYFLAG_CANTUSE
Definition lib.h:161
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
GUI display a file/email/help in a viewport with paging.
#define MUTT_PAGER_NONE
No flags are set.
Definition lib.h:63
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:143
char * pgp_keyid(struct PgpKeyInfo *k)
Get the ID of the main (parent) key.
Definition pgp.c:201
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition pgp.c:231
PGP sign, encrypt, check routines.
Pgp functions.
pid_t pgp_invoke_verify_key(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
Use PGP to verify a key.
Definition pgpinvoke.c:397
Wrapper around calls to external PGP program.
bool pgp_id_is_valid(struct PgpUid *uid)
Is a PGP key valid.
Definition pgpkey.c:152
bool pgp_id_is_strong(struct PgpUid *uid)
Is a PGP key strong?
Definition pgpkey.c:139
bool pgp_key_is_valid(struct PgpKeyInfo *k)
Is a PGP key valid?
Definition pgpkey.c:107
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition pgpkey.c:95
PGP key management routines.
Misc PGP helper routines.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
Ask the user a question.
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
#define NONULL(x)
Definition string2.h:44
String manipulation buffer.
Definition buffer.h:36
An event such as a keypress.
Definition get.h:75
Definition lib.h:86
void * mdata
Private data.
Definition lib.h:155
void * wdata
Private data.
Data to be displayed by PagerView.
Definition lib.h:162
const char * fname
Name of the file to read.
Definition lib.h:166
Paged view into some data.
Definition lib.h:173
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition lib.h:174
enum PagerMode mode
Pager mode.
Definition lib.h:175
PagerFlags flags
Additional settings to tweak pager's function.
Definition lib.h:176
const char * banner
Title to display in status bar.
Definition lib.h:177
Data to pass to the Pgp Functions.
struct Menu * menu
Pgp Menu.
bool done
Should we close the Dialog?
struct PgpUidArray * key_table
Array of Keys.
struct PgpKeyInfo * key
Selected Key.
A NeoMutt function.
int op
Op code, e.g. OP_GENERIC_SELECT_ENTRY.
pgp_function_t function
Function to call.
struct PgpKeyInfo * parent
Parent key.
Definition pgplib.h:58
PGP User ID.
Definition pgplib.h:36
#define buf_mktemp(buf)
Definition tmp.h:33