NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
autocrypt.c File Reference

Autocrypt end-to-end encryption. More...

#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "browser/lib.h"
#include "index/lib.h"
#include "ncrypt/lib.h"
#include "question/lib.h"
#include "send/lib.h"
#include "globals.h"
#include "muttlib.h"
#include "mx.h"
+ Include dependency graph for autocrypt.c:

Go to the source code of this file.

Functions

static int autocrypt_dir_init (bool can_create)
 Initialise an Autocrypt directory. More...
 
int mutt_autocrypt_init (bool can_create)
 Initialise Autocrypt. More...
 
void mutt_autocrypt_cleanup (void)
 Shutdown Autocrypt. More...
 
int mutt_autocrypt_account_init (bool prompt)
 Create a new Autocrypt account. More...
 
int mutt_autocrypt_process_autocrypt_header (struct Email *e, struct Envelope *env)
 Parse an Autocrypt email header. More...
 
int mutt_autocrypt_process_gossip_header (struct Email *e, struct Envelope *prot_headers)
 Parse an Autocrypt email gossip header. More...
 
enum AutocryptRec mutt_autocrypt_ui_recommendation (const struct Email *e, char **keylist)
 Get the recommended action for an Email. More...
 
int mutt_autocrypt_set_sign_as_default_key (struct Email *e)
 Set the Autocrypt default key for signing. More...
 
static void write_autocrypt_header_line (FILE *fp, const char *addr, bool prefer_encrypt, const char *keydata)
 Write an Autocrypt header to a file. More...
 
int mutt_autocrypt_write_autocrypt_header (struct Envelope *env, FILE *fp)
 Write the Autocrypt header to a file. More...
 
int mutt_autocrypt_write_gossip_headers (struct Envelope *env, FILE *fp)
 Write the Autocrypt gossip headers to a file. More...
 
int mutt_autocrypt_generate_gossip_list (struct Email *e)
 Create the gossip list headers. More...
 
void mutt_autocrypt_scan_mailboxes (void)
 Scan mailboxes for Autocrypt headers. More...
 

Detailed Description

Autocrypt end-to-end encryption.

Authors
  • Kevin J. McCarthy

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 autocrypt.c.

Function Documentation

◆ autocrypt_dir_init()

static int autocrypt_dir_init ( bool  can_create)
static

Initialise an Autocrypt directory.

Parameters
can_createIf true, the directory may be created
Return values
0Success
-1Error

Definition at line 59 of file autocrypt.c.

60{
61 int rc = 0;
62 struct stat st = { 0 };
63
64 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
65 if (stat(c_autocrypt_dir, &st) == 0)
66 return 0;
67
68 if (!can_create)
69 return -1;
70
71 struct Buffer *prompt = buf_pool_get();
72 /* L10N: s is a directory. NeoMutt is looking for a directory it needs
73 for some reason (e.g. autocrypt, header cache, bcache), but it
74 doesn't exist. The prompt is asking whether to create the directory */
75 buf_printf(prompt, _("%s does not exist. Create it?"), c_autocrypt_dir);
77 {
78 if (mutt_file_mkdir(c_autocrypt_dir, S_IRWXU) < 0)
79 {
80 /* L10N: mkdir() on the directory %s failed. The second %s is the
81 error message returned by libc */
82 mutt_error(_("Can't create %s: %s"), c_autocrypt_dir, strerror(errno));
83 rc = -1;
84 }
85 }
86
87 buf_pool_release(&prompt);
88 return rc;
89}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:952
#define mutt_error(...)
Definition: logging2.h:90
#define _(a)
Definition: message.h:28
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:194
String manipulation buffer.
Definition: buffer.h:34
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:

◆ mutt_autocrypt_init()

int mutt_autocrypt_init ( bool  can_create)

Initialise Autocrypt.

Parameters
can_createIf true, directories may be created
Return values
0Success
-1Error

Definition at line 97 of file autocrypt.c.

98{
99 if (AutocryptDB)
100 return 0;
101
102 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
103 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
104 if (!c_autocrypt || !c_autocrypt_dir)
105 return -1;
106
108 /* The init process can display menus at various points
109 *(e.g. browser, pgp key selection). This allows the screen to be
110 * autocleared after each menu, so the subsequent prompts can be
111 * read. */
113
114 if (autocrypt_dir_init(can_create))
115 goto bail;
116
118 goto bail;
119
120 if (mutt_autocrypt_db_init(can_create))
121 goto bail;
122
123 OptIgnoreMacroEvents = false;
124 OptMenuPopClearScreen = false;
125
126 return 0;
127
128bail:
129 OptIgnoreMacroEvents = false;
130 OptMenuPopClearScreen = false;
131 cs_subset_str_native_set(NeoMutt->sub, "autocrypt", false, NULL);
133 return -1;
134}
void mutt_autocrypt_db_close(void)
Close the Autocrypt SQLite database connection.
Definition: db.c:130
sqlite3 * AutocryptDB
Handle to the open Autocrypt database.
Definition: db.c:53
int mutt_autocrypt_db_init(bool can_create)
Initialise the Autocrypt SQLite database.
Definition: db.c:80
static int autocrypt_dir_init(bool can_create)
Initialise an Autocrypt directory.
Definition: autocrypt.c:59
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
bool OptMenuPopClearScreen
(pseudo) clear the screen when popping the last menu
Definition: globals.c:74
bool OptIgnoreMacroEvents
(pseudo) don't process macro/push/exec events while set
Definition: globals.c:72
int mutt_autocrypt_gpgme_init(void)
Initialise GPGME.
Definition: gpgme.c:67
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:310
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_cleanup()

