2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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"
|
|
|
|
#include "td/utils/port/path.h"
|
|
|
|
#include "td/utils/port/Stat.h"
|
|
|
|
#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;
|
|
|
|
}
|
2020-08-18 13:55:47 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
Status SqliteDb::init(CSlice path, bool *was_created) {
|
|
|
|
// If database does not exist, delete all other files which may left
|
|
|
|
// from older database
|
2021-03-12 12:31:51 +01:00
|
|
|
bool is_db_exists = stat(path).is_ok();
|
|
|
|
if (!is_db_exists) {
|
|
|
|
TRY_STATUS(destroy(path));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-05-23 21:27:24 +02:00
|
|
|
|
2021-03-12 12:31:51 +01:00
|
|
|
if (was_created != nullptr) {
|
|
|
|
*was_created = !is_db_exists;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
sqlite3 *db;
|
|
|
|
CHECK(sqlite3_threadsafe() != 0);
|
2021-03-12 12:31:51 +01:00
|
|
|
int rc = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE /*| SQLITE_OPEN_SHAREDCACHE*/,
|
2018-12-31 20:04:05 +01:00
|
|
|
nullptr);
|
|
|
|
if (rc != SQLITE_OK) {
|
2020-02-19 02:34:55 +01:00
|
|
|
auto res = Status::Error(PSLICE() << "Failed to open database: " << detail::RawSqliteDb::last_error(db, path));
|
2018-12-31 20:04:05 +01:00
|
|
|
sqlite3_close(db);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
sqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
|
|
|
|
raw_ = std::make_shared<detail::RawSqliteDb>(db, path.str());
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trace_callback(void *ptr, const char *query) {
|
|
|
|
LOG(ERROR) << query;
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
trace_callback(ctx, sqlite3_expanded_sql(static_cast<sqlite3_stmt *>(p_raw)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void SqliteDb::trace(bool flag) {
|
|
|
|
sqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto rc = sqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
|
2020-03-28 22:12:51 +01:00
|
|
|
if (enable_logging_) {
|
|
|
|
VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
CHECK(msg != nullptr);
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SqliteDb::begin_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
|
|
|
|
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() {
|
2021-03-12 12:31: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
|
|
|
}
|
|
|
|
|
2020-08-14 16:48:43 +02:00
|
|
|
Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key, optional<int32> cipher_version) {
|
|
|
|
auto res = do_open_with_key(path, db_key, cipher_version ? cipher_version.value() : 0);
|
|
|
|
if (res.is_error() && !cipher_version) {
|
|
|
|
return do_open_with_key(path, db_key, 3);
|
2020-08-12 21:36:46 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-14 16:48:43 +02:00
|
|
|
Result<SqliteDb> SqliteDb::do_open_with_key(CSlice path, const DbKey &db_key, int32 cipher_version) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteDb db;
|
|
|
|
TRY_STATUS(db.init(path));
|
2021-03-12 12:31:51 +01:00
|
|
|
if (!db_key.is_empty()) {
|
|
|
|
if (db.check_encryption().is_ok()) {
|
|
|
|
return Status::Error(PSLICE() << "No key is needed for database \"" << path << '"');
|
|
|
|
}
|
|
|
|
auto key = db_key_to_sqlcipher_key(db_key);
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key));
|
|
|
|
if (cipher_version != 0) {
|
|
|
|
LOG(INFO) << "Trying SQLCipher compatibility mode with version = " << cipher_version;
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA cipher_compatibility = " << cipher_version));
|
|
|
|
}
|
|
|
|
db.set_cipher_version(cipher_version);
|
|
|
|
}
|
|
|
|
TRY_STATUS_PREFIX(db.check_encryption(), "Can't open 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) {
|
2021-03-12 12:31:51 +01:00
|
|
|
raw_->set_cipher_version(cipher_version);
|
2020-08-14 16:48:43 +02:00
|
|
|
}
|
2021-03-12 12:31:51 +01:00
|
|
|
|
2020-08-14 18:53:26 +02:00
|
|
|
optional<int32> SqliteDb::get_cipher_version() const {
|
2021-03-12 12:31:51 +01:00
|
|
|
return raw_->get_cipher_version();
|
2020-08-14 16:48:43 +02:00
|
|
|
}
|
2021-03-12 12:31:51 +01:00
|
|
|
|
2020-08-14 16:11:58 +02:00
|
|
|
Result<SqliteDb> SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key) {
|
2021-03-12 12:31:51 +01:00
|
|
|
PerfWarningTimer perf("change key", 0.001);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-03-12 12:31:51 +01:00
|
|
|
// fast path
|
|
|
|
{
|
|
|
|
auto r_db = open_with_key(path, new_db_key);
|
|
|
|
if (r_db.is_ok()) {
|
|
|
|
return r_db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRY_RESULT(db, open_with_key(path, old_db_key));
|
|
|
|
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";
|
|
|
|
PerfWarningTimer timer("Encrypt SQLite database", 0.1);
|
|
|
|
auto tmp_path = path.str() + ".encrypted";
|
|
|
|
TRY_STATUS(destroy(tmp_path));
|
2020-05-23 21:27:24 +02:00
|
|
|
|
2021-03-12 12:31:51 +01:00
|
|
|
// make shure that database is not empty
|
|
|
|
TRY_STATUS(db.exec("CREATE TABLE IF NOT EXISTS encryption_dummy_table(id INT PRIMARY KEY)"));
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "ATTACH DATABASE '" << quote_string(tmp_path) << "' AS encrypted KEY " << new_key));
|
|
|
|
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";
|
|
|
|
PerfWarningTimer timer("Decrypt SQLite database", 0.1);
|
|
|
|
auto tmp_path = path.str() + ".encrypted";
|
|
|
|
TRY_STATUS(destroy(tmp_path));
|
|
|
|
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "ATTACH DATABASE '" << quote_string(tmp_path) << "' AS decrypted KEY ''"));
|
|
|
|
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";
|
|
|
|
PerfWarningTimer timer("Rekey SQLite database", 0.1);
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "PRAGMA rekey = " << new_key));
|
|
|
|
}
|
|
|
|
|
|
|
|
TRY_RESULT(new_db, open_with_key(path, new_db_key));
|
|
|
|
LOG_CHECK(new_db.user_version().ok() == user_version) << new_db.user_version().ok() << " " << user_version;
|
|
|
|
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) {
|
|
|
|
sqlite3_stmt *stmt = nullptr;
|
|
|
|
auto rc = sqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
|
|
|
|
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
|