dns.rs 12.3 KB
Newer Older
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
1
use std::net::{Ipv6Addr, Ipv4Addr};
2
use std::fmt;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
3
4

use serde::{Serialize, Deserialize};
5
6
use trust_dns_client::serialize::binary::BinEncoder;
use base64;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
7

8
use super::trust_dns_types;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
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

#[derive(Deserialize, Serialize)]
pub enum RecordType {
    A,
    AAAA,
    ANAME,
    ANY,
    AXFR,
    CAA,
    CNAME,
    HINFO,
    HTTPS,
    IXFR,
    MX,
    NAPTR,
    NS,
    NULL,
    OPENPGPKEY,
    OPT,
    PTR,
    SOA,
    SRV,
    SSHFP,
    SVCB,
    TLSA,
    TXT,
    // dnssec
    DNSKEY,
    DS,
    KEY,
    NSEC,
    NSEC3,
    NSEC3PARAM,
    RRSIG,
    SIG,

    Unknown(u16),
    ZERO,
}


impl From<trust_dns_types::RecordType> for RecordType {
    fn from(record_type: trust_dns_types::RecordType) -> RecordType {
        match record_type {
            trust_dns_types::RecordType::A => RecordType::A,
            trust_dns_types::RecordType::AAAA => RecordType::AAAA,
            trust_dns_types::RecordType::ANAME => RecordType::ANAME,
            trust_dns_types::RecordType::ANY => RecordType::ANY,
            trust_dns_types::RecordType::AXFR => RecordType::AXFR,
            trust_dns_types::RecordType::CAA => RecordType::CAA,
            trust_dns_types::RecordType::CNAME => RecordType::CNAME,
            trust_dns_types::RecordType::HINFO => RecordType::HINFO,
            trust_dns_types::RecordType::HTTPS => RecordType::HTTPS,
            trust_dns_types::RecordType::IXFR => RecordType::IXFR,
            trust_dns_types::RecordType::MX => RecordType::MX,
            trust_dns_types::RecordType::NAPTR => RecordType::NAPTR,
            trust_dns_types::RecordType::NS => RecordType::NS,
            trust_dns_types::RecordType::NULL => RecordType::NULL,
            trust_dns_types::RecordType::OPENPGPKEY => RecordType::OPENPGPKEY,
            trust_dns_types::RecordType::OPT => RecordType::OPT,
            trust_dns_types::RecordType::PTR => RecordType::PTR,
            trust_dns_types::RecordType::SOA => RecordType::SOA,
            trust_dns_types::RecordType::SRV => RecordType::SRV,
            trust_dns_types::RecordType::SSHFP => RecordType::SSHFP,
            trust_dns_types::RecordType::SVCB => RecordType::SVCB,
            trust_dns_types::RecordType::TLSA => RecordType::TLSA,
            trust_dns_types::RecordType::TXT => RecordType::TXT,

            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::DNSKEY) => RecordType::DNSKEY,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::DS) => RecordType::DS,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::KEY) => RecordType::KEY,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC) => RecordType::NSEC,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC3) => RecordType::NSEC3,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC3PARAM) => RecordType::NSEC3PARAM,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::RRSIG) => RecordType::RRSIG,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::SIG) => RecordType::SIG,
            trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::Unknown(r#type)) => RecordType::Unknown(r#type),

            trust_dns_types::RecordType::Unknown(r#type) => RecordType::Unknown(r#type),
            trust_dns_types::RecordType::ZERO => RecordType::ZERO,
        }
    }
}

