use std::io::Write as _;
use std::path::Path;
use anyhow::Result;
use openmoat_hosts::Host;
use crate::canary::{self, Offer};
use crate::cli::{Format, InitArgs};
use crate::home::{Home, user_home};
use crate::protection::os_sandbox::RunHere;
use crate::render::Deferred;
use crate::report::Paths;
use crate::start::{self, enable, rc};
pub(super) fn asked(args: &InitArgs, unattended: bool) -> bool {
!unattended
&& !args.yes
&& !args.hooks_only
&& args.format != Format::Json
&& crate::terminal::person_for("openmoat init")
}
pub(super) fn ask(home: &Home, binary: &Path, hosts: &[Host], out: &mut Deferred) -> Result<bool> {
let user = user_home()?;
let paths = Paths::detect();
let mut changed = decoys(home, &user, &paths, out)?;
changed |= sandboxed_start(home, binary, hosts, (&user, &paths), out)?;
Ok(changed)
}
fn decoys(home: &Home, user: &Path, paths: &Paths, out: &mut Deferred) -> Result<bool> {
let mut record = canary::Record::load(home)?;
let offers = canary::offers(&record, user);
if offers.is_empty() {
return Ok(false);
}
let files: Vec<String> = offers.iter().map(|o| paths.path(&o.file)).collect();
let question = format!(
" Decoy keys plant {} fake key file(s) ({}) so that any attempt to read or send \
them is caught? No real file is touched [y/N/d] ",
offers.len(),
files.join(", ")
);
if !yes(&question, out, |out| decoys_diff(&offers, paths, out))? {
return Ok(false);
}
let planted = canary::plant(home, &mut record, &offers)?;
let planted: Vec<String> = planted.iter().map(|p| paths.path(p)).collect();
writeln!(
out,
"✔ decoy keys {} planted: {}; reading or sending one is denied (rule canary)",
planted.len(),
planted.join(", ")
)?;
Ok(!planted.is_empty())
}
fn decoys_diff(offers: &[Offer], paths: &Paths, out: &mut Deferred) -> Result<()> {
for offer in offers {
let sample = offer.target.content(&offer.target.values()?);
writeln!(
out,
"--- {} (new file, {})",
paths.path(&offer.file),
offer.target.what
)?;
for line in sample.lines() {
writeln!(out, "+ {line}")?;
}
}
writeln!(
out,
" (the random part of each key is drawn again when it is planted)"
)?;
Ok(())
}
fn sandboxed_start(
home: &Home,
binary: &Path,
hosts: &[Host],
(user, paths): (&Path, &Paths),
out: &mut Deferred,
) -> Result<bool> {
let search = crate::environment::absolute_search_path();
let shims = start::offers(home, binary, hosts, &search, &RunHere::detect())?;
if shims.is_empty() {
return Ok(false);
}
let dir = start::shim_dir(home);
let shell = std::env::var("SHELL").ok();
let writes = match rc::file(user, shell.as_deref()) {
Some(file) => format!(
"a yes writes a shim to {} and, once, 2 lines to {}",
paths.path(&dir),
paths.path(&file)
),
None => format!(
"a yes writes a shim to {}; you add one line to your shell's start-up file",
paths.path(&dir)
),
};
writeln!(
out,
" Sandboxed start these agents run with the hook only: an allowed call runs with your \
permissions, so a classifier mistake is a bypass. `openmoat run` confines them; {writes}"
)?;
let score = crate::commands::run_default_score();
let mut chosen = Vec::new();
for shim in shims {
let question = format!(
" {:<16} {}: {}? [y/N/d] ",
shim.host.display_name(),
enable::score_change(score),
enable::what_starts(&shim)
);
let one = std::slice::from_ref(&shim);
if yes(&question, out, |out| enable::diff(home, one, user, out))? {
chosen.push(shim);
}
}
if chosen.is_empty() {
writeln!(
out,
" sandboxed start not set up; `openmoat confine <agent>` turns it on later, and \
`openmoat status` shows the command that starts each agent confined"
)?;
return Ok(false);
}
enable::enable(home, &chosen, (user, paths), out)?;
Ok(true)
}
fn yes(
question: &str,
out: &mut Deferred,
diff: impl Fn(&mut Deferred) -> Result<()>,
) -> Result<bool> {
loop {
write!(out, "{question}")?;
out.flush()?;
let answer = crate::terminal::read_answer()?;
if answer.is_empty() {
writeln!(out)?;
return Ok(false);
}
match answer.trim().to_ascii_lowercase().as_str() {
"d" | "diff" => diff(out)?,
"y" | "yes" => return Ok(true),
_ => return Ok(false),
}
}
}