NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
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. More...
 
int mutt_autocrypt_db_init (bool can_create)
 Initialise the Autocrypt SQLite database. More...
 
void mutt_autocrypt_db_close (void)
 Close the Autocrypt SQLite database connection. More...
 
void mutt_autocrypt_db_normalize_addr (struct Address *a)
 Normalise an Email Address. More...
 
void mutt_autocrypt_db_normalize_addrlist (struct AddressList *al)
 Normalise a list of Email Addresses. More...
 
static struct Addresscopy_normalize_addr (struct Address *addr)
 Copy a normalised Email Address. More...
 
static char * strdup_column_text (sqlite3_stmt *stmt, int index)
 Copy a string from the database. More...
 
struct AutocryptAccountmutt_autocrypt_db_account_new (void)
 Create a new AutocryptAccount. More...
 
void mutt_autocrypt_db_account_free (struct AutocryptAccount **ptr)
 Free an AutocryptAccount. More...
 
int mutt_autocrypt_db_account_get (struct Address *addr, struct AutocryptAccount **account)
 Get Autocrypt Account data from the database. More...
 
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. More...
 
int mutt_autocrypt_db_account_update (struct AutocryptAccount *acct)
 Update Account info in the Autocrypt database. More...
 
int mutt_autocrypt_db_account_delete (struct AutocryptAccount *acct)
 Delete an Account from the Autocrypt database. More...
 
int mutt_autocrypt_db_account_get_all (struct AutocryptAccount ***accounts, int *num_accounts)
 Get all accounts from an Autocrypt database. More...
 
struct AutocryptPeermutt_autocrypt_db_peer_new (void)
 Create a new AutocryptPeer. More...
 
void mutt_autocrypt_db_peer_free (struct AutocryptPeer **ptr)
 Free an AutocryptPeer. More...
 
int mutt_autocrypt_db_peer_get (struct Address *addr, struct AutocryptPeer **peer)
 Get peer info from the Autocrypt database. More...
 
int mutt_autocrypt_db_peer_insert (struct Address *addr, struct AutocryptPeer *peer)
 Insert a peer into the Autocrypt database. More...
 
int mutt_autocrypt_db_peer_update (struct AutocryptPeer *peer)
 Update the peer info in an Autocrypt database. More...
 
struct AutocryptPeerHistorymutt_autocrypt_db_peer_history_new (void)
 Create a new AutocryptPeerHistory. More...
 
void mutt_autocrypt_db_peer_history_free (struct AutocryptPeerHistory **ptr)
 Free an AutocryptPeerHistory. More...
 
int mutt_autocrypt_db_peer_history_insert (struct Address *addr, struct AutocryptPeerHistory *peerhist)
 Insert peer history into the Autocrypt database. More...
 
struct AutocryptGossipHistorymutt_autocrypt_db_gossip_history_new (void)
 Create a new AutocryptGossipHistory. More...
 
void mutt_autocrypt_db_gossip_history_free (struct AutocryptGossipHistory **ptr)
 Free an AutocryptGossipHistory. More...
 
int mutt_autocrypt_db_gossip_history_insert (struct Address *addr, struct AutocryptGossipHistory *gossip_hist)
 Insert a gossip history into the Autocrypt database. More...
 

Variables

static sqlite3_stmt * AccountGetStmt
 
static sqlite3_stmt * AccountInsertStmt
 
static sqlite3_stmt * AccountUpdateStmt
 
static sqlite3_stmt * AccountDeleteStmt
 
static sqlite3_stmt * PeerGetStmt
 
static sqlite3_stmt * PeerInsertStmt
 
static sqlite3_stmt * PeerUpdateStmt
 
static sqlite3_stmt * PeerHistoryInsertStmt
 
static sqlite3_stmt * GossipHistoryInsertStmt
 
sqlite3 * AutocryptDB = NULL
 

Detailed Description

Autocrypt database handling.

Authors
  • Kevin J. McCarthy

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file 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 60 of file db.c.

