lazydns 0.3.20

A light and fast DNS server/forwarder implementation in 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
// Arbitrary plugin moved under plugins::dataset
// (content copied from src/plugins/executable/arbitrary.rs)

use crate::RegisterPlugin;
use crate::Result;
use crate::dns::{Message, RData, ResourceRecord};
use crate::plugin::{Context, Plugin};
use async_trait::async_trait;
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use std::sync::Arc;

#[derive(Debug, Deserialize, Clone)]
pub struct ArbitraryArgs {
    pub rules: Option<Vec<String>>,
    pub files: Option<Vec<String>>,
}

#[derive(RegisterPlugin)]
pub struct ArbitraryPlugin {
    map: HashMap<String, Vec<ResourceRecord>>,
}

impl ArbitraryPlugin {
    pub fn new(args: ArbitraryArgs) -> Result<Self> {
        let mut m: HashMap<String, Vec<ResourceRecord>> = HashMap::new();
        if let Some(rules) = args.rules {
            for (i, line) in rules.into_iter().enumerate() {
                if let Some(rr) = Self::parse_rr_line(&line) {
                    m.entry(rr.name().to_string()).or_default().push(rr);
                } else {
                    return Err(crate::Error::FileParse {
                        path: format!("inline rule #{}", i),
                        reason: format!("failed to parse: {}", line),
                    });
                }
            }
        }
        if let Some(files) = args.files {
            for file in files {
                let b = fs::read_to_string(&file).map_err(|e| crate::Error::FileParse {
                    path: file.clone(),
                    reason: format!("failed to read: {}", e),
                })?;
                for (i, line) in b.lines().enumerate() {
                    let line = line.trim();
                    if line.is_empty() || line.starts_with(';') || line.starts_with('#') {
                        continue;
                    }
                    if let Some(rr) = Self::parse_rr_line(line) {
                        m.entry(rr.name().to_string()).or_default().push(rr);
                    } else {
                        return Err(crate::Error::FileParse {
                            path: file.clone(),
                            reason: format!("failed to parse line {}", i + 1),
                        });
                    }
                }
            }
        }
        Ok(Self { map: m })
    }

    fn parse_rr_line(line: &str) -> Option<ResourceRecord> {
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() < 3 {
            return None;
        }
        let name = parts[0].trim_end_matches('.').to_lowercase();
        let (typ_idx, rdata_idx) = if parts[1].eq_ignore_ascii_case("IN") && parts.len() >= 4 {
            (2, 3)
        } else {
            (1, 2)
        };
        let typ = parts.get(typ_idx)?;
        let rdata = parts.get(rdata_idx)?;
        let rr = match typ.to_uppercase().as_str() {
            "A" => {
                let ip = Ipv4Addr::from_str(rdata).ok()?;
                ResourceRecord::new(
                    name,
                    crate::dns::types::RecordType::A,
                    crate::dns::types::RecordClass::IN,
                    300,
                    RData::A(ip),
                )
            }
            "AAAA" => {
                let ip = Ipv6Addr::from_str(rdata).ok()?;
                ResourceRecord::new(
                    name,
                    crate::dns::types::RecordType::AAAA,
                    crate::dns::types::RecordClass::IN,
                    300,
                    RData::AAAA(ip),
                )
            }
            "CNAME" => ResourceRecord::new(
                name,
                crate::dns::types::RecordType::CNAME,
                crate::dns::types::RecordClass::IN,
                300,
                RData::CNAME(rdata.to_string()),
            ),
            _ => {
                return None;
            }
        };
        Some(rr)
    }
}

impl fmt::Debug for ArbitraryPlugin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Arbitrary")
            .field("rules_count", &self.map.len())
            .finish()
    }
}

#[async_trait]
impl Plugin for ArbitraryPlugin {
    fn name(&self) -> &str {
        "arbitrary"
    }

    fn init(config: &crate::config::types::PluginConfig) -> Result<Arc<dyn Plugin>> {
        use serde_yaml;
        use std::sync::Arc;

        let args: ArbitraryArgs = serde_yaml::from_value(config.args.clone())
            .map_err(|e| crate::Error::Config(format!("failed to parse arbitrary args: {}", e)))?;
        Ok(Arc::new(ArbitraryPlugin::new(args)?))
    }

    async fn execute(&self, ctx: &mut Context) -> Result<()> {
        if let Some(q) = ctx.request().questions().first() {
            let key = q.qname().trim_end_matches('.').to_lowercase();
            if let Some(rrs) = self.map.get(&key) {
                let mut msg = Message::new();
                msg.set_id(ctx.request().id());
                msg.set_response(true);
                msg.add_question(q.clone());
                for rr in rrs {
                    msg.add_answer(rr.clone());
                }
                ctx.set_response(Some(msg));
            }
        }
        // No question in the request: do nothing. Previously this returned an
        // arbitrary record from the map (HashMap order is non-deterministic),
        // leaking internal configuration to any client sending an empty query
        // (health checks, scanners). A DNS response must echo the query's
        // question section, which is impossible here, so leave the response
        // unset and let downstream handle it.
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dns::Message;
    use crate::dns::types::{RecordClass, RecordType};
    use crate::dns::{Question, RData};

    #[tokio::test]
    async fn test_arbitrary_rules() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com A 192.0.2.1".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new("example.com", RecordType::A, RecordClass::IN));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_some());
        let resp = ctx.response().unwrap();
        assert_eq!(resp.answer_count(), 1);
        if let RData::A(ip) = resp.answers()[0].rdata() {
            assert_eq!(*ip, Ipv4Addr::new(192, 0, 2, 1));
        } else {
            panic!("expected A");
        }
    }

