···11+/*
22+ * Copyright (C) 2019 Kevin J. McCarthy <kevin@8t8.us>
33+ *
44+ * This program is free software; you can redistribute it and/or modify
55+ * it under the terms of the GNU General Public License as published by
66+ * the Free Software Foundation; either version 2 of the License, or
77+ * (at your option) any later version.
88+ *
99+ * This program is distributed in the hope that it will be useful,
1010+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212+ * GNU General Public License for more details.
1313+ *
1414+ * You should have received a copy of the GNU General Public License
1515+ * along with this program; if not, write to the Free Software
1616+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717+ */
1818+1919+#if HAVE_CONFIG_H
2020+# include "config.h"
2121+#endif
2222+2323+#include "mutt.h"
2424+#include "autocrypt.h"
2525+#include "autocrypt_private.h"
2626+2727+static int autocrypt_db_create (const char *db_path)
2828+{
2929+ if (sqlite3_open_v2 (db_path,
3030+ &AutocryptDB,
3131+ SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,
3232+ NULL) != SQLITE_OK)
3333+ {
3434+ mutt_error (_("Unable to open autocrypt database %s"), db_path);
3535+ mutt_sleep (0);
3636+ return -1;
3737+ }
3838+ return mutt_autocrypt_schema_init ();
3939+}
4040+4141+int mutt_autocrypt_db_init (int can_create)
4242+{
4343+ int rv = -1;
4444+ BUFFER *db_path = NULL;
4545+ struct stat sb;
4646+4747+ if (AutocryptDB)
4848+ return 0;
4949+5050+ if (!option (OPTAUTOCRYPT) || !AutocryptDir)
5151+ return -1;
5252+5353+ db_path = mutt_buffer_pool_get ();
5454+ mutt_buffer_concat_path (db_path, AutocryptDir, "autocrypt.db");
5555+ if (stat (mutt_b2s (db_path), &sb))
5656+ {
5757+ if (!can_create || autocrypt_db_create (mutt_b2s (db_path)))
5858+ goto cleanup;
5959+ }
6060+ else
6161+ {
6262+ if (sqlite3_open_v2 (mutt_b2s (db_path),
6363+ &AutocryptDB,
6464+ SQLITE_OPEN_READWRITE,
6565+ NULL) != SQLITE_OK)
6666+ {
6767+ mutt_error (_("Unable to open autocrypt database %s"), mutt_b2s (db_path));
6868+ mutt_sleep (0);
6969+ goto cleanup;
7070+ }
7171+ }
7272+7373+ if (mutt_autocrypt_schema_update ())
7474+ goto cleanup;
7575+7676+ rv = 0;
7777+7878+cleanup:
7979+ mutt_buffer_pool_release (&db_path);
8080+ return rv;
8181+}
8282+8383+void mutt_autocrypt_db_close (void)
8484+{
8585+ if (!AutocryptDB)
8686+ return;
8787+8888+ /* TODO:
8989+ * call sqlite3_finalize () for each prepared statement
9090+ */
9191+9292+ sqlite3_close_v2 (AutocryptDB);
9393+ AutocryptDB = NULL;
9494+}
+29
autocrypt/autocrypt_private.h
···11+/*
22+ * Copyright (C) 2019 Kevin J. McCarthy <kevin@8t8.us>
33+ *
44+ * This program is free software; you can redistribute it and/or modify
55+ * it under the terms of the GNU General Public License as published by
66+ * the Free Software Foundation; either version 2 of the License, or
77+ * (at your option) any later version.
88+ *
99+ * This program is distributed in the hope that it will be useful,
1010+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212+ * GNU General Public License for more details.
1313+ *
1414+ * You should have received a copy of the GNU General Public License
1515+ * along with this program; if not, write to the Free Software
1616+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717+ */
1818+1919+#ifndef _AUTOCRYPT_PRIVATE_H
2020+#define _AUTOCRYPT_PRIVATE_H 1
2121+2222+int mutt_autocrypt_db_init (int can_create);
2323+void mutt_autocrypt_db_close (void);
2424+2525+int mutt_autocrypt_schema_init (void);
2626+int mutt_autocrypt_schema_update (void);
2727+2828+2929+#endif
+129
autocrypt/autocrypt_schema.c
···11+/*
22+ * Copyright (C) 2019 Kevin J. McCarthy <kevin@8t8.us>
33+ *
44+ * This program is free software; you can redistribute it and/or modify
55+ * it under the terms of the GNU General Public License as published by
66+ * the Free Software Foundation; either version 2 of the License, or
77+ * (at your option) any later version.
88+ *
99+ * This program is distributed in the hope that it will be useful,
1010+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212+ * GNU General Public License for more details.
1313+ *
1414+ * You should have received a copy of the GNU General Public License
1515+ * along with this program; if not, write to the Free Software
1616+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717+ */
1818+1919+#if HAVE_CONFIG_H
2020+# include "config.h"
2121+#endif
2222+2323+#include "mutt.h"
2424+#include "autocrypt.h"
2525+#include "autocrypt_private.h"
2626+2727+int mutt_autocrypt_schema_init (void)
2828+{
2929+ const char *schema;
3030+ char *errmsg = NULL;
3131+3232+ schema =
3333+ "BEGIN TRANSACTION; "
3434+3535+ "CREATE TABLE account ("
3636+ "email_addr text primary key not null, "
3737+ "keyid text, "
3838+ "keydata text, "
3939+ "prefer_encrypt int, "
4040+ "enabled int);"
4141+4242+ "CREATE TABLE peer ("
4343+ "email_addr text primary key not null, "
4444+ "last_seen int, "
4545+ "autocrypt_timestamp int, "
4646+ "keyid text, "
4747+ "keydata text, "
4848+ "prefer_encrypt int, "
4949+ "gossip_timestamp int, "
5050+ "gossip_keyid text, "
5151+ "gossip_keydata text);"
5252+5353+ "CREATE TABLE peer_history ("
5454+ "peer_email_addr text not null, "
5555+ "email_msgid text, "
5656+ "timestamp int, "
5757+ "keydata text);"
5858+5959+ "CREATE INDEX peer_history_email "
6060+ "ON peer_history ("
6161+ "peer_email_addr);"
6262+6363+ "CREATE TABLE gossip_history ("
6464+ "peer_email_addr text not null, "
6565+ "sender_email_addr text, "
6666+ "email_msgid text, "
6767+ "timestamp int, "
6868+ "gossip_keydata text);"
6969+7070+ "CREATE INDEX gossip_history_email "
7171+ "ON gossip_history ("
7272+ "peer_email_addr);"
7373+7474+ "CREATE TABLE schema ("
7575+ "version int);"
7676+7777+ "INSERT into schema (version) values (1);"
7878+7979+ "COMMIT TRANSACTION";
8080+8181+ if (sqlite3_exec (AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
8282+ {
8383+ dprint (1, (debugfile, "mutt_autocrypt_schema_init() returned %s\n", errmsg));
8484+ sqlite3_free (errmsg);
8585+ return -1;
8686+ }
8787+ return 0;
8888+}
8989+9090+int mutt_autocrypt_schema_update (void)
9191+{
9292+ sqlite3_stmt *stmt = NULL;
9393+ int rv = -1, version;
9494+9595+ if (sqlite3_prepare_v2 (
9696+ AutocryptDB,
9797+ "SELECT version FROM schema;",
9898+ -1,
9999+ &stmt,
100100+ NULL) != SQLITE_OK)
101101+ goto cleanup;
102102+103103+ if (sqlite3_step (stmt) != SQLITE_ROW)
104104+ goto cleanup;
105105+106106+ version = sqlite3_column_int (stmt, 0);
107107+108108+ if (version > 1)
109109+ {
110110+ /* L10N:
111111+ The autocrypt database keeps track of schema version numbers.
112112+ This error occurs if the version number is too high.
113113+ Presumably because this is an old version of mutt and the
114114+ database was upgraded by a future version.
115115+ */
116116+ mutt_error _("Autocrypt database version is too new");
117117+ mutt_sleep (0);
118118+ goto cleanup;
119119+ }
120120+121121+ /* TODO: schema version upgrades go here.
122122+ * Bump one by one, each update inside a transaction. */
123123+124124+ rv = 0;
125125+126126+cleanup:
127127+ sqlite3_finalize (stmt);
128128+ return rv;
129129+}
+35
lib.c
···307307 return (s);
308308}
309309310310+int mutt_mkdir (char *path, mode_t mode)
311311+{
312312+ struct stat sb;
313313+ char *s;
314314+ int rv = -1;
315315+316316+ if (stat (path, &sb) >= 0)
317317+ return 0;
318318+319319+ s = path;
320320+ do
321321+ {
322322+ s = strchr (s + 1, '/');
323323+ if (s)
324324+ *s = '\0';
325325+ if (stat (path, &sb) < 0)
326326+ {
327327+ if (errno != ENOENT)
328328+ goto cleanup;
329329+ if (mkdir (path, mode) < 0)
330330+ goto cleanup;
331331+ }
332332+ if (s)
333333+ *s = '/';
334334+ } while (s);
335335+336336+ rv = 0;
337337+338338+cleanup:
339339+ if (s)
340340+ *s = '/';
341341+342342+ return rv;
343343+}
344344+310345void mutt_unlink (const char *s)
311346{
312347 int fd;