10 "github.com/mjl-/mox/dns"
11 "github.com/mjl-/mox/mlog"
12 "github.com/mjl-/mox/mox-"
13 "github.com/mjl-/mox/mtasts"
16func mtastsPolicyHandle(w http.ResponseWriter, r *http.Request) {
17 log := func() mlog.Log {
18 return pkglog.WithContext(r.Context())
21 host := strings.ToLower(r.Host)
22 if !strings.HasPrefix(host, "mta-sts.") {
26 host = strings.TrimPrefix(host, "mta-sts.")
27 nhost, _, err := net.SplitHostPort(host)
29 // Only relevant for when host has a port.
32 domain, err := dns.ParseDomain(host)
34 log().Errorx("mtasts policy request: bad domain", err, slog.String("host", host))
39 conf, _ := mox.Conf.Domain(domain)
47 for _, s := range sts.MX {
49 if strings.HasPrefix(s, "*.") {
53 d, err := dns.ParseDomain(s)
55 log().Errorx("bad domain in mtasts config", err, slog.String("domain", s))
56 http.Error(w, "500 - internal server error - invalid domain in configuration", http.StatusInternalServerError)
63 mxs = []mtasts.MX{{Domain: mox.Conf.Static.HostnameDomain}}
66 policy := mtasts.Policy{
69 MaxAgeSeconds: int(sts.MaxAge / time.Second),
72 w.Header().Set("Content-Type", "text/plain")
73 w.Header().Set("Cache-Control", "no-cache, max-age=0")
74 _, _ = w.Write([]byte(policy.String()))