    #[tokio::test]
    async fn test_arbitrary_aaaa_record() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com AAAA 2001:db8::1".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new(
            "example.com",
            RecordType::AAAA,
            RecordClass::IN,
        ));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_some());
        let resp = ctx.response().unwrap();
        assert_eq!(resp.answer_count(), 1);
        match resp.answers()[0].rdata() {
            RData::AAAA(_) => {}
            _ => panic!("expected AAAA"),
        }
    }

    #[tokio::test]
    async fn test_arbitrary_cname_record() {
        let args = ArbitraryArgs {
            rules: Some(vec!["alias.com CNAME target.com".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new(
            "alias.com",
            RecordType::CNAME,
            RecordClass::IN,
        ));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_some());
        let resp = ctx.response().unwrap();
        assert_eq!(resp.answer_count(), 1);
        match resp.answers()[0].rdata() {
            RData::CNAME(name) => assert_eq!(name, "target.com"),
            _ => panic!("expected CNAME"),
        }
    }

    #[tokio::test]
    async fn test_arbitrary_with_in_class() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com IN A 10.0.0.1".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        assert!(!plugin.map.is_empty());
    }

    #[tokio::test]
    async fn test_arbitrary_trailing_dot() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com. A 192.0.2.2".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new("example.com", RecordType::A, RecordClass::IN));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_some());
    }

    #[test]
    fn test_arbitrary_parse_invalid_rule() {
        let args = ArbitraryArgs {
            rules: Some(vec!["invalid".to_string()]), // Too few parts
            files: None,
        };
        let result = ArbitraryPlugin::new(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_arbitrary_parse_invalid_ip() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com A not-an-ip".to_string()]),
            files: None,
        };
        let result = ArbitraryPlugin::new(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_arbitrary_parse_unknown_type() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com MX mail.example.com".to_string()]),
            files: None,
        };
        let result = ArbitraryPlugin::new(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_arbitrary_empty_rules() {
        let args = ArbitraryArgs {
            rules: Some(vec![]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        assert!(plugin.map.is_empty());
    }

    #[test]
    fn test_arbitrary_none_rules() {
        let args = ArbitraryArgs {
            rules: None,
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        assert!(plugin.map.is_empty());
    }

    #[test]
    fn test_arbitrary_debug() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com A 1.2.3.4".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let debug_str = format!("{:?}", plugin);
        assert!(debug_str.contains("Arbitrary"));
        assert!(debug_str.contains("rules_count"));
    }

    #[tokio::test]
    async fn test_arbitrary_no_question() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com A 1.2.3.4".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let req = Message::new(); // No question
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        // A request without a question section must not produce a response:
        // doing so would leak an arbitrary configured record (the previous
        // behavior returned a HashMap's first entry, which is unpredictable
        // and unrelated to the query).
        assert!(
            ctx.response().is_none(),
            "no response should be synthesized for a question-less request"
        );
    }

    #[tokio::test]
    async fn test_arbitrary_not_found() {
        let args = ArbitraryArgs {
            rules: Some(vec!["example.com A 1.2.3.4".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new("other.com", RecordType::A, RecordClass::IN));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        // Not found in map
        assert!(ctx.response().is_none());
    }

    /// Regression: domain matching was case-sensitive. A rule written as
    /// `Example.com` must match a query for `example.com` (RFC 1035).
    #[tokio::test]
    async fn test_arbitrary_case_insensitive_match() {
        let args = ArbitraryArgs {
            rules: Some(vec!["Example.COM A 1.2.3.4".to_string()]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        let mut req = Message::new();
        req.add_question(Question::new("example.com", RecordType::A, RecordClass::IN));
        let mut ctx = Context::new(req);
        plugin.execute(&mut ctx).await.unwrap();
        let resp = ctx.response().expect("case-insensitive match should hit");
        assert_eq!(resp.answer_count(), 1);
    }

    #[test]
    fn test_arbitrary_multiple_records_same_domain() {
        let args = ArbitraryArgs {
            rules: Some(vec![
                "example.com A 1.1.1.1".to_string(),
                "example.com A 2.2.2.2".to_string(),
            ]),
            files: None,
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        assert_eq!(plugin.map.get("example.com").unwrap().len(), 2);
    }

    #[test]
    fn test_arbitrary_file_not_found() {
        let args = ArbitraryArgs {
            rules: None,
            files: Some(vec!["/nonexistent/path.txt".to_string()]),
        };
        let result = ArbitraryPlugin::new(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_arbitrary_file_with_comments() {
        use std::io::Write;
        let tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp.as_file(), "# This is a comment").unwrap();
        writeln!(tmp.as_file(), "; Another comment").unwrap();
        writeln!(tmp.as_file()).unwrap();
        writeln!(tmp.as_file(), "example.com A 1.2.3.4").unwrap();

        let args = ArbitraryArgs {
            rules: None,
            files: Some(vec![tmp.path().to_str().unwrap().to_string()]),
        };
        let plugin = ArbitraryPlugin::new(args).unwrap();
        assert_eq!(plugin.map.len(), 1);
    }

    #[test]
    fn test_arbitrary_file_invalid_line() {
        use std::io::Write;
        let tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp.as_file(), "invalid").unwrap();

        let args = ArbitraryArgs {
            rules: None,
            files: Some(vec![tmp.path().to_str().unwrap().to_string()]),
        };
        let result = ArbitraryPlugin::new(args);
        assert!(result.is_err());
    }
}