Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added doas functionality #1270

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ pub struct Config {
pub mode: Mode,
pub aur_filter: bool,
pub interactive: bool,

#[default = 7]
pub completion_interval: u64,

Expand Down Expand Up @@ -536,6 +535,9 @@ pub struct Config {
pub ignore: Vec<String>,
pub ignore_group: Vec<String>,
pub ignore_devel_source: Vec<String>,
#[default = "doas"]
pub doas_bin: String,
pub doas_flags: Vec<String>,
#[default(GlobSet::empty())]
pub ignore_devel: GlobSet,
#[default(GlobSetBuilder::new())]
Expand Down Expand Up @@ -1005,13 +1007,15 @@ impl Config {
"Pkgctl" => self.pkgctl_bin = value,
"Gpg" => self.gpg_bin = value,
"Sudo" => self.sudo_bin = value,
"Doas" => self.doas_bin = value,
"Pager" => self.pager_cmd = Some(value),
"Bat" => self.bat_bin = value,
"FileManager" => self.fm = Some(value),
"MFlags" => self.mflags.extend(split),
"GitFlags" => self.git_flags.extend(split),
"GpgFlags" => self.gpg_flags.extend(split),
"SudoFlags" => self.sudo_flags.extend(split),
"DoasFlags" => self.doas_flags.extend(split),
"BatFlags" => self.bat_flags.extend(split),
"FileManagerFlags" => self.fm_flags.extend(split),
"ChrootFlags" => self.chroot_flags.extend(split),
Expand Down
69 changes: 43 additions & 26 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ fn command_err(cmd: &Command) -> String {
)
}

pub fn get_privileged_command() -> String {
if Command::new("doas").output().is_ok() {
"doas".to_string()
} else {
"sudo".to_string()
}
}

pub fn spawn_privileged_command(flags: Vec<String>) -> Result<()> {
let privileged_cmd = get_privileged_command();
update_privileged_command(&privileged_cmd, &flags)?;
thread::spawn(move || privileged_command_loop(&privileged_cmd, &flags));
Ok(())
}

fn privileged_command_loop<S: AsRef<OsStr>>(cmd: &str, flags: &[S]) -> Result<()> {
loop {
thread::sleep(Duration::from_secs(250));
update_privileged_command(cmd, flags)?;
}
}

fn update_privileged_command<S: AsRef<OsStr>>(cmd: &str, flags: &[S]) -> Result<()> {
let mut cmd = Command::new(cmd);
cmd.args(flags);
let status = command_status(&mut cmd)?;
status.success()?;
Ok(())
}


fn command_status(cmd: &mut Command) -> Result<Status> {
debug!("running command: {:?}", cmd);
let term = &*CAUGHT_SIGNAL;
Expand Down Expand Up @@ -141,27 +172,6 @@ pub fn command_output(cmd: &mut Command) -> Result<Output> {
Ok(ret)
}

pub fn spawn_sudo(sudo: String, flags: Vec<String>) -> Result<()> {
update_sudo(&sudo, &flags)?;
thread::spawn(move || sudo_loop(&sudo, &flags));
Ok(())
}

fn sudo_loop<S: AsRef<OsStr>>(sudo: &str, flags: &[S]) -> Result<()> {
loop {
thread::sleep(Duration::from_secs(250));
update_sudo(sudo, flags)?;
}
}

fn update_sudo<S: AsRef<OsStr>>(sudo: &str, flags: &[S]) -> Result<()> {
let mut cmd = Command::new(sudo);
cmd.args(flags);
let status = command_status(&mut cmd)?;
status.success()?;
Ok(())
}

fn wait_for_lock(config: &Config) {
let path = Path::new(config.alpm.dbpath()).join("db.lck");
let c = config.color;
Expand All @@ -180,14 +190,21 @@ fn wait_for_lock(config: &Config) {
}

fn new_pacman<S: AsRef<str> + Display + Debug>(config: &Config, args: &Args<S>) -> Command {
let mut cmd = if config.need_root {
wait_for_lock(config);
let mut cmd = if config.need_root {
wait_for_lock(config);
let cmd = if Path::new("/usr/bin/doas").exists() {
let mut cmd = Command::new(&config.doas_bin);
cmd.args(&config.doas_flags).arg(args.bin.as_ref());
cmd
} else {
let mut cmd = Command::new(&config.sudo_bin);
cmd.args(&config.sudo_flags).arg(args.bin.as_ref());
cmd
} else {
Command::new(args.bin.as_ref())
};
};
cmd
} else {
Command::new(args.bin.as_ref())
};

if let Some(config) = &config.pacman_conf {
cmd.args(["--config", config]);
Expand Down
5 changes: 4 additions & 1 deletion src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{args, exec, news, print_error, printtr, repo};
use alpm::{Alpm, Depend, Version};
use alpm_utils::depends::{satisfies, satisfies_nover, satisfies_provide, satisfies_provide_nover};
use alpm_utils::{DbListExt, Targ};

use ansi_term::Style;
use anyhow::{bail, ensure, Context, Result};
use aur_depends::{Actions, Base, Conflict, DepMissing, RepoPackage};
Expand Down Expand Up @@ -141,7 +142,9 @@ impl Installer {
if !config.sudo_loop.is_empty() {
let mut flags = config.sudo_flags.clone();
flags.extend(config.sudo_loop.clone());
exec::spawn_sudo(config.sudo_bin.clone(), flags)?;
let mut privilege_flags = vec![config.sudo_bin.clone()];
privilege_flags.extend(flags);
exec::spawn_privileged_command(privilege_flags)?;
}
Ok(())
}
Expand Down