forked from mjl-/moxtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
904 lines (777 loc) · 23.1 KB
/
main.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
// Moxtools is a web app for inspecting mail infrastructure, serving as a showcase
// for reusing Go packages from mox.
package main
import (
"context"
"crypto/tls"
"embed"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"log/slog"
"net"
"net/http"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/mjl-/adns"
"github.com/mjl-/sherpa"
"github.com/mjl-/sherpadoc"
"github.com/mjl-/sherpaprom"
"github.com/mjl-/mox/dkim"
"github.com/mjl-/mox/dmarc"
"github.com/mjl-/mox/dns"
"github.com/mjl-/mox/mlog"
"github.com/mjl-/mox/mtasts"
"github.com/mjl-/mox/ratelimit"
"github.com/mjl-/mox/smtpclient"
"github.com/mjl-/mox/spf"
"github.com/mjl-/mox/tlsrpt"
)
var listen string
var listenMetrics string
var hostname string
var dnsHostname dns.Domain
var ratelimiter bool
var version = "(devel)"
var metaBuf []byte // JSON object with version info.
var pkglog mlog.Log
var cidgen atomic.Int64
func init() {
cidgen.Store(time.Now().UnixMicro())
}
var apiLimiter = ratelimit.Limiter{
WindowLimits: []ratelimit.WindowLimit{
{Window: time.Minute, Limits: [...]int64{10, 20, 30}},
{Window: time.Hour, Limits: [...]int64{100, 200, 300}},
{Window: 24 * time.Hour, Limits: [...]int64{1000, 2000, 3000}},
},
}
var apiDomainLimiter = ratelimit.Limiter{
WindowLimits: []ratelimit.WindowLimit{
{Window: time.Minute, Limits: [...]int64{3, 20, 30}},
{Window: time.Hour, Limits: [...]int64{20, 30, 40}},
{Window: 24 * time.Hour, Limits: [...]int64{100, 200, 300}},
},
}
var smtpDialLimiter = ratelimit.Limiter{
WindowLimits: []ratelimit.WindowLimit{
{Window: time.Minute, Limits: [...]int64{1, 3, 9}},
{Window: time.Hour, Limits: [...]int64{10, 30, 90}},
{Window: 24 * time.Hour, Limits: [...]int64{30, 90, 270}},
},
}
//go:embed s/*
var files embed.FS
var resolver = dns.StrictResolver{}
func xcheck(err error, msg string) {
if err != nil {
pkglog.Fatalx(msg, err)
}
}
func main() {
mlog.Logfmt = true
pkglog = mlog.New("moxtools", nil)
var err error
hostname, err = os.Hostname()
xcheck(err, "get hostname")
var goversion string
buildInfo, ok := debug.ReadBuildInfo()
if ok {
goversion = buildInfo.GoVersion
version = buildInfo.Main.Version
if version == "(devel)" {
for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
version = setting.Value
break
}
}
}
}
mlog.SetConfig(map[string]slog.Level{"smtpclient": mlog.LevelTrace})
flag.BoolVar(&ratelimiter, "ratelimit", false, "enable ip-based rate limiter for incoming api requests (based on x-forwarded-for with fallback to connection ip) and outgoing smtp connections")
flag.StringVar(&listen, "listen", ":8080", "address for serve http")
flag.StringVar(&listenMetrics, "listen-metrics", ":8081", "address for serving prometheus metrics over http")
flag.StringVar(&hostname, "hostname", hostname, "hostname to use when dialing smtp server")
flag.Usage = func() {
fmt.Println("usage: moxtools [flags]")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
args := flag.Args()
if len(args) != 0 {
flag.Usage()
}
dnsHostname, err = dns.ParseDomain(hostname)
xcheck(err, "parsing hostname")
var docs sherpadoc.Section
f, err := files.Open("s/api.json")
xcheck(err, "open api docs")
err = json.NewDecoder(f).Decode(&docs)
xcheck(err, "parsing api docs")
f.Close()
collector, err := sherpaprom.NewCollector("moxtools", nil)
xcheck(err, "creating sherpa prometheus collector")
apiHandler, err := sherpa.NewHandler("/api/", version, API{}, &docs, &sherpa.HandlerOpts{Collector: collector, AdjustFunctionNames: "none"})
xcheck(err, "making api handler")
var meta = struct {
Version string
GoVersion string
GoOs string
GoArch string
}{version, goversion, runtime.GOOS, runtime.GOARCH}
metaBuf, err = json.Marshal(meta)
xcheck(err, "marshal meta")
var static fs.FS
var serveIndex func(w http.ResponseWriter, r *http.Request)
_, err = os.Stat("s")
if err == nil {
static = os.DirFS("s")
serveIndex = func(w http.ResponseWriter, r *http.Request) {
httpError := func(err error, msg string) {
pkglog.Errorx(msg, err)
http.Error(w, "500 - internal server error", http.StatusInternalServerError)
}
fh, err := static.Open("index.html")
if err != nil {
httpError(err, "open index.html")
return
}
defer fh.Close()
fjs, err := static.Open("app.js")
if err != nil {
httpError(err, "open app.js")
return
}
defer fjs.Close()
html, mtime, err := prepareHTML(fh, fjs)
if err != nil {
httpError(err, "preparing html")
return
}
http.ServeContent(w, r, "index.html", mtime, strings.NewReader(html))
}
} else {
static, err = fs.Sub(files, "s")
xcheck(err, "static files sub file system")
fh, err := static.Open("index.html")
xcheck(err, "open index.html")
defer fh.Close()
fjs, err := static.Open("app.js")
xcheck(err, "open app.js")
defer fjs.Close()
html, mtime, err := prepareHTML(fh, fjs)
xcheck(err, "preparing html")
serveIndex = func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "index.html", mtime, strings.NewReader(html))
}
}
web := http.NewServeMux()
web.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
var ip net.IP
xff := r.Header.Get("X-Forwarded-For")
if xff == "" {
host, _, err := net.SplitHostPort(r.RemoteAddr)
pkglog.Check(err, "parsing remoteaddr", slog.String("remoteaddr", r.RemoteAddr))
ip = net.ParseIP(host)
if ip == nil {
pkglog.Error("cannot parse ip from remoteaddr", slog.String("remoteaddr", r.RemoteAddr))
}
} else {
t := strings.Split(xff, ",")
ipstr := t[len(t)-1]
ip = net.ParseIP(ipstr)
if ip == nil {
pkglog.Error("cannot parse ip from x-forwarded-for header", slog.String("ipstr", ipstr))
}
}
r = r.WithContext(context.WithValue(r.Context(), keyIP, ip))
apiHandler.ServeHTTP(w, r)
})
web.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "405 - method not allowed", http.StatusMethodNotAllowed)
return
}
serveIndex(w, r)
} else {
http.FileServer(http.FS(static))
}
})
pkglog.Print("serving",
slog.String("listen", listen),
slog.String("listenmetrics", listenMetrics),
slog.Any("hostname", dnsHostname),
slog.String("version", version),
slog.String("goversion", goversion),
slog.String("goos", runtime.GOOS),
slog.String("goarch", runtime.GOARCH))
if listenMetrics != "" {
go func() {
metrics := http.NewServeMux()
metrics.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(listenMetrics, metrics)
xcheck(err, "listen and serve metrics")
}()
}
err = http.ListenAndServe(listen, web)
xcheck(err, "listen and serve")
}
func prepareHTML(fh, fjs fs.File) (string, time.Time, error) {
index, err := io.ReadAll(fh)
if err != nil {
return "", time.Time{}, err
}
js, err := io.ReadAll(fjs)
if err != nil {
return "", time.Time{}, err
}
htmlinfo, err := fh.Stat()
if err != nil {
return "", time.Time{}, err
}
jsinfo, err := fjs.Stat()
if err != nil {
return "", time.Time{}, err
}
mtime := htmlinfo.ModTime()
if t := jsinfo.ModTime(); t.Before(mtime) {
mtime = t
}
html := string(index)
html = strings.ReplaceAll(html, `<script src="app.js"></script>`, "<script>const meta = "+string(metaBuf)+"\n"+string(js)+"</script>")
return html, mtime, nil
}
func newLog() mlog.Log {
return mlog.New("moxtools", nil).With(slog.Int64("cid", cidgen.Add(1)))
}
type API struct{}
func xcheckuser(err error, msg string, attrs ...slog.Attr) {
if err == nil {
return
}
pkglog.Errorx(msg, err, attrs...)
errmsg := fmt.Sprintf("%s: %s", msg, err)
panic(&sherpa.Error{Code: "user:error", Message: errmsg})
}
type ctxKey string
var keyIP ctxKey = "ip"
func xlimit(ctx context.Context, r *ratelimit.Limiter) {
if !ratelimiter {
return
}
ip := ctx.Value(keyIP).(net.IP)
if ip == nil || !r.Add(ip, time.Now(), 1) {
xcheckuser(errors.New("too many requests from ip or subnet in window, try again soon"), "rate limiter", slog.Any("ip", ip))
}
}
type SPFReceived struct {
Status string
Mechanism string
}
func (API) SPFCheck(ctx context.Context, domain, ipstr string) (received SPFReceived, dom dns.Domain, explanation string, authentic bool) {
log := newLog()
xlimit(ctx, &apiLimiter)
log.Debug("spfcheck call", slog.String("domain", domain), slog.String("ip", ipstr))
dom, err := dns.ParseDomain(domain)
xcheckuser(err, "parsing domain")
ip := net.ParseIP(ipstr)
if ip == nil {
xcheckuser(errors.New("invalid ip"), "parsing ip")
}
opctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
args := spf.Args{
RemoteIP: ip,
HelloDomain: dns.IPDomain{Domain: dom},
}
var recv spf.Received
recv, dom, explanation, authentic, err = spf.Verify(opctx, log.Logger, resolver, args)
xcheckuser(err, "verifying spf")
received = SPFReceived{string(recv.Result), recv.Mechanism}
return
}
type DKIMStatus string
func (API) DKIMLookup(ctx context.Context, selector, domain string) (status DKIMStatus, record *dkim.Record, txt string, authentic bool) {
log := newLog()
xlimit(ctx, &apiLimiter)
log.Debug("dkimlookup call", slog.String("selector", selector), slog.String("domain", domain))
sel, err := dns.ParseDomain(selector)
xcheckuser(err, "parsing selector")
dom, err := dns.ParseDomain(domain)
xcheckuser(err, "parsing domain")
opctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
var xstatus dkim.Status
xstatus, record, txt, authentic, err = dkim.Lookup(opctx, log.Logger, resolver, sel, dom)
xcheckuser(err, "looking up dkim record")
status = DKIMStatus(string(xstatus))
return
}
type DKIMResult struct {
Status DKIMStatus
Sig *dkim.Sig // Parsed form of DKIM-Signature header. Can be nil for invalid DKIM-Signature header.
Record *dkim.Record // Parsed form of DKIM DNS record for selector and domain in Sig. Optional.
RecordAuthentic bool // Whether DKIM DNS record was DNSSEC-protected. Only valid if Sig is non-nil.
Error string // If Status is not StatusPass, this error holds the details and can be checked using errors.Is.
}
func (API) DKIMVerify(ctx context.Context, message string) []DKIMResult {
log := newLog()
xlimit(ctx, &apiLimiter)
log.Debug("dkimverify call")
opctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
message = strings.ReplaceAll(message, "\n", "\r\n")
results, err := dkim.Verify(opctx, log.Logger, resolver, true, dkim.DefaultPolicy, strings.NewReader(message), false)
xcheckuser(err, "verifying dkim signatures in message")
l := make([]DKIMResult, len(results))
for i, r := range results {
var errmsg string
if r.Err != nil {
errmsg = r.Err.Error()
}
l[i] = DKIMResult{
Status: DKIMStatus(string(r.Status)),
Sig: r.Sig,
Record: r.Record,
RecordAuthentic: r.RecordAuthentic,
Error: errmsg,
}
}
return l
}
func logPanic(log mlog.Log) {
x := recover()
if x == nil {
return
}
log.Error("uncaught panic", slog.Any("err", x))
debug.PrintStack()
}
type SPFRecord struct {
spf.Record
}
type DomainSPF struct {
DurationMS int
Status string
TXT string
Record *SPFRecord
Authentic bool
Error string
}
type DMARCRecord struct {
dmarc.Record
}
type DomainDMARC struct {
DurationMS int
Status string
Domain dns.Domain
Record *DMARCRecord
TXT string
Authentic bool
Error string
}
type TLSRPTRecord struct {
tlsrpt.Record
}
type DomainTLSRPT struct {
DurationMS int
Record *TLSRPTRecord
TXT string
Error string
}
type MTASTSRecord struct {
mtasts.Record
}
type DomainMTASTS struct {
DurationMS int
Implemented bool
Record *MTASTSRecord
Policy *mtasts.Policy
PolicyText string
Error string
}
type DomainIP struct {
DurationMS int
Authentic bool
ExpandedAuthentic bool
ExpandedHost dns.Domain
IPs []net.IP
DualStack bool
Error string
}
type TLSARecord struct {
adns.TLSA
}
type DomainDANE struct {
DurationMS int
Required bool
Records []TLSARecord
TLSABaseDomain dns.Domain
Error string
VerifiedRecord TLSARecord
}
type Proto struct {
ClientWrite bool
Text string
}
type TLSConnectionState struct {
Version string
CipherSuite string
NegotiatedProtocol string
ServerName string
}
type DomainSMTP struct {
DurationMS int
Error string
Supports8bitMIME bool
SupportsRequireTLS bool
SupportsSMTPUTF8 bool
SupportsSTARTTLS bool
TLSConnectionState *TLSConnectionState
RecipientDomainResult *TLSRPTResult
HostResult *TLSRPTResult
Trace []Proto
}
type TLSRPTResult struct {
Policy TLSRPTResultPolicy
Summary TLSRPTSummary
FailureDetails []TLSRPTFailureDetails
}
type TLSRPTResultPolicy struct {
Type string
String []string
Domain string
MXHost []string
}
type TLSRPTSummary struct {
TotalSuccessfulSessionCount int64
TotalFailureSessionCount int64
}
type TLSRPTFailureDetails struct {
ResultType string
SendingMTAIP string
ReceivingMXHostname string
ReceivingMXHelo string
ReceivingIP string
FailedSessionCount int64
AdditionalInformation string
FailureReasonCode string
}
func tlsrptResult(r tlsrpt.Result) *TLSRPTResult {
p := r.Policy
s := r.Summary
failureDetails := make([]TLSRPTFailureDetails, len(r.FailureDetails))
for i, fd := range r.FailureDetails {
failureDetails[i] = TLSRPTFailureDetails{
string(fd.ResultType),
fd.SendingMTAIP,
fd.ReceivingMXHostname,
fd.ReceivingMXHelo,
fd.ReceivingIP,
fd.FailedSessionCount,
fd.AdditionalInformation,
fd.FailureReasonCode,
}
}
return &TLSRPTResult{
TLSRPTResultPolicy{string(p.Type), p.String, p.Domain, p.MXHost},
TLSRPTSummary{s.TotalSuccessfulSessionCount, s.TotalFailureSessionCount},
failureDetails,
}
}
type DomainMX struct {
DurationMS int
Have bool
OrigNextHopAuthentic bool
ExpandedNextHopAuthentic bool
ExpandedNextHop dns.Domain
Permanent bool
Error string
}
type DomainDial struct {
DurationMS int
IP net.IP
Error string
}
type DomainMXHost struct {
DurationMS int
Host dns.IPDomain
MTASTSError string
IP DomainIP
DANE DomainDANE
Dial DomainDial
SMTP DomainSMTP
}
type DomainResult struct {
DurationMS int
Domain dns.Domain
SPF DomainSPF
DMARC DomainDMARC
TLSRPT DomainTLSRPT
MTASTS DomainMTASTS
MX DomainMX
MXHosts []DomainMXHost
}
func errmsg(err error) string {
if err == nil {
return ""
}
return err.Error()
}
func timeSince(t time.Time) int {
return int(time.Since(t) / time.Millisecond)
}
func (API) DomainCheck(ctx context.Context, domain string) (dr DomainResult) {
log := newLog()
xlimit(ctx, &apiLimiter)
xlimit(ctx, &apiDomainLimiter)
log.Debug("domaincheck call", slog.String("domain", domain))
dom, err := dns.ParseDomain(domain)
xcheck(err, "parsing domain")
start := time.Now()
dr.Domain = dom
opctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var wg sync.WaitGroup
// SPF.
wg.Add(1)
go func() {
defer logPanic(log)
defer wg.Done()
t0 := time.Now()
status, txt, record, authentic, err := spf.Lookup(opctx, log.Logger, resolver, dom)
var spfRecord *SPFRecord
if record != nil {
spfRecord = &SPFRecord{*record}
}
dr.SPF = DomainSPF{timeSince(t0), string(status), txt, spfRecord, authentic, errmsg(err)}
}()
// DMARC.
wg.Add(1)
go func() {
defer logPanic(log)
defer wg.Done()
t0 := time.Now()
status, dmarcDom, record, txt, authentic, err := dmarc.Lookup(opctx, log.Logger, resolver, dom)
var dmarcRecord *DMARCRecord
if record != nil {
dmarcRecord = &DMARCRecord{*record}
}
dr.DMARC = DomainDMARC{timeSince(t0), string(status), dmarcDom, dmarcRecord, txt, authentic, errmsg(err)}
}()
// TLSRPT.
wg.Add(1)
go func() {
defer logPanic(log)
defer wg.Done()
t0 := time.Now()
record, txt, err := tlsrpt.Lookup(opctx, log.Logger, resolver, dom)
if err != nil && errors.Is(err, tlsrpt.ErrNoRecord) {
err = nil
}
var tlsrptRecord *TLSRPTRecord
if record != nil {
tlsrptRecord = &TLSRPTRecord{*record}
}
dr.TLSRPT = DomainTLSRPT{timeSince(t0), tlsrptRecord, txt, errmsg(err)}
}()
checkMX := func(mx *DomainMXHost, dial, pkix bool) {
wg.Add(1)
go func() {
defer logPanic(log)
defer wg.Done()
t0 := time.Now()
defer func() {
mx.DurationMS = timeSince(t0)
}()
dialedIPs := map[string][]net.IP{}
authentic, expandedAuthentic, expandedHost, ips, dualstack, err := smtpclient.GatherIPs(opctx, log.Logger, resolver, "ip", mx.Host, dialedIPs)
mx.IP = DomainIP{timeSince(t0), authentic, expandedAuthentic, expandedHost, ips, dualstack, errmsg(err)}
if err != nil {
return
}
var daneRecords []adns.TLSA
var daneMoreHostnames []dns.Domain
if dr.MX.OrigNextHopAuthentic && dr.MX.ExpandedNextHopAuthentic && authentic {
t0dane := time.Now()
var daneRequired bool
var tlsaBaseDomain dns.Domain
daneRequired, daneRecords, tlsaBaseDomain, err = smtpclient.GatherTLSA(opctx, log.Logger, resolver, mx.Host.Domain, expandedAuthentic, expandedHost)
if err == nil {
daneMoreHostnames = smtpclient.GatherTLSANames(dr.MX.Have, dr.MX.ExpandedNextHopAuthentic, expandedAuthentic, dom, dr.MX.ExpandedNextHop, mx.Host.Domain, tlsaBaseDomain)
}
tlsarecords := make([]TLSARecord, len(daneRecords))
for i, r := range daneRecords {
tlsarecords[i] = TLSARecord{r}
}
mx.DANE = DomainDANE{timeSince(t0dane), daneRequired, tlsarecords, tlsaBaseDomain, errmsg(err), TLSARecord{}}
}
if !dial {
return
}
t0dial := time.Now()
dialer := &limitDialer{}
conn, ip, err := smtpclient.Dial(opctx, log.Logger, dialer, mx.Host, mx.IP.IPs, 25, dialedIPs, nil)
mx.Dial = DomainDial{timeSince(t0dial), ip, errmsg(err)}
if err != nil {
return
}
defer conn.Close()
tlsMode := smtpclient.TLSOpportunistic
if pkix || mx.DANE.Required {
tlsMode = smtpclient.TLSRequiredStartTLS
}
t0smtp := time.Now()
var daneVerifiedRecord adns.TLSA
var tlsrptRecipientDomainResult tlsrpt.Result
var tlsrptHostResult tlsrpt.Result
opts := smtpclient.Opts{
DANERecords: daneRecords,
DANEMoreHostnames: daneMoreHostnames,
DANEVerifiedRecord: &daneVerifiedRecord,
IgnoreTLSVerifyErrors: true, // note: not generally safe
RecipientDomainResult: &tlsrptRecipientDomainResult,
HostResult: &tlsrptHostResult,
}
th := traceHandler{Trace: []Proto{}}
tracelog := slog.New(&th)
client, err := smtpclient.New(opctx, tracelog, conn, tlsMode, pkix, dnsHostname, mx.Host.Domain, opts)
mx.SMTP.Error = errmsg(err)
if err != nil {
return
}
cs := client.TLSConnectionState()
client.Close()
mx.SMTP.DurationMS = timeSince(t0smtp)
mx.DANE.VerifiedRecord = TLSARecord{daneVerifiedRecord}
mx.SMTP.Supports8bitMIME = client.Supports8BITMIME()
mx.SMTP.SupportsRequireTLS = client.SupportsRequireTLS()
mx.SMTP.SupportsSMTPUTF8 = client.SupportsSMTPUTF8()
mx.SMTP.SupportsSTARTTLS = client.SupportsStartTLS()
if cs != nil {
mx.SMTP.TLSConnectionState = &TLSConnectionState{
Version: tlsVersionName(cs.Version),
CipherSuite: tls.CipherSuiteName(cs.CipherSuite),
NegotiatedProtocol: cs.NegotiatedProtocol,
ServerName: cs.ServerName,
}
}
mx.SMTP.RecipientDomainResult = tlsrptResult(tlsrptRecipientDomainResult)
mx.SMTP.HostResult = tlsrptResult(tlsrptHostResult)
mx.SMTP.Trace = th.Trace
}()
}
// MX.
wg.Add(1)
go func() {
defer logPanic(log)
defer wg.Done()
var mtastswg sync.WaitGroup
mtastswg.Add(1)
go func() {
defer logPanic(log)
defer mtastswg.Done()
t0 := time.Now()
record, policy, policyText, err := mtasts.Get(opctx, log.Logger, resolver, dom)
implemented := err == nil || !(errors.Is(err, mtasts.ErrNoRecord) || errors.Is(err, mtasts.ErrMultipleRecords) || errors.Is(err, mtasts.ErrRecordSyntax) || errors.Is(err, mtasts.ErrNoPolicy) || errors.Is(err, mtasts.ErrPolicyFetch) || errors.Is(err, mtasts.ErrPolicySyntax))
if errors.Is(err, mtasts.ErrNoRecord) {
err = nil
}
var mtastsRecord *MTASTSRecord
if record != nil {
mtastsRecord = &MTASTSRecord{*record}
}
dr.MTASTS = DomainMTASTS{timeSince(t0), implemented, mtastsRecord, policy, policyText, errmsg(err)}
}()
t0 := time.Now()
var hosts []dns.IPDomain
var err error
dr.MX.Have, dr.MX.OrigNextHopAuthentic, dr.MX.ExpandedNextHopAuthentic, dr.MX.ExpandedNextHop, hosts, dr.MX.Permanent, err = smtpclient.GatherDestinations(opctx, log.Logger, resolver, dns.IPDomain{Domain: dom})
dr.MX.Error = errmsg(err)
dr.MX.DurationMS = timeSince(t0)
mtastswg.Wait()
dr.MXHosts = make([]DomainMXHost, len(hosts))
for i, h := range hosts {
dr.MXHosts[i].Host = h
var pkix bool
if dr.MTASTS.Policy != nil && dr.MTASTS.Policy.Mode != mtasts.ModeNone {
pkix = true
if !dr.MTASTS.Policy.Matches(h.Domain) {
dr.MXHosts[i].MTASTSError = "MX target does not match MTA-STS policy"
}
}
checkMX(&dr.MXHosts[i], i < 2, pkix)
}
}()
wg.Wait()
dr.DurationMS = timeSince(start)
return
}
type traceHandler struct {
Attrs []slog.Attr
Trace []Proto
}
func (h *traceHandler) Enabled(ctx context.Context, level slog.Level) bool {
return level <= mlog.LevelTrace || pkglog.Handler().Enabled(ctx, level)
}
func (h *traceHandler) Handle(ctx context.Context, r slog.Record) error {
if r.Level <= mlog.LevelTrace {
p := Proto{strings.HasPrefix(r.Message, "LC: "), strings.TrimPrefix(strings.TrimPrefix(r.Message, "LC: "), "RS: ")}
h.Trace = append(h.Trace, p)
return nil
}
return pkglog.Handler().Handle(ctx, r)
}
func (h *traceHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
// We keep the same traceHandler instance so we can get the trace after logging.
h.Attrs = append([]slog.Attr{}, append(h.Attrs, attrs...)...)
return h
}
func (h *traceHandler) WithGroup(name string) slog.Handler {
// Not used, so not implemented.
return h
}
type limitDialer struct {
}
func (d *limitDialer) DialContext(ctx context.Context, network, addr string) (c net.Conn, err error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ip := net.ParseIP(host)
if ip == nil {
return nil, fmt.Errorf("address not an ip: %q", host)
}
if ratelimiter && !smtpDialLimiter.Add(ip, time.Now(), 1) {
return nil, fmt.Errorf("rate limited: reached max number of smtp connections to ip in window, try again soon")
}
nd := &net.Dialer{}
return nd.DialContext(ctx, network, addr)
}
var tlsVersions = map[uint16]string{
tls.VersionSSL30: "SSLv3",
tls.VersionTLS10: "TLS 1.0",
tls.VersionTLS11: "TLS 1.1",
tls.VersionTLS12: "TLS 1.2",
tls.VersionTLS13: "TLS 1.3",
}
// tls.VersionName was introduced in go1.21
func tlsVersionName(version uint16) string {
s, ok := tlsVersions[version]
if !ok {
return fmt.Sprintf("%04x", version)
}
return s
}