1
|
|
// Copyright 2016-2020 The CoreDNS authors and contributors
|
2
|
|
// Adapted for SDNS usage by Semih Alev.
|
3
|
|
|
4
|
|
package response
|
5
|
|
|
6
|
|
import (
|
7
|
|
"fmt"
|
8
|
|
"time"
|
9
|
|
|
10
|
|
"github.com/miekg/dns"
|
11
|
|
)
|
12
|
|
|
13
|
|
// Type is the type of the message.
|
14
|
|
type Type int
|
15
|
|
|
16
|
|
const (
|
17
|
|
// NoError indicates a positive reply
|
18
|
|
NoError Type = iota
|
19
|
|
// NameError is a NXDOMAIN in header, SOA in auth.
|
20
|
|
NameError
|
21
|
|
// NoData indicates name found, but not the type: NOERROR in header, SOA in auth.
|
22
|
|
NoData
|
23
|
|
// Delegation is a msg with a pointer to another nameserver: NOERROR in header, NS in auth, optionally fluff in additional (not checked).
|
24
|
|
Delegation
|
25
|
|
// Meta indicates a meta message, NOTIFY, or a transfer: qType is IXFR or AXFR.
|
26
|
|
Meta
|
27
|
|
// Update is an dynamic update message.
|
28
|
|
Update
|
29
|
|
// OtherError indicates any other error.
|
30
|
|
OtherError
|
31
|
|
// Expired if sigs expired: don't cache these
|
32
|
|
Expired
|
33
|
|
// NoCache indicates a no cache reply
|
34
|
|
NoCache
|
35
|
|
)
|
36
|
|
|
37
|
|
var toString = map[Type]string{
|
38
|
|
NoError: "NOERROR",
|
39
|
|
NameError: "NXDOMAIN",
|
40
|
|
NoData: "NODATA",
|
41
|
|
Delegation: "DELEGATION",
|
42
|
|
Meta: "META",
|
43
|
|
Update: "UPDATE",
|
44
|
|
OtherError: "OTHERERROR",
|
45
|
|
Expired: "EXPIRED",
|
46
|
|
NoCache: "NOCACHE",
|
47
|
|
}
|
48
|
|
|
49
|
1
|
func (t Type) String() string { return toString[t] }
|
50
|
|
|
51
|
|
// TypeFromString returns the type from the string s. If not type matches
|
52
|
|
// the OtherError type and an error are returned.
|
53
|
|
func TypeFromString(s string) (Type, error) {
|
54
|
|
for t, str := range toString {
|
55
|
1
|
if s == str {
|
56
|
1
|
return t, nil
|
57
|
|
}
|
58
|
|
}
|
59
|
1
|
return NoError, fmt.Errorf("invalid Type: %s", s)
|
60
|
|
}
|
61
|
|
|
62
|
|
// Typify classifies a message, it returns the Type.
|
63
|
|
func Typify(m *dns.Msg, t time.Time) (Type, *dns.OPT) {
|
64
|
1
|
if m == nil {
|
65
|
1
|
return OtherError, nil
|
66
|
|
}
|
67
|
1
|
opt := m.IsEdns0()
|
68
|
1
|
do := false
|
69
|
1
|
if opt != nil {
|
70
|
1
|
do = opt.Do()
|
71
|
|
}
|
72
|
|
|
73
|
1
|
if m.Opcode == dns.OpcodeUpdate {
|
74
|
1
|
return Update, opt
|
75
|
|
}
|
76
|
|
|
77
|
|
// Check transfer and update first
|
78
|
1
|
if m.Opcode == dns.OpcodeNotify {
|
79
|
1
|
return Meta, opt
|
80
|
|
}
|
81
|
|
|
82
|
1
|
if len(m.Question) > 0 {
|
83
|
1
|
if m.Question[0].Qtype == dns.TypeAXFR || m.Question[0].Qtype == dns.TypeIXFR {
|
84
|
1
|
return Meta, opt
|
85
|
|
}
|
86
|
|
}
|
87
|
|
|
88
|
|
// If our message contains any expired sigs and we care about that, we should return expired
|
89
|
1
|
if do {
|
90
|
1
|
if expired := typifyExpired(m, t); expired {
|
91
|
1
|
return Expired, opt
|
92
|
|
}
|
93
|
|
}
|
94
|
|
|
95
|
1
|
if len(m.Answer) > 0 && m.Rcode == dns.RcodeSuccess {
|
96
|
1
|
return NoError, opt
|
97
|
|
}
|
98
|
|
|
99
|
1
|
soa := false
|
100
|
1
|
ns := 0
|
101
|
|
for _, r := range m.Ns {
|
102
|
1
|
if r.Header().Rrtype == dns.TypeSOA {
|
103
|
1
|
soa = true
|
104
|
1
|
continue
|
105
|
|
}
|
106
|
1
|
if r.Header().Rrtype == dns.TypeNS {
|
107
|
1
|
ns++
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
1
|
if !soa && len(m.Question) > 0 {
|
112
|
1
|
if len(m.Answer) == 0 && m.Question[0].Qtype == dns.TypeDNSKEY {
|
113
|
0
|
return NoCache, opt
|
114
|
|
}
|
115
|
|
}
|
116
|
|
|
117
|
1
|
if soa && m.Rcode == dns.RcodeSuccess {
|
118
|
1
|
return NoData, opt
|
119
|
|
}
|
120
|
1
|
if soa && m.Rcode == dns.RcodeNameError {
|
121
|
1
|
return NameError, opt
|
122
|
|
}
|
123
|
|
|
124
|
1
|
if ns > 0 && m.Rcode == dns.RcodeSuccess {
|
125
|
1
|
return Delegation, opt
|
126
|
|
}
|
127
|
|
|
128
|
1
|
if m.Rcode == dns.RcodeSuccess {
|
129
|
1
|
return NoError, opt
|
130
|
|
}
|
131
|
|
|
132
|
1
|
return OtherError, opt
|
133
|
|
}
|
134
|
|
|
135
|
|
func typifyExpired(m *dns.Msg, t time.Time) bool {
|
136
|
1
|
if expired := typifyExpiredRRSIG(m.Answer, t); expired {
|
137
|
1
|
return true
|
138
|
|
}
|
139
|
1
|
if expired := typifyExpiredRRSIG(m.Ns, t); expired {
|
140
|
1
|
return true
|
141
|
|
}
|
142
|
1
|
if expired := typifyExpiredRRSIG(m.Extra, t); expired {
|
143
|
1
|
return true
|
144
|
|
}
|
145
|
1
|
return false
|
146
|
|
}
|
147
|
|
|
148
|
|
func typifyExpiredRRSIG(rrs []dns.RR, t time.Time) bool {
|
149
|
|
for _, r := range rrs {
|
150
|
1
|
if r.Header().Rrtype != dns.TypeRRSIG {
|
151
|
1
|
continue
|
152
|
|
}
|
153
|
1
|
ok := r.(*dns.RRSIG).ValidityPeriod(t)
|
154
|
1
|
if !ok {
|
155
|
1
|
return true
|
156
|
|
}
|
157
|
|
}
|
158
|
1
|
return false
|
159
|
|
}
|