1// Package tlsrptdb stores reports from "SMTP TLS Reporting" in its database.
12 "github.com/prometheus/client_golang/prometheus"
13 "github.com/prometheus/client_golang/prometheus/promauto"
15 "github.com/mjl-/bstore"
17 "github.com/mjl-/mox/dns"
18 "github.com/mjl-/mox/mlog"
19 "github.com/mjl-/mox/mox-"
20 "github.com/mjl-/mox/tlsrpt"
24 metricSession = promauto.NewCounterVec(
25 prometheus.CounterOpts{
26 Name: "mox_tlsrptdb_session_total",
27 Help: "Number of sessions, both success and known result types.",
29 []string{"type"}, // Known result types, and "success"
32 knownResultTypes = map[tlsrpt.ResultType]struct{}{
33 tlsrpt.ResultSTARTTLSNotSupported: {},
34 tlsrpt.ResultCertificateHostMismatch: {},
35 tlsrpt.ResultCertificateExpired: {},
36 tlsrpt.ResultTLSAInvalid: {},
37 tlsrpt.ResultDNSSECInvalid: {},
38 tlsrpt.ResultDANERequired: {},
39 tlsrpt.ResultCertificateNotTrusted: {},
40 tlsrpt.ResultSTSPolicyInvalid: {},
41 tlsrpt.ResultSTSWebPKIInvalid: {},
42 tlsrpt.ResultValidationFailure: {},
43 tlsrpt.ResultSTSPolicyFetch: {},
47// Record is a TLS report as a database record, including information
51 Domain string `bstore:"index"` // Policy domain to which the TLS report applies. Unicode.
54 HostReport bool // Report for host TLSRPT record, as opposed to domain TLSRPT record.
58func reportDB(ctx context.Context) (rdb *bstore.DB, rerr error) {
62 p := mox.DataDirPath("tlsrpt.db")
63 os.MkdirAll(filepath.Dir(p), 0770)
64 db, err := bstore.Open(ctx, p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, ReportDBTypes...)
73// AddReport adds a TLS report to the database.
75// The report should have come in over SMTP, with a DKIM-validated
76// verifiedFromDomain. Using HTTPS for reports is not recommended as there is no
77// authentication on the reports origin.
79// Only reports for known domains are added to the database. Unknown domains are
80// ignored without causing an error, unless no known domain was found in the report
83// Prometheus metrics are updated only for configured domains.
84func AddReport(ctx context.Context, log mlog.Log, verifiedFromDomain dns.Domain, mailFrom string, hostReport bool, r *tlsrpt.Report) error {
85 db, err := reportDB(ctx)
90 if len(r.Policies) == 0 {
91 return fmt.Errorf("no policies in report")
95 return db.Write(ctx, func(tx *bstore.Tx) error {
96 for _, p := range r.Policies {
99 d, err := dns.ParseDomain(pp.Domain)
101 return fmt.Errorf("invalid domain %v in tls report: %v", d, err)
104 if _, ok := mox.Conf.Domain(d); !ok && d != mox.Conf.Static.HostnameDomain {
105 log.Info("unknown host/recipient policy domain in tls report, not storing", slog.Any("domain", d), slog.String("mailfrom", mailFrom))
109 metricSession.WithLabelValues("success").Add(float64(p.Summary.TotalSuccessfulSessionCount))
110 for _, f := range p.FailureDetails {
112 if _, ok := knownResultTypes[f.ResultType]; ok {
113 result = string(f.ResultType)
117 metricSession.WithLabelValues(result).Add(float64(f.FailedSessionCount))
120 record := Record{0, d.Name(), verifiedFromDomain.Name(), mailFrom, d == mox.Conf.Static.HostnameDomain, *r}
121 if err := tx.Insert(&record); err != nil {
122 return fmt.Errorf("inserting report for domain: %w", err)
127 return fmt.Errorf("no domains in report recognized")
133// Records returns all TLS reports in the database.
134func Records(ctx context.Context) ([]Record, error) {
135 db, err := reportDB(ctx)
139 return bstore.QueryDB[Record](ctx, db).List()
142// RecordID returns the report for the ID.
143func RecordID(ctx context.Context, id int64) (Record, error) {
144 db, err := reportDB(ctx)
150 err = db.Get(ctx, &e)
154// RecordsPeriodPolicyDomain returns the reports overlapping start and end, for the
155// given policy domain. If policy domain is empty, records for all domains are
157func RecordsPeriodDomain(ctx context.Context, start, end time.Time, policyDomain dns.Domain) ([]Record, error) {
158 db, err := reportDB(ctx)
163 q := bstore.QueryDB[Record](ctx, db)
164 var zerodom dns.Domain
165 if policyDomain != zerodom {
166 q.FilterNonzero(Record{Domain: policyDomain.Name()})
168 q.FilterFn(func(r Record) bool {
169 dr := r.Report.DateRange
170 return !dr.Start.Before(start) && dr.Start.Before(end) || dr.End.After(start) && !dr.End.After(end)