fix: privilege escalation for debian (#25)

This commit is contained in:
steven-omaha
2023-05-17 14:23:08 +02:00
committed by GitHub
parent a1cbd9184c
commit 40fb1066e8
3 changed files with 56 additions and 2 deletions
+3 -1
View File
@@ -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"]
@@ -51,7 +51,7 @@ impl Backend for Debian {
}
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
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<ExitStatus> {
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<ExitStatus> {
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
}