-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnshandler.go
479 lines (418 loc) · 13.8 KB
/
dnshandler.go
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
/*
* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package main
import (
"log"
"net"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/miekg/dns"
"github.com/spf13/viper"
"github.com/dnstapir/tapir"
)
// var RpzZones = make(map[string]*tapir.ZoneData, 5)
// func DnsEngine(scannerq chan ScanRequest, updateq chan UpdateRequest) error {
func DnsEngine(conf *Config) error {
addresses := viper.GetStringSlice("dnsengine.addresses")
// verbose := viper.GetBool("dnsengine.verbose")
// debug := viper.GetBool("dnsengine.debug")
dns.HandleFunc(".", createHandler(conf))
conf.Loggers.Dnsengine.Printf("DnsEngine: addresses: %v", addresses)
for _, addr := range addresses {
for _, net := range []string{"udp", "tcp"} {
go func(addr, net string) {
conf.Loggers.Dnsengine.Printf("DnsEngine: serving on %s (%s)\n", addr, net)
server := &dns.Server{Addr: addr, Net: net}
// Must bump the buffer size of incoming UDP msgs, as updates
// may be much larger then queries
server.UDPSize = dns.DefaultMsgSize // 4096
if err := server.ListenAndServe(); err != nil {
conf.Loggers.Dnsengine.Printf("Failed to setup the %s server: %s\n", net, err.Error())
} else {
conf.Loggers.Dnsengine.Printf("DnsEngine: listening on %s/%s\n", addr, net)
}
}(addr, net)
}
}
return nil
}
func createHandler(conf *Config) func(w dns.ResponseWriter, r *dns.Msg) {
pd := conf.PopData
lg := conf.Loggers.Dnsengine
zonech := conf.PopData.RpzRefreshCh
// var rrtypes []string
return func(w dns.ResponseWriter, r *dns.Msg) {
qname := r.Question[0].Name
switch r.Opcode {
case dns.OpcodeNotify:
ntype := r.Question[0].Qtype
lg.Printf("Received NOTIFY(%s) for zone '%s'", dns.TypeToString[ntype], qname)
// send NOERROR response
m := new(dns.Msg)
m.SetReply(r)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
if _, ok := pd.RpzSources[qname]; ok {
lg.Printf("Received Notify for known zone %s. Fetching from upstream", qname)
zonech <- RpzRefresh{
Name: qname, // send zone name into RefreshEngine
ZoneType: pd.RpzSources[qname].ZoneType,
}
}
lg.Printf("Notify message: %v\n", m.String())
return
case dns.OpcodeQuery:
qtype := r.Question[0].Qtype
lg.Printf("Zone %s %s request from %s", qname, dns.TypeToString[qtype], w.RemoteAddr())
if qname == pd.Rpz.ZoneName {
err := pd.RpzResponder(w, r, qtype, lg)
if err != nil {
lg.Printf("Error from RpzResponder(): %v", err)
}
} else if zd, ok := pd.RpzSources[qname]; ok {
// The qname is equal to the name of a zone we have
err := ApexResponder(w, r, zd, qname, qtype, lg)
if err != nil {
lg.Printf("Error from ApexResponder(): %v", err)
}
} else {
lg.Printf("DnsHandler: Qname is '%s', which is not a known zone.", qname)
known_zones := []string{pd.Rpz.ZoneName}
for z := range pd.RpzSources {
known_zones = append(known_zones, z)
}
lg.Printf("DnsHandler: Known zones are: %v", known_zones)
// Let's see if we can find the zone
if strings.HasSuffix(qname, pd.Rpz.ZoneName) {
lg.Printf("Query for qname %s belongs in our own RPZ \"%s\"",
qname, pd.Rpz.ZoneName)
err := pd.QueryResponder(w, r, qname, qtype, lg)
if err != nil {
lg.Printf("Error from QueryResponder(): %v", err)
}
return
}
zd := pd.FindZone(qname)
if zd == nil {
lg.Printf("After FindZone: zd==nil")
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeRefused)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return // didn't find any zone for that qname or found zone, but it is an XFR zone only
}
lg.Printf("After FindZone: zd: zd.ZoneType: %v", zd.ZoneType)
if zd.ZoneType == tapir.XfrZone {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeRefused)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return // didn't find any zone for that qname or found zone, but it is an XFR zone only
}
lg.Printf("Found matching full zone for qname %s: %s", qname, zd.ZoneName)
err := QueryResponder(w, r, zd, qname, qtype, lg)
if err != nil {
lg.Printf("Error from QueryResponder(): %v", err)
}
return
}
return
default:
lg.Printf("Error: unable to handle msgs of type %s",
dns.OpcodeToString[r.Opcode])
}
}
}
func (pd *PopData) RpzResponder(w dns.ResponseWriter, r *dns.Msg, qtype uint16, lg *log.Logger) error {
m := new(dns.Msg)
m.SetReply(r)
m.MsgHdr.Authoritative = true
// apex := zd.Owners[zd.OwnerIndex[zd.ZoneName]]
// zd.Logger.Printf("*** Ownerindex(%s)=%d apex: %v", zd.ZoneName, zd.OwnerIndex[zd.ZoneName], apex)
zd := pd.Rpz.Axfr.ZoneData
// XXX: we need this, but later var glue tapir.RRset
downstream, _, err := net.SplitHostPort(w.RemoteAddr().String())
if err != nil {
lg.Printf("RpzResponder: Error from net.SplitHostPort(): %v", err)
return nil
}
switch qtype {
case dns.TypeAXFR:
lg.Printf("We have the zone %s, so let's try to serve it", pd.Rpz.ZoneName)
// log.Printf("SOA: %s", zd.SOA.String())
// log.Printf("BodyRRs: %d (+ %d apex RRs)", len(zd.BodyRRs), zd.ApexLen)
// pd.Logger.Printf("RpzResponder: sending zone %s with %d body RRs to XfrOut",
// zd.ZoneName, len(zd.RRs))
_, _, err := pd.RpzAxfrOut(w, r)
if err != nil {
lg.Printf("RpzResponder: error from RpzAxfrOut() serving zone %s: %v", zd.ZoneName, err)
}
return nil
case dns.TypeIXFR:
lg.Printf("RpzResponder: %s is our RPZ output", pd.Rpz.ZoneName)
serial, _, err := pd.RpzIxfrOut(w, r)
if err != nil {
lg.Printf("RpzResponder: error from RpzIxfrOut() serving zone %s: %v", zd.ZoneName, err)
}
pd.mu.Lock()
pd.DownstreamSerials[downstream] = serial // track the highest known serial for each downstream
pd.mu.Unlock()
return nil
case dns.TypeSOA:
// zd.Logger.Printf("There are %d SOA RRs in %s. rrset: %v", len(apex.RRtypes[dns.TypeSOA].RRs),
// zd.ZoneName, apex.RRtypes[dns.TypeSOA])
// m.Answer = append(m.Answer, dns.RR(&zd.SOA))
pd.Rpz.Axfr.SOA.Serial = pd.Rpz.CurrentSerial
m.Answer = append(m.Answer, dns.RR(&pd.Rpz.Axfr.SOA))
// m.Ns = append(m.Ns, apex.RRtypes[dns.TypeNS].RRs...)
m.Ns = append(m.Ns, pd.Rpz.Axfr.ZoneData.NSrrs...)
// glue = *zd.FindGlue(apex.RRtypes[dns.TypeNS])
// m.Extra = append(m.Extra, glue.RRs...)
default:
// every apex query we don't want to deal with
m.MsgHdr.Rcode = dns.RcodeRefused
m.Ns = append(m.Ns, zd.NSrrs...)
}
err = w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
func ApexResponder(w dns.ResponseWriter, r *dns.Msg, zd *tapir.ZoneData,
qname string, qtype uint16, lg *log.Logger) error {
m := new(dns.Msg)
m.SetReply(r)
m.MsgHdr.Authoritative = true
apex := zd.Owners[zd.OwnerIndex[zd.ZoneName]]
// zd.Logger.Printf("*** Ownerindex(%s)=%d apex: %v", zd.ZoneName, zd.OwnerIndex[zd.ZoneName], apex)
var glue tapir.RRset
switch qtype {
// case dns.TypeAXFR, dns.TypeIXFR:
// log.Printf("We have the zone %s, so let's try to serve it", qname)
// log.Printf("SOA: %s", zd.SOA.String())
// log.Printf("BodyRRs: %d (+ %d apex RRs)", len(zd.BodyRRs), zd.ApexLen)
// zd.Logger.Printf("ApexResponder: sending zone %s with %d body RRs to XfrOut",
// zd.ZoneName, len(zd.RRs))
// _, err := zd.ZoneTransferOut(w, r)
// if err != nil {
// zd.Logger.Printf("ApexResponder: error serving zone %s: %v", zd.ZoneName, err)
// }
// return nil
case dns.TypeSOA:
// zd.Logger.Printf("There are %d SOA RRs in %s. rrset: %v", len(apex.RRtypes[dns.TypeSOA].RRs),
// zd.ZoneName, apex.RRtypes[dns.TypeSOA])
m.Answer = append(m.Answer, dns.RR(&zd.SOA))
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeNS].RRs...)
glue = *zd.FindGlue(apex.RRtypes[dns.TypeNS])
m.Extra = append(m.Extra, glue.RRs...)
default:
// every apex query we don't want to deal with
m.MsgHdr.Rcode = dns.RcodeRefused
m.Ns = append(m.Ns, zd.NSrrs...)
}
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
// 0. Check for *any* existence of qname
// 1. [OK] For a qname below zone, first check if there is a delegation. If so--> send referral
// 2. If no delegation, check for exact match
// 3. [OK] If no exact match, check for CNAME match
// 4. If no CNAME match, check for wild card match
// 5. Give up.
func QueryResponder(w dns.ResponseWriter, r *dns.Msg, zd *tapir.ZoneData, qname string, qtype uint16, lg *log.Logger) error {
m := new(dns.Msg)
m.SetReply(r)
m.MsgHdr.Authoritative = true
var apex tapir.OwnerData
switch zd.ZoneType {
case 2:
apex = zd.Data[zd.ZoneName]
case 3:
apex = zd.Owners[zd.OwnerIndex[zd.ZoneName]]
}
returnNXDOMAIN := func() {
// return NXDOMAIN
m.MsgHdr.Rcode = dns.RcodeNameError
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeSOA].RRs...)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
}
// log.Printf("Zone %s Data: %v", zd.ZoneName, zd.Data)
var owner tapir.OwnerData
switch zd.ZoneType {
case tapir.MapZone, tapir.RpzZone:
if tmp, exist := zd.Data[qname]; exist {
owner = tmp
} else {
returnNXDOMAIN()
return nil
}
case tapir.SliceZone:
if _, ok := zd.OwnerIndex[qname]; !ok {
// return NXDOMAIN
m.MsgHdr.Rcode = dns.RcodeNameError
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeSOA].RRs...)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
owner = zd.Owners[zd.OwnerIndex[qname]]
default:
POPExiter("Error: QueryResponder: unknown zone type: %d", zd.ZoneType)
}
var glue *tapir.RRset
// 0. Check for *any existence of qname in zone
if len(owner.RRtypes) == 0 {
m.MsgHdr.Rcode = dns.RcodeNameError
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeSOA].RRs...)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
// 2. Check for qname + CNAME
// if len(zd.Data[qname].RRtypes) == 1 {
if len(owner.RRtypes) == 1 {
for k, v := range owner.RRtypes {
if k == dns.TypeCNAME {
if len(v.RRs) > 1 {
// XXX: NSD will not even load a zone with multiple CNAMEs. Better to check during load...
log.Printf("QueryResponder: Zone %s: Illegal content: multiple CNAME RRs: %v", zd.ZoneName, v)
}
m.Answer = append(m.Answer, v.RRs...)
tgt := v.RRs[0].(*dns.CNAME).Target
if strings.HasSuffix(tgt, zd.ZoneName) {
if tgtrrset, ok := zd.Owners[zd.OwnerIndex[tgt]].RRtypes[qtype]; ok {
m.Answer = append(m.Answer, tgtrrset.RRs...)
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeNS].RRs...)
glue = zd.FindGlue(apex.RRtypes[dns.TypeNS])
m.Extra = append(m.Extra, glue.RRs...)
}
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
}
}
}
// 2. Check for exact match qname+qtype
switch qtype {
case dns.TypeTXT, dns.TypeMX, dns.TypeA, dns.TypeAAAA:
lg.Printf("Apex data[%s]:\n", zd.ZoneName)
for rrt, d := range apex.RRtypes {
lg.Printf("%s: %v", dns.TypeToString[rrt], d)
}
lg.Printf("Qname data[%s]:\n", qname)
for rrt, d := range owner.RRtypes {
lg.Printf("%s: %v", dns.TypeToString[rrt], d)
}
if _, ok := owner.RRtypes[qtype]; ok && len(owner.RRtypes[qtype].RRs) > 0 {
m.Answer = append(m.Answer, owner.RRtypes[qtype].RRs...)
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeNS].RRs...)
glue = zd.FindGlue(apex.RRtypes[dns.TypeNS])
m.Extra = append(m.Extra, glue.RRs...)
} else {
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeSOA].RRs...)
}
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
default:
// everything we don't want to deal with
m.MsgHdr.Rcode = dns.RcodeRefused
m.Ns = append(m.Ns, apex.RRtypes[dns.TypeNS].RRs...)
glue = zd.FindGlue(apex.RRtypes[dns.TypeNS])
m.Extra = append(m.Extra, glue.RRs...)
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
}
return nil
}
func (pd *PopData) QueryResponder(w dns.ResponseWriter, r *dns.Msg, qname string, qtype uint16, lg *log.Logger) error {
m := new(dns.Msg)
m.SetReply(r)
m.MsgHdr.Authoritative = true
returnNXDOMAIN := func() {
// return NXDOMAIN
m.MsgHdr.Rcode = dns.RcodeNameError
// m.Ns = append(m.Ns, apex.RRtypes[dns.TypeSOA].RRs...)
m.Ns = append(m.Ns, dns.RR(&pd.Rpz.Axfr.SOA))
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
}
// log.Printf("Zone %s Data: %v", zd.ZoneName, zd.Data)
// var err error
var exist bool
var tn *tapir.RpzName
if tn, exist = pd.Rpz.Axfr.Data[qname]; exist {
m.MsgHdr.Rcode = dns.RcodeSuccess
switch qtype {
case dns.TypeCNAME, dns.TypeANY:
m.Answer = append(m.Answer, *tn.RR)
m.Ns = append(m.Ns, pd.Rpz.Axfr.NSrrs...)
default:
m.Ns = append(m.Ns, dns.RR(&pd.Rpz.Axfr.SOA))
}
err := w.WriteMsg(m)
if err != nil {
lg.Printf("Error from WriteMsg(): %v", err)
}
return nil
}
returnNXDOMAIN()
return nil
}
func (pd *PopData) FindZone(qname string) *tapir.ZoneData {
var tzone string
labels := strings.Split(qname, ".")
for i := 1; i < len(labels)-1; i++ {
tzone = strings.Join(labels[i:], ".")
log.Printf("FindZone for qname='%s': testing '%s'", qname, tzone)
if z, ok := pd.RpzSources[tzone]; ok {
log.Printf("Yes, zone=%s for qname=%s", tzone, qname)
return z
}
}
log.Printf("FindZone: no zone for qname=%s found", qname)
return nil
}
func (pd *PopData) FindZoneNG(qname string) *tapir.ZoneData {
i := strings.Index(qname, ".")
for {
if i == -1 {
break // done
}
log.Printf("FindZone for qname='%s': testing '%s'", qname, qname[i:])
if z, ok := pd.RpzSources[qname[i:]]; ok {
log.Printf("Yes, zone=%s for qname=%s", qname[i:], qname)
return z
}
i = strings.Index(qname[i:], ".")
}
log.Printf("FindZone: no zone for qname=%s found", qname)
return nil
}