diff --git a/Cargo.toml b/Cargo.toml index 65c96332..3dd65457 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ lalrpop-util = "0.19" regex = "1" sexp = "1.1" thiserror = "1.0" +walkdir = "2" diff --git a/src/bin/casc.rs b/src/bin/casc.rs index 58053c45..6ddad681 100644 --- a/src/bin/casc.rs +++ b/src/bin/casc.rs @@ -6,6 +6,7 @@ use selinux_cascade::error::ErrorItem; use clap::Parser; use std::fs::File; use std::io::{Error, ErrorKind, Write}; +use walkdir::WalkDir; #[derive(Parser, Debug)] #[clap(author, version, name = "casc")] @@ -16,9 +17,9 @@ struct Args { fn main() -> std::io::Result<()> { let args = Args::parse(); - let policies: Vec<&str> = args.input_file.iter().map(|s| s as &str).collect(); + let policies: Vec = get_policy_files(args.input_file); let mut out_file = File::create("out.cil")?; - let res = compile_system_policy(policies); + let res = compile_system_policy(policies.iter().map(|s| s as &str).collect()); match res { Err(error_list) => { for e in error_list { @@ -35,3 +36,32 @@ fn main() -> std::io::Result<()> { Ok(s) => out_file.write_all(s.as_bytes()), } } + +// Create a list of policy files +// Display info and proceed on errors. As long as we have at least one file, errors should be +// considered non-fatal +fn get_policy_files(filenames: Vec) -> Vec { + let mut policy_files = Vec::new(); + for file in filenames { + for entry in WalkDir::new(file) { + match entry { + Ok(entry) => { + if entry.file_type().is_file() + && entry.path().extension().unwrap_or_default() == "cas" + { + let filename = entry.path().display().to_string(); + policy_files.push(filename); + } + } + Err(e) => { + if let Some(path) = e.path() { + eprintln!("{}: {}", path.display(), e); + } else { + eprintln!("{}", e); + } + } + } + } + } + policy_files +}