NeoMutt  2020-06-26-250-g349c94
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_b2s(db_path), &sb) == 0)
92  {
93  if (sqlite3_open_v2(mutt_b2s(db_path), &AutocryptDB, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
94  {
95  /* L10N: Error message if autocrypt couldn't open the SQLite database
96  for some reason. The %s is the full path of the database file. */
97  mutt_error(_("Unable to open autocrypt database %s"), mutt_b2s(db_path));
98  goto cleanup;
99  }
100 
102  goto cleanup;
103  }
104  else
105  {
106  if (!can_create)
107  goto cleanup;
108  if (autocrypt_db_create(mutt_b2s(db_path)))
109  goto cleanup;
110  /* Don't abort the whole init process because account creation failed */
113  }
114 
115  rc = 0;
116 
117 cleanup:
118  mutt_buffer_pool_release(&db_path);
119  return rc;
120 }
121 
126 {
127  if (!AutocryptDB)
128  return;
129 
130  sqlite3_finalize(AccountGetStmt);
131  AccountGetStmt = NULL;
132  sqlite3_finalize(AccountInsertStmt);
133  AccountInsertStmt = NULL;
134  sqlite3_finalize(AccountUpdateStmt);
135  AccountUpdateStmt = NULL;
136  sqlite3_finalize(AccountDeleteStmt);
137  AccountDeleteStmt = NULL;
138 
139  sqlite3_finalize(PeerGetStmt);
140  PeerGetStmt = NULL;
141  sqlite3_finalize(PeerInsertStmt);
142  PeerInsertStmt = NULL;
143  sqlite3_finalize(PeerUpdateStmt);
144  PeerUpdateStmt = NULL;
145 
146  sqlite3_finalize(PeerHistoryInsertStmt);
147  PeerHistoryInsertStmt = NULL;
148 
149  sqlite3_finalize(GossipHistoryInsertStmt);
151 
152  sqlite3_close_v2(AutocryptDB);
153  AutocryptDB = NULL;
154 }
155 
161 {
165 }
166 
171 void mutt_autocrypt_db_normalize_addrlist(struct AddressList *al)
172 {
174 
175  struct Address *np = NULL;
176  TAILQ_FOREACH(np, al, entries)
177  {
178  mutt_str_lower(np->mailbox);
179  }
180 
181  mutt_addrlist_to_intl(al, NULL);
182 }
183 
197 static struct Address *copy_normalize_addr(struct Address *addr)
198 {
199  /* NOTE: the db functions expect a single address, so in
200  * this function we copy only the address passed in.
201  *
202  * The normalize_addrlist above is extended to work on a list
203  * because of requirements in autocrypt.c */
204 
205  struct Address *norm_addr = mutt_addr_new();
206  norm_addr->mailbox = mutt_str_dup(addr->mailbox);
207  norm_addr->is_intl = addr->is_intl;
208  norm_addr->intl_checked = addr->intl_checked;
209 
211  return norm_addr;
212 }
213 
220 static char *strdup_column_text(sqlite3_stmt *stmt, int index)
221 {
222  const char *val = (const char *) sqlite3_column_text(stmt, index);
223  return mutt_str_dup(val);
224 }
225 
231 {
232  return mutt_mem_calloc(1, sizeof(struct AutocryptAccount));
233 }
234 
240 {
241  if (!ptr || !*ptr)
242  return;
243 
244  struct AutocryptAccount *ac = *ptr;
245  FREE(&ac->email_addr);
246  FREE(&ac->keyid);
247  FREE(&ac->keydata);
248  FREE(ptr);
249 }
250 
258 int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
259 {
260  int rc = -1;
261 
262  struct Address *norm_addr = copy_normalize_addr(addr);
263  *account = NULL;
264 
265  if (!AccountGetStmt)
266  {
267  if (sqlite3_prepare_v3(AutocryptDB,
268  "SELECT "
269  "email_addr, "
270  "keyid, "
271  "keydata, "
272  "prefer_encrypt, "
273  "enabled "
274  "FROM account "
275  "WHERE email_addr = ?",
276  -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
277  {
278  goto cleanup;
279  }
280  }
281 
282  if (sqlite3_bind_text(AccountGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
283  goto cleanup;
284 
285  int result = sqlite3_step(AccountGetStmt);
286  if (result != SQLITE_ROW)
287  {
288  if (result == SQLITE_DONE)
289  rc = 0;
290  goto cleanup;
291  }
292 
293  *account = mutt_autocrypt_db_account_new();
294  (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
295  (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
296  (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
297  (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
298  (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
299 
300  rc = 1;
301 
302 cleanup:
303  mutt_addr_free(&norm_addr);
304  sqlite3_reset(AccountGetStmt);
305  return rc;
306 }
307 
317 int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid,
318  const char *keydata, bool prefer_encrypt)
319 {
320  int rc = -1;
321 
322  struct Address *norm_addr = copy_normalize_addr(addr);
323 
324  if (!AccountInsertStmt)
325  {
326  if (sqlite3_prepare_v3(AutocryptDB,
327  "INSERT INTO account "
328  "(email_addr, "
329  "keyid, "
330  "keydata, "
331  "prefer_encrypt, "
332  "enabled) "
333  "VALUES (?, ?, ?, ?, ?);",
334  -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
335  {
336  goto cleanup;
337  }
338  }
339 
340  if (sqlite3_bind_text(AccountInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
341  goto cleanup;
342  if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
343  goto cleanup;
344  if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
345  goto cleanup;
346  if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
347  goto cleanup;
348  if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
349  goto cleanup;
350 
351  if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
352  goto cleanup;
353 
354  rc = 0;
355 
356 cleanup:
357  mutt_addr_free(&norm_addr);
358  sqlite3_reset(AccountInsertStmt);
359  return rc;
360 }
361 
369 {
370  int rc = -1;
371 
372  if (!AccountUpdateStmt)
373  {
374  if (sqlite3_prepare_v3(AutocryptDB,
375  "UPDATE account SET "
376  "keyid = ?, "
377  "keydata = ?, "
378  "prefer_encrypt = ?, "
379  "enabled = ? "
380  "WHERE email_addr = ?;",
381  -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
382  {
383  goto cleanup;
384  }
385  }
386 
387  if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
388  goto cleanup;
389  if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
390  goto cleanup;
391  if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
392  goto cleanup;
393  if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
394  goto cleanup;
395  if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
396  goto cleanup;
397 
398  if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
399  goto cleanup;
400 
401  rc = 0;
402 
403 cleanup:
404  sqlite3_reset(AccountUpdateStmt);
405  return rc;
406 }
407 
415 {
416  int rc = -1;
417 
418  if (!AccountDeleteStmt)
419  {
420  if (sqlite3_prepare_v3(AutocryptDB,
421  "DELETE from account "
422  "WHERE email_addr = ?;",
423  -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
424  {
425  goto cleanup;
426  }
427  }
428 
429  if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
430  goto cleanup;
431 
432  if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
433  goto cleanup;
434 
435  rc = 0;
436 
437 cleanup:
438  sqlite3_reset(AccountDeleteStmt);
439  return rc;
440 }
441 
449 int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
450 {
451  int rc = -1, result;
452  sqlite3_stmt *stmt = NULL;
453  struct AutocryptAccount **results = NULL;
454  int results_len = 0, results_count = 0;
455 
456  *accounts = NULL;
457  *num_accounts = 0;
458 
459  /* Note, speed is not of the essence for the account management screen,
460  * so we don't bother with a persistent prepared statement */
461  if (sqlite3_prepare_v2(AutocryptDB,
462  "SELECT "
463  "email_addr, "
464  "keyid, "
465  "keydata, "
466  "prefer_encrypt, "
467  "enabled "
468  "FROM account "
469  "ORDER BY email_addr",
470  -1, &stmt, NULL) != SQLITE_OK)
471  {
472  goto cleanup;
473  }
474 
475  while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
476  {
477  if (results_count == results_len)
478  {
479  results_len += 5;
480  mutt_mem_realloc(&results, results_len * sizeof(struct AutocryptAccount *));
481  }
482 
484  results[results_count++] = account;
485 
486  account->email_addr = strdup_column_text(stmt, 0);
487  account->keyid = strdup_column_text(stmt, 1);
488  account->keydata = strdup_column_text(stmt, 2);
489  account->prefer_encrypt = sqlite3_column_int(stmt, 3);
490  account->enabled = sqlite3_column_int(stmt, 4);
491  }
492 
493  if (result == SQLITE_DONE)
494  {
495  *accounts = results;
496  rc = *num_accounts = results_count;
497  }
498  else
499  {
500  while (results_count > 0)
501  mutt_autocrypt_db_account_free(&results[--results_count]);
502  FREE(&results);
503  }
504 
505 cleanup:
506  sqlite3_finalize(stmt);
507  return rc;
508 }
509 
515 {
516  return mutt_mem_calloc(1, sizeof(struct AutocryptPeer));
517 }
518 
524 {
525  if (!ptr || !*ptr)
526  return;
527 
528  struct AutocryptPeer *peer = *ptr;
529  FREE(&peer->email_addr);
530  FREE(&peer->keyid);
531  FREE(&peer->keydata);
532  FREE(&peer->gossip_keyid);
533  FREE(&peer->gossip_keydata);
534  FREE(ptr);
535 }
536 
545 int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
546 {
547  int rc = -1;
548 
549  struct Address *norm_addr = copy_normalize_addr(addr);
550  *peer = NULL;
551 
552  if (!PeerGetStmt)
553  {
554  if (sqlite3_prepare_v3(AutocryptDB,
555  "SELECT "
556  "email_addr, "
557  "last_seen, "
558  "autocrypt_timestamp, "
559  "keyid, "
560  "keydata, "
561  "prefer_encrypt, "
562  "gossip_timestamp, "
563  "gossip_keyid, "
564  "gossip_keydata "
565  "FROM peer "
566  "WHERE email_addr = ?",
567  -1, SQLITE_PREPARE_PERSISTENT, &PeerGetStmt, NULL) != SQLITE_OK)
568  {
569  goto cleanup;
570  }
571  }
572 
573  if (sqlite3_bind_text(PeerGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
574  goto cleanup;
575 
576  int result = sqlite3_step(PeerGetStmt);
577  if (result != SQLITE_ROW)
578  {
579  if (result == SQLITE_DONE)
580  rc = 0;
581  goto cleanup;
582  }
583 
584  *peer = mutt_autocrypt_db_peer_new();
585  (*peer)->email_addr = strdup_column_text(PeerGetStmt, 0);
586  (*peer)->last_seen = sqlite3_column_int64(PeerGetStmt, 1);
587  (*peer)->autocrypt_timestamp = sqlite3_column_int64(PeerGetStmt, 2);
588  (*peer)->keyid = strdup_column_text(PeerGetStmt, 3);
589  (*peer)->keydata = strdup_column_text(PeerGetStmt, 4);
590  (*peer)->prefer_encrypt = sqlite3_column_int(PeerGetStmt, 5);
591  (*peer)->gossip_timestamp = sqlite3_column_int64(PeerGetStmt, 6);
592  (*peer)->gossip_keyid = strdup_column_text(PeerGetStmt, 7);
593  (*peer)->gossip_keydata = strdup_column_text(PeerGetStmt, 8);
594 
595  rc = 1;
596 
597 cleanup:
598  mutt_addr_free(&norm_addr);
599  sqlite3_reset(PeerGetStmt);
600  return rc;
601 }
602 
610 int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
611 {
612  int rc = -1;
613  struct Address *norm_addr = NULL;
614 
615  norm_addr = copy_normalize_addr(addr);
616 
617  if (!PeerInsertStmt)
618  {
619  if (sqlite3_prepare_v3(AutocryptDB,
620  "INSERT INTO peer "
621  "(email_addr, "
622  "last_seen, "
623  "autocrypt_timestamp, "
624  "keyid, "
625  "keydata, "
626  "prefer_encrypt, "
627  "gossip_timestamp, "
628  "gossip_keyid, "
629  "gossip_keydata) "
630  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
631  -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
632  {
633  goto cleanup;
634  }
635  }
636 
637  if (sqlite3_bind_text(PeerInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
638  goto cleanup;
639  if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
640  goto cleanup;
641  if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
642  goto cleanup;
643  if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
644  goto cleanup;
645  if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
646  goto cleanup;
647  if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
648  goto cleanup;
649  if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
650  goto cleanup;
651  if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
652  goto cleanup;
653  if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
654  goto cleanup;
655 
656  if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
657  goto cleanup;
658 
659  rc = 0;
660 
661 cleanup:
662  mutt_addr_free(&norm_addr);
663  sqlite3_reset(PeerInsertStmt);
664  return rc;
665 }
666 
674 {
675  int rc = -1;
676 
677  if (!PeerUpdateStmt)
678  {
679  if (sqlite3_prepare_v3(AutocryptDB,
680  "UPDATE peer SET "
681  "last_seen = ?, "
682  "autocrypt_timestamp = ?, "
683  "keyid = ?, "
684  "keydata = ?, "
685  "prefer_encrypt = ?, "
686  "gossip_timestamp = ?, "
687  "gossip_keyid = ?, "
688  "gossip_keydata = ? "
689  "WHERE email_addr = ?;",
690  -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
691  {
692  goto cleanup;
693  }
694  }
695 
696  if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
697  goto cleanup;
698  if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
699  goto cleanup;
700  if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
701  goto cleanup;
702  if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
703  goto cleanup;
704  if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
705  goto cleanup;
706  if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
707  goto cleanup;
708  if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
709  goto cleanup;
710  if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
711  goto cleanup;
712  if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
713  goto cleanup;
714 
715  if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
716  goto cleanup;
717 
718  rc = 0;
719 
720 cleanup:
721  sqlite3_reset(PeerUpdateStmt);
722  return rc;
723 }
724 
730 {
731  return mutt_mem_calloc(1, sizeof(struct AutocryptPeerHistory));
732 }
733 
739 {
740  if (!ptr || !*ptr)
741  return;
742 
743  struct AutocryptPeerHistory *ph = *ptr;
744  FREE(&ph->peer_email_addr);
745  FREE(&ph->email_msgid);
746  FREE(&ph->keydata);
747  FREE(ptr);
748 }
749 
758  struct AutocryptPeerHistory *peerhist)
759 {
760  int rc = -1;
761 
762  struct Address *norm_addr = copy_normalize_addr(addr);
763 
765  {
766  if (sqlite3_prepare_v3(AutocryptDB,
767  "INSERT INTO peer_history "
768  "(peer_email_addr, "
769  "email_msgid, "
770  "timestamp, "
771  "keydata) "
772  "VALUES (?, ?, ?, ?);",
773  -1, SQLITE_PREPARE_PERSISTENT,
774  &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
775  {
776  goto cleanup;
777  }
778  }
779 
780  if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
781  goto cleanup;
782  if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
783  SQLITE_STATIC) != SQLITE_OK)
784  goto cleanup;
785  if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
786  goto cleanup;
787  if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
788  goto cleanup;
789 
790  if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
791  goto cleanup;
792 
793  rc = 0;
794 
795 cleanup:
796  mutt_addr_free(&norm_addr);
797  sqlite3_reset(PeerHistoryInsertStmt);
798  return rc;
799 }
800 
806 {
807  return mutt_mem_calloc(1, sizeof(struct AutocryptGossipHistory));
808 }
809 
815 {
816  if (!ptr || !*ptr)
817  return;
818 
819  struct AutocryptGossipHistory *gh = *ptr;
820  FREE(&gh->peer_email_addr);
821  FREE(&gh->sender_email_addr);
822  FREE(&gh->email_msgid);
823  FREE(&gh->gossip_keydata);
824  FREE(ptr);
825 }
826 
835  struct AutocryptGossipHistory *gossip_hist)
836 {
837  int rc = -1;
838 
839  struct Address *norm_addr = copy_normalize_addr(addr);
840 
842  {
843  if (sqlite3_prepare_v3(AutocryptDB,
844  "INSERT INTO gossip_history "
845  "(peer_email_addr, "
846  "sender_email_addr, "
847  "email_msgid, "
848  "timestamp, "
849  "gossip_keydata) "
850  "VALUES (?, ?, ?, ?, ?);",
851  -1, SQLITE_PREPARE_PERSISTENT,
852  &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
853  {
854  goto cleanup;
855  }
856  }
857 
858  if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, norm_addr->mailbox, -1,
859  SQLITE_STATIC) != SQLITE_OK)
860  {
861  goto cleanup;
862  }
863  if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
864  -1, SQLITE_STATIC) != SQLITE_OK)
865  {
866  if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
867  -1, SQLITE_STATIC) != SQLITE_OK)
868  {
869  goto cleanup;
870  }
871  }
872  if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
873  goto cleanup;
874  if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
875  -1, SQLITE_STATIC) != SQLITE_OK)
876  {
877  goto cleanup;
878  }
879 
880  if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
881  goto cleanup;
882 
883  rc = 0;
884 
885 cleanup:
886  mutt_addr_free(&norm_addr);
887  sqlite3_reset(GossipHistoryInsertStmt);
888  return rc;
889 }
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:125
void mutt_autocrypt_db_normalize_addr(struct Address *a)
Normalise an Email Address.
Definition: db.c:160
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:729
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:239
Autocrypt end-to-end encryption.
void mutt_autocrypt_db_gossip_history_free(struct AutocryptGossipHistory **ptr)
Free an AutocryptGossipHistory.
Definition: db.c:814
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:1379
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:514
String manipulation buffer.
Definition: buffer.h:33
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
#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:230
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:1342
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.
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:414
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:897
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:197
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:509
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define mutt_b2s(buf)
Definition: buffer.h:41
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:368
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:610
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:805
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:757
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:258
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:171
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:673
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:1268
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:1297
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:523
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:738
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:545
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:834
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:449
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:317
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:220