fix: check executable for file

This commit is contained in:
Kevin Yue 2024-05-10 10:26:45 -04:00
parent 159673652c
commit 92b858884c
2 changed files with 28 additions and 19 deletions

View File

@ -1,7 +1,6 @@
use is_executable::IsExecutable; use std::{io, path::Path};
use std::path::Path;
pub use is_executable::is_executable; use is_executable::IsExecutable;
const VPNC_SCRIPT_LOCATIONS: [&str; 6] = [ const VPNC_SCRIPT_LOCATIONS: [&str; 6] = [
"/usr/local/share/vpnc-scripts/vpnc-script", "/usr/local/share/vpnc-scripts/vpnc-script",
@ -39,3 +38,17 @@ pub fn find_vpnc_script() -> Option<String> {
pub fn find_csd_wrapper() -> Option<String> { pub fn find_csd_wrapper() -> Option<String> {
find_executable(&CSD_WRAPPER_LOCATIONS) find_executable(&CSD_WRAPPER_LOCATIONS)
} }
/// If file exists, check if it is executable
pub fn check_executable(file: &str) -> Result<(), io::Error> {
let path = Path::new(file);
if path.exists() && !path.is_executable() {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!("{} is not executable", file),
));
}
Ok(())
}

View File

@ -4,7 +4,7 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use common::vpn_utils::{find_vpnc_script, is_executable}; use common::vpn_utils::{check_executable, find_vpnc_script};
use log::info; use log::info;
use crate::ffi; use crate::ffi;
@ -80,23 +80,23 @@ impl Vpn {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct VpnError<'a> { pub struct VpnError {
message: &'a str, message: String,
} }
impl<'a> VpnError<'a> { impl VpnError {
fn new(message: &'a str) -> Self { fn new(message: String) -> Self {
Self { message } Self { message }
} }
} }
impl fmt::Display for VpnError<'_> { impl fmt::Display for VpnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message) write!(f, "{}", self.message)
} }
} }
impl std::error::Error for VpnError<'_> {} impl std::error::Error for VpnError {}
pub struct VpnBuilder { pub struct VpnBuilder {
server: String, server: String,
@ -159,21 +159,17 @@ impl VpnBuilder {
self self
} }
pub fn build(self) -> Result<Vpn, VpnError<'static>> { pub fn build(self) -> Result<Vpn, VpnError> {
let script = match self.script { let script = match self.script {
Some(script) => { Some(script) => {
if !is_executable(&script) { check_executable(&script).map_err(|e| VpnError::new(e.to_string()))?;
return Err(VpnError::new("vpnc script is not executable"));
}
script script
} }
None => find_vpnc_script().ok_or_else(|| VpnError::new("Failed to find vpnc-script"))?, None => find_vpnc_script().ok_or_else(|| VpnError::new(String::from("Failed to find vpnc-script")))?,
}; };
if let Some(csd_wrapper) = &self.csd_wrapper { if let Some(csd_wrapper) = &self.csd_wrapper {
if !is_executable(csd_wrapper) { check_executable(csd_wrapper).map_err(|e| VpnError::new(e.to_string()))?;
return Err(VpnError::new("CSD wrapper is not executable"));
}
} }
let user_agent = self.user_agent.unwrap_or_default(); let user_agent = self.user_agent.unwrap_or_default();