NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "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.
 
int mutt_autocrypt_init (bool can_create)
 Initialise Autocrypt.
 
void mutt_autocrypt_cleanup (void)
 Shutdown Autocrypt.
 
int mutt_autocrypt_account_init (bool prompt)
 Create a new Autocrypt account.
 
int mutt_autocrypt_process_autocrypt_header (struct Email *e, struct Envelope *env)
 Parse an Autocrypt email header.
 
int mutt_autocrypt_process_gossip_header (struct Email *e, struct Envelope *prot_headers)
 Parse an Autocrypt email gossip header.
 
enum AutocryptRec mutt_autocrypt_ui_recommendation (const struct Email *e, char **keylist)
 Get the recommended action for an Email.
 
int mutt_autocrypt_set_sign_as_default_key (struct Email *e)
 Set the Autocrypt default key for signing.
 
static void write_autocrypt_header_line (FILE *fp, const char *addr, bool prefer_encrypt, const char *keydata)
 Write an Autocrypt header to a file.
 
int mutt_autocrypt_write_autocrypt_header (struct Envelope *env, FILE *fp)
 Write the Autocrypt header to a file.
 
int mutt_autocrypt_write_gossip_headers (struct Envelope *env, FILE *fp)
 Write the Autocrypt gossip headers to a file.
 
int mutt_autocrypt_generate_gossip_list (struct Email *e)
 Create the gossip list headers.
 
void mutt_autocrypt_scan_mailboxes (void)
 Scan mailboxes for Autocrypt headers.
 

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 58 of file autocrypt.c.

59{
60 int rc = 0;
61 struct stat st = { 0 };
62
63 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
64 if (stat(c_autocrypt_dir, &st) == 0)
65 return 0;
66
67 if (!can_create)
68 return -1;
69
70 struct Buffer *prompt = buf_pool_get();
71 /* L10N: s is a directory. NeoMutt is looking for a directory it needs
72 for some reason (e.g. autocrypt, header cache, bcache), but it
73 doesn't exist. The prompt is asking whether to create the directory */
74 buf_printf(prompt, _("%s does not exist. Create it?"), c_autocrypt_dir);
76 {
77 if (mutt_file_mkdir(c_autocrypt_dir, S_IRWXU) < 0)
78 {
79 /* L10N: mkdir() on the directory %s failed. The second %s is the
80 error message returned by libc */
81 mutt_error(_("Can't create %s: %s"), c_autocrypt_dir, strerror(errno));
82 rc = -1;
83 }
84 }
85
86 buf_pool_release(&prompt);
87 return rc;
88}
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:93
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:977
#define mutt_error(...)
Definition: logging2.h:92
#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 query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:330
String manipulation buffer.
Definition: buffer.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ 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 96 of file autocrypt.c.

97{
98 if (AutocryptDB)
99 return 0;
100
101 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
102 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
103 if (!c_autocrypt || !c_autocrypt_dir)
104 return -1;
105
107
108 if (autocrypt_dir_init(can_create))
109 goto bail;
110
112 goto bail;
113
114 if (mutt_autocrypt_db_init(can_create))
115 goto bail;
116
117 return 0;
118
119bail:
120 cs_subset_str_native_set(NeoMutt->sub, "autocrypt", false, NULL);
122 return -1;
123}
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:58
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: get.c:57
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:304
+ 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 128 of file autocrypt.c.

129{
131}
+ 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 142 of file autocrypt.c.

