From 30b7e5bfe35407d112846ef812e9ed74983599fa Mon Sep 17 00:00:00 2001 From: George Kaklamanos Date: Mon, 27 Nov 2023 20:44:37 +0200 Subject: [PATCH] Create new `open` module for calling `auth` and `gpio` appropriately --- Cargo.toml | 1 + labadoor-open/Cargo.toml | 6 ++++ labadoor-open/src/lib.rs | 66 +++++++++++++++++++++++++++++++++++++++ labadoor/config.toml | 7 +++++ labadoor/src/cli.rs | 15 +++++++++ labadoor/src/main.rs | 6 ++++ labadoor/src/to_config.rs | 13 ++++++++ 7 files changed, 114 insertions(+) create mode 100644 labadoor-open/Cargo.toml create mode 100644 labadoor-open/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index c7c42b7..fefc551 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ members = [ "labadoor-csv", "labadoor", "labadoor-auth", + "labadoor-open", "labadoor-acl", ] diff --git a/labadoor-open/Cargo.toml b/labadoor-open/Cargo.toml new file mode 100644 index 0000000..16e2704 --- /dev/null +++ b/labadoor-open/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "labadoor-open" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/labadoor-open/src/lib.rs b/labadoor-open/src/lib.rs new file mode 100644 index 0000000..5cce21e --- /dev/null +++ b/labadoor-open/src/lib.rs @@ -0,0 +1,66 @@ +use std::collections::BTreeMap; + +pub type Binary = Vec; + +#[derive(Clone, Debug)] +pub struct OpenArgs { + pub auth: BTreeMap, + pub hardware: BTreeMap, + pub method: String, + pub identifier: String, + pub resource_shortcut: i8, +} + +#[derive(Clone, Debug)] +struct AuthResult { + pub username: String, + pub resource: String, +} + +fn run_bin(bin: Binary) -> Result { + let mut ret = Err(()); + let mut iter = bin.iter(); + let mut cmd = std::process::Command::new(iter.next().unwrap()); + cmd.args(iter); + + if let Ok(_) = cmd.status() { + let stdout = cmd.output().unwrap().stdout; + let str = std::str::from_utf8(stdout.as_slice()).unwrap().trim(); + ret = Ok(String::from(str)); + } + ret +} + +fn run_auth(args: &OpenArgs, bin: Binary) -> Result { + let mut ret = Err(()); + + let mut auth_bin = bin.clone(); + auth_bin.push(args.method.clone()); + auth_bin.push(args.identifier.clone()); + auth_bin.push(args.resource_shortcut.to_string()); + let output = run_bin(auth_bin); + + if let Ok(out) = output { + let s: Vec = out.split(',').map(|s| String::from(s)).collect(); + ret = Ok(AuthResult { + username: s[0].clone(), + resource: s[1].clone(), + }) + } + ret +} + +pub fn open(args: OpenArgs) -> Result<(), String> { + let mut ret = Err("Not authorized!".to_string()); + + for (method, binary) in args.auth.iter() { + let output = run_auth(&args, binary.to_vec()); + if let Ok(user) = output { + let resource_bin = args.hardware.get(&user.resource).unwrap(); + if let Ok(_) = run_bin(resource_bin.to_vec()) { + ret = Err("Hardware failure!".to_string()); + } + } + } + ret +} diff --git a/labadoor/config.toml b/labadoor/config.toml index dfc05aa..64c95c8 100644 --- a/labadoor/config.toml +++ b/labadoor/config.toml @@ -14,3 +14,10 @@ active_time = 2000 [auth] backends = ["csv"] + +[open.auth] +labadoor = [ "/usr/bin/labadoor", "auth" ] + +[open.hardware] +tolabaki = [ "/usr/bin/labadoor", "gpio" ] + diff --git a/labadoor/src/cli.rs b/labadoor/src/cli.rs index cb97039..51f55c6 100644 --- a/labadoor/src/cli.rs +++ b/labadoor/src/cli.rs @@ -17,6 +17,8 @@ pub enum Command { GPIO(GPIO), #[cfg(feature = "auth")] Auth(labadoor_auth::cli::Cli), + #[cfg(feature = "open")] + Open(Open), } #[cfg(feature = "telegram")] @@ -52,6 +54,19 @@ pub struct GPIO { pub active_time: Option, } +use std::collections::BTreeMap; +#[cfg(feature = "open")] +#[derive(Serialize, Deserialize, Parser, Clone, Debug)] +pub struct Open { + #[clap(skip)] + pub auth: BTreeMap>, + #[clap(skip)] + pub hardware: BTreeMap>, + pub method: String, + pub identifier: String, + pub resource_shortcut: i8, +} + pub fn parse() -> Cli { Cli::parse() } diff --git a/labadoor/src/main.rs b/labadoor/src/main.rs index ace6155..89d7e80 100644 --- a/labadoor/src/main.rs +++ b/labadoor/src/main.rs @@ -38,6 +38,12 @@ fn main() -> Result<(), ()> { let gpio = config.get::("gpio").unwrap().to_config(); labadoor_gpio::gpio(gpio); } + #[cfg(feature = "open")] + cli::Command::Open(cliargs) => { + let config = add_cliargs!(config, "open", cliargs); + let open = config.get::("open").unwrap().to_config(); + labadoor_open::open(open); + } #[cfg(feature = "auth")] cli::Command::Auth(cli) => ret = labadoor_auth::auth(&cli, config), } diff --git a/labadoor/src/to_config.rs b/labadoor/src/to_config.rs index 2f9f14a..d01e6b0 100644 --- a/labadoor/src/to_config.rs +++ b/labadoor/src/to_config.rs @@ -35,3 +35,16 @@ impl ToConfig for GPIO { } } } + +#[cfg(feature = "open")] +impl ToConfig for Open { + fn to_config(&self) -> labadoor_open::OpenArgs { + labadoor_open::OpenArgs { + auth: self.auth.clone(), + hardware: self.hardware.clone(), + method: self.method.clone(), + identifier: self.identifier.clone(), + resource_shortcut: self.resource_shortcut, + } + } +}