NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
private.h File Reference

Shared constants/structs that are private to Autocrypt. More...

#include <sqlite3.h>
#include <stdbool.h>
+ Include dependency graph for private.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AccountEntry
 An entry in the Autocrypt account Menu. More...
 

Functions

int mutt_autocrypt_account_init (bool prompt)
 Create a new Autocrypt account.
 
void mutt_autocrypt_scan_mailboxes (void)
 Scan mailboxes for Autocrypt headers.
 
int mutt_autocrypt_db_account_delete (struct AutocryptAccount *acct)
 Delete an Account from the Autocrypt database.
 
void mutt_autocrypt_db_account_free (struct AutocryptAccount **ptr)
 Free an AutocryptAccount.
 
int mutt_autocrypt_db_account_get (struct Address *addr, struct AutocryptAccount **account)
 Get Autocrypt Account data from the database.
 
int mutt_autocrypt_db_account_get_all (struct AutocryptAccount ***accounts, int *num_accounts)
 Get all accounts from an Autocrypt database.
 
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.
 
struct AutocryptAccountmutt_autocrypt_db_account_new (void)
 Create a new AutocryptAccount.
 
int mutt_autocrypt_db_account_update (struct AutocryptAccount *acct)
 Update Account info in the Autocrypt database.
 
void mutt_autocrypt_db_close (void)
 Close the Autocrypt SQLite database connection.
 
void mutt_autocrypt_db_gossip_history_free (struct AutocryptGossipHistory **ptr)
 Free an AutocryptGossipHistory.
 
int mutt_autocrypt_db_gossip_history_insert (struct Address *addr, struct AutocryptGossipHistory *gossip_hist)
 Insert a gossip history into the Autocrypt database.
 
struct AutocryptGossipHistorymutt_autocrypt_db_gossip_history_new (void)
 Create a new AutocryptGossipHistory.
 
int mutt_autocrypt_db_init (bool can_create)
 Initialise the Autocrypt SQLite database.
 
void mutt_autocrypt_db_normalize_addr (struct Address *a)
 Normalise an Email Address.
 
void mutt_autocrypt_db_normalize_addrlist (struct AddressList *al)
 Normalise a list of Email Addresses.
 
void mutt_autocrypt_db_peer_free (struct AutocryptPeer **ptr)
 Free an AutocryptPeer.
 
int mutt_autocrypt_db_peer_get (struct Address *addr, struct AutocryptPeer **peer)
 Get peer info from the Autocrypt database.
 
void mutt_autocrypt_db_peer_history_free (struct AutocryptPeerHistory **ptr)
 Free an AutocryptPeerHistory.
 
int mutt_autocrypt_db_peer_history_insert (struct Address *addr, struct AutocryptPeerHistory *peerhist)
 Insert peer history into the Autocrypt database.
 
struct AutocryptPeerHistorymutt_autocrypt_db_peer_history_new (void)
 Create a new AutocryptPeerHistory.
 
int mutt_autocrypt_db_peer_insert (struct Address *addr, struct AutocryptPeer *peer)
 Insert a peer into the Autocrypt database.
 
struct AutocryptPeermutt_autocrypt_db_peer_new (void)
 Create a new AutocryptPeer.
 
int mutt_autocrypt_db_peer_update (struct AutocryptPeer *peer)
 Update the peer info in an Autocrypt database.
 
int mutt_autocrypt_schema_init (void)
 Set up an Autocrypt database.
 
int mutt_autocrypt_schema_update (void)
 Update the version number of the Autocrypt database schema.
 