143{
144 struct Address *addr = NULL;
145 struct AutocryptAccount *account = NULL;
146 bool done = false;
147 int rc = -1;
148 bool prefer_encrypt = false;
149
150 if (prompt)
151 {
152 /* L10N: The first time NeoMutt is started with $autocrypt set, it will
153 create $autocrypt_dir and then prompt to create an autocrypt account
154 with this message. */
155 if (query_yesorno(_("Create an initial autocrypt account?"), MUTT_YES) != MUTT_YES)
156 return 0;
157 }
158
159 struct Buffer *keyid = buf_pool_get();
160 struct Buffer *keydata = buf_pool_get();
161
162 const struct Address *c_from = cs_subset_address(NeoMutt->sub, "from");
163 if (c_from)
164 {
165 addr = mutt_addr_copy(c_from);
166 const char *const c_real_name = cs_subset_string(NeoMutt->sub, "real_name");
167 if (!addr->personal && c_real_name)
168 addr->personal = buf_new(c_real_name);
169 }
170
171 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
172 mutt_addrlist_append(&al, addr);
173
174 do
175 {
176 /* L10N: Autocrypt is asking for the email address to use for the
177 autocrypt account. This will generate a key and add a record
178 to the database for use in autocrypt operations. */
179 if (mutt_edit_address(&al, _("Autocrypt account address: "), false) != 0)
180 goto cleanup;
181
182 addr = TAILQ_FIRST(&al);
183 if (!addr || !addr->mailbox || TAILQ_NEXT(addr, entries))
184 {
185 /* L10N: Autocrypt prompts for an account email address, and requires
186 a single address. This is shown if they entered something invalid,
187 nothing, or more than one address for some reason. */
188 mutt_error(_("Please enter a single email address"));
189 done = false;
190 }
191 else
192 {
193 done = true;
194 }
195 } while (!done);
196
197 addr = TAILQ_FIRST(&al);
198 if (mutt_autocrypt_db_account_get(addr, &account) < 0)
199 goto cleanup;
200 if (account)
201 {
202 /* L10N: When creating an autocrypt account, this message will be displayed
203 if there is already an account in the database with the email address
204 they just entered. */
205 mutt_error(_("That email address already has an autocrypt account"));
206 goto cleanup;
207 }
208
209 if (mutt_autocrypt_gpgme_select_or_create_key(addr, keyid, keydata))
210 goto cleanup;
211
212 /* L10N: Autocrypt has a setting "prefer-encrypt".
213 When the recommendation algorithm returns "available" and BOTH sender and
214 recipient choose "prefer-encrypt", encryption will be automatically
215 enabled.
216 Otherwise the UI will show encryption is "available" but the user
217 will be required to enable encryption manually. */
218 if (query_yesorno(_("Prefer encryption?"), MUTT_NO) == MUTT_YES)
219 prefer_encrypt = true;
220
221 if (mutt_autocrypt_db_account_insert(addr, buf_string(keyid), buf_string(keydata), prefer_encrypt))
222 {
223 goto cleanup;
224 }
225
226 rc = 0;
227
228cleanup:
229 if (rc == 0)
230 {
231 /* L10N: Message displayed after an autocrypt account is successfully created. */
232 mutt_message(_("Autocrypt account creation succeeded"));
233 }
234 else
235 {
236 /* L10N: Error message displayed if creating an autocrypt account failed
237 or was aborted by the user. */
238 mutt_error(_("Autocrypt account creation aborted"));
239 }
240
243 buf_pool_release(&keyid);
244 buf_pool_release(&keydata);
245 return rc;
246}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1461
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1481
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:742
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:292
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:263
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:91
@ 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:179
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 255 of file autocrypt.c.

