82 lines
2.9 KiB
Rust
82 lines
2.9 KiB
Rust
//! Offline configuration + zone validation (`easydns check-config`, `econf`, `ezone`).
|
|
use dns_config::{Config, ZoneType};
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct Finding {
|
|
pub severity: &'static str,
|
|
pub location: String,
|
|
pub message: String,
|
|
}
|
|
|
|
impl std::fmt::Display for Finding {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}: {}: {}", self.severity, self.location, self.message)
|
|
}
|
|
}
|
|
|
|
/// Check one zone file: syntax, SOA/NS, CNAME conflicts, out-of-zone data, lint warnings.
|
|
pub fn check_zone_file(path: &std::path::Path, origin: &str) -> Vec<Finding> {
|
|
let mut out = Vec::new();
|
|
let loc = format!("{} ({origin})", path.display());
|
|
let name = match dns_core::names::parse_name(origin) {
|
|
Ok(n) => n,
|
|
Err(e) => {
|
|
out.push(Finding { severity: "error", location: loc, message: e.to_string() });
|
|
return out;
|
|
}
|
|
};
|
|
let records = match dns_core::zonefile::parse_file(path, &name) {
|
|
Ok(r) => r,
|
|
Err(e) => {
|
|
out.push(Finding { severity: "error", location: loc, message: e.to_string() });
|
|
return out;
|
|
}
|
|
};
|
|
match dns_authoritative::Zone::from_records(&name, records) {
|
|
Ok(z) => {
|
|
for w in dns_authoritative::zone::lint(&z) {
|
|
out.push(Finding { severity: "warning", location: loc.clone(), message: w });
|
|
}
|
|
out.push(Finding {
|
|
severity: "info",
|
|
location: loc,
|
|
message: format!("zone {} loaded: serial {}, {} records", z.origin(), z.serial(), z.record_count()),
|
|
});
|
|
}
|
|
Err(e) => out.push(Finding { severity: "error", location: loc, message: e.to_string() }),
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Validate a configuration and all primary zone files it references.
|
|
pub fn check_config(cfg: &Config) -> Vec<Finding> {
|
|
let mut out: Vec<Finding> = cfg
|
|
.validate()
|
|
.into_iter()
|
|
.map(|i| Finding { severity: if i.is_error() { "error" } else { "warning" }, location: i.path, message: i.message })
|
|
.collect();
|
|
for z in &cfg.zones {
|
|
if z.zone_type == ZoneType::Primary
|
|
&& let Some(f) = &z.file
|
|
{
|
|
let p = cfg.zone_file_path(f);
|
|
if p.is_file() {
|
|
out.extend(check_zone_file(&p, &z.name));
|
|
}
|
|
}
|
|
}
|
|
if let Err(e) = cfg.key_ring()
|
|
&& !out.iter().any(|f| f.location.starts_with("keys"))
|
|
{
|
|
out.push(Finding { severity: "warning", location: "keys".into(), message: e });
|
|
}
|
|
if cfg.recursion.enabled
|
|
&& let Err(e) = dns_recursive::hints::load(cfg.recursion.root_hints.as_ref().map(|p| cfg.resolve_path(p)).as_deref())
|
|
.and_then(|h| dns_recursive::hints::root_servers(&h).map(|_| ()))
|
|
{
|
|
out.push(Finding { severity: "error", location: "recursion.root_hints".into(), message: e });
|
|
}
|
|
out
|
|
}
|