sightingdb 0.4.1

A database designed for Sightings, a technique to count items
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
//! Turning a parsed DNS query into an answer.
//!
//! The conventions are the ones DNSBL clients already expect: a name that was
//! never seen is NXDOMAIN, and a name that was seen answers with a `127.0.0.x`
//! address whose last octet says roughly how often. That makes SightingDB
//! usable from anything that can already consult a blocklist, with the TXT
//! record carrying the detail for clients that want it.

use std::sync::Arc;

use hickory_proto::op::{Message, MessageType, Metadata, OpCode, Query, ResponseCode};
use hickory_proto::rr::rdata::{A, NS, SOA, TXT};
use hickory_proto::rr::{DNSClass, Name, RData, Record, RecordType};

use crate::attribute::AttributeView;
use crate::dns::name::{Exposed, resolve};
use crate::error::ApiError;
use crate::handlers::SharedState;
use crate::sighting_reader;

/// Everything the responder needs, built once at startup.
pub struct Responder {
    state: Arc<SharedState>,
    zone: Name,
    zone_labels: Vec<String>,
    exposed: Vec<Exposed>,
    ttl: u32,
    /// Whether a DNS lookup counts as a search. Off by default: it is an
    /// unauthenticated write path.
    shadow: bool,
}

impl Responder {
    pub fn new(
        state: Arc<SharedState>,
        zone: Name,
        exposed: Vec<Exposed>,
        ttl: u32,
        shadow: bool,
    ) -> Self {
        let zone_labels = labels_of(&zone);
        Self {
            state,
            zone,
            zone_labels,
            exposed,
            ttl,
            shadow,
        }
    }

    /// Answer one query. Always returns a message; the caller decides how to
    /// put it on the wire.
    pub fn respond(&self, request: &Message) -> Message {
        let mut metadata = Metadata::response_from_request(&request.metadata);
        metadata.authoritative = true;

        let mut response = Message::new(metadata.id, MessageType::Response, metadata.op_code);
        response.metadata = metadata;

        // We serve one zone authoritatively and do nothing else.
        if request.metadata.op_code != OpCode::Query {
            response.metadata.response_code = ResponseCode::NotImp;
            return response;
        }

        let Some(query) = request.queries.first() else {
            response.metadata.response_code = ResponseCode::FormErr;
            return response;
        };
        response.add_query(query.clone());

        if query.query_class() != DNSClass::IN {
            response.metadata.response_code = ResponseCode::NotImp;
            return response;
        }

        match self.answer(query) {
            Outcome::Answers(records) => {
                response.metadata.response_code = ResponseCode::NoError;
                response.answers = records;
            }
            // A name that exists but has no data of this type still needs the
            // SOA, so resolvers cache the negative for the right length.
            Outcome::NoData => {
                response.metadata.response_code = ResponseCode::NoError;
                response.authorities = vec![self.soa_record()];
            }
            Outcome::NxDomain => {
                response.metadata.response_code = ResponseCode::NXDomain;
                response.authorities = vec![self.soa_record()];
            }
            Outcome::Refused => {
                response.metadata.response_code = ResponseCode::Refused;
            }
        }

        response
    }

    fn answer(&self, query: &Query) -> Outcome {
        let qname = query.name();
        let qlabels = labels_of(qname);

        // Outside our zone entirely: say so rather than pretending the name
        // does not exist, and never act as an open resolver.
        if !qlabels.ends_with(&self.zone_labels) {
            return Outcome::Refused;
        }

        if qlabels.len() == self.zone_labels.len() {
            return self.answer_apex(query.query_type());
        }

        let Some(lookup) = resolve(&qlabels, &self.zone_labels, &self.exposed) else {
            return Outcome::NxDomain;
        };

        let view = match sighting_reader::read(
            &self.state.db,
            &lookup.exposed.namespace,
            &lookup.value,
            false,
            self.shadow,
        ) {
            Ok(view) => view,
            // Never seen, or the namespace is gone: both are "no such name" as
            // far as a DNS client is concerned.
            Err(ApiError::NotFound(_)) => return Outcome::NxDomain,
            Err(_) => return Outcome::Refused,
        };

        match query.query_type() {
            RecordType::A => Outcome::Answers(vec![self.a_record(query.name(), &view)]),
            RecordType::TXT => Outcome::Answers(vec![self.txt_record(query.name(), &view)]),
            RecordType::ANY => Outcome::Answers(vec![
                self.a_record(query.name(), &view),
                self.txt_record(query.name(), &view),
            ]),
            _ => Outcome::NoData,
        }
    }

