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
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
//! Cached DNS lookup helpers on [`MessageAuthenticator`].
use super::{
DnsEntry, DnssecStatus, IpLookupStrategy, Mx, Negative, RecordSet, ResolverCache, ToFqdn,
TxtRecord, TxtRecordParser, UnwrapTxtRecord,
};
use crate::{Error, MessageAuthenticator};
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
sync::Arc,
};
impl MessageAuthenticator {
/// Queries the TXT records of `key` and returns their contents
/// concatenated into one byte string, without parsing or caching.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn txt_raw_lookup(&self, key: impl ToFqdn) -> crate::Result<Vec<u8>> {
let key = key.to_fqdn();
Ok(self
.query_txt(key.as_ref())
.await?
.entry
.into_iter()
.flatten()
.collect())
}
/// Queries the TXT records of `key` and returns the first one that parses
/// as `T` (for example [`SpfRecord`](crate::spf::SpfRecord),
/// [`DmarcRecord`](crate::dmarc::DmarcRecord) or
/// [`TlsRptRecord`](crate::mta_sts::TlsRptRecord)).
///
/// When `cache` is set, a cached entry is returned without querying, and
/// the parse outcome (the record or the parse error) is stored after a
/// query. A negative answer that carries an SOA record is stored as
/// [`TxtRecord::Error`] until its negative TTL expires.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
/// When no record parses as `T`, returns the error of the last parse
/// attempt (usually [`DnsError::InvalidRecordType`](crate::DnsError::InvalidRecordType)),
/// or `InvalidRecordType` when the answer holds no TXT data.
pub async fn txt_lookup<T: TxtRecordParser + Into<TxtRecord> + UnwrapTxtRecord>(
&self,
key: impl ToFqdn,
cache: Option<&impl ResolverCache<Box<str>, TxtRecord>>,
) -> crate::Result<Arc<T>> {
let key = key.to_fqdn();
if let Some(value) = cache.as_ref().and_then(|c| c.get::<str>(key.as_ref())) {
return T::unwrap_txt(value);
}
let DnsEntry {
entry: records,
expires,
} = match self.resolve_txt(key.as_ref()).await {
Ok(entry) => entry,
Err(err) => {
return Err(self.cache_negative(
err,
cache,
|| key.into_owned().into_boxed_str(),
|negative| TxtRecord::Error(negative.into()),
));
}
};
let result: TxtRecord = T::parse_records(&records).into();
if let Some(cache) = cache {
cache.insert(key.into_owned().into_boxed_str(), result.clone(), expires);
}
T::unwrap_txt(result)
}
/// Queries the MX records of `key` (RFC 5321 Section 5.1).
///
/// Records are grouped by preference into [`Mx`] values sorted by
/// ascending preference. When `cache` is set, it is consulted before and
/// updated after the query; a negative answer that carries an SOA record
/// is stored as `Err(`[`Negative`]`)` until its negative TTL expires.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn mx_lookup(
&self,
key: impl ToFqdn,
cache: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Mx>, Negative>>>,
) -> crate::Result<RecordSet<Mx>> {
let key = key.to_fqdn();
if let Some(value) = cache.as_ref().and_then(|c| c.get::<str>(key.as_ref())) {
return value.map_err(Error::from);
}
let DnsEntry {
entry: mx_records,
expires,
} = match self.resolve_mx(key.as_ref()).await {
Ok(entry) => entry,
Err(err) => {
return Err(self.cache_negative(
err,
cache,
|| key.into_owned().into_boxed_str(),
Err,
));
}
};
let mut records: Vec<(u16, Vec<Box<str>>)> = Vec::with_capacity(mx_records.len());
for (preference, exchange) in mx_records {
if let Some(record) = records.iter_mut().find(|r| r.0 == preference) {
record.1.push(exchange);
} else {
records.push((preference, vec![exchange]));
}
}
records.sort_unstable_by_key(|a| a.0);
let records: Arc<[Mx]> = records
.into_iter()
.map(|(preference, exchanges)| Mx {
preference,
exchanges: exchanges.into_boxed_slice(),
})
.collect::<Arc<[Mx]>>();
let records = RecordSet {
records,
dnssec_status: DnssecStatus::Indeterminate,
};
if let Some(cache) = cache {
cache.insert(
key.into_owned().into_boxed_str(),
Ok(records.clone()),
expires,
);
}
Ok(records)
}
/// Queries the A records of `key`. When `cache` is set, it is consulted
/// before and updated after the query; a negative answer that carries an
/// SOA record is stored as `Err(`[`Negative`]`)` until its negative TTL
/// expires.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn ipv4_lookup(
&self,
key: impl ToFqdn,
cache: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv4Addr>, Negative>>>,
) -> crate::Result<RecordSet<Ipv4Addr>> {
let key = key.to_fqdn();
if let Some(value) = cache.as_ref().and_then(|c| c.get::<str>(key.as_ref())) {
return value.map_err(Error::from);
}
let ipv4_lookup = match self.resolve_ipv4(key.as_ref()).await {
Ok(entry) => entry,
Err(err) => {
return Err(self.cache_negative(
err,
cache,
|| key.into_owned().into_boxed_str(),
Err,
));
}
};
let records = RecordSet {
records: ipv4_lookup.entry,
dnssec_status: DnssecStatus::Indeterminate,
};
if let Some(cache) = cache {
cache.insert(
key.into_owned().into_boxed_str(),
Ok(records.clone()),
ipv4_lookup.expires,
);
}
Ok(records)
}
/// Queries the A records of `key` without caching. `key` is queried as
/// given, without conversion to a fully qualified name. Returns the
/// addresses with their expiry time.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn ipv4_lookup_raw(&self, key: &str) -> crate::Result<DnsEntry<Arc<[Ipv4Addr]>>> {
self.resolve_ipv4(key).await.map_err(Error::from)
}
/// Queries the AAAA records of `key`. When `cache` is set, it is consulted
/// before and updated after the query; a negative answer that carries an
/// SOA record is stored as `Err(`[`Negative`]`)` until its negative TTL
/// expires.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn ipv6_lookup(
&self,
key: impl ToFqdn,
cache: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv6Addr>, Negative>>>,
) -> crate::Result<RecordSet<Ipv6Addr>> {
let key = key.to_fqdn();
if let Some(value) = cache.as_ref().and_then(|c| c.get::<str>(key.as_ref())) {
return value.map_err(Error::from);
}
let ipv6_lookup = match self.resolve_ipv6(key.as_ref()).await {
Ok(entry) => entry,
Err(err) => {
return Err(self.cache_negative(
err,
cache,
|| key.into_owned().into_boxed_str(),
Err,
));
}
};
let records = RecordSet {
records: ipv6_lookup.entry,
dnssec_status: DnssecStatus::Indeterminate,
};
if let Some(cache) = cache {
cache.insert(
key.into_owned().into_boxed_str(),
Ok(records.clone()),
ipv6_lookup.expires,
);
}
Ok(records)
}
/// Queries the AAAA records of `key` without caching. `key` is queried as
/// given, without conversion to a fully qualified name. Returns the
/// addresses with their expiry time.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the name has no records of this type, [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name.
pub async fn ipv6_lookup_raw(&self, key: &str) -> crate::Result<DnsEntry<Arc<[Ipv6Addr]>>> {
self.resolve_ipv6(key).await.map_err(Error::from)
}
/// Resolves `key` to at most `max_results` IP addresses, querying A and
/// AAAA records in the order given by `strategy`.
///
/// With a fallback strategy ([`IpLookupStrategy::Ipv4thenIpv6`] or
/// [`IpLookupStrategy::Ipv6thenIpv4`]), any error from the first query
/// triggers the second one. The caches are consulted before and updated
/// after each query.
///
/// # Errors
///
/// Returns the error of the last query performed. See
/// [`ipv4_lookup`](Self::ipv4_lookup) for the possible errors.
pub async fn ip_lookup(
&self,
key: &str,
mut strategy: IpLookupStrategy,
max_results: usize,
cache_ipv4: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv4Addr>, Negative>>>,
cache_ipv6: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv6Addr>, Negative>>>,
) -> crate::Result<Vec<IpAddr>> {
loop {
match strategy {
IpLookupStrategy::Ipv4Only | IpLookupStrategy::Ipv4thenIpv6 => {
match (self.ipv4_lookup(key, cache_ipv4).await, strategy) {
(Ok(result), _) => {
return Ok(result
.records
.iter()
.take(max_results)
.copied()
.map(IpAddr::from)
.collect());
}
(Err(err), IpLookupStrategy::Ipv4Only) => return Err(err),
_ => {
strategy = IpLookupStrategy::Ipv6Only;
}
}
}
IpLookupStrategy::Ipv6Only | IpLookupStrategy::Ipv6thenIpv4 => {
match (self.ipv6_lookup(key, cache_ipv6).await, strategy) {
(Ok(result), _) => {
return Ok(result
.records
.iter()
.take(max_results)
.copied()
.map(IpAddr::from)
.collect());
}
(Err(err), IpLookupStrategy::Ipv6Only) => return Err(err),
_ => {
strategy = IpLookupStrategy::Ipv4Only;
}
}
}
}
}
}
/// Queries the PTR records of `addr` (reverse DNS). Host names are
/// returned lowercased. When `cache` is set, it is consulted before and
/// updated after the query; a negative answer that carries an SOA record
/// is stored as `Err(`[`Negative`]`)` until its negative TTL expires.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::RecordNotFound`](crate::DnsError::RecordNotFound)
/// when the address has no PTR record, or [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when the query fails.
pub async fn ptr_lookup(
&self,
addr: IpAddr,
cache: Option<&impl ResolverCache<IpAddr, Result<RecordSet<Box<str>>, Negative>>>,
) -> crate::Result<RecordSet<Box<str>>> {
if let Some(value) = cache.as_ref().and_then(|c| c.get(&addr)) {
return value.map_err(Error::from);
}
let DnsEntry { entry, expires } = match self.resolve_ptr(addr).await {
Ok(entry) => entry,
Err(err) => return Err(self.cache_negative(err, cache, || addr, Err)),
};
let ptr = RecordSet {
records: entry,
dnssec_status: DnssecStatus::Indeterminate,
};
if let Some(cache) = cache {
cache.insert(addr, Ok(ptr.clone()), expires);
}
Ok(ptr)
}
/// Returns `true` if `key` has at least one A or AAAA record, as needed by
/// the SPF `exists` mechanism (RFC 7208 Section 5.7).
///
/// Returns `true` without querying when a cache holds an A or AAAA answer
/// with records, and `false` without querying when the caches hold a
/// negative or empty answer for both types. Query results are not added
/// to the caches.
///
/// # Errors
///
/// Returns [`Error::Dns`] with [`DnsError::Resolver`](crate::DnsError::Resolver)
/// when a query fails, or [`Error::Parse`] when the name is not a valid
/// DNS name. A name without records is not an error.
pub async fn exists(
&self,
key: impl ToFqdn,
cache_ipv4: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv4Addr>, Negative>>>,
cache_ipv6: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv6Addr>, Negative>>>,
) -> crate::Result<bool> {
let key = key.to_fqdn();
match cached_exists(key.as_ref(), cache_ipv4, cache_ipv6) {
Some(exists) => Ok(exists),
None => self.resolve_exists(key.as_ref()).await,
}
}
}
fn cached_exists(
key: &str,
cache_ipv4: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv4Addr>, Negative>>>,
cache_ipv6: Option<&impl ResolverCache<Box<str>, Result<RecordSet<Ipv6Addr>, Negative>>>,
) -> Option<bool> {
let ipv4 = cache_ipv4
.and_then(|cache| cache.get::<str>(key))
.map(|entry| entry.is_ok_and(|set| !set.records.is_empty()));
if ipv4 == Some(true) {
return Some(true);
}
let ipv6 = cache_ipv6
.and_then(|cache| cache.get::<str>(key))
.map(|entry| entry.is_ok_and(|set| !set.records.is_empty()));
match (ipv4, ipv6) {
(_, Some(true)) => Some(true),
(Some(false), Some(false)) => Some(false),
_ => None,
}
}