use crate::error::{Error, Result};
use serde::Deserialize;
use std::collections::HashMap;
use std::io::Read;
use std::path::Path;
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HookPhase {
PreCreate,
PostCreate,
PreStart,
PostStart,
PreStop,
PostStop,
}
impl HookPhase {
#[allow(dead_code)]
pub fn all() -> &'static [HookPhase] {
&[
HookPhase::PreCreate,
HookPhase::PostCreate,
HookPhase::PreStart,
HookPhase::PostStart,
HookPhase::PreStop,
HookPhase::PostStop,
]
}
#[allow(dead_code)]
pub fn requires_running_jail(&self) -> bool {
matches!(self, HookPhase::PostStart | HookPhase::PreStop)
}
}
impl std::fmt::Display for HookPhase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
HookPhase::PreCreate => "pre_create",
HookPhase::PostCreate => "post_create",
HookPhase::PreStart => "pre_start",
HookPhase::PostStart => "post_start",
HookPhase::PreStop => "pre_stop",
HookPhase::PostStop => "post_stop",
};
write!(f, "{}", s)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum HookTarget {
#[default]
Host,
Jail,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum OnFailure {
#[default]
Abort,
Continue,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Hook {
pub phase: HookPhase,
#[serde(default)]
pub target: HookTarget,
pub command: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default = "default_timeout")]
pub timeout: u64,
#[serde(default)]
pub on_failure: OnFailure,
pub description: Option<String>,
}
fn default_timeout() -> u64 {
30
}
impl Hook {
#[allow(dead_code)]
pub fn new(phase: HookPhase, command: String) -> Self {
Self {
phase,
target: HookTarget::Host,
command,
args: Vec::new(),
timeout: default_timeout(),
on_failure: OnFailure::Abort,
description: None,
}
}
#[allow(dead_code)]
pub fn with_target(mut self, target: HookTarget) -> Self {
self.target = target;
self
}
#[allow(dead_code)]
pub fn with_args(mut self, args: Vec<String>) -> Self {
self.args = args;
self
}
#[allow(dead_code)]
pub fn with_timeout(mut self, timeout: u64) -> Self {
self.timeout = timeout;
self
}
#[allow(dead_code)]
pub fn with_on_failure(mut self, on_failure: OnFailure) -> Self {
self.on_failure = on_failure;
self
}
#[allow(dead_code)]
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
}
#[derive(Debug, Clone, Default)]
pub struct HookContext {
pub jail_name: String,
pub jail_path: String,
pub jail_ip: Option<String>,
pub jid: Option<i32>,
pub extra: HashMap<String, String>,
}
impl HookContext {
pub fn new(jail_name: &str, jail_path: &Path) -> Self {
Self {
jail_name: jail_name.to_string(),
jail_path: jail_path.display().to_string(),
jail_ip: None,
jid: None,
extra: HashMap::new(),
}
}
pub fn with_ip(mut self, ip: String) -> Self {
self.jail_ip = Some(ip);
self
}
pub fn with_jid(mut self, jid: i32) -> Self {
self.jid = Some(jid);
self
}
#[allow(dead_code)]
pub fn with_var(mut self, name: &str, value: &str) -> Self {
self.extra.insert(name.to_string(), value.to_string());
self
}
pub fn substitute(&self, input: &str) -> String {
let mut result = input.to_string();
result = result.replace("${jail_name}", &self.jail_name);
result = result.replace("${jail_path}", &self.jail_path);
if let Some(ip) = &self.jail_ip {
result = result.replace("${jail_ip}", ip);
} else {
result = result.replace("${jail_ip}", "");
}
if let Some(jid) = self.jid {
result = result.replace("${jid}", &jid.to_string());
} else {
result = result.replace("${jid}", "");
}
for (name, value) in &self.extra {
result = result.replace(&format!("${{{}}}", name), value);
}
result
}
}
#[derive(Debug)]
#[allow(dead_code)] pub struct HookResult {
pub success: bool,
pub exit_code: Option<i32>,
pub stdout: String,
pub stderr: String,
}
#[allow(dead_code)] impl HookResult {
pub fn summary(&self) -> String {
let status = if self.success { "success" } else { "failed" };
let code = self
.exit_code
.map(|c| format!(" (exit {})", c))
.unwrap_or_default();
format!("{}{}", status, code)
}
pub fn output(&self) -> String {
if self.stdout.is_empty() {
self.stderr.clone()
} else if self.stderr.is_empty() {
self.stdout.clone()
} else {
format!("{}\n{}", self.stdout, self.stderr)
}
}
}
pub struct HookRunner {
hooks: Vec<Hook>,
verbose: bool,
}
impl HookRunner {
pub fn new(hooks: Vec<Hook>) -> Self {
Self {
hooks,
verbose: false,
}
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn execute_phase(&self, phase: HookPhase, context: &HookContext) -> Result<()> {
let phase_hooks: Vec<&Hook> = self.hooks.iter().filter(|h| h.phase == phase).collect();
if phase_hooks.is_empty() {
return Ok(());
}
if self.verbose {
println!("Executing {} hooks for phase {}", phase_hooks.len(), phase);
}
for hook in phase_hooks {
let result = self.execute_hook(hook, context)?;
if !result.success {
let desc = hook.description.as_deref().unwrap_or(&hook.command);
let msg = format!(
"Hook '{}' failed at phase {}: {}",
desc, phase, result.stderr
);
match hook.on_failure {
OnFailure::Abort => {
return Err(Error::HookFailed {
phase: phase.to_string(),
command: hook.command.clone(),
message: result.stderr,
});
}
OnFailure::Continue => {
eprintln!("Warning: {}", msg);
}
}
}
}
Ok(())
}
fn execute_hook(&self, hook: &Hook, context: &HookContext) -> Result<HookResult> {
let command = context.substitute(&hook.command);
let args: Vec<String> = hook.args.iter().map(|a| context.substitute(a)).collect();
if self.verbose {
let desc = hook.description.as_deref().unwrap_or(&command);
println!(" Running: {} ({:?})", desc, hook.target);
}
match hook.target {
HookTarget::Host => self.execute_on_host(&command, &args, hook.timeout),
HookTarget::Jail => {
let jid = context.jid.ok_or_else(|| Error::HookFailed {
phase: hook.phase.to_string(),
command: command.clone(),
message: "Cannot execute jail hook: jail is not running".to_string(),
})?;
self.execute_in_jail(jid, &command, &args, hook.timeout)
}
}
}
fn execute_on_host(
&self,
command: &str,
args: &[String],
timeout_secs: u64,
) -> Result<HookResult> {
let timeout = Duration::from_secs(timeout_secs);
let mut child = Command::new(command)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| Error::HookFailed {
phase: String::new(),
command: command.to_string(),
message: e.to_string(),
})?;
let start = Instant::now();
loop {
match child.try_wait() {
Ok(Some(status)) => {
let mut stdout = String::new();
let mut stderr = String::new();
if let Some(mut stdout_handle) = child.stdout.take() {
let _ = stdout_handle.read_to_string(&mut stdout);
}
if let Some(mut stderr_handle) = child.stderr.take() {
let _ = stderr_handle.read_to_string(&mut stderr);
}
return Ok(HookResult {
success: status.success(),
exit_code: status.code(),
stdout,
stderr,
});
}
Ok(None) => {
if start.elapsed() > timeout {
let _ = child.kill();
let _ = child.wait();
return Err(Error::HookTimeout(timeout_secs));
}
thread::sleep(Duration::from_millis(100));
}
Err(e) => {
return Err(Error::HookFailed {
phase: String::new(),
command: command.to_string(),
message: format!("Failed to wait on process: {}", e),
});
}
}
}
}
fn execute_in_jail(
&self,
jid: i32,
command: &str,
args: &[String],
timeout_secs: u64,
) -> Result<HookResult> {
let timeout = Duration::from_secs(timeout_secs);
let mut cmd = Command::new("/usr/sbin/jexec");
cmd.arg("-u")
.arg("root")
.arg(jid.to_string())
.arg(command)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let mut child = cmd.spawn().map_err(|e| Error::HookFailed {
phase: String::new(),
command: command.to_string(),
message: format!("Failed to execute jexec: {}", e),
})?;
let start = Instant::now();
loop {
match child.try_wait() {
Ok(Some(status)) => {
let mut stdout = String::new();
let mut stderr = String::new();
if let Some(mut stdout_handle) = child.stdout.take() {
let _ = stdout_handle.read_to_string(&mut stdout);
}
if let Some(mut stderr_handle) = child.stderr.take() {
let _ = stderr_handle.read_to_string(&mut stderr);
}
return Ok(HookResult {
success: status.success(),
exit_code: status.code(),
stdout,
stderr,
});
}
Ok(None) => {
if start.elapsed() > timeout {
let _ = child.kill();
let _ = child.wait();
return Err(Error::HookTimeout(timeout_secs));
}
thread::sleep(Duration::from_millis(100));
}
Err(e) => {
return Err(Error::HookFailed {
phase: String::new(),
command: command.to_string(),
message: format!("Failed to wait on process: {}", e),
});
}
}
}
}
}
#[allow(dead_code)] pub fn filter_by_phase(hooks: &[Hook], phase: HookPhase) -> Vec<&Hook> {
hooks.iter().filter(|h| h.phase == phase).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hook_phase_display() {
assert_eq!(HookPhase::PreStart.to_string(), "pre_start");
assert_eq!(HookPhase::PostStop.to_string(), "post_stop");
}
#[test]
fn test_hook_context_substitution() {
let ctx = HookContext::new("myjail", Path::new("/jails/myjail"))
.with_ip("10.0.1.10".to_string())
.with_jid(42)
.with_var("custom", "value");
assert_eq!(ctx.substitute("jail: ${jail_name}"), "jail: myjail");
assert_eq!(ctx.substitute("path: ${jail_path}"), "path: /jails/myjail");
assert_eq!(ctx.substitute("ip: ${jail_ip}"), "ip: 10.0.1.10");
assert_eq!(ctx.substitute("jid: ${jid}"), "jid: 42");
assert_eq!(ctx.substitute("var: ${custom}"), "var: value");
}
#[test]
fn test_hook_builder() {
let hook = Hook::new(HookPhase::PreStart, "/bin/echo".to_string())
.with_target(HookTarget::Host)
.with_args(vec!["hello".to_string()])
.with_timeout(60)
.with_on_failure(OnFailure::Continue);
assert_eq!(hook.phase, HookPhase::PreStart);
assert_eq!(hook.target, HookTarget::Host);
assert_eq!(hook.timeout, 60);
assert_eq!(hook.on_failure, OnFailure::Continue);
}
#[test]
fn test_phase_requires_running_jail() {
assert!(!HookPhase::PreCreate.requires_running_jail());
assert!(!HookPhase::PostCreate.requires_running_jail());
assert!(!HookPhase::PreStart.requires_running_jail());
assert!(HookPhase::PostStart.requires_running_jail());
assert!(HookPhase::PreStop.requires_running_jail());
assert!(!HookPhase::PostStop.requires_running_jail());
}
#[test]
fn test_hook_deserialize() {
let toml = r#"
phase = "pre_start"
target = "host"
command = "/usr/local/bin/setup.sh"
args = ["${jail_name}", "${jail_ip}"]
timeout = 60
on_failure = "continue"
description = "Run setup script"
"#;
let hook: Hook = toml::from_str(toml).unwrap();
assert_eq!(hook.phase, HookPhase::PreStart);
assert_eq!(hook.target, HookTarget::Host);
assert_eq!(hook.command, "/usr/local/bin/setup.sh");
assert_eq!(hook.args.len(), 2);
assert_eq!(hook.timeout, 60);
assert_eq!(hook.on_failure, OnFailure::Continue);
}
}