256{
257 struct AutocryptHeader *valid_ac_hdr = NULL;
258 struct AutocryptPeer *peer = NULL;
259 struct AutocryptPeerHistory *peerhist = NULL;
260 struct Buffer *keyid = NULL;
261 bool update_db = false, insert_db = false, insert_db_history = false, import_gpg = false;
262 int rc = -1;
263
264 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
265 if (!c_autocrypt)
266 return 0;
267
268 if (mutt_autocrypt_init(false))
269 return -1;
270
271 if (!e || !e->body || !env)
272 return 0;
273
274 /* 1.1 spec says to skip emails with more than one From header */
275 struct Address *from = TAILQ_FIRST(&env->from);
276 if (!from || TAILQ_NEXT(from, entries))
277 return 0;
278
279 /* 1.1 spec also says to skip multipart/report emails */
280 if ((e->body->type == TYPE_MULTIPART) && mutt_istr_equal(e->body->subtype, "report"))
281 {
282 return 0;
283 }
284
285 /* Ignore emails that appear to be more than a week in the future,
286 * since they can block all future updates during that time. */
287 if (e->date_sent > (mutt_date_now() + (7 * 24 * 60 * 60)))
288 return 0;
289
290 for (struct AutocryptHeader *ac_hdr = env->autocrypt; ac_hdr; ac_hdr = ac_hdr->next)
291 {
292 if (ac_hdr->invalid)
293 continue;
294
295 /* NOTE: this assumes the processing is occurring right after
296 * mutt_parse_rfc822_line() and the from ADDR is still in the same
297 * form (intl) as the autocrypt header addr field */
298 if (!mutt_istr_equal(buf_string(from->mailbox), ac_hdr->addr))
299 continue;
300
301 /* 1.1 spec says ignore all, if more than one valid header is found. */
302 if (valid_ac_hdr)
303 {
304 valid_ac_hdr = NULL;
305 break;
306 }
307 valid_ac_hdr = ac_hdr;
308 }
309
310 if (mutt_autocrypt_db_peer_get(from, &peer) < 0)
311 goto cleanup;
312
313 if (peer)
314 {
315 if (e->date_sent <= peer->autocrypt_timestamp)
316 {
317 rc = 0;
318 goto cleanup;
319 }
320
321 if (e->date_sent > peer->last_seen)
322 {
323 update_db = true;
324 peer->last_seen = e->date_sent;
325 }
326
327 if (valid_ac_hdr)
328 {
329 update_db = true;
331 peer->prefer_encrypt = valid_ac_hdr->prefer_encrypt;
332 if (!mutt_str_equal(peer->keydata, valid_ac_hdr->keydata))
333 {
334 import_gpg = true;
335 insert_db_history = true;
336 mutt_str_replace(&peer->keydata, valid_ac_hdr->keydata);
337 }
338 }
339 }
340 else if (valid_ac_hdr)
341 {
342 import_gpg = true;
343 insert_db = true;
344 insert_db_history = true;
345 }
346
347 if (!(import_gpg || insert_db || update_db))
348 {
349 rc = 0;
350 goto cleanup;
351 }
352
353 if (!peer)
354 {
356 peer->last_seen = e->date_sent;
358 peer->keydata = mutt_str_dup(valid_ac_hdr->keydata);
359 peer->prefer_encrypt = valid_ac_hdr->prefer_encrypt;
360 }
361
362 if (import_gpg)
363 {
364 keyid = buf_pool_get();
366 goto cleanup;
367 mutt_str_replace(&peer->keyid, buf_string(keyid));
368 }
369
370 if (insert_db && mutt_autocrypt_db_peer_insert(from, peer))
371 goto cleanup;
372
373 if (update_db && mutt_autocrypt_db_peer_update(peer))
374 goto cleanup;
375
376 if (insert_db_history)
377 {
379 peerhist->email_msgid = mutt_str_dup(env->message_id);
380 peerhist->timestamp = e->date_sent;
381 peerhist->keydata = mutt_str_dup(peer->keydata);
382 if (mutt_autocrypt_db_peer_history_insert(from, peerhist))
383 goto cleanup;
384 }
385
386 rc = 0;
387
388cleanup:
391 buf_pool_release(&keyid);
392
393 return rc;
394}
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:96
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:87
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 403 of file autocrypt.c.

