mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 06:28:14 +03:00

to facilitate migrations from/to other mail setups. a domain can be added in "disabled" mode (or can be disabled/enabled later on). you can configure a disabled domain, but incoming/outgoing messages involving the domain are rejected with temporary error codes (as this may occur during a migration, remote servers will try again, hopefully to the correct machine or after this machine has been configured correctly). also, no acme tls certs will be requested for disabled domains (the autoconfig/mta-sts dns records may still point to the current/previous machine). accounts with addresses at disabled domains can still login, unless logins are disabled for their accounts. an account now has an option to disable logins. you can specify an error message to show. this will be shown in smtp, imap and the web interfaces. it could contain a message about migrations, and possibly a URL to a page with information about how to migrate. incoming/outgoing email involving accounts with login disabled are still accepted/delivered as normal (unless the domain involved in the messages is disabled too). account operations by the admin, such as importing/exporting messages still works. in the admin web interface, listings of domains/accounts show if they are disabled. domains & accounts can be enabled/disabled through the config file, cli commands and admin web interface. for issue #175 by RobSlgm
120 lines
4.1 KiB
Go
120 lines
4.1 KiB
Go
package mox
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/mjl-/mox/config"
|
|
"github.com/mjl-/mox/dns"
|
|
"github.com/mjl-/mox/smtp"
|
|
)
|
|
|
|
var (
|
|
ErrDomainNotFound = errors.New("domain not found")
|
|
ErrDomainDisabled = errors.New("message/transaction involving temporarily disabled domain")
|
|
ErrAddressNotFound = errors.New("address not found")
|
|
)
|
|
|
|
// LookupAddress looks up the account for localpart and domain.
|
|
//
|
|
// Can return ErrDomainNotFound and ErrAddressNotFound. If checkDomainDisabled is
|
|
// set, returns ErrDomainDisabled if domain is disabled.
|
|
func LookupAddress(localpart smtp.Localpart, domain dns.Domain, allowPostmaster, allowAlias, checkDomainDisabled bool) (accountName string, alias *config.Alias, canonicalAddress string, dest config.Destination, rerr error) {
|
|
if strings.EqualFold(string(localpart), "postmaster") {
|
|
localpart = "postmaster"
|
|
}
|
|
|
|
postmasterDomain := func() bool {
|
|
var zerodomain dns.Domain
|
|
if domain == zerodomain || domain == Conf.Static.HostnameDomain {
|
|
return true
|
|
}
|
|
for _, l := range Conf.Static.Listeners {
|
|
if l.SMTP.Enabled && domain == l.HostnameDomain {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Check for special mail host addresses.
|
|
if localpart == "postmaster" && postmasterDomain() {
|
|
if !allowPostmaster {
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
|
}
|
|
return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
|
|
}
|
|
if localpart == Conf.Static.HostTLSRPT.ParsedLocalpart && domain == Conf.Static.HostnameDomain {
|
|
// Get destination, should always be present.
|
|
canonical := smtp.NewAddress(localpart, domain).String()
|
|
accAddr, a, ok := Conf.AccountDestination(canonical)
|
|
if !ok || a != nil {
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
|
}
|
|
return accAddr.Account, nil, canonical, accAddr.Destination, nil
|
|
}
|
|
|
|
d, ok := Conf.Domain(domain)
|
|
if !ok || d.ReportsOnly {
|
|
// For ReportsOnly, we also return ErrDomainNotFound, so this domain isn't
|
|
// considered local/authoritative during delivery.
|
|
return "", nil, "", config.Destination{}, ErrDomainNotFound
|
|
}
|
|
if d.Disabled && checkDomainDisabled {
|
|
return "", nil, "", config.Destination{}, ErrDomainDisabled
|
|
}
|
|
|
|
localpart = CanonicalLocalpart(localpart, d)
|
|
canonical := smtp.NewAddress(localpart, domain).String()
|
|
|
|
accAddr, alias, ok := Conf.AccountDestination(canonical)
|
|
if ok && alias != nil {
|
|
if !allowAlias {
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
|
}
|
|
return "", alias, canonical, config.Destination{}, nil
|
|
} else if !ok {
|
|
if accAddr, alias, ok = Conf.AccountDestination("@" + domain.Name()); !ok || alias != nil {
|
|
if localpart == "postmaster" && allowPostmaster {
|
|
return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
|
|
}
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
|
}
|
|
canonical = "@" + domain.Name()
|
|
}
|
|
return accAddr.Account, nil, canonical, accAddr.Destination, nil
|
|
}
|
|
|
|
// CanonicalLocalpart returns the canonical localpart, removing optional catchall
|
|
// separator, and optionally lower-casing the string.
|
|
func CanonicalLocalpart(localpart smtp.Localpart, d config.Domain) smtp.Localpart {
|
|
if d.LocalpartCatchallSeparator != "" {
|
|
t := strings.SplitN(string(localpart), d.LocalpartCatchallSeparator, 2)
|
|
localpart = smtp.Localpart(t[0])
|
|
}
|
|
|
|
if !d.LocalpartCaseSensitive {
|
|
localpart = smtp.Localpart(strings.ToLower(string(localpart)))
|
|
}
|
|
return localpart
|
|
}
|
|
|
|
// AllowMsgFrom returns whether account is allowed to submit messages with address
|
|
// as message From header, based on configured addresses and membership of aliases
|
|
// that allow using its address.
|
|
func AllowMsgFrom(accountName string, msgFrom smtp.Address) (ok, domainDisabled bool) {
|
|
accName, alias, _, _, err := LookupAddress(msgFrom.Localpart, msgFrom.Domain, false, true, true)
|
|
if err != nil {
|
|
return false, errors.Is(err, ErrDomainDisabled)
|
|
}
|
|
if alias != nil && alias.AllowMsgFrom {
|
|
for _, aa := range alias.ParsedAddresses {
|
|
if aa.AccountName == accountName {
|
|
return true, false
|
|
}
|
|
}
|
|
return false, false
|
|
}
|
|
return accName == accountName, false
|
|
}
|