NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
pgp_functions.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdio.h>
31#include <unistd.h>
32#include "mutt/lib.h"
33#include "config/lib.h"
34#include "core/lib.h"
35#include "gui/lib.h"
36#include "pgp_functions.h"
37#include "lib.h"
38#include "menu/lib.h"
39#include "pager/lib.h"
40#include "question/lib.h"
41#include "mutt_logging.h"
42#include "muttlib.h"
43#include "opcodes.h"
44#include "options.h"
45#include "pgp.h"
46#include "pgpinvoke.h"
47#include "pgpkey.h"
48#include "pgplib.h"
49
53static int op_exit(struct PgpData *pd, int op)
54{
55 pd->done = true;
56 return FR_SUCCESS;
57}
58
62static int op_generic_select_entry(struct PgpData *pd, int op)
63{
64 /* XXX make error reporting more verbose */
65
66 const int index = menu_get_index(pd->menu);
67 struct PgpUid *cur_key = pd->key_table[index];
69 {
70 if (!pgp_key_is_valid(cur_key->parent))
71 {
72 mutt_error(_("This key can't be used: expired/disabled/revoked"));
73 return FR_ERROR;
74 }
75 }
76
77 if (OptPgpCheckTrust && (!pgp_id_is_valid(cur_key) || !pgp_id_is_strong(cur_key)))
78 {
79 const char *str = "";
80 char buf2[1024];
81
82 if (cur_key->flags & KEYFLAG_CANTUSE)
83 {
84 str = _("ID is expired/disabled/revoked. Do you really want to use the key?");
85 }
86 else
87 {
88 switch (cur_key->trust & 0x03)
89 {
90 case 0:
91 str = _("ID has undefined validity. Do you really want to use the key?");
92 break;
93 case 1:
94 str = _("ID is not valid. Do you really want to use the key?");
95 break;
96 case 2:
97 str = _("ID is only marginally valid. Do you really want to use the key?");
98 break;
99 }
100 }
101
102 snprintf(buf2, sizeof(buf2), "%s", str);
103
104 if (mutt_yesorno(buf2, MUTT_NO) != MUTT_YES)
105 {
107 return FR_NO_ACTION;
108 }
109 }
110
111 pd->key = cur_key->parent;
112 pd->done = true;
113 return FR_SUCCESS;
114}
115
119static int op_verify_key(struct PgpData *pd, int op)
120{
121 FILE *fp_null = fopen("/dev/null", "w");
122 if (!fp_null)
123 {
124 mutt_perror(_("Can't open /dev/null"));
125 return FR_ERROR;
126 }
127 struct Buffer *tempfile = NULL;
128 tempfile = mutt_buffer_pool_get();
129 mutt_buffer_mktemp(tempfile);
130 FILE *fp_tmp = mutt_file_fopen(mutt_buffer_string(tempfile), "w");
131 if (!fp_tmp)
132 {
133 mutt_perror(_("Can't create temporary file"));
134 mutt_file_fclose(&fp_null);
135 mutt_buffer_pool_release(&tempfile);
136 return FR_ERROR;
137 }
138
139 mutt_message(_("Invoking PGP..."));
140
141 const int index = menu_get_index(pd->menu);
142 struct PgpUid *cur_key = pd->key_table[index];
143 char tmpbuf[256] = { 0 };
144 snprintf(tmpbuf, sizeof(tmpbuf), "0x%s",
146
147 pid_t pid = pgp_invoke_verify_key(NULL, NULL, NULL, -1, fileno(fp_tmp),
148 fileno(fp_null), tmpbuf);
149 if (pid == -1)
150 {
151 mutt_perror(_("Can't create filter"));
152 unlink(mutt_buffer_string(tempfile));
153 mutt_file_fclose(&fp_tmp);
154 mutt_file_fclose(&fp_null);
155 }
156
157 filter_wait(pid);
158 mutt_file_fclose(&fp_tmp);
159 mutt_file_fclose(&fp_null);
161 char title[1024] = { 0 };
162 snprintf(title, sizeof(title), _("Key ID: 0x%s"),
164
165 struct PagerData pdata = { 0 };
166 struct PagerView pview = { &pdata };
167
168 pdata.fname = mutt_buffer_string(tempfile);
169
170 pview.banner = title;
172 pview.mode = PAGER_MODE_OTHER;
173
174 mutt_do_pager(&pview, NULL);
175 mutt_buffer_pool_release(&tempfile);
177 return FR_SUCCESS;
178}
179
183static int op_view_id(struct PgpData *pd, int op)
184{
185 const int index = menu_get_index(pd->menu);
186 struct PgpUid *cur_key = pd->key_table[index];
187 mutt_message("%s", NONULL(cur_key->addr));
188 return FR_SUCCESS;
189}
190
191// -----------------------------------------------------------------------------
192
197 // clang-format off
198 { OP_EXIT, op_exit },
199 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
200 { OP_VERIFY_KEY, op_verify_key },
201 { OP_VIEW_ID, op_view_id },
202 { 0, NULL },
203 // clang-format on
204};
205
210{
211 if (!win || !win->wdata)
212 return FR_UNKNOWN;
213
214 struct MuttWindow *dlg = dialog_find(win);
215 if (!dlg)
216 return FR_ERROR;
217
218 struct PgpData *pd = dlg->wdata;
219
220 int rc = FR_UNKNOWN;
221 for (size_t i = 0; PgpFunctions[i].op != OP_NULL; i++)
222 {
223 const struct PgpFunction *fn = &PgpFunctions[i];
224 if (fn->op == op)
225 {
226 rc = fn->function(pd, op);
227 break;
228 }
229 }
230
231 if (rc == FR_UNKNOWN) // Not our function
232 return rc;
233
234 const char *result = dispacher_get_retval_name(rc);
235 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
236
237 return rc;
238}
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
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:83
const char * dispacher_get_retval_name(int rv)
Get the name of a return value.
Definition: dispatcher.c:54
@ FR_SUCCESS
Valid function - successfully performed.
Definition: dispatcher.h:39
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
@ FR_NO_ACTION
Valid function - no action performed.
Definition: dispatcher.h:37
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:123
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:618
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
int pgp_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pgp function - Implements function_dispatcher_t -.
#define mutt_error(...)
Definition: logging.h:87
#define mutt_message(...)
Definition: logging.h:86
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
#define mutt_perror(...)
Definition: logging.h:88
static int op_verify_key(struct PgpData *pd, int op)
Verify a PGP public key - Implements pgp_function_t -.
static int op_exit(struct PgpData *pd, int op)
Exit this menu - Implements pgp_function_t -.
Definition: pgp_functions.c:53
static int op_view_id(struct PgpData *pd, int op)
View the key's user id - Implements pgp_function_t -.
static int op_generic_select_entry(struct PgpData *pd, int op)
Select the current entry - Implements pgp_function_t -.
Definition: pgp_functions.c:62
Convenience wrapper for the gui headers.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:178
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:154
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)
Definition: mutt_logging.c:74
NeoMutt Logging.
Some miscellaneous functions.
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
#define KEYFLAG_CANTUSE
Definition: lib.h:139
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:46
All user-callable functions.
Handling of global boolean variables.
bool OptPgpCheckTrust
(pseudo) used by dlg_select_pgp_key()
Definition: options.h:54
GUI display a file/email/help in a viewport with paging.
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: lib.h:58
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:139
char * pgp_keyid(struct PgpKeyInfo *k)
Get the ID of the main (parent) key.
Definition: pgp.c:204
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:234
PGP sign, encrypt, check routines.
struct PgpFunction PgpFunctions[]
All the NeoMutt functions that the Pgp supports.
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:518
Wrapper around calls to external PGP program.
bool pgp_id_is_valid(struct PgpUid *uid)
Is a PGP key valid.
Definition: pgpkey.c:130
bool pgp_id_is_strong(struct PgpUid *uid)
Is a PGP key strong?
Definition: pgpkey.c:117
bool pgp_key_is_valid(struct PgpKeyInfo *k)
Is a PGP key valid?
Definition: pgpkey.c:101
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:89
PGP key management routines.
Misc PGP helper routines.
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
@ 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 mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:194
Key value store.
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:34
void * wdata
Private data.
Definition: mutt_window.h:145
Data to be displayed by PagerView.
Definition: lib.h:158
const char * fname
Name of the file to read.
Definition: lib.h:162
Paged view into some data.
Definition: lib.h:169
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:170
enum PagerMode mode
Pager mode.
Definition: lib.h:171
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:172
const char * banner
Title to display in status bar.
Definition: lib.h:173
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
A NeoMutt function.
Definition: pgp_functions.h:57
int op
Op code, e.g. OP_GENERIC_SELECT_ENTRY.
Definition: pgp_functions.h:58
pgp_function_t function
Function to call.
Definition: pgp_functions.h:59
PGP User ID.
Definition: pgplib.h:35
short trust
Definition: pgplib.h:37
struct PgpKeyInfo * parent
Parent key.
Definition: pgplib.h:39
int flags
Definition: pgplib.h:38
char * addr
Definition: pgplib.h:36