Skip to content

Commit

Permalink
update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
LeChatP committed Mar 14, 2024
1 parent cd5777e commit 2c2d610
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/sr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,19 @@ fn add_dashes() -> Vec<String> {
args
}

const CAPABILITIES_ERROR : &str = "You need at least dac_override, setpcap, setuid capabilities to run sr";
fn cap_effective_error(caplist : &str) -> String {
format!("Unable to toggle {} privilege. {}",caplist, CAPABILITIES_ERROR)
}

fn main() {
subsribe();

let args = add_dashes();
let args = Cli::parse_from(args.iter());
read_effective(true).expect("Failed to read_effective");
read_effective(true).or(dac_override_effective(true)).expect(&cap_effective_error("dac_read_search or dac_override"));
let config = load_config(&FILENAME).expect("Failed to load config file");
read_effective(false).expect("Failed to read_effective");
read_effective(false).or(dac_override_effective(false)).expect(&cap_effective_error("dac_read_search or dac_override"));
debug!("loaded config : {:#?}", config);
let user = User::from_uid(getuid())
.expect("Failed to get user")
Expand Down Expand Up @@ -246,7 +252,7 @@ fn main() {
ppid,
};

dac_override_effective(true).expect("Failed to dac_override_effective");
dac_override_effective(true).expect(&cap_effective_error("dac_override"));
let is_valid = timeout::is_valid(&user, &user, &config.as_ref().borrow().timestamp);
debug!("need to re-authenticate : {}", !is_valid);
if !is_valid {
Expand All @@ -257,7 +263,7 @@ fn main() {
}

Check warning on line 263 in src/sr/main.rs

View workflow job for this annotation

GitHub Actions / clippy

use of `expect` followed by a function call

warning: use of `expect` followed by a function call --> src/sr/main.rs:263:34 | 263 | dac_override_effective(true).expect(&cap_effective_error("dac_override")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| { panic!("{}", cap_effective_error("dac_override")) })` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
timeout::update_cookie(&user, &user, &config.as_ref().borrow().timestamp)
.expect("Failed to add cookie");
dac_override_effective(false).expect("Failed to dac_override_effective");
dac_override_effective(false).expect(&cap_effective_error("dac_override"));
let matching = match args.role {
None => match config.matches(&user, &args.command) {
Err(err) => {
Expand Down Expand Up @@ -333,14 +339,14 @@ fn main() {
.collect::<Vec<_>>()
});

setuid_effective(true).expect("Failed to setuid_effective");
setuid_effective(true).expect(&cap_effective_error("setuid"));
capctl::cap_set_ids(uid, gid, groups.as_ref().map(|g| g.as_slice()))
.expect("Failed to set ids");
setuid_effective(false).expect("Failed to setuid_effective");
setuid_effective(false).expect(&cap_effective_error("setuid"));

//set capabilities
if let Some(caps) = matching.caps() {
setpcap_effective(true).expect("Failed to setpcap_effective");
setpcap_effective(true).expect(CAPABILITIES_ERROR);
let mut capstate = CapState::empty();

Check warning on line 350 in src/sr/main.rs

View workflow job for this annotation

GitHub Actions / clippy

use of `expect` followed by a function call

warning: use of `expect` followed by a function call --> src/sr/main.rs:350:28 | 350 | setuid_effective(true).expect(&cap_effective_error("setuid")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| { panic!("{}", cap_effective_error("setuid")) })` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
if optstack.get_bounding().1 {

Check warning on line 351 in src/sr/main.rs

View workflow job for this annotation

GitHub Actions / clippy

called `.as_ref().map(|g| g.as_slice())` on an `Option` value

warning: called `.as_ref().map(|g| g.as_slice())` on an `Option` value --> src/sr/main.rs:351:35 | 351 | capctl::cap_set_ids(uid, gid, groups.as_ref().map(|g| g.as_slice())) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `groups.as_deref()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref = note: `#[warn(clippy::option_as_ref_deref)]` on by default
for cap in caps.not().iter() {
Expand All @@ -353,15 +359,15 @@ fn main() {
for cap in caps.iter() {
capctl::ambient::raise(cap).expect("Failed to set ambiant cap");
}
setpcap_effective(false).expect("Failed to setpcap_effective");
setpcap_effective(false).expect(&cap_effective_error("setpcap"));
} else {
setpcap_effective(true).expect("Failed to setpcap_effective");
setpcap_effective(true).expect(CAPABILITIES_ERROR);
if optstack.get_bounding().1 {
capctl::bounding::clear().expect("Failed to clear bounding cap");
capctl::bounding::clear().expect(&cap_effective_error("setpcap"));
}
let capstate = CapState::empty();
capstate.set_current().expect("Failed to set current cap");
setpcap_effective(false).expect("Failed to setpcap_effective");
setpcap_effective(false).expect(&cap_effective_error("setpcap"));

Check warning on line 370 in src/sr/main.rs

View workflow job for this annotation

GitHub Actions / clippy

use of `expect` followed by a function call

warning: use of `expect` followed by a function call --> src/sr/main.rs:370:34 | 370 | setpcap_effective(false).expect(&cap_effective_error("setpcap")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| { panic!("{}", cap_effective_error("setpcap")) })` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
}

//execute command
Expand Down

0 comments on commit 2c2d610

Please sign in to comment.