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();