//! Local operator CLI. Verifies the scoped OIDC review; a fresh ordinary Sync is still required. #[cfg(any(windows, test))] #[path = "windows_reviewed_recovery/local.rs"] mod local; #[cfg(windows)] mod windows { use super::local::{LocalJournals, Reviewed}; use anyhow::{Context, Result, ensure}; use fs2::FileExt; use rmm_agent_core::{ auth::Binding, windows_sync_history::{BINDING_SHA256, BOOT, FAULT, verify_local_history}, }; use rmm_agent_windows::{ SERVICE_NAME, helper_ipc::{self as ipc, PinnedFile}, helper_runtime::intent, helper_system, native, }; use rmm_domain::{jobs::hash, windows_sync_history::case_witness}; use rusqlite::{Connection, OpenFlags}; use std::{ fs::{File, OpenOptions}, io::{Read, Seek, SeekFrom}, os::windows::{ fs::{MetadataExt, OpenOptionsExt}, io::AsRawHandle, }, path::Path, }; use windows_sys::Win32::{ Storage::FileSystem::{BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle}, System::Services::*, }; const ROOT: &str = r"C:\ProgramData\RustDiscovery"; const STATE: &str = r"C:\ProgramData\RustDiscovery\state"; const RECEIPT: &str = r"C:\ProgramData\RustDiscoveryBootstrap\windows-checkpoint-review.json"; const PASSIVE: &str = "7c9f6f3642ef84337c3cd761d8a4788c4babe81522fdd987a3eb9b819bc8d851"; const AGENT_DB: &str = r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite"; const LOCAL_DB: &str = r"C:\ProgramData\RustDiscovery\state\consumption.sqlite"; const HELPER_DB: &str = r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite"; struct ScHandle(SC_HANDLE); impl Drop for ScHandle { fn drop(&mut self) { unsafe { CloseServiceHandle(self.0); } } } fn is_stopped(name: &str) -> Result { let mgr = ScHandle(unsafe { OpenSCManagerW(std::ptr::null(), std::ptr::null(), SC_MANAGER_CONNECT) }); ensure!(!mgr.0.is_null(), "SCM unreadable"); let name: Vec = name.encode_utf16().chain(Some(0)).collect(); let svc = ScHandle(unsafe { OpenServiceW(mgr.0, name.as_ptr(), SERVICE_QUERY_STATUS) }); ensure!(!svc.0.is_null(), "service missing"); let mut status: SERVICE_STATUS_PROCESS = unsafe { std::mem::zeroed() }; let mut size = 0; ensure!( unsafe { QueryServiceStatusEx( svc.0, SC_STATUS_PROCESS_INFO, (&mut status as *mut SERVICE_STATUS_PROCESS).cast(), size_of::() as u32, &mut size, ) } != 0, "service status unreadable" ); Ok(status.dwCurrentState == SERVICE_STOPPED && status.dwProcessId == 0) } fn both_stopped() -> Result { Ok(is_stopped(SERVICE_NAME)? && is_stopped(ipc::HELPER_SERVICE)?) } fn leaf(path: &Path) -> Result { for a in path.ancestors() { ensure!( a.symlink_metadata()?.file_attributes() & 0x400 == 0, "reparse path" ); } let f = OpenOptions::new().read(true).share_mode(3).open(path)?; let mut i: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() }; ensure!( unsafe { GetFileInformationByHandle(f.as_raw_handle(), &mut i) } != 0 && i.nNumberOfLinks == 1 && i.dwFileAttributes & 0x410 == 0, "unexpected file/hardlink" ); Ok(f) } struct Db { db: Connection, _handles: Vec, } impl Db { fn open(path: &str, write: bool, stopped: bool) -> Result { let mut main = leaf(Path::new(path))?; let mut header = [0u8; 20]; main.read_exact(&mut header)?; ensure!( header.starts_with(b"SQLite format 3\0") && header[18] == header[19] && matches!(header[18], 1 | 2), "unexpected SQLite header" ); let mut files = vec![main]; let mut has_wal = false; let mut has_shm = false; let mut has_journal = false; for (suffix, present) in [ ("-wal", &mut has_wal), ("-shm", &mut has_shm), ("-journal", &mut has_journal), ] { let name = format!("{path}{suffix}"); if Path::new(&name).try_exists()? { files.push(leaf(Path::new(&name))?); *present = true; } } let flags = if write { OpenFlags::SQLITE_OPEN_READ_WRITE } else { OpenFlags::SQLITE_OPEN_READ_ONLY }; let db = if !write && header[18] == 2 && !(has_wal && has_shm) { // Only under both exclusive lifetime service locks, after a clean WAL close. ensure!( stopped && !has_wal && !has_shm && !has_journal, "read-only WAL snapshot unavailable" ); let url = format!("file:///{}?mode=ro&immutable=1", path.replace('\\', "/")); Connection::open_with_flags(url, flags | OpenFlags::SQLITE_OPEN_URI)? } else { Connection::open_with_flags(path, flags)? }; db.busy_timeout(std::time::Duration::from_secs(2))?; db.execute_batch("PRAGMA trusted_schema=OFF")?; if write { db.execute_batch("PRAGMA synchronous=FULL")?; } else { db.execute_batch("PRAGMA query_only=ON; BEGIN;")?; } Ok(Self { db, _handles: files, }) } } fn data(file: &mut File, max: u64) -> Result> { ensure!(file.metadata()?.len() <= max, "public file too large"); file.seek(SeekFrom::Start(0))?; let mut bytes = vec![]; file.take(max + 1).read_to_end(&mut bytes)?; ensure!(bytes.len() as u64 <= max, "public file changed"); Ok(bytes) } pub fn run() -> Result<()> { let args: Vec<_> = std::env::args().skip(1).collect(); ensure!( args.len() == 1 && matches!( args[0].as_str(), "prepare" | "read" | "verify-receipt" | "apply" | "echo-read" | "echo-verify-receipt" | "echo-apply" ), "use prepare, read, verify-receipt or apply" ); let echo = args[0].starts_with("echo-"); let mode = args[0].strip_prefix("echo-").unwrap_or(&args[0]); let apply = mode == "apply"; let key_readers = [ipc::service_sid(SERVICE_NAME)?]; let _root = PinnedFile::open(Path::new(ROOT), true, &key_readers, &[], false)?; let mut binding_file = PinnedFile::open( &Path::new(ROOT).join("binding.json"), false, &key_readers, &[], false, )?; let bytes = data(&mut binding_file.file, 8192)?; ensure!(hash(&bytes) == BINDING_SHA256, "changed job binding"); let agent: Binding = serde_json::from_slice(&bytes)?; let (settings, _helper_pins) = helper_system::read_settings()?; let witness = case_witness(); ensure!( !settings.enable_effects && !settings.allow_reboot, "expected passive helper policy" ); let mut config = PinnedFile::open( &Path::new(ROOT).join("config.json"), false, &key_readers, &[], false, )?; if echo { let expected = serde_json::json!({ "server_name":"discovery.ease-technologies.de","connect_address":"88.99.80.202:443", "ca_file":Path::new(ROOT).join("server-ca.pem"),"state_directory":STATE, "enrollment_token_file":Path::new(STATE).join("enrollment.token"),"transport":"auto", "job_binding_file":Path::new(ROOT).join("binding.json"),"enable_jobs":true, "windows_execution_profile":"powershell_51_remote_signed","windows_echo_probe_only":true}); let actual: serde_json::Value = serde_json::from_slice(&data(&mut config.file, 32768)?)?; ensure!(actual == expected, "expected exact Echo-only configuration"); } else { ensure!( config.sha256()? == PASSIVE, "operator must restore passive config first" ); } let boot = tokio::runtime::Builder::new_current_thread() .enable_all() .build()? .block_on(native::boot_id())?; ensure!(boot == BOOT, "unexpected Windows boot"); let review = if matches!(mode, "verify-receipt" | "apply") { let _folder = PinnedFile::open( Path::new(RECEIPT).parent().context("bootstrap path")?, true, &[], &[], false, )?; let mut file = PinnedFile::open(Path::new(RECEIPT), false, &[], &[], false)?; let bytes = data(&mut file.file, 65536)?; Some(if echo { let scope = rmm_agent_windows::echo_probe::protected_scope()?; Reviewed::verify_echo(bytes, &agent, &settings.binding, &witness, &scope)? } else { Reviewed::verify(bytes, &agent, &settings.binding, &witness)? }) } else { None }; let mut locks = vec![]; let quiescent = both_stopped()?; ensure!( !apply || quiescent, "both services must be stopped by the operator" ); if quiescent { for path in [ Path::new(STATE).join("agent.lock"), Path::new(helper_system::STATE_ROOT).join("executor.lock"), ] { let f = leaf(&path)?; f.try_lock_exclusive().context("service lock busy")?; locks.push(f); } ensure!(both_stopped()?, "service started during recovery"); } native::private_path(Path::new(STATE), true)?; for p in [AGENT_DB, LOCAL_DB] { native::private_path(Path::new(p), false)?; } let helper_sid = [ipc::service_sid(ipc::HELPER_SERVICE)?]; let _helper_state = PinnedFile::open_read_shared( Path::new(helper_system::STATE_ROOT), true, &[], &helper_sid, )?; let helper_file = PinnedFile::open_read_shared(Path::new(HELPER_DB), false, &[], &helper_sid)?; let mut a = Db::open(AGENT_DB, apply, quiescent)?; let c = Db::open(LOCAL_DB, false, quiescent)?; let h = Db::open(HELPER_DB, false, quiescent)?; let (ledger, lock, pin): (Vec, Vec, String) = h.db.query_row( "SELECT ledger,executor_lock,binding_sha256 FROM windows_helper_files WHERE id=1", [], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)), )?; ensure!( serde_json::from_slice::(&ledger)? == helper_file.identity && pin == ipc::binding_hash(&settings.binding)?, "helper journal or binding changed" ); let _intents = if Path::new(intent::DB).try_exists()? { Some(intent::Store::open_empty_history_snapshot()?) } else { None }; if apply { ensure!( serde_json::from_slice::(&lock)? == ipc::file_identity(&locks[1])?, "helper lock identity changed" ); ensure!(both_stopped()?, "service started during recovery"); let mut db = LocalJournals { agent: &mut a.db, consumption: &c.db, helper: &h.db, agent_binding: &agent, helper_binding: &settings.binding, }; println!( "{}", db.apply( review.as_ref().context("missing real human receipt")?, chrono::Utc::now )? ); } else { let fault: Option = a.db.query_row("SELECT fault FROM reconcile_meta WHERE id=1", [], |r| { r.get(0) })?; ensure!( fault.is_none() || fault.as_deref() == Some(FAULT), "different fault" ); let decisions = verify_local_history( &a.db, &c.db, &h.db, &agent, &settings.binding, fault.as_deref(), )?; ensure!( serde_json::Value::Array(decisions) == witness["decisions"], "unreviewed current history" ); if let Some(review) = &review { review.timely(chrono::Utc::now())?; println!( "valid {} review for the verified local history", if echo { "simulated Echo-only" } else { "human" } ); } println!( "verified contracted history; fault={}", fault.as_deref().unwrap_or("none") ); } Ok(()) } } fn main() -> anyhow::Result<()> { #[cfg(windows)] { windows::run() } #[cfg(not(windows))] { anyhow::bail!("native Windows operator executable") } }