void mutt_autocrypt_cleanup ( void  )

Shutdown Autocrypt.

Definition at line 139 of file autocrypt.c.

140{
142}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_account_init()

int mutt_autocrypt_account_init ( bool  prompt)

Create a new Autocrypt account.

Parameters
promptPrompt the user
Return values
0Success
-1Error

This is used the first time autocrypt is initialized, and in the account menu.

Definition at line 153 of file autocrypt.c.

154{
155 struct Address *addr = NULL;
156 struct AutocryptAccount *account = NULL;
157 bool done = false;
158 int rc = -1;
159 bool prefer_encrypt = false;
160
161 if (prompt)
162 {
163 /* L10N: The first time NeoMutt is started with $autocrypt set, it will
164 create $autocrypt_dir and then prompt to create an autocrypt account
165 with this message. */
166 if (mutt_yesorno(_("Create an initial autocrypt account?"), MUTT_YES) != MUTT_YES)
167 return 0;
168 }
169
170 struct Buffer *keyid = buf_pool_get();
171 struct Buffer *keydata = buf_pool_get();
172
173 const struct Address *c_from = cs_subset_address(NeoMutt->sub, "from");
174 if (c_from)
175 {
176 addr = mutt_addr_copy(c_from);
177 const char *const c_real_name = cs_subset_string(NeoMutt->sub, "real_name");
178 if (!addr->personal && c_real_name)
179 addr->personal = buf_new(c_real_name);
180 }
181
182 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
183 mutt_addrlist_append(&al, addr);
184
185 do
186 {
187 /* L10N: Autocrypt is asking for the email address to use for the
188 autocrypt account. This will generate a key and add a record
189 to the database for use in autocrypt operations. */
190 if (mutt_edit_address(&al, _("Autocrypt account address: "), false) != 0)
191 goto cleanup;
192
193 addr = TAILQ_FIRST(&al);
194 if (!addr || !addr->mailbox || TAILQ_NEXT(addr, entries))
195 {
196 /* L10N: Autocrypt prompts for an account email address, and requires
197 a single address. This is shown if they entered something invalid,
198 nothing, or more than one address for some reason. */
199 mutt_error(_("Please enter a single email address"));
200 done = false;
201 }
202 else
203 {
204 done = true;
205 }
206 } while (!done);
207
208 addr = TAILQ_FIRST(&al);
209 if (mutt_autocrypt_db_account_get(addr, &account) < 0)
210 goto cleanup;
211 if (account)
212 {
213 /* L10N: When creating an autocrypt account, this message will be displayed
214 if there is already an account in the database with the email address
215 they just entered. */
216 mutt_error(_("That email address already has an autocrypt account"));
217 goto cleanup;
218 }
219
220 if (mutt_autocrypt_gpgme_select_or_create_key(addr, keyid, keydata))
221 goto cleanup;
222
223 /* L10N: Autocrypt has a setting "prefer-encrypt".
224 When the recommendation algorithm returns "available" and BOTH sender and
225 recipient choose "prefer-encrypt", encryption will be automatically
226 enabled.
227 Otherwise the UI will show encryption is "available" but the user
228 will be required to enable encryption manually. */
229 if (mutt_yesorno(_("Prefer encryption?"), MUTT_NO) == MUTT_YES)
230 prefer_encrypt = true;
231
232 if (mutt_autocrypt_db_account_insert(addr, buf_string(keyid), buf_string(keydata), prefer_encrypt))
233 {
234 goto cleanup;
235 }
236
237 rc = 0;
238
239cleanup:
240 if (rc == 0)
241 {
242 /* L10N: Message displayed after an autocrypt account is successfully created. */
243 mutt_message(_("Autocrypt account creation succeeded"));
244 }
245 else
246 {
247 /* L10N: Error message displayed if creating an autocrypt account failed
248 or was aborted by the user. */
249 mutt_error(_("Autocrypt account creation aborted"));
250 }
251
254 buf_pool_release(&keyid);
255 buf_pool_release(&keydata);
256 return rc;
257}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1449
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1469
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:730
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:263
int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid, const char *keydata, bool prefer_encrypt)
Insert an Account into the Autocrypt database.
Definition: db.c:325
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:244
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:316
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: helpers.c:49
int mutt_autocrypt_gpgme_select_or_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Ask the user to select or create an Autocrypt key.
Definition: gpgme.c:276
#define mutt_message(...)
Definition: logging2.h:89
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_NEXT(elm, field)
Definition: queue.h:832
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
int mutt_edit_address(struct AddressList *al, const char *field, bool expand_aliases)
Edit an email address.
Definition: send.c:178
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:106
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:110
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_process_autocrypt_header()

