domain 0.12.0

A DNS 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
//! Importing from and (in future) exporting to a zone files.

use std::collections::{BTreeMap, HashMap};
use std::vec::Vec;

use crate::base::iana::{Class, Rtype};
use crate::base::name::{FlattenInto, ToName};
use crate::base::Name;
use crate::rdata::ZoneRecordData;
use crate::zonefile::inplace::{self, Entry};
use crate::zonetree::ZoneBuilder;
use crate::zonetree::{Rrset, SharedRr};

use super::error::{ContextError, RecordError, ZoneErrors};
use super::types::{StoredName, StoredRecord};

//------------ Zonefile ------------------------------------------------------

/// A parsed sanity checked representation of a zone file.
///
/// This type eases creation of a [`ZoneBuilder`] from a collection of
/// [`StoredRecord`]s, e.g.  and accepts only records that are valid within
/// the zone.
///
/// The zone origin and class may be specified explicitly or be derived from
/// the SOA record when inserted. The relationship of each resource record
/// with the zone is classified on insert, similar to that described by
/// [RFC 1034, section 4.2.1].
///
/// Getter functions provide insight into the classification results.
///
/// When ready the [`ZoneBuilder::try_from`] function can be used to convert
/// the parsed zone file into a pre-populated [`ZoneBuilder`].
///
/// # Usage
///
/// See the [zonetree] module docs for example usage.
///
/// [RFC 1034, section 4.2.1]:
///     https://datatracker.ietf.org/doc/html/rfc1034#section-4.2.1
/// [zonetree]: crate::zonetree
#[derive(Clone, Default)]
pub struct Zonefile {
    /// The name of the apex of the zone.
    origin: Option<StoredName>,

    /// The class of the zone.
    class: Option<Class>,

    /// The records for names that have regular RRsets attached to them.
    normal: Owners<Normal>,

    /// The records for names that are zone cuts.
    zone_cuts: Owners<ZoneCut>,

    /// The records for names that are CNAMEs.
    cnames: Owners<SharedRr>,

    /// Out of zone records.
    out_of_zone: Owners<Normal>,
}

impl Zonefile {
    /// Creates an empty in-memory zone file representation for the given apex
    /// and class.
    pub fn new(apex: StoredName, class: Class) -> Self {
        Zonefile {
            origin: Some(apex),
            class: Some(class),
            ..Default::default()
        }
    }
}

impl Zonefile {
    /// Sets the origin of the zone.
    ///
    /// If parsing a zone file one might call this method on encoutering an
    /// `$ORIGIN` directive.
    pub fn set_origin(&mut self, origin: StoredName) {
        self.origin = Some(origin)
    }

    /// Inserts the given record into the zone file.
    #[allow(clippy::result_large_err)]
    pub fn insert(
        &mut self,
        record: StoredRecord,
    ) -> Result<(), RecordError> {
        // If a zone apex and class were not provided via [`Self::new`], i.e.
        // we were created by [`Self::default`], require the first record to
        // be a SOA record and use its owner name and class as the zone apex
        // name and class.
        if self.origin.is_none() {
            if record.rtype() != Rtype::SOA {
                return Err(RecordError::MissingSoa(record));
            } else {
                let apex = record.owner().to_name();
                self.class = Some(record.class());
                self.origin = Some(apex);
            }
        }

        let (zone_apex, zone_class) =
            (self.origin().unwrap(), self.class().unwrap());

        if record.class() != zone_class {
            return Err(RecordError::ClassMismatch(record, zone_class));
        }

        if !record.owner().ends_with(zone_apex) {
            self.out_of_zone
                .entry(record.owner().clone())
                .insert(record);
            Ok(())
        } else {
            match record.rtype() {
                // An Name Server (NS) record at the apex is a nameserver RR
                // that indicates a server for the zone. An NS record is only
                // an indication of a zone cut when it is NOT at the apex.
                //
                // A Delegation Signer (DS) record can only appear within the
                // parent zone and refer to a child zone, a DS record cannot
                // therefore appear at the apex.
                Rtype::NS | Rtype::DS if record.owner() != zone_apex => {
                    // Zone cuts can only be made when records already exist
                    // at the owner if all such records are glue (records that
                    // ease resolution of a zone cut name to an address).
                    let incompatible_normal_record = self
                        .normal
                        .get(record.owner())
                        .and_then(|normal| normal.first_non_glue());

                    if let Some((&rtype, _)) = incompatible_normal_record {
                        Err(RecordError::IllegalZoneCut(record, rtype))
                    } else if self.cnames.contains(record.owner()) {
                        Err(RecordError::IllegalZoneCut(record, Rtype::CNAME))
                    } else {
                        self.zone_cuts
                            .entry(record.owner().clone())
                            .insert(record);
                        Ok(())
                    }
                }
                Rtype::CNAME => {
                    if let Some(normal_records) =
                        self.normal.get(record.owner())
                    {
                        let rtype = normal_records.sample_rtype().unwrap();
                        Err(RecordError::IllegalCname(record, rtype))
                    } else if let Some(zone_cut) =
                        self.zone_cuts.get(record.owner())
                    {
                        let rtype = zone_cut.sample_rtype().unwrap();
                        Err(RecordError::IllegalCname(record, rtype))
                    } else if self.cnames.contains(record.owner()) {
                        Err(RecordError::MultipleCnames(record))
                    } else {
                        self.cnames
                            .insert(record.owner().clone(), record.into());
                        Ok(())
                    }
                }
                _ => {
                    // Only glue records can only be added at the same owner as
                    // a zone cut.
                    let incompatible_zone_cut = match record.rtype().is_glue()
                    {
                        true => None,
                        false => self.zone_cuts.get(record.owner()),
                    };

                    if let Some(zone_cut) = incompatible_zone_cut {
                        let rtype = zone_cut.sample_rtype().unwrap();
                        Err(RecordError::IllegalRecord(record, rtype))
                    } else if self.cnames.contains(record.owner()) {
                        Err(RecordError::IllegalRecord(record, Rtype::CNAME))
                    } else {
                        self.normal
                            .entry(record.owner().clone())
                            .insert(record);
                        Ok(())
                    }
                }
            }
        }
    }
}

