use std::io::{self, IsTerminal as _, Write as _};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use anyhow::{Context as _, Result};
use openmoat_audit::{Event, Store};
use openmoat_core::{Policy, Verdict};
use openmoat_hosts::Host;
use crate::VERSION;
use crate::cli::{Only, WatchArgs};
use crate::exit::Code;
use crate::home::Home;
use crate::report::{Paths, Style};
mod row;
#[cfg(test)]
mod tests;
use row::View;
const POLL: Duration = Duration::from_millis(500);
const TICK: Duration = Duration::from_millis(50);
const PAGE: usize = 500;
const SCAN: usize = 5_000;
const DEFAULT_WIDTH: usize = 100;
pub fn run(args: &WatchArgs) -> Result<Code> {
let home = Home::locate()?;
let store = home.open_audit()?;
let policy = home.load_policy().ok();
let terminal = io::stdout().is_terminal();
let view = View {
style: Style::detect(),
width: terminal.then(width),
paths: Paths::detect(),
policy: policy.as_ref(),
};
let filter = Filter::of(args);
let stop = Arc::new(AtomicBool::new(false));
signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&stop))
.context("stopping on Ctrl-C")?;
let mut out = io::stdout().lock();
writeln!(out, "{}", view.style.bold(&header(&home, policy.as_ref())))?;
writeln!(out)?;
let mut last = store.latest_id()?;
for event in earlier(&store, &filter, args.recent)? {
print(&mut out, &event, &view)?;
}
out.flush()?;
if args.no_follow {
return Ok(Code::Ok);
}
loop {
if wait(&stop) {
return Ok(Code::Ok);
}
for event in store.after(last, PAGE)? {
last = Some(event.id);
if filter.keeps(&event) {
print(&mut out, &event, &view)?;
}
}
out.flush()?;
}
}
fn wait(stop: &AtomicBool) -> bool {
let mut waited = Duration::ZERO;
while waited < POLL {
if stop.load(Ordering::Relaxed) {
return true;
}
std::thread::sleep(TICK);
waited += TICK;
}
stop.load(Ordering::Relaxed)
}
fn print(out: &mut impl io::Write, event: &Event, view: &View<'_>) -> io::Result<()> {
for line in row::rows(event, view) {
writeln!(out, "{line}")?;
}
Ok(())
}
fn earlier(store: &Store, filter: &Filter, count: usize) -> Result<Vec<Event>> {
if count == 0 {
return Ok(Vec::new());
}
let mut kept: Vec<Event> = store
.recent(SCAN)?
.into_iter()
.filter(|e| filter.keeps(e))
.take(count)
.collect();
kept.reverse();
Ok(kept)
}
fn width() -> usize {
std::env::var("COLUMNS")
.ok()
.and_then(|c| c.trim().parse().ok())
.filter(|w| *w > 0)
.unwrap_or(DEFAULT_WIDTH)
}
fn header(home: &Home, policy: Option<&Policy>) -> String {
let agents = set_up(home);
let watching = if agents.is_empty() {
"watching the audit log".to_owned()
} else {
format!("watching {}", agents.join(", "))
};
let rules = policy.map(|p| {
let (deny, allow, review) = p.rule_count();
format!(" · policy {deny} deny · {allow} allow · {review} review")
});
format!(
"OpenMoat {VERSION} · {watching}{}",
rules.unwrap_or_default()
)
}
fn set_up(home: &Home) -> Vec<&'static str> {
let Ok(binary) = crate::install::hook_binary() else {
return Vec::new();
};
Host::ALL
.into_iter()
.filter(|host| {
crate::install::HostConfig::for_host(home, *host).is_ok_and(|c| c.set_up(&binary))
})
.map(Host::display_name)
.collect()
}
struct Filter {
host: Option<&'static str>,
verdict: Option<Verdict>,
session: Option<String>,
}
impl Filter {
fn of(args: &WatchArgs) -> Self {
Self {
host: args.host.map(Host::id),
verdict: args.only.map(|only| match only {
Only::Deny => Verdict::Deny,
Only::Review => Verdict::Review,
}),
session: args.session.as_deref().map(openmoat_audit::redact),
}
}
fn keeps(&self, event: &Event) -> bool {
self.host.is_none_or(|h| event.host == h)
&& self.verdict.is_none_or(|v| event.verdict == v)
&& self.session.as_ref().is_none_or(|s| &event.session_id == s)
}
}