2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/db/SqliteDb.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
2021-09-22 16:05:52 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/path.h"
|
|
|
|
#include "td/utils/port/Stat.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/Timer.h"
|
|
|
|
|
|
|
|
#include "sqlite/sqlite3.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace {
|
2020-11-18 17:55:42 +01:00
|
|
|
string quote_string(Slice str) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
for (auto &c : str) {
|
|
|
|
if (c == '\'') {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cnt == 0) {
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
string result;
|
|
|
|
result.reserve(str.size() + cnt);
|
|
|
|
for (auto &c : str) {
|
|
|
|
if (c == '\'') {
|
|
|
|
result += '\'';
|
|
|
|
}
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
string db_key_to_sqlcipher_key(const DbKey &db_key) {
|
|
|
|
if (db_key.is_empty()) {
|
|
|
|
return "''";
|
|
|
|
}
|
|
|
|
if (db_key.is_password()) {
|
2020-11-18 17:55:42 +01:00
|
|
|
return PSTRING() << "'" << quote_string(db_key.data()) << "'";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
CHECK(db_key.is_raw_key());
|
|
|
|
Slice raw_key = db_key.data();
|
|
|
|
CHECK(raw_key.size() == 32);
|
|
|
|
size_t expected_size = 64 + 5;
|
|
|
|
string res(expected_size + 50, ' ');
|
|
|
|
StringBuilder sb(res);
|
|
|
|
sb << '"';
|
|
|
|
sb << 'x';
|
|
|
|
sb << '\'';
|
|
|
|
sb << format::as_hex_dump<0>(raw_key);
|
|
|
|
sb << '\'';
|
|
|
|
sb << '"';
|
|
|
|
CHECK(!sb.is_error());
|
|
|
|
CHECK(sb.as_cslice().size() == expected_size);
|
|
|
|
res.resize(expected_size);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SqliteDb::~SqliteDb() = default;
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
Status SqliteDb::init(CSlice path, bool allow_creation) {
|
2021-09-22 16:05:52 +02:00
|
|
|
// if database does not exist, delete all other files which could have been left from the old database
|
2021-11-14 08:07:04 +01:00
|
|
|
auto database_stat = stat(path);
|
|
|
|
if (database_stat.is_error()) {
|
2021-09-22 18:04:56 +02:00
|
|
|
if (!allow_creation) {
|
2022-06-30 00:32:32 +02:00
|
|
|
bool was_destroyed = detail::RawSqliteDb::was_any_database_destroyed();
|
|
|
|
auto reason = was_destroyed ? Slice("was corrupted and deleted") : Slice("disappeared");
|
2022-07-30 11:33:57 +02:00
|
|
|
return Status::Error(PSLICE() << "Database " << reason
|
|
|
|
<< " during execution and can't be recreated: " << database_stat.error());
|
2021-09-22 18:04:56 +02:00
|
|
|
}
|
2018-06-07 21:56:03 +02:00
|
|
|
TRY_STATUS(destroy(path));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3 *db;
|
|
|
|
CHECK(tdsqlite3_threadsafe() != 0);
|
2021-09-22 19:17:37 +02:00
|
|
|
int rc =
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | (allow_creation ? SQLITE_OPEN_CREATE : 0), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (rc != SQLITE_OK) {
|
2021-09-22 13:56:48 +02:00
|
|
|
auto res = detail::RawSqliteDb::last_error(db, path);
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3_close(db);
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
|
2018-12-31 20:04:05 +01:00
|
|
|
raw_ = std::make_shared<detail::RawSqliteDb>(db, path.str());
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trace_callback(void *ptr, const char *query) {
|
|
|
|
LOG(ERROR) << query;
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static int trace_v2_callback(unsigned code, void *ctx, void *p_raw, void *x_raw) {
|
|
|
|
CHECK(code == SQLITE_TRACE_STMT);
|
|
|
|
auto x = static_cast<const char *>(x_raw);
|
|
|
|
if (x[0] == '-' && x[1] == '-') {
|
|
|
|
trace_callback(ctx, x);
|
|
|
|
} else {
|
2022-08-04 13:37:08 +02:00
|
|
|
trace_callback(ctx, tdsqlite3_expanded_sql(static_cast<tdsqlite3_stmt *>(p_raw)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SqliteDb::trace(bool flag) {
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SqliteDb::exec(CSlice cmd) {
|
|
|
|
CHECK(!empty());
|
|
|
|
char *msg;
|
2020-03-28 22:12:51 +01:00
|
|
|
if (enable_logging_) {
|
|
|
|
VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
|
|
|
|
}
|
2022-08-04 13:37:08 +02:00
|
|
|
auto rc = tdsqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
CHECK(msg != nullptr);
|
2022-10-05 17:24:19 +02:00
|
|
|
if (enable_logging_) {
|
|
|
|
VLOG(sqlite) << "Finish exec with error " << msg;
|
|
|
|
}
|
2020-02-19 02:34:55 +01:00
|
|
|
return Status::Error(PSLICE() << tag("query", cmd) << " to database \"" << raw_->path() << "\" failed: " << msg);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
CHECK(msg == nullptr);
|
2022-10-05 17:24:19 +02:00
|
|
|
if (enable_logging_) {
|
|
|
|
VLOG(sqlite) << "Finish exec";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<bool> SqliteDb::has_table(Slice table) {
|
|
|
|
TRY_RESULT(stmt, get_statement(PSLICE() << "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" << table
|
|
|
|
<< "'"));
|
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
CHECK(stmt.has_row());
|
|
|
|
auto cnt = stmt.view_int32(0);
|
|
|
|
return cnt == 1;
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<string> SqliteDb::get_pragma(Slice name) {
|
|
|
|
TRY_RESULT(stmt, get_statement(PSLICE() << "PRAGMA " << name));
|
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
CHECK(stmt.has_row());
|
|
|
|
auto res = stmt.view_blob(0).str();
|
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
CHECK(!stmt.can_step());
|
|
|
|
return std::move(res);
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
|
2020-08-12 21:36:46 +02:00
|
|
|
Result<string> SqliteDb::get_pragma_string(Slice name) {
|
|
|
|
TRY_RESULT(stmt, get_statement(PSLICE() << "PRAGMA " << name));
|
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
CHECK(stmt.has_row());
|
|
|
|
auto res = stmt.view_string(0).str();
|
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
CHECK(!stmt.can_step());
|
|
|
|
return std::move(res);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<int32> SqliteDb::user_version() {
|
|
|
|
TRY_RESULT(get_version_stmt, get_statement("PRAGMA user_version"));
|
|
|
|
TRY_STATUS(get_version_stmt.step());
|
|
|
|
if (!get_version_stmt.has_row()) {
|
2020-02-19 02:34:55 +01:00
|
|
|
return Status::Error(PSLICE() << "PRAGMA user_version failed for database \"" << raw_->path() << '"');
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_version_stmt.view_int32(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SqliteDb::set_user_version(int32 version) {
|
|
|
|
return exec(PSLICE() << "PRAGMA user_version = " << version);
|
|
|
|
}
|
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
Status SqliteDb::begin_read_transaction() {
|
2019-08-09 13:31:45 +02:00
|
|
|
if (raw_->on_begin()) {
|
|
|
|
return exec("BEGIN");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-09 13:31:45 +02:00
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
Status SqliteDb::begin_write_transaction() {
|
|
|
|
if (raw_->on_begin()) {
|
|
|
|
return exec("BEGIN IMMEDIATE");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SqliteDb::commit_transaction() {
|
2019-08-09 13:31:45 +02:00
|
|
|
TRY_RESULT(need_commit, raw_->on_commit());
|
|
|
|
if (need_commit) {
|
|
|
|
return exec("COMMIT");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 13:12:22 +01:00
|
|
|
Status SqliteDb::check_encryption() {
|
2020-03-28 22:12:51 +01:00
|
|
|
auto status = exec("SELECT count(*) FROM sqlite_master");
|
|
|
|
if (status.is_ok()) {
|
|
|
|
enable_logging_ = true;
|
|
|
|
}
|
|
|
|
return status;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
Result<SqliteDb> SqliteDb::open_with_key(CSlice path, bool allow_creation, const DbKey &db_key,
|
|
|
|
optional<int32> cipher_version) {
|
|
|
|
auto res = do_open_with_key(path, allow_creation, db_key, cipher_version ? cipher_version.value() : 0);
|
2021-09-22 16:05:52 +02:00
|
|
|
if (res.is_error() && !cipher_version && !db_key.is_empty()) {
|
2021-09-22 18:04:56 +02:00
|
|
|
return do_open_with_key(path, false, db_key, 3);
|
2020-08-12 21:36:46 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
Result<SqliteDb> SqliteDb::do_open_with_key(CSlice path, bool allow_creation, const DbKey &db_key,
|
|
|
|
int32 cipher_version) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteDb db;
|
2021-09-22 18:04:56 +02:00
|
|
|
TRY_STATUS(db.init(path, allow_creation));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!db_key.is_empty()) {
|
2020-01-27 13:12:22 +01:00
|
|
|
if (db.check_encryption().is_ok()) {
|
2020-02-19 02:34:55 +01:00
|
|
|
return Status::Error(PSLICE() << "No key is needed for database \"" << path << '"');
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto key = db_key_to_sqlcipher_key(db_key);
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key));
|
2020-08-14 16:48:43 +02:00
|
|
|
if (cipher_version != 0) {
|
2020-08-14 21:51:10 +02:00
|
|
|
LOG(INFO) << "Trying SQLCipher compatibility mode with version = " << cipher_version;
|
2020-08-14 16:48:43 +02:00
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA cipher_compatibility = " << cipher_version));
|
2020-08-12 21:36:46 +02:00
|
|
|
}
|
2020-08-14 21:51:10 +02:00
|
|
|
db.set_cipher_version(cipher_version);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-22 16:05:52 +02:00
|
|
|
TRY_STATUS_PREFIX(db.check_encryption(), "Can't check database: ");
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::move(db);
|
|
|
|
}
|
|
|
|
|
2020-08-14 16:48:43 +02:00
|
|
|
void SqliteDb::set_cipher_version(int32 cipher_version) {
|
|
|
|
raw_->set_cipher_version(cipher_version);
|
|
|
|
}
|
|
|
|
|
2020-08-14 18:53:26 +02:00
|
|
|
optional<int32> SqliteDb::get_cipher_version() const {
|
2020-08-14 16:48:43 +02:00
|
|
|
return raw_->get_cipher_version();
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
Result<SqliteDb> SqliteDb::change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
|
|
|
|
const DbKey &old_db_key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// fast path
|
|
|
|
{
|
2022-02-13 20:51:53 +01:00
|
|
|
PerfWarningTimer perf("open database", 0.05);
|
2021-09-22 18:04:56 +02:00
|
|
|
auto r_db = open_with_key(path, allow_creation, new_db_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_db.is_ok()) {
|
2020-08-14 16:11:58 +02:00
|
|
|
return r_db;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 20:51:53 +01:00
|
|
|
PerfWarningTimer perf("change database key", 0.5);
|
2021-09-22 19:17:37 +02:00
|
|
|
auto create_database = [](CSlice tmp_path) -> Status {
|
|
|
|
TRY_STATUS(destroy(tmp_path));
|
|
|
|
SqliteDb db;
|
|
|
|
return db.init(tmp_path, true);
|
|
|
|
};
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
TRY_RESULT(db, open_with_key(path, false, old_db_key));
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(user_version, db.user_version());
|
|
|
|
auto new_key = db_key_to_sqlcipher_key(new_db_key);
|
|
|
|
if (old_db_key.is_empty() && !new_db_key.is_empty()) {
|
|
|
|
LOG(DEBUG) << "ENCRYPT";
|
2018-12-13 23:48:36 +01:00
|
|
|
PerfWarningTimer timer("Encrypt SQLite database", 0.1);
|
2020-11-18 17:55:42 +01:00
|
|
|
auto tmp_path = path.str() + ".encrypted";
|
2021-09-22 19:17:37 +02:00
|
|
|
TRY_STATUS(create_database(tmp_path));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-22 19:17:37 +02:00
|
|
|
// make sure that database is not empty
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec("CREATE TABLE IF NOT EXISTS encryption_dummy_table(id INT PRIMARY KEY)"));
|
2020-11-18 17:55:42 +01:00
|
|
|
TRY_STATUS(db.exec(PSLICE() << "ATTACH DATABASE '" << quote_string(tmp_path) << "' AS encrypted KEY " << new_key));
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec("SELECT sqlcipher_export('encrypted')"));
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA encrypted.user_version = " << user_version));
|
|
|
|
TRY_STATUS(db.exec("DETACH DATABASE encrypted"));
|
|
|
|
db.close();
|
|
|
|
TRY_STATUS(rename(tmp_path, path));
|
|
|
|
} else if (!old_db_key.is_empty() && new_db_key.is_empty()) {
|
|
|
|
LOG(DEBUG) << "DECRYPT";
|
2018-12-13 23:48:36 +01:00
|
|
|
PerfWarningTimer timer("Decrypt SQLite database", 0.1);
|
2020-11-18 17:55:42 +01:00
|
|
|
auto tmp_path = path.str() + ".encrypted";
|
2021-09-22 19:17:37 +02:00
|
|
|
TRY_STATUS(create_database(tmp_path));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-18 17:55:42 +01:00
|
|
|
TRY_STATUS(db.exec(PSLICE() << "ATTACH DATABASE '" << quote_string(tmp_path) << "' AS decrypted KEY ''"));
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec("SELECT sqlcipher_export('decrypted')"));
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA decrypted.user_version = " << user_version));
|
|
|
|
TRY_STATUS(db.exec("DETACH DATABASE decrypted"));
|
|
|
|
db.close();
|
|
|
|
TRY_STATUS(rename(tmp_path, path));
|
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "REKEY";
|
2018-12-13 23:48:36 +01:00
|
|
|
PerfWarningTimer timer("Rekey SQLite database", 0.1);
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA rekey = " << new_key));
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
TRY_RESULT(new_db, open_with_key(path, false, new_db_key));
|
|
|
|
CHECK(new_db.user_version().ok() == user_version);
|
2020-08-14 16:11:58 +02:00
|
|
|
return std::move(new_db);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
Status SqliteDb::destroy(Slice path) {
|
|
|
|
return detail::RawSqliteDb::destroy(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<SqliteStatement> SqliteDb::get_statement(CSlice statement) {
|
2022-08-04 13:37:08 +02:00
|
|
|
tdsqlite3_stmt *stmt = nullptr;
|
|
|
|
auto rc =
|
|
|
|
tdsqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (rc != SQLITE_OK) {
|
2018-12-13 23:48:36 +01:00
|
|
|
return Status::Error(PSLICE() << "Failed to prepare SQLite " << tag("statement", statement) << raw_->last_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(stmt != nullptr) << statement;
|
2018-12-31 20:04:05 +01:00
|
|
|
return SqliteStatement(stmt, raw_);
|
|
|
|
}
|
2018-07-20 15:15:19 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|