NeoMutt  2021-02-05-666-ge300cd
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 "config/lib.h"
38 #include "core/lib.h"
39 #include "lib.h"
40 
41 /* Prepared statements */
42 static sqlite3_stmt *AccountGetStmt;
43 static sqlite3_stmt *AccountInsertStmt;
44 static sqlite3_stmt *AccountUpdateStmt;
45 static sqlite3_stmt *AccountDeleteStmt;
46 static sqlite3_stmt *PeerGetStmt;
47 static sqlite3_stmt *PeerInsertStmt;
48 static sqlite3_stmt *PeerUpdateStmt;
49 static sqlite3_stmt *PeerHistoryInsertStmt;
50 static sqlite3_stmt *GossipHistoryInsertStmt;
51 
52 sqlite3 *AutocryptDB = NULL;
53 
60 static int autocrypt_db_create(const char *db_path)
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 }
72 
80 int mutt_autocrypt_db_init(struct Mailbox *m, bool can_create)
81 {
82  int rc = -1;
83 
84  if (AutocryptDB)
85  return 0;
86 
87  const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
88  const char *const c_autocrypt_dir =
89  cs_subset_path(NeoMutt->sub, "autocrypt_dir");
90  if (!c_autocrypt || !c_autocrypt_dir)
91  return -1;
92 
93  struct Buffer *db_path = mutt_buffer_pool_get();
94  mutt_buffer_concat_path(db_path, c_autocrypt_dir, "autocrypt.db");
95 
96  struct stat sb;
97  if (stat(mutt_buffer_string(db_path), &sb) == 0)
98  {
99  if (sqlite3_open_v2(mutt_buffer_string(db_path), &AutocryptDB,
100  SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
101  {
102  /* L10N: Error message if autocrypt couldn't open the SQLite database
103  for some reason. The %s is the full path of the database file. */
104  mutt_error(_("Unable to open autocrypt database %s"), mutt_buffer_string(db_path));
105  goto cleanup;
106  }
107 
109  goto cleanup;
110  }
111  else
112  {
113  if (!can_create)
114  goto cleanup;
116  goto cleanup;
117  /* Don't abort the whole init process because account creation failed */
120  }
121 
122  rc = 0;
123 
124 cleanup:
125  mutt_buffer_pool_release(&db_path);
126  return rc;
127 }
128 
133 {
134  if (!AutocryptDB)
135  return;
136 
137  sqlite3_finalize(AccountGetStmt);
138  AccountGetStmt = NULL;
139  sqlite3_finalize(AccountInsertStmt);
140  AccountInsertStmt = NULL;
141  sqlite3_finalize(AccountUpdateStmt);
142  AccountUpdateStmt = NULL;
143  sqlite3_finalize(AccountDeleteStmt);
144  AccountDeleteStmt = NULL;
145 
146  sqlite3_finalize(PeerGetStmt);
147  PeerGetStmt = NULL;
148  sqlite3_finalize(PeerInsertStmt);
149  PeerInsertStmt = NULL;
150  sqlite3_finalize(PeerUpdateStmt);
151  PeerUpdateStmt = NULL;
152 
153  sqlite3_finalize(PeerHistoryInsertStmt);
154  PeerHistoryInsertStmt = NULL;
155 
156  sqlite3_finalize(GossipHistoryInsertStmt);
158 
159  sqlite3_close_v2(AutocryptDB);
160  AutocryptDB = NULL;
161 }
162 
168 {
172 }
173 
178 void mutt_autocrypt_db_normalize_addrlist(struct AddressList *al)
179 {
181 
182  struct Address *np = NULL;
183  TAILQ_FOREACH(np, al, entries)
184  {
185  mutt_str_lower(np->mailbox);
186  }
187 
188  mutt_addrlist_to_intl(al, NULL);
189 }
190 
204 static struct Address *copy_normalize_addr(struct Address *addr)
205 {
206  /* NOTE: the db functions expect a single address, so in
207  * this function we copy only the address passed in.
208  *
209  * The normalize_addrlist above is extended to work on a list
210  * because of requirements in autocrypt.c */
211 
212  struct Address *norm_addr = mutt_addr_new();
213  norm_addr->mailbox = mutt_str_dup(addr->mailbox);
214  norm_addr->is_intl = addr->is_intl;
215  norm_addr->intl_checked = addr->intl_checked;
216 
218  return norm_addr;
219 }
220 
227 static char *strdup_column_text(sqlite3_stmt *stmt, int index)
228 {
229  const char *val = (const char *) sqlite3_column_text(stmt, index);
230  return mutt_str_dup(val);
231 }
232 
238 {
239  return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
240 }
241 
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 }
257 
265 int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
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, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
290  goto cleanup;
291 
292  int result = sqlite3_step(AccountGetStmt);
293  if (result != SQLITE_ROW)
294  {
295  if (result == SQLITE_DONE)
296  rc = 0;
297  goto cleanup;
298  }
299 
300  *account = mutt_autocrypt_db_account_new();
301  (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
302  (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
303  (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
304  (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
305  (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
306 
307  rc = 1;
308 
309 cleanup:
310  mutt_addr_free(&norm_addr);
311  sqlite3_reset(AccountGetStmt);
312  return rc;
313 }
314 
324 int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid,
325  const char *keydata, bool prefer_encrypt)
326 {
327  int rc = -1;
328 
329  struct Address *norm_addr = copy_normalize_addr(addr);
330 
331  if (!AccountInsertStmt)
332  {
333  if (sqlite3_prepare_v3(AutocryptDB,
334  "INSERT INTO account "
335  "(email_addr, "
336  "keyid, "
337  "keydata, "
338  "prefer_encrypt, "
339  "enabled) "
340  "VALUES (?, ?, ?, ?, ?);",
341  -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
342  {
343  goto cleanup;
344  }
345  }
346 
347  if (sqlite3_bind_text(AccountInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
348  goto cleanup;
349  if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
350  goto cleanup;
351  if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
352  goto cleanup;
353  if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
354  goto cleanup;
355  if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
356  goto cleanup;
357 
358  if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
359  goto cleanup;
360 
361  rc = 0;
362 
363 cleanup:
364  mutt_addr_free(&norm_addr);
365  sqlite3_reset(AccountInsertStmt);
366  return rc;
367 }
368 
376 {
377  int rc = -1;
378 
379  if (!AccountUpdateStmt)
380  {
381  if (sqlite3_prepare_v3(AutocryptDB,
382  "UPDATE account SET "
383  "keyid = ?, "
384  "keydata = ?, "
385  "prefer_encrypt = ?, "
386  "enabled = ? "
387  "WHERE email_addr = ?;",
388  -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
389  {
390  goto cleanup;
391  }
392  }
393 
394  if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
395  goto cleanup;
396  if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
397  goto cleanup;
398  if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
399  goto cleanup;
400  if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
401  goto cleanup;
402  if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
403  goto cleanup;
404 
405  if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
406  goto cleanup;
407 
408  rc = 0;
409 
410 cleanup:
411  sqlite3_reset(AccountUpdateStmt);
412  return rc;
413 }
414 
422 {
423  int rc = -1;
424 
425  if (!AccountDeleteStmt)
426  {
427  if (sqlite3_prepare_v3(AutocryptDB,
428  "DELETE from account "
429  "WHERE email_addr = ?;",
430  -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
431  {
432  goto cleanup;
433  }
434  }
435 
436  if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
437  goto cleanup;
438 
439  if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
440  goto cleanup;
441 
442  rc = 0;
443 
444 cleanup:
445  sqlite3_reset(AccountDeleteStmt);
446  return rc;
447 }
448 
456 int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
457 {
458  int rc = -1, result;
459  sqlite3_stmt *stmt = NULL;
460  struct AutocryptAccount **results = NULL;
461  int results_len = 0, results_count = 0;
462 
463  *accounts = NULL;
464  *num_accounts = 0;
465 
466  /* Note, speed is not of the essence for the account management screen,
467  * so we don't bother with a persistent prepared statement */
468  if (sqlite3_prepare_v2(AutocryptDB,
469  "SELECT "
470  "email_addr, "
471  "keyid, "
472  "keydata, "
473  "prefer_encrypt, "
474  "enabled "
475  "FROM account "
476  "ORDER BY email_addr",
477  -1, &stmt, NULL) != SQLITE_OK)
478  {
479  goto cleanup;
480  }
481 
482  while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
483  {
484  if (results_count == results_len)
485  {
486  results_len += 5;
487  mutt_mem_realloc(&results, results_len * sizeof(struct AutocryptAccount *));
488  }
489 
491  results[results_count++] = account;
492 
493  account->email_addr = strdup_column_text(stmt, 0);
494  account->keyid = strdup_column_text(stmt, 1);
495  account->keydata = strdup_column_text(stmt, 2);
496  account->prefer_encrypt = sqlite3_column_int(stmt, 3);
497  account->enabled = sqlite3_column_int(stmt, 4);
498  }
499 
500  if (result == SQLITE_DONE)
501  {
502  *accounts = results;
503  rc = *num_accounts = results_count;
504  }
505  else
506  {
507  while (results_count > 0)
508  mutt_autocrypt_db_account_free(&results[--results_count]);
509  FREE(&results);
510  }
511 
512 cleanup:
513  sqlite3_finalize(stmt);
514  return rc;
515 }
516 
522 {
523  return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
524 }
525 
531 {
532  if (!ptr || !*ptr)
533  return;
534 
535  struct AutocryptPeer *peer = *ptr;
536  FREE(&peer->email_addr);
537  FREE(&peer->keyid);
538  FREE(&peer->keydata);
539  FREE(&peer->gossip_keyid);
540  FREE(&peer->gossip_keydata);
541  FREE(ptr);
542 }
543 
552 int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
553 {
554  int rc = -1;
555 
556  struct Address *norm_addr = copy_normalize_addr(addr);
557  *peer = NULL;
558 
559  if (!PeerGetStmt)
560  {
561  if (sqlite3_prepare_v3(AutocryptDB,
562  "SELECT "
563  "email_addr, "
564  "last_seen, "
565  "autocrypt_timestamp, "
566  "keyid, "
567  "keydata, "
568  "prefer_encrypt, "
569  "gossip_timestamp, "
570  "gossip_keyid, "
571  "gossip_keydata "
572  "FROM peer "
573  "WHERE email_addr = ?",
574  -1, SQLITE_PREPARE_PERSISTENT, &PeerGetStmt, NULL) != SQLITE_OK)
575  {
576  goto cleanup;
577  }
578  }
579 
580  if (sqlite3_bind_text(PeerGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
581  goto cleanup;
582 
583  int result = sqlite3_step(PeerGetStmt);
584  if (result != SQLITE_ROW)
585  {
586  if (result == SQLITE_DONE)
587  rc = 0;
588  goto cleanup;
589  }
590 
591  *peer = mutt_autocrypt_db_peer_new();
592  (*peer)->email_addr = strdup_column_text(PeerGetStmt, 0);
593  (*peer)->last_seen = sqlite3_column_int64(PeerGetStmt, 1);
594  (*peer)->autocrypt_timestamp = sqlite3_column_int64(PeerGetStmt, 2);
595  (*peer)->keyid = strdup_column_text(PeerGetStmt, 3);
596  (*peer)->keydata = strdup_column_text(PeerGetStmt, 4);
597  (*peer)->prefer_encrypt = sqlite3_column_int(PeerGetStmt, 5);
598  (*peer)->gossip_timestamp = sqlite3_column_int64(PeerGetStmt, 6);
599  (*peer)->gossip_keyid = strdup_column_text(PeerGetStmt, 7);
600  (*peer)->gossip_keydata = strdup_column_text(PeerGetStmt, 8);
601 
602  rc = 1;
603 
604 cleanup:
605  mutt_addr_free(&norm_addr);
606  sqlite3_reset(PeerGetStmt);
607  return rc;
608 }
609 
617 int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
618 {
619  int rc = -1;
620  struct Address *norm_addr = NULL;
621 
622  norm_addr = copy_normalize_addr(addr);
623 
624  if (!PeerInsertStmt)
625  {
626  if (sqlite3_prepare_v3(AutocryptDB,
627  "INSERT INTO peer "
628  "(email_addr, "
629  "last_seen, "
630  "autocrypt_timestamp, "
631  "keyid, "
632  "keydata, "
633  "prefer_encrypt, "
634  "gossip_timestamp, "
635  "gossip_keyid, "
636  "gossip_keydata) "
637  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
638  -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
639  {
640  goto cleanup;
641  }
642  }
643 
644  if (sqlite3_bind_text(PeerInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
645  goto cleanup;
646  if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
647  goto cleanup;
648  if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
649  goto cleanup;
650  if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
651  goto cleanup;
652  if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
653  goto cleanup;
654  if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
655  goto cleanup;
656  if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
657  goto cleanup;
658  if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
659  goto cleanup;
660  if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
661  goto cleanup;
662 
663  if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
664  goto cleanup;
665 
666  rc = 0;
667 
668 cleanup:
669  mutt_addr_free(&norm_addr);
670  sqlite3_reset(PeerInsertStmt);
671  return rc;
672 }
673 
681 {
682  int rc = -1;
683 
684  if (!PeerUpdateStmt)
685  {
686  if (sqlite3_prepare_v3(AutocryptDB,
687  "UPDATE peer SET "
688  "last_seen = ?, "
689  "autocrypt_timestamp = ?, "
690  "keyid = ?, "
691  "keydata = ?, "
692  "prefer_encrypt = ?, "
693  "gossip_timestamp = ?, "
694  "gossip_keyid = ?, "
695  "gossip_keydata = ? "
696  "WHERE email_addr = ?;",
697  -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
698  {
699  goto cleanup;
700  }
701  }
702 
703  if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
704  goto cleanup;
705  if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
706  goto cleanup;
707  if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
708  goto cleanup;
709  if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
710  goto cleanup;
711  if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
712  goto cleanup;
713  if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
714  goto cleanup;
715  if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
716  goto cleanup;
717  if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
718  goto cleanup;
719  if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
720  goto cleanup;
721 
722  if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
723  goto cleanup;
724 
725  rc = 0;
726 
727 cleanup:
728  sqlite3_reset(PeerUpdateStmt);
729  return rc;
730 }
731 
737 {
738  return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
739 }
740 
746 {
747  if (!ptr || !*ptr)
748  return;
749 
750  struct AutocryptPeerHistory *ph = *ptr;
751  FREE(&ph->peer_email_addr);
752  FREE(&ph->email_msgid);
753  FREE(&ph->keydata);
754  FREE(ptr);
755 }
756 
765  struct AutocryptPeerHistory *peerhist)
766 {
767  int rc = -1;
768 
769  struct Address *norm_addr = copy_normalize_addr(addr);
770 
772  {
773  if (sqlite3_prepare_v3(AutocryptDB,
774  "INSERT INTO peer_history "
775  "(peer_email_addr, "
776  "email_msgid, "
777  "timestamp, "
778  "keydata) "
779  "VALUES (?, ?, ?, ?);",
780  -1, SQLITE_PREPARE_PERSISTENT,
781  &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
782  {
783  goto cleanup;
784  }
785  }
786 
787  if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
788  goto cleanup;
789  if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
790  SQLITE_STATIC) != SQLITE_OK)
791  {
792  goto cleanup;
793  }
794  if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
795  goto cleanup;
796  if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
797  goto cleanup;
798 
799  if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
800  goto cleanup;
801 
802  rc = 0;
803 
804 cleanup:
805  mutt_addr_free(&norm_addr);
806  sqlite3_reset(PeerHistoryInsertStmt);
807  return rc;
808 }
809 
815 {
816  return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
817 }
818 
824 {
825  if (!ptr || !*ptr)
826  return;
827 
828  struct AutocryptGossipHistory *gh = *ptr;
829  FREE(&gh->peer_email_addr);
830  FREE(&gh->sender_email_addr);
831  FREE(&gh->email_msgid);
832  FREE(&gh->gossip_keydata);
833  FREE(ptr);
834 }
835 
844  struct AutocryptGossipHistory *gossip_hist)
845 {
846  int rc = -1;
847 
848  struct Address *norm_addr = copy_normalize_addr(addr);
849 
851  {
852  if (sqlite3_prepare_v3(AutocryptDB,
853  "INSERT INTO gossip_history "
854  "(peer_email_addr, "
855  "sender_email_addr, "
856  "email_msgid, "
857  "timestamp, "
858  "gossip_keydata) "
859  "VALUES (?, ?, ?, ?, ?);",
860  -1, SQLITE_PREPARE_PERSISTENT,
861  &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
862  {
863  goto cleanup;
864  }
865  }
866 
867  if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, norm_addr->mailbox, -1,
868  SQLITE_STATIC) != SQLITE_OK)
869  {
870  goto cleanup;
871  }
872  if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
873  -1, SQLITE_STATIC) != SQLITE_OK)
874  {
875  if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
876  -1, SQLITE_STATIC) != SQLITE_OK)
877  {
878  goto cleanup;
879  }
880  }
881  if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
882  goto cleanup;
883  if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
884  -1, SQLITE_STATIC) != SQLITE_OK)
885  {
886  goto cleanup;
887  }
888 
889  if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
890  goto cleanup;
891 
892  rc = 0;
893 
894 cleanup:
895  mutt_addr_free(&norm_addr);
896  sqlite3_reset(GossipHistoryInsertStmt);
897  return rc;
898 }
void mutt_autocrypt_db_close(void)
Close the Autocrypt SQLite database connection.
Definition: db.c:132
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:167
sqlite3 * AutocryptDB
Definition: db.c:52
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
struct AutocryptPeerHistory * mutt_autocrypt_db_peer_history_new(void)
Create a new AutocryptPeerHistory.
Definition: db.c:736
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:725
#define mutt_error(...)
Definition: logging.h:88
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:246
void mutt_autocrypt_db_gossip_history_free(struct AutocryptGossipHistory **ptr)
Free an AutocryptGossipHistory.
Definition: db.c:823
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:1388
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:521
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:237
An email address.
Definition: address.h:35
char * mailbox
Mailbox and host address.
Definition: address.h:38
char * email_addr
Definition: lib.h:105
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:440
Container for Accounts, Notifications.
Definition: neomutt.h:36
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1351
char * keyid
Definition: lib.h:106
char * gossip_keydata
Definition: lib.h:125
static sqlite3_stmt * AccountUpdateStmt
Definition: db.c:44
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:155
Convenience wrapper for the config headers.
bool is_intl
International Domain Name.
Definition: address.h:40
static sqlite3_stmt * AccountInsertStmt
Definition: db.c:43
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:421
sqlite3_int64 autocrypt_timestamp
Definition: lib.h:119
char * sender_email_addr
Definition: lib.h:145
bool enabled
Definition: lib.h:109
Autocrypt peer.
Definition: lib.h:115
char * email_msgid
Definition: lib.h:146
Convenience wrapper for the core headers.
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition: db.c:204
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:41
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
void mutt_autocrypt_scan_mailboxes(struct Mailbox *m)
Scan mailboxes for Autocrypt headers.
Definition: autocrypt.c:933
static sqlite3_stmt * PeerGetStmt
Definition: db.c:46
A mailbox.
Definition: mailbox.h:81
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:375
sqlite3_int64 last_seen
Definition: lib.h:118
static sqlite3_stmt * GossipHistoryInsertStmt
Definition: db.c:50
int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
Insert a peer into the Autocrypt database.
Definition: db.c:617
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:38
int mutt_autocrypt_db_init(struct Mailbox *m, bool can_create)
Initialise the Autocrypt SQLite database.
Definition: db.c:80
struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new(void)
Create a new AutocryptGossipHistory.
Definition: db.c:814
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
Autocrypt peer history.
Definition: lib.h:131
static sqlite3_stmt * PeerUpdateStmt
Definition: db.c:48
int mutt_autocrypt_db_peer_history_insert(struct Address *addr, struct AutocryptPeerHistory *peerhist)
Insert peer history into the Autocrypt database.
Definition: db.c:764
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:265
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:178
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:680
Autocrypt account.
Definition: lib.h:103
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition: db.c:60
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1276
static sqlite3_stmt * AccountDeleteStmt
Definition: db.c:45
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1305
static sqlite3_stmt * AccountGetStmt
Definition: db.c:42
static sqlite3_stmt * PeerInsertStmt
Definition: db.c:47
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:504
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:530
static sqlite3_stmt * PeerHistoryInsertStmt
Definition: db.c:49
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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:745
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:552
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:843
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:104
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:456
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:324
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:227