Skip to content
API Documentation
BastionGuard™
LeakDatabase Class Reference

#include <LeakDatabase.hpp>

Public Member Functions

 LeakDatabase ()
void ensureIdentity (const std::string &emailHash)
bool isNewLeak (const std::string &emailHash, const LeakRecord &record)
void storeLeak (const std::string &emailHash, const LeakRecord &record)

Private Member Functions

void open ()
void initSchema ()

Private Attributes

std::string dbPath
void * db
std::mutex dbMutex

Detailed Description

Definition at line 27 of file LeakDatabase.hpp.

Constructor & Destructor Documentation

◆ LeakDatabase()

LeakDatabase::LeakDatabase ( )

Definition at line 46 of file LeakDatabase.cpp.

47: db(nullptr)
48{
49 // getenv("HOME") può restituire NULL (sandbox, setuid, ambiente pulito).
50 // std::string(nullptr) è UB: proteggiamoci con un fallback ragionevole.
51 const char* home_env = std::getenv("HOME");
52 std::string base;
53 if (home_env && *home_env) {
54 base = std::string(home_env) + "/.local/share/BastionGuard";
55 } else {
56 base = "/tmp/BastionGuard";
57 }
58
59 fs::create_directories(base);
60 dbPath = base + "/identity_leaks.db";
61
62 open();
63 initSchema();
64}
std::string dbPath

References db, dbPath, initSchema(), and open().

Member Function Documentation

◆ ensureIdentity()

void LeakDatabase::ensureIdentity ( const std::string & emailHash)

Definition at line 100 of file LeakDatabase.cpp.

101{
102 std::lock_guard<std::mutex> lock(dbMutex);
103
104 const char* sql =
105 "INSERT OR IGNORE INTO identity(email_hash) VALUES (?);";
106
107 sqlite3_stmt* stmt = nullptr;
108 if (sqlite3_prepare_v2(
109 reinterpret_cast<sqlite3*>(db),
110 sql,
111 -1,
112 &stmt,
113 nullptr) != SQLITE_OK || !stmt)
114 {
115 // Se prepare fallisce, stmt è NULL e bind/step andrebbero in crash.
116 return;
117 }
118
119 sqlite3_bind_text(stmt, 1,
120 emailHash.c_str(), -1, SQLITE_TRANSIENT);
121
122 sqlite3_step(stmt);
123 sqlite3_finalize(stmt);
124}
std::mutex dbMutex

References db, and dbMutex.

Referenced by LeakCheckWorker::run().

◆ initSchema()

void LeakDatabase::initSchema ( )
private

Definition at line 81 of file LeakDatabase.cpp.

82{
83 std::lock_guard<std::mutex> lock(dbMutex);
84
85 char* err = nullptr;
86 if (sqlite3_exec(
87 reinterpret_cast<sqlite3*>(db),
89 nullptr,
90 nullptr,
91 &err) != SQLITE_OK)
92 {
93 std::string msg = err ? err : "unknown";
94 sqlite3_free(err);
95 throw std::runtime_error(
96 "SQLite schema error: " + msg);
97 }
98}
static const char * SCHEMA_SQL

References db, dbMutex, and SCHEMA_SQL.

Referenced by LeakDatabase().

◆ isNewLeak()

bool LeakDatabase::isNewLeak ( const std::string & emailHash,
const LeakRecord & record )

Definition at line 126 of file LeakDatabase.cpp.