61{
62 if (sqlite3_open_v2(db_path, &AutocryptDB,
63 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK)
64 {
65 /* L10N: s is the path to the database.
66 For some reason sqlite3 failed to open that database file. */
67 mutt_error(_("Unable to open autocrypt database %s"), db_path);
68 return -1;
69 }
71}
sqlite3 * AutocryptDB
Definition: db.c:52
int mutt_autocrypt_schema_init(void)
Set up an Autocrypt database.
Definition: schema.c:40
#define mutt_error(...)
Definition: logging.h:87
#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 79 of file db.c.

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

◆ mutt_autocrypt_db_close()

void mutt_autocrypt_db_close ( void  )

Close the Autocrypt SQLite database connection.

Definition at line 130 of file db.c.

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

◆ mutt_autocrypt_db_normalize_addr()

void mutt_autocrypt_db_normalize_addr ( struct Address a)

Normalise an Email Address.

Parameters
aAddress to normalise

Definition at line 165 of file db.c.

166{
170}
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1351
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1276
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:384
char * 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 176 of file db.c.

177{
179
180 struct Address *np = NULL;
181 TAILQ_FOREACH(np, al, entries)
182 {
184 }
185
186 mutt_addrlist_to_intl(al, NULL);
187}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition: address.c:1388
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1305
#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 202 of file db.c.

203{
204 /* NOTE: the db functions expect a single address, so in
205 * this function we copy only the address passed in.
206 *
207 * The normalize_addrlist above is extended to work on a list
208 * because of requirements in autocrypt.c */
209
210 struct Address *norm_addr = mutt_addr_new();
211 norm_addr->mailbox = mutt_str_dup(addr->mailbox);
212 norm_addr->is_intl = addr->is_intl;
213 norm_addr->intl_checked = addr->intl_checked;
214
216 return norm_addr;
217}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:385
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:165
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
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 225 of file db.c.

226{
227 const char *val = (const char *) sqlite3_column_text(stmt, index);
228 return mutt_str_dup(val);
229}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_new()

struct AutocryptAccount * mutt_autocrypt_db_account_new ( void  )

Create a new AutocryptAccount.

Return values
ptrNew AutocryptAccount

Definition at line 235 of file db.c.

236{
237 return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
238}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Autocrypt account.
Definition: lib.h:106
+ 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 244 of file db.c.

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

◆ mutt_autocrypt_db_account_get()

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

Get Autocrypt Account data from the database.

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

Definition at line 263 of file db.c.

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

◆ mutt_autocrypt_db_account_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 322 of file db.c.

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

374{
375 int rc = -1;
376
378 {
379 if (sqlite3_prepare_v3(AutocryptDB,
380 "UPDATE account SET "
381 "keyid = ?, "
382 "keydata = ?, "
383 "prefer_encrypt = ?, "
384 "enabled = ? "
385 "WHERE email_addr = ?;",
386 -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
387 {
388 goto cleanup;
389 }
390 }
391
392 if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
393 goto cleanup;
394 if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
395 goto cleanup;
396 if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
397 goto cleanup;
398 if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
399 goto cleanup;
400 if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
401 goto cleanup;
402
403 if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
404 goto cleanup;
405
406 rc = 0;
407
408cleanup:
409 sqlite3_reset(AccountUpdateStmt);
410 return rc;
411}
bool enabled
Is this account enabled.
Definition: lib.h:111
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:110
+ 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 419 of file db.c.

420{
421 int rc = -1;
422
424 {
425 if (sqlite3_prepare_v3(AutocryptDB,
426 "DELETE from account "
427 "WHERE email_addr = ?;",
428 -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
429 {
430 goto cleanup;
431 }
432 }
433
434 if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
435 goto cleanup;
436
437 if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
438 goto cleanup;
439
440 rc = 0;
441
442cleanup:
443 sqlite3_reset(AccountDeleteStmt);
444 return rc;
445}
+ 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 454 of file db.c.

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

520{
521 return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
522}
Autocrypt peer.
Definition: lib.h:118
+ 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 528 of file db.c.

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

◆ mutt_autocrypt_db_peer_get()

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

Get peer info from the Autocrypt database.

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

Definition at line 550 of file db.c.

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

616{
617 int rc = -1;
618 struct Address *norm_addr = NULL;
619
620 norm_addr = copy_normalize_addr(addr);
621
622 if (!PeerInsertStmt)
623 {
624 if (sqlite3_prepare_v3(AutocryptDB,
625 "INSERT INTO peer "
626 "(email_addr, "
627 "last_seen, "
628 "autocrypt_timestamp, "
629 "keyid, "
630 "keydata, "
631 "prefer_encrypt, "
632 "gossip_timestamp, "
633 "gossip_keyid, "
634 "gossip_keydata) "
635 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
636 -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
637 {
638 goto cleanup;
639 }
640 }
641
642 if (sqlite3_bind_text(PeerInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
643 goto cleanup;
644 if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
645 goto cleanup;
646 if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
647 goto cleanup;
648 if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
649 goto cleanup;
650 if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
651 goto cleanup;
652 if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
653 goto cleanup;
654 if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
655 goto cleanup;
656 if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
657 goto cleanup;
658 if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
659 goto cleanup;
660
661 if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
662 goto cleanup;
663
664 rc = 0;
665
666cleanup:
667 mutt_addr_free(&norm_addr);
668 sqlite3_reset(PeerInsertStmt);
669 return rc;
670}
sqlite3_int64 autocrypt_timestamp
When the email was sent.
Definition: lib.h:121
sqlite3_int64 last_seen
When was the peer last seen.
Definition: lib.h:120
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:124
sqlite3_int64 gossip_timestamp
Timestamp of Gossip header.
Definition: lib.h:125
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_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 678 of file db.c.

679{
680 int rc = -1;
681
682 if (!PeerUpdateStmt)
683 {
684 if (sqlite3_prepare_v3(AutocryptDB,
685 "UPDATE peer SET "
686 "last_seen = ?, "
687 "autocrypt_timestamp = ?, "
688 "keyid = ?, "
689 "keydata = ?, "
690 "prefer_encrypt = ?, "
691 "gossip_timestamp = ?, "
692 "gossip_keyid = ?, "
693 "gossip_keydata = ? "
694 "WHERE email_addr = ?;",
695 -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
696 {
697 goto cleanup;
698 }
699 }
700
701 if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
702 goto cleanup;
703 if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
704 goto cleanup;
705 if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
706 goto cleanup;
707 if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
708 goto cleanup;
709 if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
710 goto cleanup;
711 if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
712 goto cleanup;
713 if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
714 goto cleanup;
715 if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
716 goto cleanup;
717 if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
718 goto cleanup;
719
720 if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
721 goto cleanup;
722
723 rc = 0;
724
725cleanup:
726 sqlite3_reset(PeerUpdateStmt);
727 return rc;
728}
+ 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 734 of file db.c.

735{
736 return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
737}
Autocrypt peer history.
Definition: lib.h:134
+ 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 743 of file db.c.

744{
745 if (!ptr || !*ptr)
746 return;
747
748 struct AutocryptPeerHistory *ph = *ptr;
749 FREE(&ph->peer_email_addr);
750 FREE(&ph->email_msgid);
751 FREE(&ph->keydata);
752 FREE(ptr);
753}
char * peer_email_addr
Email address of the peer.
Definition: lib.h:135
char * email_msgid
Message id of the email.
Definition: lib.h:136
char * keydata
PGP Key data.
Definition: lib.h:138
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_insert()

int mutt_autocrypt_db_peer_history_insert ( struct Address addr,
struct AutocryptPeerHistory peerhist 
)

Insert peer history into the Autocrypt database.

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

Definition at line 762 of file db.c.

764{
765 int rc = -1;
766
767 struct Address *norm_addr = copy_normalize_addr(addr);
768
770 {
771 if (sqlite3_prepare_v3(AutocryptDB,
772 "INSERT INTO peer_history "
773 "(peer_email_addr, "
774 "email_msgid, "
775 "timestamp, "
776 "keydata) "
777 "VALUES (?, ?, ?, ?);",
778 -1, SQLITE_PREPARE_PERSISTENT,
779 &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
780 {
781 goto cleanup;
782 }
783 }
784
785 if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
786 goto cleanup;
787 if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
788 SQLITE_STATIC) != SQLITE_OK)
789 {
790 goto cleanup;
791 }
792 if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
793 goto cleanup;
794 if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
795 goto cleanup;
796
797 if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
798 goto cleanup;
799
800 rc = 0;
801
802cleanup:
803 mutt_addr_free(&norm_addr);
804 sqlite3_reset(PeerHistoryInsertStmt);
805 return rc;
806}
sqlite3_int64 timestamp
Timestamp of email.
Definition: lib.h:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_new()

struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new ( void  )

Create a new AutocryptGossipHistory.

Return values
ptrNew AutocryptGossipHistory

Definition at line 812 of file db.c.

813{
814 return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
815}
Autocrypt gossip history.
Definition: lib.h:145
+ 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 821 of file db.c.

822{
823 if (!ptr || !*ptr)
824 return;
825
826 struct AutocryptGossipHistory *gh = *ptr;
827 FREE(&gh->peer_email_addr);
829 FREE(&gh->email_msgid);
830 FREE(&gh->gossip_keydata);
831 FREE(ptr);
832}
char * peer_email_addr
Email addressof the peer.
Definition: lib.h:146
char * email_msgid
Sender's email's message id.
Definition: lib.h:148
char * sender_email_addr
Sender's email address.
Definition: lib.h:147
char * gossip_keydata
Gossip Key data.
Definition: lib.h:150
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_insert()

int mutt_autocrypt_db_gossip_history_insert ( struct Address addr,
struct AutocryptGossipHistory gossip_hist 
)

Insert a gossip history into the Autocrypt database.

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

Definition at line 841 of file db.c.

843{
844 int rc = -1;
845
846 struct Address *norm_addr = copy_normalize_addr(addr);
847
849 {
850 if (sqlite3_prepare_v3(AutocryptDB,
851 "INSERT INTO gossip_history "
852 "(peer_email_addr, "
853 "sender_email_addr, "
854 "email_msgid, "
855 "timestamp, "
856 "gossip_keydata) "
857 "VALUES (?, ?, ?, ?, ?);",
858 -1, SQLITE_PREPARE_PERSISTENT,
859 &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
860 {
861 goto cleanup;
862 }
863 }
864
865 if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, norm_addr->mailbox, -1,
866 SQLITE_STATIC) != SQLITE_OK)
867 {
868 goto cleanup;
869 }
870 if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
871 -1, SQLITE_STATIC) != SQLITE_OK)
872 {
873 if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
874 -1, SQLITE_STATIC) != SQLITE_OK)
875 {
876 goto cleanup;
877 }
878 }
879 if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
880 goto cleanup;
881 if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
882 -1, SQLITE_STATIC) != SQLITE_OK)
883 {
884 goto cleanup;
885 }
886
887 if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
888 goto cleanup;
889
890 rc = 0;
891
892cleanup:
893 mutt_addr_free(&norm_addr);
894 sqlite3_reset(GossipHistoryInsertStmt);
895 return rc;
896}
sqlite3_int64 timestamp
Timestamp of sender's email.
Definition: lib.h:149
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ AccountGetStmt

sqlite3_stmt* AccountGetStmt
static

Definition at line 42 of file db.c.

◆ AccountInsertStmt

sqlite3_stmt* AccountInsertStmt
static

Definition at line 43 of file db.c.

◆ AccountUpdateStmt

sqlite3_stmt* AccountUpdateStmt
static

Definition at line 44 of file db.c.

◆ AccountDeleteStmt

sqlite3_stmt* AccountDeleteStmt
static

Definition at line 45 of file db.c.

◆ PeerGetStmt

sqlite3_stmt* PeerGetStmt
static

Definition at line 46 of file db.c.

◆ PeerInsertStmt

sqlite3_stmt* PeerInsertStmt
static

Definition at line 47 of file db.c.

◆ PeerUpdateStmt

sqlite3_stmt* PeerUpdateStmt
static

Definition at line 48 of file db.c.

◆ PeerHistoryInsertStmt

sqlite3_stmt* PeerHistoryInsertStmt
static

Definition at line 49 of file db.c.

◆ GossipHistoryInsertStmt

sqlite3_stmt* GossipHistoryInsertStmt
static

Definition at line 50 of file db.c.

◆ AutocryptDB

sqlite3* AutocryptDB = NULL

Definition at line 52 of file db.c.