NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
db.c File Reference

Autocrypt database handling. More...

#include "config.h"
#include <sqlite3.h>
#include <stdbool.h>
#include <stddef.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 AutocryptAccountArray *aaa)
 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: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
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:168
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
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_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:736
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:743
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_create(NULL, buf_string(addr->mailbox));
214 norm_addr->is_intl = addr->is_intl;
215 norm_addr->intl_checked = addr->intl_checked;
216
218 return norm_addr;
219}
struct Address * mutt_addr_create(const char *personal, const char *mailbox)
Create and populate a new Address.
Definition: address.c:414
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:168
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 227 of file db.c.

228{
229 const char *val = (const char *) sqlite3_column_text(stmt, index);
230 return mutt_str_dup(val);
231}
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 237 of file db.c.

238{
239 return MUTT_MEM_CALLOC(1, struct AutocryptAccount);
240}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Autocrypt account.
Definition: lib.h:110
+ 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 246 of file db.c.

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

266{
267 int rc = -1;
268
269 struct Address *norm_addr = copy_normalize_addr(addr);
270 *account = NULL;
271
272 if (!AccountGetStmt)
273 {
274 if (sqlite3_prepare_v3(AutocryptDB,
275 "SELECT "
276 "email_addr, "
277 "keyid, "
278 "keydata, "
279 "prefer_encrypt, "
280 "enabled "
281 "FROM account "
282 "WHERE email_addr = ?",
283 -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
284 {
285 goto cleanup;
286 }
287 }
288
289 if (sqlite3_bind_text(AccountGetStmt, 1, buf_string(norm_addr->mailbox), -1,
290 SQLITE_STATIC) != SQLITE_OK)
291 {
292 goto cleanup;
293 }
294
295 int result = sqlite3_step(AccountGetStmt);
296 if (result != SQLITE_ROW)
297 {
298 if (result == SQLITE_DONE)
299 rc = 0;
300 goto cleanup;
301 }
302
304 (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
305 (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
306 (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
307 (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
308 (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
309
310 rc = 1;
311
312cleanup:
313 mutt_addr_free(&norm_addr);
314 sqlite3_reset(AccountGetStmt);
315 return rc;
316}
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:237
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:227
+ 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 327 of file db.c.

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

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

428{
429 int rc = -1;
430
432 {
433 if (sqlite3_prepare_v3(AutocryptDB,
434 "DELETE from account "
435 "WHERE email_addr = ?;",
436 -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
437 {
438 goto cleanup;
439 }
440 }
441
442 if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
443 goto cleanup;
444
445 if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
446 goto cleanup;
447
448 rc = 0;
449
450cleanup:
451 sqlite3_reset(AccountDeleteStmt);
452 return rc;
453}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_get_all()

int mutt_autocrypt_db_account_get_all ( struct AutocryptAccountArray *  aaa)

Get all accounts from an Autocrypt database.

Parameters
aaaAccount array
Return values
nSuccess, number of Accounts
-1Error

Definition at line 461 of file db.c.

462{
463 if (!aaa)
464 return -1;
465
466 int rc = -1;
467 sqlite3_stmt *stmt = NULL;
468
469 /* Note, speed is not of the essence for the account management screen,
470 * so we don't bother with a persistent prepared statement */
471 if (sqlite3_prepare_v2(AutocryptDB,
472 "SELECT "
473 "email_addr, "
474 "keyid, "
475 "keydata, "
476 "prefer_encrypt, "
477 "enabled "
478 "FROM account "
479 "ORDER BY email_addr",
480 -1, &stmt, NULL) != SQLITE_OK)
481 {
482 goto cleanup;
483 }
484
485 int result = SQLITE_ERROR;
486 while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
487 {
489
490 ac->email_addr = strdup_column_text(stmt, 0);
491 ac->keyid = strdup_column_text(stmt, 1);
492 ac->keydata = strdup_column_text(stmt, 2);
493 ac->prefer_encrypt = sqlite3_column_int(stmt, 3);
494 ac->enabled = sqlite3_column_int(stmt, 4);
495
496 ARRAY_ADD(aaa, ac);
497 }
498
499 if (result == SQLITE_DONE)
500 {
501 rc = ARRAY_SIZE(aaa);
502 }
503 else
504 {
505 struct AutocryptAccount **pac = NULL;
506 ARRAY_FOREACH(pac, aaa)
507 {
509 }
510 ARRAY_FREE(aaa);
511 }
512
513cleanup:
514 sqlite3_finalize(stmt);
515 return rc;
516}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:246
+ 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 522 of file db.c.

523{
524 return MUTT_MEM_CALLOC(1, struct AutocryptPeer);
525}
Autocrypt peer.
Definition: lib.h:123
+ 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 531 of file db.c.

532{
533 if (!ptr || !*ptr)
534 return;
535
536 struct AutocryptPeer *peer = *ptr;
537 FREE(&peer->email_addr);
538 FREE(&peer->keyid);
539 FREE(&peer->keydata);
540 FREE(&peer->gossip_keyid);
541 FREE(&peer->gossip_keydata);
542 FREE(ptr);
543}
char * gossip_keydata
Gossip Key data.
Definition: lib.h:132
char * keyid
PGP Key id.
Definition: lib.h:127
char * gossip_keyid
Gossip Key id.
Definition: lib.h:131
char * keydata
PGP Key data.
Definition: lib.h:128
char * email_addr
Email address.
Definition: lib.h:124
+ 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 553 of file db.c.

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

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

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

744{
745 return MUTT_MEM_CALLOC(1, struct AutocryptPeerHistory);
746}
Autocrypt peer history.
Definition: lib.h:139
+ 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 752 of file db.c.

753{
754 if (!ptr || !*ptr)
755 return;
756
757 struct AutocryptPeerHistory *ph = *ptr;
758 FREE(&ph->peer_email_addr);
759 FREE(&ph->email_msgid);
760 FREE(&ph->keydata);
761 FREE(ptr);
762}
char * peer_email_addr
Email address of the peer.
Definition: lib.h:140
char * email_msgid
Message id of the email.
Definition: lib.h:141
char * keydata
PGP Key data.
Definition: lib.h:143
+ 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 771 of file db.c.

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

825{
826 return MUTT_MEM_CALLOC(1, struct AutocryptGossipHistory);
827}
Autocrypt gossip history.
Definition: lib.h:150
+ 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 833 of file db.c.

834{
835 if (!ptr || !*ptr)
836 return;
837
838 struct AutocryptGossipHistory *gh = *ptr;
839 FREE(&gh->peer_email_addr);
841 FREE(&gh->email_msgid);
842 FREE(&gh->gossip_keydata);
843 FREE(ptr);
844}
char * peer_email_addr
Email addressof the peer.
Definition: lib.h:151
char * email_msgid
Sender's email's message id.
Definition: lib.h:153
char * sender_email_addr
Sender's email address.
Definition: lib.h:152
char * gossip_keydata
Gossip Key data.
Definition: lib.h:155
+ 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 853 of file db.c.

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