1package mox
2
3import (
4 "errors"
5 "strings"
6
7 "github.com/mjl-/mox/config"
8 "github.com/mjl-/mox/dns"
9 "github.com/mjl-/mox/smtp"
10)
11
12var (
13 ErrDomainNotFound = errors.New("domain not found")
14 ErrAddressNotFound = errors.New("address not found")
15)
16
17// FindAccount looks up the account for localpart and domain.
18//
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"
23 }
24
25 postmasterDomain := func() bool {
26 var zerodomain dns.Domain
27 if domain == zerodomain || domain == Conf.Static.HostnameDomain {
28 return true
29 }
30 for _, l := range Conf.Static.Listeners {
31 if l.SMTP.Enabled && domain == l.HostnameDomain {
32 return true
33 }
34 }
35 return false
36 }
37
38 // Check for special mail host addresses.
39 if localpart == "postmaster" && postmasterDomain() {
40 if !allowPostmaster {
41 return "", nil, "", config.Destination{}, ErrAddressNotFound
42 }
43 return Conf.Static.Postmaster.Account, nil, "postmaster", config.Destination{Mailbox: Conf.Static.Postmaster.Mailbox}, nil
44 }
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)
49 if !ok || a != nil {
50 return "", nil, "", config.Destination{}, ErrAddressNotFound
51 }
52 return accAddr.Account, nil, canonical, accAddr.Destination, nil
53 }
54
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
60 }
61
62 localpart = CanonicalLocalpart(localpart, d)
63 canonical := smtp.NewAddress(localpart, domain).String()
64
65 accAddr, alias, ok := Conf.AccountDestination(canonical)
66 if ok && alias != nil && allowAlias {
67 return "", alias, canonical, config.Destination{}, nil
68 } else if !ok {
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
72 }
73 return "", nil, "", config.Destination{}, ErrAddressNotFound
74 }
75 canonical = "@" + domain.Name()
76 }
77 return accAddr.Account, nil, canonical, accAddr.Destination, nil
78}
79
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])
86 }
87
88 if !d.LocalpartCaseSensitive {
89 localpart = smtp.Localpart(strings.ToLower(string(localpart)))
90 }
91 return localpart
92}
93
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)
99 if err != nil {
100 return false
101 }
102 if alias != nil && alias.AllowMsgFrom {
103 for _, aa := range alias.ParsedAddresses {
104 if aa.AccountName == accountName {
105 return true
106 }
107 }
108 return false
109 }
110 return accName == accountName
111}
112