dns-update 0.5.1

Dynamic DNS update (RFC 2136 and cloud) library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * Copyright Stalwart Labs LLC See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

use crate::{
    CAARecord, DnsRecord, DnsRecordType, Error, IntoFqdn, KeyValue, MXRecord, SRVRecord,
    TLSARecord, TlsaCertUsage, TlsaMatching, TlsaSelector,
    http::{HttpClient, HttpClientBuilder},
    utils::strip_origin_from_name,
};
use base64::{Engine, engine::general_purpose::STANDARD};
use serde::{Deserialize, Serialize};
use std::time::Duration;

const DEFAULT_ENDPOINT: &str = "https://api.autodns.com/v1";
pub const DEFAULT_CONTEXT: u32 = 4;

#[derive(Clone)]
pub struct AutodnsProvider {
    client: HttpClient,
    endpoint: String,
}

#[derive(Serialize, Debug)]
pub struct ZoneStream {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub adds: Vec<ResourceRecord>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub rems: Vec<ResourceRecord>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ResourceRecord {
    pub name: String,
    pub ttl: u32,
    #[serde(rename = "type")]
    pub record_type: String,
    pub value: String,
    #[serde(default, skip_serializing_if = "is_zero")]
    pub pref: u32,
}

fn is_zero(v: &u32) -> bool {
    *v == 0
}

#[derive(Deserialize, Debug)]
pub struct Zone {
    #[serde(rename = "origin", default)]
    pub origin: String,
    #[serde(rename = "resourceRecords", default)]
    pub resource_records: Vec<ResourceRecord>,
}

#[derive(Deserialize, Debug)]
pub struct DataZoneResponse {
    #[serde(default)]
    pub data: Vec<Zone>,
}

impl AutodnsProvider {
    pub(crate) fn new(
        username: impl AsRef<str>,
        password: impl AsRef<str>,
        context: Option<u32>,
        timeout: Option<Duration>,
    ) -> crate::Result<Self> {
        let user = username.as_ref();
        let pass = password.as_ref();
        if user.is_empty() {
            return Err(Error::Api("AutoDNS username is empty".to_string()));
        }
        if pass.is_empty() {
            return Err(Error::Api("AutoDNS password is empty".to_string()));
        }
        let encoded = STANDARD.encode(format!("{user}:{pass}"));
        let ctx = context.unwrap_or(DEFAULT_CONTEXT);
        let client = HttpClientBuilder::default()
            .with_header("Authorization", format!("Basic {encoded}"))
            .with_header("X-Domainrobot-Context", ctx.to_string())
            .with_header("Accept", "application/json")
            .with_timeout(timeout)
            .build();
        Ok(Self {
            client,
            endpoint: DEFAULT_ENDPOINT.to_string(),
        })
    }

    #[cfg(test)]
    pub(crate) fn with_endpoint(self, endpoint: impl AsRef<str>) -> Self {
        Self {
            endpoint: endpoint.as_ref().to_string(),
            ..self
        }
    }

    fn stream_url(&self, domain: &str) -> String {
        format!("{}/zone/{}/_stream", self.endpoint, domain)
    }

    pub(crate) async fn set_rrset(
        &self,
        name: impl IntoFqdn<'_>,
        record_type: DnsRecordType,
        ttl: u32,
        records: Vec<DnsRecord>,
        origin: impl IntoFqdn<'_>,
    ) -> crate::Result<()> {
        check_record_types(record_type, &records)?;
        let domain = origin.into_name();
        let fqdn = name.into_fqdn();

        let existing = self.find_existing(&domain, &fqdn, record_type).await?;
        let desired: Vec<ResourceRecord> = records
            .iter()
            .map(|r| build_resource_record(&fqdn, r, ttl))
            .collect();

        let mut rems: Vec<ResourceRecord> = Vec::new();
        for current in &existing {
            if !desired.iter().any(|d| records_equal(d, current)) {
                rems.push(current.clone());
            }
        }
        let mut adds: Vec<ResourceRecord> = Vec::new();
        for want in &desired {
            if !existing.iter().any(|e| records_equal(want, e)) {
                adds.push(want.clone());
            }
        }

        if adds.is_empty() && rems.is_empty() {
            return Ok(());
        }

        let body = ZoneStream { adds, rems };
        self.client
            .post(self.stream_url(&domain))
            .with_body(body)?
            .send_raw()
            .await
            .map(|_| ())
    }

