7 "github.com/mjl-/mox/config"
8 "github.com/mjl-/mox/dns"
9 "github.com/mjl-/mox/smtp"
13 ErrDomainNotFound = errors.New("domain not found")
14 ErrAddressNotFound = errors.New("address not found")
17// FindAccount looks up the account for localpart and domain.
19// Can return ErrDomainNotFound and ErrAddressNotFound.
20func LookupAddress(localpart smtp.Localpart, domain dns.Domain, allowPostmaster, allowAlias bool) (accountName string, alias *config.Alias, canonicalAddress string, dest config.Destination, rerr error) {
21 if strings.EqualFold(string(localpart), "postmaster") {
22 localpart = "postmaster"
25 postmasterDomain := func() bool {
26 var zerodomain dns.Domain
27 if domain == zerodomain || domain == Conf.Static.HostnameDomain {
30 for _, l := range Conf.Static.Listeners {
31 if l.SMTP.Enabled && domain == l.HostnameDomain {
38 // Check for special mail host addresses.
39 if localpart == "postmaster" && postmasterDomain() {
41 return "", nil, "", config.Destination{}, ErrAddressNotFound
43 return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
45 if localpart == Conf.Static.HostTLSRPT.ParsedLocalpart && domain == Conf.Static.HostnameDomain {
46 // Get destination, should always be present.
47 canonical := smtp.NewAddress(localpart, domain).String()
48 accAddr, a, ok := Conf.AccountDestination(canonical)
50 return "", nil, "", config.Destination{}, ErrAddressNotFound
52 return accAddr.Account, nil, canonical, accAddr.Destination, nil
55 d, ok := Conf.Domain(domain)
56 if !ok || d.ReportsOnly {
57 // For ReportsOnly, we also return ErrDomainNotFound, so this domain isn't
58 // considered local/authoritative during delivery.
59 return "", nil, "", config.Destination{}, ErrDomainNotFound
62 localpart = CanonicalLocalpart(localpart, d)
63 canonical := smtp.NewAddress(localpart, domain).String()
65 accAddr, alias, ok := Conf.AccountDestination(canonical)
66 if ok && alias != nil && allowAlias {
67 return "", alias, canonical, config.Destination{}, nil
69 if accAddr, alias, ok = Conf.AccountDestination("@" + domain.Name()); !ok || alias != nil {
70 if localpart == "postmaster" && allowPostmaster {
71 return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
73 return "", nil, "", config.Destination{}, ErrAddressNotFound
75 canonical = "@" + domain.Name()
77 return accAddr.Account, nil, canonical, accAddr.Destination, nil
80// CanonicalLocalpart returns the canonical localpart, removing optional catchall
81// separator, and optionally lower-casing the string.
82func CanonicalLocalpart(localpart smtp.Localpart, d config.Domain) smtp.Localpart {
83 if d.LocalpartCatchallSeparator != "" {
84 t := strings.SplitN(string(localpart), d.LocalpartCatchallSeparator, 2)
85 localpart = smtp.Localpart(t[0])
88 if !d.LocalpartCaseSensitive {
89 localpart = smtp.Localpart(strings.ToLower(string(localpart)))
94// AllowMsgFrom returns whether account is allowed to submit messages with address
95// as message From header, based on configured addresses and membership of aliases
96// that allow using its address.
97func AllowMsgFrom(accountName string, msgFrom smtp.Address) bool {
98 accName, alias, _, _, err := LookupAddress(msgFrom.Localpart, msgFrom.Domain, false, true)
102 if alias != nil && alias.AllowMsgFrom {
103 for _, aa := range alias.ParsedAddresses {
104 if aa.AccountName == accountName {
110 return accName == accountName