1package mtastsdb
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 mathrand "math/rand"
8 "runtime/debug"
9 "time"
10
11 "golang.org/x/exp/slog"
12
13 "github.com/mjl-/bstore"
14
15 "github.com/mjl-/mox/dns"
16 "github.com/mjl-/mox/metrics"
17 "github.com/mjl-/mox/mlog"
18 "github.com/mjl-/mox/mox-"
19 "github.com/mjl-/mox/mtasts"
20)
21
22func refresh() int {
23 interval := 24 * time.Hour
24 ticker := time.NewTicker(interval)
25 defer ticker.Stop()
26
27 var refreshed int
28
29 // Pro-actively refresh policies every 24 hours. ../rfc/8461:583
30 for {
31 ticker.Reset(interval)
32
33 log := mlog.New("mtastsdb", nil).WithCid(mox.Cid())
34 n, err := refresh1(mox.Context, log, dns.StrictResolver{Pkg: "mtastsdb"}, time.Sleep)
35 log.Check(err, "periodic refresh of cached mtasts policies")
36 if n > 0 {
37 refreshed += n
38 }
39
40 select {
41 case <-mox.Shutdown.Done():
42 return refreshed
43 case <-ticker.C:
44 }
45 }
46}
47
48// refresh policies that have not been updated in the past 12 hours and remove
49// policies not used for 180 days. We start with the first domain immediately, so
50// an admin can see any (configuration) issues that are logged. We spread the
51// refreshes evenly over the next 3 hours, randomizing the domains, and we add some
52// jitter to the timing. Each refresh is done in a new goroutine, so a single slow
53// refresh doesn't mess up the timing.
54func refresh1(ctx context.Context, log mlog.Log, resolver dns.Resolver, sleep func(d time.Duration)) (int, error) {
55 db, err := database(ctx)
56 if err != nil {
57 return 0, err
58 }
59
60 now := timeNow()
61 qdel := bstore.QueryDB[PolicyRecord](ctx, db)
62 qdel.FilterLess("LastUse", now.Add(-180*24*time.Hour))
63 if _, err := qdel.Delete(); err != nil {
64 return 0, fmt.Errorf("deleting old unused policies: %s", err)
65 }
66
67 qup := bstore.QueryDB[PolicyRecord](ctx, db)
68 qup.FilterLess("LastUpdate", now.Add(-12*time.Hour))
69 prs, err := qup.List()
70 if err != nil {
71 return 0, fmt.Errorf("querying policies to refresh: %s", err)
72 }
73
74 if len(prs) == 0 {
75 // Nothing to do.
76 return 0, nil
77 }
78
79 // Randomize list.
80 rand := mathrand.New(mathrand.NewSource(time.Now().UnixNano()))
81 for i := range prs {
82 if i == 0 {
83 continue
84 }
85 j := rand.Intn(i + 1)
86 prs[i], prs[j] = prs[j], prs[i]
87 }
88
89 // Launch goroutine with the refresh.
90 log.Debug("will refresh mta-sts policies over next 3 hours", slog.Int("count", len(prs)))
91 start := timeNow()
92 for i, pr := range prs {
93 go refreshDomain(ctx, log, db, resolver, pr)
94 if i < len(prs)-1 {
95 interval := 3 * int64(time.Hour) / int64(len(prs)-1)
96 extra := time.Duration(rand.Int63n(interval) - interval/2)
97 next := start.Add(time.Duration(int64(i+1)*interval) + extra)
98 d := next.Sub(timeNow())
99 if d > 0 {
100 sleep(d)
101 }
102 }
103 }
104 return len(prs), nil
105}
106
107func refreshDomain(ctx context.Context, log mlog.Log, db *bstore.DB, resolver dns.Resolver, pr PolicyRecord) {
108 defer func() {
109 x := recover()
110 if x != nil {
111 // Should not happen, but make sure errors don't take down the application.
112 log.Error("refresh1", slog.Any("panic", x))
113 debug.PrintStack()
114 metrics.PanicInc(metrics.Mtastsdb)
115 }
116 }()
117
118 ctx, cancel := context.WithTimeout(ctx, time.Minute)
119 defer cancel()
120
121 d, err := dns.ParseDomain(pr.Domain)
122 if err != nil {
123 log.Errorx("refreshing mta-sts policy: parsing policy domain", err, slog.Any("domain", d))
124 return
125 }
126 log.Debug("refreshing mta-sts policy for domain", slog.Any("domain", d))
127 record, _, err := mtasts.LookupRecord(ctx, log.Logger, resolver, d)
128 if err == nil && record.ID == pr.RecordID {
129 qup := bstore.QueryDB[PolicyRecord](ctx, db)
130 qup.FilterNonzero(PolicyRecord{Domain: pr.Domain, LastUpdate: pr.LastUpdate})
131 now := timeNow()
132 update := PolicyRecord{
133 LastUpdate: now,
134 ValidEnd: now.Add(time.Duration(pr.MaxAgeSeconds) * time.Second),
135 }
136 if n, err := qup.UpdateNonzero(update); err != nil {
137 log.Errorx("updating refreshed, unmodified policy in database", err)
138 } else if n != 1 {
139 log.Info("expected to update 1 policy after refresh", slog.Int("count", n))
140 }
141 return
142 }
143 if err != nil && pr.Mode == mtasts.ModeNone {
144 if errors.Is(err, mtasts.ErrNoRecord) {
145 // Policy was in mode "none". Now it doesn't have a policy anymore. Remove from our
146 // database so we don't keep refreshing it.
147 err := db.Delete(ctx, &pr)
148 log.Check(err, "removing mta-sts policy with mode none, dns record is gone")
149 }
150 // Else, don't bother operator with temporary error about policy none.
151 // ../rfc/8461:587
152 return
153 } else if err != nil {
154 log.Errorx("looking up mta-sts record for domain", err, slog.Any("domain", d))
155 // Try to fetch new policy. It could be just DNS that is down. We don't want to let our policy expire.
156 }
157
158 p, _, err := mtasts.FetchPolicy(ctx, log.Logger, d)
159 if err != nil {
160 if !errors.Is(err, mtasts.ErrNoPolicy) || pr.Mode != mtasts.ModeNone {
161 log.Errorx("refreshing mtasts policy for domain", err, slog.Any("domain", d))
162 }
163 return
164 }
165 now := timeNow()
166 update := map[string]any{
167 "LastUpdate": now,
168 "ValidEnd": now.Add(time.Duration(p.MaxAgeSeconds) * time.Second),
169 "Backoff": false,
170 "Policy": *p,
171 }
172 if record != nil {
173 update["RecordID"] = record.ID
174 }
175 qup := bstore.QueryDB[PolicyRecord](ctx, db)
176 qup.FilterNonzero(PolicyRecord{Domain: pr.Domain, LastUpdate: pr.LastUpdate})
177 if n, err := qup.UpdateFields(update); err != nil {
178 log.Errorx("updating refreshed, modified policy in database", err)
179 } else if n != 1 {
180 log.Info("updating refreshed, did not update 1 policy", slog.Int("count", n))
181 }
182}
183