use std::path::{Path, PathBuf};
use anyhow::Result;
use openmoat_hosts::Host;
use super::run_words;
use crate::protection::os_sandbox::RunCommand;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Shim {
pub host: Host,
pub name: &'static str,
pub program: PathBuf,
pub text: String,
}
impl Shim {
pub fn new(host: Host, binary: &Path, program: &Path, run: &RunCommand) -> Self {
let mut words = vec![quote(&binary.display().to_string())];
words.extend(run_words(run).into_iter().map(str::to_owned));
for write in &run.writes {
words.push("--write".to_owned());
words.push(quote(&write.display().to_string()));
}
words.push("--".to_owned());
words.push(quote(&program.display().to_string()));
words.extend(run.args.iter().map(|a| quote(a)));
words.push("\"$@\"".to_owned());
let text = format!(
"#!/bin/sh\n\
# OpenMoat: `{name}` starts {agent} in `openmoat run`'s sandbox (ADR-106).\n\
# Written by `openmoat init`; `openmoat uninstall` removes it.\n\
exec {}\n",
words.join(" "),
name = run.program,
agent = host.display_name(),
);
Self {
host,
name: run.program,
program: program.to_path_buf(),
text,
}
}
}
pub(super) fn quote(word: &str) -> String {
format!("'{}'", word.replace('\'', "'\\''"))
}
pub(super) fn write_executable(path: &Path, bytes: &[u8]) -> Result<()> {
crate::home::write_private_program(path, bytes)
}