-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
282 lines (247 loc) · 5.93 KB
/
util.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
package main
import (
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/apex/log"
"github.com/miekg/dns"
"github.com/spf13/viper"
)
func readZonefile(zonef io.Reader) (origin string, cache Cache) {
defer log.Trace("Read Zone File").Stop(nil)
cache = make(Cache, 0)
//
zp := dns.NewZoneParser(zonef, "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
if rr.Header().Rrtype == dns.TypeSOA {
origin = rr.Header().Name
}
label := rr.Header().Name
rrtype := dns.TypeToString[rr.Header().Rrtype]
if rr.Header().Rrtype == dns.TypeRRSIG {
rrtype = rrtype + dns.TypeToString[rr.(*dns.RRSIG).TypeCovered]
}
if _, ok := cache[label]; !ok {
cache[label] = make(map[string][]dns.RR)
}
if _, ok := cache[label][rrtype]; !ok {
cache[label][rrtype] = make([]dns.RR, 0)
}
cache[label][rrtype] = append(cache[label][rrtype], rr)
}
if err := zp.Err(); err != nil {
panic(err)
}
return
}
func hasNSEC(cache Cache) bool {
for l := range cache {
if _, ok := cache[l]["NSEC"]; ok {
return true
}
}
return false
}
func hasNSEC3(cache Cache) bool {
for l := range cache {
if _, ok := cache[l]["NSEC3"]; ok {
return true
}
}
return false
}
func isDelegated(label string, cache Cache, origin string) bool {
// out of zone, has to be glue
if !strings.HasSuffix(label, origin) {
return true
}
// origin is not delegated
if label == origin {
return false
}
numOrigin := dns.CountLabel(origin)
numLabel := dns.CountLabel(label)
labels := dns.SplitDomainName(label)
for i := 0; i < numLabel-numOrigin; i += 1 {
l := dns.Fqdn(strings.Join(labels[i:numLabel], "."))
if _, ok := cache[l]; ok {
if _, ok = cache[l]["NS"]; ok {
return true
}
}
}
// no delegation found
return false
}
// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) []string {
labels := dns.SplitDomainName(s)
res := make([]string, 0)
for i := len(labels) - 1; i >= 0; i = i - 1 {
res = append(res, labels[i])
}
return res
}
func getLabels(cache Cache) []string {
defer log.Trace("Get Labels").Stop(nil)
t0 := time.Now()
var labels [][]string = make([][]string, 0)
for label := range cache {
labels = append(labels, Reverse(label))
}
log.Debugf("Get labels unsorted %v",time.Since(t0))
sort.Slice(labels, func(i, j int) bool {
li := len(labels[i])
lj := len(labels[j])
min := li
if lj < min {
min = lj
}
for n := 0; n < min; n += 1 {
if labels[i][n] == labels[j][n] {
continue
}
return labels[i][n] < labels[j][n]
}
return li < lj
})
var result []string = make([]string,len(labels))
for i:=range labels {
rev := make([]string,len(labels[i]))
for n,m:= 0,len(labels[i])-1; m>=0; n,m = n+1,m-1 {
rev[n]=labels[i][m]
}
result[i] = strings.Join(rev,".")+"."
}
log.Debugf("Get labels sorted %v",time.Since(t0))
return result
}
func getNsecLabels(cache Cache, origin string) []string {
defer log.Trace("Get NSEC Labels").Stop(nil)
t0 := time.Now()
var labels [][]string = make([][]string, 0)
for label := range cache {
// origin must have nsec records
if label == origin {
labels = append(labels, Reverse(label))
continue
}
// out of zone data should have no nsec records
if !strings.HasSuffix(label, origin) {
continue
}
/*
ONLY IN ZONE LABELS BELOW
*/
// if the name is a delegation point it has nsec
if _, ok := cache[label]["NS"]; ok {
labels = append(labels, Reverse(label))
continue
}
if isDelegated(label, cache, origin) {
// this is glue, no nsec
continue
}
// NSEC3 records do not get NSEC records
if _, ok := cache[label]["NSEC3"]; ok {
nsec3only := true
for l := range cache[label] {
if l != "NSEC3" && l != "RRSIGNSEC3" {
nsec3only = false
break
}
}
if nsec3only {
// this is a nsec3 hash, no nsec record needed
continue
}
}
// zone data, needs an nsec record
labels = append(labels, Reverse(label))
}
log.Debugf("Get NSEC labels unsorted %v",time.Since(t0))
sort.Slice(labels, func(i, j int) bool {
li := len(labels[i])
lj := len(labels[j])
min := li
if lj < min {
min = lj
}
for n := 0; n < min; n += 1 {
if labels[i][n] == labels[j][n] {
continue
}
return labels[i][n] < labels[j][n]
}
return li < lj
})
var result []string = make([]string,len(labels))
for i:=range labels {
rev := make([]string,len(labels[i]))
for n,m:= 0,len(labels[i])-1; m>=0; n,m = n+1,m-1 {
rev[n]=labels[i][m]
}
result[i] = strings.Join(rev,".")+"."
}
log.Debugf("Get NSEC labels sorted %v",time.Since(t0))
return result
}
func hash2string(digesttype uint8) string {
if dt, ok := dns.HashToString[digesttype]; ok {
return dt
}
return fmt.Sprintf("DIGESTTYPE%d", digesttype)
}
func algorithm2string(algorithm uint8) string {
if algStr, ok := dns.AlgorithmToString[algorithm]; ok {
return algStr
}
return fmt.Sprintf("ALGORITHM%d", algorithm)
}
func okAlgorithm(algorithm uint8) bool {
alg := algorithm2string(algorithm)
if !viper.IsSet(alg) || !viper.GetBool(alg) {
return false
}
return true
}
func okDigestType(dt uint8) bool {
dtstr := hash2string(dt)
if !viper.IsSet(dtstr) || !viper.GetBool(dtstr) {
return false
}
return true
}
func bool2allow(b bool) string {
if b {
return "allowed"
}
return "not allowed"
}
func checkOnlyAtApex(cache Cache, origin string, rrtype string) (r Result) {
for label := range cache {
if label == origin {
continue
}
if _, ok := cache[label][rrtype]; ok {
log.Errorf("Label %s has %s record.", label, rrtype)
r.errors++
}
}
return
}
func checkSignedBySEP(cache Cache, origin string, rrtype string) (r Result) {
for _, rr := range cache[origin]["RRSIG"+rrtype] {
rrsig := rr.(*dns.RRSIG)
for _, dd := range cache[origin]["DNSKEY"] {
dnskey := dd.(*dns.DNSKEY)
if rrsig.KeyTag == dnskey.KeyTag() && dnskey.Flags&dns.SEP != dns.SEP {
log.Warnf("%s records signed with ZSK keytag %d", rrtype, dnskey.KeyTag())
r.warnings++
}
}
}
return
}