error messages with more context

This commit is contained in:
Albert S. 2018-10-27 12:49:57 +02:00
parent 841e1eadc5
commit 21b208bff9

View File

@ -52,6 +52,7 @@ struct Passwd
pw_shell : String, pw_shell : String,
} }
fn initgroups(user : &str, group : libc::gid_t) -> std::io::Result<()> fn initgroups(user : &str, group : libc::gid_t) -> std::io::Result<()>
{ {
let userarg = CString::new(user); let userarg = CString::new(user);
@ -262,7 +263,7 @@ fn create_execv_args(entry : & Entry, cmdargs : &Vec<String>) -> Vec<* const lib
} }
else 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() if ! &entry.argv0.is_empty()
{ {
@ -281,6 +282,9 @@ fn exec(entryname : &str, cmdargs : &Vec<String>) -> std::io::Result<()>
let mut filepath : String = String::from("/etc/raou.d/"); let mut filepath : String = String::from("/etc/raou.d/");
filepath = filepath + entryname; 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 entry : Entry = create_entry_from_file(&filepath)?;
let destuserpasswd : Passwd = getpwnam(&entry.dest_user)?; let destuserpasswd : Passwd = getpwnam(&entry.dest_user)?;
let currentuser : u32 = geteuid(); let currentuser : u32 = geteuid();
@ -291,13 +295,12 @@ fn exec(entryname : &str, cmdargs : &Vec<String>) -> std::io::Result<()>
ensure_allowed(currentuser, &entry)?; ensure_allowed(currentuser, &entry)?;
become_user(&destuserpasswd)?; 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)?; 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)?; init_sandbox(&entry).or_else(|e| return Err(Error::new(ErrorKind::Other, "Sandbox init failure: ".to_owned() + &e.to_string())))?;
unsafe 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())))?;
errnowrapper(libc::execv(to_cstring(entry.cmd), args.as_ptr()))?;
} }
std::process::exit(0); std::process::exit(0);
Ok(()) Ok(())
@ -308,6 +311,7 @@ fn main() -> Result<(), std::io::Error> {
let cmdargs : Vec<String> = argv.collect(); let cmdargs : Vec<String> = argv.collect();
let entryname = cmdargs.get(1); let entryname = cmdargs.get(1);
if entryname.is_some() { if entryname.is_some() {
match exec(&entryname.unwrap(), &cmdargs) { match exec(&entryname.unwrap(), &cmdargs) {
Err(e) => { Err(e) => {
eprintln!("The following error ocurred:"); eprintln!("The following error ocurred:");