diff --git a/Cargo.lock b/Cargo.lock index 7e7a095..43e708f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2213,7 +2213,7 @@ dependencies = [ [[package]] name = "rmm-agent-windows" -version = "0.1.12" +version = "0.1.13" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/crates/agent-windows/Cargo.toml b/crates/agent-windows/Cargo.toml index 31115f6..53d1542 100644 --- a/crates/agent-windows/Cargo.toml +++ b/crates/agent-windows/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rmm-agent-windows" -version = "0.1.12" +version = "0.1.13" edition.workspace = true license.workspace = true diff --git a/crates/agent-windows/src/bin/rmm-updater-windows.rs b/crates/agent-windows/src/bin/rmm-updater-windows.rs index 0b2d681..6b81899 100644 --- a/crates/agent-windows/src/bin/rmm-updater-windows.rs +++ b/crates/agent-windows/src/bin/rmm-updater-windows.rs @@ -15,6 +15,10 @@ fn main() -> anyhow::Result<()> { println!("managed-script-helper/1"); Ok(()) } + Some(flag) if flag == "--managed-script-policy-version" && args.next().is_none() { + println!("release-preserves-script-policy/1"); + Ok(()) + } Some(flag) if flag == "--verify-bootstrap" => { let release = rmm_updater::verify_bootstrap_files( rmm_agent_windows::release_platform::VERIFY, diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index d7bccb5..9ef85cf 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -92,6 +92,20 @@ fn rotation_file_id(path: &Path) -> Result<[u32; 3]> { ]) } impl Config { + fn allows_enrolled_action(&self, action: &Action, identity: &str) -> bool { + if self.localservice_time_policy.is_none() { + return true; + } + let Action::RunScript { interpreter, windows_execution_profile, .. } = action else { + return false; + }; + interpreter == "powershell" + && windows_execution_profile.is_none_or(|p| self.profile_enabled(p)) + && action.valid_execution_identity(identity) + && (identity == "agent_service" || (identity == "system" + && ((self.managed_script_helper && action.full_maintenance()) + || (self.windows_update_helper && action.windows_update_or_identity_probe())))) + } fn profile_enabled(&self, profile: rmm_domain::jobs::WindowsExecutionProfile) -> bool { self.enable_jobs && (self.job_binding_file.is_some() || self.localservice_time_policy.is_some()) @@ -1409,15 +1423,7 @@ impl Agent { Ok(()) } fn supported_job(&self, job: &rmm_domain::jobs::JobPayload) -> bool { - if self.config.localservice_time_policy.is_some() - && !matches!(job.action, rmm_domain::jobs::Action::RunScript { .. }) - { - return false; - } - if self.config.localservice_time_policy.is_some() && job.execution_identity != "agent_service" - && !(job.execution_identity == "system" - && (self.config.windows_update_helper && job.action.windows_update_or_identity_probe() - || self.config.managed_script_helper && job.action.full_maintenance())) { + if !self.config.allows_enrolled_action(&job.action, &job.execution_identity) { return false; } if self.config.windows_echo_probe_only @@ -2664,9 +2670,12 @@ impl Agent { // A transiently stale clock skips this tick; it must not end the session. if let Ok(now)=self.clock.now() && let Some(offer)=jobs::next_at(self.journal.connection(),now)? { let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?; - ensure!(self.scope_permits(&offer,&job),"WINDOWS_ECHO_OFFER_OUT_OF_SCOPE"); if now>=job.start_before {self.record(generation,&offer,"",jobs::no_start("expired","START_DEADLINE_EXPIRED"))?;needs_sync=true;} - else if now-chrono::Duration::seconds(2)>=job.not_before && self.ready_identity(generation,job.execution_identity=="system") { + else if !self.config.allows_enrolled_action(&job.action, &job.execution_identity) || !self.scope_permits(&offer, &job) { + // Only next_at's accepted row is known to have no reserved permit. + self.record(generation, &offer, "", jobs::no_start("failed", "LEGACY_ACTION_RETIRED"))?; + needs_sync = true; + } else if now-chrono::Duration::seconds(2)>=job.not_before && self.ready_identity(generation,job.execution_identity=="system") { #[cfg(feature="system-helper")] let prefetched=self.prefetch_update(generation,&offer,&job).await?; #[cfg(not(feature="system-helper"))] @@ -3112,6 +3121,32 @@ fn record_session_error(state: &Path, error: &anyhow::Error) { mod tests { use super::*; #[cfg(feature = "system-helper")] + #[test] + fn accepted_actions_require_the_current_local_script_scope_before_a_permit() { + let mut config: Config = serde_json::from_value(serde_json::json!({ + "server_name":"fixture", "connect_address":"127.0.0.1:443", "ca_file":"ca", + "state_directory":"state", "enrollment_token_file":"token", "transport":"https", + "localservice_time_policy":"job-time.json", "enable_jobs":true, + "windows_execution_profile":"powershell_51_remote_signed" + })).unwrap(); + let mut script: Action = serde_json::from_value(serde_json::json!({ + "action":"run_script","interpreter":"powershell","script_utf8":"Write-Output 1", + "arguments":[],"expected_stdout":null,"windows_execution_profile":"powershell_51_remote_signed" + })).unwrap(); + assert!(config.allows_enrolled_action(&script, "agent_service")); + let legacy = Action::RefreshInventory { scope: "windows.base".into() }; + assert!(!config.allows_enrolled_action(&legacy, "agent_service")); + assert!(!config.allows_enrolled_action(&script,"system")); + let Action::RunScript { ref mut privileged_script_profile, .. } = script else { unreachable!() }; + *privileged_script_profile = Some(rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1); + config.windows_update_helper = true; + assert!(!config.allows_enrolled_action(&script,"system")); + config.windows_update_helper = false; + config.managed_script_helper = true; + assert!(config.allows_enrolled_action(&script, "system")); + assert!(!config.allows_enrolled_action(&legacy, "system")); + } + #[cfg(feature = "system-helper")] #[tokio::test] async fn gate_close_retains_the_original_helper_apply_worker_without_replaying_it() { let (sender, receiver) = tokio::sync::oneshot::channel::<()>(); diff --git a/crates/server/src/bootstrap/install.ps1 b/crates/server/src/bootstrap/install.ps1 index 6b4d4c1..3e57fe5 100644 --- a/crates/server/src/bootstrap/install.ps1 +++ b/crates/server/src/bootstrap/install.ps1 @@ -152,6 +152,10 @@ function Install-ScriptHelper { $supported = & $binary --managed-script-helper-version if ($LASTEXITCODE -ne 0 -or $supported -cne 'managed-script-helper/1') { throw 'Update the agent pair and stable updater before enabling administrator scripts.' } } + $preservesPolicy = & $worker --managed-script-policy-version + if ($LASTEXITCODE -ne 0 -or $preservesPolicy -cne 'release-preserves-script-policy/1') { + throw 'Administrator scripts require the stable updater that preserves the local policy during release switches and rollbacks.' + } $id = [IO.File]::ReadAllText($identity) | ConvertFrom-Json $policy = [IO.File]::ReadAllText($timePath) | ConvertFrom-Json if ($policy.tenant_id -cne $installed.tenant_id -or $policy.job_signing_public_key_hex -cnotmatch '^[0-9a-f]{64}$') { diff --git a/crates/server/src/windows_msi_catalog.rs b/crates/server/src/windows_msi_catalog.rs index 01f9326..845ce28 100644 --- a/crates/server/src/windows_msi_catalog.rs +++ b/crates/server/src/windows_msi_catalog.rs @@ -6,8 +6,10 @@ use axum::{ extract::{Path, Query, State}, http::{HeaderMap, StatusCode}, }; +#[cfg(test)] +use rmm_domain::jobs::Action; use rmm_domain::{ - jobs::{Action, ArtifactReference, JobPayload, PlanPayload, RecipeReference, encode, hash}, + jobs::{ArtifactReference, JobPayload, PlanPayload, RecipeReference, encode, hash}, windows_execution::{MSI_PACKAGE_ID, ResolvedWindowsMsi, WindowsMsiRecipe}, }; use serde_json::{Value, json};