impl Into<trust_dns_types::RecordType> for RecordType {
    fn into(self) -> trust_dns_types::RecordType {
        match self {
            RecordType::A => trust_dns_types::RecordType::A,
            RecordType::AAAA => trust_dns_types::RecordType::AAAA,
            RecordType::ANAME => trust_dns_types::RecordType::ANAME,
            RecordType::ANY => trust_dns_types::RecordType::ANY,
            RecordType::AXFR => trust_dns_types::RecordType::AXFR,
            RecordType::CAA => trust_dns_types::RecordType::CAA,
            RecordType::CNAME => trust_dns_types::RecordType::CNAME,
            RecordType::HINFO => trust_dns_types::RecordType::HINFO,
            RecordType::HTTPS => trust_dns_types::RecordType::HTTPS,
            RecordType::IXFR => trust_dns_types::RecordType::IXFR,
            RecordType::MX => trust_dns_types::RecordType::MX,
            RecordType::NAPTR => trust_dns_types::RecordType::NAPTR,
            RecordType::NS => trust_dns_types::RecordType::NS,
            RecordType::NULL => trust_dns_types::RecordType::NULL,
            RecordType::OPENPGPKEY => trust_dns_types::RecordType::OPENPGPKEY,
            RecordType::OPT => trust_dns_types::RecordType::OPT,
            RecordType::PTR => trust_dns_types::RecordType::PTR,
            RecordType::SOA => trust_dns_types::RecordType::SOA,
            RecordType::SRV => trust_dns_types::RecordType::SRV,
            RecordType::SSHFP => trust_dns_types::RecordType::SSHFP,
            RecordType::SVCB => trust_dns_types::RecordType::SVCB,
            RecordType::TLSA => trust_dns_types::RecordType::TLSA,
            RecordType::TXT => trust_dns_types::RecordType::TXT,

            RecordType::DNSKEY => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::DNSKEY),
            RecordType::DS => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::DS),
            RecordType::KEY => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::KEY),
            RecordType::NSEC => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC),
            RecordType::NSEC3 => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC3),
            RecordType::NSEC3PARAM => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::NSEC3PARAM),
            RecordType::RRSIG => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::RRSIG),
            RecordType::SIG => trust_dns_types::RecordType::DNSSEC(trust_dns_types::DNSSECRecordType::SIG),

            RecordType::Unknown(r#type) => trust_dns_types::RecordType::Unknown(r#type),
            RecordType::ZERO => trust_dns_types::RecordType::ZERO,
        }
    }
}


#[derive(Deserialize, Serialize)]
#[serde(tag = "Type")]
138
#[serde(rename_all = "UPPERCASE")]
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
139
140
141
142
143
144
145
146
147
pub enum RData {
    #[serde(rename_all = "PascalCase")]
    A {
        address: Ipv4Addr
    },
    #[serde(rename_all = "PascalCase")]
    AAAA {
        address: Ipv6Addr
    },
148
149
150
151
152
153
154
155
156
157
    #[serde(rename_all = "PascalCase")]
    CAA {
        issuer_critical: bool,
        value: String,
        property_tag: String,
    },
    #[serde(rename_all = "PascalCase")]
    CNAME {
        target: String
    },
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
158
159
    // HINFO(HINFO),
    // HTTPS(SVCB),
160
161
162
163
164
    #[serde(rename_all = "PascalCase")]
    MX {
        preference: u16,
        mail_exchanger: String
    },
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
165
    // NAPTR(NAPTR),
166
167
168
169
170
171
172
173
    #[serde(rename_all = "PascalCase")]
    NULL {
        data: String
    },
    #[serde(rename_all = "PascalCase")]
    NS {
        target: String
    },
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
174
175
    // OPENPGPKEY(OPENPGPKEY),
    // OPT(OPT),
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    #[serde(rename_all = "PascalCase")]
    PTR {
        target: String
    },
    #[serde(rename_all = "PascalCase")]
    SOA {
        master_server_name: String,
        maintainer_name: String,
        refresh: i32,
        retry: i32,
        expire: i32,
        minimum: u32,
        serial: u32
    },
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
190
191
192
193
194
    // SRV(SRV),
    // SSHFP(SSHFP),
    // SVCB(SVCB),
    // TLSA(TLSA),
    // TXT(TXT),
195
196
197
198
199

    // TODO: Eventually allow deserialization of DNSSEC records
    #[serde(skip)]
    DNSSEC(trust_dns_types::DNSSECRData),
    #[serde(rename_all = "PascalCase")]
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
200
201
    Unknown {
        code: u16,
202
        data: String,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
203
204
205
206
207
208
209
210
211
    },
    // ZERO,
}