int mutt_autocrypt_process_autocrypt_header ( struct Email e,
struct Envelope env 
)

Parse an Autocrypt email header.

Parameters
eEmail
envEnvelope
Return values
0Success
-1Error

Definition at line 266 of file autocrypt.c.

267{
268 struct AutocryptHeader *valid_ac_hdr = NULL;
269 struct AutocryptPeer *peer = NULL;
270 struct AutocryptPeerHistory *peerhist = NULL;
271 struct Buffer *keyid = NULL;
272 bool update_db = false, insert_db = false, insert_db_history = false, import_gpg = false;
273 int rc = -1;
274
275 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
276 if (!c_autocrypt)
277 return 0;
278
279 if (mutt_autocrypt_init(false))
280 return -1;
281
282 if (!e || !e->body || !env)
283 return 0;
284
285 /* 1.1 spec says to skip emails with more than one From header */
286 struct Address *from = TAILQ_FIRST(&env->from);
287 if (!from || TAILQ_NEXT(from, entries))
288 return 0;
289
290 /* 1.1 spec also says to skip multipart/report emails */
291 if ((e->body->type == TYPE_MULTIPART) && mutt_istr_equal(e->body->subtype, "report"))
292 {
293 return 0;
294 }
295
296 /* Ignore emails that appear to be more than a week in the future,
297 * since they can block all future updates during that time. */
298 if (e->date_sent > (mutt_date_now() + (7 * 24 * 60 * 60)))
299 return 0;
300
301 for (struct AutocryptHeader *ac_hdr = env->autocrypt; ac_hdr; ac_hdr = ac_hdr->next)
302 {
303 if (ac_hdr->invalid)
304 continue;
305
306 /* NOTE: this assumes the processing is occurring right after
307 * mutt_parse_rfc822_line() and the from ADDR is still in the same
308 * form (intl) as the autocrypt header addr field */
309 if (!mutt_istr_equal(buf_string(from->mailbox), ac_hdr->addr))
310 continue;
311
312 /* 1.1 spec says ignore all, if more than one valid header is found. */
313 if (valid_ac_hdr)
314 {
315 valid_ac_hdr = NULL;
316 break;
317 }
318 valid_ac_hdr = ac_hdr;
319 }
320
321 if (mutt_autocrypt_db_peer_get(from, &peer) < 0)
322 goto cleanup;
323
324 if (peer)
325 {
326 if (e->date_sent <= peer->autocrypt_timestamp)
327 {
328 rc = 0;
329 goto cleanup;
330 }
331
332 if (e->date_sent > peer->last_seen)
333 {
334 update_db = true;
335 peer->last_seen = e->date_sent;
336 }
337
338 if (valid_ac_hdr)
339 {
340 update_db = true;
342 peer->prefer_encrypt = valid_ac_hdr->prefer_encrypt;
343 if (!mutt_str_equal(peer->keydata, valid_ac_hdr->keydata))
344 {
345 import_gpg = true;
346 insert_db_history = true;
347 mutt_str_replace(&peer->keydata, valid_ac_hdr->keydata);
348 }
349 }
350 }
351 else if (valid_ac_hdr)
352 {
353 import_gpg = true;
354 insert_db = true;
355 insert_db_history = true;
356 }
357
358 if (!(import_gpg || insert_db || update_db))
359 {
360 rc = 0;
361 goto cleanup;
362 }
363
364 if (!peer)
365 {
367 peer->last_seen = e->date_sent;
369 peer->keydata = mutt_str_dup(valid_ac_hdr->keydata);
370 peer->prefer_encrypt = valid_ac_hdr->prefer_encrypt;
371 }
372
373 if (import_gpg)
374 {
375 keyid = buf_pool_get();
377 goto cleanup;
378 mutt_str_replace(&peer->keyid, buf_string(keyid));
379 }
380
381 if (insert_db && mutt_autocrypt_db_peer_insert(from, peer))
382 goto cleanup;
383
384 if (update_db && mutt_autocrypt_db_peer_update(peer))
385 goto cleanup;
386
387 if (insert_db_history)
388 {
390 peerhist->email_msgid = mutt_str_dup(env->message_id);
391 peerhist->timestamp = e->date_sent;
392 peerhist->keydata = mutt_str_dup(peer->keydata);
393 if (mutt_autocrypt_db_peer_history_insert(from, peerhist))
394 goto cleanup;
395 }
396
397 rc = 0;
398
399cleanup:
402 buf_pool_release(&keyid);
403
404 return rc;
405}
struct AutocryptPeer * mutt_autocrypt_db_peer_new(void)
Create a new AutocryptPeer.
Definition: db.c:525
int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
Insert a peer into the Autocrypt database.
Definition: db.c:624
int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
Get peer info from the Autocrypt database.
Definition: db.c:556
int mutt_autocrypt_db_peer_update(struct AutocryptPeer *peer)
Update the peer info in an Autocrypt database.
Definition: db.c:690
void mutt_autocrypt_db_peer_history_free(struct AutocryptPeerHistory **ptr)
Free an AutocryptPeerHistory.
Definition: db.c:755
void mutt_autocrypt_db_peer_free(struct AutocryptPeer **ptr)
Free an AutocryptPeer.
Definition: db.c:534
struct AutocryptPeerHistory * mutt_autocrypt_db_peer_history_new(void)
Create a new AutocryptPeerHistory.
Definition: db.c:746
int mutt_autocrypt_db_peer_history_insert(struct Address *addr, struct AutocryptPeerHistory *peerhist)
Insert peer history into the Autocrypt database.
Definition: db.c:774
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:97
int mutt_autocrypt_gpgme_import_key(const char *keydata, struct Buffer *keyid)
Read a key from GPGME.
Definition: gpgme.c:317
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:810
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
Parse Autocrypt header info.
Definition: envelope.h:44
struct AutocryptHeader * next
Linked list.
Definition: envelope.h:49
char * keydata
PGP Key data.
Definition: envelope.h:46
bool prefer_encrypt
User prefers encryption.
Definition: envelope.h:47
Autocrypt peer history.
Definition: lib.h:134
char * email_msgid
Message id of the email.
Definition: lib.h:136
char * keydata
PGP Key data.
Definition: lib.h:138
sqlite3_int64 timestamp
Timestamp of email.
Definition: lib.h:137
Autocrypt peer.
Definition: lib.h:118
sqlite3_int64 autocrypt_timestamp
When the email was sent.
Definition: lib.h:121
char * keyid
PGP Key id.
Definition: lib.h:122
char * keydata
PGP Key data.
Definition: lib.h:123
sqlite3_int64 last_seen
When was the peer last seen.
Definition: lib.h:120
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:124
char * subtype
content-type subtype
Definition: body.h:60
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
struct Body * body
List of MIME parts.
Definition: email.h:67
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:58
char * message_id
Message ID.
Definition: envelope.h:73
struct AutocryptHeader * autocrypt
Autocrypt header.
Definition: envelope.h:89
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_process_gossip_header()

