From 32bac9b6756e84c3561f95241beb88857be94783 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Tue, 1 Sep 2026 07:32:16 +0000 Subject: [PATCH] fix: run UFW status with required privileges (#655) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- Cargo.lock | 1 + apps/gpclient/Cargo.toml | 1 + apps/gpclient/src/hip.rs | 43 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 176aa5c..c356a94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1819,6 +1819,7 @@ dependencies = [ "tempfile", "tokio", "uuid", + "uzers", "whoami", "xmltree", ] diff --git a/apps/gpclient/Cargo.toml b/apps/gpclient/Cargo.toml index 6ff57a8..2f30f52 100644 --- a/apps/gpclient/Cargo.toml +++ b/apps/gpclient/Cargo.toml @@ -31,6 +31,7 @@ compile-time.workspace = true netdev.workspace = true xmltree.workspace = true uuid = { workspace = true, features = ["v5"] } +uzers.workspace = true [features] default = ["webview-auth"] diff --git a/apps/gpclient/src/hip.rs b/apps/gpclient/src/hip.rs index 3c9ddd2..286d571 100644 --- a/apps/gpclient/src/hip.rs +++ b/apps/gpclient/src/hip.rs @@ -406,9 +406,14 @@ fn detect_ufw() -> Option { let version_str = String::from_utf8(version_out.stdout).ok()?; let version = parse_ufw_version(&version_str)?; - let status_out = Command::new("ufw").arg("status").env("LC_ALL", "C").output().ok()?; + let is_root = uzers::get_effective_uid() == 0; + let status_out = ufw_status_command(is_root).output().ok()?; if !status_out.status.success() { - log::warn!("ufw status failed"); + if is_root { + log::warn!("ufw status failed while running as root"); + } else { + log::warn!("sudo -n ufw status failed. You may need to configure sudoers to allow execution without a password."); + } return None; } @@ -418,6 +423,19 @@ fn detect_ufw() -> Option { Some(UfwInfo { version, is_enabled }) } +fn ufw_status_command(is_root: bool) -> Command { + let mut command = if is_root { + Command::new("ufw") + } else { + let mut command = Command::new("sudo"); + command.args(["-n", "ufw"]); + command + }; + + command.arg("status").env("LC_ALL", "C"); + command +} + fn parse_clamav_version(output: &str) -> Option<(String, Option)> { let mut output_parts = output.split_whitespace(); if output_parts.next()? != "ClamAV" { @@ -557,6 +575,7 @@ fn derive_windows_network_name(host_id: &str, iface: &NetworkInterface) -> Strin mod tests { use super::*; use gpapi::os_profile::runtime_client_os; + use std::ffi::OsStr; fn make_hip_args(client_os: Os) -> HipArgs { HipArgs { @@ -722,4 +741,24 @@ mod tests { assert_eq!(parse_ufw_status("Status: inactive\n"), Some(false)); assert_eq!(parse_ufw_status("Status: unknown\n"), None); } + + #[test] + fn runs_ufw_status_directly_as_root() { + let command = ufw_status_command(true); + + assert_eq!(command.get_program(), OsStr::new("ufw")); + assert!(command.get_args().eq(["status"].into_iter().map(OsStr::new))); + } + + #[test] + fn runs_ufw_status_through_non_interactive_sudo_as_non_root() { + let command = ufw_status_command(false); + + assert_eq!(command.get_program(), OsStr::new("sudo")); + assert!( + command + .get_args() + .eq(["-n", "ufw", "status"].into_iter().map(OsStr::new)) + ); + } }