    pub(crate) async fn add_to_rrset(
        &self,
        name: impl IntoFqdn<'_>,
        record_type: DnsRecordType,
        ttl: u32,
        records: Vec<DnsRecord>,
        origin: impl IntoFqdn<'_>,
    ) -> crate::Result<()> {
        check_record_types(record_type, &records)?;
        if records.is_empty() {
            return Ok(());
        }
        let domain = origin.into_name();
        let fqdn = name.into_fqdn();

        let existing = self.find_existing(&domain, &fqdn, record_type).await?;
        let mut adds: Vec<ResourceRecord> = Vec::new();
        for record in &records {
            let candidate = build_resource_record(&fqdn, record, ttl);
            if existing.iter().any(|e| records_equal(&candidate, e)) {
                continue;
            }
            adds.push(candidate);
        }

        if adds.is_empty() {
            return Ok(());
        }

        let body = ZoneStream { adds, rems: vec![] };
        self.client
            .post(self.stream_url(&domain))
            .with_body(body)?
            .send_raw()
            .await
            .map(|_| ())
    }

    pub(crate) async fn remove_from_rrset(
        &self,
        name: impl IntoFqdn<'_>,
        record_type: DnsRecordType,
        records: Vec<DnsRecord>,
        origin: impl IntoFqdn<'_>,
    ) -> crate::Result<()> {
        check_record_types(record_type, &records)?;
        if records.is_empty() {
            return Ok(());
        }
        let domain = origin.into_name();
        let fqdn = name.into_fqdn();

        let existing = self.find_existing(&domain, &fqdn, record_type).await?;
        let mut rems: Vec<ResourceRecord> = Vec::new();
        for record in &records {
            let candidate = build_resource_record(&fqdn, record, 0);
            if let Some(found) = existing.iter().find(|e| records_equal(&candidate, e))
                && !rems.iter().any(|r| r == found)
            {
                rems.push(found.clone());
            }
        }

        if rems.is_empty() {
            return Ok(());
        }

        let body = ZoneStream { adds: vec![], rems };
        self.client
            .post(self.stream_url(&domain))
            .with_body(body)?
            .send_raw()
            .await
            .map(|_| ())
    }

    pub(crate) async fn list_rrset(
        &self,
        name: impl IntoFqdn<'_>,
        record_type: DnsRecordType,
        origin: impl IntoFqdn<'_>,
    ) -> crate::Result<Vec<DnsRecord>> {
        let domain = origin.into_name();
        let fqdn = name.into_fqdn();
        let existing = self.find_existing(&domain, &fqdn, record_type).await?;
        existing
            .into_iter()
            .map(|r| resource_record_to_dns_record(&r, record_type))
            .collect()
    }

