Skip to content

Commit

Permalink
Add config option for cname records
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hellhake authored and benfrancis committed Sep 30, 2024
1 parent 492a819 commit 130dfd4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ txt_records = [
[ "_psl", "https://github.com/publicsuffix/list/pull/XYZ" ],
[ "@", "something useful" ],
]
cname_records = [
[ "subdomain.mydomain.org", "mydomain.org" ],
[ "subdomain2.mydomain.org", "mydomain.org" ],
]
soa_record = "ns1.mydomain.org. dns-admin.mydomain.org. 2018082801 900 900 1209600 60"
www_addresses = [
"10.11.12.13",
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct PdnsOptions {
pub mx_records: Vec<String>,
pub ns_records: Vec<Vec<String>>,
pub txt_records: Vec<Vec<String>>,
pub cname_records: Vec<Vec<String>>,
pub soa_record: String,
pub www_addresses: Vec<String>,
pub geoip: GeoIp,
Expand Down
65 changes: 65 additions & 0 deletions src/pdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,37 @@ fn build_txt_response(
records
}

// Returns a CNAME record for a given qname.
fn build_config_cname_response(
qname: &str,
config: &Config,
) -> Vec<PdnsLookupResponse> {
let sanitized_qname = remove_trailing_dot(qname);
let mut records = vec![];
for cname in &config.options.pdns.cname_records {
if cname[0] == sanitized_qname {
records.push(PdnsLookupResponse {
qtype: "CNAME".to_owned(),
qname: qname.to_owned(),
content: cname[1].to_owned(),
ttl: config.options.pdns.dns_ttl,
domain_id: None,
scope_mask: None,
auth: None,
});
}
}

records
}

fn remove_trailing_dot(s: &str) -> &str {
match s.chars().last() {
Some('.') => &s[0..s.len() - 1],
_ => s
}
}

// Returns a TXT record with the DNS challenge content.
fn build_dns_challenge_response(
qname: &str,
Expand Down Expand Up @@ -476,6 +507,13 @@ fn handle_lookup(req: PdnsRequest, config: &Config) -> Result<PdnsResponse, Stri
}
}

if qtype == "CNAME" || qtype == "ANY" {
// Add "CNAME" records.
for record in build_config_cname_response(&original_qname, config) {
result.push(PdnsResponseParams::Lookup(record));
}
}

let conn = config.db.get_connection();
if conn.is_err() {
error!(
Expand Down Expand Up @@ -979,6 +1017,33 @@ mod tests {
})
);

// A query for a subdomain
let request = build_lookup(
"lookup",
Some("CNAME"),
Some("subdomain.mydomain.org."),
None,
);
let body = serde_json::to_string(&request).unwrap();
stream.write_all(body.as_bytes()).unwrap();
stream.write_all(b"\n").unwrap();

let len = stream.read(&mut answer).unwrap();
let response: serde_json::Value = serde_json::from_slice(&answer[..len]).unwrap();
assert_json_eq!(
response,
json!({
"result": [
{
"qtype": "CNAME",
"qname": "subdomain.mydomain.org.",
"content": "mydomain.org",
"ttl": 86400,
}
],
})
);

// A query for ns1
let request = build_lookup("lookup", Some("A"), Some("ns1.mydomain.org."), None);
let body = serde_json::to_string(&request).unwrap();
Expand Down

0 comments on commit 130dfd4

Please sign in to comment.