impl Zonefile {
    /// The [origin] of the zone.
    ///
    /// [origin]: https://datatracker.ietf.org/doc/html/rfc9499#section-7-2.8
    pub fn origin(&self) -> Option<&StoredName> {
        self.origin.as_ref()
    }

    /// The [class] of the zone.
    ///
    /// [class]: https://datatracker.ietf.org/doc/html/rfc9499#section-4-2.2
    pub fn class(&self) -> Option<Class> {
        self.class
    }

    /// The collection of normal records in the zone.
    ///
    /// Normal records are all records in the zone that are neither top of
    /// zone administrative records, zone cuts nor glue records.
    pub fn normal(&self) -> &Owners<Normal> {
        &self.normal
    }

    /// The collection of [zone cut] records in the zone.
    ///
    /// [zone cut]: https://datatracker.ietf.org/doc/html/rfc9499#section-7-2.16
    pub fn zone_cuts(&self) -> &Owners<ZoneCut> {
        &self.zone_cuts
    }

    /// The collection of [CNAME] records in the zone.
    ///
    /// [CNAME]: https://datatracker.ietf.org/doc/html/rfc9499#section-7-2.16
    pub fn cnames(&self) -> &Owners<SharedRr> {
        &self.cnames
    }

    /// The collection of records that lie outside the zone.
    ///
    /// In a valid zone this collection will be empty.
    pub fn out_of_zone(&self) -> &Owners<Normal> {
        &self.out_of_zone
    }
}

impl TryFrom<Zonefile> for ZoneBuilder {
    type Error = ZoneErrors<ContextError>;

    fn try_from(mut zonefile: Zonefile) -> Result<Self, Self::Error> {
        let mut builder = ZoneBuilder::new(
            zonefile.origin.unwrap(),
            zonefile.class.unwrap(),
        );
        let mut errors = ZoneErrors::<ContextError>::default();

        // Insert all the zone cuts first. Fish out potential glue records
        // from the normal or out-of-zone records.
        for (name, cut) in zonefile.zone_cuts.into_iter() {
            let ns = match cut.ns {
                Some(ns) => ns.into_shared(),
                None => {
                    errors.add_error(name, ContextError::MissingNs);
                    continue;
                }
            };
            let ds = cut.ds.map(Rrset::into_shared);
            let mut glue = vec![];
            for rdata in ns.data() {
                if let ZoneRecordData::Ns(ns) = rdata {
                    glue.append(
                        &mut zonefile.normal.collect_glue(ns.nsdname()),
                    );
                }
            }

            if let Err(err) = builder.insert_zone_cut(&name, ns, ds, glue) {
                errors.add_error(name, ContextError::InvalidZonecut(err))
            }
        }

        // Now insert all the CNAMEs.
        for (name, rrset) in zonefile.cnames.into_iter() {
            if let Err(err) = builder.insert_cname(&name, rrset) {
                errors.add_error(name, ContextError::InvalidCname(err))
            }
        }

        // Finally, all the normal records.
        for (name, rrsets) in zonefile.normal.into_iter() {
            for (rtype, rrset) in rrsets.into_iter() {
                if builder.insert_rrset(&name, rrset.into_shared()).is_err() {
                    errors.add_error(
                        name.clone(),
                        ContextError::OutOfZone(rtype),
                    );
                }
            }
        }

        // If there are out-of-zone records left, we will error to avoid
        // surprises.
        for (name, rrsets) in zonefile.out_of_zone.into_iter() {
            for (rtype, _) in rrsets.into_iter() {
                errors
                    .add_error(name.clone(), ContextError::OutOfZone(rtype));
            }
        }

        errors.unwrap().map(|_| builder)
    }
}

//--- TryFrom<inplace::Zonefile>

impl TryFrom<inplace::Zonefile> for Zonefile {
    type Error = ZoneErrors<RecordError>;