    fn answer_apex(&self, query_type: RecordType) -> Outcome {
        match query_type {
            RecordType::SOA => Outcome::Answers(vec![self.soa_record()]),
            RecordType::NS => Outcome::Answers(vec![Record::from_rdata(
                self.zone.clone(),
                self.ttl,
                RData::NS(NS(self.zone.clone())),
            )]),
            _ => Outcome::NoData,
        }
    }

    fn a_record(&self, name: &Name, view: &AttributeView) -> Record {
        let address = std::net::Ipv4Addr::new(127, 0, 0, magnitude(view.count));
        Record::from_rdata(name.clone(), self.ttl, RData::A(A(address)))
    }

    fn txt_record(&self, name: &Name, view: &AttributeView) -> Record {
        let text = format!(
            "count={} first_seen={} last_seen={} consensus={}",
            view.count, view.first_seen, view.last_seen, view.consensus
        );
        Record::from_rdata(name.clone(), self.ttl, RData::TXT(TXT::new(vec![text])))
    }

    fn soa_record(&self) -> Record {
        let soa = SOA::new(
            self.zone.clone(),
            // No mailbox to give, so the conventional placeholder.
            Name::from_labels(vec![b"hostmaster".to_vec()])
                .unwrap_or_else(|_| Name::root())
                .append_domain(&self.zone)
                .unwrap_or_else(|_| self.zone.clone()),
            1,
            // Nothing ever transfers this zone, so the slave timers are
            // nominal; `minimum` is the one that matters, since it caps how
            // long a resolver caches an NXDOMAIN.
            3600,
            600,
            86_400,
            self.ttl,
        );
        Record::from_rdata(self.zone.clone(), self.ttl, RData::SOA(soa))
    }
}

enum Outcome {
    Answers(Vec<Record>),
    NoData,
    NxDomain,
    Refused,
}

/// The last octet of the `127.0.0.x` answer: how many times the value was seen,
/// by order of magnitude. 1 is once, 2 is single digits, 3 is tens, and so on.
/// A DNSBL client that only checks "did I get an address" still works.
fn magnitude(count: u64) -> u8 {
    match count {
        0 => 0,
        1 => 1,
        2..=9 => 2,
        10..=99 => 3,
        100..=999 => 4,
        1_000..=9_999 => 5,
        10_000..=99_999 => 6,
        100_000..=999_999 => 7,
        1_000_000..=9_999_999 => 8,
        _ => 9,
    }
}

