Skip to content

Commit

Permalink
Merge branch 'reorganisation' of github.com:LeChatP/RootAsRole into r…
Browse files Browse the repository at this point in the history
…eorganisation
  • Loading branch information
LeChatP committed Sep 9, 2024
2 parents 4182827 + 28020e3 commit e980c06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
54 changes: 42 additions & 12 deletions xtask/src/install/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ pub enum Elevated {
No,
}

pub fn install(priv_exe: &Option<String>,profile: Profile, clean_after: bool, copy: bool) -> Result<Elevated, anyhow::Error> {
pub fn install(
priv_exe: &Option<String>,
profile: Profile,
clean_after: bool,
copy: bool,
) -> Result<Elevated, anyhow::Error> {
// test if current process has CAP_DAC_OVERRIDE,CAP_CHOWN capabilities
let mut state = capctl::CapState::get_current()?;
if !state.permitted.has(Cap::DAC_OVERRIDE)
Expand All @@ -96,29 +101,54 @@ pub fn install(priv_exe: &Option<String>,profile: Profile, clean_after: bool, co
{
let bounding = capctl::bounding::probe();
// get parent process
if !bounding.has(Cap::DAC_OVERRIDE) ||
!bounding.has(Cap::CHOWN) ||
!bounding.has(Cap::SETFCAP)
if !bounding.has(Cap::DAC_OVERRIDE)
|| !bounding.has(Cap::CHOWN)
|| !bounding.has(Cap::SETFCAP)
{
return Err(anyhow!("The bounding set misses DAC_OVERRIDE, CHOWN or SETFCAP capabilities"));
return Err(anyhow!(
"The bounding set misses DAC_OVERRIDE, CHOWN or SETFCAP capabilities"
));
} else if env::var("ROOTASROLE_INSTALLER_NESTED").is_ok_and(|v| v == "1") {
env::remove_var("ROOTASROLE_INSTALLER_NESTED");
return Err(anyhow!("Unable to elevate required capabilities, is LSM blocking installation?"));
return Err(anyhow!(
"Unable to elevate required capabilities, is LSM blocking installation?"
));
}

let priv_bin = detect_priv_bin();
let priv_exe = priv_exe.as_ref().or(priv_bin.as_ref()).context("Privileged binary is required").map_err(|e|{
return anyhow::Error::msg(format!("Please run {} as an administrator.", current_exe().unwrap_or(PathBuf::from_str("the command").unwrap()).to_str().unwrap()));
})?;
let priv_exe = priv_exe
.as_ref()
.or(priv_bin.as_ref())
.context("Privileged binary is required")
.map_err(|e| {
return anyhow::Error::msg(format!(
"Please run {} as an administrator.",
current_exe()
.unwrap_or(PathBuf::from_str("the command").unwrap())
.to_str()
.unwrap()
));
})?;
env::set_var("ROOTASROLE_INSTALLER_NESTED", "1");
tracing::warn!("Elevating privileges...");
std::process::Command::new(priv_exe)
.arg(current_exe()?.to_str().context("Failed to get current exe path")?)
.arg(
current_exe()?
.to_str()
.context("Failed to get current exe path")?,
)
.arg("install")
.status()
.context("Failed to run privileged binary").map_err(|e|{
.context("Failed to run privileged binary")
.map_err(|e| {
error!("{}", e);
return anyhow::Error::msg(format!("Failed to run privileged binary. Please run {} as an administrator.", current_exe().unwrap_or(PathBuf::from_str("the command").unwrap()).to_str().unwrap()));
return anyhow::Error::msg(format!(
"Failed to run privileged binary. Please run {} as an administrator.",
current_exe()
.unwrap_or(PathBuf::from_str("the command").unwrap())
.to_str()
.unwrap()
));
})?;
return Ok(Elevated::Yes);
}
Expand Down
10 changes: 8 additions & 2 deletions xtask/src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,18 @@ pub(crate) fn install(opts: &InstallOptions) -> Result<(), anyhow::Error> {
debug!("Building sr and chsr");
build(&opts.build_opts)?;
}
if install::install(&opts.priv_bin, opts.build_opts.profile, opts.clean_after, true)?.is_yes(){
if install::install(
&opts.priv_bin,
opts.build_opts.profile,
opts.clean_after,
true,
)?
.is_yes()
{
Ok(())
} else {
configure(Some(os))
}

}

pub(crate) fn build(opts: &BuildOptions) -> Result<(), anyhow::Error> {
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/postinst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
match action {
Some(action) => match action.as_str() {
"configure" => {
let res = install::install::install(&None,install::Profile::Release, false, false);
let res = install::install::install(&None, install::Profile::Release, false, false);
if let Err(e) = res {
warn!("{:#}", e);
std::process::exit(1);
Expand Down

0 comments on commit e980c06

Please sign in to comment.