fix: run UFW status with required privileges (#655)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Kevin Yueandgithub-actions[bot] authored and GitHub committed 2026-09-01 15:32:16 +08:00
1 parent 0e1aedce82
commit 32bac9b675
3 files changed
+43 -2

No files matched your search

Generated
+1
View File
@@ -1819,6 +1819,7 @@ dependencies = [
"tempfile",
"tokio",
"uuid",
"uzers",
"whoami",
"xmltree",
]
+1
View File
@@ -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"]
+41 -2
View File
@@ -406,9 +406,14 @@ fn detect_ufw() -> Option<UfwInfo> {
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<UfwInfo> {
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<String>)> {
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))
);
}
}