use crate::error::{Error, Result};
fn is_valid_env_key(key: &str) -> bool {
if key.is_empty() {
return false;
}
let mut chars = key.chars();
match chars.next() {
Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
_ => return false,
}
chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}
fn shell_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
use crate::jail::{jail_attach, jail_getid};
use std::ffi::CString;
use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus, Stdio};
#[derive(Debug, Clone)]
pub struct ExecOptions {
pub user: String,
pub workdir: Option<String>,
pub env: Vec<(String, String)>,
#[allow(dead_code)]
pub clear_env: bool,
}
impl Default for ExecOptions {
fn default() -> Self {
Self {
user: "root".to_string(),
workdir: None,
env: Vec::new(),
clear_env: false,
}
}
}
pub fn exec_in_jail(jail: &str, command: &[String], opts: &ExecOptions) -> Result<ExitStatus> {
let jid = jail_getid(jail)?;
let mut cmd = Command::new("/usr/sbin/jexec");
cmd.arg("-u").arg(&opts.user);
cmd.arg(jid.to_string());
if opts.workdir.is_some() || !opts.env.is_empty() {
cmd.arg("/bin/sh");
cmd.arg("-c");
let mut script = String::new();
for (key, value) in &opts.env {
if !is_valid_env_key(key) {
return Err(Error::JailExecFailed(format!(
"Invalid environment variable key: '{}'. Keys must start with a letter or underscore, followed by alphanumerics or underscores.",
key
)));
}
let escaped = shell_quote(value);
script.push_str(&format!("export {}={}; ", key, escaped));
}
if let Some(ref workdir) = opts.workdir {
let escaped_workdir = shell_quote(workdir);
script.push_str(&format!("cd {} || exit 1; ", escaped_workdir));
}
if command.is_empty() {
script.push_str("exec /bin/sh");
} else {
let quoted: Vec<String> = command.iter().map(|arg| shell_quote(arg)).collect();
script.push_str(&format!("exec {}", quoted.join(" ")));
}
cmd.arg(script);
} else {
if command.is_empty() {
cmd.arg("/bin/sh");
} else {
cmd.args(command);
}
}
cmd.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let status = cmd
.status()
.map_err(|e| Error::JailExecFailed(format!("Failed to execute jexec: {}", e)))?;
Ok(status)
}
pub fn console(jail: &str, user: &str) -> Result<ExitStatus> {
let opts = ExecOptions {
user: user.to_string(),
..Default::default()
};
exec_in_jail(jail, &["-".to_string()], &opts)
}
#[allow(dead_code)]
pub fn exec_in_jail_direct(
jail: &str,
command: &[String],
opts: &ExecOptions,
) -> Result<ExitStatus> {
let jid = jail_getid(jail)?;
if command.is_empty() {
return Err(Error::JailExecFailed("No command specified".to_string()));
}
match unsafe { libc::fork() } {
-1 => Err(Error::JailExecFailed("Fork failed".to_string())),
0 => {
if let Err(e) = jail_attach(jid) {
eprintln!("Failed to attach to jail: {}", e);
std::process::exit(1);
}
if opts.user != "root"
&& let Err(e) = set_user(&opts.user) {
eprintln!("Failed to set user: {}", e);
std::process::exit(1);
}
if let Some(ref workdir) = opts.workdir
&& let Err(e) = std::env::set_current_dir(workdir) {
eprintln!("Failed to change directory: {}", e);
std::process::exit(1);
}
let program = CString::new(command[0].as_str()).unwrap();
let args: Vec<CString> = command
.iter()
.map(|s| CString::new(s.as_str()).unwrap())
.collect();
let args_ptr: Vec<*const libc::c_char> = args
.iter()
.map(|s| s.as_ptr())
.chain(std::iter::once(std::ptr::null()))
.collect();
unsafe {
libc::execvp(program.as_ptr(), args_ptr.as_ptr());
}
eprintln!("Failed to exec: {}", std::io::Error::last_os_error());
std::process::exit(1);
}
pid => {
let mut status: libc::c_int = 0;
unsafe {
libc::waitpid(pid, &mut status, 0);
}
let _exit_code = if libc::WIFEXITED(status) {
libc::WEXITSTATUS(status)
} else {
1
};
Ok(std::process::ExitStatus::from_raw(status))
}
}
}
fn set_user(username: &str) -> Result<()> {
let username_c = CString::new(username)
.map_err(|_| Error::JailExecFailed("Invalid username".to_string()))?;
unsafe {
let pwd = libc::getpwnam(username_c.as_ptr());
if pwd.is_null() {
return Err(Error::JailExecFailed(format!(
"User '{}' not found",
username
)));
}
let uid = (*pwd).pw_uid;
let gid = (*pwd).pw_gid;
if libc::initgroups(username_c.as_ptr(), gid as libc::gid_t) != 0 {
return Err(Error::JailExecFailed("Failed to set groups".to_string()));
}
if libc::setgid(gid) != 0 {
return Err(Error::JailExecFailed("Failed to set GID".to_string()));
}
if libc::setuid(uid) != 0 {
return Err(Error::JailExecFailed("Failed to set UID".to_string()));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exec_options_default() {
let opts = ExecOptions::default();
assert_eq!(opts.user, "root");
assert!(opts.workdir.is_none());
assert!(opts.env.is_empty());
}
#[test]
fn test_is_valid_env_key() {
assert!(is_valid_env_key("PATH"));
assert!(is_valid_env_key("_PRIVATE"));
assert!(is_valid_env_key("HOME"));
assert!(is_valid_env_key("MY_VAR_123"));
assert!(is_valid_env_key("a"));
assert!(is_valid_env_key("_"));
assert!(is_valid_env_key("_1"));
assert!(!is_valid_env_key(""));
assert!(!is_valid_env_key("123ABC"));
assert!(!is_valid_env_key("MY-VAR"));
assert!(!is_valid_env_key("MY VAR"));
assert!(!is_valid_env_key("MY.VAR"));
assert!(!is_valid_env_key("$(whoami)"));
assert!(!is_valid_env_key("VAR;rm -rf /"));
}
#[test]
fn test_shell_quote() {
assert_eq!(shell_quote("hello"), "'hello'");
assert_eq!(shell_quote("hello world"), "'hello world'");
assert_eq!(shell_quote("it's"), "'it'\\''s'");
assert_eq!(shell_quote(""), "''");
assert_eq!(shell_quote("a'b'c"), "'a'\\''b'\\''c'");
}
#[test]
fn test_shell_quote_special_chars() {
assert_eq!(shell_quote("$(whoami)"), "'$(whoami)'");
assert_eq!(shell_quote("`id`"), "'`id`'");
assert_eq!(shell_quote("$HOME"), "'$HOME'");
assert_eq!(shell_quote("foo;bar"), "'foo;bar'");
assert_eq!(shell_quote("foo|bar"), "'foo|bar'");
assert_eq!(shell_quote("foo&bar"), "'foo&bar'");
assert_eq!(shell_quote("foo\nbar"), "'foo\nbar'");
}
}