/// A name as lowercase label strings, without the root label.
fn labels_of(name: &Name) -> Vec<String> {
    name.iter()
        .map(|label| String::from_utf8_lossy(label).to_ascii_lowercase())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::WriteOpts;
    use crate::dns::name::Encoding;
    use chrono::Utc;

    fn responder(shadow: bool) -> Responder {
        let state = Arc::new(SharedState::new(false));
        state.db.write(
            "malware/ips",
            "1.2.3.4",
            Utc::now(),
            WriteOpts {
                consensus: true,
                ttl: None,
            },
        );
        state.db.write(
            "malware/domains",
            "evil.com",
            Utc::now(),
            WriteOpts {
                consensus: true,
                ttl: None,
            },
        );
        state.db.write(
            "secrets",
            "hidden",
            Utc::now(),
            WriteOpts {
                consensus: true,
                ttl: None,
            },
        );

        Responder::new(
            state,
            Name::parse("sdb.example.com.", None).unwrap(),
            vec![
                Exposed {
                    label: "malware".into(),
                    namespace: "malware/ips".into(),
                    encoding: Encoding::Ip,
                },
                Exposed {
                    label: "domains".into(),
                    namespace: "malware/domains".into(),
                    encoding: Encoding::Domain,
                },
            ],
            60,
            shadow,
        )
    }

    fn ask(responder: &Responder, name: &str, query_type: RecordType) -> Message {
        let mut request = Message::query();
        request.add_query(Query::query(
            Name::parse(name, Some(&Name::root())).unwrap(),
            query_type,
        ));
        responder.respond(&request)
    }

    #[test]
    fn a_known_value_answers_with_a_loopback_address() {
        let r = responder(false);
        let response = ask(&r, "4.3.2.1.malware.sdb.example.com.", RecordType::A);

        assert_eq!(response.metadata.response_code, ResponseCode::NoError);
        assert!(response.metadata.authoritative);
        let RData::A(a) = &response.answers[0].data else {
            panic!("expected an A record, got {:?}", response.answers)
        };
        assert_eq!(a.0, std::net::Ipv4Addr::new(127, 0, 0, 1));
    }

    #[test]
    fn the_last_octet_grows_with_the_count() {
        assert_eq!(magnitude(1), 1);
        assert_eq!(magnitude(9), 2);
        assert_eq!(magnitude(10), 3);
        assert_eq!(magnitude(999), 4);
        assert_eq!(magnitude(u64::MAX), 9);
    }

    #[test]
    fn a_txt_query_carries_the_detail() {
        let r = responder(false);
        let response = ask(&r, "4.3.2.1.malware.sdb.example.com.", RecordType::TXT);

        let RData::TXT(txt) = &response.answers[0].data else {
            panic!("expected TXT")
        };
        let text = txt.to_string();
        assert!(text.contains("count=1"), "{text}");
        assert!(text.contains("consensus=1"), "{text}");
    }

    #[test]
    fn a_domain_namespace_uses_the_labels_directly() {
        let r = responder(false);
        let response = ask(&r, "evil.com.domains.sdb.example.com.", RecordType::A);

        assert_eq!(response.metadata.response_code, ResponseCode::NoError);
        assert_eq!(response.answers.len(), 1);
    }

    #[test]
    fn an_unknown_value_is_nxdomain_with_an_soa() {
        let r = responder(false);
        let response = ask(&r, "9.9.9.9.malware.sdb.example.com.", RecordType::A);

        assert_eq!(response.metadata.response_code, ResponseCode::NXDomain);
        // The SOA is what lets resolvers cache the negative answer.
        assert_eq!(response.authorities.len(), 1);
    }

    /// The ACL does not apply to DNS, so only namespaces named in the
    /// configuration may be reachable — everything else must look absent.
    #[test]
    fn unexposed_namespaces_are_invisible() {
        let r = responder(false);

        for name in [
            "hidden.secrets.sdb.example.com.",
            "hidden._config.sdb.example.com.",
        ] {
            let response = ask(&r, name, RecordType::A);
            assert_eq!(
                response.metadata.response_code,
                ResponseCode::NXDomain,
                "{name}"
            );
            assert!(response.answers.is_empty(), "{name}");
        }
    }

    /// Answering for names we are not authoritative for would make this an
    /// open resolver.
    #[test]
    fn names_outside_the_zone_are_refused() {
        let r = responder(false);
        let response = ask(&r, "www.google.com.", RecordType::A);

        assert_eq!(response.metadata.response_code, ResponseCode::Refused);
        assert!(response.answers.is_empty());
    }

    #[test]
    fn the_apex_serves_soa_and_ns() {
        let r = responder(false);

        let soa = ask(&r, "sdb.example.com.", RecordType::SOA);
        assert_eq!(soa.metadata.response_code, ResponseCode::NoError);
        assert!(matches!(&soa.answers[0].data, RData::SOA(_)));

        let ns = ask(&r, "sdb.example.com.", RecordType::NS);
        assert!(matches!(&ns.answers[0].data, RData::NS(_)));
    }

    #[test]
    fn an_unsupported_type_is_nodata_not_nxdomain() {
        let r = responder(false);
        let response = ask(&r, "4.3.2.1.malware.sdb.example.com.", RecordType::MX);

        assert_eq!(response.metadata.response_code, ResponseCode::NoError);
        assert!(response.answers.is_empty());
        assert_eq!(response.authorities.len(), 1);
    }

    #[test]
    fn shadow_sightings_are_off_unless_asked_for() {
        let r = responder(false);
        ask(&r, "4.3.2.1.malware.sdb.example.com.", RecordType::A);
        assert_eq!(r.state.db.count("_shadow/malware/ips", "1.2.3.4"), 0);

        let r = responder(true);
        ask(&r, "4.3.2.1.malware.sdb.example.com.", RecordType::A);
        assert_eq!(r.state.db.count("_shadow/malware/ips", "1.2.3.4"), 1);
    }

    #[test]
    fn queries_are_echoed_and_the_id_preserved() {
        let r = responder(false);
        let mut request = Message::query();
        request.metadata.id = 0x1234;
        request.add_query(Query::query(
            Name::parse("4.3.2.1.malware.sdb.example.com.", None).unwrap(),
            RecordType::A,
        ));

        let response = r.respond(&request);

        assert_eq!(response.metadata.id, 0x1234);
        assert_eq!(response.metadata.message_type, MessageType::Response);
        assert_eq!(response.queries.len(), 1);
    }

    #[test]
    fn a_query_with_no_question_is_a_format_error() {
        let r = responder(false);
        let response = r.respond(&Message::query());

        assert_eq!(response.metadata.response_code, ResponseCode::FormErr);
    }
}