123 lines
5.9 KiB
Rust
123 lines
5.9 KiB
Rust
//! Offline typed public-metadata renderer for the first approved SYSTEM script.
|
|
//! It performs no file write, service operation, IPC, job or policy activation.
|
|
#[cfg(all(windows, feature = "system-helper"))]
|
|
fn main() -> anyhow::Result<()> {
|
|
use anyhow::{Result, ensure};
|
|
use rmm_agent_windows::helper_system::Settings;
|
|
use rmm_domain::jobs::{PrivilegedScriptProfile, hash};
|
|
use serde::Deserialize;
|
|
use std::io::{Read, Write};
|
|
use uuid::Uuid;
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Input {
|
|
schema_version: u32,
|
|
current_settings: Vec<u8>,
|
|
current_settings_sha256: String,
|
|
agent_binary_sha256: String,
|
|
helper_binary_sha256: String,
|
|
qualification_reference: Uuid,
|
|
native_prerequisite_result: serde_json::Value,
|
|
}
|
|
fn digest(value: &str) -> bool {
|
|
rmm_domain::agent_update::digest(value)
|
|
}
|
|
fn render(input: Input) -> Result<serde_json::Value> {
|
|
ensure!(
|
|
input.schema_version == 1
|
|
&& input.current_settings.len() <= 32768
|
|
&& hash(&input.current_settings) == input.current_settings_sha256
|
|
&& digest(&input.agent_binary_sha256)
|
|
&& digest(&input.helper_binary_sha256)
|
|
&& !input.qualification_reference.is_nil(),
|
|
"SYSTEM_PREPARE_INPUT"
|
|
);
|
|
let mut settings: Settings = serde_json::from_slice(&input.current_settings)?;
|
|
settings.validate()?;
|
|
ensure!(
|
|
!settings.enable_effects
|
|
&& !settings.allow_reboot
|
|
&& settings.allowed_services.is_empty()
|
|
&& settings
|
|
.allowed_msi_recipe_sha256
|
|
.as_ref()
|
|
.is_none_or(|v| v.is_empty())
|
|
&& settings.allowed_script_profiles.is_none()
|
|
&& settings.helper.binary_sha256 == input.helper_binary_sha256,
|
|
"SYSTEM_PREPARE_ORIGINAL_PASSIVE_SCRIPT_ONLY_SCOPE"
|
|
);
|
|
let evidence = &input.native_prerequisite_result;
|
|
ensure!(
|
|
evidence["schema_version"] == 1
|
|
&& evidence["platform"] == "windows-x86_64"
|
|
&& evidence["scope"] == "native_prerequisites_only"
|
|
&& evidence["source"] == "operator_reviewed_native_units_and_peer"
|
|
&& evidence["status"] == "passed"
|
|
&& evidence["target_job_executed"] == false
|
|
&& evidence["qualification_reference"] == input.qualification_reference.to_string()
|
|
&& evidence["agent_binary_sha256"] == input.agent_binary_sha256
|
|
&& evidence["helper_binary_sha256"] == input.helper_binary_sha256
|
|
&& evidence["validated_actions"] == serde_json::json!(["run_script"])
|
|
&& evidence["evidence_sha256"]
|
|
.as_array()
|
|
.is_some_and(|items| !items.is_empty()
|
|
&& items.len() <= 16
|
|
&& items.iter().all(|v| v.as_str().is_some_and(digest))),
|
|
"SYSTEM_PREPARE_ACTUAL_PREREQUISITE_RESULT_REQUIRED"
|
|
);
|
|
settings.schema_version = 3;
|
|
settings.agent.binary_sha256 = input.agent_binary_sha256.clone();
|
|
settings.max_runtime_seconds = 30;
|
|
settings.enable_effects = true;
|
|
settings.allow_reboot = false;
|
|
settings.allowed_services.clear();
|
|
settings.allowed_msi_recipe_sha256 = None;
|
|
settings.allowed_script_profiles = Some(vec![PrivilegedScriptProfile::FullMaintenanceV1]);
|
|
settings.validate()?;
|
|
let settings_bytes = serde_json::to_vec(&settings)?;
|
|
let pin = settings.pin()?;
|
|
let pin_bytes = serde_json::to_vec(&pin)?;
|
|
ensure!(
|
|
pin.policy_sha256 == hash(&settings_bytes),
|
|
"SYSTEM_PREPARE_CANONICAL_POLICY"
|
|
);
|
|
let native_result = serde_json::to_vec(evidence)?;
|
|
ensure!(
|
|
native_result.len() <= 65536,
|
|
"SYSTEM_PREPARE_NATIVE_RESULT_LIMIT"
|
|
);
|
|
let qualification = serde_json::json!({"schema_version":1,
|
|
"tenant_id":settings.binding.tenant_id,"asset_id":settings.binding.asset_id,
|
|
"agent_id":settings.binding.agent_id,"enrollment_generation":settings.binding.enrollment_generation,
|
|
"assignment_version":settings.binding.assignment_version,
|
|
"agent_binary_sha256":input.agent_binary_sha256,
|
|
"helper_binary_sha256":input.helper_binary_sha256,"policy_sha256":pin.policy_sha256,
|
|
"native_result_sha256":hash(&native_result),"qualification_reference":input.qualification_reference,
|
|
"actions":["run_script"]});
|
|
let qualification_bytes = serde_json::to_vec(&qualification)?;
|
|
Ok(
|
|
serde_json::json!({"schema_version":1,"source":"actual_rust_settings_codec",
|
|
"prepared_only":true,"target_mutations":false,"target_job_executed":false,
|
|
"settings_bytes":settings_bytes,"settings_sha256":hash(&settings_bytes),
|
|
"policy_sha256":pin.policy_sha256,"pin_bytes":pin_bytes,"pin_sha256":hash(&pin_bytes),
|
|
"native_result_bytes":native_result,"native_result_sha256":hash(&native_result),
|
|
"qualification_bytes":qualification_bytes,"qualification_sha256":hash(&qualification_bytes),
|
|
"allowed_actions":["run_script"],"allow_reboot":false,"allow_msi":false,"allow_scm":false}),
|
|
)
|
|
}
|
|
let mut raw = Vec::new();
|
|
std::io::stdin()
|
|
.take(256 * 1024 + 1)
|
|
.read_to_end(&mut raw)?;
|
|
ensure!(raw.len() <= 256 * 1024, "SYSTEM_PREPARE_FRAME_LIMIT");
|
|
let output = render(serde_json::from_slice(&raw)?)?;
|
|
std::io::stdout().write_all(&serde_json::to_vec(&output)?)?;
|
|
std::io::stdout().write_all(b"\n")?;
|
|
Ok(())
|
|
}
|
|
#[cfg(not(all(windows, feature = "system-helper")))]
|
|
fn main() {
|
|
eprintln!("Windows/system-helper typed renderer only; no operation executed");
|
|
std::process::exit(2);
|
|
}
|