From d4da25b0d7dbabeb0a682da386994f51cd5c997b Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 16 Dec 2020 14:25:03 +0100 Subject: [PATCH] Load passdb \o/ --- examples/pass.toml | 1 + src/db/pass.rs | 23 +++++++++++++++++++++++ src/main.rs | 4 ++++ 3 files changed, 28 insertions(+) create mode 100644 examples/pass.toml diff --git a/examples/pass.toml b/examples/pass.toml new file mode 100644 index 0000000..6d4855d --- /dev/null +++ b/examples/pass.toml @@ -0,0 +1 @@ +Testuser = "secret" diff --git a/src/db/pass.rs b/src/db/pass.rs index 7a7da05..4ee9321 100644 --- a/src/db/pass.rs +++ b/src/db/pass.rs @@ -1,4 +1,7 @@ use std::sync::Arc; +use std::path::Path; +use std::fs; +use std::collections::HashMap; use argon2; use lmdb::{Environment, Transaction, RwTransaction, Cursor}; @@ -53,4 +56,24 @@ impl PassDB { txn.put(self.db, &authcid.as_bytes(), &hash.as_bytes(), lmdb::WriteFlags::empty()) .map_err(Into::into) } + + pub fn insert_multiple(&self, vec: Vec<(String, String)>) -> Result<()> { + let mut txn = self.env.begin_rw_txn()?; + for (authcid, password) in vec.iter() { + self.store_with_txn(&mut txn, authcid.as_ref(), password.as_bytes())?; + } + txn.commit()?; + + let v: Vec<&String> = vec.iter().map(|(a,_)| a).collect(); + debug!(self.log, "Loaded passwords for: {:?}", v); + + Ok(()) + } + + pub fn load_file>(&self, path: P) -> Result<()> { + let f = fs::read(path)?; + let mut map: HashMap = toml::from_slice(&f)?; + + self.insert_multiple(map.drain().collect()) + } } diff --git a/src/main.rs b/src/main.rs index 58df028..3e9e58b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,6 +146,10 @@ fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { db.access.internal.load_roles(&dir)?; dir.pop(); + dir.push("pass.toml"); + db.passdb.load_file(&dir); + dir.pop(); + Ok(()) } else { let ex = Executor::new();