NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_functions.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdio.h>
31#include <sys/types.h>
32#include <unistd.h>
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "core/lib.h"
36#include "gui/lib.h"
37#include "pgp_functions.h"
38#include "lib.h"
39#include "menu/lib.h"
40#include "pager/lib.h"
41#include "question/lib.h"
42#include "globals.h"
43#include "mutt_logging.h"
44#include "pgp.h"
45#include "pgpinvoke.h"
46#include "pgpkey.h"
47#include "pgplib.h"
48
52static int op_exit(struct PgpData *pd, int op)
53{
54 pd->done = true;
55 return FR_SUCCESS;
56}
57
61static int op_generic_select_entry(struct PgpData *pd, int op)
62{
63 /* XXX make error reporting more verbose */
64
65 const int index = menu_get_index(pd->menu);
66 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
67 if (!pkey)
68 return FR_ERROR;
69
71 {
72 if (!pgp_key_is_valid((*pkey)->parent))
73 {
74 mutt_error(_("This key can't be used: expired/disabled/revoked"));
75 return FR_ERROR;
76 }
77 }
78
79 if (OptPgpCheckTrust && (!pgp_id_is_valid((*pkey)) || !pgp_id_is_strong((*pkey))))
80 {
81 const char *str = "";
82 char buf2[1024] = { 0 };
83
84 if ((*pkey)->flags & KEYFLAG_CANTUSE)
85 {
86 str = _("ID is expired/disabled/revoked. Do you really want to use the key?");
87 }
88 else
89 {
90 switch ((*pkey)->trust & 0x03)
91 {
92 case 0:
93 str = _("ID has undefined validity. Do you really want to use the key?");
94 break;
95 case 1:
96 str = _("ID is not valid. Do you really want to use the key?");
97 break;
98 case 2:
99 str = _("ID is only marginally valid. Do you really want to use the key?");
100 break;
101 }
102 }
103
104 snprintf(buf2, sizeof(buf2), "%s", str);
105
106 if (query_yesorno(buf2, MUTT_NO) != MUTT_YES)
107 {
109 return FR_NO_ACTION;
110 }
111 }
112
113 pd->key = (*pkey)->parent;
114 pd->done = true;
115 return FR_SUCCESS;
116}
117
121static int op_verify_key(struct PgpData *pd, int op)
122{
123 FILE *fp_null = mutt_file_fopen("/dev/null", "w");
124 if (!fp_null)
125 {
126 mutt_perror(_("Can't open /dev/null"));
127 return FR_ERROR;
128 }
129 struct Buffer *tempfile = NULL;
130 tempfile = buf_pool_get();
131 buf_mktemp(tempfile);
132 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "w");
133 if (!fp_tmp)
134 {
135 mutt_perror(_("Can't create temporary file"));
136 mutt_file_fclose(&fp_null);
137 buf_pool_release(&tempfile);
138 return FR_ERROR;
139 }
140
141 mutt_message(_("Invoking PGP..."));
142
143 const int index = menu_get_index(pd->menu);
144 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
145 if (!pkey)
146 {
147 mutt_file_fclose(&fp_tmp);
148 mutt_file_fclose(&fp_null);
149 return FR_ERROR;
150 }
151
152 char tmpbuf[256] = { 0 };
153 snprintf(tmpbuf, sizeof(tmpbuf), "0x%s",
154 pgp_fpr_or_lkeyid(pgp_principal_key((*pkey)->parent)));
155
156 pid_t pid = pgp_invoke_verify_key(NULL, NULL, NULL, -1, fileno(fp_tmp),
157 fileno(fp_null), tmpbuf);
158 if (pid == -1)
159 {
160 mutt_perror(_("Can't create filter"));
161 unlink(buf_string(tempfile));
162 mutt_file_fclose(&fp_tmp);
163 mutt_file_fclose(&fp_null);
164 }
165
166 filter_wait(pid);
167 mutt_file_fclose(&fp_tmp);
168 mutt_file_fclose(&fp_null);
170 char title[1024] = { 0 };
171 snprintf(title, sizeof(title), _("Key ID: 0x%s"),
172 pgp_keyid(pgp_principal_key((*pkey)->parent)));
173
174 struct PagerData pdata = { 0 };
175 struct PagerView pview = { &pdata };
176
177 pdata.fname = buf_string(tempfile);
178
179 pview.banner = title;
181 pview.mode = PAGER_MODE_OTHER;
182
183 mutt_do_pager(&pview, NULL);
184 buf_pool_release(&tempfile);
186 return FR_SUCCESS;
187}
188
192static int op_view_id(struct PgpData *pd, int op)
193{
194 const int index = menu_get_index(pd->menu);
195 struct PgpUid **pkey = ARRAY_GET(pd->key_table, index);
196 if (!pkey)
197 return FR_ERROR;
198
199 mutt_message("%s", NONULL((*pkey)->addr));
200 return FR_SUCCESS;
201}
202
203// -----------------------------------------------------------------------------
204
208static const struct PgpFunction PgpFunctions[] = {
209 // clang-format off
210 { OP_EXIT, op_exit },
211 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
212 { OP_VERIFY_KEY, op_verify_key },
213 { OP_VIEW_ID, op_view_id },
214 { 0, NULL },
215 // clang-format on
216};
217
222{
223 // The Dispatcher may be called on any Window in the Dialog
224 struct MuttWindow *dlg = dialog_find(win);
225 if (!dlg || !dlg->wdata)
226 return FR_ERROR;
227
228 struct Menu *menu = dlg->wdata;
229 struct PgpData *pd = menu->mdata;
230
231 int rc = FR_UNKNOWN;
232 for (size_t i = 0; PgpFunctions[i].op != OP_NULL; i++)
233 {
234 const struct PgpFunction *fn = &PgpFunctions[i];
235 if (fn->op == op)
236 {
237 rc = fn->function(pd, op);
238 break;
239 }
240 }
241
242 if (rc == FR_UNKNOWN) // Not our function
243 return rc;
244
245 const char *result = dispatcher_get_retval_name(rc);
246 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
247
248 return rc;
249}
#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: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:122
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition: globals.c:70
int pgp_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pgp function - Implements function_dispatcher_t -.
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
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:52
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:61
Convenience wrapper for the gui headers.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
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:184
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
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.
#define KEYFLAG_CANTUSE
Definition: lib.h:145
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_NO_FLAGS
No flags are set.
Definition: lib.h:60
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:142
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.
static const 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:398
Wrapper around calls to external PGP program.
bool pgp_id_is_valid(struct PgpUid *uid)
Is a PGP key valid.
Definition: pgpkey.c:149
bool pgp_id_is_strong(struct PgpUid *uid)
Is a PGP key strong?
Definition: pgpkey.c:136
bool pgp_key_is_valid(struct PgpKeyInfo *k)
Is a PGP key valid?
Definition: pgpkey.c:104
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:92
PGP key management routines.
Misc PGP helper routines.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
@ 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:327
Key value store.
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
Definition: lib.h:79
void * mdata
Private data.
Definition: lib.h:147
void * wdata
Private data.
Definition: mutt_window.h:145
Data to be displayed by PagerView.
Definition: lib.h:161
const char * fname
Name of the file to read.
Definition: lib.h:165
Paged view into some data.
Definition: lib.h:172
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:173
enum PagerMode mode
Pager mode.
Definition: lib.h:174
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:175
const char * banner
Title to display in status bar.
Definition: lib.h:176
Data to pass to the Pgp Functions.
Definition: pgp_functions.h:34
struct Menu * menu
Pgp Menu.
Definition: pgp_functions.h:36
bool done
Should we close the Dialog?
Definition: pgp_functions.h:35
struct PgpUidArray * key_table
Array of Keys.
Definition: pgp_functions.h:37
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
struct PgpKeyInfo * parent
Definition: pgplib.h:58
PGP User ID.
Definition: pgplib.h:36
#define buf_mktemp(buf)
Definition: tmp.h:33