NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
db.c File Reference

Autocrypt database handling. More...

#include "config.h"
#include <stddef.h>
#include <sqlite3.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "lib.h"
+ Include dependency graph for db.c:

Go to the source code of this file.

Functions

static int autocrypt_db_create (const char *db_path)
 Create an Autocrypt SQLite database.
 
int mutt_autocrypt_db_init (bool can_create)
 Initialise the Autocrypt SQLite database.
 
void mutt_autocrypt_db_close (void)
 Close the Autocrypt SQLite database connection.
 
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.
 
static struct Addresscopy_normalize_addr (struct Address *addr)
 Copy a normalised Email Address.
 
static char * strdup_column_text (sqlite3_stmt *stmt, int index)
 Copy a string from the database.
 
struct AutocryptAccountmutt_autocrypt_db_account_new (void)
 Create a new AutocryptAccount.
 
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_insert (struct Address *addr, const char *keyid, const char *keydata, bool prefer_encrypt)
 Insert an Account into the Autocrypt database.
 
int mutt_autocrypt_db_account_update (struct AutocryptAccount *acct)
 Update Account info in the Autocrypt database.
 
int mutt_autocrypt_db_account_delete (struct AutocryptAccount *acct)
 Delete an Account from the Autocrypt database.
 
int mutt_autocrypt_db_account_get_all (struct AutocryptAccount ***accounts, int *num_accounts)
 Get all accounts from an Autocrypt database.
 
struct AutocryptPeermutt_autocrypt_db_peer_new (void)
 Create a new AutocryptPeer.
 
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.
 
int mutt_autocrypt_db_peer_insert (struct Address *addr, struct AutocryptPeer *peer)
 Insert a peer into the Autocrypt database.
 
int mutt_autocrypt_db_peer_update (struct AutocryptPeer *peer)
 Update the peer info in an Autocrypt database.
 
struct AutocryptPeerHistorymutt_autocrypt_db_peer_history_new (void)
 Create a new AutocryptPeerHistory.
 
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 AutocryptGossipHistorymutt_autocrypt_db_gossip_history_new (void)
 Create a new AutocryptGossipHistory.
 
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.
 

Variables

static sqlite3_stmt * AccountGetStmt = NULL
 Get the matching autocrypt accounts.
 
static sqlite3_stmt * AccountInsertStmt = NULL
 Insert a new autocrypt account.
 
static sqlite3_stmt * AccountUpdateStmt = NULL
 Update an autocrypt account.
 
static sqlite3_stmt * AccountDeleteStmt = NULL
 Delete an autocrypt account.
 
static sqlite3_stmt * PeerGetStmt = NULL
 Get the matching peer addresses.
 
static sqlite3_stmt * PeerInsertStmt = NULL
 Insert a new peer address.
 
static sqlite3_stmt * PeerUpdateStmt = NULL
 Update a peer address.
 
static sqlite3_stmt * PeerHistoryInsertStmt = NULL
 Add to the peer history.
 
static sqlite3_stmt * GossipHistoryInsertStmt = NULL
 Add to the gossip history.
 
sqlite3 * AutocryptDB = NULL
 Handle to the open Autocrypt database.
 

Detailed Description

Autocrypt database handling.

Authors
  • Kevin J. McCarthy
  • Richard Russon
  • Pietro Cerutti
  • Anna Figueiredo Gomes

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

Function Documentation

◆ autocrypt_db_create()

static int autocrypt_db_create ( const char *  db_path)
static

Create an Autocrypt SQLite database.

Parameters
db_pathPath to database file
Return values
0Success
-1Error

Definition at line 64 of file db.c.

