mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package dmarcdb
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
"github.com/hanzoai/mail/dmarcrpt"
|
|
"github.com/hanzoai/mail/dns"
|
|
)
|
|
|
|
var (
|
|
ReportsDBTypes = []any{DomainFeedback{}} // Types stored in DB.
|
|
ReportsDB *bstore.DB // Exported for backups.
|
|
)
|
|
|
|
var (
|
|
metricEvaluated = promauto.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "mox_dmarcdb_policy_evaluated_total",
|
|
Help: "Number of policy evaluations.",
|
|
},
|
|
// We only register validated domains for which we have a config.
|
|
[]string{"domain", "disposition", "dkim", "spf"},
|
|
)
|
|
metricDKIM = promauto.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "mox_dmarcdb_dkim_result_total",
|
|
Help: "Number of DKIM results.",
|
|
},
|
|
[]string{"result"},
|
|
)
|
|
metricSPF = promauto.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "mox_dmarcdb_spf_result_total",
|
|
Help: "Number of SPF results.",
|
|
},
|
|
[]string{"result"},
|
|
)
|
|
)
|
|
|
|
// DomainFeedback is a single report stored in the database.
|
|
type DomainFeedback struct {
|
|
ID int64
|
|
// Domain where DMARC DNS record was found, could be organizational domain.
|
|
Domain string `bstore:"index"`
|
|
// Domain in From-header.
|
|
FromDomain string `bstore:"index"`
|
|
dmarcrpt.Feedback
|
|
}
|
|
|
|
// AddReport adds a DMARC aggregate feedback report from an email to the database,
|
|
// and updates prometheus metrics.
|
|
//
|
|
// fromDomain is the domain in the report message From header.
|
|
func AddReport(ctx context.Context, f *dmarcrpt.Feedback, fromDomain dns.Domain) error {
|
|
d, err := dns.ParseDomain(f.PolicyPublished.Domain)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing domain in report: %v", err)
|
|
}
|
|
|
|
df := DomainFeedback{0, d.Name(), fromDomain.Name(), *f}
|
|
if err := ReportsDB.Insert(ctx, &df); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, r := range f.Records {
|
|
for _, dkim := range r.AuthResults.DKIM {
|
|
count := r.Row.Count
|
|
if count > 0 {
|
|
metricDKIM.With(prometheus.Labels{
|
|
"result": string(dkim.Result),
|
|
}).Add(float64(count))
|
|
}
|
|
}
|
|
|
|
for _, spf := range r.AuthResults.SPF {
|
|
count := r.Row.Count
|
|
if count > 0 {
|
|
metricSPF.With(prometheus.Labels{
|
|
"result": string(spf.Result),
|
|
}).Add(float64(count))
|
|
}
|
|
}
|
|
|
|
count := r.Row.Count
|
|
if count > 0 {
|
|
pe := r.Row.PolicyEvaluated
|
|
metricEvaluated.With(prometheus.Labels{
|
|
"domain": f.PolicyPublished.Domain,
|
|
"disposition": string(pe.Disposition),
|
|
"dkim": string(pe.DKIM),
|
|
"spf": string(pe.SPF),
|
|
}).Add(float64(count))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Records returns all reports in the database.
|
|
func Records(ctx context.Context) ([]DomainFeedback, error) {
|
|
return bstore.QueryDB[DomainFeedback](ctx, ReportsDB).List()
|
|
}
|
|
|
|
// RecordID returns the report for the ID.
|
|
func RecordID(ctx context.Context, id int64) (DomainFeedback, error) {
|
|
e := DomainFeedback{ID: id}
|
|
err := ReportsDB.Get(ctx, &e)
|
|
return e, err
|
|
}
|
|
|
|
// RecordsPeriodDomain returns the reports overlapping start and end, for the given
|
|
// domain. If domain is empty, all records match for domain.
|
|
func RecordsPeriodDomain(ctx context.Context, start, end time.Time, domain string) ([]DomainFeedback, error) {
|
|
s := start.Unix()
|
|
e := end.Unix()
|
|
|
|
q := bstore.QueryDB[DomainFeedback](ctx, ReportsDB)
|
|
if domain != "" {
|
|
q.FilterNonzero(DomainFeedback{Domain: domain})
|
|
}
|
|
q.FilterFn(func(d DomainFeedback) bool {
|
|
m := d.Feedback.ReportMetadata.DateRange
|
|
return m.Begin >= s && m.Begin < e || m.End > s && m.End <= e
|
|
})
|
|
return q.List()
|
|
}
|