int mutt_autocrypt_process_gossip_header ( struct Email e,
struct Envelope prot_headers 
)

Parse an Autocrypt email gossip header.

Parameters
eEmail
prot_headersEnvelope with protected headers
Return values
0Success
-1Error

Definition at line 414 of file autocrypt.c.

415{
416 struct AutocryptPeer *peer = NULL;
417 struct AutocryptGossipHistory *gossip_hist = NULL;
418 struct Address *peer_addr = NULL;
419 struct Address ac_hdr_addr = { 0 };
420 bool update_db = false, insert_db = false, insert_db_history = false, import_gpg = false;
421 int rc = -1;
422
423 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
424 if (!c_autocrypt)
425 return 0;
426
427 if (mutt_autocrypt_init(false))
428 return -1;
429
430 if (!e || !e->env || !prot_headers)
431 return 0;
432
433 struct Envelope *env = e->env;
434
435 struct Address *from = TAILQ_FIRST(&env->from);
436 if (!from)
437 return 0;
438
439 /* Ignore emails that appear to be more than a week in the future,
440 * since they can block all future updates during that time. */
441 if (e->date_sent > (mutt_date_now() + (7 * 24 * 60 * 60)))
442 return 0;
443
444 struct Buffer *keyid = buf_pool_get();
445
446 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
447
448 /* Normalize the recipient list for comparison */
449 mutt_addrlist_copy(&recips, &env->to, false);
450 mutt_addrlist_copy(&recips, &env->cc, false);
451 mutt_addrlist_copy(&recips, &env->reply_to, false);
453
454 for (struct AutocryptHeader *ac_hdr = prot_headers->autocrypt_gossip; ac_hdr;
455 ac_hdr = ac_hdr->next)
456 {
457 if (ac_hdr->invalid)
458 continue;
459
460 /* normalize for comparison against recipient list */
461 buf_strcpy(ac_hdr_addr.mailbox, ac_hdr->addr);
462 ac_hdr_addr.is_intl = true;
463 ac_hdr_addr.intl_checked = true;
465
466 /* Check to make sure the address is in the recipient list. */
467 TAILQ_FOREACH(peer_addr, &recips, entries)
468 {
469 if (buf_str_equal(peer_addr->mailbox, ac_hdr_addr.mailbox))
470 break;
471 }
472
473 if (!peer_addr)
474 continue;
475
476 if (mutt_autocrypt_db_peer_get(peer_addr, &peer) < 0)
477 goto cleanup;
478
479 if (peer)
480 {
481 if (e->date_sent <= peer->gossip_timestamp)
482 {
484 continue;
485 }
486
487 update_db = true;
488 peer->gossip_timestamp = e->date_sent;
489 /* This is slightly different from the autocrypt 1.1 spec.
490 * Avoid setting an empty peer.gossip_keydata with a value that matches
491 * the current peer.keydata. */
492 if ((peer->gossip_keydata && !mutt_str_equal(peer->gossip_keydata, ac_hdr->keydata)) ||
493 (!peer->gossip_keydata && !mutt_str_equal(peer->keydata, ac_hdr->keydata)))
494 {
495 import_gpg = true;
496 insert_db_history = true;
497 mutt_str_replace(&peer->gossip_keydata, ac_hdr->keydata);
498 }
499 }
500 else
501 {
502 import_gpg = true;
503 insert_db = true;
504 insert_db_history = true;
505 }
506
507 if (!peer)
508 {
510 peer->gossip_timestamp = e->date_sent;
511 peer->gossip_keydata = mutt_str_dup(ac_hdr->keydata);
512 }
513
514 if (import_gpg)
515 {
517 goto cleanup;
519 }
520
521 if (insert_db && mutt_autocrypt_db_peer_insert(peer_addr, peer))
522 goto cleanup;
523
524 if (update_db && mutt_autocrypt_db_peer_update(peer))
525 goto cleanup;
526
527 if (insert_db_history)
528 {
530 gossip_hist->sender_email_addr = buf_strdup(from->mailbox);
531 gossip_hist->email_msgid = mutt_str_dup(env->message_id);
532 gossip_hist->timestamp = e->date_sent;
533 gossip_hist->gossip_keydata = mutt_str_dup(peer->gossip_keydata);
534 if (mutt_autocrypt_db_gossip_history_insert(peer_addr, gossip_hist))
535 goto cleanup;
536 }
537
540 buf_reset(keyid);
541 update_db = false;
542 insert_db = false;
543 insert_db_history = false;
544 import_gpg = false;
545 }
546
547 rc = 0;
548
549cleanup:
550 FREE(&ac_hdr_addr.mailbox);
551 mutt_addrlist_clear(&recips);
554 buf_pool_release(&keyid);
555
556 return rc;
557}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:750
void mutt_autocrypt_db_normalize_addrlist(struct AddressList *al)
Normalise a list of Email Addresses.
Definition: db.c:176
struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new(void)
Create a new AutocryptGossipHistory.
Definition: db.c:827
int mutt_autocrypt_db_gossip_history_insert(struct Address *addr, struct AutocryptGossipHistory *gossip_hist)
Insert a gossip history into the Autocrypt database.
Definition: db.c:856
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:165
void mutt_autocrypt_db_gossip_history_free(struct AutocryptGossipHistory **ptr)
Free an AutocryptGossipHistory.
Definition: db.c:836
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
bool buf_str_equal(struct Buffer *a, struct Buffer *b)
Return if two buffers are equal.
Definition: buffer.c:647
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:401
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:536
#define FREE(x)
Definition: memory.h:43
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
bool intl_checked
Checked for IDN?
Definition: address.h:41
bool is_intl
International Domain Name.
Definition: address.h:40
Autocrypt gossip history.
Definition: lib.h:145
char * email_msgid
Sender's email's message id.
Definition: lib.h:148
char * sender_email_addr
Sender's email address.
Definition: lib.h:147
char * gossip_keydata
Gossip Key data.
Definition: lib.h:150
sqlite3_int64 timestamp
Timestamp of sender's email.
Definition: lib.h:149
char * gossip_keydata
Gossip Key data.
Definition: lib.h:127
char * gossip_keyid
Gossip Key id.
Definition: lib.h:126
sqlite3_int64 gossip_timestamp
Timestamp of Gossip header.
Definition: lib.h:125
struct Envelope * env
Envelope information.
Definition: email.h:66
The header of an Email.
Definition: envelope.h:57
struct AddressList to
Email's 'To' list.
Definition: envelope.h:60
struct AddressList reply_to
Email's 'reply-to'.
Definition: envelope.h:64
struct AutocryptHeader * autocrypt_gossip
Autocrypt Gossip header.
Definition: envelope.h:90
struct AddressList cc
Email's 'Cc' list.
Definition: envelope.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_ui_recommendation()

