use log::{info, warn};
use mlua::Lua;
use crate::config;
use crate::security::SDetailedAccessAllow;
use crate::security::env_modify::LuaApis;
pub fn enable_detailed_restrict_fio(lua: &Lua, filectrl: &SDetailedAccessAllow, cmdctrl: &SDetailedAccessAllow) -> mlua::Result<()> {
let mut apis = LuaApis::new();
apis.insert("os".to_string(), vec![
"execute".to_string(), "remove".to_string(), "rename".to_string(), ]);
apis.insert("io".to_string(), vec![
"open".to_string(), "close".to_string(), "read".to_string(), "write".to_string(), "lines".to_string(), "popen".to_string(), ]);
let global = lua.globals();
for (module, apis) in apis {
let module_table = global.get::<mlua::Table>(module.clone())?;
for api in apis {
let bak_function: Option<mlua::Function> = module_table.get(api.clone()).ok();
if bak_function.is_none() {
continue;
}
let bak_function = bak_function.unwrap();
let data = match (module.clone().as_str(), api.clone().as_str()) {
("os", "remove") | ("os", "rename") => filectrl.get(&api),
("os", "execute") => cmdctrl.get(&api),
("io", "popen") => cmdctrl.get(&api),
("io", _) => filectrl.get(&api),
_ => None };
let data = data.cloned().unwrap_or_default();
let api_name = format!("{}.{}", module.clone(), api.clone());
let new_func = lua.create_function(move |lua_q, args: mlua::MultiValue| -> Result<mlua::MultiValue, mlua::Error> {
let data = data.clone();
if !data.contains(match &args.clone().into_vec()[0].to_string(){
Ok(s) => s,
Err(_) => return mlua::Result::Err(mlua::Error::RuntimeError("Invalid inputs.".to_string()))
}
) { info!("Denied api: {}", api_name);
return mlua::Result::Err(mlua::Error::RuntimeError("Rejected by mint.".to_string()));
}
warn!("Allowed api: {}", api_name);
bak_function.call::<mlua::MultiValue>(args)
})?;
module_table.set(api, new_func)?; }
global.set(module, module_table)?; }
Ok(())
}