-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolve.go
535 lines (479 loc) · 14.8 KB
/
resolve.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright © 2017,2018,2020 Pennock Tech, LLC.
// All rights reserved, except as granted under license.
// Licensed per file LICENSE.txt
package main
import (
"errors"
"fmt"
"math/rand"
"net"
"os"
"regexp"
"sort"
"strconv"
"sync"
"time"
"go.pennock.tech/smtpdane/internal/errorlist"
"github.com/miekg/dns"
)
const EnvKeyDNSResolver = "DNS_RESOLVER"
var dnsSettings struct {
sync.RWMutex
conf *dns.ClientConfig
client *dns.Client
}
func initDNS() (*dns.ClientConfig, *dns.Client, error) {
dnsSettings.RLock()
if dnsSettings.client != nil {
defer dnsSettings.RUnlock()
return dnsSettings.conf, dnsSettings.client, nil
}
dnsSettings.RUnlock()
dnsSettings.Lock()
defer dnsSettings.Unlock()
if dnsSettings.client != nil {
return dnsSettings.conf, dnsSettings.client, nil
}
var (
conf *dns.ClientConfig
err error
)
if os.Getenv(EnvKeyDNSResolver) == "" {
conf, err = dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return nil, nil, err
}
if conf.Attempts < 3 {
conf.Attempts = 3
}
} else {
// we now use the config always, for things like timeouts,
// so construct a skeletal one
conf = &dns.ClientConfig{
Timeout: 5,
Attempts: 3,
}
}
c := new(dns.Client)
dnsSettings.conf = conf
dnsSettings.client = c
return dnsSettings.conf, dnsSettings.client, nil
}
func resolversFromList(input []string, defDNSPort string) []string {
r := make([]string, len(input))
for i := range input {
r[i] = HostPortWithDefaultPort(input[i], defDNSPort)
}
return r
}
var resolverSplitRE *regexp.Regexp
func init() {
resolverSplitRE = regexp.MustCompile(`[,\s]+`)
}
func resolversFromString(input string) []string {
return resolversFromList(resolverSplitRE.Split(input, -1), "53")
}
// FIXME: This is not doing DNS validation locally.
// It's resolving DNS, delegating trust in validation to the resolver by
// trusting the AD bit.
// I want to get this working without needing a validating resolver.
// This should be a standalone monitoring tool.
func resolveRRSecure(
cbfunc func(typ uint16, rr dns.RR, rrname string) (interface{}, error),
rrname string,
typlist ...uint16,
) ([]interface{}, error) {
return resolveRRmaybeSecure(true, cbfunc, rrname, typlist...)
}
func resolveRRmaybeSecure(
needSecure bool,
// the cbfunc is called the the confirmed RR type and the rr and the rrname;
// it should return an item to be added to the resolveRRSecure return list,
// and an error; non-nil error inhibits appending to the list.
cbfunc func(typ uint16, rr dns.RR, rrname string) (interface{}, error),
rrname string,
typlist ...uint16,
) ([]interface{}, error) {
config, c, err := initDNS()
if err != nil {
return nil, err
}
var resolvers []string
if r := os.Getenv(EnvKeyDNSResolver); r != "" {
resolvers = resolversFromString(r)
} else {
resolvers = resolversFromList(config.Servers, config.Port)
}
resultList := make([]interface{}, 0, 20)
errList := errorlist.New()
m := new(dns.Msg)
m.SetEdns0(dns.DefaultMsgSize, true)
// why is this uint16 ipv dns.Type ? Infelicity stuck in API?
DNS_RRTYPE_LOOP:
for _, typ := range typlist {
m.SetQuestion(rrname, typ)
var (
r *dns.Msg
err error
)
DNS_RESOLVER_LOOP:
for _, resolver := range resolvers {
c.Net = "udp"
RETRY_DNS_LOOKUP:
for i := 0; i < config.Attempts; i++ {
if i > 0 {
time.Sleep(retryJitter((2 << (i - 1)) * time.Second))
}
debugf("resolver exchange %s/%s for %s %q\n", resolver, c.Net, dns.Type(typ), rrname)
r, _, err = c.Exchange(m, resolver)
if err != nil {
var netError net.Error
if errors.As(err, &netError) && netError.Timeout() {
continue
}
errList.Add(err)
r = nil
continue DNS_RESOLVER_LOOP
}
if r != nil {
break RETRY_DNS_LOOKUP
}
}
if r == nil {
continue
}
if r.Rcode != dns.RcodeSuccess {
failure, known := dns.RcodeToString[r.Rcode]
if !known {
failure = fmt.Sprintf("Rcode<%d> (unknown)", r.Rcode)
}
errList.AddErrorf("DNS lookup non-successful [resolver %v]: %v", resolver, failure)
rcode := r.Rcode
r = nil
// There are enough broken server implementations when it comes
// to AD and unknown types (often including AAAA) that we
// currently only consider NXDOMAIN definitive.
// We can expand upon this as needed.
switch rcode {
case dns.RcodeNameError:
continue DNS_RRTYPE_LOOP
default:
continue DNS_RESOLVER_LOOP
}
}
if r == nil || r.Rcode != dns.RcodeSuccess {
panic("expected to be evaluating DNS SUCCESS scenarios but was not")
}
// Check for truncation first, in case some bad servers truncate
// the DNSSEC data needed to be AD.
if r.Truncated {
c.Net = "tcp"
goto RETRY_DNS_LOOKUP
}
// Here we depend upon AD bit and so are still secure, assuming secure
// link to trusted resolver.
// Assume all our resolvers are equivalent for AD/not, so if not AD, try the
// next type (because some DNS servers break horribly on AAAA).
if needSecure && !r.AuthenticatedData {
errList.AddErrorf("not AD set for results from %v for %q/%v query, skipping any remaining resolvers", resolver, rrname, dns.Type(typ))
r = nil
continue DNS_RRTYPE_LOOP
}
// We have successfully made a query which doesn't need us to retry,
// so skip the rest of the DNS resolvers.
break DNS_RESOLVER_LOOP
}
if r == nil {
errList.AddErrorf("[%q/%v]: all DNS resolver queries failed, unable to get authentic result", rrname, dns.Type(typ))
// seems likely might be SERVFAIL from broken auth servers for AAAA records
continue DNS_RRTYPE_LOOP
}
for _, rr := range r.Answer {
// TODO: CNAME?
if rr.Header().Rrtype != typ {
continue
}
x, err := cbfunc(typ, dns.Copy(rr), rrname)
if err != nil {
errList.Add(err)
} else {
resultList = append(resultList, x)
}
}
}
if len(resultList) == 0 {
errList.Add(errors.New("no results found"))
return nil, errList
}
return resultList, errList.Maybe()
}
// There's a lot of repetition/boilerplate in the below.
// If we expand beyond where we are at now, then we really should consider reflection; more complexity, less repetition.
type addrRecord struct {
addr net.IP
rrname string
}
func cbRRTypeAddr(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeA:
if ip, ok := rr.(*dns.A); ok {
return addrRecord{ip.A, rr.Header().Name}, nil
} else {
//lint:ignore ST1005 this is not capitalized, it's an "A" record
return nil, fmt.Errorf("A record failed to cast to *dns.A [%q/%v]", rrname, dns.Type(typ))
}
case dns.TypeAAAA:
if ip, ok := rr.(*dns.AAAA); ok {
return addrRecord{ip.AAAA, rr.Header().Name}, nil
} else {
return nil, fmt.Errorf("AAAA record failed to cast to *dns.AAAA [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeAddr(%v,..,%q) called, expected A/AAAA", dns.Type(typ), rrname)
}
// ResolveAddrSecure takes a hostname and returns a list of address records and
// the hostname to which those address records correspond; if the returned
// hostname is not the input hostname, then CNAMEs of some kind are involved.
func ResolveAddrSecure(hostname string) ([]net.IP, string, error) {
rl, e := resolveRRSecure(cbRRTypeAddr, dns.Fqdn(hostname), dns.TypeAAAA, dns.TypeA)
if e != nil {
return nil, "", e
}
var resolvedName string
addrList := make([]net.IP, len(rl))
for i := range rl {
ar := rl[i].(addrRecord)
addrList[i] = ar.addr
if resolvedName != "" && resolvedName != ar.rrname {
return nil, "", fmt.Errorf("seen multiple RRnames for %q: both %q & %q", hostname, resolvedName, ar.rrname)
}
if resolvedName == "" {
resolvedName = ar.rrname
}
}
return addrList, resolvedName, nil
}
// ResolveAddrINSECURE lets us get address records when we don't care about the
// DNSSEC security. We just want the list of IP addresses.
func ResolveAddrINSECURE(hostname string) ([]net.IP, error) {
rl, e := resolveRRmaybeSecure(false, cbRRTypeAddr, dns.Fqdn(hostname), dns.TypeAAAA, dns.TypeA)
if e != nil {
return nil, e
}
var resolvedName string
addrList := make([]net.IP, len(rl))
for i := range rl {
ar := rl[i].(addrRecord)
addrList[i] = ar.addr
if resolvedName != "" && resolvedName != ar.rrname {
return nil, fmt.Errorf("seen multiple RRnames for %q: both %q & %q", hostname, resolvedName, ar.rrname)
}
if resolvedName == "" {
resolvedName = ar.rrname
}
}
return addrList, nil
}
type TLSAset struct {
RRs []*dns.TLSA
name string
foundName string
}
func cbRRTypeTLSA(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeTLSA:
if tlsa, ok := rr.(*dns.TLSA); ok {
return tlsa, nil
} else {
return nil, fmt.Errorf("TLSA record failed to cast to *dns.TLSA [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeTLSA(%v,..,%q) called, expected TLSA", dns.Type(typ), rrname)
}
func ResolveTLSA(hostname string, port int) (*TLSAset, error) {
tlsaName := fmt.Sprintf("_%d._tcp.%s", port, dns.Fqdn(hostname))
rl, e := resolveRRSecure(cbRRTypeTLSA, tlsaName, dns.TypeTLSA)
if e != nil {
return nil, e
}
TLSAList := make([]*dns.TLSA, len(rl))
for i := range rl {
TLSAList[i] = rl[i].(*dns.TLSA)
}
return &TLSAset{
RRs: TLSAList,
name: tlsaName,
foundName: TLSAList[0].Hdr.Name,
}, nil
}
// TLSAShortString provides something suitable for output without showing the
// full contents; for our uses, we don't need the RR_Header and for
// full-certs-in-DNS we don't _want_ to print it all.
// Viktor points out that for full certs in DNS, the start of the record will
// be less useful, so show the _last_ 16 octets
// TLSAShortString is "enough to probably fit on a line with much other text".
func TLSAShortString(rr *dns.TLSA) string {
offset := len(rr.Certificate) - 16
prefix := "..."
if offset < 0 {
offset = 0
prefix = ""
}
return strconv.Itoa(int(rr.Usage)) + " " +
strconv.Itoa(int(rr.Selector)) + " " +
strconv.Itoa(int(rr.MatchingType)) + " " +
prefix + rr.Certificate[offset:]
}
// TLSAMediumString is for where the TLSA record is probably all that's on a line.
// Assume 2 leading spaces, 1 digit for each of the three leading fields, a space
// after each, that's 8, allow for 70.
func TLSAMediumString(rr *dns.TLSA) string {
var rest, prefix string
if len(rr.Certificate) <= 70 {
rest = rr.Certificate
} else {
prefix = "..."
rest = rr.Certificate[(len(rr.Certificate) - 67):]
}
return strconv.Itoa(int(rr.Usage)) + " " +
strconv.Itoa(int(rr.Selector)) + " " +
strconv.Itoa(int(rr.MatchingType)) + " " +
prefix + rest
}
func cbRRTypeMXjustnames(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeMX:
if mx, ok := rr.(*dns.MX); ok {
return mx.Mx, nil
} else {
return nil, fmt.Errorf("MX record failed to cast to *dns.MX [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeMX(%v,..,%q) called, expected MX", dns.Type(typ), rrname)
}
func cbRRTypeMXresults(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeMX:
if mx, ok := rr.(*dns.MX); ok {
return mx, nil
} else {
return nil, fmt.Errorf("MX record failed to cast to *dns.MX [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeMX(%v,..,%q) called, expected MX", dns.Type(typ), rrname)
}
// ResolveMX only returns the hostnames, we don't care about the Preference
func ResolveMX(hostname string) ([]string, error) {
rl, e := resolveRRSecure(cbRRTypeMXjustnames, dns.Fqdn(hostname), dns.TypeMX)
if e != nil {
return nil, e
}
hostnameList := make([]string, len(rl))
for i := range rl {
hostnameList[i] = rl[i].(string)
}
return hostnameList, nil
}
type MXTierResults struct {
Preference int
Hostnames []string
}
// ResolveMXTiers returns an ordered slice of the MX tiers, ordering by MX
// Preference from "try first" (lowest number) to "try last" (highest number),
// each entry in the slice being one tier, an MXTierResults.
// The second result is the total count of MX records seen, which may include
// duplicates.
func ResolveMXTiers(hostname string) ([]MXTierResults, int, error) {
rl, e := resolveRRSecure(cbRRTypeMXresults, dns.Fqdn(hostname), dns.TypeMX)
if e != nil {
return nil, 0, e
}
count := 0
all := make(map[int]MXTierResults, len(rl))
dupPreferences := make([]int, 0, len(rl))
for i := range rl {
item := rl[i].(*dns.MX)
pref := int(item.Preference)
m, ok := all[pref]
if !ok {
m = MXTierResults{Preference: pref, Hostnames: make([]string, 0, 3)}
}
m.Hostnames = append(m.Hostnames, item.Mx)
all[pref] = m
dupPreferences = append(dupPreferences, pref)
count++
}
sort.Ints(dupPreferences)
results := make([]MXTierResults, 0, len(all))
prev := -1
for _, i := range dupPreferences {
if i == prev {
continue
}
prev = i
results = append(results, all[i])
}
return results, count, nil
}
func cbRRTypeSRV(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeSRV:
if srv, ok := rr.(*dns.SRV); ok {
return srv, nil
} else {
return nil, fmt.Errorf("SRV record failed to cast to *dns.SRV [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeSRV(%v,..,%q) called, expected SRV", dns.Type(typ), rrname)
}
// ResolveSRV returns MX records, we need at least the Port, not just the Target
func ResolveSRV(lookup string) ([]*dns.SRV, error) {
rl, e := resolveRRSecure(cbRRTypeSRV, dns.Fqdn(lookup), dns.TypeSRV)
if e != nil {
return nil, e
}
srvList := make([]*dns.SRV, len(rl))
for i := range rl {
srvList[i] = rl[i].(*dns.SRV)
}
return srvList, nil
}
func cbRRTypeCNAME(typ uint16, rr dns.RR, rrname string) (interface{}, error) {
switch typ {
case dns.TypeCNAME:
if cname, ok := rr.(*dns.CNAME); ok {
return cname.Target, nil
} else {
return nil, fmt.Errorf("CNAME record failed to cast to *dns.CNAME [%q/%v]", rrname, dns.Type(typ))
}
}
return nil, fmt.Errorf("BUG: cbRRTypeCNAME(%v,..,%q) called, expected CNAME", dns.Type(typ), rrname)
}
// ResolveCNAME returns a string of the CNAME's Target. Since we asked for a CNAME, CNAME
// results should not have been chased.
func ResolveCNAME(lookup string) (string, error) {
rl, e := resolveRRSecure(cbRRTypeCNAME, dns.Fqdn(lookup), dns.TypeCNAME)
if e != nil {
return "", e
}
found := false
var resolvedName string
for i := range rl {
target := rl[i].(string)
if found && target != resolvedName {
return "", fmt.Errorf("seen multiple CNAME targets for %q: both %q & %q", lookup, resolvedName, target)
}
resolvedName = target
found = true
}
if found {
return resolvedName, nil
}
panic("should not have reached here, resolveRRSecure should have errored instead")
}
func retryJitter(base time.Duration) time.Duration {
b := float64(base)
// 10% +/-
offsetFactor := rand.Float64()*0.2 - 0.1
return time.Duration(b + offsetFactor*b)
}