69 lines
1.5 KiB
Rust
69 lines
1.5 KiB
Rust
mod agents;
|
|
mod blacklist;
|
|
mod errors;
|
|
mod preflight;
|
|
mod sandbox;
|
|
|
|
pub use errors::SandboxError;
|
|
|
|
use std::env;
|
|
use std::ffi::OsString;
|
|
use std::fs;
|
|
use std::os::unix::process::CommandExt;
|
|
use std::path::PathBuf;
|
|
|
|
pub enum SandboxMode {
|
|
Blacklist,
|
|
Whitelist,
|
|
}
|
|
|
|
pub struct SandboxConfig {
|
|
pub mode: SandboxMode,
|
|
pub hardened: bool,
|
|
pub no_net: bool,
|
|
pub extra_rw: Vec<PathBuf>,
|
|
pub extra_ro: Vec<PathBuf>,
|
|
pub command: PathBuf,
|
|
pub command_args: Vec<OsString>,
|
|
pub chdir: PathBuf,
|
|
pub dry_run: bool,
|
|
}
|
|
|
|
pub fn require_home() -> Result<String, SandboxError> {
|
|
env::var("HOME")
|
|
.ok()
|
|
.filter(|h| !h.is_empty())
|
|
.ok_or(SandboxError::HomeNotSet)
|
|
}
|
|
|
|
pub fn require_run_user() -> Result<String, SandboxError> {
|
|
env::var("XDG_RUNTIME_DIR")
|
|
.ok()
|
|
.or_else(resolve_run_user_from_proc)
|
|
.ok_or(SandboxError::RunUserNotFound)
|
|
}
|
|
|
|
fn resolve_run_user_from_proc() -> Option<String> {
|
|
let status = fs::read_to_string("/proc/self/status").ok()?;
|
|
for line in status.lines() {
|
|
if let Some(rest) = line.strip_prefix("Uid:") {
|
|
let uid = rest.split_whitespace().next()?;
|
|
return Some(format!("/run/user/{uid}"));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn run(config: SandboxConfig) -> Result<(), SandboxError> {
|
|
preflight::check(&config)?;
|
|
|
|
let mut cmd = sandbox::build_command(&config)?;
|
|
|
|
if config.dry_run {
|
|
println!("{:?}", cmd);
|
|
return Ok(());
|
|
}
|
|
|
|
Err(SandboxError::Io(cmd.exec()))
|
|
}
|