enum AutocryptRec mutt_autocrypt_ui_recommendation ( const struct Email e,
char **  keylist 
)

Get the recommended action for an Email.

Parameters
[in]eEmail
[out]keylistList of Autocrypt key ids
Return values
enumAutocryptRec Recommendation, e.g. AUTOCRYPT_REC_AVAILABLE

If the recommendataion is > NO and keylist is not NULL, keylist will be populated with the autocrypt keyids.

Definition at line 568 of file autocrypt.c.

569{
571 struct AutocryptAccount *account = NULL;
572 struct AutocryptPeer *peer = NULL;
573 struct Address *recip = NULL;
574 bool all_encrypt = true, has_discourage = false;
575 const char *matching_key = NULL;
576 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
577 struct Buffer *keylist_buf = NULL;
578
579 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
580 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
581 {
582 if (keylist)
583 {
584 /* L10N: Error displayed if the user tries to force sending an Autocrypt
585 email when the engine is not available. */
586 mutt_message(_("Autocrypt is not available"));
587 }
588 return AUTOCRYPT_REC_OFF;
589 }
590
591 struct Address *from = TAILQ_FIRST(&e->env->from);
592 if (!from || TAILQ_NEXT(from, entries))
593 {
594 if (keylist)
595 mutt_message(_("Autocrypt is not available"));
596 return AUTOCRYPT_REC_OFF;
597 }
598
600 {
601 if (keylist)
602 mutt_message(_("Autocrypt is not available"));
603 return AUTOCRYPT_REC_OFF;
604 }
605
606 if ((mutt_autocrypt_db_account_get(from, &account) <= 0) || !account->enabled)
607 {
608 if (keylist)
609 {
610 /* L10N: Error displayed if the user tries to force sending an Autocrypt
611 email when the account does not exist or is not enabled.
612 %s is the From email address used to look up the Autocrypt account.
613 */
614 mutt_message(_("Autocrypt is not enabled for %s"), buf_string(from->mailbox));
615 }
616 goto cleanup;
617 }
618
619 keylist_buf = buf_pool_get();
620 buf_addstr(keylist_buf, account->keyid);
621
622 mutt_addrlist_copy(&recips, &e->env->to, false);
623 mutt_addrlist_copy(&recips, &e->env->cc, false);
624 mutt_addrlist_copy(&recips, &e->env->bcc, false);
625
626 rc = AUTOCRYPT_REC_NO;
627 if (TAILQ_EMPTY(&recips))
628 goto cleanup;
629
630 TAILQ_FOREACH(recip, &recips, entries)
631 {
632 if (mutt_autocrypt_db_peer_get(recip, &peer) <= 0)
633 {
634 if (keylist)
635 {
636 /* L10N: s is an email address. Autocrypt is scanning for the keyids
637 to use to encrypt, but it can't find a valid keyid for this address.
638 The message is printed and they are returned to the compose menu. */
639 mutt_message(_("No (valid) autocrypt key found for %s"), recip->mailbox);
640 }
641 goto cleanup;
642 }
643
645 {
646 matching_key = peer->keyid;
647
648 if (!(peer->last_seen && peer->autocrypt_timestamp) ||
649 (peer->last_seen - peer->autocrypt_timestamp > (35 * 24 * 60 * 60)))
650 {
651 has_discourage = true;
652 all_encrypt = false;
653 }
654
655 if (!account->prefer_encrypt || !peer->prefer_encrypt)
656 all_encrypt = false;
657 }
659 {
660 matching_key = peer->gossip_keyid;
661
662 has_discourage = true;
663 all_encrypt = false;
664 }
665 else
666 {
667 if (keylist)
668 mutt_message(_("No (valid) autocrypt key found for %s"), recip->mailbox);
669 goto cleanup;
670 }
671
672 if (!buf_is_empty(keylist_buf))
673 buf_addch(keylist_buf, ' ');
674 buf_addstr(keylist_buf, matching_key);
675
677 }
678
679 if (all_encrypt)
681 else if (has_discourage)
683 else
685
686 if (keylist)
687 mutt_str_replace(keylist, buf_string(keylist_buf));
688
689cleanup:
691 mutt_addrlist_clear(&recips);
693 buf_pool_release(&keylist_buf);
694 return rc;
695}
AutocryptRec
Recommendation.
Definition: lib.h:157
@ AUTOCRYPT_REC_DISCOURAGE
Prefer not to use Autocrypt.
Definition: lib.h:160
@ AUTOCRYPT_REC_NO
Do no use Autocrypt.
Definition: lib.h:159
@ AUTOCRYPT_REC_OFF
No recommendations.
Definition: lib.h:158
@ AUTOCRYPT_REC_AVAILABLE
Autocrypt is available.
Definition: lib.h:161
@ AUTOCRYPT_REC_YES
Autocrypt should be used.
Definition: lib.h:162
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:303
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:253
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
bool mutt_autocrypt_gpgme_is_valid_key(const char *keyid)
Is a key id valid?
Definition: gpgme.c:357
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:91
#define TAILQ_EMPTY(head)
Definition: queue.h:721
char * keyid
PGP Key id.
Definition: lib.h:108
bool enabled
Is this account enabled.
Definition: lib.h:111
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:41
struct AddressList bcc
Email's 'Bcc' list.
Definition: envelope.h:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_set_sign_as_default_key()