404{
405 struct AutocryptPeer *peer = NULL;
406 struct AutocryptGossipHistory *gossip_hist = NULL;
407 struct Address *peer_addr = NULL;
408 struct Address ac_hdr_addr = { 0 };
409 bool update_db = false, insert_db = false, insert_db_history = false, import_gpg = false;
410 int rc = -1;
411
412 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
413 if (!c_autocrypt)
414 return 0;
415
416 if (mutt_autocrypt_init(false))
417 return -1;
418
419 if (!e || !e->env || !prot_headers)
420 return 0;
421
422 struct Envelope *env = e->env;
423
424 struct Address *from = TAILQ_FIRST(&env->from);
425 if (!from)
426 return 0;
427
428 /* Ignore emails that appear to be more than a week in the future,
429 * since they can block all future updates during that time. */
430 if (e->date_sent > (mutt_date_now() + (7 * 24 * 60 * 60)))
431 return 0;
432
433 struct Buffer *keyid = buf_pool_get();
434
435 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
436
437 /* Normalize the recipient list for comparison */
438 mutt_addrlist_copy(&recips, &env->to, false);
439 mutt_addrlist_copy(&recips, &env->cc, false);
440 mutt_addrlist_copy(&recips, &env->reply_to, false);
442
443 for (struct AutocryptHeader *ac_hdr = prot_headers->autocrypt_gossip; ac_hdr;
444 ac_hdr = ac_hdr->next)
445 {
446 if (ac_hdr->invalid)
447 continue;
448
449 /* normalize for comparison against recipient list */
450 buf_strcpy(ac_hdr_addr.mailbox, ac_hdr->addr);
451 ac_hdr_addr.is_intl = true;
452 ac_hdr_addr.intl_checked = true;
454
455 /* Check to make sure the address is in the recipient list. */
456 TAILQ_FOREACH(peer_addr, &recips, entries)
457 {
458 if (buf_str_equal(peer_addr->mailbox, ac_hdr_addr.mailbox))
459 break;
460 }
461
462 if (!peer_addr)
463 continue;
464
465 if (mutt_autocrypt_db_peer_get(peer_addr, &peer) < 0)
466 goto cleanup;
467
468 if (peer)
469 {
470 if (e->date_sent <= peer->gossip_timestamp)
471 {
473 continue;
474 }
475
476 update_db = true;
477 peer->gossip_timestamp = e->date_sent;
478 /* This is slightly different from the autocrypt 1.1 spec.
479 * Avoid setting an empty peer.gossip_keydata with a value that matches
480 * the current peer.keydata. */
481 if ((peer->gossip_keydata && !mutt_str_equal(peer->gossip_keydata, ac_hdr->keydata)) ||
482 (!peer->gossip_keydata && !mutt_str_equal(peer->keydata, ac_hdr->keydata)))
483 {
484 import_gpg = true;
485 insert_db_history = true;
486 mutt_str_replace(&peer->gossip_keydata, ac_hdr->keydata);
487 }
488 }
489 else
490 {
491 import_gpg = true;
492 insert_db = true;
493 insert_db_history = true;
494 }
495
496 if (!peer)
497 {
499 peer->gossip_timestamp = e->date_sent;
500 peer->gossip_keydata = mutt_str_dup(ac_hdr->keydata);
501 }
502
503 if (import_gpg)
504 {
506 goto cleanup;
508 }
509
510 if (insert_db && mutt_autocrypt_db_peer_insert(peer_addr, peer))
511 goto cleanup;
512
513 if (update_db && mutt_autocrypt_db_peer_update(peer))
514 goto cleanup;
515
516 if (insert_db_history)
517 {
519 gossip_hist->sender_email_addr = buf_strdup(from->mailbox);
520 gossip_hist->email_msgid = mutt_str_dup(env->message_id);
521 gossip_hist->timestamp = e->date_sent;
522 gossip_hist->gossip_keydata = mutt_str_dup(peer->gossip_keydata);
523 if (mutt_autocrypt_db_gossip_history_insert(peer_addr, gossip_hist))
524 goto cleanup;
525 }
526
529 buf_reset(keyid);
530 update_db = false;
531 insert_db = false;
532 insert_db_history = false;
533 import_gpg = false;
534 }
535
536 rc = 0;
537
538cleanup:
539 FREE(&ac_hdr_addr.mailbox);
540 mutt_addrlist_clear(&recips);
543 buf_pool_release(&keyid);
544
545 return rc;
546}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:762
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(const struct Buffer *a, const struct Buffer *b)
Return if two buffers are equal.
Definition: buffer.c:653
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:407
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
#define FREE(x)
Definition: memory.h:45
#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:88
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 557 of file autocrypt.c.

