v0.1.1
This commit is contained in:
1 parent
fa432bebda
commit
b796a6685e
15 files changed
+390
-153
No files matched your search
@@ -66,6 +66,15 @@ pub fn emit(origin: &Name, records: &[Record]) -> String {
|
||||
for r in recs {
|
||||
let data = match &r.data {
|
||||
RData::Unknown { .. } => crate::zonefile_ext::generic_rdata(&r.data).unwrap_or_default(),
|
||||
// hickory's Display joins TXT strings without quotes, which does not parse back
|
||||
RData::TXT(t) => t.txt_data.iter().map(|s| quote_character_string(s)).collect::<Vec<_>>().join(" "),
|
||||
RData::HINFO(h) => format!("{} {}", quote_character_string(&h.cpu), quote_character_string(&h.os)),
|
||||
// hickory prints SvcParam lists with a trailing comma (`alpn=h3,h2,`), which
|
||||
// other software (e.g. BIND reading a file written here) rejects
|
||||
RData::SVCB(_) | RData::HTTPS(_) => {
|
||||
let d = r.data.to_string();
|
||||
d.split(' ').map(|p| p.strip_suffix(',').unwrap_or(p)).collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
d => d.to_string(),
|
||||
};
|
||||
let t = match r.record_type() {
|
||||
@@ -77,6 +86,42 @@ pub fn emit(origin: &Name, records: &[Record]) -> String {
|
||||
s
|
||||
}
|
||||
|
||||
/// RFC 1035 §5.1 <character-string> in quotes: `"` and `\\` escaped, control bytes as
|
||||
/// `\DDD`. Valid UTF-8 text is written as is (the zone parser reads it back byte-exact;
|
||||
/// it would misread `\DDD` escapes above 127).
|
||||
fn quote_character_string(s: &[u8]) -> String {
|
||||
let mut o = String::with_capacity(s.len() + 2);
|
||||
o.push('"');
|
||||
match std::str::from_utf8(s) {
|
||||
Ok(text) => {
|
||||
for c in text.chars() {
|
||||
match c {
|
||||
'"' | '\\' => {
|
||||
o.push('\\');
|
||||
o.push(c);
|
||||
}
|
||||
c if (c as u32) < 0x20 || c as u32 == 0x7f => o.push_str(&format!("\\{:03}", c as u32)),
|
||||
c => o.push(c),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
for &b in s {
|
||||
match b {
|
||||
b'"' | b'\\' => {
|
||||
o.push('\\');
|
||||
o.push(b as char);
|
||||
}
|
||||
0x20..=0x7e => o.push(b as char),
|
||||
_ => o.push_str(&format!("\\{b:03}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
o.push('"');
|
||||
o
|
||||
}
|
||||
|
||||
/// Atomically write `contents` to `path` (temp file + fsync + rename + dir fsync).
|
||||
pub fn atomic_write(path: &Path, contents: &[u8]) -> CoreResult<()> {
|
||||
use std::io::Write;
|
||||
@@ -150,9 +195,81 @@ y IN A \# 4 c0000201
|
||||
assert!(recs.iter().any(|r| r.name.to_string() == "y.example." && r.data.to_string() == "192.0.2.1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ttl_directive_changes_apply_to_following_records() {
|
||||
// BIND `named-compilezone -s relative` output: `$TTL` changes mid-file and
|
||||
// owner-less lines; each record must get the `$TTL` in force at its line.
|
||||
let origin = Name::from_ascii("example.org.").unwrap();
|
||||
let text = "$ORIGIN .\n$TTL 3600\nexample.org IN SOA ns hm 1 2 3 4 5\n$TTL 600\n\
|
||||
\t\tNS ns1.example.net.\n$TTL 60\n\t\tA 192.0.2.1\n$ORIGIN example.org.\n\
|
||||
www 300 A 192.0.2.2\n\tAAAA 2001:db8::2\n";
|
||||
let recs = parse_str(text, &origin, None).unwrap();
|
||||
let ttl = |t: RecordType, n: &str| recs.iter().find(|r| r.record_type() == t && r.name.to_string() == n).unwrap().ttl;
|
||||
assert_eq!(ttl(RecordType::SOA, "example.org."), 3600);
|
||||
assert_eq!(ttl(RecordType::NS, "example.org."), 600);
|
||||
assert_eq!(ttl(RecordType::A, "example.org."), 60);
|
||||
assert_eq!(ttl(RecordType::A, "www.example.org."), 300);
|
||||
assert_eq!(ttl(RecordType::AAAA, "www.example.org."), 60);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ttl_without_directive_follows_last_explicit_ttl() {
|
||||
let origin = Name::from_ascii("example.org.").unwrap();
|
||||
let text = "@ 900 IN SOA ns hm 1 2 3 4 5\n IN NS ns1.example.net.\nwww A 192.0.2.1\n";
|
||||
let recs = parse_str(text, &origin, None).unwrap();
|
||||
assert!(recs.iter().all(|r| r.ttl == 900));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_error_is_reported() {
|
||||
let origin = Name::from_ascii("example.com.").unwrap();
|
||||
assert!(parse_str("@ IN A not-an-ip\n", &origin, None).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod roundtrip_tests {
|
||||
use super::*;
|
||||
|
||||
fn roundtrip(src: &str, origin: &str) {
|
||||
let origin = Name::from_ascii(origin).unwrap();
|
||||
let mut a = parse_str(src, &origin, None).unwrap();
|
||||
let text = emit(&origin, &a);
|
||||
let mut b = parse_str(&text, &origin, None).unwrap_or_else(|e| panic!("emitted zone does not parse: {e}\n{text}"));
|
||||
let key = |r: &Record| format!("{} {} {:?}", r.name, r.record_type(), r.data);
|
||||
a.sort_by_key(key);
|
||||
b.sort_by_key(key);
|
||||
assert!(!text.contains(", ") && !text.contains(",\n"), "trailing comma in SvcParams:\n{text}");
|
||||
let only_a: Vec<_> = a.iter().filter(|r| !b.contains(r)).collect();
|
||||
let only_b: Vec<_> = b.iter().filter(|r| !a.contains(r)).collect();
|
||||
assert!(only_a.is_empty() && only_b.is_empty(), "round trip changed records:\nbefore: {only_a:?}\nafter: {only_b:?}\n{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn txt_strings_survive_writing() {
|
||||
roundtrip(
|
||||
"$TTL 300\n@ SOA ns hm 1 1 1 1 1\n@ NS ns\n@ TXT \"v=spf1 include:_spf.example.net -all\"\n\
|
||||
@ TXT \"a\" \"b c\"\nx TXT \"quote \\\"inside\\\" and ; semicolon \\\\ backslash\"\n\
|
||||
y TXT \"zażółć gęślą jaźń \\009tab\"\nz TXT \"\"\n\
|
||||
dkim._domainkey TXT \"v=DKIM1; k=rsa; \" \"p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\"\n",
|
||||
"example.com.",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_types_survive_writing() {
|
||||
roundtrip(
|
||||
"$TTL 300\n@ SOA ns hm 1 1 1 1 1\n@ NS ns\nns A 192.0.2.1\nns AAAA 2001:db8::1\n@ MX 10 mail\nmail A 192.0.2.2\n\
|
||||
www CNAME @\nalias MX 5 @\n_sip._tcp SRV 10 60 5060 sip\nsip A 192.0.2.3\n@ CAA 0 issue \"letsencrypt.org\"\n\
|
||||
@ CAA 128 iodef \"mailto:security@example.com\"\nh HINFO \"Intel x86\" \"Linux 6.1\"\n\
|
||||
@ NAPTR 100 10 \"U\" \"E2U+sip\" \"!^.*$!sip:info@example.com!\" .\n\
|
||||
@ SSHFP 4 2 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\
|
||||
_443._tcp TLSA 3 1 1 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\
|
||||
@ HTTPS 1 . alpn=h2,h3 ipv4hint=192.0.2.1,192.0.2.2\nsvc SVCB 1 web.example.com. port=8443\n\
|
||||
sub NS ns.sub\nns.sub A 192.0.2.9\nsub DS 12345 13 2 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\n\
|
||||
4.2.0.192.in-addr.arpa. PTR host.example.com.\n",
|
||||
"example.com.",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -244,7 +244,10 @@ pub fn extract(text: &str, origin: &Name, default_ttl: u32) -> CoreResult<(Strin
|
||||
let mut origin = origin.clone();
|
||||
let mut ttl = default_ttl;
|
||||
let mut last_owner: Option<Name> = None;
|
||||
let mut last_ttl: Option<u32> = None;
|
||||
// RFC 2308 / BIND: after a `$TTL` directive, records without a TTL get the
|
||||
// current `$TTL`; before any `$TTL`, the last explicitly given TTL (RFC 1035).
|
||||
let mut has_ttl_directive = false;
|
||||
let mut last_explicit_ttl: Option<u32> = None;
|
||||
let mut pass = String::with_capacity(text.len());
|
||||
let mut extra = Vec::new();
|
||||
for (ws, toks) in logical_lines(text) {
|
||||
@@ -259,6 +262,7 @@ pub fn extract(text: &str, origin: &Name, default_ttl: u32) -> CoreResult<(Strin
|
||||
"$TTL" => {
|
||||
if let Some(t) = toks.get(1) {
|
||||
ttl = crate::proto::serialize::txt::parse_ttl(t).map_err(|e| CoreError::ZoneFile(e.to_string()))?;
|
||||
has_ttl_directive = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -291,9 +295,15 @@ pub fn extract(text: &str, origin: &Name, default_ttl: u32) -> CoreResult<(Strin
|
||||
let tu = tname.to_ascii_uppercase();
|
||||
let rd = &toks[i + 1..];
|
||||
let generic_syntax = rd.first().is_some_and(|t| t == "\\#");
|
||||
let record_ttl = rec_ttl.or(if ws { last_ttl } else { None }).unwrap_or(ttl);
|
||||
let record_ttl = match rec_ttl {
|
||||
Some(t) => {
|
||||
last_explicit_ttl = Some(t);
|
||||
t
|
||||
}
|
||||
None if has_ttl_directive => ttl,
|
||||
None => last_explicit_ttl.unwrap_or(ttl),
|
||||
};
|
||||
last_owner = Some(owner.clone());
|
||||
last_ttl = Some(record_ttl);
|
||||
let rtype_generic = tu.strip_prefix("TYPE").and_then(|n| n.parse::<u16>().ok()).map(RecordType::from);
|
||||
if SPECIAL.contains(&tu.as_str()) || generic_syntax || rtype_generic.is_some() {
|
||||
let data = if generic_syntax {
|
||||
@@ -312,9 +322,17 @@ pub fn extract(text: &str, origin: &Name, default_ttl: u32) -> CoreResult<(Strin
|
||||
extra.push(r);
|
||||
continue;
|
||||
}
|
||||
// pass through with explicit owner/TTL/class
|
||||
pass.push_str(&format!("{owner} {record_ttl} {class} "));
|
||||
pass.push_str(&toks[i..].join(" "));
|
||||
// pass through with explicit owner/TTL/class; a bare `@` in the data (BIND:
|
||||
// `www CNAME @`, `MX 10 @`) means the current origin, which the parser lacks
|
||||
pass.push_str(&format!("{owner} {record_ttl} {class} {tname}"));
|
||||
for t in rd {
|
||||
pass.push(' ');
|
||||
if t == "@" {
|
||||
pass.push_str(&origin.to_string());
|
||||
} else {
|
||||
pass.push_str(t);
|
||||
}
|
||||
}
|
||||
pass.push('\n');
|
||||
}
|
||||
Ok((pass, extra))
|
||||
|
||||
Reference in new issue
Block a user