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]>
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package mox
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/mail/config"
|
|
"github.com/hanzoai/mail/dkim"
|
|
"github.com/hanzoai/mail/dns"
|
|
"github.com/hanzoai/mail/mlog"
|
|
"github.com/hanzoai/mail/smtp"
|
|
)
|
|
|
|
// DKIMSelectors returns the selectors to use for signing.
|
|
func DKIMSelectors(dkimConf config.DKIM) []dkim.Selector {
|
|
var l []dkim.Selector
|
|
for _, sign := range dkimConf.Sign {
|
|
sel := dkimConf.Selectors[sign]
|
|
s := dkim.Selector{
|
|
Hash: sel.HashEffective,
|
|
HeaderRelaxed: sel.Canonicalization.HeaderRelaxed,
|
|
BodyRelaxed: sel.Canonicalization.BodyRelaxed,
|
|
Headers: sel.HeadersEffective,
|
|
SealHeaders: !sel.DontSealHeaders,
|
|
Expiration: time.Duration(sel.ExpirationSeconds) * time.Second,
|
|
PrivateKey: sel.Key,
|
|
Domain: sel.Domain,
|
|
}
|
|
l = append(l, s)
|
|
}
|
|
return l
|
|
}
|
|
|
|
// DKIMSign looks up the domain for "from", and uses its DKIM configuration to
|
|
// generate DKIM-Signature headers, for inclusion in a message. The
|
|
// DKIM-Signatur headers, are returned. If no domain was found an empty string and
|
|
// nil error is returned.
|
|
func DKIMSign(ctx context.Context, log mlog.Log, from smtp.Path, smtputf8 bool, data []byte) (string, error) {
|
|
// Add DKIM signature for domain, even if higher up than the full mail hostname.
|
|
// This helps with an assumed (because default) relaxed DKIM policy. If the DMARC
|
|
// policy happens to be strict, the signature won't help, but won't hurt either.
|
|
fd := from.IPDomain.Domain
|
|
var zerodom dns.Domain
|
|
for fd != zerodom {
|
|
confDom, ok := Conf.Domain(fd)
|
|
if !ok {
|
|
var nfd dns.Domain
|
|
_, nfd.ASCII, _ = strings.Cut(fd.ASCII, ".")
|
|
_, nfd.Unicode, _ = strings.Cut(fd.Unicode, ".")
|
|
fd = nfd
|
|
continue
|
|
}
|
|
|
|
if confDom.Disabled {
|
|
return "", ErrDomainDisabled
|
|
}
|
|
|
|
selectors := DKIMSelectors(confDom.DKIM)
|
|
dkimHeaders, err := dkim.Sign(ctx, log.Logger, from.Localpart, fd, selectors, smtputf8, bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", fmt.Errorf("dkim sign for domain %s: %v", fd, err)
|
|
}
|
|
return dkimHeaders, nil
|
|
}
|
|
return "", nil
|
|
}
|