bug fix and refactor

main
elfeiin 2024-02-08 20:54:57 -08:00
parent 2a4d6c9f30
commit ab96d1babf
Signed by: elfein
GPG Key ID: A53FDD4FD091A276
3 changed files with 14 additions and 27 deletions

View File

@ -16,17 +16,11 @@ pub fn clear_auth_cache(alias: &str, ppid: u32) -> Result<(), std::io::Error> {
let ppid_str = format!["{ppid}"];
let mut path = PathBuf::from(SESSION_CACHE_PATH);
path.push(&ppid_str);
if let Err(e) = std::fs::remove_file(&path) {
println!["Failed to remove session file: {e}"];
std::process::exit(1);
}
std::fs::remove_file(&path)?;
let mut path = PathBuf::from(AUTH_CACHE_PATH);
path.push(ppid_str);
path.push(alias);
if let Err(e) = std::fs::remove_dir_all(&path) {
println!["Failed to remove session dir: {e}"];
std::process::exit(1);
}
std::fs::remove_dir_all(&path)?;
Ok(())
}

View File

@ -9,12 +9,11 @@ use libc::{close, getpwuid};
use rudoers::check_rudoers;
use users::{get_group_by_name, get_user_by_name};
mod authentication;
use crate::rudoers::make_example_rudoers_file;
use authentication::check_auth;
use std::ffi::CStr;
use user_info::get_command_path;
use crate::rudoers::make_example_rudoers_file;
/// RUDOLPH THE RED NOSE REINDEER
/// HAD A VERY SHINY NOSE
/// AND IF YOU EVER SAW IT
@ -106,6 +105,7 @@ pub fn run() -> Result<(), String> {
}
if app.gen_config {
println!["{}", make_example_rudoers_file()];
return Ok(());
}
let alias = match &app.user {
Some(x) => &x,
@ -120,8 +120,7 @@ pub fn run() -> Result<(), String> {
let failed_to_update = String::from("Error: Rudo: Failed to update auth cache: ");
if !app.no_update && !app.remove_timestamp {
if let Err(e) = update_auth_cache(alias, parent_id) {
eprintln!["{failed_to_update}{e}"];
std::process::exit(1);
return Err(format!["{failed_to_update}{e}"]);
}
}
if app.validate {
@ -129,8 +128,7 @@ pub fn run() -> Result<(), String> {
}
if app.remove_timestamp {
if let Err(e) = clear_auth_cache(alias, parent_id) {
eprintln!["{failed_to_update}{e}"];
std::process::exit(1);
return Err(format!["{failed_to_update}{e}"]);
}
if app.cmd.is_empty() {
return Ok(());
@ -138,12 +136,10 @@ pub fn run() -> Result<(), String> {
}
if app.reset_timestamp {
if let Err(e) = clear_auth_cache(alias, parent_id) {
eprintln!["{failed_to_update}{e}"];
std::process::exit(1);
return Err(format!["{failed_to_update}{e}"]);
}
if let Err(e) = update_auth_cache(alias, parent_id) {
eprintln!["{failed_to_update}{e}"];
std::process::exit(1);
return Err(format!["{failed_to_update}{e}"]);
}
if app.cmd.is_empty() {
return Ok(());

View File

@ -38,27 +38,24 @@ pub struct Rudoers {
groups: BTreeMap<String, Entry>,
}
fn parse_rudoers() -> Rudoers {
fn parse_rudoers() -> Result<Rudoers, String> {
let mut rudoers = match File::open(DEFAULT_RUDOERS) {
Ok(file) => file,
Err(e) => {
eprintln!["Cannot open {DEFAULT_RUDOERS}: {e}"];
std::process::exit(1);
return Err(format!["Cannot open {DEFAULT_RUDOERS}: {e}"]);
}
};
let mut contents = String::new();
match rudoers.read_to_string(&mut contents) {
Ok(_) => (),
Err(e) => {
eprintln!["Could not read {DEFAULT_RUDOERS}: {e}"];
std::process::exit(1);
return Err(format!["Could not read {DEFAULT_RUDOERS}: {e}"]);
}
};
match toml::from_str(&contents) {
Ok(rudoers) => rudoers,
Ok(rudoers) => Ok(rudoers),
Err(e) => {
eprintln!["Failed to parse {DEFAULT_RUDOERS}: {e}"];
std::process::exit(1);
return Err(format!["Failed to parse {DEFAULT_RUDOERS}: {e}"]);
}
}
}
@ -111,7 +108,7 @@ pub fn check_rudoers(args: &App) -> Result<bool, String> {
let hostname: String = get_hostname()?;
let username: String = get_username()?;
let groups: Vec<Group> = get_groups();
let rudoers = parse_rudoers();
let rudoers = parse_rudoers()?;
Ok(if let Some(entry) = rudoers.users.get(&username) {
check_entry(&entry, &hostname, &alias, &command_path.to_string_lossy())?
} else {