    fn try_from(source: inplace::Zonefile) -> Result<Self, Self::Error> {
        let mut zonefile = Zonefile::default();
        let mut errors = ZoneErrors::<RecordError>::default();

        for res in source {
            match res.map_err(RecordError::MalformedRecord) {
                Ok(Entry::Record(r)) => {
                    let stored_rec = r.flatten_into();
                    let name = stored_rec.owner().clone();
                    if let Err(err) = zonefile.insert(stored_rec) {
                        errors.add_error(name, err);
                    }
                }

                Ok(Entry::Include { .. }) => {
                    // Not supported at this time.
                }

                Err(err) => {
                    match err.owner() {
                        Some(name) => errors.add_error(name.clone(), err),
                        None => errors.add_error(Name::root_bytes(), err),
                    }
                    // The inplace::Zonefile parser is not capable of
                    // continuing after an error, so we immediately return for
                    // now.
                    return Err(errors);
                }
            }
        }

        if errors.is_empty() {
            Ok(zonefile)
        } else {
            Err(errors)
        }
    }
}

//------------ Owners --------------------------------------------------------

/// A set of records of a common type within a zone file.
#[derive(Clone)]
pub struct Owners<Content> {
    owners: BTreeMap<StoredName, Content>,
}

impl<Content> Owners<Content> {
    fn contains(&self, name: &StoredName) -> bool {
        self.owners.contains_key(name)
    }

    fn get(&self, name: &StoredName) -> Option<&Content> {
        self.owners.get(name)
    }

    fn insert(&mut self, name: StoredName, content: Content) -> bool {
        use std::collections::btree_map::Entry;

        match self.owners.entry(name) {
            Entry::Occupied(_) => false,
            Entry::Vacant(vacant) => {
                vacant.insert(content);
                true
            }
        }
    }

    fn entry(&mut self, name: StoredName) -> &mut Content
    where
        Content: Default,
    {
        self.owners.entry(name).or_default()
    }

    fn into_iter(self) -> impl Iterator<Item = (StoredName, Content)> {
        self.owners.into_iter()
    }
}

impl Owners<Normal> {
    fn collect_glue(&mut self, name: &StoredName) -> Vec<StoredRecord> {
        let mut glue_records = vec![];

        // https://www.rfc-editor.org/rfc/rfc9471.html
        // 2.1. Glue for In-Domain Name Servers

        // For each NS delegation find the names of the nameservers the NS
        // records point to, and then see if the A/AAAA records for this names
        // are defined in the authoritative (normal) data for this zone, and
        // if so extract them.
        if let Some(normal) = self.owners.get(name) {
            // Now see if A/AAAA records exists for the name in
            // this zone.
            for (_rtype, rrset) in
                normal.records.iter().filter(|(&rtype, _)| rtype.is_glue())
            {
                for rdata in rrset.data() {
                    let glue_record = StoredRecord::new(
                        name.clone(),
                        Class::IN,
                        rrset.ttl(),
                        rdata.clone(),
                    );
                    glue_records.push(glue_record);
                }
            }
        }

        glue_records
    }
}

impl<Content> Default for Owners<Content> {
    fn default() -> Self {
        Owners {
            owners: Default::default(),
        }
    }
}

//------------ Normal --------------------------------------------------------

/// A collection of "normal" zone file records.
///
/// I.e. zone file records that are not CNAMEs or zone cuts.
#[derive(Clone, Default)]
pub struct Normal {
    records: HashMap<Rtype, Rrset>,
}

impl Normal {
    fn insert(&mut self, record: StoredRecord) {
        use std::collections::hash_map::Entry;

        match self.records.entry(record.rtype()) {
            Entry::Occupied(mut occupied) => {
                occupied.get_mut().push_record(record)
            }
            Entry::Vacant(vacant) => {
                vacant.insert(record.into());
            }
        }
    }

    fn into_iter(self) -> impl Iterator<Item = (Rtype, Rrset)> {
        self.records.into_iter()
    }

    fn sample_rtype(&self) -> Option<Rtype> {
        self.records.iter().next().map(|(&rtype, _)| rtype)
    }

    fn first_non_glue(&self) -> Option<(&Rtype, &Rrset)> {
        self.records.iter().find(|(rtype, _)| !rtype.is_glue())
    }
}

//------------ ZoneCut -------------------------------------------------------

/// The set of records that comprise a zone cut within a zone file.
#[derive(Clone, Default)]
pub struct ZoneCut {
    ns: Option<Rrset>,
    ds: Option<Rrset>,
}

impl ZoneCut {
    fn insert(&mut self, record: StoredRecord) {
        match record.rtype() {
            Rtype::NS => {
                if let Some(ns) = self.ns.as_mut() {
                    ns.push_record(record)
                } else {
                    self.ns = Some(record.into())
                }
            }
            Rtype::DS => {
                if let Some(ds) = self.ds.as_mut() {
                    ds.push_record(record)
                } else {
                    self.ds = Some(record.into())
                }
            }
            _ => panic!("inserting wrong rtype to zone cut"),
        }
    }

    fn sample_rtype(&self) -> Option<Rtype> {
        self.ds.as_ref().or(self.ns.as_ref()).map(|r| r.rtype())
    }
}