From 8f5cea673b18e458d73c452aded5a061daa43572 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 16 Dec 2020 13:51:47 +0100 Subject: [PATCH] Put the users in the db as well --- src/db.rs | 6 +++++- src/db/user.rs | 14 ++++++++++++++ src/main.rs | 16 ++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index dfcee80..cb65212 100644 --- a/src/db.rs +++ b/src/db.rs @@ -28,6 +28,7 @@ pub struct Databases { pub access: Arc, pub machine: Arc, pub passdb: Arc, + pub userdb: Arc, } const LMDB_MAX_DB: u32 = 16; @@ -55,10 +56,13 @@ impl Databases { let passdb = pass::PassDB::init(log.new(o!("system" => "passwords")), env.clone()).unwrap(); + let userdb = user::init(log.new(o!("system" => "users")), &config, env.clone())?; + Ok(Self { access: Arc::new(ac), passdb: Arc::new(passdb), - machine: Arc::new(mdb) + machine: Arc::new(mdb), + userdb: Arc::new(userdb), }) } } diff --git a/src/db/user.rs b/src/db/user.rs index 23914f4..e26bdaa 100644 --- a/src/db/user.rs +++ b/src/db/user.rs @@ -4,14 +4,19 @@ use serde::{Serialize, Deserialize}; use std::fmt; use std::fs; +use std::sync::Arc; use std::iter::FromIterator; use std::path::Path; use crate::db::access::RoleIdentifier; use std::collections::HashMap; +use slog::Logger; + use crate::error::Result; +use crate::config::Config; mod internal; +pub use internal::Internal; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] /// An user @@ -117,6 +122,15 @@ pub fn load_file>(path: P) -> Result> { ))) } +pub fn init(log: Logger, config: &Config, env: Arc) -> Result { + let mut flags = lmdb::DatabaseFlags::empty(); + flags.set(lmdb::DatabaseFlags::INTEGER_KEY, true); + let db = env.create_db(Some("users"), flags)?; + debug!(&log, "Opened user db successfully."); + + Ok(Internal::new(log, env, db)) +} + #[cfg(test_DISABLED)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index d9cf6dc..a15d368 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,23 +122,27 @@ fn main() { // Returning a `Result` from `main` allows us to use the `?` shorthand. // In the case of an Err it will be printed using `fmt::Debug` fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { + // If no `config` option is given use a preset default. + let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); + let config = config::read(&PathBuf::from_str(configpath).unwrap())?; + debug!(log, "Loaded Config: {:?}", config); if matches.is_present("dump") { error!(log, "Dumping is currently not implemented"); Ok(()) } else if matches.is_present("load") { + let db = db::Databases::new(&log, &config)?; + let mut dir = PathBuf::from(matches.value_of_os("load").unwrap()); dir.push("users.toml"); - let map = db::user::load_file(&dir); + let map = db::user::load_file(&dir)?; + for (uid,user) in map.iter() { + db.userdb.put_user(uid, user)?; + } debug!(log, "Loaded users: {:?}", map); dir.pop(); Ok(()) } else { - // If no `config` option is given use a preset default. - let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); - let config = config::read(&PathBuf::from_str(configpath).unwrap())?; - debug!(log, "Loaded Config: {:?}", config); - let ex = Executor::new(); let mqtt = AsyncClient::new(config.mqtt_url.clone())?;