This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
answers.go
300 lines (256 loc) · 9.19 KB
/
answers.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
package main
import (
"math/rand"
"net"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
)
// The top-level key in the config for the default (not client-specific answers)
const DEFAULT_KEY = "default"
// The 2nd-level key in the config for the recursive resolver addresses
const RECURSE_KEY = "recurse"
// Maximum recursion when resolving CNAMEs
const MAX_DEPTH = 10
// Recursive servers
func (answers *Answers) Recursers(clientUUID string) []string {
var hosts []string
more := answers.recursersFor(clientUUID)
if len(more) > 0 {
hosts = append(hosts, more...)
}
more = answers.recursersFor(DEFAULT_KEY)
if len(more) > 0 {
hosts = append(hosts, more...)
}
return hosts
}
func (answers *Answers) recursersFor(clientUUID string) []string {
var hosts []string
client, ok := (*answers)[clientUUID]
if ok {
if ok && len(client.Recurse) > 0 {
hosts = client.Recurse
}
}
return hosts
}
// Search suffixes
func (answers *Answers) SearchSuffixes(clientUUID string) []string {
var suffixes []string
client, ok := (*answers)[clientUUID]
if ok {
if ok && len(client.Search) > 0 {
suffixes = client.Search
}
}
return suffixes
}
// Authoritative suffixes
func (answers *Answers) AuthoritativeSuffixes() []string {
var suffixes []string
client, ok := (*answers)[DEFAULT_KEY]
if ok && len(client.Authoritative) > 0 {
for _, suffix := range client.Authoritative {
withDots := "." + strings.Trim(suffix, ".") + "."
suffixes = append(suffixes, withDots)
}
}
return suffixes
}
func (answers *Answers) Addresses(clientUUID string, fqdn string, answerFqdn string, cnameParents []dns.RR, depth int) (records []dns.RR, ok bool) {
fqdn = dns.Fqdn(fqdn)
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Trying to resolve addresses")
// Limit recursing for non-obvious loops
if len(cnameParents) >= MAX_DEPTH {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Warn("Followed CNAME too many times ", cnameParents)
return nil, false
}
// Look for a CNAME entry
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Trying CNAME Records")
result, ok := answers.Matching(dns.TypeCNAME, clientUUID, fqdn, answerFqdn)
if ok && len(result) > 0 {
cname := result[0].(*dns.CNAME)
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Matched CNAME ", cname.Target)
// Stop obvious loops
if dns.Fqdn(cname.Target) == fqdn {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Warn("CNAME is a loop ", cname.Target)
return nil, false
}
// Recurse to find the eventual A for this CNAME
children, ok := answers.Addresses(clientUUID, dns.Fqdn(cname.Target), dns.Fqdn(cname.Target), append(cnameParents, cname), depth+1)
if ok && len(children) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "target": cname.Target, "client": clientUUID, "depth": depth}).Debug("Resolved CNAME ", children)
records = append(records, cname)
records = append(records, children...)
return records, true
}
}
// Look for an A entry
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Trying A Records")
result, ok = answers.Matching(dns.TypeA, clientUUID, fqdn, answerFqdn)
if ok && len(result) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Matched A ", result)
shuffle(&result)
return result, true
}
// When resolving CNAMES, check recursive server
if len(cnameParents) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Trying recursive servers")
r := new(dns.Msg)
r.SetQuestion(fqdn, dns.TypeA)
msg, err := ResolveTryAll(r, answers.Recursers(clientUUID))
if err == nil {
return msg.Answer, true
}
}
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID, "depth": depth}).Debug("Did not match anything")
return nil, false
}
func (answers *Answers) Matching(qtype uint16, clientUUID string, fqdn string, answerFqdn string) (records []dns.RR, ok bool) {
authoritativeFor := answers.AuthoritativeSuffixes()
authoritative := false
for _, suffix := range authoritativeFor {
if strings.HasSuffix(fqdn, suffix) {
authoritative = true
break
}
}
// If we are authoritative for a suffix the label has, there's no point trying alternate search suffixes
var clientSearches []string
if authoritative {
clientSearches = []string{}
} else {
clientSearches = answers.SearchSuffixes(clientUUID)
}
// Client answers, client search
log.WithFields(log.Fields{"label": fqdn, "client": clientUUID}).Debug("Trying client answers, client search")
records, ok = answers.MatchingSearch(qtype, clientUUID, fqdn, answerFqdn, []string{})
if ok {
return
}
// Default answers, client search
log.WithFields(log.Fields{"label": fqdn, "client": clientUUID}).Debug("Trying default answers, client search")
records, ok = answers.MatchingSearch(qtype, DEFAULT_KEY, fqdn, answerFqdn, clientSearches)
if ok {
return
}
// Default answers, default search
log.WithFields(log.Fields{"label": fqdn, "client": clientUUID}).Debug("Trying default answers, default search")
defaultSearches := answers.SearchSuffixes(DEFAULT_KEY)
records, ok = answers.MatchingSearch(qtype, DEFAULT_KEY, fqdn, answerFqdn, defaultSearches)
if ok {
return
}
return nil, false
}
func (answers *Answers) MatchingSearch(qtype uint16, clientUUID string, fqdn string, answerFqdn string, searches []string) (records []dns.RR, ok bool) {
records, ok = answers.MatchingExact(qtype, clientUUID, fqdn, answerFqdn)
if ok {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientUUID}).Debug("Matched exact FQDN")
return
}
base := strings.TrimRight(fqdn, ".")
limit := int(*ndots)
if limit == 0 || strings.Count(base, ".") < limit {
if searches != nil && len(searches) > 0 {
for _, suffix := range searches {
newFqdn := base + "." + strings.TrimRight(suffix, ".") + "."
log.WithFields(log.Fields{"fqdn": newFqdn, "client": clientUUID}).Debug("Trying alternate suffix")
records, ok = answers.MatchingExact(qtype, clientUUID, newFqdn, answerFqdn)
if ok {
log.WithFields(log.Fields{"fqdn": newFqdn, "client": clientUUID}).Debug("Matched alternate suffix")
return
}
}
}
}
return nil, false
}
func (answers *Answers) MatchingExact(qtype uint16, clientUUID string, fqdn string, answerFqdn string) (records []dns.RR, ok bool) {
client, ok := (*answers)[clientUUID]
if ok {
switch qtype {
case dns.TypeA:
//log.WithFields(log.Fields{"qtype": "A", "client": clientUUID, "fqdn": fqdn}).Debug("Searching for A")
res, ok := client.A[fqdn]
if ok && len(res.Answer) > 0 {
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
for i := 0; i < len(res.Answer); i++ {
hdr := dns.RR_Header{Name: answerFqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
ip := net.ParseIP(res.Answer[i])
record := &dns.A{Hdr: hdr, A: ip}
records = append(records, record)
}
shuffle(&records)
}
case dns.TypeCNAME:
//log.WithFields(log.Fields{"qtype": "CNAME", "client": clientUUID, "fqdn": fqdn}).Debug("Searching for CNAME")
res, ok := client.Cname[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
hdr := dns.RR_Header{Name: answerFqdn, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl}
record := &dns.CNAME{Hdr: hdr, Target: res.Answer}
records = append(records, record)
}
case dns.TypePTR:
//log.WithFields(log.Fields{"qtype": "PTR", "client": clientUUID, "fqdn": fqdn}).Debug("Searching for PTR")
res, ok := client.Ptr[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
hdr := dns.RR_Header{Name: answerFqdn, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}
record := &dns.PTR{Hdr: hdr, Ptr: res.Answer}
records = append(records, record)
}
case dns.TypeTXT:
//log.WithFields(log.Fields{"qtype": "TXT", "client": clientUUID, "fqdn": fqdn}).Debug("Searching for TXT")
res, ok := client.Txt[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
for i := 0; i < len(res.Answer); i++ {
hdr := dns.RR_Header{Name: answerFqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: ttl}
str := res.Answer[i]
if len(str) > 255 {
log.WithFields(log.Fields{"qtype": "TXT", "client": clientUUID, "fqdn": fqdn}).Warn("TXT record too long: ", str)
return nil, false
}
record := &dns.TXT{Hdr: hdr, Txt: []string{str}}
records = append(records, record)
}
}
}
}
if len(records) > 0 {
return records, true
} else {
return nil, false
}
}
// Shuffles the sub-section of the supplied slice starting from the first A or AAAA record and going
// until the end. In other words, doesn't shuffle CNAME records at the start of the slice whose order
// should be maintained.
func shuffle(items *[]dns.RR) {
max := len(*items)
foundA := false
for i := 0; i < max; i++ {
record := (*items)[i].Header()
if !foundA && record.Rrtype != dns.TypeA && record.Rrtype != dns.TypeAAAA {
continue
}
foundA = true
j := i + rand.Intn(max-i)
(*items)[i], (*items)[j] = (*items)[j], (*items)[i]
}
}