int mutt_autocrypt_set_sign_as_default_key ( struct Email e)

Set the Autocrypt default key for signing.

Parameters
eEmail
Return values
0Success
-1Error

Definition at line 703 of file autocrypt.c.

704{
705 int rc = -1;
706 struct AutocryptAccount *account = NULL;
707
708 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
709 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
710 return -1;
711
712 struct Address *from = TAILQ_FIRST(&e->env->from);
713 if (!from || TAILQ_NEXT(from, entries))
714 return -1;
715
716 if (mutt_autocrypt_db_account_get(from, &account) <= 0)
717 goto cleanup;
718 if (!account->keyid)
719 goto cleanup;
720 if (!account->enabled)
721 goto cleanup;
722
725
726 rc = 0;
727
728cleanup:
730 return rc;
731}
char * AutocryptSignAs
Autocrypt Key id to sign as.
Definition: config.c:35
char * AutocryptDefaultKey
Autocrypt default key id (used for postponing messages)
Definition: config.c:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write_autocrypt_header_line()

static void write_autocrypt_header_line ( FILE *  fp,
const char *  addr,
bool  prefer_encrypt,
const char *  keydata 
)
static

Write an Autocrypt header to a file.

Parameters
fpFile to write to
addrEmail address
prefer_encryptWhether encryption is preferred
keydataRaw Autocrypt data

