use std::path::Path;
use std::process::{Command, ExitStatus};
use anyhow::{Result, bail};
use openmoat_core::{EvalContext, Policy};
use crate::cli::IsolatedArgs;
use crate::exit::Code;
use crate::sandbox::{Grants, Report, seatbelt_profile};
const SANDBOX_EXEC: &str = "/usr/bin/sandbox-exec";
pub struct Confined {
command: Command,
report: Report,
}
pub fn confine(
policy: &Policy,
ctx: &EvalContext,
grants: Grants,
program: &Path,
) -> Result<Confined> {
let generated = seatbelt_profile(policy, ctx, grants)?;
let mut command = Command::new(SANDBOX_EXEC);
command.arg("-p").arg(&generated.profile).arg(program);
Ok(Confined {
command,
report: generated.report,
})
}
pub fn isolate(_: &Policy, _: &EvalContext, _: Grants, _: &Path) -> Result<Confined> {
bail!(
"`moat run --isolate` runs on Linux only for now; on macOS the Isolated tier needs a \
virtual machine (#175). `moat run` without --isolate is the Lightweight tier"
)
}
pub fn inside(_: &IsolatedArgs) -> Result<Code> {
bail!("only `moat run --isolate` on Linux starts this")
}
impl Confined {
pub fn command(&mut self) -> &mut Command {
&mut self.command
}
pub fn report(&self) -> &Report {
&self.report
}
pub fn status(mut self) -> Result<ExitStatus> {
Ok(self.command.status()?)
}
}