parse env

This commit is contained in:
2019-09-17 20:39:40 +02:00
parent 40bf047528
commit 34e428f507
4 changed files with 55 additions and 10 deletions

View File

@@ -8,9 +8,36 @@ use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::path::{PathBuf, Path};
use std::process::Command;
#[derive(Debug, Deserialize, Serialize)]
pub struct EnvConfig {
credential_id: String,
uuid: String,
salt: String,
mapper_name: String,
password_helper: String
}
impl Into<Config> for EnvConfig {
fn into(self) -> Config {
Config{
credential_id: self.credential_id,
device: format!("/dev/disk/by-uuid/{}", self.uuid).into(),
mapper_name: self.mapper_name,
password_helper: PasswordHelper::Script(self.password_helper),
input_salt: if PathBuf::from(&self.salt).exists() {
InputSalt::File { path: self.salt.into() }
} else {
InputSalt::AskPassword
}
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub credential_id: String,

View File

@@ -17,7 +17,7 @@ use luks::device::Error::CryptsetupError;
use std::collections::HashMap;
use std::env;
use std::io::{self, Write};
use std::io::{self, stdout, Write};
use std::path::PathBuf;
use std::process::exit;
@@ -84,26 +84,33 @@ fn open(conf: &Config, secret: &[u8; 32]) -> Fido2LuksResult<()> {
fn main() -> Fido2LuksResult<()> {
let args: Vec<_> = env::args().skip(1).collect(); //Ignore program name -> Vec
let env = env::vars().collect::<HashMap<_, _>>();
let conf = Config::load_default_location()?;
let secret = || -> Fido2LuksResult<[u8; 32]> {
let salt = conf.input_salt.obtain(&conf.password_helper)?;
Ok(assemble_secret(
&perform_challenge(&conf.credential_id, &salt)?,
&salt,
))
};
if args.is_empty() {
let conf = Config::load_default_location()?;
let salt = conf.input_salt.obtain(&conf.password_helper)?;
dbg!(hex::encode(&salt));
let secret = {
let salt = conf.input_salt.obtain(&conf.password_helper)?;
assemble_secret(&perform_challenge(&conf.credential_id, &salt)?, &salt)
};
if env.contains_key("CRYPTTAB_NAME") {
//Indicates that this script is being run as keyscript
open(&conf, &secret)
let mut out = stdout();
out.write(&secret()?)?;
Ok(out.flush()?)
} else {
io::stdout().write(&secret)?;
io::stdout().write(&secret()?)?;
Ok(io::stdout().flush()?)
}
} else {
match args.first().map(|s| s.as_ref()).unwrap() {
"addkey" => add_key_to_luks(&Config::load_default_location()?).map(|_| ()),
"setup" => setup(),
"open" if args.get(1).map(|a| &*a == "-e").unwrap_or(false) => open(&envy::prefixed("FIDO2LUKS_").from_env::<EnvConfig>().expect("Missing env config values").into(), &secret()?),
"open" => open(&conf, &secret()?),
"connected" => match authenticator_connected()? {
false => {
println!("no");