Definition at line 740 of file autocrypt.c.

742{
743 fprintf(fp, "addr=%s; ", addr);
744 if (prefer_encrypt)
745 fputs("prefer-encrypt=mutual; ", fp);
746 fputs("keydata=\n", fp);
747
748 while (*keydata)
749 {
750 int count = 0;
751 fputs("\t", fp);
752 while (*keydata && count < 75)
753 {
754 fputc(*keydata, fp);
755 count++;
756 keydata++;
757 }
758 fputs("\n", fp);
759 }
760}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_write_autocrypt_header()

int mutt_autocrypt_write_autocrypt_header ( struct Envelope env,
FILE *  fp 
)

Write the Autocrypt header to a file.

Parameters
envEnvelope
fpFile to write to
Return values
0Success
-1Error

Definition at line 769 of file autocrypt.c.

770{
771 int rc = -1;
772 struct AutocryptAccount *account = NULL;
773
774 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
775 if (!c_autocrypt || mutt_autocrypt_init(false) || !env)
776 return -1;
777
778 struct Address *from = TAILQ_FIRST(&env->from);
779 if (!from || TAILQ_NEXT(from, entries))
780 return -1;
781
782 if (mutt_autocrypt_db_account_get(from, &account) <= 0)
783 goto cleanup;
784 if (!account->keydata)
785 goto cleanup;
786 if (!account->enabled)
787 goto cleanup;
788
789 fputs("Autocrypt: ", fp);
791 account->keydata);
792
793 rc = 0;
794
795cleanup:
797 return rc;
798}
static void write_autocrypt_header_line(FILE *fp, const char *addr, bool prefer_encrypt, const char *keydata)
Write an Autocrypt header to a file.
Definition: autocrypt.c:740
char * email_addr
Email address.
Definition: lib.h:107
char * keydata
PGP Key data.
Definition: lib.h:109
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_write_gossip_headers()

int mutt_autocrypt_write_gossip_headers ( struct Envelope env,
FILE *  fp 
)

Write the Autocrypt gossip headers to a file.

Parameters
envEnvelope
fpFile to write to
Return values
0Success
-1Error

Definition at line 807 of file autocrypt.c.

808{
809 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
810 if (!c_autocrypt || mutt_autocrypt_init(false) || !env)
811 return -1;
812
813 for (struct AutocryptHeader *gossip = env->autocrypt_gossip; gossip;
814 gossip = gossip->next)
815 {
816 fputs("Autocrypt-Gossip: ", fp);
817 write_autocrypt_header_line(fp, gossip->addr, 0, gossip->keydata);
818 }
819
820 return 0;
821}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_generate_gossip_list()

int mutt_autocrypt_generate_gossip_list ( struct Email e)

Create the gossip list headers.

Parameters
eEmail
Return values
0Success
-1Error

Definition at line 829 of file autocrypt.c.

