diff options
| author | salmonik <salmonik@tuta.io> | 2026-10-28 22:28:05 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-10-28 22:28:05 +0100 |
| commit | ea5e7d336719571e51ef14478ab603e71126e3d8 (patch) | |
| tree | 4fb59337501f798e6591701e52b9e785307efd63 /src/client/database.rs | |
| parent | Fix TUI: render_lines 6 for correct MoveUp, drain buffered events before clea... (diff) | |
| download | zagan-bet-ea5e7d336719571e51ef14478ab603e71126e3d8.tar.gz zagan-bet-ea5e7d336719571e51ef14478ab603e71126e3d8.zip | |
v0.2.2: split client/server logic into the library
Diffstat (limited to 'src/client/database.rs')
| -rw-r--r-- | src/client/database.rs | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/client/database.rs b/src/client/database.rs new file mode 100644 index 0000000..e1fa42d --- /dev/null +++ b/src/client/database.rs | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | use rusqlite::Connection; | ||
| 2 | |||
| 3 | pub fn init_db(conn: &Connection) { | ||
| 4 | conn.execute( | ||
| 5 | "CREATE TABLE IF NOT EXISTS hosts ( | ||
| 6 | host TEXT PRIMARY KEY, | ||
| 7 | last_used DATETIME DEFAULT CURRENT_TIMESTAMP, | ||
| 8 | fingerprint TEXT | ||
| 9 | )", | ||
| 10 | [], | ||
| 11 | ) | ||
| 12 | .unwrap(); | ||
| 13 | // Migration for databases created before TOFU trust. | ||
| 14 | let _ = conn.execute("ALTER TABLE hosts ADD COLUMN fingerprint TEXT", []); | ||
| 15 | conn.execute( | ||
| 16 | "CREATE TABLE IF NOT EXISTS credentials ( | ||
| 17 | host TEXT PRIMARY KEY, | ||
| 18 | username TEXT NOT NULL, | ||
| 19 | password TEXT NOT NULL | ||
| 20 | )", | ||
| 21 | [], | ||
| 22 | ) | ||
| 23 | .unwrap(); | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn get_last_host(conn: &Connection) -> Option<String> { | ||
| 27 | let mut stmt = conn | ||
| 28 | .prepare("SELECT host FROM hosts ORDER BY last_used DESC LIMIT 1") | ||
| 29 | .unwrap(); | ||
| 30 | let mut rows = stmt.query([]).unwrap(); | ||
| 31 | if let Ok(Some(row)) = rows.next() { | ||
| 32 | row.get(0).ok() | ||
| 33 | } else { | ||
| 34 | None | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | pub fn save_host(conn: &Connection, host: &str) { | ||
| 39 | conn.execute( | ||
| 40 | "INSERT INTO hosts (host) VALUES (?1) | ||
| 41 | ON CONFLICT(host) DO UPDATE SET last_used = CURRENT_TIMESTAMP", | ||
| 42 | [host], | ||
| 43 | ) | ||
| 44 | .unwrap(); | ||
| 45 | } | ||
| 46 | |||
| 47 | pub fn save_credentials( | ||
| 48 | conn: &Connection, | ||
| 49 | host: &str, | ||
| 50 | username: &str, | ||
| 51 | password: &str, | ||
| 52 | ) -> Result<(), Box<dyn std::error::Error>> { | ||
| 53 | conn.execute( | ||
| 54 | "INSERT OR REPLACE INTO credentials (host, username, password) VALUES (?1, ?2, ?3)", | ||
| 55 | (host, username, password), | ||
| 56 | )?; | ||
| 57 | Ok(()) | ||
| 58 | } | ||
| 59 | |||
| 60 | pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String)> { | ||
| 61 | let mut stmt = conn | ||
| 62 | .prepare("SELECT username, password FROM credentials WHERE host = ?1") | ||
| 63 | .unwrap(); | ||
| 64 | let mut rows = stmt.query([host]).unwrap(); | ||
| 65 | if let Ok(Some(row)) = rows.next() { | ||
| 66 | let username: String = row.get(0).unwrap(); | ||
| 67 | let password: String = row.get(1).unwrap(); | ||
| 68 | Some((username, password)) | ||
| 69 | } else { | ||
| 70 | None | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | /// SHA-256 SPKI fingerprint of a host's certificate, if trusted. | ||
| 75 | pub fn get_fingerprint(conn: &Connection, host: &str) -> Option<String> { | ||
| 76 | let mut stmt = conn | ||
| 77 | .prepare("SELECT fingerprint FROM hosts WHERE host = ?1") | ||
| 78 | .ok()?; | ||
| 79 | let mut rows = stmt.query([host]).ok()?; | ||
| 80 | match rows.next() { | ||
| 81 | Ok(Some(row)) => row.get(0).ok(), | ||
| 82 | _ => None, | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | pub fn save_fingerprint( | ||
| 87 | conn: &Connection, | ||
| 88 | host: &str, | ||
| 89 | fingerprint: &str, | ||
| 90 | ) -> Result<(), rusqlite::Error> { | ||
| 91 | // Hosts may not exist yet (fingerprint can be saved before first | ||
| 92 | // successful connect completes save_host). | ||
| 93 | conn.execute("INSERT OR IGNORE INTO hosts (host) VALUES (?1)", [host])?; | ||
| 94 | conn.execute( | ||
| 95 | "UPDATE hosts SET fingerprint = ?2 WHERE host = ?1", | ||
| 96 | (host, fingerprint), | ||
| 97 | )?; | ||
| 98 | Ok(()) | ||
| 99 | } | ||
| 100 | |||
| 101 | /// Remove all trust and credentials for a host ("forget this host"). | ||
| 102 | pub fn forget_host(conn: &Connection, host: &str) -> Result<(), rusqlite::Error> { | ||
| 103 | conn.execute("DELETE FROM hosts WHERE host = ?1", [host])?; | ||
| 104 | conn.execute("DELETE FROM credentials WHERE host = ?1", [host])?; | ||
| 105 | Ok(()) | ||
| 106 | } | ||
| 107 | |||
| 108 | /// List all remembered hosts with their fingerprints. | ||
| 109 | pub fn get_all_hosts(conn: &Connection) -> Vec<(String, Option<String>)> { | ||
| 110 | let mut stmt = conn | ||
| 111 | .prepare("SELECT host, fingerprint FROM hosts ORDER BY last_used DESC") | ||
| 112 | .unwrap(); | ||
| 113 | let rows = stmt.query_map([], |row| { | ||
| 114 | Ok((row.get(0)?, row.get(1)?)) | ||
| 115 | }) | ||
| 116 | .unwrap(); | ||
| 117 | rows.filter_map(|r| r.ok()).collect() | ||
| 118 | } | ||
