2023-01-30 16:27:06 +03:00
|
|
|
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")
|
2024-04-24 20:15:30 +03:00
|
|
|
ErrAddressNotFound = errors.New("address not found")
|
2023-01-30 16:27:06 +03:00
|
|
|
)
|
|
|
|
|
implement tls client certificate authentication
the imap & smtp servers now allow logging in with tls client authentication and
the "external" sasl authentication mechanism. email clients like thunderbird,
fairemail, k9, macos mail implement it. this seems to be the most secure among
the authentication mechanism commonly implemented by clients. a useful property
is that an account can have a separate tls public key for each device/email
client. with tls client cert auth, authentication is also bound to the tls
connection. a mitm cannot pass the credentials on to another tls connection,
similar to scram-*-plus. though part of scram-*-plus is that clients verify
that the server knows the client credentials.
for tls client auth with imap, we send a "preauth" untagged message by default.
that puts the connection in authenticated state. given the imap connection
state machine, further authentication commands are not allowed. some clients
don't recognize the preauth message, and try to authenticate anyway, which
fails. a tls public key has a config option to disable preauth, keeping new
connections in unauthenticated state, to work with such email clients.
for smtp (submission), we don't require an explicit auth command.
both for imap and smtp, we allow a client to authenticate with another
mechanism than "external". in that case, credentials are verified, and have to
be for the same account as the tls client auth, but the adress can be another
one than the login address configured with the tls public key.
only the public key is used to identify the account that is authenticating. we
ignore the rest of the certificate. expiration dates, names, constraints, etc
are not verified. no certificate authorities are involved.
users can upload their own (minimal) certificate. the account web interface
shows openssl commands you can run to generate a private key, minimal cert, and
a p12 file (the format that email clients seem to like...) containing both
private key and certificate.
the imapclient & smtpclient packages can now also use tls client auth. and so
does "mox sendmail", either with a pem file with private key and certificate,
or with just an ed25519 private key.
there are new subcommands "mox config tlspubkey ..." for
adding/removing/listing tls public keys from the cli, by the admin.
2024-12-06 00:41:49 +03:00
|
|
|
// LookupAddress looks up the account for localpart and domain.
|
2023-01-30 16:27:06 +03:00
|
|
|
//
|
2024-04-24 20:15:30 +03:00
|
|
|
// Can return ErrDomainNotFound and ErrAddressNotFound.
|
|
|
|
func LookupAddress(localpart smtp.Localpart, domain dns.Domain, allowPostmaster, allowAlias bool) (accountName string, alias *config.Alias, canonicalAddress string, dest config.Destination, rerr error) {
|
2023-01-30 16:27:06 +03:00
|
|
|
if strings.EqualFold(string(localpart), "postmaster") {
|
|
|
|
localpart = "postmaster"
|
|
|
|
}
|
2023-04-24 13:04:46 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-12 13:37:15 +03:00
|
|
|
// Check for special mail host addresses.
|
2023-04-24 13:04:46 +03:00
|
|
|
if localpart == "postmaster" && postmasterDomain() {
|
2023-01-30 16:27:06 +03:00
|
|
|
if !allowPostmaster {
|
2024-04-24 20:15:30 +03:00
|
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
2023-11-12 13:37:15 +03:00
|
|
|
if localpart == Conf.Static.HostTLSRPT.ParsedLocalpart && domain == Conf.Static.HostnameDomain {
|
|
|
|
// Get destination, should always be present.
|
|
|
|
canonical := smtp.NewAddress(localpart, domain).String()
|
2024-04-24 20:15:30 +03:00
|
|
|
accAddr, a, ok := Conf.AccountDestination(canonical)
|
|
|
|
if !ok || a != nil {
|
|
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
2023-11-12 13:37:15 +03:00
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
return accAddr.Account, nil, canonical, accAddr.Destination, nil
|
2023-11-12 13:37:15 +03:00
|
|
|
}
|
2023-01-30 16:27:06 +03:00
|
|
|
|
|
|
|
d, ok := Conf.Domain(domain)
|
2024-01-26 21:51:23 +03:00
|
|
|
if !ok || d.ReportsOnly {
|
|
|
|
// For ReportsOnly, we also return ErrDomainNotFound, so this domain isn't
|
|
|
|
// considered local/authoritative during delivery.
|
2024-04-24 20:15:30 +03:00
|
|
|
return "", nil, "", config.Destination{}, ErrDomainNotFound
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
2024-04-24 20:15:30 +03:00
|
|
|
localpart = CanonicalLocalpart(localpart, d)
|
2023-01-30 16:27:06 +03:00
|
|
|
canonical := smtp.NewAddress(localpart, domain).String()
|
|
|
|
|
2024-04-24 20:15:30 +03:00
|
|
|
accAddr, alias, ok := Conf.AccountDestination(canonical)
|
2024-11-11 01:13:38 +03:00
|
|
|
if ok && alias != nil {
|
|
|
|
if !allowAlias {
|
|
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
return "", alias, canonical, config.Destination{}, nil
|
|
|
|
} else if !ok {
|
|
|
|
if accAddr, alias, ok = Conf.AccountDestination("@" + domain.Name()); !ok || alias != nil {
|
2023-04-24 13:04:46 +03:00
|
|
|
if localpart == "postmaster" && allowPostmaster {
|
2024-04-24 20:15:30 +03:00
|
|
|
return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
|
2023-04-24 13:04:46 +03:00
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
return "", nil, "", config.Destination{}, ErrAddressNotFound
|
2023-03-29 22:11:43 +03:00
|
|
|
}
|
|
|
|
canonical = "@" + domain.Name()
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
return accAddr.Account, nil, canonical, accAddr.Destination, nil
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanonicalLocalpart returns the canonical localpart, removing optional catchall
|
|
|
|
// separator, and optionally lower-casing the string.
|
2024-04-24 20:15:30 +03:00
|
|
|
func CanonicalLocalpart(localpart smtp.Localpart, d config.Domain) smtp.Localpart {
|
2023-01-30 16:27:06 +03:00
|
|
|
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)))
|
|
|
|
}
|
2024-04-24 20:15:30 +03:00
|
|
|
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) bool {
|
|
|
|
accName, alias, _, _, err := LookupAddress(msgFrom.Localpart, msgFrom.Domain, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if alias != nil && alias.AllowMsgFrom {
|
|
|
|
for _, aa := range alias.ParsedAddresses {
|
|
|
|
if aa.AccountName == accountName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return accName == accountName
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|