use std::{
collections::HashSet,
fs,
io,
path::{Path, PathBuf},
sync::Mutex,
};
use aho_corasick::AhoCorasick;
use anyhow::Context as _;
use toml_edit::{
DocumentMut,
Item,
Table,
Value,
};
use crate::toml_edit_tree::Node;
use sequoia_directories::Component;
use sequoia_directories::Home;
use crate::Result;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
modules: Vec<PathBuf>,
}
impl Default for Config {
fn default() -> Self {
Config {
modules: Vec::new(),
}
}
}
impl Config {
pub fn modules(&self) -> &[PathBuf] {
&self.modules
}
pub fn to_config_file(&self) -> Result<ConfigFile> {
let mut doc = DocumentMut::new();
doc.as_table_mut()
.insert("modules", Item::ArrayOfTables(Default::default()));
let modules = doc
.get_mut("modules").expect("created")
.as_array_of_tables_mut().expect("invalid type");
for file_name in self.modules.iter() {
let mut table = Table::new();
table.set(
&"path".into(),
file_name.to_str()
.ok_or_else(|| {
anyhow::anyhow!("Path ({}) is not valid UTF-8",
file_name.display())
})?
.into())?;
modules.push(table);
}
let mut config = Config::default();
apply_schema(&mut Some(&mut config), None, doc.iter(),
TOP_LEVEL_SCHEMA)?;
Ok(ConfigFile {
doc,
config: Mutex::new(Some(config.clone())),
})
}
pub fn add_module<P>(&mut self, path: P) -> &mut Self
where P: Into<PathBuf>
{
self.modules.push(path.into());
self
}
pub fn extend_modules<P>(&mut self, paths: impl Iterator<Item=P>)
-> &mut Self
where P: Into<PathBuf>
{
self.modules.extend(paths.map(|p| p.into()));
self
}
}
#[derive(Debug, Default)]
pub struct ConfigFile {
doc: DocumentMut,
config: Mutex<Option<Config>>,
}
impl Clone for ConfigFile {
fn clone(&self) -> Self {
if let Ok(config) = self.config.try_lock() {
Self {
doc: self.doc.clone(),
config: Mutex::new(config.clone()),
}
} else {
Self {
doc: self.doc.clone(),
config: Mutex::new(None),
}
}
}
}
impl ConfigFile {
const TEMPLATE: &'static str = "\
# Configuration template for sq-cryptoki <SQ-CRYPTOKI-VERSION>
<SQ-CRYPTOKI-CONFIG-PATH-HINT>
# [[module]]
# path = \"/path/to/some/pkcs11/module\"
#
# [[module]]
# path = \"/path/to/another/pkcs11/module\"
";
const TEMPLATE_PATTERNS: &'static [&'static str] = &[
"<SQ-CRYPTOKI-VERSION>",
"<SQ-CRYPTOKI-CONFIG-PATH-HINT>",
];
fn config_template(path_hint: Option<&Path>) -> Result<String> {
let ac = AhoCorasick::new(Self::TEMPLATE_PATTERNS)?;
let mut path_hint = path_hint;
let path_hint_;
if let Some(ph) = path_hint.as_ref() {
if let Some(home) = dirs::home_dir() {
let home = home.display().to_string();
if let Ok(rest) = ph.strip_prefix(&home) {
path_hint_ = PathBuf::from("$HOME").join(rest);
path_hint = Some(path_hint_.as_ref());
}
}
};
Ok(ac.replace_all(Self::TEMPLATE, &[
&env!("CARGO_PKG_VERSION").to_string(),
&if let Some(path_hint) = path_hint {
format!(
"\n\
# To use it, edit it to your liking and write it to\n\
# {}",
&path_hint.display())
} else {
"".into()
}
]))
}
pub fn default_template(home: Option<&Home>) -> Result<Self> {
Self::parse_str(&Self::config_template(home.map(Self::file_name).as_deref())?)
}
pub fn file_name(home: &Home) -> PathBuf {
home.config_dir(Component::Keystore)
.join("cryptoki")
.join("config.toml")
}
pub fn parse_default_home() -> Result<Self> {
if let Some(home) = sequoia_directories::Home::default() {
Self::parse_home(home)
} else {
Err(anyhow::anyhow!("Platform does not have a default home directory"))
}
}
pub fn parse_home(home: &Home) -> Result<Self> {
Self::parse_file(&Self::file_name(home))
}
pub fn parse_file(path: &Path) -> Result<Self> {
let config = match fs::read_to_string(&path) {
Ok(r) => r,
Err(e) if e.kind() == io::ErrorKind::NotFound =>
Self::config_template(Some(path))?,
Err(e) => return Err(anyhow::Error::from(e).context(
format!("Reading configuration file {} failed",
path.display()))),
};
Self::parse_str(&config)
.with_context(|| {
format!("Reading configuration file {}", path.display())
})
}
pub fn parse_str(config: &str) -> Result<Self> {
let doc: DocumentMut = config.parse()
.context("Parsing configuration failed")?;
let mut config = Config::default();
apply_schema(&mut Some(&mut config), None, doc.iter(), TOP_LEVEL_SCHEMA)
.context("Parsing configuration failed")?;
Ok(Self {
doc,
config: Mutex::new(Some(config)),
})
}
pub fn persist_to_home(&self, home: &Home) -> Result<()> {
let path = Self::file_name(home);
self.persist(path)
}
pub fn persist<P>(&self, path: P) -> Result<()>
where P: AsRef<Path>
{
let path = path.as_ref();
let dir = path.parent().unwrap();
fs::create_dir_all(dir)?;
let mut t = tempfile::NamedTempFile::new_in(dir)?;
self.dump(&mut t)?;
t.persist(path)?;
Ok(())
}
pub fn dump(&self, sink: &mut dyn io::Write) -> Result<()> {
write!(sink, "{}", self.doc.to_string())?;
Ok(())
}
pub fn to_config(&self) -> Result<Config> {
let config = self.config.lock().unwrap();
if let Some(config) = config.as_ref() {
Ok(config.clone())
} else {
let mut c = Default::default();
apply_schema(&mut Some(&mut c), None, self.doc.iter(),
TOP_LEVEL_SCHEMA)?;
Ok(c)
}
}
pub fn as_item(&self) -> &Item {
self.doc.as_item()
}
pub fn as_item_mut(&mut self) -> &mut Item {
*self.config.lock().unwrap() = None;
self.doc.as_item_mut()
}
}
fn apply_schema<'toml>(config: &mut Option<&mut Config>,
path: Option<&str>,
section: toml_edit::Iter<'toml>,
schema: Schema) -> Result<()> {
let section = section.collect::<Vec<_>>();
let known_keys: Vec<_> =
schema.iter().map(|(key, _)| *key).collect();
debug_assert!(known_keys.iter().all(|&s| &s.to_lowercase() == s),
"keys in schema must be lowercase");
debug_assert!(known_keys.windows(2).all(|v| v[0] <= v[1]),
"keys in schema must be sorted");
let prefix = if let Some(path) = path {
format!("{}.", path)
} else {
"".to_string()
};
let keys: HashSet<&str> = section
.iter().map(|(key, _value)| *key)
.collect();
let mut allowed_keys: Vec<&str> = known_keys.to_vec();
if let Some(ignore) = section.iter()
.find_map(|(k, v)| (*k == "ignore_invalid").then_some(*v))
{
allowed_keys.push("ignore_invalid");
match ignore {
Item::Value(Value::String(k)) =>
allowed_keys.push(k.value().as_str()),
Item::Value(Value::Array(ks)) => {
for k in ks {
if let Value::String(k) = k {
allowed_keys.push(k.value().as_str());
} else {
Err(Error::ParseError(format!(
"'{}ignore_invalid' takes a string \
or an array of strings",
prefix)))?
}
}
}
_ => {
return Err(Error::ParseError(format!(
"Invalid value for '{}ignore_invalid': {}, \
expected a string or an array of strings",
prefix, ignore)).into());
}
}
}
let unknown_keys = keys
.difference(&allowed_keys.into_iter().collect())
.map(|s| *s)
.collect::<Vec<_>>();
if ! unknown_keys.is_empty() {
return Err(Error::ParseError(format!(
"{} has unknown keys: {}, valid keys are: {}",
if let Some(path) = path {
path
} else {
"top-level section"
},
unknown_keys.join(", "),
known_keys.join(", "))).into());
}
for (key, value) in §ion {
if let Ok(i) = schema.binary_search_by_key(key, |(k, _)| k) {
let apply = schema[i].1;
(apply)(config, &format!("{}{}", prefix, key), value)
.with_context(|| format!("Error validating {:?}", key))?;
}
}
Ok(())
}
#[non_exhaustive]
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum Error {
#[error("Parse error: {0}")]
ParseError(String),
#[error("Configuration item {0:?} is not a {1} but a {2}")]
BadType(String, &'static str, &'static str),
}
impl Error {
fn bad_item_type(path: &str, i: &Item, want_type: &'static str)
-> anyhow::Error
{
Error::BadType(path.into(), want_type, i.type_name()).into()
}
#[allow(unused)]
fn bad_value_type(path: &str, v: &Value, want_type: &'static str)
-> anyhow::Error
{
Error::BadType(path.into(), want_type, v.type_name()).into()
}
}
type Applicator = fn(&mut Option<&mut Config>, &str, &Item)
-> Result<()>;
#[allow(unused)]
fn apply_nop(_: &mut Option<&mut Config>,
_: &str, _: &Item)
-> Result<()>
{
Ok(())
}
type Schema = &'static [(&'static str, Applicator)];
const TOP_LEVEL_SCHEMA: Schema = &[
("modules", apply_modules),
];
const MODULES_SCHEMA: Schema = &[
("path", apply_modules_path),
];
fn apply_modules(config: &mut Option<&mut Config>,
path: &str, item: &Item)
-> Result<()>
{
let tables = item.as_array_of_tables()
.ok_or_else(|| Error::bad_item_type(path, item, "array of tables"))?;
for (i, table) in tables.iter().enumerate() {
let path = format!("{}.{}", path, i);
apply_schema(config, Some(&path), table.iter(), MODULES_SCHEMA)?;
}
Ok(())
}
fn apply_modules_path(config: &mut Option<&mut Config>,
path: &str, item: &Item)
-> Result<()>
{
let path = item.as_str()
.ok_or_else(|| Error::bad_item_type(path, item, "string"))?;
if let Some(config) = config {
config.modules.push(path.into());
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use toml_edit::Array;
use toml_edit::Formatted;
#[test]
fn default_template_is_default() {
let default_template
= ConfigFile::default_template(None).expect("invalid template")
.to_config().expect("invalid template");
assert_eq!(Config::default(), default_template);
}
#[test]
fn config_file() {
let modules = [ "/some/pkcs11.dll", "/other/pkcs11.dll" ];
let base = modules.iter()
.map(|module| {
format!("\
[[modules]]
path = \"{}\"", module)
})
.collect::<Vec<String>>()
.join("\n");
let config = ConfigFile::parse_str(&base)
.expect("invalid template")
.to_config()
.expect("invalid template");
let modules = modules
.into_iter()
.map(PathBuf::from).collect::<Vec<PathBuf>>();
assert_eq!(&config.modules(), &modules);
let invalid = format!("{}
foo = \"blah\"", base);
assert!(ConfigFile::parse_str(&invalid).is_err());
let ignore_invalid = format!("{}
ignore_invalid = [ \"foo\", \"nonexistent\" ]", invalid);
let config = ConfigFile::parse_str(&ignore_invalid)
.expect("invalid template")
.to_config()
.expect("invalid template");
assert_eq!(&config.modules(), &modules);
}
#[test]
fn modify_config_file() {
let modules = [ "/some/pkcs11.dll", "/other/pkcs11.dll" ];
let base = modules.iter()
.map(|module| {
format!("\
[[modules]]
path = \"{}\"", module)
})
.collect::<Vec<String>>()
.join("\n");
let mut config_file = ConfigFile::parse_str(&base)
.expect("invalid template");
let config = config_file
.to_config()
.expect("invalid template");
let modules = modules
.into_iter()
.map(PathBuf::from).collect::<Vec<PathBuf>>();
assert_eq!(&config.modules(), &modules);
let doc = config_file.as_item_mut();
doc.set(&"foo".into(), Value::String(Formatted::new("bar".to_string())))
.expect("couldn't set");
assert!(config_file.to_config().is_err());
let doc = config_file.as_item_mut();
let mut ignore_invalid = Array::new();
ignore_invalid.push("foo");
doc.set(&"ignore_invalid".into(), Value::Array(ignore_invalid))
.expect("couldn't set");
let config = config_file
.to_config()
.expect("invalid template");
let modules = modules
.into_iter()
.map(PathBuf::from).collect::<Vec<PathBuf>>();
assert_eq!(&config.modules(), &modules);
}
}