use std::io::Write as _;
use std::path::{Path, PathBuf};
use anyhow::{Result, bail};
use openmoat_core::{DEFAULT_POLICY, Policy};
use openmoat_hosts::Host;
use crate::cli::InitArgs;
use crate::environment::Snapshot;
use crate::exit::Code;
use crate::home::Home;
use crate::install::{HookLayout, HostConfig, Outcome, Recorded, dir_variable, env_config_dir};
use crate::integrity;
use crate::render::Deferred;
use crate::report::Style;
use crate::sandbox::{self, Plan};
mod ask;
mod managed;
pub(super) mod mcp;
mod preview;
mod refuse;
mod summary;
#[cfg(test)]
mod tests;
mod unattended;
use refuse::{refuse_repin, refuse_writable_binary};
pub fn run(args: &InitArgs) -> Result<Code> {
let dry_run = args.dry_run;
let verbose = args.verbose || dry_run;
if args.managed.managed {
managed::refuse_unless_person()?;
}
let home = Home::locate()?;
let binary = crate::install::hook_binary()?;
refuse_writable_binary(&home, &binary)?;
let snapshot = search_path(&home)?;
let mut out = Deferred::default();
let mut detail = if verbose {
Deferred::default()
} else {
Deferred::quiet()
};
let title = if dry_run {
"what `openmoat init` would change (dry run)"
} else {
"protecting the agents on this machine"
};
writeln!(
out,
"{}\n",
Style::detect().bold(&format!("OpenMoat {} · {title}", crate::VERSION))
)?;
if snapshot.is_none() {
writeln!(
out,
"! environment {} kept: this shell's PATH finds none of the programs \
OpenMoat pins; run `openmoat init` from a normal shell to record a new one\n",
home.environment_path().display()
)?;
}
let lock_existed = !dry_run && refuse_repin(&home, &mut out)?;
let unattended =
unattended::applies(lock_existed, crate::terminal::person_for("openmoat init"));
if unattended {
let recorded = crate::install::self_paths::recorded_binary(&home)?;
unattended::refuse_other_binary(&recorded, &binary)?;
}
let hosts = choose_hosts(&home, args, &binary, &mut out)?;
let mut steps = summary::Steps {
update: state(&home, dry_run, unattended, snapshot.as_ref(), &mut detail)?,
..summary::Steps::default()
};
if !dry_run {
home.ensure_approval_files()?;
record(&home, &hosts, unattended)?;
}
let mut files = Writers {
out: &mut out,
detail: &mut detail,
};
for host in hosts.iter().copied() {
install(
&home, &binary, host, unattended, dry_run, &mut steps, &mut files,
)?;
}
steps.changed |= sandboxes(&home, &binary, &hosts, dry_run, &mut files)?;
if args.managed.managed {
managed::write(&home, &binary, &hosts, dry_run, files.out)?;
}
if dry_run {
let detail = &mut *files.detail;
writeln!(
detail,
"would lock {}",
home.lock_path().display()
)?;
if args.mcp.mcp_proxy {
let sandbox = args.mcp.sandbox();
super::mcp::wrap_servers(&home, &binary, &hosts, &[], sandbox, true, detail)?;
}
writeln!(files.out, "dry run: nothing was written")?;
} else {
let lock = integrity::repin(&home, &binary, integrity::HookPins::Adopt)?;
writeln!(
files.detail,
"✔ lock {} ({} files pinned)",
home.lock_path().display(),
lock.entries.len()
)?;
let wrap = mcp::Wrap::of(args, unattended);
mcp::servers(&home, &binary, wrap, &hosts, unattended, &mut files)?;
let backups = if hosts.is_empty() {
0
} else {
backups(&home, &hosts, files.detail)?
};
summary::write(&home, &binary, &hosts, &steps, backups, verbose, files.out)?;
}
detail.finish()?;
out.finish()?;
Ok(Code::Ok)
}
struct Writers<'a> {
out: &'a mut Deferred,
detail: &'a mut Deferred,
}
fn install(
home: &Home,
binary: &Path,
host: Host,
unattended: bool,
dry_run: bool,
steps: &mut summary::Steps,
files: &mut Writers<'_>,
) -> Result<()> {
let prefix = if dry_run { "would" } else { "✔" };
let config = unattended::config(home, host, unattended)?;
let replaced = match config.layout {
HookLayout::Owned(file) | HookLayout::Script(file) => {
crate::install::owned::persons_file(&config, file)
}
HookLayout::Entries(_) | HookLayout::Plugin(_) | HookLayout::Yaml(_) => None,
};
let outcome = config.install(home, binary, dry_run)?;
let verb = match outcome {
Outcome::Installed => "installed",
Outcome::Updated => "updated",
Outcome::Unchanged => "unchanged",
};
steps.changed |= outcome != Outcome::Unchanged;
let events: Vec<&str> = config.hooks.iter().map(|spec| spec.event).collect();
writeln!(
files.detail,
"{prefix} {:<16} {} ({verb}: {} → {}{})",
host.display_name(),
config.settings_path.display(),
events.join(", "),
binary.display(),
config.guard(binary).shell_args(host.id())
)?;
if let Some(backup) = replaced.filter(|_| outcome != Outcome::Unchanged) {
let verb = if dry_run { "would move" } else { "moved" };
writeln!(
files.out,
"! {:<16} {verb} your own hook {} to {}: {} runs one file there, so yours does \
not run while OpenMoat is installed; `openmoat uninstall` puts it back",
host.display_name(),
config.settings_path.display(),
backup.display(),
host.display_name()
)?;
}
let trust = crate::install::trust_note::after_install(host, outcome).or_else(|| {
config
.trust_warning()
.and_then(|_| crate::install::trust_note::step(host))
});
if let Some(note) = trust {
writeln!(
files.detail,
"{prefix} {:<16} next: {note}",
host.display_name()
)?;
steps.trust.push((host.display_name(), note));
}
Ok(())
}
#[derive(Debug, PartialEq, Eq)]
struct RepinChecks {
agent: bool,
drift: bool,
}
fn repin_checks(lock_exists: bool, interactive: bool) -> RepinChecks {
RepinChecks {
agent: lock_exists,
drift: lock_exists && !interactive,
}
}
fn state(
home: &Home,
dry_run: bool,
unattended: bool,
snapshot: Option<&Snapshot>,
out: &mut Deferred,
) -> Result<Option<String>> {
let prefix = if dry_run { "would" } else { "✔" };
if !dry_run {
home.ensure()?;
}
writeln!(out, "{prefix} state directory {}", home.root().display())?;
let policy_path = home.policy_path();
let wrote_policy = if dry_run {
!policy_path.exists()
} else {
home.ensure_policy()?
};
let mut update = None;
if wrote_policy {
writeln!(
out,
"{prefix} policy {} (defaults v1)",
policy_path.display()
)?;
} else {
writeln!(out, "✔ policy {} (kept)", policy_path.display())?;
update = crate::upgrade::available(home);
if let Some(update) = &update {
writeln!(
out,
"! policy update {update}: `openmoat policy upgrade` shows them and asks"
)?;
}
}
let kept = if dry_run {
None
} else {
crate::audit_key::open(home)?
};
writeln!(
out,
"{prefix} audit log {}",
home.audit_path().display()
)?;
if let Some(kept) = kept {
writeln!(
out,
" a new audit log starts with a new audit key; the previous key is kept as {} \
(it still checks a log you moved aside)",
kept.display()
)?;
}
if dry_run {
writeln!(
out,
"would environment {}",
home.environment_path().display()
)?;
} else if unattended && home.environment_path().is_file() {
writeln!(
out,
"· environment {} kept: no person at a terminal to record a new search path",
home.environment_path().display()
)?;
} else if let Some(snapshot) = snapshot {
snapshot.save(&home.environment_path())?;
writeln!(
out,
"✔ environment {} ({} dirs, {} programs pinned)",
home.environment_path().display(),
snapshot.path.len(),
snapshot.programs.len()
)?;
} else {
writeln!(
out,
"· environment {} kept",
home.environment_path().display()
)?;
}
Ok(update)
}
fn search_path(home: &Home) -> Result<Option<Snapshot>> {
let snapshot = Snapshot::capture();
if !snapshot.programs.is_empty() {
return Ok(Some(snapshot));
}
if home.environment_path().is_file() {
return Ok(None);
}
bail!(
"this shell's PATH finds none of the programs OpenMoat pins (sh, git, curl, …), so \
there is no search path to record and nothing was changed; run `openmoat init` from a \
normal shell"
)
}
fn backups(home: &Home, hosts: &[Host], out: &mut Deferred) -> Result<usize> {
let recorded = Recorded::load(home)?;
let mut backups = Vec::new();
for host in hosts {
for file in host_files(&HostConfig::for_host(home, *host)?) {
let made = crate::install::backups(&file);
backups.extend(made.into_iter().filter(|b| recorded.owns(b)));
}
}
let backups: Vec<String> = backups.iter().map(|p| p.display().to_string()).collect();
if backups.is_empty() {
writeln!(
out,
"· backups none: the agents had no files to back up"
)?;
} else {
writeln!(out, "✔ backups {}", backups.join(", "))?;
}
Ok(backups.len())
}
fn choose_hosts(
home: &Home,
args: &InitArgs,
binary: &std::path::Path,
out: &mut Deferred,
) -> Result<Vec<Host>> {
if let Some(explicit) = &args.hosts {
return supported(explicit.iter().copied(), out);
}
let mut found = Vec::new();
for config in Host::ALL
.into_iter()
.filter_map(|h| HostConfig::for_init(home, h).ok())
{
if config.host_present() {
found.push(config);
} else if env_config_dir(config.host)?.is_some()
&& crate::install::location::custom(config.host)
.is_none_or(openmoat_hosts::Locate::variables_are_the_hosts)
{
let name = config.host.display_name();
writeln!(
out,
"· {name}: {} is {}, which does not exist yet; start {name} once to create \
it, then run openmoat init again",
dir_variable(config.host)?.0,
config.dir().display()
)?;
}
}
let takes_hook = supported(found.iter().map(|c| c.host), out)?;
found.retain(|c| takes_hook.contains(&c.host));
let names: Vec<&str> = found.iter().map(|c| c.host.display_name()).collect();
if found.is_empty() {
writeln!(out, "· No agents found; pass --hosts to set one up")?;
return Ok(Vec::new());
}
if args.yes || args.dry_run {
return Ok(found.iter().map(|c| c.host).collect());
}
if !crate::terminal::interactive() {
writeln!(
out,
"! OpenMoat found {}; none changed without a terminal: run `openmoat init` \
in a terminal to choose, or `openmoat init --yes` to set up all of them",
names.join(", ")
)?;
return Ok(Vec::new());
}
let plan = sandbox_plan(home, binary).ok();
let asking = ask::Asking {
plan: plan.as_ref(),
verbose: args.verbose,
every_agent: args.mcp.mcp_proxy,
};
let chosen = ask::ask(home, &found, binary, &asking, out)?;
if chosen.is_empty() {
writeln!(out, "· No agent accepted; no agent's files were changed")?;
}
Ok(chosen)
}
fn supported(hosts: impl Iterator<Item = Host>, out: &mut Deferred) -> Result<Vec<Host>> {
let mut kept = Vec::new();
for host in hosts {
match crate::install::host_version::unsupported(host) {
Some(why) => writeln!(out, "· {:<16} not set up: {why}", host.display_name())?,
None => kept.push(host),
}
}
Ok(kept)
}
fn host_files(config: &HostConfig) -> Vec<PathBuf> {
let mut files = vec![config.settings_path.clone()];
let spec = config.host.spec();
if let (Some(sandbox), Some(install)) = (&spec.sandbox, &spec.install)
&& sandbox.file != install.config_file
{
files.push(config.settings_path.with_file_name(sandbox.file));
}
files.extend(config.extra_files());
files
}
fn record(home: &Home, hosts: &[Host], unattended: bool) -> Result<()> {
let mut recorded = Recorded::load(home)?;
for host in hosts {
let config = HostConfig::for_init(home, *host)?;
unattended::record(&mut recorded, &config, unattended);
}
recorded.save(home)
}
fn sandbox_plan(home: &Home, binary: &std::path::Path) -> Result<Plan> {
let policy = if home.policy_path().exists() {
home.load_policy()?
} else {
Policy::parse(DEFAULT_POLICY)?
};
Plan::for_binary(home, &policy, binary)
}
fn sandboxes(
home: &Home,
binary: &std::path::Path,
hosts: &[Host],
dry_run: bool,
files: &mut Writers<'_>,
) -> Result<bool> {
let prefix = if dry_run { "would" } else { "✔" };
let selected: Vec<Host> = sandbox::install::hosts()
.filter(|h| hosts.contains(h))
.collect();
if selected.is_empty() {
return Ok(false);
}
let plan = match sandbox_plan(home, binary) {
Ok(plan) => plan,
Err(e) => {
writeln!(files.out, "✗ sandbox not written: {e:#}")?;
return Ok(false);
}
};
let out = &mut *files.detail;
let mut any = false;
for host in selected {
let changed = sandbox::install::write(home, host, &plan, dry_run)?;
any |= changed;
if let Some(note) = sandbox::install::unavailable(host) {
if changed {
writeln!(
out,
"{prefix} {:<16} {}",
host.display_name(),
sandbox::install::old_sandbox_removed(host)
)?;
}
writeln!(out, "· {:<16} {note}", host.display_name())?;
continue;
}
let verb = if changed { "updated" } else { "unchanged" };
let report = sandbox::install::report(host, &plan);
writeln!(
out,
"{prefix} {:<16} {} (sandbox {verb}; {} stricter, {} wider than the policy: `openmoat sandbox show`)",
host.display_name(),
sandbox::install::settings_path(home, host)?.display(),
report.losses.len(),
report.allowances.len()
)?;
}
Ok(any)
}