use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result, bail};
use openmoat_hosts::{Host, Managed, ManagedHooks, Platform, SystemFile};
use serde_json::Value;
use crate::home::Home;
use crate::install::HostConfig;
mod command;
#[cfg(not(unix))]
mod mode_other;
#[cfg(unix)]
mod mode_unix;
#[cfg(test)]
mod tests;
#[cfg(not(unix))]
use mode_other as mode;
#[cfg(unix)]
use mode_unix as mode;
pub use command::install_command;
#[cfg(debug_assertions)]
const TEST_ROOT: &str = "MOAT_TEST_SYSTEM_ROOT";
#[must_use]
pub fn hooks_of(host: Host) -> Option<&'static ManagedHooks> {
match &host.spec().hook_off.managed {
Managed::Written(hooks) => Some(hooks),
Managed::Manual { .. } | Managed::None { .. } => None,
}
}
#[must_use]
pub fn system_path(file: &SystemFile) -> PathBuf {
#[cfg(debug_assertions)]
if let Some(root) = std::env::var_os(TEST_ROOT).filter(|v| !v.is_empty()) {
let rel = file
.path
.split_once(":/")
.map_or(file.path, |(_, rest)| rest);
return PathBuf::from(root).join(rel.trim_start_matches('/'));
}
PathBuf::from(file.path)
}
fn on_this_platform(files: &[SystemFile], platform: Platform) -> Option<PathBuf> {
files
.iter()
.find(|file| file.platforms.contains(&platform))
.map(system_path)
}
#[must_use]
pub fn target(hooks: &ManagedHooks) -> Option<PathBuf> {
on_this_platform(hooks.files, crate::os_version::platform())
}
#[must_use]
pub fn shadow(hooks: &ManagedHooks) -> Option<PathBuf> {
let platform = crate::os_version::platform();
hooks
.shadowed_by
.iter()
.filter(|file| file.platforms.contains(&platform))
.map(system_path)
.find(|path| path.exists())
}
fn staged_path(home: &Home, host: Host) -> PathBuf {
home.root()
.join("managed")
.join(format!("{}.json", host.id()))
}
fn content(config: &HostConfig, binary: &Path) -> Result<String> {
match config.render(binary, "")? {
Some(text) => Ok(text),
None => bail!("no hook document for {}", config.host.display_name()),
}
}
fn read(path: &Path) -> io::Result<Option<Vec<u8>>> {
match fs::read(path) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
fn only_ours(config: &HostConfig, bytes: &[u8]) -> bool {
let Ok(mut root) = serde_json::from_slice::<Value>(bytes) else {
return false;
};
config.remove(&mut root) && empty(&root)
}
fn empty(value: &Value) -> bool {
match value {
Value::Null => true,
Value::Array(items) => items.iter().all(empty),
Value::Object(map) => map
.iter()
.all(|(key, v)| (key == "version" && v.is_number()) || empty(v)),
_ => false,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum State {
NoPlace,
Current(PathBuf),
Missing(PathBuf),
Outdated(PathBuf),
Theirs(PathBuf),
Unreadable(PathBuf, String),
}
pub fn state(hooks: &ManagedHooks, config: &HostConfig, binary: &Path) -> Result<State> {
let Some(path) = target(hooks) else {
return Ok(State::NoPlace);
};
let want = content(config, binary)?;
Ok(match read(&path) {
Ok(None) => State::Missing(path),
Ok(Some(bytes)) if bytes == want.as_bytes() => State::Current(path),
Ok(Some(bytes)) if only_ours(config, &bytes) => State::Outdated(path),
Ok(Some(_)) => State::Theirs(path),
Err(e) => State::Unreadable(path, e.to_string()),
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Done {
Unchanged(PathBuf),
Written(PathBuf),
Staged {
target: PathBuf,
staged: PathBuf,
command: String,
},
Skipped(String),
}
pub fn install(home: &Home, config: &HostConfig, binary: &Path, dry_run: bool) -> Result<Done> {
let host = config.host;
let Some(hooks) = hooks_of(host) else {
return Ok(Done::Skipped(format!(
"{} has no managed hook file OpenMoat writes",
host.display_name()
)));
};
let target = match state(hooks, config, binary)? {
State::NoPlace => {
return Ok(Done::Skipped(format!(
"{} reads no managed hook file on this platform",
host.display_name()
)));
}
State::Current(path) => return Ok(Done::Unchanged(path)),
State::Theirs(path) => {
return Ok(Done::Skipped(format!(
"{} holds an administrator's own hooks, which OpenMoat does not edit; add \
OpenMoat's entries from {} to it",
path.display(),
config.settings_path.display()
)));
}
State::Unreadable(path, why) => {
return Ok(Done::Skipped(format!(
"{} cannot be read ({why}), so OpenMoat cannot tell whose it is and leaves it \
alone; an administrator can add OpenMoat's entries from {} to it",
path.display(),
config.settings_path.display()
)));
}
State::Missing(path) | State::Outdated(path) => path,
};
if dry_run {
return Ok(Done::Written(target));
}
let text = content(config, binary)?;
if !hooks.root_owned && mode::write_readable(&target, text.as_bytes()).is_ok() {
return Ok(Done::Written(target));
}
let staged = staged_path(home, host);
crate::home::write_private(&staged, text.as_bytes())
.with_context(|| format!("keeping {}", staged.display()))?;
let digest = crate::integrity::sha256_hex(text.as_bytes());
let command = install_command(&staged, &digest, &target, crate::os_version::platform());
Ok(Done::Staged {
target,
staged,
command,
})
}
pub fn uninstall(home: &Home, config: &HostConfig) -> Option<String> {
let host = config.host;
let hooks = hooks_of(host)?;
let staged = staged_path(home, host);
let _ = fs::remove_file(&staged);
let target = target(hooks)?;
let bytes = read(&target).ok()??;
if !only_ours(config, &bytes) {
return Some(format!(
"{} holds more than OpenMoat's hook and was kept",
target.display()
));
}
Some(match fs::remove_file(&target) {
Ok(()) => format!("{} removed", target.display()),
Err(_) => format!(
"{} needs an administrator to remove it, or {} keeps running OpenMoat's hook: {}",
target.display(),
host.display_name(),
command::remove_command(&target, crate::os_version::platform())
),
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Row {
Note(String),
Warn(String),
}
pub fn doctor_rows(home: &Home, binary: &Path) -> Vec<Row> {
crate::mcp::set_up_hosts(home, binary)
.into_iter()
.filter_map(|host| {
let hooks = hooks_of(host)?;
let config = HostConfig::for_host(home, host).ok()?;
let state = state(hooks, &config, binary).ok()?;
row(home, host, hooks, &state)
})
.collect()
}
fn row(home: &Home, host: Host, hooks: &ManagedHooks, state: &State) -> Option<Row> {
let name = format!("{:<16} managed hook", host.display_name());
let holds = if hooks.outlasts_project {
"a project's settings cannot turn it off"
} else {
"it outranks the person's own hooks"
};
let staged = staged_path(home, host);
Some(match state {
State::NoPlace => return None,
State::Current(path) => match shadow(hooks) {
Some(shadow) => Row::Warn(format!(
"{name} {} holds OpenMoat's hook, but {} is present and {} applies that source \
instead of the file",
path.display(),
shadow.display(),
host.display_name()
)),
None if hooks.unseen_shadows.is_empty() => {
Row::Note(format!("{name} {}: {holds}", path.display()))
}
None => Row::Note(format!(
"{name} {}: {holds}, unless {} supply a policy, which {} applies instead",
path.display(),
hooks.unseen_shadows,
host.display_name()
)),
},
State::Missing(path) | State::Outdated(path) if staged.exists() => {
let digest = fs::read(&staged)
.map(|bytes| crate::integrity::sha256_hex(&bytes))
.unwrap_or_default();
Row::Warn(format!(
"{name} {} is not in place yet; run: {}",
path.display(),
install_command(&staged, &digest, path, crate::os_version::platform())
))
}
State::Missing(path) => Row::Note(format!(
"{name} `openmoat init --managed` writes OpenMoat's hook to {}, where {holds}",
path.display()
)),
State::Outdated(path) => Row::Warn(format!(
"{name} {} holds an older OpenMoat hook; run `openmoat init --managed`",
path.display()
)),
State::Theirs(path) => Row::Note(format!(
"{name} {} holds an administrator's own hooks; OpenMoat's are not among them",
path.display()
)),
State::Unreadable(path, why) => {
Row::Note(format!("{name} {} cannot be read ({why})", path.display()))
}
})
}
pub fn outlasts_project(config: &HostConfig, binary: &Path) -> bool {
hooks_of(config.host).is_some_and(|hooks| {
hooks.outlasts_project
&& shadow(hooks).is_none()
&& matches!(state(hooks, config, binary), Ok(State::Current(_)))
})
}