    async fn find_existing(
        &self,
        domain: &str,
        fqdn: &str,
        record_type: DnsRecordType,
    ) -> crate::Result<Vec<ResourceRecord>> {
        let url = format!("{}/zone/{}/_search", self.endpoint, domain);
        let response = self
            .client
            .post(url)
            .with_raw_body("{}".to_string())
            .send_raw()
            .await
            .ok();
        let Some(body) = response else {
            return Ok(Vec::new());
        };
        if body.is_empty() {
            return Ok(Vec::new());
        }
        let parsed: DataZoneResponse = match serde_json::from_str(&body) {
            Ok(p) => p,
            Err(_) => return Ok(Vec::new()),
        };
        let target_name = strip_origin_from_name(fqdn, domain, Some("@"));
        let type_str = record_type.as_str();
        let mut matches = Vec::new();
        for zone in parsed.data {
            for r in zone.resource_records {
                let candidate_name = strip_origin_from_name(&r.name, domain, Some("@"));
                if candidate_name == target_name && r.record_type == type_str {
                    matches.push(r);
                }
            }
        }
        Ok(matches)
    }
}

fn records_equal(a: &ResourceRecord, b: &ResourceRecord) -> bool {
    a.record_type == b.record_type && a.value == b.value && a.pref == b.pref
}

fn check_record_types(expected: DnsRecordType, records: &[DnsRecord]) -> crate::Result<()> {
    for r in records {
        if r.as_type() != expected {
            return Err(Error::Api(format!(
                "RRSet record type mismatch: expected {}, got {}",
                expected.as_str(),
                r.as_type().as_str(),
            )));
        }
    }
    Ok(())
}

fn resource_record_to_dns_record(
    rr: &ResourceRecord,
    expected_type: DnsRecordType,
) -> crate::Result<DnsRecord> {
    Ok(match expected_type {
        DnsRecordType::A => DnsRecord::A(
            rr.value
                .parse()
                .map_err(|e| Error::Parse(format!("invalid A: {e}")))?,
        ),
        DnsRecordType::AAAA => DnsRecord::AAAA(
            rr.value
                .parse()
                .map_err(|e| Error::Parse(format!("invalid AAAA: {e}")))?,
        ),
        DnsRecordType::CNAME => DnsRecord::CNAME(autodns_strip_dot(&rr.value)),
        DnsRecordType::NS => DnsRecord::NS(autodns_strip_dot(&rr.value)),
        DnsRecordType::MX => DnsRecord::MX(MXRecord {
            exchange: autodns_strip_dot(&rr.value),
            priority: u16::try_from(rr.pref).unwrap_or(0),
        }),
        DnsRecordType::TXT => DnsRecord::TXT(rr.value.clone()),
        DnsRecordType::SRV => {
            let mut parts = rr.value.split_whitespace();
            let weight: u16 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::Parse(format!("invalid SRV: {}", rr.value)))?;
            let port: u16 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::Parse(format!("invalid SRV: {}", rr.value)))?;
            let target = parts
                .next()
                .ok_or_else(|| Error::Parse(format!("invalid SRV: {}", rr.value)))?
                .to_string();
            DnsRecord::SRV(SRVRecord {
                priority: u16::try_from(rr.pref).unwrap_or(0),
                weight,
                port,
                target: autodns_strip_dot(&target),
            })
        }
        DnsRecordType::TLSA => {
            let mut parts = rr.value.split_whitespace();
            let usage: u8 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::Parse(format!("invalid TLSA: {}", rr.value)))?;
            let selector: u8 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::Parse(format!("invalid TLSA: {}", rr.value)))?;
            let matching: u8 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::Parse(format!("invalid TLSA: {}", rr.value)))?;
            let hex = parts
                .next()
                .ok_or_else(|| Error::Parse(format!("invalid TLSA: {}", rr.value)))?;
            let cert_data = autodns_decode_hex(hex)?;
            DnsRecord::TLSA(TLSARecord {
                cert_usage: autodns_tlsa_cert_usage(usage)?,
                selector: autodns_tlsa_selector(selector)?,
                matching: autodns_tlsa_matching(matching)?,
                cert_data,
            })
        }
        DnsRecordType::CAA => DnsRecord::CAA(autodns_parse_caa(&rr.value)?),
    })
}

fn autodns_strip_dot(s: &str) -> String {
    s.strip_suffix('.').unwrap_or(s).to_string()
}

fn autodns_decode_hex(hex: &str) -> crate::Result<Vec<u8>> {
    if !hex.len().is_multiple_of(2) {
        return Err(Error::Parse(format!("invalid hex: {hex}")));
    }
    (0..hex.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).map_err(|e| Error::Parse(e.to_string())))
        .collect()
}

fn autodns_tlsa_cert_usage(value: u8) -> crate::Result<TlsaCertUsage> {
    Ok(match value {
        0 => TlsaCertUsage::PkixTa,
        1 => TlsaCertUsage::PkixEe,
        2 => TlsaCertUsage::DaneTa,
        3 => TlsaCertUsage::DaneEe,
        255 => TlsaCertUsage::Private,
        _ => return Err(Error::Parse(format!("unknown TLSA cert usage: {value}"))),
    })
}