int mutt_autocrypt_gpgme_create_key (struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
 Create a GPGME key.
 
int mutt_autocrypt_gpgme_import_key (const char *keydata, struct Buffer *keyid)
 Read a key from GPGME.
 
int mutt_autocrypt_gpgme_init (void)
 Initialise GPGME.
 
bool mutt_autocrypt_gpgme_is_valid_key (const char *keyid)
 Is a key id valid?
 
int mutt_autocrypt_gpgme_select_key (struct Buffer *keyid, struct Buffer *keydata)
 Select a Autocrypt key.
 
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.
 
bool populate_menu (struct Menu *menu)
 Add the Autocrypt data to a Menu.
 

Variables

sqlite3 * AutocryptDB
 Handle to the open Autocrypt database.
 

Detailed Description

Shared constants/structs that are private to Autocrypt.

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 private.h.

Function Documentation

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

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

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

int mutt_autocrypt_db_account_delete ( struct AutocryptAccount acct)

Delete an Account from the Autocrypt database.

Parameters
acctAccount to delete
Return values
0Success
-1Error

Definition at line 425 of file db.c.

426{
427 int rc = -1;
428
430 {
431 if (sqlite3_prepare_v3(AutocryptDB,
432 "DELETE from account "
433 "WHERE email_addr = ?;",
434 -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
435 {
436 goto cleanup;
437 }
438 }
439
440 if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
441 goto cleanup;
442
443 if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
444 goto cleanup;
445
446 rc = 0;
447
448cleanup:
449 sqlite3_reset(AccountDeleteStmt);
450 return rc;
451}
sqlite3 * AutocryptDB
Handle to the open Autocrypt database.
Definition: db.c:53
static sqlite3_stmt * AccountDeleteStmt
Delete an autocrypt account.
Definition: db.c:45
char * email_addr
Email address.
Definition: lib.h:107
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_free()

void mutt_autocrypt_db_account_free ( struct AutocryptAccount **  ptr)

Free an AutocryptAccount.

Parameters
ptrAccount to free

Definition at line 244 of file db.c.

245{
246 if (!ptr || !*ptr)
247 return;
248
249 struct AutocryptAccount *ac = *ptr;
250 FREE(&ac->email_addr);
251 FREE(&ac->keyid);
252 FREE(&ac->keydata);
253 FREE(ptr);
254}
#define FREE(x)
Definition: memory.h:45
char * keydata
PGP Key data.
Definition: lib.h:109
char * keyid
PGP Key id.
Definition: lib.h:108
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_get()

int mutt_autocrypt_db_account_get ( struct Address addr,
struct AutocryptAccount **  account 
)

Get Autocrypt Account data from the database.

Parameters
[in]addrEmail Address to lookup
[out]accountMatched account
Return values
0Success
-1Error

Definition at line 263 of file db.c.

264{
265 int rc = -1;
266
267 struct Address *norm_addr = copy_normalize_addr(addr);
268 *account = NULL;
269
270 if (!AccountGetStmt)
271 {
272 if (sqlite3_prepare_v3(AutocryptDB,
273 "SELECT "
274 "email_addr, "
275 "keyid, "
276 "keydata, "
277 "prefer_encrypt, "
278 "enabled "
279 "FROM account "
280 "WHERE email_addr = ?",
281 -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
282 {
283 goto cleanup;
284 }
285 }
286
287 if (sqlite3_bind_text(AccountGetStmt, 1, buf_string(norm_addr->mailbox), -1,
288 SQLITE_STATIC) != SQLITE_OK)
289 {
290 goto cleanup;
291 }
292
293 int result = sqlite3_step(AccountGetStmt);
294 if (result != SQLITE_ROW)
295 {
296 if (result == SQLITE_DONE)
297 rc = 0;
298 goto cleanup;
299 }
300
302 (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
303 (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
304 (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
305 (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
306 (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
307
308 rc = 1;
309
310cleanup:
311 mutt_addr_free(&norm_addr);
312 sqlite3_reset(AccountGetStmt);
313 return rc;
314}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:460
struct AutocryptAccount * mutt_autocrypt_db_account_new(void)
Create a new AutocryptAccount.
Definition: db.c:235
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition: db.c:202
static sqlite3_stmt * AccountGetStmt
Get the matching autocrypt accounts.
Definition: db.c:42
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:225
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_get_all()

int mutt_autocrypt_db_account_get_all ( struct AutocryptAccount ***  accounts,
int *  num_accounts 
)

Get all accounts from an Autocrypt database.

Parameters
[out]accountsList of accounts
[out]num_accountsNumber of accounts
Return values
0Success
-1Error

Definition at line 460 of file db.c.

461{
462 int rc = -1, result;
463 sqlite3_stmt *stmt = NULL;
464 struct AutocryptAccount **results = NULL;
465 int results_len = 0, results_count = 0;
466
467 *accounts = NULL;
468 *num_accounts = 0;
469
470 /* Note, speed is not of the essence for the account management screen,
471 * so we don't bother with a persistent prepared statement */
472 if (sqlite3_prepare_v2(AutocryptDB,
473 "SELECT "
474 "email_addr, "
475 "keyid, "
476 "keydata, "
477 "prefer_encrypt, "
478 "enabled "
479 "FROM account "
480 "ORDER BY email_addr",
481 -1, &stmt, NULL) != SQLITE_OK)
482 {
483 goto cleanup;
484 }
485
486 while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
487 {
488 if (results_count == results_len)
489 {
490 results_len += 5;
491 mutt_mem_realloc(&results, results_len * sizeof(struct AutocryptAccount *));
492 }
493
495 results[results_count++] = account;
496
497 account->email_addr = strdup_column_text(stmt, 0);
498 account->keyid = strdup_column_text(stmt, 1);
499 account->keydata = strdup_column_text(stmt, 2);
500 account->prefer_encrypt = sqlite3_column_int(stmt, 3);
501 account->enabled = sqlite3_column_int(stmt, 4);
502 }
503
504 if (result == SQLITE_DONE)
505 {
506 *accounts = results;
507 rc = *num_accounts = results_count;
508 }
509 else
510 {
511 while (results_count > 0)
512 mutt_autocrypt_db_account_free(&results[--results_count]);
513 FREE(&results);
514 }
515
516cleanup:
517 sqlite3_finalize(stmt);
518 return rc;
519}
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
bool enabled
Is this account enabled.
Definition: lib.h:111
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_insert()

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.

Parameters
addrEmail Address for the account
keyidAutocrypt KeyID
keydataAutocrypt key data
prefer_encryptWhether the account prefers encryption
Return values
0Success
-1Error

Definition at line 325 of file db.c.

327{
328 int rc = -1;
329
330 struct Address *norm_addr = copy_normalize_addr(addr);
331
333 {
334 if (sqlite3_prepare_v3(AutocryptDB,
335 "INSERT INTO account "
336 "(email_addr, "
337 "keyid, "
338 "keydata, "
339 "prefer_encrypt, "
340 "enabled) "
341 "VALUES (?, ?, ?, ?, ?);",
342 -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
343 {
344 goto cleanup;
345 }
346 }
347
348 if (sqlite3_bind_text(AccountInsertStmt, 1, buf_string(norm_addr->mailbox),
349 -1, SQLITE_STATIC) != SQLITE_OK)
350 {
351 goto cleanup;
352 }
353 if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
354 goto cleanup;
355 if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
356 goto cleanup;
357 if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
358 goto cleanup;
359 if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
360 goto cleanup;
361
362 if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
363 goto cleanup;
364
365 rc = 0;
366
367cleanup:
368 mutt_addr_free(&norm_addr);
369 sqlite3_reset(AccountInsertStmt);
370 return rc;
371}
static sqlite3_stmt * AccountInsertStmt
Insert a new autocrypt account.
Definition: db.c:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_new()

struct AutocryptAccount * mutt_autocrypt_db_account_new ( void  )

Create a new AutocryptAccount.

Return values
ptrNew AutocryptAccount

Definition at line 235 of file db.c.

236{
237 return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
238}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_update()

int mutt_autocrypt_db_account_update ( struct AutocryptAccount acct)

Update Account info in the Autocrypt database.

Parameters
acctAutocrypt Account data
Return values
0Success
-1Error

Definition at line 379 of file db.c.

380{
381 int rc = -1;
382
384 {
385 if (sqlite3_prepare_v3(AutocryptDB,
386 "UPDATE account SET "
387 "keyid = ?, "
388 "keydata = ?, "
389 "prefer_encrypt = ?, "
390 "enabled = ? "
391 "WHERE email_addr = ?;",
392 -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
393 {
394 goto cleanup;
395 }
396 }
397
398 if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
399 goto cleanup;
400 if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
401 goto cleanup;
402 if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
403 goto cleanup;
404 if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
405 goto cleanup;
406 if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
407 goto cleanup;
408
409 if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
410 goto cleanup;
411
412 rc = 0;
413
414cleanup:
415 sqlite3_reset(AccountUpdateStmt);
416 return rc;
417}
static sqlite3_stmt * AccountUpdateStmt
Update an autocrypt account.
Definition: db.c:44
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_close()

void mutt_autocrypt_db_close ( void  )

Close the Autocrypt SQLite database connection.

Definition at line 130 of file db.c.

131{
132 if (!AutocryptDB)
133 return;
134
135 sqlite3_finalize(AccountGetStmt);
136 AccountGetStmt = NULL;
137 sqlite3_finalize(AccountInsertStmt);
138 AccountInsertStmt = NULL;
139 sqlite3_finalize(AccountUpdateStmt);
140 AccountUpdateStmt = NULL;
141 sqlite3_finalize(AccountDeleteStmt);
142 AccountDeleteStmt = NULL;
143
144 sqlite3_finalize(PeerGetStmt);
145 PeerGetStmt = NULL;
146 sqlite3_finalize(PeerInsertStmt);
147 PeerInsertStmt = NULL;
148 sqlite3_finalize(PeerUpdateStmt);
149 PeerUpdateStmt = NULL;
150
151 sqlite3_finalize(PeerHistoryInsertStmt);
153
154 sqlite3_finalize(GossipHistoryInsertStmt);
156
157 sqlite3_close_v2(AutocryptDB);
158 AutocryptDB = NULL;
159}
static sqlite3_stmt * PeerUpdateStmt
Update a peer address.
Definition: db.c:48
static sqlite3_stmt * PeerGetStmt
Get the matching peer addresses.
Definition: db.c:46
static sqlite3_stmt * PeerHistoryInsertStmt
Add to the peer history.
Definition: db.c:49
static sqlite3_stmt * GossipHistoryInsertStmt
Add to the gossip history.
Definition: db.c:50
static sqlite3_stmt * PeerInsertStmt
Insert a new peer address.
Definition: db.c:47
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_free()

void mutt_autocrypt_db_gossip_history_free ( struct AutocryptGossipHistory **  ptr)

Free an AutocryptGossipHistory.

Parameters
ptrAutocryptGossipHistory to free

Definition at line 836 of file db.c.

837{
838 if (!ptr || !*ptr)
839 return;
840
841 struct AutocryptGossipHistory *gh = *ptr;
842 FREE(&gh->peer_email_addr);
844 FREE(&gh->email_msgid);
845 FREE(&gh->gossip_keydata);
846 FREE(ptr);
847}
Autocrypt gossip history.
Definition: lib.h:145
char * peer_email_addr
Email addressof the peer.
Definition: lib.h:146
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
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_insert()

int mutt_autocrypt_db_gossip_history_insert ( struct Address addr,
struct AutocryptGossipHistory gossip_hist 
)

Insert a gossip history into the Autocrypt database.

Parameters
addrEmail Address
gossip_histGossip history to insert
Return values
0Success
-1Error

Definition at line 856 of file db.c.

858{
859 int rc = -1;
860
861 struct Address *norm_addr = copy_normalize_addr(addr);
862
864 {
865 if (sqlite3_prepare_v3(AutocryptDB,
866 "INSERT INTO gossip_history "
867 "(peer_email_addr, "
868 "sender_email_addr, "
869 "email_msgid, "
870 "timestamp, "
871 "gossip_keydata) "
872 "VALUES (?, ?, ?, ?, ?);",
873 -1, SQLITE_PREPARE_PERSISTENT,
874 &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
875 {
876 goto cleanup;
877 }
878 }
879
880 if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, buf_string(norm_addr->mailbox),
881 -1, SQLITE_STATIC) != SQLITE_OK)
882 {
883 goto cleanup;
884 }
885 if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
886 -1, SQLITE_STATIC) != SQLITE_OK)
887 {
888 if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
889 -1, SQLITE_STATIC) != SQLITE_OK)
890 {
891 goto cleanup;
892 }
893 }
894 if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
895 goto cleanup;
896 if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
897 -1, SQLITE_STATIC) != SQLITE_OK)
898 {
899 goto cleanup;
900 }
901
902 if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
903 goto cleanup;
904
905 rc = 0;
906
907cleanup:
908 mutt_addr_free(&norm_addr);
909 sqlite3_reset(GossipHistoryInsertStmt);
910 return rc;
911}
sqlite3_int64 timestamp
Timestamp of sender's email.
Definition: lib.h:149
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_new()

struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new ( void  )

Create a new AutocryptGossipHistory.

Return values
ptrNew AutocryptGossipHistory

Definition at line 827 of file db.c.

828{
829 return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
830}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_init()

int mutt_autocrypt_db_init ( bool  can_create)

Initialise the Autocrypt SQLite database.

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

Definition at line 80 of file db.c.

81{
82 int rc = -1;
83
84 if (AutocryptDB)
85 return 0;
86
87 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
88 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
89 if (!c_autocrypt || !c_autocrypt_dir)
90 return -1;
91
92 struct Buffer *db_path = buf_pool_get();
93 buf_concat_path(db_path, c_autocrypt_dir, "autocrypt.db");
94
95 struct stat st = { 0 };
96 if (stat(buf_string(db_path), &st) == 0)
97 {
98 if (sqlite3_open_v2(buf_string(db_path), &AutocryptDB, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
99 {
100 /* L10N: Error message if autocrypt couldn't open the SQLite database
101 for some reason. The %s is the full path of the database file. */
102 mutt_error(_("Unable to open autocrypt database %s"), buf_string(db_path));
103 goto cleanup;
104 }
105
107 goto cleanup;
108 }
109 else
110 {
111 if (!can_create)
112 goto cleanup;
113 if (autocrypt_db_create(buf_string(db_path)))
114 goto cleanup;
115 /* Don't abort the whole init process because account creation failed */
118 }
119
120 rc = 0;
121
122cleanup:
123 buf_pool_release(&db_path);
124 return rc;
125}
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition: db.c:61
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition: schema.c:106
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:143
void mutt_autocrypt_scan_mailboxes(void)
Scan mailboxes for Autocrypt headers.
Definition: autocrypt.c:914
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:484
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_normalize_addr()

void mutt_autocrypt_db_normalize_addr ( struct Address a)

Normalise an Email Address.

Parameters
aAddress to normalise

Definition at line 165 of file db.c.

166{
168 buf_lower(a->mailbox);
170}
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1341
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1264
void buf_lower(struct Buffer *buf)
Sets a buffer to lowercase.
Definition: buffer.c:701
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_normalize_addrlist()

void mutt_autocrypt_db_normalize_addrlist ( struct AddressList *  al)

Normalise a list of Email Addresses.

Parameters
alList of Addresses to normalise

Definition at line 176 of file db.c.

177{
179
180 struct Address *np = NULL;
181 TAILQ_FOREACH(np, al, entries)
182 {
183 buf_lower(np->mailbox);
184 }
185
186 mutt_addrlist_to_intl(al, NULL);
187}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition: address.c:1379
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1294
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_free()

void mutt_autocrypt_db_peer_free ( struct AutocryptPeer **  ptr)

Free an AutocryptPeer.

Parameters
ptrAutocryptPeer to free

Definition at line 534 of file db.c.

535{
536 if (!ptr || !*ptr)
537 return;
538
539 struct AutocryptPeer *peer = *ptr;
540 FREE(&peer->email_addr);
541 FREE(&peer->keyid);
542 FREE(&peer->keydata);
543 FREE(&peer->gossip_keyid);
544 FREE(&peer->gossip_keydata);
545 FREE(ptr);
546}
Autocrypt peer.
Definition: lib.h:118
char * gossip_keydata
Gossip Key data.
Definition: lib.h:127
char * keyid
PGP Key id.
Definition: lib.h:122
char * gossip_keyid
Gossip Key id.
Definition: lib.h:126
char * keydata
PGP Key data.
Definition: lib.h:123
char * email_addr
Email address.
Definition: lib.h:119
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_get()

int mutt_autocrypt_db_peer_get ( struct Address addr,
struct AutocryptPeer **  peer 
)

Get peer info from the Autocrypt database.

Parameters
[in]addrEmail Address to look up
[out]peerMatching Autocrypt Peer
Return values
0Success, no matches
1Success, a match
-1Error

Definition at line 556 of file db.c.

557{
558 int rc = -1;
559
560 struct Address *norm_addr = copy_normalize_addr(addr);
561 *peer = NULL;
562
563 if (!PeerGetStmt)
564 {
565 if (sqlite3_prepare_v3(AutocryptDB,
566 "SELECT "
567 "email_addr, "
568 "last_seen, "
569 "autocrypt_timestamp, "
570 "keyid, "
571 "keydata, "
572 "prefer_encrypt, "
573 "gossip_timestamp, "
574 "gossip_keyid, "
575 "gossip_keydata "
576 "FROM peer "
577 "WHERE email_addr = ?",
578 -1, SQLITE_PREPARE_PERSISTENT, &PeerGetStmt, NULL) != SQLITE_OK)
579 {
580 goto cleanup;
581 }
582 }
583
584 if (sqlite3_bind_text(PeerGetStmt, 1, buf_string(norm_addr->mailbox), -1,
585 SQLITE_STATIC) != SQLITE_OK)
586 {
587 goto cleanup;
588 }
589
590 int result = sqlite3_step(PeerGetStmt);
591 if (result != SQLITE_ROW)
592 {
593 if (result == SQLITE_DONE)
594 rc = 0;
595 goto cleanup;
596 }
597
599 (*peer)->email_addr = strdup_column_text(PeerGetStmt, 0);
600 (*peer)->last_seen = sqlite3_column_int64(PeerGetStmt, 1);
601 (*peer)->autocrypt_timestamp = sqlite3_column_int64(PeerGetStmt, 2);
602 (*peer)->keyid = strdup_column_text(PeerGetStmt, 3);
603 (*peer)->keydata = strdup_column_text(PeerGetStmt, 4);
604 (*peer)->prefer_encrypt = sqlite3_column_int(PeerGetStmt, 5);
605 (*peer)->gossip_timestamp = sqlite3_column_int64(PeerGetStmt, 6);
606 (*peer)->gossip_keyid = strdup_column_text(PeerGetStmt, 7);
607 (*peer)->gossip_keydata = strdup_column_text(PeerGetStmt, 8);
608
609 rc = 1;
610
611cleanup:
612 mutt_addr_free(&norm_addr);
613 sqlite3_reset(PeerGetStmt);
614 return rc;
615}
struct AutocryptPeer * mutt_autocrypt_db_peer_new(void)
Create a new AutocryptPeer.
Definition: db.c:525
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_free()

void mutt_autocrypt_db_peer_history_free ( struct AutocryptPeerHistory **  ptr)

Free an AutocryptPeerHistory.

Parameters
ptrAutocryptPeerHistory to free

Definition at line 755 of file db.c.

756{
757 if (!ptr || !*ptr)
758 return;
759
760 struct AutocryptPeerHistory *ph = *ptr;
761 FREE(&ph->peer_email_addr);
762 FREE(&ph->email_msgid);
763 FREE(&ph->keydata);
764 FREE(ptr);
765}
Autocrypt peer history.
Definition: lib.h:134
char * peer_email_addr
Email address of the peer.
Definition: lib.h:135
char * email_msgid
Message id of the email.
Definition: lib.h:136
char * keydata
PGP Key data.
Definition: lib.h:138
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_insert()

int mutt_autocrypt_db_peer_history_insert ( struct Address addr,
struct AutocryptPeerHistory peerhist 
)

Insert peer history into the Autocrypt database.

Parameters
addrEmail Address
peerhistPeer history to insert
Return values
0Success
-1Error

Definition at line 774 of file db.c.

776{
777 int rc = -1;
778
779 struct Address *norm_addr = copy_normalize_addr(addr);
780
782 {
783 if (sqlite3_prepare_v3(AutocryptDB,
784 "INSERT INTO peer_history "
785 "(peer_email_addr, "
786 "email_msgid, "
787 "timestamp, "
788 "keydata) "
789 "VALUES (?, ?, ?, ?);",
790 -1, SQLITE_PREPARE_PERSISTENT,
791 &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
792 {
793 goto cleanup;
794 }
795 }
796
797 if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, buf_string(norm_addr->mailbox),
798 -1, SQLITE_STATIC) != SQLITE_OK)
799 {
800 goto cleanup;
801 }
802 if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
803 SQLITE_STATIC) != SQLITE_OK)
804 {
805 goto cleanup;
806 }
807 if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
808 goto cleanup;
809 if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
810 goto cleanup;
811
812 if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
813 goto cleanup;
814
815 rc = 0;
816
817cleanup:
818 mutt_addr_free(&norm_addr);
819 sqlite3_reset(PeerHistoryInsertStmt);
820 return rc;
821}
sqlite3_int64 timestamp
Timestamp of email.
Definition: lib.h:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_new()

struct AutocryptPeerHistory * mutt_autocrypt_db_peer_history_new ( void  )

Create a new AutocryptPeerHistory.

Return values
ptrNew AutocryptPeerHistory

Definition at line 746 of file db.c.

747{
748 return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
749}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_insert()

int mutt_autocrypt_db_peer_insert ( struct Address addr,
struct AutocryptPeer peer 
)

Insert a peer into the Autocrypt database.

Parameters
addrEmail Address
peerAutocryptPeer to insert
Return values
0Success
-1Error

Definition at line 624 of file db.c.

625{
626 int rc = -1;
627 struct Address *norm_addr = NULL;
628
629 norm_addr = copy_normalize_addr(addr);
630
631 if (!PeerInsertStmt)
632 {
633 if (sqlite3_prepare_v3(AutocryptDB,
634 "INSERT INTO peer "
635 "(email_addr, "
636 "last_seen, "
637 "autocrypt_timestamp, "
638 "keyid, "
639 "keydata, "
640 "prefer_encrypt, "
641 "gossip_timestamp, "
642 "gossip_keyid, "
643 "gossip_keydata) "
644 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
645 -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
646 {
647 goto cleanup;
648 }
649 }
650
651 if (sqlite3_bind_text(PeerInsertStmt, 1, buf_string(norm_addr->mailbox), -1,
652 SQLITE_STATIC) != SQLITE_OK)
653 {
654 goto cleanup;
655 }
656 if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
657 goto cleanup;
658 if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
659 goto cleanup;
660 if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
661 goto cleanup;
662 if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
663 goto cleanup;
664 if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
665 goto cleanup;
666 if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
667 goto cleanup;
668 if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
669 goto cleanup;
670 if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
671 goto cleanup;
672
673 if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
674 goto cleanup;
675
676 rc = 0;
677
678cleanup:
679 mutt_addr_free(&norm_addr);
680 sqlite3_reset(PeerInsertStmt);
681 return rc;
682}
sqlite3_int64 autocrypt_timestamp
When the email was sent.
Definition: lib.h:121
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
sqlite3_int64 gossip_timestamp
Timestamp of Gossip header.
Definition: lib.h:125
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_new()

struct AutocryptPeer * mutt_autocrypt_db_peer_new ( void  )

Create a new AutocryptPeer.

Return values
ptrNew AutocryptPeer

Definition at line 525 of file db.c.

526{
527 return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
528}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_update()

int mutt_autocrypt_db_peer_update ( struct AutocryptPeer peer)

Update the peer info in an Autocrypt database.

Parameters
peerAutocryptPeer to update
Return values
0Success
-1Error

Definition at line 690 of file db.c.

691{
692 int rc = -1;
693
694 if (!PeerUpdateStmt)
695 {
696 if (sqlite3_prepare_v3(AutocryptDB,
697 "UPDATE peer SET "
698 "last_seen = ?, "
699 "autocrypt_timestamp = ?, "
700 "keyid = ?, "
701 "keydata = ?, "
702 "prefer_encrypt = ?, "
703 "gossip_timestamp = ?, "
704 "gossip_keyid = ?, "
705 "gossip_keydata = ? "
706 "WHERE email_addr = ?;",
707 -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
708 {
709 goto cleanup;
710 }
711 }
712
713 if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
714 goto cleanup;
715 if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
716 goto cleanup;
717 if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
718 goto cleanup;
719 if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
720 goto cleanup;
721 if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
722 goto cleanup;
723 if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
724 goto cleanup;
725 if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
726 goto cleanup;
727 if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
728 goto cleanup;
729 if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
730 goto cleanup;
731
732 if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
733 goto cleanup;
734
735 rc = 0;
736
737cleanup:
738 sqlite3_reset(PeerUpdateStmt);
739 return rc;
740}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_schema_init()

int mutt_autocrypt_schema_init ( void  )

Set up an Autocrypt database.

Return values
0Success
-1Error

Definition at line 40 of file schema.c.

41{
42 char *errmsg = NULL;
43
44 const char *schema = "BEGIN TRANSACTION; "
45
46 "CREATE TABLE account ("
47 "email_addr text primary key not null, "
48 "keyid text, "
49 "keydata text, "
50 "prefer_encrypt int, "
51 "enabled int);"
52
53 "CREATE TABLE peer ("
54 "email_addr text primary key not null, "
55 "last_seen int, "
56 "autocrypt_timestamp int, "
57 "keyid text, "
58 "keydata text, "
59 "prefer_encrypt int, "
60 "gossip_timestamp int, "
61 "gossip_keyid text, "
62 "gossip_keydata text);"
63
64 "CREATE TABLE peer_history ("
65 "peer_email_addr text not null, "
66 "email_msgid text, "
67 "timestamp int, "
68 "keydata text);"
69
70 "CREATE INDEX peer_history_email "
71 "ON peer_history ("
72 "peer_email_addr);"
73
74 "CREATE TABLE gossip_history ("
75 "peer_email_addr text not null, "
76 "sender_email_addr text, "
77 "email_msgid text, "
78 "timestamp int, "
79 "gossip_keydata text);"
80
81 "CREATE INDEX gossip_history_email "
82 "ON gossip_history ("
83 "peer_email_addr);"
84
85 "CREATE TABLE schema ("
86 "version int);"
87
88 "INSERT into schema (version) values (1);"
89
90 "COMMIT TRANSACTION";
91
92 if (sqlite3_exec(AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
93 {
94 mutt_debug(LL_DEBUG1, "mutt_autocrypt_schema_init() returned %s\n", errmsg);
95 sqlite3_free(errmsg);
96 return -1;
97 }
98 return 0;
99}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
+ Here is the caller graph for this function:

◆ mutt_autocrypt_schema_update()

int mutt_autocrypt_schema_update ( void  )

Update the version number of the Autocrypt database schema.

Return values
0Success
-1Error

Definition at line 106 of file schema.c.

107{
108 sqlite3_stmt *stmt = NULL;
109 int rc = -1;
110
111 if (sqlite3_prepare_v2(AutocryptDB, "SELECT version FROM schema;", -1, &stmt, NULL) != SQLITE_OK)
112 goto cleanup;
113
114 if (sqlite3_step(stmt) != SQLITE_ROW)
115 goto cleanup;
116
117 int version = sqlite3_column_int(stmt, 0);
118
119 if (version > 1)
120 {
121 /* L10N: The autocrypt database keeps track of schema version numbers.
122 This error occurs if the version number is too high.
123 Presumably because this is an old version of NeoMutt and the
124 database was upgraded by a future version. */
125 mutt_error(_("Autocrypt database version is too new"));
126 goto cleanup;
127 }
128
129 /* TODO: schema version upgrades go here.
130 * Bump one by one, each update inside a transaction. */
131
132 rc = 0;
133
134cleanup:
135 sqlite3_finalize(stmt);
136 return rc;
137}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_create_key()

int mutt_autocrypt_gpgme_create_key ( struct Address addr,
struct Buffer keyid,
struct Buffer keydata 
)

Create a GPGME key.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 155 of file gpgme.c.

157{
158 int rc = -1;
159 gpgme_ctx_t ctx = NULL;
160 gpgme_genkey_result_t keyresult = NULL;
161 gpgme_key_t primary_key = NULL;
162 struct Buffer *buf = buf_pool_get();
163
164 /* GPGME says addresses should not be in idna form */
165 struct Address *copy = mutt_addr_copy(addr);
166 mutt_addr_to_local(copy);
167 mutt_addr_write(buf, copy, false);
168 mutt_addr_free(&copy);
169
170 if (create_gpgme_context(&ctx))
171 goto cleanup;
172
173 /* L10N: Message displayed just before a GPG key is generated for a created
174 autocrypt account. */
175 mutt_message(_("Generating autocrypt key..."));
176
177 /* Primary key */
178 gpgme_error_t err = gpgme_op_createkey(ctx, buf_string(buf), "ed25519", 0, 0, NULL,
179 GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE |
180 GPGME_CREATE_NOEXPIRE);
181 if (err)
182 {
183 /* L10N: GPGME was unable to generate a key for some reason.
184 %s is the error message returned by GPGME. */
185 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
186 goto cleanup;
187 }
188 keyresult = gpgme_op_genkey_result(ctx);
189 if (!keyresult->fpr)
190 goto cleanup;
191 buf_strcpy(keyid, keyresult->fpr);
192 mutt_debug(LL_DEBUG1, "Generated key with id %s\n", buf_string(keyid));
193
194 /* Get gpgme_key_t to create the secondary key and export keydata */
195 err = gpgme_get_key(ctx, buf_string(keyid), &primary_key, 0);
196 if (err)
197 goto cleanup;
198
199 /* Secondary key */
200 err = gpgme_op_createsubkey(ctx, primary_key, "cv25519", 0, 0,
201 GPGME_CREATE_NOPASSWD | GPGME_CREATE_NOEXPIRE);
202 if (err)
203 {
204 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
205 goto cleanup;
206 }
207
208 /* get keydata */
209 if (export_keydata(ctx, primary_key, keydata))
210 goto cleanup;
211 mutt_debug(LL_DEBUG1, "key has keydata *%s*\n", buf_string(keydata));
212
213 rc = 0;
214
215cleanup:
216 gpgme_key_unref(primary_key);
217 gpgme_release(ctx);
218 buf_pool_release(&buf);
219 return rc;
220}
size_t mutt_addr_write(struct Buffer *buf, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1047
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:407
static int create_gpgme_context(gpgme_ctx_t *ctx)
Create a GPGME context.
Definition: gpgme.c:48
static int export_keydata(gpgme_ctx_t ctx, gpgme_key_t key, struct Buffer *keydata)
Export Key data from GPGME into a Buffer.
Definition: gpgme.c:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_import_key()

int mutt_autocrypt_gpgme_import_key ( const char *  keydata,
struct Buffer keyid 
)

Read a key from GPGME.

Parameters
keydataBuffer for key data
keyidBuffer for key id
Return values
0Success
-1Error

Definition at line 317 of file gpgme.c.

318{
319 int rc = -1;
320 gpgme_ctx_t ctx = NULL;
321 gpgme_data_t dh = NULL;
322
323 if (create_gpgme_context(&ctx))
324 goto cleanup;
325
326 struct Buffer *raw_keydata = buf_pool_get();
327 if (!mutt_b64_buffer_decode(raw_keydata, keydata))
328 goto cleanup;
329
330 if (gpgme_data_new_from_mem(&dh, buf_string(raw_keydata), buf_len(raw_keydata), 0))
331 {
332 goto cleanup;
333 }
334
335 if (gpgme_op_import(ctx, dh))
336 goto cleanup;
337
338 gpgme_import_result_t result = gpgme_op_import_result(ctx);
339 if (!result->imports || !result->imports->fpr)
340 goto cleanup;
341 buf_strcpy(keyid, result->imports->fpr);
342
343 rc = 0;
344
345cleanup:
346 gpgme_data_release(dh);
347 gpgme_release(ctx);
348 buf_pool_release(&raw_keydata);
349 return rc;
350}
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:217
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:466
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_init()

int mutt_autocrypt_gpgme_init ( void  )

Initialise GPGME.

Return values
0Always

Definition at line 67 of file gpgme.c.

68{
70 return 0;
71}
void pgp_gpgme_init(void)
Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3751
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_is_valid_key()

bool mutt_autocrypt_gpgme_is_valid_key ( const char *  keyid)

Is a key id valid?

Parameters
keyidKey id to check
Return values
trueKey id is valid

Definition at line 357 of file gpgme.c.

358{
359 bool rc = false;
360 gpgme_ctx_t ctx = NULL;
361 gpgme_key_t key = NULL;
362
363 if (!keyid)
364 return false;
365
366 if (create_gpgme_context(&ctx))
367 goto cleanup;
368
369 if (gpgme_get_key(ctx, keyid, &key, 0))
370 goto cleanup;
371
372 rc = true;
373 if (key->revoked || key->expired || key->disabled || key->invalid || !key->can_encrypt)
374 rc = false;
375
376cleanup:
377 gpgme_key_unref(key);
378 gpgme_release(ctx);
379 return rc;
380}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_key()

int mutt_autocrypt_gpgme_select_key ( struct Buffer keyid,
struct Buffer keydata 
)

Select a Autocrypt key.

Parameters
[in]keyidKey id to select
[out]keydataBuffer for resulting Key data
Return values
0Success
-1Error

Definition at line 229 of file gpgme.c.

230{
231 int rc = -1;
232 gpgme_ctx_t ctx = NULL;
233 gpgme_key_t key = NULL;
234
235 OptAutocryptGpgme = true;
237 goto cleanup;
238
239 if (create_gpgme_context(&ctx))
240 goto cleanup;
241
242 if (gpgme_get_key(ctx, buf_string(keyid), &key, 0))
243 goto cleanup;
244
245 if (key->revoked || key->expired || key->disabled || key->invalid ||
246 !key->can_encrypt || !key->can_sign)
247 {
248 /* L10N: After selecting a key for an autocrypt account,
249 this is displayed if the key was revoked/expired/disabled/invalid
250 or can't be used for both signing and encryption.
251 %s is the key fingerprint. */
252 mutt_error(_("The key %s is not usable for autocrypt"), buf_string(keyid));
253 goto cleanup;
254 }
255
256 if (export_keydata(ctx, key, keydata))
257 goto cleanup;
258
259 rc = 0;
260
261cleanup:
262 OptAutocryptGpgme = false;
263 gpgme_key_unref(key);
264 gpgme_release(ctx);
265 return rc;
266}
int mutt_gpgme_select_secret_key(struct Buffer *keyid)
Select a private Autocrypt key for a new account.
Definition: crypt_gpgme.c:3570
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: globals.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_or_create_key()

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.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 276 of file gpgme.c.

278{
279 int rc = -1;
280
281 /* L10N: During autocrypt account creation, this prompt asks the
282 user whether they want to create a new GPG key for the account,
283 or select an existing account from the keyring. */
284 const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
285 /* L10N: The letters corresponding to the
286 "(c)reate new, or (s)elect existing GPG key?" prompt. */
287 const char *letters = _("cs");
288
289 int choice = mw_multi_choice(prompt, letters);
290 switch (choice)
291 {
292 case 2: /* select existing */
293 rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
294 if (rc == 0)
295 break;
296
297 /* L10N: During autocrypt account creation, if selecting an existing key fails
298 for some reason, we prompt to see if they want to create a key instead. */
299 if (query_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
300 break;
301 /* fallthrough */
302
303 case 1: /* create new */
304 rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
305 }
306
307 return rc;
308}
int mutt_autocrypt_gpgme_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Create a GPGME key.
Definition: gpgme.c:155
int mutt_autocrypt_gpgme_select_key(struct Buffer *keyid, struct Buffer *keydata)
Select a Autocrypt key.
Definition: gpgme.c:229
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ populate_menu()

bool populate_menu ( struct Menu menu)

Add the Autocrypt data to a Menu.

Parameters
menuMenu to populate
Return values
trueSuccess

Definition at line 213 of file dlg_autocrypt.c.

214{
215 // Clear out any existing data
216 autocrypt_menu_free(menu, &menu->mdata);
217 menu->max = 0;
218
219 struct AutocryptAccount **accounts = NULL;
220 int num_accounts = 0;
221
222 if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
223 return false;
224
225 struct AccountEntry *entries = mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
226 menu->mdata = entries;
228 menu->max = num_accounts;
229
230 for (int i = 0; i < num_accounts; i++)
231 {
232 entries[i].num = i + 1;
233 /* note: we are transferring the account pointer to the entries
234 * array, and freeing the accounts array below. the account
235 * will be freed in autocrypt_menu_free(). */
236 entries[i].account = accounts[i];
237
238 entries[i].addr = mutt_addr_new();
239 entries[i].addr->mailbox = buf_new(accounts[i]->email_addr);
240 mutt_addr_to_local(entries[i].addr);
241 }
242 FREE(&accounts);
243
245 return true;
246}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:399
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:460
static void autocrypt_menu_free(struct Menu *menu, void **ptr)
Free the Autocrypt account Menu - Implements Menu::mdata_free() -.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:180
An entry in the Autocrypt account Menu.
Definition: private.h:44
struct Address * addr
Email address associated with the account.
Definition: private.h:47
struct AutocryptAccount * account
Account details.
Definition: private.h:46
int num
Number in the index.
Definition: private.h:45
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:151
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ AutocryptDB

sqlite3* AutocryptDB
extern

Handle to the open Autocrypt database.

Definition at line 53 of file db.c.