129{
130 std::lock_guard<std::mutex> lock(dbMutex);
131
132 const char* sql =
133 "SELECT 1 FROM leak "
134 "WHERE email_hash=? AND breach_name=? AND breach_date=?;";
135
136 sqlite3_stmt* stmt = nullptr;
137 if (sqlite3_prepare_v2(
138 reinterpret_cast<sqlite3*>(db),
139 sql,
140 -1,
141 &stmt,
142 nullptr) != SQLITE_OK || !stmt)
143 {
144 // prepare fallita: trattiamolo come "nuovo leak" per non perdere notifiche.
145 return true;
146 }
147
148 sqlite3_bind_text(stmt, 1,
149 emailHash.c_str(), -1, SQLITE_TRANSIENT);
150 sqlite3_bind_text(stmt, 2,
151 record.breachName.c_str(), -1, SQLITE_TRANSIENT);
152 sqlite3_bind_text(stmt, 3,
153 record.breachDate.c_str(), -1, SQLITE_TRANSIENT);
154
155 int rc = sqlite3_step(stmt);
156 sqlite3_finalize(stmt);
157
158 return rc != SQLITE_ROW;
159}
std::string breachDate
std::string breachName

References LeakRecord::breachDate, LeakRecord::breachName, db, and dbMutex.

◆ open()

void LeakDatabase::open ( )
private

Definition at line 66 of file LeakDatabase.cpp.

67{
68 std::lock_guard<std::mutex> lock(dbMutex);
69
70 sqlite3* handle = nullptr;
71 if (sqlite3_open(dbPath.c_str(), &handle) != SQLITE_OK)
72 {
73 // Anche se open fallisce, SQLite alloca un handle che va chiuso
74 // (documentato in https://sqlite.org/c3ref/open.html).
75 if (handle) sqlite3_close(handle);
76 throw std::runtime_error(_("SQLite: open failed"));
77 }
78 db = handle;
79}
#define _(s)

References _, db, dbMutex, and dbPath.

Referenced by LeakDatabase().

◆ storeLeak()

void LeakDatabase::storeLeak ( const std::string & emailHash,
const LeakRecord & record )

Definition at line 161 of file LeakDatabase.cpp.

164{
165 std::lock_guard<std::mutex> lock(dbMutex);
166
167 const char* sql =
168 "INSERT INTO leak "
169 "(email_hash, breach_name, breach_date, severity, provider, description) "
170 "VALUES (?, ?, ?, ?, ?, ?);";
171
172 sqlite3_stmt* stmt = nullptr;
173 if (sqlite3_prepare_v2(
174 reinterpret_cast<sqlite3*>(db),
175 sql,
176 -1,
177 &stmt,
178 nullptr) != SQLITE_OK || !stmt)
179 {
180 // prepare fallita: non possiamo scrivere, ma non crashiamo.
181 return;
182 }
183
184 sqlite3_bind_text(stmt, 1,
185 emailHash.c_str(), -1, SQLITE_TRANSIENT);
186 sqlite3_bind_text(stmt, 2,
187 record.breachName.c_str(), -1, SQLITE_TRANSIENT);
188 sqlite3_bind_text(stmt, 3,
189 record.breachDate.c_str(), -1, SQLITE_TRANSIENT);
190 sqlite3_bind_int(stmt, 4,
191 static_cast<int>(record.severity));
192 sqlite3_bind_text(stmt, 5,
193 record.provider.c_str(), -1, SQLITE_TRANSIENT);
194 sqlite3_bind_text(stmt, 6,
195 record.description.c_str(), -1, SQLITE_TRANSIENT);
196
197 sqlite3_step(stmt);
198 sqlite3_finalize(stmt);
199}
Severity severity
std::string provider
std::string description

References LeakRecord::breachDate, LeakRecord::breachName, db, dbMutex, LeakRecord::description, LeakRecord::provider, and LeakRecord::severity.

Member Data Documentation

◆ db

void* LeakDatabase::db
private

Definition at line 44 of file LeakDatabase.hpp.

Referenced by ensureIdentity(), initSchema(), isNewLeak(), LeakDatabase(), open(), and storeLeak().

◆ dbMutex

std::mutex LeakDatabase::dbMutex
private

Definition at line 45 of file LeakDatabase.hpp.

Referenced by ensureIdentity(), initSchema(), isNewLeak(), open(), and storeLeak().

◆ dbPath

std::string LeakDatabase::dbPath
private

Definition at line 43 of file LeakDatabase.hpp.

Referenced by LeakDatabase(), and open().


The documentation for this class was generated from the following files: