v0.1.0
This commit is contained in:
commit
fa432bebda
189 files changed
+26219
No files matched your search
@@ -0,0 +1,336 @@
|
||||
//! Authoritative behaviour, Primary↔Secondary synchronisation, TSIG/ACL, DDNS.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
mod common;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use common::*;
|
||||
use dns_core::client::{QueryOptions, TransferKind, TransferLimits, exchange, transfer};
|
||||
use dns_core::proto::op::ResponseCode;
|
||||
use dns_core::proto::rr::rdata::{A, TXT};
|
||||
use dns_core::proto::rr::{DNSClass, RData, Record, RecordType};
|
||||
use dns_core::tsig::TsigKey;
|
||||
|
||||
const ZONE: &str = r#"$TTL 300
|
||||
@ IN SOA ns1 hostmaster 2024010101 3600 5 1209600 60
|
||||
IN NS ns1
|
||||
IN MX 10 mail
|
||||
ns1 IN A 127.0.0.1
|
||||
mail IN A 192.0.2.25
|
||||
www IN A 192.0.2.10
|
||||
www IN AAAA 2001:db8::10
|
||||
alias IN CNAME www
|
||||
*.wild IN TXT "wild"
|
||||
sub IN NS ns.sub
|
||||
ns.sub IN A 192.0.2.53
|
||||
"#;
|
||||
|
||||
fn primary_yaml(dir: &std::path::Path, extra_zone: &str) -> String {
|
||||
write(dir, "example.com.zone", ZONE);
|
||||
format!(
|
||||
r#"
|
||||
server: {{ mode: primary, listen: ["127.0.0.1:0"], data_dir: data }}
|
||||
keys:
|
||||
- {{ name: xfr-key, algorithm: hmac-sha256, secret: "base64:{KEY_B64}" }}
|
||||
- {{ name: ddns-key, algorithm: hmac-sha256, secret: "base64:{KEY_B64}" }}
|
||||
api: {{ enabled: false }}
|
||||
rate_limit: {{ enabled: false }}
|
||||
zones:
|
||||
- name: example.com
|
||||
type: primary
|
||||
file: example.com.zone
|
||||
transfer_tsig_key: xfr-key
|
||||
update_keys: [ddns-key]
|
||||
update_acl: ["127.0.0.0/8"]
|
||||
{extra_zone}
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn authoritative_answers() {
|
||||
let dir = tmpdir("auth");
|
||||
let h = start(&dir, &primary_yaml(&dir, "")).await;
|
||||
let s = h.bound.dns[0];
|
||||
|
||||
let m = q(s, "www.example.com.", RecordType::A).await;
|
||||
assert!(m.metadata.authoritative);
|
||||
assert!(!m.metadata.recursion_available);
|
||||
assert_eq!(answers(&m), vec!["A 192.0.2.10"]);
|
||||
|
||||
let m = q(s, "alias.example.com.", RecordType::AAAA).await;
|
||||
assert_eq!(answers(&m), vec!["AAAA 2001:db8::10", "CNAME www.example.com."]);
|
||||
|
||||
let m = q(s, "nope.example.com.", RecordType::A).await;
|
||||
assert_eq!(m.metadata.response_code, ResponseCode::NXDomain);
|
||||
assert!(m.metadata.authoritative);
|
||||
assert_eq!(m.authorities[0].record_type(), RecordType::SOA);
|
||||
|
||||
let m = q(s, "www.example.com.", RecordType::MX).await;
|
||||
assert_eq!(m.metadata.response_code, ResponseCode::NoError);
|
||||
assert!(m.answers.is_empty());
|
||||
assert_eq!(m.authorities[0].ttl, 60, "negative TTL = min(SOA TTL, MINIMUM)");
|
||||
|
||||
let m = q(s, "x.wild.example.com.", RecordType::TXT).await;
|
||||
assert_eq!(answers(&m), vec!["TXT wild"]);
|
||||
|
||||
let m = q(s, "host.sub.example.com.", RecordType::A).await;
|
||||
assert!(!m.metadata.authoritative, "referral is not authoritative");
|
||||
assert_eq!(m.authorities[0].record_type(), RecordType::NS);
|
||||
assert_eq!(m.additionals.len(), 1);
|
||||
|
||||
let m = q(s, "example.com.", RecordType::MX).await;
|
||||
assert_eq!(m.additionals.len(), 1, "MX target address in additional section");
|
||||
|
||||
// Not our zone + no recursion configured -> REFUSED (no open resolver).
|
||||
let m = q(s, "www.example.org.", RecordType::A).await;
|
||||
assert_eq!(m.metadata.response_code, ResponseCode::Refused);
|
||||
|
||||
// TCP works too.
|
||||
let m = q_opts(s, "www.example.com.", RecordType::A, QueryOptions { tcp_only: true, ..Default::default() }).await;
|
||||
assert_eq!(answers(&m), vec!["A 192.0.2.10"]);
|
||||
|
||||
// Malformed packet -> FORMERR, response packets are ignored.
|
||||
let sock = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
|
||||
sock.send_to(&[0x12, 0x34, 0x01, 0x00, 0, 1, 0, 0, 0, 0, 0, 0, 0xff, 0xff], s).await.unwrap();
|
||||
let mut buf = [0u8; 512];
|
||||
let (n, _) = tokio::time::timeout(Duration::from_secs(2), sock.recv_from(&mut buf)).await.unwrap().unwrap();
|
||||
let r = dns_core::proto::op::Message::from_vec(&buf[..n]).unwrap();
|
||||
assert_eq!((r.metadata.id, r.metadata.response_code), (0x1234, ResponseCode::FormErr));
|
||||
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn transfers_require_tsig_and_acl() {
|
||||
let dir = tmpdir("xfr");
|
||||
let h = start(&dir, &primary_yaml(&dir, "")).await;
|
||||
let s = h.bound.dns[0];
|
||||
let zone = n("example.com.");
|
||||
let lim = TransferLimits { timeout: Duration::from_secs(5), ..Default::default() };
|
||||
|
||||
// No TSIG -> refused.
|
||||
let e = transfer(s, &zone, &TransferKind::Axfr, None, lim).await.unwrap_err();
|
||||
assert!(e.to_string().contains("REFUSED") || e.to_string().contains("Refused"), "{e}");
|
||||
// Wrong key secret -> NOTAUTH, and response cannot be verified.
|
||||
let bad = TsigKey::new("xfr-key", "hmac-sha256", "d3JvbmdzZWNyZXR3cm9uZ3NlY3JldA==").unwrap();
|
||||
assert!(transfer(s, &zone, &TransferKind::Axfr, Some(&bad), lim).await.is_err());
|
||||
// Right key -> full zone, every message verified.
|
||||
let key = TsigKey::new("xfr-key", "hmac-sha256", KEY_B64).unwrap();
|
||||
let st = transfer(s, &zone, &TransferKind::Axfr, Some(&key), lim).await.unwrap();
|
||||
assert!(st.tsig_verified);
|
||||
assert_eq!(st.records.first().unwrap().record_type(), RecordType::SOA);
|
||||
assert_eq!(st.records.last().unwrap().record_type(), RecordType::SOA);
|
||||
assert_eq!(st.records.len(), 12);
|
||||
let m = &h.shared.metrics;
|
||||
assert_eq!(m.xfr_out_refused.load(std::sync::atomic::Ordering::Relaxed), 1, "unsigned AXFR refused");
|
||||
assert_eq!(m.tsig_failures.load(std::sync::atomic::Ordering::Relaxed), 1, "bad MAC -> NOTAUTH");
|
||||
// AXFR over UDP is refused
|
||||
let mut am = dns_core::proto::op::Message::query();
|
||||
am.add_query(dns_core::proto::op::Query::query(zone.clone(), RecordType::AXFR));
|
||||
let mut opts = dns_core::client::QueryOptions { tsig: Some(key.clone()), ignore_tc: true, ..Default::default() };
|
||||
opts.retries = 0;
|
||||
let ex = exchange(s, am, &opts).await.unwrap();
|
||||
assert_eq!(ex.message.metadata.response_code, ResponseCode::Refused);
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn primary_secondary_sync_notify_ixfr_restart() {
|
||||
// --- primary
|
||||
let pdir = tmpdir("pri");
|
||||
// the secondary listens on a fixed free port so the primary can NOTIFY it
|
||||
let sport = free_port();
|
||||
let secondary_addr = format!("127.0.0.1:{sport}");
|
||||
let p_yaml = primary_yaml(&pdir, "").replace(
|
||||
" update_keys: [ddns-key]",
|
||||
&format!(" notify: [\"{secondary_addr}\"]\n notify_tsig_key: xfr-key\n update_keys: [ddns-key]"),
|
||||
);
|
||||
let p = start(&pdir, &p_yaml).await;
|
||||
let paddr = p.bound.dns[0];
|
||||
|
||||
// --- secondary
|
||||
let sdir = tmpdir("sec");
|
||||
let s_yaml = format!(
|
||||
r#"
|
||||
server: {{ mode: secondary, listen: ["{secondary_addr}"], data_dir: data }}
|
||||
keys:
|
||||
- {{ name: xfr-key, algorithm: hmac-sha256, secret: "base64:{KEY_B64}" }}
|
||||
api: {{ enabled: false }}
|
||||
rate_limit: {{ enabled: false }}
|
||||
zones:
|
||||
- name: example.com
|
||||
type: secondary
|
||||
primaries: ["{paddr}"]
|
||||
transfer: {{ prefer: ixfr, fallback_axfr: true, tsig_key: xfr-key }}
|
||||
"#
|
||||
);
|
||||
let s = start(&sdir, &s_yaml).await;
|
||||
let saddr = s.bound.dns[0];
|
||||
|
||||
// initial AXFR
|
||||
eventually("secondary loads zone", || async { q(saddr, "www.example.com.", RecordType::A).await.answers.len() == 1 }).await;
|
||||
let m = q(saddr, "www.example.com.", RecordType::A).await;
|
||||
assert!(m.metadata.authoritative, "secondary answers authoritatively from its own copy");
|
||||
let entry = s.shared.rt().zones.get(&n("example.com.")).unwrap();
|
||||
assert_eq!(entry.transfers().last().unwrap().kind, "AXFR");
|
||||
|
||||
// change on the primary (same path the API uses) -> NOTIFY -> IXFR
|
||||
let pentry = p.shared.rt().zones.get(&n("example.com.")).unwrap();
|
||||
let new = Record::from_rdata(n("new.example.com."), 300, RData::A(A::new(192, 0, 2, 77)));
|
||||
let serial = pentry.modify(&[], &[new]).unwrap();
|
||||
pentry.send_notifies();
|
||||
eventually("secondary receives IXFR", || async { q(saddr, "new.example.com.", RecordType::A).await.answers.len() == 1 }).await;
|
||||
assert_eq!(entry.current().unwrap().serial(), serial);
|
||||
assert_eq!(entry.transfers().last().unwrap().kind, "IXFR");
|
||||
assert!(s.shared.metrics.notify_received.load(std::sync::atomic::Ordering::Relaxed) >= 1);
|
||||
// the primary's zone file was rewritten atomically with the new serial
|
||||
let text = std::fs::read_to_string(pdir.join("example.com.zone")).unwrap();
|
||||
assert!(text.contains("192.0.2.77"));
|
||||
|
||||
// NOTIFY from an unauthorised source is refused
|
||||
let mut nm = dns_core::proto::op::Message::new(7, dns_core::proto::op::MessageType::Query, dns_core::proto::op::OpCode::Notify);
|
||||
nm.add_query(dns_core::proto::op::Query::query(n("example.com."), RecordType::SOA));
|
||||
let sock = tokio::net::UdpSocket::bind("127.0.0.9:0").await.unwrap();
|
||||
sock.send_to(&nm.to_vec().unwrap(), saddr).await.unwrap();
|
||||
let mut buf = [0u8; 512];
|
||||
let (len, _) = tokio::time::timeout(Duration::from_secs(2), sock.recv_from(&mut buf)).await.unwrap().unwrap();
|
||||
assert_eq!(dns_core::proto::op::Message::from_vec(&buf[..len]).unwrap().metadata.response_code, ResponseCode::Refused);
|
||||
|
||||
// restart the secondary while the primary is down: the persisted copy is served
|
||||
s.shutdown().await;
|
||||
p.shutdown().await;
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
let s2 = start(&sdir, &s_yaml).await;
|
||||
let m = q(s2.bound.dns[0], "new.example.com.", RecordType::A).await;
|
||||
assert_eq!(answers(&m), vec!["A 192.0.2.77"]);
|
||||
assert!(m.metadata.authoritative);
|
||||
s2.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn secondary_expires_when_primary_unreachable() {
|
||||
let sdir = tmpdir("exp");
|
||||
std::fs::create_dir_all(sdir.join("data/secondary")).unwrap();
|
||||
// snapshot with tiny EXPIRE whose last success is long ago
|
||||
let z = ZONE.replace("3600 5 1209600 60", "5 5 10 60");
|
||||
write(&sdir.join("data/secondary"), "example.com.zone", &z);
|
||||
write(
|
||||
&sdir.join("data/secondary"),
|
||||
"example.com.zone.meta.json",
|
||||
r#"{"last_success_unix": 1000, "serial": 2024010101, "primary": "x"}"#,
|
||||
);
|
||||
let yaml = r#"
|
||||
server: { listen: ["127.0.0.1:0"], data_dir: data }
|
||||
api: { enabled: false }
|
||||
zones:
|
||||
- { name: example.com, type: secondary, primaries: ["127.0.0.1:9"] }
|
||||
"#;
|
||||
let s = start(&sdir, yaml).await;
|
||||
let m = q(s.bound.dns[0], "www.example.com.", RecordType::A).await;
|
||||
assert_eq!(m.metadata.response_code, ResponseCode::ServFail, "expired zone must not be served as authoritative");
|
||||
s.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn dynamic_update_authorised_and_refused() {
|
||||
let dir = tmpdir("ddns");
|
||||
let h = start(&dir, &primary_yaml(&dir, "")).await;
|
||||
let s = h.bound.dns[0];
|
||||
let zone = n("example.com.");
|
||||
let rec = Record::from_rdata(n("dyn.example.com."), 120, RData::A(A::new(192, 0, 2, 200)));
|
||||
|
||||
// unsigned -> REFUSED
|
||||
let m = dns_authoritative::update::build_update(&zone, vec![], vec![rec.clone()]);
|
||||
let ex = exchange(s, m, &QueryOptions::default()).await.unwrap();
|
||||
assert_eq!(ex.message.metadata.response_code, ResponseCode::Refused);
|
||||
|
||||
// signed with the update key -> applied
|
||||
let key = TsigKey::new("ddns-key", "hmac-sha256", KEY_B64).unwrap();
|
||||
let opts = QueryOptions { tsig: Some(key.clone()), ..Default::default() };
|
||||
let m = dns_authoritative::update::build_update(&zone, vec![], vec![rec.clone()]);
|
||||
let ex = exchange(s, m, &opts).await.unwrap();
|
||||
assert_eq!(ex.message.metadata.response_code, ResponseCode::NoError);
|
||||
assert!(ex.tsig_verified);
|
||||
assert_eq!(answers(&q(s, "dyn.example.com.", RecordType::A).await), vec!["A 192.0.2.200"]);
|
||||
|
||||
// prerequisite "name not in use" fails -> YXDOMAIN
|
||||
let mut pre = Record::update0(n("dyn.example.com."), 0, RecordType::ANY);
|
||||
pre.dns_class = DNSClass::NONE;
|
||||
let m = dns_authoritative::update::build_update(&zone, vec![pre], vec![rec.clone()]);
|
||||
let ex = exchange(s, m, &opts).await.unwrap();
|
||||
assert_eq!(ex.message.metadata.response_code, ResponseCode::YXDomain);
|
||||
|
||||
// delete the RRset
|
||||
let mut del = Record::update0(n("dyn.example.com."), 0, RecordType::A);
|
||||
del.dns_class = DNSClass::ANY;
|
||||
let m = dns_authoritative::update::build_update(&zone, vec![], vec![del]);
|
||||
assert_eq!(exchange(s, m, &opts).await.unwrap().message.metadata.response_code, ResponseCode::NoError);
|
||||
assert_eq!(q(s, "dyn.example.com.", RecordType::A).await.metadata.response_code, ResponseCode::NXDomain);
|
||||
|
||||
// the audit log captured both
|
||||
let audit = h.shared.audit.tail(10);
|
||||
assert!(audit.len() >= 3);
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn update_policy_with_bind_key_file_and_ip_only_updates() {
|
||||
let dir = tmpdir("ddns-policy");
|
||||
write(&dir, "example.com.zone", ZONE);
|
||||
write(&dir, "example.net.zone", "$TTL 60\n@ SOA ns hostmaster 1 60 60 600 60\n@ NS ns\nns A 192.0.2.1\n");
|
||||
// key file copied from BIND, included unchanged
|
||||
write(
|
||||
&dir,
|
||||
"keys.conf",
|
||||
&format!(
|
||||
"key \"acme-key.\" {{\n\talgorithm hmac-sha256;\n\tsecret \"{KEY_B64}\";\n}};\nkey \"other-key\" {{ algorithm hmac-sha256; secret \"{KEY_B64}\"; }};\n"
|
||||
),
|
||||
);
|
||||
let yaml = r#"
|
||||
include: [keys.conf]
|
||||
server: { mode: primary, listen: ["127.0.0.1:0"], data_dir: data }
|
||||
api: { enabled: false }
|
||||
rate_limit: { enabled: false }
|
||||
zones:
|
||||
- name: example.com
|
||||
type: primary
|
||||
file: example.com.zone
|
||||
update_policy:
|
||||
- "grant acme-key. name _acme-challenge.example.com. TXT"
|
||||
- "grant acme-key. wildcard *.dyn.example.com. A"
|
||||
- { deny: other-key, match: zonesub }
|
||||
- name: example.net
|
||||
type: primary
|
||||
file: example.net.zone
|
||||
allow_update: ["127.0.0.1"] # IP only, no key (BIND allow-update { 127.0.0.1; })
|
||||
"#;
|
||||
let h = start(&dir, yaml).await;
|
||||
let s = h.bound.dns[0];
|
||||
let zone = n("example.com.");
|
||||
let txt = |owner: &str| Record::from_rdata(n(owner), 60, RData::TXT(TXT::new(vec!["token".into()])));
|
||||
let a = |owner: &str| Record::from_rdata(n(owner), 60, RData::A(A::new(192, 0, 2, 7)));
|
||||
let acme = QueryOptions { tsig: Some(TsigKey::new("acme-key.", "hmac-sha256", KEY_B64).unwrap()), ..Default::default() };
|
||||
let other = QueryOptions { tsig: Some(TsigKey::new("other-key", "hmac-sha256", KEY_B64).unwrap()), ..Default::default() };
|
||||
let rc = |ex: dns_core::client::Exchange| ex.message.metadata.response_code;
|
||||
let up = |r: Record| dns_authoritative::update::build_update(&zone, vec![], vec![r]);
|
||||
|
||||
// granted: exact name + type
|
||||
assert_eq!(rc(exchange(s, up(txt("_acme-challenge.example.com.")), &acme).await.unwrap()), ResponseCode::NoError);
|
||||
assert_eq!(answers(&q(s, "_acme-challenge.example.com.", RecordType::TXT).await), vec!["TXT token"]);
|
||||
// wrong type / wrong name / unsigned / other key -> refused
|
||||
assert_eq!(rc(exchange(s, up(a("_acme-challenge.example.com.")), &acme).await.unwrap()), ResponseCode::Refused);
|
||||
assert_eq!(rc(exchange(s, up(txt("www.example.com.")), &acme).await.unwrap()), ResponseCode::Refused);
|
||||
assert_eq!(rc(exchange(s, up(txt("_acme-challenge.example.com.")), &QueryOptions::default()).await.unwrap()), ResponseCode::Refused);
|
||||
assert_eq!(rc(exchange(s, up(txt("_acme-challenge.example.com.")), &other).await.unwrap()), ResponseCode::Refused);
|
||||
// wildcard: strictly below dyn.example.com
|
||||
assert_eq!(rc(exchange(s, up(a("h1.dyn.example.com.")), &acme).await.unwrap()), ResponseCode::NoError);
|
||||
assert_eq!(rc(exchange(s, up(a("dyn.example.com.")), &acme).await.unwrap()), ResponseCode::Refused);
|
||||
|
||||
// IP-only zone: unsigned update from 127.0.0.1 is accepted
|
||||
let net = n("example.net.");
|
||||
let m = dns_authoritative::update::build_update(&net, vec![], vec![a("host.example.net.")]);
|
||||
assert_eq!(rc(exchange(s, m, &QueryOptions::default()).await.unwrap()), ResponseCode::NoError);
|
||||
assert_eq!(answers(&q(s, "host.example.net.", RecordType::A).await), vec!["A 192.0.2.7"]);
|
||||
h.shutdown().await;
|
||||
}
|
||||
Reference in new issue
Block a user