impl From<trust_dns_types::RData> for RData {
    fn from(rdata: trust_dns_types::RData) -> RData {
        match rdata {
            trust_dns_types::RData::A(address) => RData::A { address },
            trust_dns_types::RData::AAAA(address) => RData::AAAA { address },
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
            // Still a draft, no iana number yet, I don't to put something that is not currently supported so that's why NULL and not unknown.
            // TODO: probably need better error here, I don't know what to do about that as this would require to change the From for something else.
            // (empty data because I'm lazy)
            trust_dns_types::RData::ANAME(_) =>  RData::NULL {
                data: String::new()
            },
            trust_dns_types::RData::CNAME(target) => RData::CNAME {
                target: target.to_utf8()
            },
            trust_dns_types::RData::CAA(caa) => RData::CAA {
                issuer_critical: caa.issuer_critical(),
                value: format!("{}", CAAValue(caa.value())),
                property_tag: caa.tag().as_str().to_string(),
            },
            trust_dns_types::RData::MX(mx) => RData::MX {
                preference: mx.preference(),
                mail_exchanger: mx.exchange().to_utf8()
            },
            trust_dns_types::RData::NULL(null) =>  RData::NULL {
                data: base64::encode(null.anything().map(|data| data.to_vec()).unwrap_or_default())
            },
            trust_dns_types::RData::NS(target) => RData::NS {
                target: target.to_utf8()
            },
            trust_dns_types::RData::PTR(target) => RData::PTR {
                target: target.to_utf8()
            },
            trust_dns_types::RData::SOA(soa) => RData::SOA {
                master_server_name: soa.mname().to_utf8(),
                maintainer_name: soa.rname().to_utf8(),
                refresh: soa.refresh(),
                retry: soa.retry(),
                expire: soa.expire(),
                minimum: soa.minimum(),
                serial: soa.serial()
            },
            trust_dns_types::RData::DNSSEC(data) => RData::DNSSEC(data),
            rdata => {
                let code = rdata.to_record_type().into();
                let mut data = Vec::new();
                let mut encoder = BinEncoder::new(&mut data);
                rdata.emit(&mut encoder).expect("could not encode data");

                RData::Unknown {
                    code,
                    data: base64::encode(data),
                }
            }
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
260
261
262
263
        }
    }
}

264
struct CAAValue<'a>(&'a trust_dns_types::caa::Value);
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
265

266
267
268
269
270
271
272
// trust_dns Display implementation panics if no parameters
// Implementation based on caa::emit_value
// Also the quotes are strips to render in JSON
impl<'a> fmt::Display for CAAValue<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self.0 {
            trust_dns_types::caa::Value::Issuer(name, parameters) => {
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
273

274
275
276
                if let Some(name) = name {
                    write!(f, "{}", name)?;
                }
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
277

278
279
280
                if name.is_none() && parameters.is_empty() {
                    write!(f, ";")?;
                }
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
281

282
283
284
285
286
287
                for value in parameters {
                    write!(f, "; {}", value)?;
                }
            }
            trust_dns_types::caa::Value::Url(url) => write!(f, "{}", url)?,
            trust_dns_types::caa::Value::Unknown(v) => write!(f, "{:?}", v)?,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
288
        }
289
        Ok(())
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
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
    }
}

#[derive(Deserialize, Serialize)]
pub enum DNSClass {
    IN,
    CH,
    HS,
    NONE,
    ANY,
    OPT(u16),
}

impl From<trust_dns_types::DNSClass> for DNSClass {
    fn from(dns_class: trust_dns_types::DNSClass) -> DNSClass {
        match dns_class {
            trust_dns_types::DNSClass::IN => DNSClass::IN,
            trust_dns_types::DNSClass::CH => DNSClass::CH,
            trust_dns_types::DNSClass::HS => DNSClass::HS,
            trust_dns_types::DNSClass::NONE => DNSClass::NONE,
            trust_dns_types::DNSClass::ANY => DNSClass::ANY,
            trust_dns_types::DNSClass::OPT(v) => DNSClass::OPT(v),
        }
    }
}


#[derive(Deserialize, Serialize)]
pub struct Record {
    #[serde(rename = "Name")]
320
    pub name: String,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
321
322
323
    //#[serde(rename = "Type")]
    //rr_type: RecordType,
    #[serde(rename = "Class")]
324
    pub dns_class: DNSClass,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
325
    #[serde(rename = "TTL")]
326
    pub ttl: u32,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
327
    #[serde(flatten)]
328
    pub rdata: RData,
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
329
330
331
332
333
}

impl From<trust_dns_types::Record> for Record {
    fn from(record: trust_dns_types::Record) -> Record {
        Record {
334
            name: record.name().to_utf8(),
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
335
336
337
338
339
340
341
            //rr_type: record.rr_type().into(),
            dns_class: record.dns_class().into(),
            ttl: record.ttl(),
            rdata: record.into_data().into(),
        }
    }
}