use std::io::Write as _;
use std::path::{Path, PathBuf};
use anyhow::{Result, bail};
use openmoat_audit::{ChainReport, Store};
use openmoat_core::Policy;
use crate::cli::{DoctorArgs, Format};
use crate::exit::Code;
use crate::home::Home;
use crate::integrity::{self, HookPinGap, HookPins, Lock};
use crate::protection;
use crate::render::Deferred;
use crate::report::{Mark, Row};
use crate::sandbox::landlock::abi::{self, Kernel};
use human::Place;
use rows::Report;
mod bundle;
mod hooks;
mod human;
mod json;
mod mcp;
mod rows;
mod sandboxes;
mod state_files;
mod warnings;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
enum Area {
State,
Policy,
Lock,
Binary,
Environment,
Hook,
Sandbox,
Audit,
Service,
Mcp,
}
fn service(report: &mut Report, home: &Home) {
for (ok, text) in super::proxy::service_lines(home) {
report.line(Area::Service, ok, text);
}
}
pub fn run(args: &DoctorArgs) -> Result<Code> {
let home = Home::locate()?;
let binary = crate::install::hook_binary()?;
let as_json = args.format == Format::Json;
let mut report = Report::new(if as_json || args.bundle.is_some() {
Deferred::quiet()
} else {
Deferred::default()
});
report.line(
Area::State,
home.exists(),
format!("state directory {}", home.root().display()),
);
let policy = policies(&mut report, &home);
if let Some(update) = crate::upgrade::available(&home) {
report.warn_as(
&format!("policy update {update}: `openmoat policy upgrade` shows them and asks"),
Row::new(Mark::Attention, "Policy", format!("{update} to review"))
.fix("openmoat policy upgrade"),
);
}
let lock = load_lock(&mut report, &home);
let drift = lock
.as_ref()
.map(|lock| verify_lock(&mut report, &home, lock, &binary))
.unwrap_or_default();
environment(&mut report, &home);
state_files::rows(&mut report, &home);
let hook_files = hooks::hooks(&mut report, &home, &binary)?;
if let Some(lock) = &lock {
hook_pins(&mut report, lock, &home, &hook_files);
mcp::lines(&mut report, &home, lock, &binary);
}
if let Some(policy) = &policy {
sandboxes::sandboxes(
&mut report,
&home,
policy,
lock.as_ref(),
&binary,
args.verbose,
);
}
service(&mut report, &home);
let open = warnings::tiocsti_open(&home, &binary)?;
let tiocsti = warnings::tiocsti(open, warnings::legacy_tiocsti().as_deref());
let hangs = warnings::hangs(&home, crate::time::now_ms());
let undecided = Store::open_read_only(&home.audit_path())
.ok()
.and_then(|store| warnings::kernel_errors(&store, crate::time::now_ms()));
let linux = cfg!(target_os = "linux");
let state = policy
.as_ref()
.and_then(|policy| warnings::state_in_grant(&home, policy, linux));
let macos = warnings::old_macos(crate::os_version::macos().as_deref());
let network = super::run::port_only_network();
let project = policy
.as_ref()
.and_then(|policy| warnings::project_open(&home, policy));
let alias = warnings::alias_hooks(&home, &binary);
let expiring = warnings::grant_expiry(&home, crate::time::now_ms());
let keys = warnings::key_modes(&home);
for warning in [
tiocsti, hangs, undecided, state, macos, network, project, expiring,
]
.into_iter()
.flatten()
.chain(alias)
.chain(keys)
{
report.warn(&warning);
}
report.note(&warnings::relay_token());
report.note(warnings::RENAMED);
if let Some(note) = crate::os_version::wsl_note() {
report.note(¬e);
}
for warning in crate::project_settings::warnings_here(&home) {
let name = warning.host.display_name();
report.warn(&format!("{name:<16} project settings {}", warning.text));
}
for warning in crate::project_settings::other_hooks::warnings_here(&home) {
let name = warning.host.display_name();
report.warn(&format!("{name:<16} other hook {}", warning.text));
}
for warning in crate::project_settings::git_config::warnings_here() {
report.warn(&format!("{:<16} git config {warning}", "Repository"));
}
protections(&mut report, &home, &binary)?;
mcp::servers(&mut report, &home, &binary);
let boundary = protection::run_boundary(&home, &binary);
if let Some(boundary) = &boundary {
let text = format!("run boundary {}", boundary.describe());
report.line(Area::Sandbox, boundary.met, text);
}
let strict = boundary.as_ref().map(|b| b.by.as_str());
match super::run::tier_row(strict) {
Some((true, text)) => report.warn(&text),
Some((false, text)) => report.line(Area::Sandbox, true, text),
None => {}
}
if let Some(kernel) = super::run::landlock() {
landlock(&mut report, &kernel, args.verbose);
}
if let Some(policy) = &policy {
let hosts = crate::mcp::set_up_hosts(&home, &binary);
let names = super::run::withheld_names(policy, &hosts);
let line = super::run::withheld_line(&names, boundary.is_some());
report.note_at(&line, Place::OpenMoat);
}
match Store::open_read_only(&home.audit_path()).and_then(|store| store.verify_chain()) {
Ok(chain) => audit_line(&mut report, &chain),
Err(e) => report.line(Area::Audit, false, format!("audit log {e}")),
}
if let Some(last) = crate::anchor::Last::load(&home) {
let text = format!(
"audit anchor last made {} (event {}) in {}",
crate::time::timestamp(last.anchored_ms),
last.event,
last.to
);
report.note_at(&text, Place::OpenMoat);
}
if !as_json && args.bundle.is_none() {
let agents = std::mem::take(&mut report.agents);
human::sections(&mut report, &agents, args.verbose);
}
if args.accept {
let intact = drift.is_empty() && lock.is_some();
accept(&mut report, &home, &binary, policy.is_some(), intact)?;
}
if let Some(path) = &args.bundle {
return bundle::write(&home, &binary, &report.rows, path, args.bundle_events);
}
if as_json {
return json::finish(&report.rows, report.problems.len());
}
finish(report, args.verbose)
}
fn landlock(report: &mut Report, kernel: &Kernel, verbose: bool) {
let text = format!("landlock {}", abi::summary(kernel));
if kernel.meets_floor() {
report.line(Area::Sandbox, true, text);
} else {
report.warn(&text);
}
if verbose {
for line in abi::lines(kernel) {
report.human(Place::Details, Row::new(Mark::Info, "Landlock", line));
}
}
}
fn protections(report: &mut Report, home: &Home, binary: &Path) -> Result<()> {
let agents = protection::report(home, binary)?;
for agent in &agents {
if let Some(line) = crate::install::trust_note::protection_line(agent) {
report.note(&line);
if let Some(limit) = agent.limit {
let text = format!("{:<16} limit: {limit}", agent.name);
report.warn_at(&text, Place::Details);
}
report.note(&format!("{:<16} known gaps: {}", agent.name, agent.gaps));
}
if let Some(line) = agent
.hook_failure
.as_ref()
.and_then(protection::Failure::line)
{
report.note(&format!("{:<16} {line}", agent.name));
}
if let Some(required) = &agent.required {
let head = format!("required: {} ({})", required.label(), required.by);
match (&required.missing, &required.fix) {
(Some(missing), Some(fix)) => {
let detail = format!("{head}, not met: {missing}; to meet it, {fix}");
let row = Row::new(Mark::Broken, agent.name, detail.clone());
let text = format!("{:<16} {detail}", agent.name);
report.line_as(Area::Hook, false, text, Place::Details, row);
}
_ => report.note(&format!("{:<16} {head}, met", agent.name)),
}
}
}
report.agents = agents;
Ok(())
}
fn policies(report: &mut Report, home: &Home) -> Option<Policy> {
let policy = match home.load_policy() {
Ok(policy) => {
let (deny, allow, review) = policy.rule_count();
report.line_as(
Area::Policy,
true,
format!("policy lints ({deny} deny, {allow} allow, {review} review)"),
Place::OpenMoat,
Row::new(
Mark::Ok,
"Policy",
format!("{deny} deny · {allow} allow · {review} review rules"),
),
);
Some(policy)
}
Err(e) => {
report.line(Area::Policy, false, format!("policy {e:#}"));
None
}
};
match crate::repo::summary(home) {
Ok(Some(line)) => report.note_at(&format!("repo policy {line}"), Place::OpenMoat),
Ok(None) => {}
Err(e) => report.line(
Area::Policy,
false,
format!("repo policy {e:#}; every call in this project is denied"),
),
}
policy
}
fn load_lock(report: &mut Report, home: &Home) -> Option<Lock> {
let lock_path = home.lock_path();
match Lock::load(&lock_path) {
Ok(lock) => Some(lock),
Err(_) if !lock_path.exists() => {
report.line(
Area::Lock,
false,
"lock missing; run `openmoat init`".to_owned(),
);
None
}
Err(e) => {
report.line(Area::Lock, false, format!("lock {e:#}"));
None
}
}
}
fn verify_lock(
report: &mut Report,
home: &Home,
lock: &Lock,
binary: &Path,
) -> Vec<integrity::Drift> {
let drift = lock.verify();
if drift.is_empty() {
report.line(
Area::Lock,
true,
format!(
"lock {} files pinned, all intact",
lock.entries.len()
),
);
}
for d in &drift {
report.line(Area::Lock, false, format!("lock {d}"));
}
let own = integrity::binary::check(home, lock);
match (own.deny_reason(), own.note()) {
(Some(reason), _) => report.line(Area::Binary, false, format!("binary {reason}")),
(None, Some(note)) => report.warn(&format!("binary {note}")),
(None, None) => {}
}
if let Some(warning) = warnings::alias_lock(&lock.binary, binary) {
report.warn(&warning);
} else if lock.binary != binary.to_string_lossy() {
report.line(
Area::Binary,
false,
format!(
"binary lock expects {}, running {}; run `openmoat init`",
lock.binary,
binary.display()
),
);
}
drift
}
fn environment(report: &mut Report, home: &Home) {
match crate::environment::Snapshot::load(&home.environment_path()) {
Ok(snapshot) => report.line(
Area::Environment,
true,
format!(
"environment {} dirs, {} programs pinned",
snapshot.path.len(),
snapshot.programs.len()
),
),
Err(e) => report.line(
Area::Environment,
false,
format!("environment {e:#}; run `openmoat init`"),
),
}
}
fn hook_pins(report: &mut Report, lock: &Lock, home: &Home, hook_files: &[PathBuf]) {
let gap = HookPinGap::new(lock, home, hook_files);
for path in gap.unpinned {
report.line(
Area::Hook,
false,
format!(
"hook file {} is not pinned by the lock; run `openmoat init` to pin it",
path.display()
),
);
}
for path in gap.elsewhere {
let (path, moved) = (path.display(), HookPinGap::moved_by());
report.note_at(
&format!(
"hook file {path} is pinned but not this shell's ({moved}); re-pinning keeps it"
),
Place::OpenMoat,
);
}
}
fn accept(
report: &mut Report,
home: &Home,
binary: &Path,
policy_lints: bool,
intact: bool,
) -> Result<()> {
crate::terminal::refuse_agent("openmoat doctor --accept")?;
if !crate::terminal::interactive() {
bail!(
"`openmoat doctor --accept` must be run by a person in a terminal, not from a hook or script"
);
}
if !policy_lints {
bail!(
"refusing to pin a policy that does not lint; fix it, then run `openmoat doctor --accept` again"
);
}
writeln!(report.out)?;
if intact {
if home.lock_path().is_file() {
mcp::accept(report, home, binary)?;
}
writeln!(report.out, "nothing else to accept: lock is intact")?;
return Ok(());
}
let lock = integrity::repin(home, binary, HookPins::Keep)?;
writeln!(
report.out,
"✔ lock re-pinned for {} files",
lock.entries.len()
)?;
report
.problems
.retain(|(area, _)| !matches!(area, Area::Lock | Area::Binary));
mcp::accept(report, home, binary)
}
fn finish(mut report: Report, verbose: bool) -> Result<Code> {
human::footer(&mut report, verbose);
let healthy = report.problems.is_empty();
report.out.finish()?;
Ok(if healthy { Code::Ok } else { Code::Usage })
}
fn audit_line(report: &mut Report, chain: &ChainReport) {
if let Some(broken) = &chain.broken {
report.line(
Area::Audit,
false,
format!(
"audit log hash chain broken at event {}: {}; keep the file as evidence",
broken.id,
broken.kind.describe()
),
);
return;
}
report.line(
Area::Audit,
true,
format!(
"audit log {} events, hash chain intact",
chain.events
),
);
if let Some(head) = &chain.head {
report.note_at(
&format!(
"audit log head {head}; record it elsewhere (`openmoat audit anchor`), then `openmoat audit verify --against` it later"
),
Place::OpenMoat,
);
}
if chain.unchained > 0 {
report.note_at(
&format!(
"audit log {} events written before the hash chain existed are not covered",
chain.unchained
),
Place::OpenMoat,
);
}
}