NeoMutt  2021-02-05
Teaching an old dog new tricks
DOXYGEN
db.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <sqlite3.h>
32 #include <stdbool.h>
33 #include <sys/stat.h>
34 #include "private.h"
35 #include "mutt/lib.h"
36 #include "address/lib.h"
37 #include "autocrypt/lib.h"
38 
39 /* Prepared statements */
40 static sqlite3_stmt *AccountGetStmt;
41 static sqlite3_stmt *AccountInsertStmt;
42 static sqlite3_stmt *AccountUpdateStmt;
43 static sqlite3_stmt *AccountDeleteStmt;
44 static sqlite3_stmt *PeerGetStmt;
45 static sqlite3_stmt *PeerInsertStmt;
46 static sqlite3_stmt *PeerUpdateStmt;
47 static sqlite3_stmt *PeerHistoryInsertStmt;
48 static sqlite3_stmt *GossipHistoryInsertStmt;
49 
50 sqlite3 *AutocryptDB = NULL;
51 
58 static int autocrypt_db_create(const char *db_path)
59 {
60  if (sqlite3_open_v2(db_path, &AutocryptDB,
61  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK)
62  {
63  /* L10N: s is the path to the database.
64  For some reason sqlite3 failed to open that database file. */
65  mutt_error(_("Unable to open autocrypt database %s"), db_path);
66  return -1;
67  }
69 }
70 
77 int mutt_autocrypt_db_init(bool can_create)
78 {
79  int rc = -1;
80 
81  if (AutocryptDB)
82  return 0;
83 
84  if (!C_Autocrypt || !C_AutocryptDir)
85  return -1;
86 
87  struct Buffer *db_path = mutt_buffer_pool_get();
88  mutt_buffer_concat_path(db_path, C_AutocryptDir, "autocrypt.db");
89 
90  struct stat sb;
91  if (stat(mutt_buffer_string(db_path), &sb) == 0)
92  {
93  if (sqlite3_open_v2(mutt_buffer_string(db_path), &AutocryptDB,
94  SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
95  {
96  /* L10N: Error message if autocrypt couldn't open the SQLite database
97  for some reason. The %s is the full path of the database file. */
98  mutt_error(_("Unable to open autocrypt database %s"), mutt_buffer_string(db_path));
99  goto cleanup;
100  }
101 
103  goto cleanup;
104  }
105  else
106  {
107  if (!can_create)
108  goto cleanup;
110  goto cleanup;
111  /* Don't abort the whole init process because account creation failed */
114  }
115 
116  rc = 0;
117 
118 cleanup:
119  mutt_buffer_pool_release(&db_path);
120  return rc;
121 }
122 
127 {
128  if (!AutocryptDB)
129  return;
130 
131  sqlite3_finalize(AccountGetStmt);
132  AccountGetStmt = NULL;
133  sqlite3_finalize(AccountInsertStmt);
134  AccountInsertStmt = NULL;
135  sqlite3_finalize(AccountUpdateStmt);
136  AccountUpdateStmt = NULL;
137  sqlite3_finalize(AccountDeleteStmt);
138  AccountDeleteStmt = NULL;
139 
140  sqlite3_finalize(PeerGetStmt);
141  PeerGetStmt = NULL;
142  sqlite3_finalize(PeerInsertStmt);
143  PeerInsertStmt = NULL;
144  sqlite3_finalize(PeerUpdateStmt);
145  PeerUpdateStmt = NULL;
146 
147  sqlite3_finalize(PeerHistoryInsertStmt);
148  PeerHistoryInsertStmt = NULL;
149 
150  sqlite3_finalize(GossipHistoryInsertStmt);
152 
153  sqlite3_close_v2(AutocryptDB);
154  AutocryptDB = NULL;
155 }
156 
162 {
166 }
167 
172 void mutt_autocrypt_db_normalize_addrlist(struct AddressList *al)
173 {
175 
176  struct Address *np = NULL;
177  TAILQ_FOREACH(np, al, entries)
178  {
179  mutt_str_lower(np->mailbox);
180  }
181 
182  mutt_addrlist_to_intl(al, NULL);
183 }
184 
198 static struct Address *copy_normalize_addr(struct Address *addr)
199 {
200  /* NOTE: the db functions expect a single address, so in
201  * this function we copy only the address passed in.
202  *
203  * The normalize_addrlist above is extended to work on a list
204  * because of requirements in autocrypt.c */
205 
206  struct Address *norm_addr = mutt_addr_new();
207  norm_addr->mailbox = mutt_str_dup(addr->mailbox);
208  norm_addr->is_intl = addr->is_intl;
209  norm_addr->intl_checked = addr->intl_checked;
210 
212  return norm_addr;
213 }
214 
221 static char *strdup_column_text(sqlite3_stmt *stmt, int index)
222 {
223  const char *val = (const char *) sqlite3_column_text(stmt, index);
224  return mutt_str_dup(val);
225 }
226 
232 {
233  return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
234 }
235 
241 {
242  if (!ptr || !*ptr)
243  return;
244 
245  struct AutocryptAccount *ac = *ptr;
246  FREE(&ac->email_addr);
247  FREE(&ac->keyid);
248  FREE(&ac->keydata);
249  FREE(ptr);
250 }
251 
259 int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
260 {
261  int rc = -1;
262 
263  struct Address *norm_addr = copy_normalize_addr(addr);
264  *account = NULL;
265 
266  if (!AccountGetStmt)
267  {
268  if (sqlite3_prepare_v3(AutocryptDB,
269  "SELECT "
270  "email_addr, "
271  "keyid, "
272  "keydata, "
273  "prefer_encrypt, "
274  "enabled "
275  "FROM account "
276  "WHERE email_addr = ?",
277  -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
278  {
279  goto cleanup;
280  }
281  }
282 
283  if (sqlite3_bind_text(AccountGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
284  goto cleanup;
285 
286  int result = sqlite3_step(AccountGetStmt);
287  if (result != SQLITE_ROW)
288  {
289  if (result == SQLITE_DONE)
290  rc = 0;
291  goto cleanup;
292  }
293 
294  *account = mutt_autocrypt_db_account_new();
295  (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
296  (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
297  (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
298  (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
299  (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
300 
301  rc = 1;
302 
303 cleanup:
304  mutt_addr_free(&norm_addr);
305  sqlite3_reset(AccountGetStmt);
306  return rc;
307 }
308 
318 int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid,
319  const char *keydata, bool prefer_encrypt)
320 {
321  int rc = -1;
322 
323  struct Address *norm_addr = copy_normalize_addr(addr);
324 
325  if (!AccountInsertStmt)
326  {
327  if (sqlite3_prepare_v3(AutocryptDB,
328  "INSERT INTO account "
329  "(email_addr, "
330  "keyid, "
331  "keydata, "
332  "prefer_encrypt, "
333  "enabled) "
334  "VALUES (?, ?, ?, ?, ?);",
335  -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
336  {
337  goto cleanup;
338  }
339  }
340 
341  if (sqlite3_bind_text(AccountInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
342  goto cleanup;
343  if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
344  goto cleanup;
345  if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
346  goto cleanup;
347  if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
348  goto cleanup;
349  if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
350  goto cleanup;
351 
352  if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
353  goto cleanup;
354 
355  rc = 0;
356 
357 cleanup:
358  mutt_addr_free(&norm_addr);
359  sqlite3_reset(AccountInsertStmt);
360  return rc;
361 }
362 
370 {
371  int rc = -1;
372 
373  if (!AccountUpdateStmt)
374  {
375  if (sqlite3_prepare_v3(AutocryptDB,
376  "UPDATE account SET "
377  "keyid = ?, "
378  "keydata = ?, "
379  "prefer_encrypt = ?, "
380  "enabled = ? "
381  "WHERE email_addr = ?;",
382  -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
383  {
384  goto cleanup;
385  }
386  }
387 
388  if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
389  goto cleanup;
390  if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
391  goto cleanup;
392  if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
393  goto cleanup;
394  if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
395  goto cleanup;
396  if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
397  goto cleanup;
398 
399  if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
400  goto cleanup;
401 
402  rc = 0;
403 
404 cleanup:
405  sqlite3_reset(AccountUpdateStmt);
406  return rc;
407 }
408 
416 {
417  int rc = -1;
418 
419  if (!AccountDeleteStmt)
420  {
421  if (sqlite3_prepare_v3(AutocryptDB,
422  "DELETE from account "
423  "WHERE email_addr = ?;",
424  -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
425  {
426  goto cleanup;
427  }
428  }
429 
430  if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
431  goto cleanup;
432 
433  if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
434  goto cleanup;
435 
436  rc = 0;
437 
438 cleanup:
439  sqlite3_reset(AccountDeleteStmt);
440  return rc;
441 }
442 
450 int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
451 {
452  int rc = -1, result;
453  sqlite3_stmt *stmt = NULL;
454  struct AutocryptAccount **results = NULL;
455  int results_len = 0, results_count = 0;
456 
457  *accounts = NULL;
458  *num_accounts = 0;
459 
460  /* Note, speed is not of the essence for the account management screen,
461  * so we don't bother with a persistent prepared statement */
462  if (sqlite3_prepare_v2(AutocryptDB,
463  "SELECT "
464  "email_addr, "
465  "keyid, "
466  "keydata, "
467  "prefer_encrypt, "
468  "enabled "
469  "FROM account "
470  "ORDER BY email_addr",
471  -1, &stmt, NULL) != SQLITE_OK)
472  {
473  goto cleanup;
474  }
475 
476  while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
477  {
478  if (results_count == results_len)
479  {
480  results_len += 5;
481  mutt_mem_realloc(&results, results_len * sizeof(struct AutocryptAccount *));
482  }
483 
485  results[results_count++] = account;
486 
487  account->email_addr = strdup_column_text(stmt, 0);
488  account->keyid = strdup_column_text(stmt, 1);
489  account->keydata = strdup_column_text(stmt, 2);
490  account->prefer_encrypt = sqlite3_column_int(stmt, 3);
491  account->enabled = sqlite3_column_int(stmt, 4);
492  }
493 
494  if (result == SQLITE_DONE)
495  {
496  *accounts = results;
497  rc = *num_accounts = results_count;
498  }
499  else
500  {
501  while (results_count > 0)
502  mutt_autocrypt_db_account_free(&results[--results_count]);
503  FREE(&results);
504  }
505 
506 cleanup:
507  sqlite3_finalize(stmt);
508  return rc;
509 }
510 
516 {
517  return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
518 }
519 
525 {
526  if (!ptr || !*ptr)
527  return;
528 
529  struct AutocryptPeer *peer = *ptr;
530  FREE(&peer->email_addr);
531  FREE(&peer->keyid);
532  FREE(&peer->keydata);
533  FREE(&peer->gossip_keyid);
534  FREE(&peer->gossip_keydata);
535  FREE(ptr);
536 }
537 
546 int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
547 {
548  int rc = -1;
549 
550  struct Address *norm_addr = copy_normalize_addr(addr);
551  *peer = NULL;
552 
553  if (!PeerGetStmt)
554  {
555  if (sqlite3_prepare_v3(AutocryptDB,
556  "SELECT "
557  "email_addr, "
558  "last_seen, "
559  "autocrypt_timestamp, "
560  "keyid, "
561  "keydata, "
562  "prefer_encrypt, "
563  "gossip_timestamp, "
564  "gossip_keyid, "
565  "gossip_keydata "
566  "FROM peer "
567  "WHERE email_addr = ?",
568  -1, SQLITE_PREPARE_PERSISTENT, &PeerGetStmt, NULL) != SQLITE_OK)
569  {
570  goto cleanup;
571  }
572  }
573 
574  if (sqlite3_bind_text(PeerGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
575  goto cleanup;
576 
577  int result = sqlite3_step(PeerGetStmt);
578  if (result != SQLITE_ROW)
579  {
580  if (result == SQLITE_DONE)
581  rc = 0;
582  goto cleanup;
583  }
584 
585  *peer = mutt_autocrypt_db_peer_new();
586  (*peer)->email_addr = strdup_column_text(PeerGetStmt, 0);
587  (*peer)->last_seen = sqlite3_column_int64(PeerGetStmt, 1);
588  (*peer)->autocrypt_timestamp = sqlite3_column_int64(PeerGetStmt, 2);
589  (*peer)->keyid = strdup_column_text(PeerGetStmt, 3);
590  (*peer)->keydata = strdup_column_text(PeerGetStmt, 4);
591  (*peer)->prefer_encrypt = sqlite3_column_int(PeerGetStmt, 5);
592  (*peer)->gossip_timestamp = sqlite3_column_int64(PeerGetStmt, 6);
593  (*peer)->gossip_keyid = strdup_column_text(PeerGetStmt, 7);
594  (*peer)->gossip_keydata = strdup_column_text(PeerGetStmt, 8);
595 
596  rc = 1;
597 
598 cleanup:
599  mutt_addr_free(&norm_addr);
600  sqlite3_reset(PeerGetStmt);
601  return rc;
602 }
603 
611 int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
612 {
613  int rc = -1;
614  struct Address *norm_addr = NULL;
615 
616  norm_addr = copy_normalize_addr(addr);
617 
618  if (!PeerInsertStmt)
619  {
620  if (sqlite3_prepare_v3(AutocryptDB,
621  "INSERT INTO peer "
622  "(email_addr, "
623  "last_seen, "
624  "autocrypt_timestamp, "
625  "keyid, "
626  "keydata, "
627  "prefer_encrypt, "
628  "gossip_timestamp, "
629  "gossip_keyid, "
630  "gossip_keydata) "
631  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
632  -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
633  {
634  goto cleanup;
635  }
636  }
637 
638  if (sqlite3_bind_text(PeerInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
639  goto cleanup;
640  if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
641  goto cleanup;
642  if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
643  goto cleanup;
644  if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
645  goto cleanup;
646  if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
647  goto cleanup;
648  if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
649  goto cleanup;
650  if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
651  goto cleanup;
652  if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
653  goto cleanup;
654  if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
655  goto cleanup;
656 
657  if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
658  goto cleanup;
659 
660  rc = 0;
661 
662 cleanup:
663  mutt_addr_free(&norm_addr);
664  sqlite3_reset(PeerInsertStmt);
665  return rc;
666 }
667 
675 {
676  int rc = -1;
677 
678  if (!PeerUpdateStmt)
679  {
680  if (sqlite3_prepare_v3(AutocryptDB,
681  "UPDATE peer SET "
682  "last_seen = ?, "
683  "autocrypt_timestamp = ?, "
684  "keyid = ?, "
685  "keydata = ?, "
686  "prefer_encrypt = ?, "
687  "gossip_timestamp = ?, "
688  "gossip_keyid = ?, "
689  "gossip_keydata = ? "
690  "WHERE email_addr = ?;",
691  -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
692  {
693  goto cleanup;
694  }
695  }
696 
697  if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
698  goto cleanup;
699  if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
700  goto cleanup;
701  if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
702  goto cleanup;
703  if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
704  goto cleanup;
705  if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
706  goto cleanup;
707  if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
708  goto cleanup;
709  if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
710  goto cleanup;
711  if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
712  goto cleanup;
713  if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
714  goto cleanup;
715 
716  if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
717  goto cleanup;
718 
719  rc = 0;
720 
721 cleanup:
722  sqlite3_reset(PeerUpdateStmt);
723  return rc;
724 }
725 
731 {
732  return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
733 }
734 
740 {
741  if (!ptr || !*ptr)
742  return;
743 
744  struct AutocryptPeerHistory *ph = *ptr;
745  FREE(&ph->peer_email_addr);
746  FREE(&ph->email_msgid);
747  FREE(&ph->keydata);
748  FREE(ptr);
749 }
750 
759  struct AutocryptPeerHistory *peerhist)
760 {
761  int rc = -1;
762 
763  struct Address *norm_addr = copy_normalize_addr(addr);
764 
766  {
767  if (sqlite3_prepare_v3(AutocryptDB,
768  "INSERT INTO peer_history "
769  "(peer_email_addr, "
770  "email_msgid, "
771  "timestamp, "
772  "keydata) "
773  "VALUES (?, ?, ?, ?);",
774  -1, SQLITE_PREPARE_PERSISTENT,
775  &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
776  {
777  goto cleanup;
778  }
779  }
780 
781  if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
782  goto cleanup;
783  if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
784  SQLITE_STATIC) != SQLITE_OK)
785  goto cleanup;
786  if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
787  goto cleanup;
788  if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
789  goto cleanup;
790 
791  if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
792  goto cleanup;
793 
794  rc = 0;
795 
796 cleanup:
797  mutt_addr_free(&norm_addr);
798  sqlite3_reset(PeerHistoryInsertStmt);
799  return rc;
800 }
801 
807 {
808  return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
809 }
810 
816 {
817  if (!ptr || !*ptr)
818  return;
819 
820  struct AutocryptGossipHistory *gh = *ptr;
821  FREE(&gh->peer_email_addr);
822  FREE(&gh->sender_email_addr);
823  FREE(&gh->email_msgid);
824  FREE(&gh->gossip_keydata);
825  FREE(ptr);
826 }
827 
836  struct AutocryptGossipHistory *gossip_hist)
837 {
838  int rc = -1;
839 
840  struct Address *norm_addr = copy_normalize_addr(addr);
841 
843  {
844  if (sqlite3_prepare_v3(AutocryptDB,
845  "INSERT INTO gossip_history "
846  "(peer_email_addr, "
847  "sender_email_addr, "
848  "email_msgid, "
849  "timestamp, "
850  "gossip_keydata) "
851  "VALUES (?, ?, ?, ?, ?);",
852  -1, SQLITE_PREPARE_PERSISTENT,
853  &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
854  {
855  goto cleanup;
856  }
857  }
858 
859  if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, norm_addr->mailbox, -1,
860  SQLITE_STATIC) != SQLITE_OK)
861  {
862  goto cleanup;
863  }
864  if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
865  -1, SQLITE_STATIC) != SQLITE_OK)
866  {
867  if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
868  -1, SQLITE_STATIC) != SQLITE_OK)
869  {
870  goto cleanup;
871  }
872  }
873  if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
874  goto cleanup;
875  if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
876  -1, SQLITE_STATIC) != SQLITE_OK)
877  {
878  goto cleanup;
879  }
880 
881  if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
882  goto cleanup;
883 
884  rc = 0;
885 
886 cleanup:
887  mutt_addr_free(&norm_addr);
888  sqlite3_reset(GossipHistoryInsertStmt);
889  return rc;
890 }
char * C_AutocryptDir
Config: Location of autocrypt files, including the GPG keyring and SQLite database.
Definition: config.c:40
void mutt_autocrypt_db_close(void)
Close the Autocrypt SQLite database connection.
Definition: db.c:126
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:161
sqlite3 * AutocryptDB
Definition: db.c:50
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct AutocryptPeerHistory * mutt_autocrypt_db_peer_history_new(void)
Create a new AutocryptPeerHistory.
Definition: db.c:730
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:240
Autocrypt end-to-end encryption.
void mutt_autocrypt_db_gossip_history_free(struct AutocryptGossipHistory **ptr)
Free an AutocryptGossipHistory.
Definition: db.c:815
char * peer_email_addr
Definition: lib.h:144
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition: address.c:1386
bool C_Autocrypt
Config: Enables the Autocrypt feature.
Definition: config.c:37
char * email_msgid
Definition: lib.h:134
char * keydata
Definition: lib.h:121
sqlite3_int64 timestamp
Definition: lib.h:135
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct AutocryptPeer * mutt_autocrypt_db_peer_new(void)
Create a new AutocryptPeer.
Definition: db.c:515
String manipulation buffer.
Definition: buffer.h:33
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define _(a)
Definition: message.h:28
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:385
struct AutocryptAccount * mutt_autocrypt_db_account_new(void)
Create a new AutocryptAccount.
Definition: db.c:231
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
char * email_addr
Definition: lib.h:105
int mutt_autocrypt_db_init(bool can_create)
Initialise the Autocrypt SQLite database.
Definition: db.c:77
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:440
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1349
char * keyid
Definition: lib.h:106
char * gossip_keydata
Definition: lib.h:125
static sqlite3_stmt * AccountUpdateStmt
Definition: db.c:42
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:147
bool is_intl
International Domain Name.
Definition: address.h:39
static sqlite3_stmt * AccountInsertStmt
Definition: db.c:41
Email Address Handling.
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
sqlite3_int64 gossip_timestamp
Definition: lib.h:123
int mutt_autocrypt_db_account_delete(struct AutocryptAccount *acct)
Delete an Account from the Autocrypt database.
Definition: db.c:415
sqlite3_int64 autocrypt_timestamp
Definition: lib.h:119
char * sender_email_addr
Definition: lib.h:145
void mutt_autocrypt_scan_mailboxes(void)
Scan mailboxes for Autocrypt headers.
Definition: autocrypt.c:901
bool enabled
Definition: lib.h:109
Autocrypt peer.
Definition: lib.h:115
char * email_msgid
Definition: lib.h:146
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition: db.c:198
char * email_addr
Definition: lib.h:117
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:108
bool intl_checked
Checked for IDN?
Definition: address.h:40
char * mutt_str_lower(char *s)
Convert all characters in the string to lowercase.
Definition: string.c:504
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
static sqlite3_stmt * PeerGetStmt
Definition: db.c:44
Autocrypt gossip history.
Definition: lib.h:142
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition: db.c:369
sqlite3_int64 last_seen
Definition: lib.h:118
static sqlite3_stmt * GossipHistoryInsertStmt
Definition: db.c:48
int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
Insert a peer into the Autocrypt database.
Definition: db.c:611
Shared constants/structs that are private to Autocrypt.
char * gossip_keyid
Definition: lib.h:124
int mutt_autocrypt_schema_init(void)
Set up an Autocrypt database.
Definition: schema.c:39
struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new(void)
Create a new AutocryptGossipHistory.
Definition: db.c:806
Autocrypt peer history.
Definition: lib.h:131
static sqlite3_stmt * PeerUpdateStmt
Definition: db.c:46
int mutt_autocrypt_db_peer_history_insert(struct Address *addr, struct AutocryptPeerHistory *peerhist)
Insert peer history into the Autocrypt database.
Definition: db.c:758
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:259
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:122
void mutt_autocrypt_db_normalize_addrlist(struct AddressList *al)
Normalise a list of Email Addresses.
Definition: db.c:172
char * keydata
Definition: lib.h:136
int mutt_autocrypt_db_peer_update(struct AutocryptPeer *peer)
Update the peer info in an Autocrypt database.
Definition: db.c:674
Autocrypt account.
Definition: lib.h:103
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition: db.c:58
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1275
static sqlite3_stmt * AccountDeleteStmt
Definition: db.c:43
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1304
static sqlite3_stmt * AccountGetStmt
Definition: db.c:40
static sqlite3_stmt * PeerInsertStmt
Definition: db.c:45
#define mutt_error(...)
Definition: logging.h:84
char * keydata
Definition: lib.h:107
#define FREE(x)
Definition: memory.h:40
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:374
void mutt_autocrypt_db_peer_free(struct AutocryptPeer **ptr)
Free an AutocryptPeer.
Definition: db.c:524
static sqlite3_stmt * PeerHistoryInsertStmt
Definition: db.c:47
char * gossip_keydata
Definition: lib.h:148
Convenience wrapper for the library headers.
void mutt_autocrypt_db_peer_history_free(struct AutocryptPeerHistory **ptr)
Free an AutocryptPeerHistory.
Definition: db.c:739
char * keyid
Definition: lib.h:120
int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
Get peer info from the Autocrypt database.
Definition: db.c:546
int mutt_autocrypt_db_gossip_history_insert(struct Address *addr, struct AutocryptGossipHistory *gossip_hist)
Insert a gossip history into the Autocrypt database.
Definition: db.c:835
sqlite3_int64 timestamp
Definition: lib.h:147
char * peer_email_addr
Definition: lib.h:133
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition: schema.c:105
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:450
int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid, const char *keydata, bool prefer_encrypt)
Insert an Account into the Autocrypt database.
Definition: db.c:318
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:221