-
Notifications
You must be signed in to change notification settings - Fork 109
/
lookup.go
84 lines (77 loc) · 2.3 KB
/
lookup.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
package tlsrpt
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
"github.com/mjl-/mox/dns"
"github.com/mjl-/mox/mlog"
"github.com/mjl-/mox/stub"
)
var (
MetricLookup stub.HistogramVec = stub.HistogramVecIgnore{}
)
var (
ErrNoRecord = errors.New("tlsrpt: no tlsrpt dns txt record")
ErrMultipleRecords = errors.New("tlsrpt: multiple tlsrpt records") // Must be treated as if domain does not implement TLSRPT.
ErrDNS = errors.New("tlsrpt: temporary error")
ErrRecordSyntax = errors.New("tlsrpt: record syntax error")
)
// Lookup looks up a TLSRPT DNS TXT record for domain at "_smtp._tls.<domain>" and
// parses it.
func Lookup(ctx context.Context, elog *slog.Logger, resolver dns.Resolver, domain dns.Domain) (rrecord *Record, rtxt string, rerr error) {
log := mlog.New("tlsrpt", elog)
start := time.Now()
defer func() {
result := "ok"
if rerr != nil {
if errors.Is(rerr, ErrNoRecord) {
result = "notfound"
} else if errors.Is(rerr, ErrMultipleRecords) {
result = "multiple"
} else if errors.Is(rerr, ErrDNS) {
result = "temperror"
} else if errors.Is(rerr, ErrRecordSyntax) {
result = "malformed"
} else {
result = "error"
}
}
MetricLookup.ObserveLabels(float64(time.Since(start))/float64(time.Second), result)
log.Debugx("tlsrpt lookup result", rerr,
slog.Any("domain", domain),
slog.Any("record", rrecord),
slog.Duration("duration", time.Since(start)))
}()
name := "_smtp._tls." + domain.ASCII + "."
txts, _, err := dns.WithPackage(resolver, "tlsrpt").LookupTXT(ctx, name)
if dns.IsNotFound(err) {
return nil, "", ErrNoRecord
} else if err != nil {
return nil, "", fmt.Errorf("%w: %s", ErrDNS, err)
}
var text string
var record *Record
for _, txt := range txts {
r, istlsrpt, err := ParseRecord(txt)
if !istlsrpt {
// This is a loose but probably reasonable interpretation of ../rfc/8460:375 which
// wants us to discard otherwise valid records that start with e.g. "v=TLSRPTv1 ;"
// (note the space before the ";") when multiple TXT records were returned.
continue
}
if err != nil {
return nil, "", fmt.Errorf("parsing record: %w", err)
}
if record != nil {
return nil, "", ErrMultipleRecords
}
record = r
text = txt
}
if record == nil {
return nil, "", ErrNoRecord
}
return record, text, nil
}