558{
560 struct AutocryptAccount *account = NULL;
561 struct AutocryptPeer *peer = NULL;
562 struct Address *recip = NULL;
563 bool all_encrypt = true, has_discourage = false;
564 const char *matching_key = NULL;
565 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
566 struct Buffer *keylist_buf = NULL;
567
568 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
569 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
570 {
571 if (keylist)
572 {
573 /* L10N: Error displayed if the user tries to force sending an Autocrypt
574 email when the engine is not available. */
575 mutt_message(_("Autocrypt is not available"));
576 }
577 return AUTOCRYPT_REC_OFF;
578 }
579
580 struct Address *from = TAILQ_FIRST(&e->env->from);
581 if (!from || TAILQ_NEXT(from, entries))
582 {
583 if (keylist)
584 mutt_message(_("Autocrypt is not available"));
585 return AUTOCRYPT_REC_OFF;
586 }
587
589 {
590 if (keylist)
591 mutt_message(_("Autocrypt is not available"));
592 return AUTOCRYPT_REC_OFF;
593 }
594
595 if ((mutt_autocrypt_db_account_get(from, &account) <= 0) || !account->enabled)
596 {
597 if (keylist)
598 {
599 /* L10N: Error displayed if the user tries to force sending an Autocrypt
600 email when the account does not exist or is not enabled.
601 %s is the From email address used to look up the Autocrypt account.
602 */
603 mutt_message(_("Autocrypt is not enabled for %s"), buf_string(from->mailbox));
604 }
605 goto cleanup;
606 }
607
608 keylist_buf = buf_pool_get();
609 buf_addstr(keylist_buf, account->keyid);
610
611 mutt_addrlist_copy(&recips, &e->env->to, false);
612 mutt_addrlist_copy(&recips, &e->env->cc, false);
613 mutt_addrlist_copy(&recips, &e->env->bcc, false);
614
615 rc = AUTOCRYPT_REC_NO;
616 if (TAILQ_EMPTY(&recips))
617 goto cleanup;
618
619 TAILQ_FOREACH(recip, &recips, entries)
620 {
621 if (mutt_autocrypt_db_peer_get(recip, &peer) <= 0)
622 {
623 if (keylist)
624 {
625 /* L10N: s is an email address. Autocrypt is scanning for the keyids
626 to use to encrypt, but it can't find a valid keyid for this address.
627 The message is printed and they are returned to the compose menu. */
628 mutt_message(_("No (valid) autocrypt key found for %s"),
629 buf_string(recip->mailbox));
630 }
631 goto cleanup;
632 }
633
635 {
636 matching_key = peer->keyid;
637
638 if (!(peer->last_seen && peer->autocrypt_timestamp) ||
639 (peer->last_seen - peer->autocrypt_timestamp > (35 * 24 * 60 * 60)))
640 {
641 has_discourage = true;
642 all_encrypt = false;
643 }
644
645 if (!account->prefer_encrypt || !peer->prefer_encrypt)
646 all_encrypt = false;
647 }
649 {
650 matching_key = peer->gossip_keyid;
651
652 has_discourage = true;
653 all_encrypt = false;
654 }
655 else
656 {
657 if (keylist)
658 {
659 mutt_message(_("No (valid) autocrypt key found for %s"),
660 buf_string(recip->mailbox));
661 }
662 goto cleanup;
663 }
664
665 if (!buf_is_empty(keylist_buf))
666 buf_addch(keylist_buf, ' ');
667 buf_addstr(keylist_buf, matching_key);
668
670 }
671
672 if (all_encrypt)
674 else if (has_discourage)
676 else
678
679 if (keylist)
680 mutt_str_replace(keylist, buf_string(keylist_buf));
681
682cleanup:
684 mutt_addrlist_clear(&recips);
686 buf_pool_release(&keylist_buf);
687 return rc;
688}
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:358
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:92
#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 696 of file autocrypt.c.