fn autodns_tlsa_selector(value: u8) -> crate::Result<TlsaSelector> {
    Ok(match value {
        0 => TlsaSelector::Full,
        1 => TlsaSelector::Spki,
        255 => TlsaSelector::Private,
        _ => return Err(Error::Parse(format!("unknown TLSA selector: {value}"))),
    })
}

fn autodns_tlsa_matching(value: u8) -> crate::Result<TlsaMatching> {
    Ok(match value {
        0 => TlsaMatching::Raw,
        1 => TlsaMatching::Sha256,
        2 => TlsaMatching::Sha512,
        255 => TlsaMatching::Private,
        _ => return Err(Error::Parse(format!("unknown TLSA matching: {value}"))),
    })
}

fn autodns_parse_caa(value: &str) -> crate::Result<CAARecord> {
    let mut parts = value.splitn(3, ' ');
    let flags: u8 = parts
        .next()
        .and_then(|s| s.parse().ok())
        .ok_or_else(|| Error::Parse(format!("invalid CAA: {value}")))?;
    let tag = parts
        .next()
        .ok_or_else(|| Error::Parse(format!("invalid CAA: {value}")))?
        .to_string();
    let rest = parts
        .next()
        .ok_or_else(|| Error::Parse(format!("invalid CAA: {value}")))?
        .trim();
    let inner = rest
        .strip_prefix('"')
        .and_then(|s| s.strip_suffix('"'))
        .unwrap_or(rest);
    let issuer_critical = flags & 0x80 != 0;
    match tag.as_str() {
        "issue" => {
            let (name, options) = autodns_split_caa_value(inner);
            Ok(CAARecord::Issue {
                issuer_critical,
                name,
                options,
            })
        }
        "issuewild" => {
            let (name, options) = autodns_split_caa_value(inner);
            Ok(CAARecord::IssueWild {
                issuer_critical,
                name,
                options,
            })
        }
        "iodef" => Ok(CAARecord::Iodef {
            issuer_critical,
            url: inner.to_string(),
        }),
        other => Err(Error::Parse(format!("unknown CAA tag: {other}"))),
    }
}

fn autodns_split_caa_value(value: &str) -> (Option<String>, Vec<KeyValue>) {
    let mut iter = value.split(';').map(str::trim);
    let name_part = iter.next().unwrap_or("").trim().to_string();
    let name = if name_part.is_empty() {
        None
    } else {
        Some(name_part)
    };
    let options = iter
        .filter(|p| !p.is_empty())
        .map(|p| match p.split_once('=') {
            Some((k, v)) => KeyValue {
                key: k.trim().to_string(),
                value: v.trim().to_string(),
            },
            None => KeyValue {
                key: p.trim().to_string(),
                value: String::new(),
            },
        })
        .collect();
    (name, options)
}

fn build_resource_record(name: &str, record: &DnsRecord, ttl: u32) -> ResourceRecord {
    let (value, pref) = match record {
        DnsRecord::A(ip) => (ip.to_string(), 0),
        DnsRecord::AAAA(ip) => (ip.to_string(), 0),
        DnsRecord::CNAME(value) => (value.clone(), 0),
        DnsRecord::NS(value) => (value.clone(), 0),
        DnsRecord::MX(mx) => (mx.exchange.clone(), u32::from(mx.priority)),
        DnsRecord::TXT(value) => (value.clone(), 0),
        DnsRecord::SRV(srv) => (
            format!("{} {} {}", srv.weight, srv.port, srv.target),
            u32::from(srv.priority),
        ),
        DnsRecord::TLSA(tlsa) => (tlsa.to_string(), 0),
        DnsRecord::CAA(caa) => (caa.to_string(), 0),
    };
    ResourceRecord {
        name: name.to_string(),
        ttl,
        record_type: record.as_type().as_str().to_string(),
        value,
        pref,
    }
}