Files
rust-discovery/crates/updater-worker/src/main.rs
T

113 lines
4.8 KiB
Rust

//! Stable local service entrypoint. No network endpoint, command arguments,
//! approval API or startup replay of a previously admitted update.
#[cfg(target_os = "linux")]
mod linux {
use anyhow::{Context, Result, ensure};
use rmm_agent_core::auth::Binding;
use rmm_updater_linux::{files::Directory, ipc::Listener};
use rmm_updater_worker::engine::Runtime;
use std::{
ffi::OsStr,
path::Path,
sync::atomic::{AtomicBool, Ordering},
};
static STOP: AtomicBool = AtomicBool::new(false);
extern "C" fn stop(_: libc::c_int) {
STOP.store(true, Ordering::SeqCst);
}
fn binding() -> Result<Binding> {
let directory = Directory::open(Path::new("/etc/rmm-agent-updater"), 0, false)?;
let name = OsStr::new("binding.json");
let mut file = directory.inspect(name, 0, 32768)?;
ensure!(
file.proof().identity.mode & 0o777 == 0o600 && file.proof().identity.links == 1,
"UPDATE_DAEMON_PRIVATE_BINDING"
);
let bytes = file.bytes(32768)?;
let binding: Binding = serde_json::from_slice(&bytes)?;
let public: [u8; 32] = hex::decode(&binding.public_key_hex)?
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_DAEMON_SIGNING_PIN"))?;
ensure!(
!binding.tenant_id.is_nil()
&& !binding.asset_id.is_nil()
&& !binding.agent_id.is_nil()
&& binding.enrollment_generation > 0
&& binding.assignment_version > 0
&& !ed25519_dalek::VerifyingKey::from_bytes(&public)?.is_weak(),
"UPDATE_DAEMON_BINDING"
);
directory.file(name, file.proof(), 32768)?;
Ok(binding)
}
pub fn run() -> Result<()> {
let args: Vec<_> = std::env::args_os().skip(1).collect();
if args.first().is_some_and(|arg| arg == "--verify-bootstrap") {
let release =
rmm_updater::verify_bootstrap_files(rmm_domain::agent_update::Platform::NATIVE_LINUX.into(), &args[1..])?;
println!("{}", serde_json::to_string(release.manifest())?);
return Ok(());
}
if args == [OsStr::new("--help")] {
println!(
"rmm-agent-updater --serve\nFixed protected local binding/policy and authenticated Fullhelper IPC only. No automatic activation or startup replay. A failed/ambiguous pass stops closed; operator recovery must inspect the original handoff."
);
return Ok(());
}
ensure!(args == [OsStr::new("--serve")], "UPDATE_DAEMON_ARGUMENTS");
// SAFETY: geteuid/getegid have no pointer arguments or preconditions.
ensure!(
unsafe { libc::geteuid() } == 0 && unsafe { libc::getegid() } == 0,
"UPDATE_DAEMON_ROOT_SERVICE_REQUIRED"
);
// SAFETY: handlers only set a lock-free atomic and hold no borrowed state.
unsafe {
let mut action: libc::sigaction = std::mem::zeroed();
action.sa_sigaction = stop as *const () as usize;
libc::sigemptyset(&mut action.sa_mask);
ensure!(
libc::sigaction(libc::SIGTERM, &action, std::ptr::null_mut()) == 0
&& libc::sigaction(libc::SIGINT, &action, std::ptr::null_mut()) == 0,
"UPDATE_DAEMON_SIGNALS"
);
}
let binding = binding()?;
let mut runtime = Runtime::new(binding.clone())?;
let listener = Listener::bind()?;
while !STOP.load(Ordering::SeqCst) {
if let Err(error) = runtime.serve_one(&listener) {
// A short idle poll is normal. Other failed handshakes do not
// become an activation; after possible dispatch the runtime's
// fuse closes permanently and this process must stop.
if runtime.failed() {
anyhow::bail!("UPDATE_DAEMON_ORIGINAL_PASS_FAILED");
}
if error.to_string() != "UPDATE_IPC_NO_REQUEST" {
eprintln!("UPDATE_DAEMON_REQUEST_REJECTED");
}
}
if runtime.terminal() {
// A successful ORIGINAL finalization permits the service to
// observe its new installed record for a later, separately
// approved attempt. No old handoff is resumed by construction.
runtime = Runtime::new(binding.clone()).context("UPDATE_DAEMON_NEW_BASELINE")?;
}
}
Ok(())
}
}
#[cfg(target_os = "linux")]
fn main() {
if linux::run().is_err() {
eprintln!("UPDATE_DAEMON_CLOSED");
std::process::exit(1);
}
}
#[cfg(not(target_os = "linux"))]
fn main() {
eprintln!("Native Linux updater service only");
std::process::exit(1);
}