697{
698 int rc = -1;
699 struct AutocryptAccount *account = NULL;
700
701 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
702 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
703 return -1;
704
705 struct Address *from = TAILQ_FIRST(&e->env->from);
706 if (!from || TAILQ_NEXT(from, entries))
707 return -1;
708
709 if (mutt_autocrypt_db_account_get(from, &account) <= 0)
710 goto cleanup;
711 if (!account->keyid)
712 goto cleanup;
713 if (!account->enabled)
714 goto cleanup;
715
718
719 rc = 0;
720
721cleanup:
723 return rc;
724}
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 733 of file autocrypt.c.

735{
736 fprintf(fp, "addr=%s; ", addr);
737 if (prefer_encrypt)
738 fputs("prefer-encrypt=mutual; ", fp);
739 fputs("keydata=\n", fp);
740
741 while (*keydata)
742 {
743 int count = 0;
744 fputs("\t", fp);
745 while (*keydata && count < 75)
746 {
747 fputc(*keydata, fp);
748 count++;
749 keydata++;
750 }
751 fputs("\n", fp);
752 }
753}
+ 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 762 of file autocrypt.c.

763{
764 int rc = -1;
765 struct AutocryptAccount *account = NULL;
766
767 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
768 if (!c_autocrypt || mutt_autocrypt_init(false) || !env)
769 return -1;
770
771 struct Address *from = TAILQ_FIRST(&env->from);
772 if (!from || TAILQ_NEXT(from, entries))
773 return -1;
774
775 if (mutt_autocrypt_db_account_get(from, &account) <= 0)
776 goto cleanup;
777 if (!account->keydata)
778 goto cleanup;
779 if (!account->enabled)
780 goto cleanup;
781
782 fputs("Autocrypt: ", fp);
784 account->keydata);
785
786 rc = 0;
787
788cleanup:
790 return rc;
791}
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:733
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 800 of file autocrypt.c.

801{
802 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
803 if (!c_autocrypt || mutt_autocrypt_init(false) || !env)
804 return -1;
805
806 for (struct AutocryptHeader *gossip = env->autocrypt_gossip; gossip;
807 gossip = gossip->next)
808 {
809 fputs("Autocrypt-Gossip: ", fp);
810 write_autocrypt_header_line(fp, gossip->addr, 0, gossip->keydata);
811 }
812
813 return 0;
814}
+ 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 822 of file autocrypt.c.

