use crate::inventory::hosts::HostOSType;
use crate::tasks::FileAttributesInput;
use crate::tasks::files::Recurse;
pub fn screen_path(path: &String) -> Result<String,String> {
let path2 = path.trim().to_string();
let path3 = screen_general_input_strict(&path2)?;
return Ok(path3.to_string());
}
pub fn screen_general_input_strict(input: &String) -> Result<String,String> {
let input2 = input.trim();
let bad = vec![ ";", "{", "}", "(", ")", "<", ">", "&", "*", "|", "=", "?", "[", "]", "$", "%", "+", "`"];
for invalid in bad.iter() {
if input2.find(invalid).is_some() {
return Err(format!("illegal characters found: {} ('{}')", input2, invalid.to_string()));
}
}
return Ok(input2.to_string());
}
pub fn screen_general_input_loose(input: &String) -> Result<String,String> {
let input2 = input.trim();
let bad = vec![ ";", "<", ">", "&", "*", "?", "{", "}", "[", "]", "$", "`"];
for invalid in bad.iter() {
if input2.find(invalid).is_some() {
return Err(format!("illegal characters detected: {} ('{}')", input2, invalid.to_string()));
}
}
return Ok(input2.to_string());
}
pub fn screen_mode(mode: &String) -> Result<String,String> {
if FileAttributesInput::is_octal_string(&mode) {
return Ok(mode.clone());
} else {
return Err(format!("not an octal string: {}", mode));
}
}
pub fn get_mode_command(os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return match os_type {
HostOSType::Linux => Ok(format!("stat --format '%a' '{}'", path)),
HostOSType::MacOS => Ok(format!("stat -f '%A' '{}'", path)),
}
}
pub fn get_sha512_command(os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return match os_type {
HostOSType::Linux => Ok(format!("sha512sum '{}'", path)),
HostOSType::MacOS => Ok(format!("shasum -b -a 512 '{}'", path)),
}
}
pub fn get_ownership_command(_os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return Ok(format!("ls -ld '{}'", path));
}
pub fn get_is_directory_command(_os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return Ok(format!("ls -ld '{}'", path));
}
pub fn get_touch_command(_os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return Ok(format!("touch '{}'", path));
}
pub fn get_create_directory_command(_os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return Ok(format!("mkdir -p '{}'", path));
}
pub fn get_delete_file_command(_os_type: HostOSType, untrusted_path: &String) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
return Ok(format!("rm -f '{}'", path));
}
pub fn get_delete_directory_command(_os_type: HostOSType, untrusted_path: &String, recurse: Recurse) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
match recurse {
Recurse::No => { return Ok(format!("rm -d '{}'", path)); },
Recurse::Yes => { return Ok(format!("rm -rf '{}'", path)); }
}
}
pub fn set_owner_command(_os_type: HostOSType, untrusted_path: &String, untrusted_owner: &String, recurse: Recurse) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
let owner = screen_general_input_strict(untrusted_owner)?;
match recurse {
Recurse::No => { return Ok(format!("chown '{}' '{}'", owner, path)); },
Recurse::Yes => { return Ok(format!("chown -R '{}' '{}'", owner, path)); }
}
}
pub fn set_group_command(_os_type: HostOSType, untrusted_path: &String, untrusted_group: &String, recurse: Recurse) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
let group = screen_general_input_strict(untrusted_group)?;
match recurse {
Recurse::No => { return Ok(format!("chgrp '{}' '{}'", group, path)); },
Recurse::Yes => { return Ok(format!("chgrp -R '{}' '{}'", group, path)); }
}
}
pub fn set_mode_command(_os_type: HostOSType, untrusted_path: &String, untrusted_mode: &String, recurse: Recurse) -> Result<String,String> {
let path = screen_path(untrusted_path)?;
let mode = screen_mode(untrusted_mode)?;
match recurse {
Recurse::No => { return Ok(format!("chmod '{}' '{}'", mode, path)); },
Recurse::Yes => { return Ok(format!("chmod -R '{}' '{}'", mode, path)); }
}
}