/** * Config Vault - cfgvault * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2026 */ use crate::config::AppConfig; use keepass::db::NodeRef; use keepass::error::DatabaseOpenError; use keepass::{Database, DatabaseKey}; use std::fs::File; use std::sync::OnceLock; static DB: OnceLock = OnceLock::new(); pub struct RetrievalError {/* ... */} pub fn retrieve_entry(entry_name: &str) -> Result { let db = DB.get().expect("Database not initialized"); for node in &db.root { match node { NodeRef::Group(_g) => { //println!("Saw group '{0}'", g.name); } NodeRef::Entry(entry) => { if entry.get_title().unwrap_or("No such entry name)") == entry_name { return Ok(String::from(entry.get_password().unwrap())); } } } } Err(RetrievalError {}) } pub fn unlock(config: &AppConfig) { let db = open_database(&config.keystore, &config.secretkey).expect("Failed to unlock database"); // set exactly once at startup DB.set(db).expect("Database already initialized"); } fn open_database( kdbx_filename: &String, key_filename: &String, ) -> Result { let key = DatabaseKey::new().with_keyfile(&mut File::open(key_filename)?)?; let db = Database::open(&mut File::open(kdbx_filename)?, key)?; Ok(db) }