830{
831 int rc = -1;
832 struct AutocryptPeer *peer = NULL;
833 struct AutocryptAccount *account = NULL;
834 struct Address *recip = NULL;
835
836 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
837 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
838 return -1;
839
840 struct Envelope *mime_headers = e->body->mime_headers;
841 if (!mime_headers)
842 mime_headers = e->body->mime_headers = mutt_env_new();
844
845 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
846
847 mutt_addrlist_copy(&recips, &e->env->to, false);
848 mutt_addrlist_copy(&recips, &e->env->cc, false);
849
850 TAILQ_FOREACH(recip, &recips, entries)
851 {
852 /* At this point, we just accept missing keys and include what we can. */
853 if (mutt_autocrypt_db_peer_get(recip, &peer) <= 0)
854 continue;
855
856 const char *keydata = NULL;
858 keydata = peer->keydata;
860 keydata = peer->gossip_keydata;
861
862 if (keydata)
863 {
864 struct AutocryptHeader *gossip = mutt_autocrypthdr_new();
865 gossip->addr = mutt_str_dup(peer->email_addr);
866 gossip->keydata = mutt_str_dup(keydata);
867 gossip->next = mime_headers->autocrypt_gossip;
868 mime_headers->autocrypt_gossip = gossip;
869 }
870
872 }
873
874 TAILQ_FOREACH(recip, &e->env->reply_to, entries)
875 {
876 const char *addr = NULL;
877 const char *keydata = NULL;
878 if (mutt_autocrypt_db_account_get(recip, &account) > 0)
879 {
880 addr = account->email_addr;
881 keydata = account->keydata;
882 }
883 else if (mutt_autocrypt_db_peer_get(recip, &peer) > 0)
884 {
885 addr = peer->email_addr;
887 keydata = peer->keydata;
889 keydata = peer->gossip_keydata;
890 }
891
892 if (keydata)
893 {
894 struct AutocryptHeader *gossip = mutt_autocrypthdr_new();
895 gossip->addr = mutt_str_dup(addr);
896 gossip->keydata = mutt_str_dup(keydata);
897 gossip->next = mime_headers->autocrypt_gossip;
898 mime_headers->autocrypt_gossip = gossip;
899 }
902 }
903
904 mutt_addrlist_clear(&recips);
907 return rc;
908}
struct Envelope * mutt_env_new(void)
Create a new Envelope.
Definition: envelope.c:43
void mutt_autocrypthdr_free(struct AutocryptHeader **p)
Free an AutocryptHeader.
Definition: envelope.c:75
struct AutocryptHeader * mutt_autocrypthdr_new(void)
Create a new AutocryptHeader.
Definition: envelope.c:66
char * addr
Email address.
Definition: envelope.h:45
char * email_addr
Email address.
Definition: lib.h:119
struct Envelope * mime_headers
Memory hole protected headers.
Definition: body.h:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_scan_mailboxes()

void mutt_autocrypt_scan_mailboxes ( void  )

Scan mailboxes for Autocrypt headers.

This is invoked during the first autocrypt initialization, to scan one or more mailboxes for autocrypt headers.

Due to the implementation, header-cached headers are not scanned, so this routine just opens up the mailboxes with $header_cache temporarily disabled.

Definition at line 920 of file autocrypt.c.

921{
922#ifdef USE_HCACHE
923 const char *c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
924 char *old_hdrcache = mutt_str_dup(c_header_cache);
925 c_header_cache = NULL;
926#endif
927
928 struct Buffer *folderbuf = buf_pool_get();
929
930 /* L10N: The first time autocrypt is enabled, NeoMutt will ask to scan
931 through one or more mailboxes for Autocrypt: headers. Those headers are
932 then captured in the database as peer records and used for encryption.
933 If this is answered yes, they will be prompted for a mailbox. */
934 enum QuadOption scan = mutt_yesorno(_("Scan a mailbox for autocrypt headers?"), MUTT_YES);
935 while (scan == MUTT_YES)
936 {
937 struct Mailbox *m_cur = get_current_mailbox();
938 // L10N: The prompt for a mailbox to scan for Autocrypt: headers
939 if ((!buf_enter_fname(_("Scan mailbox"), folderbuf, true, m_cur, false,
940 NULL, NULL, MUTT_SEL_NO_FLAGS)) &&
941 (!buf_is_empty(folderbuf)))
942 {
943 buf_expand_path_regex(folderbuf, false);
944 struct Mailbox *m_ac = mx_path_resolve(buf_string(folderbuf));
945 /* NOTE: I am purposely *not* executing folder hooks here,
946 * as they can do all sorts of things like push into the getch() buffer.
947 * Authentication should be in account-hooks. */
948 if (mx_mbox_open(m_ac, MUTT_READONLY))
949 {
950 mx_mbox_close(m_ac);
951 }
952 buf_reset(folderbuf);
953 }
954
955 /* L10N: This is the second prompt to see if the user would like
956 to scan more than one mailbox for Autocrypt headers.
957 I'm purposely being extra verbose; asking first then prompting
958 for a mailbox. This is because this is a one-time operation
959 and I don't want them to accidentally ctrl-g and abort it. */
960 scan = mutt_yesorno(_("Scan another mailbox for autocrypt headers?"), MUTT_YES);
961 }
962
963#ifdef USE_HCACHE
964 cs_subset_str_native_set(NeoMutt->sub, "header_cache", (intptr_t) old_hdrcache, NULL);
965 old_hdrcache = NULL;
966#endif
967 buf_pool_release(&folderbuf);
968}
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:55
int buf_enter_fname(const char *prompt, struct Buffer *fname, bool mailbox, struct Mailbox *m, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
Ask the user to select a file.
Definition: curs_lib.c:446
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:662
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:136
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:307
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1698
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:618
#define MUTT_READONLY
Open in read-only mode.
Definition: mxapi.h:64
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
A mailbox.
Definition: mailbox.h:79
+ Here is the call graph for this function:
+ Here is the caller graph for this function: