2023-11-27 20:44:37 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
pub type Binary = Vec<String>;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct OpenArgs {
|
|
|
|
pub auth: BTreeMap<String, Binary>,
|
|
|
|
pub hardware: BTreeMap<String, Binary>,
|
|
|
|
pub method: String,
|
|
|
|
pub identifier: String,
|
|
|
|
pub resource_shortcut: i8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct AuthResult {
|
|
|
|
pub username: String,
|
|
|
|
pub resource: String,
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:51:44 +02:00
|
|
|
fn run_bin(bin: Binary) -> Result<String, String> {
|
2023-11-28 16:29:40 +02:00
|
|
|
use std::{
|
|
|
|
io::Read,
|
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
|
|
|
|
2023-11-27 20:51:44 +02:00
|
|
|
let mut ret = Err("".to_string());
|
2023-11-27 20:44:37 +02:00
|
|
|
let mut iter = bin.iter();
|
2023-11-28 16:29:40 +02:00
|
|
|
let mut cmd = Command::new(iter.next().unwrap());
|
2023-11-27 20:44:37 +02:00
|
|
|
cmd.args(iter);
|
|
|
|
|
2023-11-28 16:29:40 +02:00
|
|
|
let mut child = cmd.stdout(Stdio::piped()).spawn().unwrap();
|
|
|
|
let mut success = child.wait().unwrap().success();
|
|
|
|
|
|
|
|
let mut s = String::from("");
|
|
|
|
child.stdout.unwrap().read_to_string(&mut s).unwrap();
|
|
|
|
s = String::from(s.trim());
|
|
|
|
|
|
|
|
ret = if success { Ok(s) } else { Err(s) };
|
2023-11-27 20:44:37 +02:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:51:44 +02:00
|
|
|
fn run_auth(args: &OpenArgs, bin: Binary) -> Result<AuthResult, String> {
|
|
|
|
let mut ret = Err("".to_string());
|
2023-11-27 20:44:37 +02:00
|
|
|
|
|
|
|
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<String> = out.split(',').map(|s| String::from(s)).collect();
|
|
|
|
ret = Ok(AuthResult {
|
|
|
|
username: s[0].clone(),
|
|
|
|
resource: s[1].clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:51:44 +02:00
|
|
|
pub fn open(args: OpenArgs) -> Result<(), ()> {
|
|
|
|
let mut msg = "Not authorized!";
|
|
|
|
let mut ret = Err(());
|
2023-11-27 20:44:37 +02:00
|
|
|
|
|
|
|
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()) {
|
2023-11-27 20:51:44 +02:00
|
|
|
msg = "Open sesame!";
|
|
|
|
ret = Ok(());
|
|
|
|
} else {
|
|
|
|
msg = "Hardware failure!";
|
2023-11-27 20:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 20:51:44 +02:00
|
|
|
println!("{}", msg);
|
2023-11-27 20:44:37 +02:00
|
|
|
ret
|
|
|
|
}
|