From 21b208bff9a6958fe6bdeb828c75ac3cbe14aad7 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 27 Oct 2018 12:49:57 +0200 Subject: [PATCH] error messages with more context --- src/main.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5455bd0..332f58a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,7 @@ struct Passwd pw_shell : String, } + fn initgroups(user : &str, group : libc::gid_t) -> std::io::Result<()> { let userarg = CString::new(user); @@ -262,7 +263,7 @@ fn create_execv_args(entry : & Entry, cmdargs : &Vec) -> Vec<* const lib } else { - args = (&entry.args).split_whitespace().map(to_cstring).collect(); + args = entry.args.as_str().split_whitespace().map(to_cstring).collect(); } if ! &entry.argv0.is_empty() { @@ -281,6 +282,9 @@ fn exec(entryname : &str, cmdargs : &Vec) -> std::io::Result<()> let mut filepath : String = String::from("/etc/raou.d/"); filepath = filepath + entryname; + if ! std::path::Path::new(&filepath).exists() { + return Err(std::io::Error::new(ErrorKind::NotFound, "The entry ".to_owned() + &filepath + " does not exist")); + } let entry : Entry = create_entry_from_file(&filepath)?; let destuserpasswd : Passwd = getpwnam(&entry.dest_user)?; let currentuser : u32 = geteuid(); @@ -291,14 +295,13 @@ fn exec(entryname : &str, cmdargs : &Vec) -> std::io::Result<()> ensure_allowed(currentuser, &entry)?; - become_user(&destuserpasswd)?; - setup_environment(&destuserpasswd, &entry.inherit_envs)?; - init_sandbox(&entry)?; + become_user(&destuserpasswd).or_else(|e| return Err(Error::new(ErrorKind::PermissionDenied, "Failed to switch user: ".to_owned() + &e.to_string())))?; + setup_environment(&destuserpasswd, &entry.inherit_envs).or_else(|e| return Err(Error::new(ErrorKind::Other, "Environment setup failure: ".to_owned() + &e.to_string())))?; + init_sandbox(&entry).or_else(|e| return Err(Error::new(ErrorKind::Other, "Sandbox init failure: ".to_owned() + &e.to_string())))?; - unsafe - { - errnowrapper(libc::execv(to_cstring(entry.cmd), args.as_ptr()))?; - } + unsafe { + errnowrapper(libc::execv(to_cstring(entry.cmd), args.as_ptr())).or_else(|e| return Err(Error::new(ErrorKind::Other, "execv failed: ".to_owned() + &e.to_string())))?; + } std::process::exit(0); Ok(()) } @@ -308,6 +311,7 @@ fn main() -> Result<(), std::io::Error> { let cmdargs : Vec = argv.collect(); let entryname = cmdargs.get(1); if entryname.is_some() { + match exec(&entryname.unwrap(), &cmdargs) { Err(e) => { eprintln!("The following error ocurred:");