fix: privilege escalation for debian (#25)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user