Skip to content

Commit

Permalink
* Now can escalate using root automatically if not running as root
Browse files Browse the repository at this point in the history
  • Loading branch information
CKingX committed May 17, 2022
1 parent cbb5a73 commit e583c5d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
49 changes: 30 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ddr-mount"
license = "AGPL-3.0-only"
version = "1.1.3"
version = "1.1.4"
edition = "2021"
authors = ["CKingX"]
description = "ddr-mount allows you to mount image files while passing bad sectors"
Expand All @@ -24,6 +24,7 @@ clap-verbosity-flag = "1.0.0"
unicode-width = "0.1.9"
update-informer = "0.5.0"
indexmap = {version = "1.8.1", features = ["serde"] }
sudo = "0.6.0"

[build-dependencies]
clap_complete = "3.1.3"
Expand Down
2 changes: 1 addition & 1 deletion package/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: ddr-mount
Version: 1.1.3
Version: 1.1.4
Architecture: amd64
Depends: mount (>= 2.31), dmsetup (>= 2:1.02)
Recommends: bash
Expand Down
12 changes: 5 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,13 @@ pub fn read_config_error() -> ! {
process::exit(ExitCode::ConfigError as i32);
}

pub fn check_root() {
pub fn root_error() {
let env_vars = env::vars().find(|n| n.0 == "USER");
if let Some((_, user)) = env_vars {
if user != "root" {
error!("User running as {user}, rather than root");
let arguments = env::args().reduce(|a, b| format!("{a} {b}")).unwrap();
print_error(format!("You must run as root.\nTry sudo {arguments}"));
process::exit(ExitCode::NonRoot as i32);
}
error!("User running as {user}, rather than root");
let arguments = env::args().reduce(|a, b| format!("{a} {b}")).unwrap();
print_error(format!("You must run as root.\nTry sudo {arguments}"));
process::exit(ExitCode::NonRoot as i32);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use config::list_devices;
use mount::*;

use log::info;
use sudo::escalate_if_needed;
use update_informer::{registry, Check};

fn main() {
Expand All @@ -35,19 +36,26 @@ fn main() {
map,
block_size,
} => {
error::check_root();
ensure_root();
mount(image, map, block_size);
}
Commands::Unmount { device } => {
error::check_root();
ensure_root();
unmount::unmount(device)
}
Commands::UnmountAll => {
error::check_root();
ensure_root();
unmount::unmount_all();
}
Commands::List => {
list_devices();
}
}
}

fn ensure_root() {
match escalate_if_needed() {
Ok(_) => (),
Err(_) => error::root_error(),
};
}

0 comments on commit e583c5d

Please sign in to comment.