NeoMutt  2024-10-02-37-gfa9146
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...
 

Enumerations

enum  ExpandoDataAutocrypt {
  ED_AUT_ENABLED = 1 , ED_AUT_KEYID , ED_AUT_ADDRESS , ED_AUT_NUMBER ,
  ED_AUT_PREFER_ENCRYPT
}
 Expando UIDs for Autocrypt. 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
  • Richard Russon

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.

Enumeration Type Documentation

◆ ExpandoDataAutocrypt

Expando UIDs for Autocrypt.

See also
ED_AUTOCRYPT, ExpandoDomain
Enumerator
ED_AUT_ENABLED 

AutocryptAccount.enabled.

ED_AUT_KEYID 

AutocryptAccount.keyid.

ED_AUT_ADDRESS 

AccountEntry.addr.

ED_AUT_NUMBER 

AccountEntry.num.

ED_AUT_PREFER_ENCRYPT 

AutocryptAccount.prefer_encrypt.

Definition at line 56 of file private.h.

57{
58 ED_AUT_ENABLED = 1,
63};
@ ED_AUT_ADDRESS
AccountEntry.addr.
Definition: private.h:60
@ ED_AUT_KEYID
AutocryptAccount.keyid.
Definition: private.h:59
@ ED_AUT_NUMBER
AccountEntry.num.
Definition: private.h:61
@ ED_AUT_ENABLED
AutocryptAccount.enabled.
Definition: private.h:58
@ ED_AUT_PREFER_ENCRYPT
AutocryptAccount.prefer_encrypt.
Definition: private.h:62

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_ignore_macro(_("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_ignore_macro(_("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:1460
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1480
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:745
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:272
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:266
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:328
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:247
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
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:279
#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_ignore_macro(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question ignoring the macro buffer.
Definition: question.c:340
#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:191
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:107
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:111
String manipulation buffer.
Definition: buffer.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ 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_ignore_macro(_("Scan a mailbox for autocrypt headers?"),
929 MUTT_YES);
930 while (scan == MUTT_YES)
931 {
932 struct Mailbox *m_cur = get_current_mailbox();
933 // L10N: The prompt for a mailbox to scan for Autocrypt: headers
934 if ((!mw_enter_fname(_("Scan mailbox"), folderbuf, true, m_cur, false, NULL,
935 NULL, MUTT_SEL_NO_FLAGS)) &&
936 (!buf_is_empty(folderbuf)))
937 {
938 buf_expand_path_regex(folderbuf, false);
939 struct Mailbox *m_ac = mx_path_resolve(buf_string(folderbuf));
940 /* NOTE: I am purposely *not* executing folder hooks here,
941 * as they can do all sorts of things like push into the getch() buffer.
942 * Authentication should be in account-hooks. */
943 if (mx_mbox_open(m_ac, MUTT_READONLY))
944 {
945 mx_mbox_close(m_ac);
946 }
947 buf_reset(folderbuf);
948 }
949
950 /* L10N: This is the second prompt to see if the user would like
951 to scan more than one mailbox for Autocrypt headers.
952 I'm purposely being extra verbose; asking first then prompting
953 for a mailbox. This is because this is a one-time operation
954 and I don't want them to accidentally ctrl-g and abort it. */
955 scan = query_yesorno_ignore_macro(_("Scan another mailbox for autocrypt headers?"), MUTT_YES);
956 }
957
958#ifdef USE_HCACHE
959 cs_subset_str_native_set(NeoMutt->sub, "header_cache", (intptr_t) old_hdrcache, NULL);
960 old_hdrcache = NULL;
961#endif
962 buf_pool_release(&folderbuf);
963}
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:56
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:168
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:236
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:715
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:122
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:288
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1636
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:598
#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:297
+ 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 428 of file db.c.

429{
430 int rc = -1;
431
433 {
434 if (sqlite3_prepare_v3(AutocryptDB,
435 "DELETE from account "
436 "WHERE email_addr = ?;",
437 -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
438 {
439 goto cleanup;
440 }
441 }
442
443 if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
444 goto cleanup;
445
446 if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
447 goto cleanup;
448
449 rc = 0;
450
451cleanup:
452 sqlite3_reset(AccountDeleteStmt);
453 return rc;
454}
sqlite3 * AutocryptDB
Handle to the open Autocrypt database.
Definition: db.c:56
static sqlite3_stmt * AccountDeleteStmt
Delete an autocrypt account.
Definition: db.c:48
char * email_addr
Email address.
Definition: lib.h:108
+ 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 247 of file db.c.

248{
249 if (!ptr || !*ptr)
250 return;
251
252 struct AutocryptAccount *ac = *ptr;
253 FREE(&ac->email_addr);
254 FREE(&ac->keyid);
255 FREE(&ac->keydata);
256 FREE(ptr);
257}
#define FREE(x)
Definition: memory.h:45
char * keydata
PGP Key data.
Definition: lib.h:110
char * keyid
PGP Key id.
Definition: lib.h:109
+ 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 266 of file db.c.

267{
268 int rc = -1;
269
270 struct Address *norm_addr = copy_normalize_addr(addr);
271 *account = NULL;
272
273 if (!AccountGetStmt)
274 {
275 if (sqlite3_prepare_v3(AutocryptDB,
276 "SELECT "
277 "email_addr, "
278 "keyid, "
279 "keydata, "
280 "prefer_encrypt, "
281 "enabled "
282 "FROM account "
283 "WHERE email_addr = ?",
284 -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
285 {
286 goto cleanup;
287 }
288 }
289
290 if (sqlite3_bind_text(AccountGetStmt, 1, buf_string(norm_addr->mailbox), -1,
291 SQLITE_STATIC) != SQLITE_OK)
292 {
293 goto cleanup;
294 }
295
296 int result = sqlite3_step(AccountGetStmt);
297 if (result != SQLITE_ROW)
298 {
299 if (result == SQLITE_DONE)
300 rc = 0;
301 goto cleanup;
302 }
303
305 (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
306 (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
307 (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
308 (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
309 (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
310
311 rc = 1;
312
313cleanup:
314 mutt_addr_free(&norm_addr);
315 sqlite3_reset(AccountGetStmt);
316 return rc;
317}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:462
struct AutocryptAccount * mutt_autocrypt_db_account_new(void)
Create a new AutocryptAccount.
Definition: db.c:238
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition: db.c:205
static sqlite3_stmt * AccountGetStmt
Get the matching autocrypt accounts.
Definition: db.c:45
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:228
+ 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 463 of file db.c.

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

330{
331 int rc = -1;
332
333 struct Address *norm_addr = copy_normalize_addr(addr);
334
336 {
337 if (sqlite3_prepare_v3(AutocryptDB,
338 "INSERT INTO account "
339 "(email_addr, "
340 "keyid, "
341 "keydata, "
342 "prefer_encrypt, "
343 "enabled) "
344 "VALUES (?, ?, ?, ?, ?);",
345 -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
346 {
347 goto cleanup;
348 }
349 }
350
351 if (sqlite3_bind_text(AccountInsertStmt, 1, buf_string(norm_addr->mailbox),
352 -1, SQLITE_STATIC) != SQLITE_OK)
353 {
354 goto cleanup;
355 }
356 if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
357 goto cleanup;
358 if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
359 goto cleanup;
360 if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
361 goto cleanup;
362 if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
363 goto cleanup;
364
365 if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
366 goto cleanup;
367
368 rc = 0;
369
370cleanup:
371 mutt_addr_free(&norm_addr);
372 sqlite3_reset(AccountInsertStmt);
373 return rc;
374}
static sqlite3_stmt * AccountInsertStmt
Insert a new autocrypt account.
Definition: db.c:46
+ 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 238 of file db.c.

239{
240 return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
241}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
+ 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 382 of file db.c.

383{
384 int rc = -1;
385
387 {
388 if (sqlite3_prepare_v3(AutocryptDB,
389 "UPDATE account SET "
390 "keyid = ?, "
391 "keydata = ?, "
392 "prefer_encrypt = ?, "
393 "enabled = ? "
394 "WHERE email_addr = ?;",
395 -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
396 {
397 goto cleanup;
398 }
399 }
400
401 if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
402 goto cleanup;
403 if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
404 goto cleanup;
405 if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
406 goto cleanup;
407 if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
408 goto cleanup;
409 if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
410 goto cleanup;
411
412 if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
413 goto cleanup;
414
415 rc = 0;
416
417cleanup:
418 sqlite3_reset(AccountUpdateStmt);
419 return rc;
420}
static sqlite3_stmt * AccountUpdateStmt
Update an autocrypt account.
Definition: db.c:47
+ 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 133 of file db.c.

134{
135 if (!AutocryptDB)
136 return;
137
138 sqlite3_finalize(AccountGetStmt);
139 AccountGetStmt = NULL;
140 sqlite3_finalize(AccountInsertStmt);
141 AccountInsertStmt = NULL;
142 sqlite3_finalize(AccountUpdateStmt);
143 AccountUpdateStmt = NULL;
144 sqlite3_finalize(AccountDeleteStmt);
145 AccountDeleteStmt = NULL;
146
147 sqlite3_finalize(PeerGetStmt);
148 PeerGetStmt = NULL;
149 sqlite3_finalize(PeerInsertStmt);
150 PeerInsertStmt = NULL;
151 sqlite3_finalize(PeerUpdateStmt);
152 PeerUpdateStmt = NULL;
153
154 sqlite3_finalize(PeerHistoryInsertStmt);
156
157 sqlite3_finalize(GossipHistoryInsertStmt);
159
160 sqlite3_close_v2(AutocryptDB);
161 AutocryptDB = NULL;
162}
static sqlite3_stmt * PeerUpdateStmt
Update a peer address.
Definition: db.c:51
static sqlite3_stmt * PeerGetStmt
Get the matching peer addresses.
Definition: db.c:49
static sqlite3_stmt * PeerHistoryInsertStmt
Add to the peer history.
Definition: db.c:52
static sqlite3_stmt * GossipHistoryInsertStmt
Add to the gossip history.
Definition: db.c:53
static sqlite3_stmt * PeerInsertStmt
Insert a new peer address.
Definition: db.c:50
+ 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 839 of file db.c.

840{
841 if (!ptr || !*ptr)
842 return;
843
844 struct AutocryptGossipHistory *gh = *ptr;
845 FREE(&gh->peer_email_addr);
847 FREE(&gh->email_msgid);
848 FREE(&gh->gossip_keydata);
849 FREE(ptr);
850}
Autocrypt gossip history.
Definition: lib.h:146
char * peer_email_addr
Email addressof the peer.
Definition: lib.h:147
char * email_msgid
Sender's email's message id.
Definition: lib.h:149
char * sender_email_addr
Sender's email address.
Definition: lib.h:148
char * gossip_keydata
Gossip Key data.
Definition: lib.h:151
+ 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 859 of file db.c.

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

831{
832 return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
833}
+ 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 83 of file db.c.

84{
85 int rc = -1;
86
87 if (AutocryptDB)
88 return 0;
89
90 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
91 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
92 if (!c_autocrypt || !c_autocrypt_dir)
93 return -1;
94
95 struct Buffer *db_path = buf_pool_get();
96 buf_concat_path(db_path, c_autocrypt_dir, "autocrypt.db");
97
98 struct stat st = { 0 };
99 if (stat(buf_string(db_path), &st) == 0)
100 {
101 if (sqlite3_open_v2(buf_string(db_path), &AutocryptDB, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
102 {
103 /* L10N: autocrypt couldn't open the SQLite database.
104 The %s is the full path of the database file. */
105 mutt_error(_("Unable to open autocrypt database %s"), buf_string(db_path));
106 goto cleanup;
107 }
108
110 goto cleanup;
111 }
112 else
113 {
114 if (!can_create)
115 goto cleanup;
116 if (autocrypt_db_create(buf_string(db_path)))
117 goto cleanup;
118 /* Don't abort the whole init process because account creation failed */
121 }
122
123 rc = 0;
124
125cleanup:
126 buf_pool_release(&db_path);
127 return rc;
128}
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition: db.c:64
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition: schema.c:107
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:509
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
+ 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 168 of file db.c.

169{
171 buf_lower(a->mailbox);
173}
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1340
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1263
void buf_lower(struct Buffer *buf)
Sets a buffer to lowercase.
Definition: buffer.c:736
+ 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 179 of file db.c.

180{
182
183 struct Address *np = NULL;
184 TAILQ_FOREACH(np, al, entries)
185 {
186 buf_lower(np->mailbox);
187 }
188
189 mutt_addrlist_to_intl(al, NULL);
190}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition: address.c:1378
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1293
#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 537 of file db.c.

538{
539 if (!ptr || !*ptr)
540 return;
541
542 struct AutocryptPeer *peer = *ptr;
543 FREE(&peer->email_addr);
544 FREE(&peer->keyid);
545 FREE(&peer->keydata);
546 FREE(&peer->gossip_keyid);
547 FREE(&peer->gossip_keydata);
548 FREE(ptr);
549}
Autocrypt peer.
Definition: lib.h:119
char * gossip_keydata
Gossip Key data.
Definition: lib.h:128
char * keyid
PGP Key id.
Definition: lib.h:123
char * gossip_keyid
Gossip Key id.
Definition: lib.h:127
char * keydata
PGP Key data.
Definition: lib.h:124
char * email_addr
Email address.
Definition: lib.h:120
+ 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 559 of file db.c.

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

759{
760 if (!ptr || !*ptr)
761 return;
762
763 struct AutocryptPeerHistory *ph = *ptr;
764 FREE(&ph->peer_email_addr);
765 FREE(&ph->email_msgid);
766 FREE(&ph->keydata);
767 FREE(ptr);
768}
Autocrypt peer history.
Definition: lib.h:135
char * peer_email_addr
Email address of the peer.
Definition: lib.h:136
char * email_msgid
Message id of the email.
Definition: lib.h:137
char * keydata
PGP Key data.
Definition: lib.h:139
+ 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 777 of file db.c.

779{
780 int rc = -1;
781
782 struct Address *norm_addr = copy_normalize_addr(addr);
783
785 {
786 if (sqlite3_prepare_v3(AutocryptDB,
787 "INSERT INTO peer_history "
788 "(peer_email_addr, "
789 "email_msgid, "
790 "timestamp, "
791 "keydata) "
792 "VALUES (?, ?, ?, ?);",
793 -1, SQLITE_PREPARE_PERSISTENT,
794 &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
795 {
796 goto cleanup;
797 }
798 }
799
800 if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, buf_string(norm_addr->mailbox),
801 -1, SQLITE_STATIC) != SQLITE_OK)
802 {
803 goto cleanup;
804 }
805 if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
806 SQLITE_STATIC) != SQLITE_OK)
807 {
808 goto cleanup;
809 }
810 if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
811 goto cleanup;
812 if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
813 goto cleanup;
814
815 if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
816 goto cleanup;
817
818 rc = 0;
819
820cleanup:
821 mutt_addr_free(&norm_addr);
822 sqlite3_reset(PeerHistoryInsertStmt);
823 return rc;
824}
sqlite3_int64 timestamp
Timestamp of email.
Definition: lib.h:138
+ 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 749 of file db.c.

750{
751 return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
752}
+ 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 627 of file db.c.

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

529{
530 return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
531}
+ 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 693 of file db.c.

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

42{
43 char *errmsg = NULL;
44
45 const char *schema = "BEGIN TRANSACTION; "
46
47 "CREATE TABLE account ("
48 "email_addr text primary key not null, "
49 "keyid text, "
50 "keydata text, "
51 "prefer_encrypt int, "
52 "enabled int);"
53
54 "CREATE TABLE peer ("
55 "email_addr text primary key not null, "
56 "last_seen int, "
57 "autocrypt_timestamp int, "
58 "keyid text, "
59 "keydata text, "
60 "prefer_encrypt int, "
61 "gossip_timestamp int, "
62 "gossip_keyid text, "
63 "gossip_keydata text);"
64
65 "CREATE TABLE peer_history ("
66 "peer_email_addr text not null, "
67 "email_msgid text, "
68 "timestamp int, "
69 "keydata text);"
70
71 "CREATE INDEX peer_history_email "
72 "ON peer_history ("
73 "peer_email_addr);"
74
75 "CREATE TABLE gossip_history ("
76 "peer_email_addr text not null, "
77 "sender_email_addr text, "
78 "email_msgid text, "
79 "timestamp int, "
80 "gossip_keydata text);"
81
82 "CREATE INDEX gossip_history_email "
83 "ON gossip_history ("
84 "peer_email_addr);"
85
86 "CREATE TABLE schema ("
87 "version int);"
88
89 "INSERT into schema (version) values (1);"
90
91 "COMMIT TRANSACTION";
92
93 if (sqlite3_exec(AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
94 {
95 mutt_debug(LL_DEBUG1, "mutt_autocrypt_schema_init() returned %s\n", errmsg);
96 sqlite3_free(errmsg);
97 return -1;
98 }
99 return 0;
100}
#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 107 of file schema.c.

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

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

321{
322 int rc = -1;
323 gpgme_ctx_t ctx = NULL;
324 gpgme_data_t dh = NULL;
325 struct Buffer *raw_keydata = NULL;
326
327 if (create_gpgme_context(&ctx))
328 goto cleanup;
329
330 raw_keydata = buf_pool_get();
331 if (!mutt_b64_buffer_decode(raw_keydata, keydata))
332 goto cleanup;
333
334 if (gpgme_data_new_from_mem(&dh, buf_string(raw_keydata), buf_len(raw_keydata), 0))
335 {
336 goto cleanup;
337 }
338
339 if (gpgme_op_import(ctx, dh))
340 goto cleanup;
341
342 gpgme_import_result_t result = gpgme_op_import_result(ctx);
343 if (!result->imports || !result->imports->fpr)
344 goto cleanup;
345 buf_strcpy(keyid, result->imports->fpr);
346
347 rc = 0;
348
349cleanup:
350 gpgme_data_release(dh);
351 gpgme_release(ctx);
352 buf_pool_release(&raw_keydata);
353 return rc;
354}
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:216
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
+ 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 70 of file gpgme.c.

71{
73 return 0;
74}
void pgp_gpgme_init(void)
Initialise the crypto module - Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3865
+ 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 361 of file gpgme.c.

362{
363 bool rc = false;
364 gpgme_ctx_t ctx = NULL;
365 gpgme_key_t key = NULL;
366
367 if (!keyid)
368 return false;
369
370 if (create_gpgme_context(&ctx))
371 goto cleanup;
372
373 if (gpgme_get_key(ctx, keyid, &key, 0))
374 goto cleanup;
375
376 rc = true;
377 if (key->revoked || key->expired || key->disabled || key->invalid || !key->can_encrypt)
378 rc = false;
379
380cleanup:
381 gpgme_key_unref(key);
382 gpgme_release(ctx);
383 return rc;
384}
+ 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 232 of file gpgme.c.

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

281{
282 int rc = -1;
283
284 /* L10N: During autocrypt account creation, this prompt asks the
285 user whether they want to create a new GPG key for the account,
286 or select an existing account from the keyring. */
287 const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
288 /* L10N: The letters corresponding to the
289 "(c)reate new, or (s)elect existing GPG key?" prompt. */
290 const char *letters = _("cs");
291
292 int choice = mw_multi_choice(prompt, letters);
293 switch (choice)
294 {
295 case 2: /* select existing */
296 rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
297 if (rc == 0)
298 break;
299
300 /* L10N: During autocrypt account creation, if selecting an existing key fails
301 for some reason, we prompt to see if they want to create a key instead. */
302 if (query_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
303 break;
305
306 case 1: /* create new */
307 rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
308 }
309
310 return rc;
311}
int mutt_autocrypt_gpgme_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Create a GPGME key.
Definition: gpgme.c:158
int mutt_autocrypt_gpgme_select_key(struct Buffer *keyid, struct Buffer *keydata)
Select a Autocrypt key.
Definition: gpgme.c:232
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:63
#define FALLTHROUGH
Definition: lib.h:111
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:327
+ 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 234 of file dlg_autocrypt.c.

235{
236 // Clear out any existing data
237 autocrypt_menu_free(menu, &menu->mdata);
238 menu->max = 0;
239
240 struct AutocryptAccount **accounts = NULL;
241 int num_accounts = 0;
242
243 if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
244 return false;
245
246 struct AccountEntry *entries = mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
247 menu->mdata = entries;
249 menu->max = num_accounts;
250
251 for (int i = 0; i < num_accounts; i++)
252 {
253 entries[i].num = i + 1;
254 /* note: we are transferring the account pointer to the entries
255 * array, and freeing the accounts array below. the account
256 * will be freed in autocrypt_menu_free(). */
257 entries[i].account = accounts[i];
258
259 entries[i].addr = mutt_addr_new();
260 entries[i].addr->mailbox = buf_new(accounts[i]->email_addr);
261 mutt_addr_to_local(entries[i].addr);
262 }
263 FREE(&accounts);
264
266 return true;
267}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:401
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:463
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:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
An entry in the Autocrypt account Menu.
Definition: private.h:45
struct Address * addr
Email address associated with the account.
Definition: private.h:48
struct AutocryptAccount * account
Account details.
Definition: private.h:47
int num
Number in the index.
Definition: private.h:46
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:161
void * mdata
Private data.
Definition: lib.h:147
int max
Number of entries in the menu.
Definition: lib.h:81
+ 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 56 of file db.c.