65{
66 if (sqlite3_open_v2(db_path, &AutocryptDB,
67 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK)
68 {
69 /* L10N: autocrypt couldn't open the SQLite database.
70 The %s is the full path of the database file. */
71 mutt_error(_("Unable to open autocrypt database %s"), db_path);
72 return -1;
73 }
75}
sqlite3 * AutocryptDB
Handle to the open Autocrypt database.
Definition: db.c:56
int mutt_autocrypt_schema_init(void)
Set up an Autocrypt database.
Definition: schema.c:41
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
+ 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:145
void mutt_autocrypt_scan_mailboxes(void)
Scan mailboxes for Autocrypt headers.
Definition: autocrypt.c:916
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:508
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_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
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
String manipulation buffer.
Definition: buffer.h:36
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_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 * AccountInsertStmt
Insert a new autocrypt account.
Definition: db.c:46
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 * AccountUpdateStmt
Update an autocrypt account.
Definition: db.c:47
static sqlite3_stmt * AccountGetStmt
Get the matching autocrypt accounts.
Definition: db.c:45
static sqlite3_stmt * AccountDeleteStmt
Delete an autocrypt account.
Definition: db.c:48
static sqlite3_stmt * PeerInsertStmt
Insert a new peer address.
Definition: db.c:50
+ 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:735
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
+ 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
An email address.
Definition: address.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ copy_normalize_addr()

static struct Address * copy_normalize_addr ( struct Address addr)
static

Copy a normalised Email Address.

Parameters
addrAddress to normalise and copy
Return values
ptrCopy of the Address

The autocrypt spec says email addresses should be normalized to lower case and stored in idna form.

In order to avoid visible changes to addresses in the index, we make a copy of the address before lowercasing it.

Note
The return value must be freed

Definition at line 205 of file db.c.

206{
207 /* NOTE: the db functions expect a single address, so in
208 * this function we copy only the address passed in.
209 *
210 * The normalize_addrlist above is extended to work on a list
211 * because of requirements in autocrypt.c */
212
213 struct Address *norm_addr = mutt_addr_new();
214 buf_copy(norm_addr->mailbox, addr->mailbox);
215 norm_addr->is_intl = addr->is_intl;
216 norm_addr->intl_checked = addr->intl_checked;
217
219 return norm_addr;
220}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:401
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:168
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:600
bool intl_checked
Checked for IDN?
Definition: address.h:41
bool is_intl
International Domain Name.
Definition: address.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ strdup_column_text()

static char * strdup_column_text ( sqlite3_stmt *  stmt,
int  index 
)
static

Copy a string from the database.

Parameters
stmtSQLite database statement
indexDatabase row
Return values
ptrCopy of string

Definition at line 228 of file db.c.

229{
230 const char *val = (const char *) sqlite3_column_text(stmt, index);
231 return mutt_str_dup(val);
232}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ 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:50
Autocrypt account.
Definition: lib.h:107
+ Here is the call graph for this function:
+ 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 * email_addr
Email address.
Definition: lib.h:108
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 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_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}
+ 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}
bool enabled
Is this account enabled.
Definition: lib.h:112
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:111
+ 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}
+ 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_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:247
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
+ 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}
Autocrypt peer.
Definition: lib.h:119
+ 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}
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_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_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_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}
Autocrypt peer history.
Definition: lib.h:135
+ 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}
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_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}
Autocrypt gossip history.
Definition: lib.h:146
+ Here is the call graph for this function:
+ 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}
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:

Variable Documentation

◆ AccountGetStmt

sqlite3_stmt* AccountGetStmt = NULL
static

Get the matching autocrypt accounts.

Definition at line 45 of file db.c.

◆ AccountInsertStmt

sqlite3_stmt* AccountInsertStmt = NULL
static

Insert a new autocrypt account.

Definition at line 46 of file db.c.

◆ AccountUpdateStmt

sqlite3_stmt* AccountUpdateStmt = NULL
static

Update an autocrypt account.

Definition at line 47 of file db.c.

◆ AccountDeleteStmt

sqlite3_stmt* AccountDeleteStmt = NULL
static

Delete an autocrypt account.

Definition at line 48 of file db.c.

◆ PeerGetStmt

sqlite3_stmt* PeerGetStmt = NULL
static

Get the matching peer addresses.

Definition at line 49 of file db.c.

◆ PeerInsertStmt

sqlite3_stmt* PeerInsertStmt = NULL
static

Insert a new peer address.

Definition at line 50 of file db.c.

◆ PeerUpdateStmt

sqlite3_stmt* PeerUpdateStmt = NULL
static

Update a peer address.

Definition at line 51 of file db.c.

◆ PeerHistoryInsertStmt

sqlite3_stmt* PeerHistoryInsertStmt = NULL
static

Add to the peer history.

Definition at line 52 of file db.c.

◆ GossipHistoryInsertStmt

sqlite3_stmt* GossipHistoryInsertStmt = NULL
static

Add to the gossip history.

Definition at line 53 of file db.c.

◆ AutocryptDB

sqlite3* AutocryptDB = NULL

Handle to the open Autocrypt database.

Definition at line 56 of file db.c.