823{
824 int rc = -1;
825 struct AutocryptPeer *peer = NULL;
826 struct AutocryptAccount *account = NULL;
827 struct Address *recip = NULL;
828
829 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
830 if (!c_autocrypt || mutt_autocrypt_init(false) || !e)
831 return -1;
832
833 struct Envelope *mime_headers = e->body->mime_headers;
834 if (!mime_headers)
835 mime_headers = e->body->mime_headers = mutt_env_new();
837
838 struct AddressList recips = TAILQ_HEAD_INITIALIZER(recips);
839
840 mutt_addrlist_copy(&recips, &e->env->to, false);
841 mutt_addrlist_copy(&recips, &e->env->cc, false);
842
843 TAILQ_FOREACH(recip, &recips, entries)
844 {
845 /* At this point, we just accept missing keys and include what we can. */
846 if (mutt_autocrypt_db_peer_get(recip, &peer) <= 0)
847 continue;
848
849 const char *keydata = NULL;
851 keydata = peer->keydata;
853 keydata = peer->gossip_keydata;
854
855 if (keydata)
856 {
857 struct AutocryptHeader *gossip = mutt_autocrypthdr_new();
858 gossip->addr = mutt_str_dup(peer->email_addr);
859 gossip->keydata = mutt_str_dup(keydata);
860 gossip->next = mime_headers->autocrypt_gossip;
861 mime_headers->autocrypt_gossip = gossip;
862 }
863
865 }
866
867 TAILQ_FOREACH(recip, &e->env->reply_to, entries)
868 {
869 const char *addr = NULL;
870 const char *keydata = NULL;
871 if (mutt_autocrypt_db_account_get(recip, &account) > 0)
872 {
873 addr = account->email_addr;
874 keydata = account->keydata;
875 }
876 else if (mutt_autocrypt_db_peer_get(recip, &peer) > 0)
877 {
878 addr = peer->email_addr;
880 keydata = peer->keydata;
882 keydata = peer->gossip_keydata;
883 }
884
885 if (keydata)
886 {
887 struct AutocryptHeader *gossip = mutt_autocrypthdr_new();
888 gossip->addr = mutt_str_dup(addr);
889 gossip->keydata = mutt_str_dup(keydata);
890 gossip->next = mime_headers->autocrypt_gossip;
891 mime_headers->autocrypt_gossip = gossip;
892 }
895 }
896
897 mutt_addrlist_clear(&recips);
900 return rc;
901}
struct Envelope * mutt_env_new(void)
Create a new Envelope.
Definition: envelope.c:43
struct AutocryptHeader * mutt_autocrypthdr_new(void)
Create a new AutocryptHeader.
Definition: envelope.c:66
void mutt_autocrypthdr_free(struct AutocryptHeader **ptr)
Free an AutocryptHeader.
Definition: envelope.c:75
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 913 of file autocrypt.c.

914{
915#ifdef USE_HCACHE
916 const char *c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
917 char *old_hdrcache = mutt_str_dup(c_header_cache);
918 cs_subset_str_native_set(NeoMutt->sub, "header_cache", 0, NULL);
919#endif
920
921 struct Buffer *folderbuf = buf_pool_get();
922
923 /* L10N: The first time autocrypt is enabled, NeoMutt will ask to scan
924 through one or more mailboxes for Autocrypt: headers. Those headers are
925 then captured in the database as peer records and used for encryption.
926 If this is answered yes, they will be prompted for a mailbox. */
927 enum QuadOption scan = query_yesorno(_("Scan a mailbox for autocrypt headers?"), MUTT_YES);
928 while (scan == MUTT_YES)
929 {
930 struct Mailbox *m_cur = get_current_mailbox();
931 // L10N: The prompt for a mailbox to scan for Autocrypt: headers
932 if ((!mw_enter_fname(_("Scan mailbox"), folderbuf, true, m_cur, false, NULL,
933 NULL, MUTT_SEL_NO_FLAGS)) &&
934 (!buf_is_empty(folderbuf)))
935 {
936 buf_expand_path_regex(folderbuf, false);
937 struct Mailbox *m_ac = mx_path_resolve(buf_string(folderbuf));
938 /* NOTE: I am purposely *not* executing folder hooks here,
939 * as they can do all sorts of things like push into the getch() buffer.
940 * Authentication should be in account-hooks. */
941 if (mx_mbox_open(m_ac, MUTT_READONLY))
942 {
943 mx_mbox_close(m_ac);
944 }
945 buf_reset(folderbuf);
946 }
947
948 /* L10N: This is the second prompt to see if the user would like
949 to scan more than one mailbox for Autocrypt headers.
950 I'm purposely being extra verbose; asking first then prompting
951 for a mailbox. This is because this is a one-time operation
952 and I don't want them to accidentally ctrl-g and abort it. */
953 scan = query_yesorno(_("Scan another mailbox for autocrypt headers?"), MUTT_YES);
954 }
955
956#ifdef USE_HCACHE
957 cs_subset_str_native_set(NeoMutt->sub, "header_cache", (intptr_t) old_hdrcache, NULL);
958 old_hdrcache = NULL;
959#endif
960 buf_pool_release(&folderbuf);
961}
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:57
int mw_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:247
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:284
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1652
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:593
#define MUTT_READONLY
Open in read-only mode.
Definition: mxapi.h:43
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: