From 40fb1066e8c63d3f15e96f33eb0e8ed75073b29c Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Wed, 17 May 2023 14:23:08 +0200 Subject: [PATCH] fix: privilege escalation for debian (#25) --- Cargo.lock | 1 + crates/pacdef_core/Cargo.toml | 4 +- .../pacdef_core/src/backend/actual/debian.rs | 53 ++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01feb8b..87fdd30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -353,6 +353,7 @@ dependencies = [ "anyhow", "clap", "const_format", + "libc", "pacdef_macros", "path-absolutize", "regex", diff --git a/crates/pacdef_core/Cargo.toml b/crates/pacdef_core/Cargo.toml index 6609230..5810d24 100644 --- a/crates/pacdef_core/Cargo.toml +++ b/crates/pacdef_core/Cargo.toml @@ -31,7 +31,9 @@ pacdef_macros = { path = "../pacdef_macros", version = "0.1" } alpm = { version = "2.2", optional = true } rust-apt = { version = "0.5", optional = true } +libc = { version = "*", optional = true } # for debian + [features] default = [] arch = ["dep:alpm"] -debian = ["dep:rust-apt"] +debian = ["dep:rust-apt", "dep:libc"] diff --git a/crates/pacdef_core/src/backend/actual/debian.rs b/crates/pacdef_core/src/backend/actual/debian.rs index d62824f..eff57b8 100644 --- a/crates/pacdef_core/src/backend/actual/debian.rs +++ b/crates/pacdef_core/src/backend/actual/debian.rs @@ -51,7 +51,7 @@ impl Backend for Debian { } fn make_dependency(&self, packages: &[Package]) -> Result { - let mut cmd = Command::new("apt-mark"); + let mut cmd = build_base_command_with_privileges("apt-mark"); cmd.arg("auto"); for p in packages { cmd.arg(format!("{p}")); @@ -59,6 +59,41 @@ impl Backend for Debian { cmd.status() .with_context(|| format!("running command [{cmd:?}]")) } + + /// Install the specified packages. + fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result { + let mut cmd = build_base_command_with_privileges(self.get_binary()); + + cmd.args(self.get_switches_install()); + + if noconfirm { + cmd.args(self.get_switches_noconfirm()); + } + + for p in packages { + cmd.arg(format!("{p}")); + } + + cmd.status() + .with_context(|| format!("running command {cmd:?}")) + } + + /// Remove the specified packages. + fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result { + let mut cmd = build_base_command_with_privileges(self.get_binary()); + cmd.args(self.get_switches_remove()); + + if noconfirm { + cmd.args(self.get_switches_noconfirm()); + } + + for p in packages { + cmd.arg(format!("{p}")); + } + + cmd.status() + .with_context(|| format!("running command [{cmd:?}]")) + } } impl Debian { @@ -68,3 +103,19 @@ impl Debian { } } } + +fn we_are_root() -> bool { + let uid = unsafe { libc::geteuid() }; + uid == 0 +} + +fn build_base_command_with_privileges(binary: &str) -> Command { + let cmd = if we_are_root() { + Command::new(binary) + } else { + let mut cmd = Command::new("sudo"); + cmd.arg(binary); + cmd + }; + cmd +}