use std::env;
use limes_proto::{AuthSuccess, SessionSpec};
use crate::error::{LimesError, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrontendSpec {
pub program: String,
pub args: Vec<String>,
}
impl FrontendSpec {
#[must_use]
pub fn external(program: impl Into<String>, args: Vec<String>) -> Self {
Self {
program: program.into(),
args,
}
}
#[must_use]
pub fn command_line(&self) -> Vec<String> {
let mut command = Vec::with_capacity(self.args.len() + 1);
command.push(self.program.clone());
command.extend(self.args.iter().cloned());
command
}
}
#[derive(Debug, Clone)]
pub struct Config {
pub login_frontend: Option<FrontendSpec>,
pub lock_frontend: Option<FrontendSpec>,
pub session_command: Vec<String>,
pub max_auth_attempts: u8,
}
impl Config {
pub fn from_env() -> Result<Self> {
let login_frontend = match env::var("LIMES_LOGIN_FRONTEND") {
Ok(value) if value.trim().is_empty() => None,
Ok(value) => Some(parse_external_command(&value)?),
Err(_) => None,
};
let lock_frontend = match env::var("LIMES_LOCK_FRONTEND") {
Ok(value) if value.trim().is_empty() => None,
Ok(value) => Some(parse_external_command(&value)?),
Err(_) => None,
};
let session_command = match env::var("LIMES_SESSION_COMMAND") {
Ok(value) if !value.trim().is_empty() => split_words(&value),
_ => Vec::new(),
};
let max_auth_attempts = env::var("LIMES_MAX_AUTH_ATTEMPTS")
.ok()
.and_then(|value| value.parse::<u8>().ok())
.unwrap_or(3)
.max(1);
Ok(Self {
login_frontend,
lock_frontend,
session_command,
max_auth_attempts,
})
}
#[must_use]
pub fn session_spec_for(&self, success: &AuthSuccess) -> SessionSpec {
let command = if self.session_command.is_empty() {
vec![
success
.shell
.clone()
.filter(|shell| !shell.trim().is_empty())
.unwrap_or_else(|| "/bin/sh".to_owned()),
]
} else {
self.session_command.clone()
};
let mut spec =
SessionSpec::new(success.username.clone(), success.uid, success.gid, command);
spec.env.push(("USER".to_owned(), success.username.clone()));
spec.env
.push(("LOGNAME".to_owned(), success.username.clone()));
spec.env.push(("UID".to_owned(), success.uid.to_string()));
spec.env.push(("GID".to_owned(), success.gid.to_string()));
if let Some(home) = &success.home {
spec.env.push(("HOME".to_owned(), home.clone()));
spec.env.push(("PWD".to_owned(), home.clone()));
spec.working_dir = Some(home.clone());
}
if let Some(shell) = &success.shell {
spec.env.push(("SHELL".to_owned(), shell.clone()));
}
spec.env.push((
"PATH".to_owned(),
env::var("LIMES_SESSION_PATH").unwrap_or_else(|_| {
"/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin".to_owned()
}),
));
spec
}
}
fn parse_external_command(value: &str) -> Result<FrontendSpec> {
let words = split_words(value);
let (program, args) = words
.split_first()
.ok_or_else(|| LimesError::Config("empty frontend command".to_owned()))?;
Ok(FrontendSpec::external(program.clone(), args.to_vec()))
}
fn split_words(value: &str) -> Vec<String> {
value
.split_whitespace()
.map(std::borrow::ToOwned::to_owned)
.collect()
}