1package mox
2
3import (
4 "bytes"
5 "cmp"
6 "context"
7 "crypto"
8 "crypto/ecdsa"
9 "crypto/ed25519"
10 "crypto/elliptic"
11 cryptorand "crypto/rand"
12 "crypto/rsa"
13 "crypto/tls"
14 "crypto/x509"
15 "encoding/base64"
16 "encoding/pem"
17 "errors"
18 "fmt"
19 "io"
20 "log/slog"
21 "maps"
22 "net"
23 "net/http"
24 "net/url"
25 "os"
26 "os/user"
27 "path/filepath"
28 "regexp"
29 "slices"
30 "strconv"
31 "strings"
32 "sync"
33 "time"
34
35 "golang.org/x/text/unicode/norm"
36
37 "github.com/mjl-/autocert"
38
39 "github.com/mjl-/sconf"
40
41 "github.com/mjl-/mox/autotls"
42 "github.com/mjl-/mox/config"
43 "github.com/mjl-/mox/dkim"
44 "github.com/mjl-/mox/dns"
45 "github.com/mjl-/mox/message"
46 "github.com/mjl-/mox/mlog"
47 "github.com/mjl-/mox/moxio"
48 "github.com/mjl-/mox/mtasts"
49 "github.com/mjl-/mox/smtp"
50)
51
52var pkglog = mlog.New("mox", nil)
53
54// Pedantic enables stricter parsing.
55var Pedantic bool
56
57// Config paths are set early in program startup. They will point to files in
58// the same directory.
59var (
60 ConfigStaticPath string
61 ConfigDynamicPath string
62 Conf = Config{Log: map[string]slog.Level{"": slog.LevelError}}
63)
64
65var ErrConfig = errors.New("config error")
66
67// Set by packages webadmin, webaccount, webmail, webapisrv to prevent cyclic dependencies.
68var NewWebadminHandler = func(basePath string, isForwarded bool) http.Handler { return nopHandler }
69var NewWebaccountHandler = func(basePath string, isForwarded bool) http.Handler { return nopHandler }
70var NewWebmailHandler = func(maxMsgSize int64, basePath string, isForwarded bool, accountPath string) http.Handler {
71 return nopHandler
72}
73var NewWebapiHandler = func(maxMsgSize int64, basePath string, isForwarded bool) http.Handler { return nopHandler }
74
75var nopHandler = http.HandlerFunc(nil)
76
77// Config as used in the code, a processed version of what is in the config file.
78//
79// Use methods to lookup a domain/account/address in the dynamic configuration.
80type Config struct {
81 Static config.Static // Does not change during the lifetime of a running instance.
82
83 logMutex sync.Mutex // For accessing the log levels.
84 Log map[string]slog.Level
85
86 dynamicMutex sync.Mutex
87 Dynamic config.Dynamic // Can only be accessed directly by tests. Use methods on Config for locked access.
88 dynamicMtime time.Time
89 DynamicLastCheck time.Time // For use by quickstart only to skip checks.
90
91 // From canonical full address (localpart@domain, lower-cased when
92 // case-insensitive, stripped of catchall separator) to account and address.
93 // Domains are IDNA names in utf8. Dynamic config lock must be held when accessing.
94 AccountDestinationsLocked map[string]AccountDestination
95
96 // Like AccountDestinationsLocked, but for aliases.
97 aliases map[string]config.Alias
98}
99
100type AccountDestination struct {
101 Catchall bool // If catchall destination for its domain.
102 Localpart smtp.Localpart // In original casing as written in config file.
103 Account string
104 Destination config.Destination
105}
106
107// LogLevelSet sets a new log level for pkg. An empty pkg sets the default log
108// value that is used if no explicit log level is configured for a package.
109// This change is ephemeral, no config file is changed.
110func (c *Config) LogLevelSet(log mlog.Log, pkg string, level slog.Level) {
111 c.logMutex.Lock()
112 defer c.logMutex.Unlock()
113 l := c.copyLogLevels()
114 l[pkg] = level
115 c.Log = l
116 log.Print("log level changed", slog.String("pkg", pkg), slog.Any("level", mlog.LevelStrings[level]))
117 mlog.SetConfig(c.Log)
118}
119
120// LogLevelRemove removes a configured log level for a package.
121func (c *Config) LogLevelRemove(log mlog.Log, pkg string) {
122 c.logMutex.Lock()
123 defer c.logMutex.Unlock()
124 l := c.copyLogLevels()
125 delete(l, pkg)
126 c.Log = l
127 log.Print("log level cleared", slog.String("pkg", pkg))
128 mlog.SetConfig(c.Log)
129}
130
131// copyLogLevels returns a copy of c.Log, for modifications.
132// must be called with log lock held.
133func (c *Config) copyLogLevels() map[string]slog.Level {
134 m := map[string]slog.Level{}
135 maps.Copy(m, c.Log)
136 return m
137}
138
139// LogLevels returns a copy of the current log levels.
140func (c *Config) LogLevels() map[string]slog.Level {
141 c.logMutex.Lock()
142 defer c.logMutex.Unlock()
143 return c.copyLogLevels()
144}
145
146// DynamicLockUnlock locks the dynamic config, will try updating the latest state
147// from disk, and return an unlock function. Should be called as "defer
148// Conf.DynamicLockUnlock()()".
149func (c *Config) DynamicLockUnlock() func() {
150 c.dynamicMutex.Lock()
151 now := time.Now()
152 if now.Sub(c.DynamicLastCheck) > time.Second {
153 c.DynamicLastCheck = now
154 if fi, err := os.Stat(ConfigDynamicPath); err != nil {
155 pkglog.Errorx("stat domains config", err)
156 } else if !fi.ModTime().Equal(c.dynamicMtime) {
157 if errs := c.loadDynamic(); len(errs) > 0 {
158 pkglog.Errorx("loading domains config", errs[0], slog.Any("errors", errs))
159 } else {
160 pkglog.Info("domains config reloaded")
161 c.dynamicMtime = fi.ModTime()
162 }
163 }
164 }
165 return c.dynamicMutex.Unlock
166}
167
168func (c *Config) withDynamicLock(fn func()) {
169 defer c.DynamicLockUnlock()()
170 fn()
171}
172
173// must be called with dynamic lock held.
174func (c *Config) loadDynamic() []error {
175 d, mtime, accDests, aliases, err := ParseDynamicConfig(context.Background(), pkglog, ConfigDynamicPath, c.Static)
176 if err != nil {
177 return err
178 }
179 c.Dynamic = d
180 c.dynamicMtime = mtime
181 c.AccountDestinationsLocked = accDests
182 c.aliases = aliases
183 c.allowACMEHosts(pkglog, true)
184 return nil
185}
186
187// DynamicConfig returns a shallow copy of the dynamic config. Must not be modified.
188func (c *Config) DynamicConfig() (config config.Dynamic) {
189 c.withDynamicLock(func() {
190 config = c.Dynamic // Shallow copy.
191 })
192 return
193}
194
195func (c *Config) Domains() (l []string) {
196 c.withDynamicLock(func() {
197 for name := range c.Dynamic.Domains {
198 l = append(l, name)
199 }
200 })
201 slices.Sort(l)
202 return l
203}
204
205func (c *Config) Accounts() (l []string) {
206 c.withDynamicLock(func() {
207 for name := range c.Dynamic.Accounts {
208 l = append(l, name)
209 }
210 })
211 return
212}
213
214func (c *Config) AccountsDisabled() (all, disabled []string) {
215 c.withDynamicLock(func() {
216 for name, conf := range c.Dynamic.Accounts {
217 all = append(all, name)
218 if conf.LoginDisabled != "" {
219 disabled = append(disabled, name)
220 }
221 }
222 })
223 return
224}
225
226// DomainLocalparts returns a mapping of encoded localparts to account names for a
227// domain, and encoded localparts to aliases. An empty localpart is a catchall
228// destination for a domain.
229func (c *Config) DomainLocalparts(d dns.Domain) (map[string]string, map[string]config.Alias) {
230 suffix := "@" + d.Name()
231 m := map[string]string{}
232 aliases := map[string]config.Alias{}
233 c.withDynamicLock(func() {
234 for addr, ad := range c.AccountDestinationsLocked {
235 if strings.HasSuffix(addr, suffix) {
236 if ad.Catchall {
237 m[""] = ad.Account
238 } else {
239 m[ad.Localpart.String()] = ad.Account
240 }
241 }
242 }
243 for addr, a := range c.aliases {
244 if strings.HasSuffix(addr, suffix) {
245 aliases[a.LocalpartStr] = a
246 }
247 }
248 })
249 return m, aliases
250}
251
252func (c *Config) Domain(d dns.Domain) (dom config.Domain, ok bool) {
253 c.withDynamicLock(func() {
254 dom, ok = c.Dynamic.Domains[d.Name()]
255 })
256 return
257}
258
259func (c *Config) DomainConfigs() (doms []config.Domain) {
260 c.withDynamicLock(func() {
261 doms = make([]config.Domain, 0, len(c.Dynamic.Domains))
262 for _, d := range c.Dynamic.Domains {
263 doms = append(doms, d)
264 }
265 })
266 slices.SortFunc(doms, func(a, b config.Domain) int {
267 return cmp.Compare(a.Domain.Name(), b.Domain.Name())
268 })
269 return
270}
271
272func (c *Config) Account(name string) (acc config.Account, ok bool) {
273 c.withDynamicLock(func() {
274 acc, ok = c.Dynamic.Accounts[name]
275 })
276 return
277}
278
279func (c *Config) AccountDestination(addr string) (accDest AccountDestination, alias *config.Alias, ok bool) {
280 c.withDynamicLock(func() {
281 accDest, ok = c.AccountDestinationsLocked[addr]
282 if !ok {
283 var a config.Alias
284 a, ok = c.aliases[addr]
285 if ok {
286 alias = &a
287 }
288 }
289 })
290 return
291}
292
293func (c *Config) Routes(accountName string, domain dns.Domain) (accountRoutes, domainRoutes, globalRoutes []config.Route) {
294 c.withDynamicLock(func() {
295 acc := c.Dynamic.Accounts[accountName]
296 accountRoutes = acc.Routes
297
298 dom := c.Dynamic.Domains[domain.Name()]
299 domainRoutes = dom.Routes
300
301 globalRoutes = c.Dynamic.Routes
302 })
303 return
304}
305
306func (c *Config) IsClientSettingsDomain(d dns.Domain) (is bool) {
307 c.withDynamicLock(func() {
308 _, is = c.Dynamic.ClientSettingDomains[d]
309 })
310 return
311}
312
313func (c *Config) allowACMEHosts(log mlog.Log, checkACMEHosts bool) {
314 managerHosts := map[*autotls.Manager]map[dns.Domain]struct{}{}
315
316 for _, l := range c.Static.Listeners {
317 if l.TLS == nil || l.TLS.ACME == "" {
318 continue
319 }
320
321 m := c.Static.ACME[l.TLS.ACME].Manager
322 if managerHosts[m] == nil {
323 managerHosts[m] = map[dns.Domain]struct{}{}
324 }
325 hostnames := managerHosts[m]
326
327 hostnames[c.Static.HostnameDomain] = struct{}{}
328 if l.HostnameDomain.ASCII != "" {
329 hostnames[l.HostnameDomain] = struct{}{}
330 }
331
332 for _, dom := range c.Dynamic.Domains {
333 // Do not allow TLS certificates for domains for which we only accept DMARC/TLS
334 // reports as external party.
335 if dom.ReportsOnly {
336 continue
337 }
338
339 // Do not fetch TLS certs for disabled domains. The A/AAAA records may not be
340 // configured or still point to a previous machine before a migration.
341 if dom.Disabled {
342 continue
343 }
344
345 if l.AutoconfigHTTPS.Enabled && !l.AutoconfigHTTPS.NonTLS {
346 if d, err := dns.ParseDomain("autoconfig." + dom.Domain.ASCII); err != nil {
347 log.Errorx("parsing autoconfig domain", err, slog.Any("domain", dom.Domain))
348 } else {
349 hostnames[d] = struct{}{}
350 }
351 }
352
353 if l.MTASTSHTTPS.Enabled && dom.MTASTS != nil && !l.MTASTSHTTPS.NonTLS {
354 d, err := dns.ParseDomain("mta-sts." + dom.Domain.ASCII)
355 if err != nil {
356 log.Errorx("parsing mta-sts domain", err, slog.Any("domain", dom.Domain))
357 } else {
358 hostnames[d] = struct{}{}
359 }
360 }
361
362 if dom.ClientSettingsDomain != "" {
363 hostnames[dom.ClientSettingsDNSDomain] = struct{}{}
364 }
365 }
366
367 if l.WebserverHTTPS.Enabled {
368 for from := range c.Dynamic.WebDNSDomainRedirects {
369 hostnames[from] = struct{}{}
370 }
371 for _, wh := range c.Dynamic.WebHandlers {
372 hostnames[wh.DNSDomain] = struct{}{}
373 }
374 }
375
376 }
377
378 public := c.Static.Listeners["public"]
379 ips := public.IPs
380 if len(public.NATIPs) > 0 {
381 ips = public.NATIPs
382 }
383 if public.IPsNATed {
384 ips = nil
385 }
386
387 for m, hostnames := range managerHosts {
388 m.SetAllowedHostnames(log, dns.StrictResolver{Pkg: "autotls", Log: log.Logger}, hostnames, ips, checkACMEHosts)
389 }
390}
391
392// todo future: write config parsing & writing code that can read a config and remembers the exact tokens including newlines and comments, and can write back a modified file. the goal is to be able to write a config file automatically (after changing fields through the ui), but not loose comments and whitespace, to still get useful diffs for storing the config in a version control system.
393
394// WriteDynamicLocked prepares an updated internal state for the new dynamic
395// config, then writes it to disk and activates it.
396//
397// Returns ErrConfig if the configuration is not valid.
398//
399// Must be called with config lock held.
400func WriteDynamicLocked(ctx context.Context, log mlog.Log, c config.Dynamic) error {
401 accDests, aliases, errs := prepareDynamicConfig(ctx, log, ConfigDynamicPath, Conf.Static, &c)
402 if len(errs) > 0 {
403 errstrs := make([]string, len(errs))
404 for i, err := range errs {
405 errstrs[i] = err.Error()
406 }
407 return fmt.Errorf("%w: %s", ErrConfig, strings.Join(errstrs, "; "))
408 }
409
410 var b bytes.Buffer
411 err := sconf.Write(&b, c)
412 if err != nil {
413 return err
414 }
415 f, err := os.OpenFile(ConfigDynamicPath, os.O_WRONLY, 0660)
416 if err != nil {
417 return err
418 }
419 defer func() {
420 if f != nil {
421 err := f.Close()
422 log.Check(err, "closing file after error")
423 }
424 }()
425 buf := b.Bytes()
426 if _, err := f.Write(buf); err != nil {
427 return fmt.Errorf("write domains.conf: %v", err)
428 }
429 if err := f.Truncate(int64(len(buf))); err != nil {
430 return fmt.Errorf("truncate domains.conf after write: %v", err)
431 }
432 if err := f.Sync(); err != nil {
433 return fmt.Errorf("sync domains.conf after write: %v", err)
434 }
435 if err := moxio.SyncDir(log, filepath.Dir(ConfigDynamicPath)); err != nil {
436 return fmt.Errorf("sync dir of domains.conf after write: %v", err)
437 }
438
439 fi, err := f.Stat()
440 if err != nil {
441 return fmt.Errorf("stat after writing domains.conf: %v", err)
442 }
443
444 if err := f.Close(); err != nil {
445 return fmt.Errorf("close written domains.conf: %v", err)
446 }
447 f = nil
448
449 Conf.dynamicMtime = fi.ModTime()
450 Conf.DynamicLastCheck = time.Now()
451 Conf.Dynamic = c
452 Conf.AccountDestinationsLocked = accDests
453 Conf.aliases = aliases
454
455 Conf.allowACMEHosts(log, true)
456
457 return nil
458}
459
460// MustLoadConfig loads the config, quitting on errors.
461func MustLoadConfig(doLoadTLSKeyCerts, checkACMEHosts bool) {
462 errs := LoadConfig(context.Background(), pkglog, doLoadTLSKeyCerts, checkACMEHosts)
463 if len(errs) > 1 {
464 pkglog.Error("loading config file: multiple errors")
465 for _, err := range errs {
466 pkglog.Errorx("config error", err)
467 }
468 pkglog.Fatal("stopping after multiple config errors")
469 } else if len(errs) == 1 {
470 pkglog.Fatalx("loading config file", errs[0])
471 }
472}
473
474// LoadConfig attempts to parse and load a config, returning any errors
475// encountered.
476func LoadConfig(ctx context.Context, log mlog.Log, doLoadTLSKeyCerts, checkACMEHosts bool) []error {
477 Shutdown, ShutdownCancel = context.WithCancel(context.Background())
478 Context, ContextCancel = context.WithCancel(context.Background())
479
480 c, errs := ParseConfig(ctx, log, ConfigStaticPath, false, doLoadTLSKeyCerts, checkACMEHosts)
481 if len(errs) > 0 {
482 return errs
483 }
484
485 mlog.SetConfig(c.Log)
486 SetConfig(c)
487 return nil
488}
489
490// SetConfig sets a new config. Not to be used during normal operation.
491func SetConfig(c *Config) {
492 // Cannot just assign *c to Conf, it would copy the mutex.
493 Conf = Config{c.Static, sync.Mutex{}, c.Log, sync.Mutex{}, c.Dynamic, c.dynamicMtime, c.DynamicLastCheck, c.AccountDestinationsLocked, c.aliases}
494
495 // If we have non-standard CA roots, use them for all HTTPS requests.
496 if Conf.Static.TLS.CertPool != nil {
497 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
498 RootCAs: Conf.Static.TLS.CertPool,
499 }
500 }
501
502 SetPedantic(c.Static.Pedantic)
503}
504
505// Set pedantic in all packages.
506func SetPedantic(p bool) {
507 dkim.Pedantic = p
508 dns.Pedantic = p
509 message.Pedantic = p
510 smtp.Pedantic = p
511 Pedantic = p
512}
513
514// ParseConfig parses the static config at path p. If checkOnly is true, no changes
515// are made, such as registering ACME identities. If doLoadTLSKeyCerts is true,
516// the TLS KeyCerts configuration is loaded and checked. This is used during the
517// quickstart in the case the user is going to provide their own certificates.
518// If checkACMEHosts is true, the hosts allowed for acme are compared with the
519// explicitly configured ips we are listening on.
520func ParseConfig(ctx context.Context, log mlog.Log, p string, checkOnly, doLoadTLSKeyCerts, checkACMEHosts bool) (c *Config, errs []error) {
521 c = &Config{
522 Static: config.Static{
523 DataDir: ".",
524 },
525 }
526
527 f, err := os.Open(p)
528 if err != nil {
529 if os.IsNotExist(err) && os.Getenv("MOXCONF") == "" {
530 return nil, []error{fmt.Errorf("open config file: %v (hint: use mox -config ... or set MOXCONF=...)", err)}
531 }
532 return nil, []error{fmt.Errorf("open config file: %v", err)}
533 }
534 defer f.Close()
535 if err := sconf.Parse(f, &c.Static); err != nil {
536 return nil, []error{fmt.Errorf("parsing %s%v", p, err)}
537 }
538
539 if xerrs := PrepareStaticConfig(ctx, log, p, c, checkOnly, doLoadTLSKeyCerts); len(xerrs) > 0 {
540 return nil, xerrs
541 }
542
543 pp := filepath.Join(filepath.Dir(p), "domains.conf")
544 c.Dynamic, c.dynamicMtime, c.AccountDestinationsLocked, c.aliases, errs = ParseDynamicConfig(ctx, log, pp, c.Static)
545
546 if !checkOnly {
547 c.allowACMEHosts(log, checkACMEHosts)
548 }
549
550 return c, errs
551}
552
553// PrepareStaticConfig parses the static config file and prepares data structures
554// for starting mox. If checkOnly is set no substantial changes are made, like
555// creating an ACME registration.
556func PrepareStaticConfig(ctx context.Context, log mlog.Log, configFile string, conf *Config, checkOnly, doLoadTLSKeyCerts bool) (errs []error) {
557 addErrorf := func(format string, args ...any) {
558 errs = append(errs, fmt.Errorf(format, args...))
559 }
560
561 c := &conf.Static
562
563 // check that mailbox is in unicode NFC normalized form.
564 checkMailboxNormf := func(mailbox string, format string, args ...any) {
565 s := norm.NFC.String(mailbox)
566 if mailbox != s {
567 msg := fmt.Sprintf(format, args...)
568 addErrorf("%s: mailbox %q is not in NFC normalized form, should be %q", msg, mailbox, s)
569 }
570 }
571
572 // Post-process logging config.
573 if logLevel, ok := mlog.Levels[c.LogLevel]; ok {
574 conf.Log = map[string]slog.Level{"": logLevel}
575 } else {
576 addErrorf("invalid log level %q", c.LogLevel)
577 }
578 for pkg, s := range c.PackageLogLevels {
579 if logLevel, ok := mlog.Levels[s]; ok {
580 conf.Log[pkg] = logLevel
581 } else {
582 addErrorf("invalid package log level %q", s)
583 }
584 }
585
586 if c.User == "" {
587 c.User = "mox"
588 }
589 u, err := user.Lookup(c.User)
590 if err != nil {
591 uid, err := strconv.ParseUint(c.User, 10, 32)
592 if err != nil {
593 addErrorf("parsing unknown user %s as uid: %v (hint: add user mox with \"useradd -d $PWD mox\" or specify a different username on the quickstart command-line)", c.User, err)
594 } else {
595 // We assume the same gid as uid.
596 c.UID = uint32(uid)
597 c.GID = uint32(uid)
598 }
599 } else {
600 if uid, err := strconv.ParseUint(u.Uid, 10, 32); err != nil {
601 addErrorf("parsing uid %s: %v", u.Uid, err)
602 } else {
603 c.UID = uint32(uid)
604 }
605 if gid, err := strconv.ParseUint(u.Gid, 10, 32); err != nil {
606 addErrorf("parsing gid %s: %v", u.Gid, err)
607 } else {
608 c.GID = uint32(gid)
609 }
610 }
611
612 hostname, err := dns.ParseDomain(c.Hostname)
613 if err != nil {
614 addErrorf("parsing hostname: %s", err)
615 } else if hostname.Name() != c.Hostname {
616 addErrorf("hostname must be in unicode form %q instead of %q", hostname.Name(), c.Hostname)
617 }
618 c.HostnameDomain = hostname
619
620 if c.HostTLSRPT.Account != "" {
621 tlsrptLocalpart, err := smtp.ParseLocalpart(c.HostTLSRPT.Localpart)
622 if err != nil {
623 addErrorf("invalid localpart %q for host tlsrpt: %v", c.HostTLSRPT.Localpart, err)
624 } else if tlsrptLocalpart.IsInternational() {
625 // Does not appear documented in ../rfc/8460, but similar to DMARC it makes sense
626 // to keep this ascii-only addresses.
627 addErrorf("host TLSRPT localpart %q is an internationalized address, only conventional ascii-only address allowed for interopability", tlsrptLocalpart)
628 }
629 c.HostTLSRPT.ParsedLocalpart = tlsrptLocalpart
630 }
631
632 // Return private key for host name for use with an ACME. Used to return the same
633 // private key as pre-generated for use with DANE, with its public key in DNS.
634 // We only use this key for Listener's that have this ACME configured, and for
635 // which the effective listener host name (either specific to the listener, or the
636 // global name) is requested. Other host names can get a fresh private key, they
637 // don't appear in DANE records.
638 //
639 // - run 0: only use listener with explicitly matching host name in listener
640 // (default quickstart config does not set it).
641 // - run 1: only look at public listener (and host matching mox host name)
642 // - run 2: all listeners (and host matching mox host name)
643 findACMEHostPrivateKey := func(acmeName, host string, keyType autocert.KeyType, run int) crypto.Signer {
644 for listenerName, l := range Conf.Static.Listeners {
645 if l.TLS == nil || l.TLS.ACME != acmeName {
646 continue
647 }
648 if run == 0 && host != l.HostnameDomain.ASCII {
649 continue
650 }
651 if run == 1 && listenerName != "public" || host != Conf.Static.HostnameDomain.ASCII {
652 continue
653 }
654 switch keyType {
655 case autocert.KeyRSA2048:
656 if len(l.TLS.HostPrivateRSA2048Keys) == 0 {
657 continue
658 }
659 return l.TLS.HostPrivateRSA2048Keys[0]
660 case autocert.KeyECDSAP256:
661 if len(l.TLS.HostPrivateECDSAP256Keys) == 0 {
662 continue
663 }
664 return l.TLS.HostPrivateECDSAP256Keys[0]
665 default:
666 return nil
667 }
668 }
669 return nil
670 }
671 // Make a function for an autocert.Manager.GetPrivateKey, using findACMEHostPrivateKey.
672 makeGetPrivateKey := func(acmeName string) func(host string, keyType autocert.KeyType) (crypto.Signer, error) {
673 return func(host string, keyType autocert.KeyType) (crypto.Signer, error) {
674 key := findACMEHostPrivateKey(acmeName, host, keyType, 0)
675 if key == nil {
676 key = findACMEHostPrivateKey(acmeName, host, keyType, 1)
677 }
678 if key == nil {
679 key = findACMEHostPrivateKey(acmeName, host, keyType, 2)
680 }
681 if key != nil {
682 log.Debug("found existing private key for certificate for host",
683 slog.String("acmename", acmeName),
684 slog.String("host", host),
685 slog.Any("keytype", keyType))
686 return key, nil
687 }
688 log.Debug("generating new private key for certificate for host",
689 slog.String("acmename", acmeName),
690 slog.String("host", host),
691 slog.Any("keytype", keyType))
692 switch keyType {
693 case autocert.KeyRSA2048:
694 return rsa.GenerateKey(cryptorand.Reader, 2048)
695 case autocert.KeyECDSAP256:
696 return ecdsa.GenerateKey(elliptic.P256(), cryptorand.Reader)
697 default:
698 return nil, fmt.Errorf("unrecognized requested key type %v", keyType)
699 }
700 }
701 }
702 for name, acme := range c.ACME {
703 addAcmeErrorf := func(format string, args ...any) {
704 addErrorf("acme provider %s: %s", name, fmt.Sprintf(format, args...))
705 }
706
707 var eabKeyID string
708 var eabKey []byte
709 if acme.ExternalAccountBinding != nil {
710 eabKeyID = acme.ExternalAccountBinding.KeyID
711 p := configDirPath(configFile, acme.ExternalAccountBinding.KeyFile)
712 buf, err := os.ReadFile(p)
713 if err != nil {
714 addAcmeErrorf("reading external account binding key: %s", err)
715 } else {
716 dec := make([]byte, base64.RawURLEncoding.DecodedLen(len(buf)))
717 n, err := base64.RawURLEncoding.Decode(dec, buf)
718 if err != nil {
719 addAcmeErrorf("parsing external account binding key as base64: %s", err)
720 } else {
721 eabKey = dec[:n]
722 }
723 }
724 }
725
726 if checkOnly {
727 continue
728 }
729
730 acmeDir := dataDirPath(configFile, c.DataDir, "acme")
731 os.MkdirAll(acmeDir, 0770)
732 manager, err := autotls.Load(log, name, acmeDir, acme.ContactEmail, acme.DirectoryURL, eabKeyID, eabKey, makeGetPrivateKey(name), Shutdown.Done())
733 if err != nil {
734 addAcmeErrorf("loading ACME identity: %s", err)
735 }
736 acme.Manager = manager
737
738 // Help configurations from older quickstarts.
739 if acme.IssuerDomainName == "" && acme.DirectoryURL == "https://acme-v02.api.letsencrypt.org/directory" {
740 acme.IssuerDomainName = "letsencrypt.org"
741 }
742
743 c.ACME[name] = acme
744 }
745
746 var haveUnspecifiedSMTPListener bool
747 for name, l := range c.Listeners {
748 addListenerErrorf := func(format string, args ...any) {
749 addErrorf("listener %s: %w", name, fmt.Errorf(format, args...))
750 }
751
752 if l.Hostname != "" {
753 d, err := dns.ParseDomain(l.Hostname)
754 if err != nil {
755 addListenerErrorf("parsing hostname %q: %s", l.Hostname, err)
756 }
757 l.HostnameDomain = d
758 }
759 if l.TLS != nil {
760 if l.TLS.ACME != "" && len(l.TLS.KeyCerts) != 0 {
761 addListenerErrorf("cannot have ACME and static key/certificates")
762 } else if l.TLS.ACME != "" {
763 acme, ok := c.ACME[l.TLS.ACME]
764 if !ok {
765 addListenerErrorf("unknown ACME provider %q", l.TLS.ACME)
766 }
767
768 // If only checking or with missing ACME definition, we don't have an acme manager,
769 // so set an empty tls config to continue.
770 var tlsconfig, tlsconfigFallback *tls.Config
771 if checkOnly || acme.Manager == nil {
772 tlsconfig = &tls.Config{}
773 tlsconfigFallback = &tls.Config{}
774 } else {
775 hostname := c.HostnameDomain
776 if l.Hostname != "" {
777 hostname = l.HostnameDomain
778 }
779 // If SNI is absent, we will use the listener hostname, but reject connections with
780 // an SNI hostname that is not allowlisted.
781 // Incoming SMTP deliveries use tlsconfigFallback for interoperability. TLS
782 // connections for unknown SNI hostnames fall back to a certificate for the
783 // listener hostname instead of causing the TLS connection to fail.
784 tlsconfig = acme.Manager.TLSConfig(hostname, true, false)
785 tlsconfigFallback = acme.Manager.TLSConfig(hostname, true, true)
786 l.TLS.ACMEConfig = acme.Manager.ACMETLSConfig
787 }
788 l.TLS.Config = tlsconfig
789 l.TLS.ConfigFallback = tlsconfigFallback
790 } else if len(l.TLS.KeyCerts) != 0 {
791 if doLoadTLSKeyCerts {
792 if err := loadTLSKeyCerts(configFile, "listener "+name, l.TLS); err != nil {
793 addListenerErrorf("%w", err)
794 }
795 }
796 } else {
797 addListenerErrorf("cannot have TLS config without ACME and without static keys/certificates")
798 }
799 for _, privKeyFile := range l.TLS.HostPrivateKeyFiles {
800 keyPath := configDirPath(configFile, privKeyFile)
801 privKey, err := loadPrivateKeyFile(keyPath)
802 if err != nil {
803 addListenerErrorf("parsing host private key for DANE and ACME certificates: %v", err)
804 continue
805 }
806 switch k := privKey.(type) {
807 case *rsa.PrivateKey:
808 if k.N.BitLen() != 2048 {
809 log.Error("need rsa key with 2048 bits, for host private key for DANE/ACME certificates, ignoring",
810 slog.String("listener", name),
811 slog.String("file", keyPath),
812 slog.Int("bits", k.N.BitLen()))
813 continue
814 }
815 l.TLS.HostPrivateRSA2048Keys = append(l.TLS.HostPrivateRSA2048Keys, k)
816 case *ecdsa.PrivateKey:
817 if k.Curve != elliptic.P256() {
818 log.Error("unrecognized ecdsa curve for host private key for DANE/ACME certificates, ignoring", slog.String("listener", name), slog.String("file", keyPath))
819 continue
820 }
821 l.TLS.HostPrivateECDSAP256Keys = append(l.TLS.HostPrivateECDSAP256Keys, k)
822 default:
823 log.Error("unrecognized key type for host private key for DANE/ACME certificates, ignoring",
824 slog.String("listener", name),
825 slog.String("file", keyPath),
826 slog.String("keytype", fmt.Sprintf("%T", privKey)))
827 continue
828 }
829 }
830 if l.TLS.ACME != "" && (len(l.TLS.HostPrivateRSA2048Keys) == 0) != (len(l.TLS.HostPrivateECDSAP256Keys) == 0) {
831 log.Warn("uncommon configuration with either only an RSA 2048 or ECDSA P256 host private key for DANE/ACME certificates; this ACME implementation can retrieve certificates for both type of keys, it is recommended to set either both or none; continuing")
832 }
833
834 // TLS 1.2 was introduced in 2008. TLS <1.2 was deprecated by ../rfc/8996:31 and ../rfc/8997:66 in 2021.
835 var minVersion uint16 = tls.VersionTLS12
836 if l.TLS.MinVersion != "" {
837 versions := map[string]uint16{
838 "TLSv1.0": tls.VersionTLS10,
839 "TLSv1.1": tls.VersionTLS11,
840 "TLSv1.2": tls.VersionTLS12,
841 "TLSv1.3": tls.VersionTLS13,
842 }
843 v, ok := versions[l.TLS.MinVersion]
844 if !ok {
845 addListenerErrorf("unknown TLS mininum version %q", l.TLS.MinVersion)
846 }
847 minVersion = v
848 }
849 if l.TLS.Config != nil {
850 l.TLS.Config.MinVersion = minVersion
851 }
852 if l.TLS.ConfigFallback != nil {
853 l.TLS.ConfigFallback.MinVersion = minVersion
854 }
855 if l.TLS.ACMEConfig != nil {
856 l.TLS.ACMEConfig.MinVersion = minVersion
857 }
858 } else {
859 var needsTLS []string
860 needtls := func(s string, v bool) {
861 if v {
862 needsTLS = append(needsTLS, s)
863 }
864 }
865 needtls("IMAPS", l.IMAPS.Enabled)
866 needtls("SMTP", l.SMTP.Enabled && !l.SMTP.NoSTARTTLS)
867 needtls("Submissions", l.Submissions.Enabled)
868 needtls("Submission", l.Submission.Enabled && !l.Submission.NoRequireSTARTTLS)
869 needtls("AccountHTTPS", l.AccountHTTPS.Enabled)
870 needtls("AdminHTTPS", l.AdminHTTPS.Enabled)
871 needtls("AutoconfigHTTPS", l.AutoconfigHTTPS.Enabled && !l.AutoconfigHTTPS.NonTLS)
872 needtls("MTASTSHTTPS", l.MTASTSHTTPS.Enabled && !l.MTASTSHTTPS.NonTLS)
873 needtls("WebserverHTTPS", l.WebserverHTTPS.Enabled)
874 if len(needsTLS) > 0 {
875 addListenerErrorf("no tls config specified, but requires tls for %s", strings.Join(needsTLS, ", "))
876 }
877 }
878 if l.AutoconfigHTTPS.Enabled && l.MTASTSHTTPS.Enabled && l.AutoconfigHTTPS.Port == l.MTASTSHTTPS.Port && l.AutoconfigHTTPS.NonTLS != l.MTASTSHTTPS.NonTLS {
879 addListenerErrorf("autoconfig and mta-sts enabled on same port but with both http and https")
880 }
881 if l.SMTP.Enabled {
882 if len(l.IPs) == 0 {
883 haveUnspecifiedSMTPListener = true
884 }
885 for _, ipstr := range l.IPs {
886 ip := net.ParseIP(ipstr)
887 if ip == nil {
888 addListenerErrorf("invalid IP %q", ipstr)
889 continue
890 }
891 if ip.IsUnspecified() {
892 haveUnspecifiedSMTPListener = true
893 break
894 }
895 if len(c.SpecifiedSMTPListenIPs) >= 2 {
896 haveUnspecifiedSMTPListener = true
897 } else if len(c.SpecifiedSMTPListenIPs) > 0 && (c.SpecifiedSMTPListenIPs[0].To4() == nil) == (ip.To4() == nil) {
898 haveUnspecifiedSMTPListener = true
899 } else {
900 c.SpecifiedSMTPListenIPs = append(c.SpecifiedSMTPListenIPs, ip)
901 }
902 }
903 }
904 for _, s := range l.SMTP.DNSBLs {
905 d, err := dns.ParseDomain(s)
906 if err != nil {
907 addListenerErrorf("parsing DNSBL zone %q: %s", s, err)
908 continue
909 }
910 l.SMTP.DNSBLZones = append(l.SMTP.DNSBLZones, d)
911 }
912 if l.IPsNATed && len(l.NATIPs) > 0 {
913 addListenerErrorf("both IPsNATed and NATIPs configued (remove deprecated IPsNATed)")
914 }
915 for _, ipstr := range l.NATIPs {
916 ip := net.ParseIP(ipstr)
917 if ip == nil {
918 addListenerErrorf("invalid ip %q", ipstr)
919 } else if ip.IsUnspecified() || ip.IsLoopback() {
920 addListenerErrorf("NAT ip that is the unspecified or loopback address %s", ipstr)
921 }
922 }
923 cleanPath := func(kind string, enabled bool, path string) string {
924 if !enabled {
925 return path
926 }
927 if path != "" && !strings.HasPrefix(path, "/") {
928 addListenerErrorf("%s with path %q that must start with a slash", kind, path)
929 } else if path != "" && !strings.HasSuffix(path, "/") {
930 log.Warn("http service path should end with a slash, using effective path with slash", slog.String("kind", kind), slog.String("path", path), slog.String("effectivepath", path+"/"))
931 path += "/"
932 }
933 return path
934 }
935 l.AccountHTTP.Path = cleanPath("AccountHTTP", l.AccountHTTP.Enabled, l.AccountHTTP.Path)
936 l.AccountHTTPS.Path = cleanPath("AccountHTTPS", l.AccountHTTPS.Enabled, l.AccountHTTPS.Path)
937 l.AdminHTTP.Path = cleanPath("AdminHTTP", l.AdminHTTP.Enabled, l.AdminHTTP.Path)
938 l.AdminHTTPS.Path = cleanPath("AdminHTTPS", l.AdminHTTPS.Enabled, l.AdminHTTPS.Path)
939 l.WebmailHTTP.Path = cleanPath("WebmailHTTP", l.WebmailHTTP.Enabled, l.WebmailHTTP.Path)
940 l.WebmailHTTPS.Path = cleanPath("WebmailHTTPS", l.WebmailHTTPS.Enabled, l.WebmailHTTPS.Path)
941 l.WebAPIHTTP.Path = cleanPath("WebAPIHTTP", l.WebAPIHTTP.Enabled, l.WebAPIHTTP.Path)
942 l.WebAPIHTTPS.Path = cleanPath("WebAPIHTTPS", l.WebAPIHTTPS.Enabled, l.WebAPIHTTPS.Path)
943 c.Listeners[name] = l
944 }
945 if haveUnspecifiedSMTPListener {
946 c.SpecifiedSMTPListenIPs = nil
947 }
948
949 var zerouse config.SpecialUseMailboxes
950 if len(c.DefaultMailboxes) > 0 && (c.InitialMailboxes.SpecialUse != zerouse || len(c.InitialMailboxes.Regular) > 0) {
951 addErrorf("cannot have both DefaultMailboxes and InitialMailboxes")
952 }
953 // DefaultMailboxes is deprecated.
954 for _, mb := range c.DefaultMailboxes {
955 checkMailboxNormf(mb, "default mailbox")
956 // We don't create parent mailboxes for default mailboxes.
957 if ParentMailboxName(mb) != "" {
958 addErrorf("default mailbox cannot be a child mailbox")
959 }
960 }
961 checkSpecialUseMailbox := func(nameOpt string) {
962 if nameOpt != "" {
963 checkMailboxNormf(nameOpt, "special-use initial mailbox")
964 if strings.EqualFold(nameOpt, "inbox") {
965 addErrorf("initial mailbox cannot be set to Inbox (Inbox is always created)")
966 }
967 // We don't currently create parent mailboxes for initial mailboxes.
968 if ParentMailboxName(nameOpt) != "" {
969 addErrorf("initial mailboxes cannot be child mailboxes")
970 }
971 }
972 }
973 checkSpecialUseMailbox(c.InitialMailboxes.SpecialUse.Archive)
974 checkSpecialUseMailbox(c.InitialMailboxes.SpecialUse.Draft)
975 checkSpecialUseMailbox(c.InitialMailboxes.SpecialUse.Junk)
976 checkSpecialUseMailbox(c.InitialMailboxes.SpecialUse.Sent)
977 checkSpecialUseMailbox(c.InitialMailboxes.SpecialUse.Trash)
978 for _, name := range c.InitialMailboxes.Regular {
979 checkMailboxNormf(name, "regular initial mailbox")
980 if strings.EqualFold(name, "inbox") {
981 addErrorf("initial regular mailbox cannot be set to Inbox (Inbox is always created)")
982 }
983 if ParentMailboxName(name) != "" {
984 addErrorf("initial mailboxes cannot be child mailboxes")
985 }
986 }
987
988 checkTransportSMTP := func(name string, isTLS bool, t *config.TransportSMTP) {
989 addTransportErrorf := func(format string, args ...any) {
990 addErrorf("transport %s: %s", name, fmt.Sprintf(format, args...))
991 }
992
993 var err error
994 t.DNSHost, err = dns.ParseDomain(t.Host)
995 if err != nil {
996 addTransportErrorf("bad host %s: %v", t.Host, err)
997 }
998
999 if isTLS && t.STARTTLSInsecureSkipVerify {
1000 addTransportErrorf("cannot have STARTTLSInsecureSkipVerify with immediate TLS")
1001 }
1002 if isTLS && t.NoSTARTTLS {
1003 addTransportErrorf("cannot have NoSTARTTLS with immediate TLS")
1004 }
1005
1006 if t.Auth == nil {
1007 return
1008 }
1009 seen := map[string]bool{}
1010 for _, m := range t.Auth.Mechanisms {
1011 if seen[m] {
1012 addTransportErrorf("duplicate authentication mechanism %s", m)
1013 }
1014 seen[m] = true
1015 switch m {
1016 case "SCRAM-SHA-256-PLUS":
1017 case "SCRAM-SHA-256":
1018 case "SCRAM-SHA-1-PLUS":
1019 case "SCRAM-SHA-1":
1020 case "CRAM-MD5":
1021 case "PLAIN":
1022 default:
1023 addTransportErrorf("unknown authentication mechanism %s", m)
1024 }
1025 }
1026
1027 t.Auth.EffectiveMechanisms = t.Auth.Mechanisms
1028 if len(t.Auth.EffectiveMechanisms) == 0 {
1029 t.Auth.EffectiveMechanisms = []string{"SCRAM-SHA-256-PLUS", "SCRAM-SHA-256", "SCRAM-SHA-1-PLUS", "SCRAM-SHA-1", "CRAM-MD5"}
1030 }
1031 }
1032
1033 checkTransportSocks := func(name string, t *config.TransportSocks) {
1034 addTransportErrorf := func(format string, args ...any) {
1035 addErrorf("transport %s: %s", name, fmt.Sprintf(format, args...))
1036 }
1037
1038 _, _, err := net.SplitHostPort(t.Address)
1039 if err != nil {
1040 addTransportErrorf("bad address %s: %v", t.Address, err)
1041 }
1042 for _, ipstr := range t.RemoteIPs {
1043 ip := net.ParseIP(ipstr)
1044 if ip == nil {
1045 addTransportErrorf("bad ip %s", ipstr)
1046 } else {
1047 t.IPs = append(t.IPs, ip)
1048 }
1049 }
1050 t.Hostname, err = dns.ParseDomain(t.RemoteHostname)
1051 if err != nil {
1052 addTransportErrorf("bad hostname %s: %v", t.RemoteHostname, err)
1053 }
1054 }
1055
1056 checkTransportDirect := func(name string, t *config.TransportDirect) {
1057 addTransportErrorf := func(format string, args ...any) {
1058 addErrorf("transport %s: %s", name, fmt.Sprintf(format, args...))
1059 }
1060
1061 if t.DisableIPv4 && t.DisableIPv6 {
1062 addTransportErrorf("both IPv4 and IPv6 are disabled, enable at least one")
1063 }
1064 t.IPFamily = "ip"
1065 if t.DisableIPv4 {
1066 t.IPFamily = "ip6"
1067 }
1068 if t.DisableIPv6 {
1069 t.IPFamily = "ip4"
1070 }
1071 }
1072
1073 checkTransportFail := func(name string, t *config.TransportFail) {
1074 addTransportErrorf := func(format string, args ...any) {
1075 addErrorf("transport %s: %s", name, fmt.Sprintf(format, args...))
1076 }
1077
1078 if t.SMTPCode == 0 {
1079 t.Code = smtp.C554TransactionFailed
1080 } else if t.SMTPCode/100 != 4 && t.SMTPCode/100 != 5 {
1081 addTransportErrorf("smtp code %d must be 4xx or 5xx", t.SMTPCode/100)
1082 } else {
1083 t.Code = t.SMTPCode
1084 }
1085
1086 if len(t.SMTPMessage) > 256 {
1087 addTransportErrorf("message must be <= 256 characters")
1088 }
1089 for _, c := range t.SMTPMessage {
1090 if c < ' ' || c >= 0x7f {
1091 addTransportErrorf("message cannot contain control characters including newlines, and must be ascii-only")
1092 }
1093 }
1094 t.Message = t.SMTPMessage
1095 if t.Message == "" {
1096 t.Message = "transport fail: explicit immediate delivery failure per configuration"
1097 }
1098 }
1099
1100 for name, t := range c.Transports {
1101 addTransportErrorf := func(format string, args ...any) {
1102 addErrorf("transport %s: %s", name, fmt.Sprintf(format, args...))
1103 }
1104
1105 n := 0
1106 if t.Submissions != nil {
1107 n++
1108 checkTransportSMTP(name, true, t.Submissions)
1109 }
1110 if t.Submission != nil {
1111 n++
1112 checkTransportSMTP(name, false, t.Submission)
1113 }
1114 if t.SMTP != nil {
1115 n++
1116 checkTransportSMTP(name, false, t.SMTP)
1117 }
1118 if t.Socks != nil {
1119 n++
1120 checkTransportSocks(name, t.Socks)
1121 }
1122 if t.Direct != nil {
1123 n++
1124 checkTransportDirect(name, t.Direct)
1125 }
1126 if t.Fail != nil {
1127 n++
1128 checkTransportFail(name, t.Fail)
1129 }
1130 if n > 1 {
1131 addTransportErrorf("cannot have multiple methods in a transport")
1132 }
1133 }
1134
1135 // Load CA certificate pool.
1136 if c.TLS.CA != nil {
1137 if c.TLS.CA.AdditionalToSystem {
1138 var err error
1139 c.TLS.CertPool, err = x509.SystemCertPool()
1140 if err != nil {
1141 addErrorf("fetching system CA cert pool: %v", err)
1142 }
1143 } else {
1144 c.TLS.CertPool = x509.NewCertPool()
1145 }
1146 for _, certfile := range c.TLS.CA.CertFiles {
1147 p := configDirPath(configFile, certfile)
1148 pemBuf, err := os.ReadFile(p)
1149 if err != nil {
1150 addErrorf("reading TLS CA cert file: %v", err)
1151 continue
1152 } else if !c.TLS.CertPool.AppendCertsFromPEM(pemBuf) {
1153 // todo: can we check more fully if we're getting some useful data back?
1154 addErrorf("no CA certs added from %q", p)
1155 }
1156 }
1157 }
1158 return
1159}
1160
1161// PrepareDynamicConfig parses the dynamic config file given a static file.
1162func ParseDynamicConfig(ctx context.Context, log mlog.Log, dynamicPath string, static config.Static) (c config.Dynamic, mtime time.Time, accDests map[string]AccountDestination, aliases map[string]config.Alias, errs []error) {
1163 addErrorf := func(format string, args ...any) {
1164 errs = append(errs, fmt.Errorf(format, args...))
1165 }
1166
1167 f, err := os.Open(dynamicPath)
1168 if err != nil {
1169 addErrorf("parsing domains config: %v", err)
1170 return
1171 }
1172 defer f.Close()
1173 fi, err := f.Stat()
1174 if err != nil {
1175 addErrorf("stat domains config: %v", err)
1176 }
1177 if err := sconf.Parse(f, &c); err != nil {
1178 addErrorf("parsing dynamic config file: %v", err)
1179 return
1180 }
1181
1182 accDests, aliases, errs = prepareDynamicConfig(ctx, log, dynamicPath, static, &c)
1183 return c, fi.ModTime(), accDests, aliases, errs
1184}
1185
1186func prepareDynamicConfig(ctx context.Context, log mlog.Log, dynamicPath string, static config.Static, c *config.Dynamic) (accDests map[string]AccountDestination, aliases map[string]config.Alias, errs []error) {
1187 addErrorf := func(format string, args ...any) {
1188 errs = append(errs, fmt.Errorf(format, args...))
1189 }
1190
1191 // Check that mailbox is in unicode NFC normalized form.
1192 checkMailboxNormf := func(mailbox string, what string, errorf func(format string, args ...any)) {
1193 s := norm.NFC.String(mailbox)
1194 if mailbox != s {
1195 errorf("%s: mailbox %q is not in NFC normalized form, should be %q", what, mailbox, s)
1196 }
1197 }
1198
1199 // Validate postmaster account exists.
1200 if _, ok := c.Accounts[static.Postmaster.Account]; !ok {
1201 addErrorf("postmaster account %q does not exist", static.Postmaster.Account)
1202 }
1203 checkMailboxNormf(static.Postmaster.Mailbox, "postmaster mailbox", addErrorf)
1204
1205 accDests = map[string]AccountDestination{}
1206 aliases = map[string]config.Alias{}
1207
1208 // Validate host TLSRPT account/address.
1209 if static.HostTLSRPT.Account != "" {
1210 if _, ok := c.Accounts[static.HostTLSRPT.Account]; !ok {
1211 addErrorf("host tlsrpt account %q does not exist", static.HostTLSRPT.Account)
1212 }
1213 checkMailboxNormf(static.HostTLSRPT.Mailbox, "host tlsrpt mailbox", addErrorf)
1214
1215 // Localpart has been parsed already.
1216
1217 addrFull := smtp.NewAddress(static.HostTLSRPT.ParsedLocalpart, static.HostnameDomain).String()
1218 dest := config.Destination{
1219 Mailbox: static.HostTLSRPT.Mailbox,
1220 HostTLSReports: true,
1221 }
1222 accDests[addrFull] = AccountDestination{false, static.HostTLSRPT.ParsedLocalpart, static.HostTLSRPT.Account, dest}
1223 }
1224
1225 var haveSTSListener, haveWebserverListener bool
1226 for _, l := range static.Listeners {
1227 if l.MTASTSHTTPS.Enabled {
1228 haveSTSListener = true
1229 }
1230 if l.WebserverHTTP.Enabled || l.WebserverHTTPS.Enabled {
1231 haveWebserverListener = true
1232 }
1233 }
1234
1235 checkRoutes := func(descr string, routes []config.Route) {
1236 parseRouteDomains := func(l []string) []string {
1237 var r []string
1238 for _, e := range l {
1239 if e == "." {
1240 r = append(r, e)
1241 continue
1242 }
1243 prefix := ""
1244 if strings.HasPrefix(e, ".") {
1245 prefix = "."
1246 e = e[1:]
1247 }
1248 d, err := dns.ParseDomain(e)
1249 if err != nil {
1250 addErrorf("%s: invalid domain %s: %v", descr, e, err)
1251 }
1252 r = append(r, prefix+d.ASCII)
1253 }
1254 return r
1255 }
1256
1257 for i := range routes {
1258 routes[i].FromDomainASCII = parseRouteDomains(routes[i].FromDomain)
1259 routes[i].ToDomainASCII = parseRouteDomains(routes[i].ToDomain)
1260 var ok bool
1261 routes[i].ResolvedTransport, ok = static.Transports[routes[i].Transport]
1262 if !ok {
1263 addErrorf("%s: route references undefined transport %s", descr, routes[i].Transport)
1264 }
1265 }
1266 }
1267
1268 checkRoutes("global routes", c.Routes)
1269
1270 // Validate domains.
1271 c.ClientSettingDomains = map[dns.Domain]struct{}{}
1272 for d, domain := range c.Domains {
1273 addDomainErrorf := func(format string, args ...any) {
1274 addErrorf("domain %v: %s", d, fmt.Sprintf(format, args...))
1275 }
1276
1277 dnsdomain, err := dns.ParseDomain(d)
1278 if err != nil {
1279 addDomainErrorf("parsing domain: %s", err)
1280 } else if dnsdomain.Name() != d {
1281 addDomainErrorf("must be specified in unicode form, %s", dnsdomain.Name())
1282 }
1283
1284 domain.Domain = dnsdomain
1285
1286 if domain.ClientSettingsDomain != "" {
1287 csd, err := dns.ParseDomain(domain.ClientSettingsDomain)
1288 if err != nil {
1289 addDomainErrorf("bad client settings domain %q: %s", domain.ClientSettingsDomain, err)
1290 }
1291 domain.ClientSettingsDNSDomain = csd
1292 c.ClientSettingDomains[csd] = struct{}{}
1293 }
1294
1295 if domain.LocalpartCatchallSeparator != "" && len(domain.LocalpartCatchallSeparators) != 0 {
1296 addDomainErrorf("cannot have both LocalpartCatchallSeparator and LocalpartCatchallSeparators")
1297 }
1298 domain.LocalpartCatchallSeparatorsEffective = domain.LocalpartCatchallSeparators
1299 if domain.LocalpartCatchallSeparator != "" {
1300 domain.LocalpartCatchallSeparatorsEffective = append(domain.LocalpartCatchallSeparatorsEffective, domain.LocalpartCatchallSeparator)
1301 }
1302 sepSeen := map[string]bool{}
1303 for _, sep := range domain.LocalpartCatchallSeparatorsEffective {
1304 if sepSeen[sep] {
1305 addDomainErrorf("duplicate localpart catchall separator %q", sep)
1306 }
1307 sepSeen[sep] = true
1308 }
1309
1310 for _, sign := range domain.DKIM.Sign {
1311 if _, ok := domain.DKIM.Selectors[sign]; !ok {
1312 addDomainErrorf("unknown selector %s for signing", sign)
1313 }
1314 }
1315 for name, sel := range domain.DKIM.Selectors {
1316 addSelectorErrorf := func(format string, args ...any) {
1317 addDomainErrorf("selector %s: %s", name, fmt.Sprintf(format, args...))
1318 }
1319
1320 seld, err := dns.ParseDomain(name)
1321 if err != nil {
1322 addSelectorErrorf("parsing selector: %s", err)
1323 } else if seld.Name() != name {
1324 addSelectorErrorf("must be specified in unicode form, %q", seld.Name())
1325 }
1326 sel.Domain = seld
1327
1328 if sel.Expiration != "" {
1329 exp, err := time.ParseDuration(sel.Expiration)
1330 if err != nil {
1331 addSelectorErrorf("invalid expiration %q: %v", sel.Expiration, err)
1332 } else {
1333 sel.ExpirationSeconds = int(exp / time.Second)
1334 }
1335 }
1336
1337 sel.HashEffective = sel.Hash
1338 switch sel.HashEffective {
1339 case "":
1340 sel.HashEffective = "sha256"
1341 case "sha1":
1342 log.Error("using sha1 with DKIM is deprecated as not secure enough, switch to sha256")
1343 case "sha256":
1344 default:
1345 addSelectorErrorf("unsupported hash %q", sel.HashEffective)
1346 }
1347
1348 pemBuf, err := os.ReadFile(configDirPath(dynamicPath, sel.PrivateKeyFile))
1349 if err != nil {
1350 addSelectorErrorf("reading private key: %s", err)
1351 continue
1352 }
1353 p, _ := pem.Decode(pemBuf)
1354 if p == nil {
1355 addSelectorErrorf("private key has no PEM block")
1356 continue
1357 }
1358 key, err := x509.ParsePKCS8PrivateKey(p.Bytes)
1359 if err != nil {
1360 addSelectorErrorf("parsing private key: %s", err)
1361 continue
1362 }
1363 switch k := key.(type) {
1364 case *rsa.PrivateKey:
1365 if k.N.BitLen() < 1024 {
1366 // ../rfc/6376:757
1367 // Let's help user do the right thing.
1368 addSelectorErrorf("rsa keys should be >= 1024 bits, is %d bits", k.N.BitLen())
1369 }
1370 sel.Key = k
1371 sel.Algorithm = fmt.Sprintf("rsa-%d", k.N.BitLen())
1372 case ed25519.PrivateKey:
1373 if sel.HashEffective != "sha256" {
1374 addSelectorErrorf("hash algorithm %q is not supported with ed25519, only sha256 is", sel.HashEffective)
1375 }
1376 sel.Key = k
1377 sel.Algorithm = "ed25519"
1378 default:
1379 addSelectorErrorf("private key type %T not yet supported", key)
1380 }
1381
1382 if len(sel.Headers) == 0 {
1383 // ../rfc/6376:2139
1384 // ../rfc/6376:2203
1385 // ../rfc/6376:2212
1386 // By default we seal signed headers, and we sign user-visible headers to
1387 // prevent/limit reuse of previously signed messages: All addressing fields, date
1388 // and subject, message-referencing fields, parsing instructions (content-type).
1389 sel.HeadersEffective = strings.Split("From,To,Cc,Bcc,Reply-To,References,In-Reply-To,Subject,Date,Message-Id,Content-Type", ",")
1390 } else {
1391 var from bool
1392 for _, h := range sel.Headers {
1393 from = from || strings.EqualFold(h, "From")
1394 // ../rfc/6376:2269
1395 if strings.EqualFold(h, "DKIM-Signature") || strings.EqualFold(h, "Received") || strings.EqualFold(h, "Return-Path") {
1396 log.Error("DKIM-signing header %q is recommended against as it may be modified in transit")
1397 }
1398 }
1399 if !from {
1400 addSelectorErrorf("From-field must always be DKIM-signed")
1401 }
1402 sel.HeadersEffective = sel.Headers
1403 }
1404
1405 domain.DKIM.Selectors[name] = sel
1406 }
1407
1408 if domain.MTASTS != nil {
1409 if !haveSTSListener {
1410 addDomainErrorf("MTA-STS enabled, but there is no listener for MTASTS")
1411 }
1412 sts := domain.MTASTS
1413 if sts.PolicyID == "" {
1414 addDomainErrorf("invalid empty MTA-STS PolicyID")
1415 }
1416 switch sts.Mode {
1417 case mtasts.ModeNone, mtasts.ModeTesting, mtasts.ModeEnforce:
1418 default:
1419 addDomainErrorf("invalid mtasts mode %q", sts.Mode)
1420 }
1421 }
1422
1423 checkRoutes("routes for domain", domain.Routes)
1424
1425 c.Domains[d] = domain
1426 }
1427
1428 // To determine ReportsOnly.
1429 domainHasAddress := map[string]bool{}
1430
1431 // Validate email addresses.
1432 for accName, acc := range c.Accounts {
1433 addAccountErrorf := func(format string, args ...any) {
1434 addErrorf("account %q: %s", accName, fmt.Sprintf(format, args...))
1435 }
1436
1437 var err error
1438 acc.DNSDomain, err = dns.ParseDomain(acc.Domain)
1439 if err != nil {
1440 addAccountErrorf("parsing domain %s: %s", acc.Domain, err)
1441 }
1442
1443 if strings.EqualFold(acc.RejectsMailbox, "Inbox") {
1444 addAccountErrorf("cannot set RejectsMailbox to inbox, messages will be removed automatically from the rejects mailbox")
1445 }
1446 if acc.Introbox != "" {
1447 mailbox, _, err := config.CheckMailboxName(acc.Introbox, false)
1448 if err != nil {
1449 addAccountErrorf("invalid Introbox mailbox: %v", err)
1450 } else {
1451 acc.Introbox = mailbox
1452 }
1453 if acc.Introbox == acc.RejectsMailbox {
1454 addAccountErrorf("cannot set Introbox and RejectsMailbox to the same mailbox")
1455 }
1456 }
1457 checkMailboxNormf(acc.RejectsMailbox, "rejects mailbox", addErrorf)
1458
1459 if len(acc.LoginDisabled) > 256 {
1460 addAccountErrorf("message for disabled login must be <256 characters")
1461 }
1462 for _, c := range acc.LoginDisabled {
1463 // For IMAP and SMTP. IMAP only allows UTF8 after "ENABLE IMAPrev2".
1464 if c < ' ' || c >= 0x7f {
1465 addAccountErrorf("message cannot contain control characters including newlines, and must be ascii-only")
1466 }
1467 }
1468
1469 if acc.AutomaticJunkFlags.JunkMailboxRegexp != "" {
1470 r, err := regexp.Compile(acc.AutomaticJunkFlags.JunkMailboxRegexp)
1471 if err != nil {
1472 addAccountErrorf("invalid JunkMailboxRegexp regular expression: %v", err)
1473 }
1474 acc.JunkMailbox = r
1475 }
1476 if acc.AutomaticJunkFlags.NeutralMailboxRegexp != "" {
1477 r, err := regexp.Compile(acc.AutomaticJunkFlags.NeutralMailboxRegexp)
1478 if err != nil {
1479 addAccountErrorf("invalid NeutralMailboxRegexp regular expression: %v", err)
1480 }
1481 acc.NeutralMailbox = r
1482 }
1483 if acc.AutomaticJunkFlags.NotJunkMailboxRegexp != "" {
1484 r, err := regexp.Compile(acc.AutomaticJunkFlags.NotJunkMailboxRegexp)
1485 if err != nil {
1486 addAccountErrorf("invalid NotJunkMailboxRegexp regular expression: %v", err)
1487 }
1488 acc.NotJunkMailbox = r
1489 }
1490
1491 if acc.JunkFilter != nil {
1492 params := acc.JunkFilter.Params
1493 if params.MaxPower < 0 || params.MaxPower > 0.5 {
1494 addAccountErrorf("junk filter MaxPower must be >= 0 and < 0.5")
1495 }
1496 if params.TopWords < 0 {
1497 addAccountErrorf("junk filter TopWords must be >= 0")
1498 }
1499 if params.IgnoreWords < 0 || params.IgnoreWords > 0.5 {
1500 addAccountErrorf("junk filter IgnoreWords must be >= 0 and < 0.5")
1501 }
1502 if params.RareWords < 0 {
1503 addAccountErrorf("junk filter RareWords must be >= 0")
1504 }
1505 }
1506
1507 acc.ParsedFromIDLoginAddresses = make([]smtp.Address, len(acc.FromIDLoginAddresses))
1508 for i, s := range acc.FromIDLoginAddresses {
1509 a, err := smtp.ParseAddress(s)
1510 if err != nil {
1511 addAccountErrorf("invalid fromid login address %q: %v", s, err)
1512 }
1513 // We check later on if address belongs to account.
1514 dom, ok := c.Domains[a.Domain.Name()]
1515 if !ok {
1516 addAccountErrorf("unknown domain in fromid login address %q", s)
1517 } else if len(dom.LocalpartCatchallSeparatorsEffective) == 0 {
1518 addAccountErrorf("localpart catchall separator not configured for domain for fromid login address %q", s)
1519 }
1520 acc.ParsedFromIDLoginAddresses[i] = a
1521 }
1522
1523 // Clear any previously derived state.
1524 acc.Aliases = nil
1525
1526 c.Accounts[accName] = acc
1527
1528 if acc.OutgoingWebhook != nil {
1529 u, err := url.Parse(acc.OutgoingWebhook.URL)
1530 if err == nil && (u.Scheme != "http" && u.Scheme != "https") {
1531 err = errors.New("scheme must be http or https")
1532 }
1533 if err != nil {
1534 addAccountErrorf("parsing outgoing hook url %q: %v", acc.OutgoingWebhook.URL, err)
1535 }
1536
1537 // note: outgoing hook events are in ../queue/hooks.go, ../mox-/config.go, ../queue.go and ../webapi/gendoc.sh. keep in sync.
1538 outgoingHookEvents := []string{"delivered", "suppressed", "delayed", "failed", "relayed", "expanded", "canceled", "unrecognized"}
1539 for _, e := range acc.OutgoingWebhook.Events {
1540 if !slices.Contains(outgoingHookEvents, e) {
1541 addAccountErrorf("unknown outgoing hook event %q", e)
1542 }
1543 }
1544 }
1545 if acc.IncomingWebhook != nil {
1546 u, err := url.Parse(acc.IncomingWebhook.URL)
1547 if err == nil && (u.Scheme != "http" && u.Scheme != "https") {
1548 err = errors.New("scheme must be http or https")
1549 }
1550 if err != nil {
1551 addAccountErrorf("parsing incoming hook url %q: %v", acc.IncomingWebhook.URL, err)
1552 }
1553 }
1554
1555 // todo deprecated: only localpart as keys for Destinations, we are replacing them with full addresses. if domains.conf is written, we won't have to do this again.
1556 replaceLocalparts := map[string]string{}
1557
1558 for addrName, dest := range acc.Destinations {
1559 addDestErrorf := func(format string, args ...any) {
1560 addAccountErrorf("destination %q: %s", addrName, fmt.Sprintf(format, args...))
1561 }
1562
1563 checkMailboxNormf(dest.Mailbox, "destination mailbox", addDestErrorf)
1564
1565 if dest.SMTPError != "" {
1566 if len(dest.SMTPError) > 256 {
1567 addDestErrorf("smtp error must be smaller than 256 bytes")
1568 }
1569 for _, c := range dest.SMTPError {
1570 if c < ' ' || c >= 0x7f {
1571 addDestErrorf("smtp error cannot contain contain control characters (including newlines) or non-ascii")
1572 break
1573 }
1574 }
1575
1576 if dest.Mailbox != "" {
1577 addDestErrorf("cannot have both SMTPError and Mailbox")
1578 }
1579 if len(dest.Rulesets) != 0 {
1580 addDestErrorf("cannot have both SMTPError and Rulesets")
1581 }
1582
1583 t := strings.SplitN(dest.SMTPError, " ", 2)
1584 switch t[0] {
1585 default:
1586 addDestErrorf("smtp error must be 421 or 550 (with optional message), not %q", dest.SMTPError)
1587
1588 case "421":
1589 dest.SMTPErrorCode = smtp.C451LocalErr
1590 dest.SMTPErrorSecode = smtp.SeSys3Other0
1591 dest.SMTPErrorMsg = "error processing"
1592 case "550":
1593 dest.SMTPErrorCode = smtp.C550MailboxUnavail
1594 dest.SMTPErrorSecode = smtp.SeAddr1UnknownDestMailbox1
1595 dest.SMTPErrorMsg = "no such user(s)"
1596 }
1597 if len(t) > 1 {
1598 dest.SMTPErrorMsg = strings.TrimSpace(t[1])
1599 }
1600 acc.Destinations[addrName] = dest
1601 }
1602
1603 if dest.MessageAuthRequiredSMTPError != "" {
1604 if len(dest.MessageAuthRequiredSMTPError) > 256 {
1605 addDestErrorf("message authentication required smtp error must be smaller than 256 bytes")
1606 }
1607 for _, c := range dest.MessageAuthRequiredSMTPError {
1608 if c < ' ' || c >= 0x7f {
1609 addDestErrorf("message authentication required smtp error cannot contain contain control characters (including newlines) or non-ascii")
1610 break
1611 }
1612 }
1613 }
1614
1615 for i, rs := range dest.Rulesets {
1616 addRulesetErrorf := func(format string, args ...any) {
1617 addDestErrorf("ruleset %d: %s", i+1, fmt.Sprintf(format, args...))
1618 }
1619
1620 checkMailboxNormf(rs.Mailbox, "ruleset mailbox", addRulesetErrorf)
1621
1622 n := 0
1623
1624 if rs.SMTPMailFromRegexp != "" {
1625 n++
1626 r, err := regexp.Compile(rs.SMTPMailFromRegexp)
1627 if err != nil {
1628 addRulesetErrorf("invalid SMTPMailFrom regular expression: %v", err)
1629 }
1630 c.Accounts[accName].Destinations[addrName].Rulesets[i].SMTPMailFromRegexpCompiled = r
1631 }
1632 if rs.MsgFromRegexp != "" {
1633 n++
1634 r, err := regexp.Compile(rs.MsgFromRegexp)
1635 if err != nil {
1636 addRulesetErrorf("invalid MsgFrom regular expression: %v", err)
1637 }
1638 c.Accounts[accName].Destinations[addrName].Rulesets[i].MsgFromRegexpCompiled = r
1639 }
1640 if rs.VerifiedDomain != "" {
1641 n++
1642 d, err := dns.ParseDomain(rs.VerifiedDomain)
1643 if err != nil {
1644 addRulesetErrorf("invalid VerifiedDomain: %v", err)
1645 }
1646 c.Accounts[accName].Destinations[addrName].Rulesets[i].VerifiedDNSDomain = d
1647 }
1648
1649 var hdr [][2]*regexp.Regexp
1650 for k, v := range rs.HeadersRegexp {
1651 n++
1652 if strings.ToLower(k) != k {
1653 addRulesetErrorf("header field %q must only have lower case characters", k)
1654 }
1655 if strings.ToLower(v) != v {
1656 addRulesetErrorf("header value %q must only have lower case characters", v)
1657 }
1658 rk, err := regexp.Compile(k)
1659 if err != nil {
1660 addRulesetErrorf("invalid rule header regexp %q: %v", k, err)
1661 }
1662 rv, err := regexp.Compile(v)
1663 if err != nil {
1664 addRulesetErrorf("invalid rule header regexp %q: %v", v, err)
1665 }
1666 hdr = append(hdr, [...]*regexp.Regexp{rk, rv})
1667 }
1668 c.Accounts[accName].Destinations[addrName].Rulesets[i].HeadersRegexpCompiled = hdr
1669
1670 if n == 0 {
1671 addRulesetErrorf("ruleset must have at least one rule")
1672 }
1673
1674 if rs.IsForward && rs.ListAllowDomain != "" {
1675 addRulesetErrorf("ruleset cannot have both IsForward and ListAllowDomain")
1676 }
1677 if rs.IsForward {
1678 if rs.SMTPMailFromRegexp == "" || rs.VerifiedDomain == "" {
1679 addRulesetErrorf("ruleset with IsForward must have both SMTPMailFromRegexp and VerifiedDomain too")
1680 }
1681 }
1682 if rs.ListAllowDomain != "" {
1683 d, err := dns.ParseDomain(rs.ListAllowDomain)
1684 if err != nil {
1685 addRulesetErrorf("invalid ListAllowDomain %q: %v", rs.ListAllowDomain, err)
1686 }
1687 c.Accounts[accName].Destinations[addrName].Rulesets[i].ListAllowDNSDomain = d
1688 }
1689
1690 checkMailboxNormf(rs.AcceptRejectsToMailbox, "rejects mailbox", addRulesetErrorf)
1691 if strings.EqualFold(rs.AcceptRejectsToMailbox, "inbox") {
1692 addRulesetErrorf("AcceptRejectsToMailbox cannot be set to Inbox")
1693 }
1694 }
1695
1696 // Catchall destination for domain.
1697 if strings.HasPrefix(addrName, "@") {
1698 d, err := dns.ParseDomain(addrName[1:])
1699 if err != nil {
1700 addDestErrorf("parsing domain %q", addrName[1:])
1701 continue
1702 } else if _, ok := c.Domains[d.Name()]; !ok {
1703 addDestErrorf("unknown domain for address")
1704 continue
1705 }
1706 domainHasAddress[d.Name()] = true
1707 addrFull := "@" + d.Name()
1708 if _, ok := accDests[addrFull]; ok {
1709 addDestErrorf("duplicate canonicalized catchall destination address %s", addrFull)
1710 }
1711 accDests[addrFull] = AccountDestination{true, "", accName, dest}
1712 continue
1713 }
1714
1715 // todo deprecated: remove support for parsing destination as just a localpart instead full address.
1716 var address smtp.Address
1717 if localpart, err := smtp.ParseLocalpart(addrName); err != nil && errors.Is(err, smtp.ErrBadLocalpart) {
1718 address, err = smtp.ParseAddress(addrName)
1719 if err != nil {
1720 addDestErrorf("invalid email address")
1721 continue
1722 } else if _, ok := c.Domains[address.Domain.Name()]; !ok {
1723 addDestErrorf("unknown domain for address")
1724 continue
1725 }
1726 } else {
1727 if err != nil {
1728 addDestErrorf("invalid localpart %q", addrName)
1729 continue
1730 }
1731 address = smtp.NewAddress(localpart, acc.DNSDomain)
1732 if _, ok := c.Domains[acc.DNSDomain.Name()]; !ok {
1733 addDestErrorf("unknown domain %s", acc.DNSDomain.Name())
1734 continue
1735 }
1736 replaceLocalparts[addrName] = address.Pack(true)
1737 }
1738
1739 origLP := address.Localpart
1740 dc := c.Domains[address.Domain.Name()]
1741 domainHasAddress[address.Domain.Name()] = true
1742 lp := CanonicalLocalpart(address.Localpart, dc)
1743 var hasSep bool
1744 for _, sep := range dc.LocalpartCatchallSeparatorsEffective {
1745 if strings.Contains(string(address.Localpart), sep) {
1746 hasSep = true
1747 addDestErrorf("localpart of address %s includes domain catchall separator %s", address, sep)
1748 }
1749 }
1750 if !hasSep {
1751 address.Localpart = lp
1752 }
1753 addrFull := address.Pack(true)
1754 if _, ok := accDests[addrFull]; ok {
1755 addDestErrorf("duplicate canonicalized destination address %s", addrFull)
1756 }
1757 accDests[addrFull] = AccountDestination{false, origLP, accName, dest}
1758 }
1759
1760 for lp, addr := range replaceLocalparts {
1761 dest, ok := acc.Destinations[lp]
1762 if !ok {
1763 addAccountErrorf("could not find localpart %q to replace with address in destinations", lp)
1764 } else {
1765 log.Warn(`deprecation warning: support for account destination addresses specified as just localpart ("username") instead of full email address will be removed in the future; update domains.conf, for each Account, for each Destination, ensure each key is an email address by appending "@" and the default domain for the account`,
1766 slog.Any("localpart", lp),
1767 slog.Any("address", addr),
1768 slog.String("account", accName))
1769 acc.Destinations[addr] = dest
1770 delete(acc.Destinations, lp)
1771 }
1772 }
1773
1774 // Now that all addresses are parsed, check if all fromid login addresses match
1775 // configured addresses.
1776 for i, a := range acc.ParsedFromIDLoginAddresses {
1777 // For domain catchall.
1778 if _, ok := accDests["@"+a.Domain.Name()]; ok {
1779 continue
1780 }
1781 dc := c.Domains[a.Domain.Name()]
1782 a.Localpart = CanonicalLocalpart(a.Localpart, dc)
1783 if _, ok := accDests[a.Pack(true)]; !ok {
1784 addAccountErrorf("fromid login address %q does not match its destination addresses", acc.FromIDLoginAddresses[i])
1785 }
1786 }
1787
1788 checkRoutes("routes for account", acc.Routes)
1789 }
1790
1791 // Set DMARC destinations.
1792 for d, domain := range c.Domains {
1793 addDomainErrorf := func(format string, args ...any) {
1794 addErrorf("domain %s: %s", d, fmt.Sprintf(format, args...))
1795 }
1796
1797 dmarc := domain.DMARC
1798 if dmarc == nil {
1799 continue
1800 }
1801 if _, ok := c.Accounts[dmarc.Account]; !ok {
1802 addDomainErrorf("DMARC account %q does not exist", dmarc.Account)
1803 }
1804
1805 // Note: For backwards compabilitiy, DMARC reporting localparts can contain catchall separators.
1806 lp, err := smtp.ParseLocalpart(dmarc.Localpart)
1807 if err != nil {
1808 addDomainErrorf("invalid DMARC localpart %q: %s", dmarc.Localpart, err)
1809 }
1810 if lp.IsInternational() {
1811 // ../rfc/8616:234
1812 addDomainErrorf("DMARC localpart %q is an internationalized address, only conventional ascii-only address possible for interopability", lp)
1813 }
1814 addrdom := domain.Domain
1815 if dmarc.Domain != "" {
1816 addrdom, err = dns.ParseDomain(dmarc.Domain)
1817 if err != nil {
1818 addDomainErrorf("DMARC domain %q: %s", dmarc.Domain, err)
1819 } else if adomain, ok := c.Domains[addrdom.Name()]; !ok {
1820 addDomainErrorf("unknown domain %q for DMARC address", addrdom)
1821 } else if !adomain.LocalpartCaseSensitive {
1822 lp = smtp.Localpart(strings.ToLower(string(lp)))
1823 }
1824 } else if !domain.LocalpartCaseSensitive {
1825 lp = smtp.Localpart(strings.ToLower(string(lp)))
1826 }
1827 if addrdom == domain.Domain {
1828 domainHasAddress[addrdom.Name()] = true
1829 }
1830
1831 domain.DMARC.ParsedLocalpart = lp
1832 domain.DMARC.DNSDomain = addrdom
1833 c.Domains[d] = domain
1834 addrFull := smtp.NewAddress(lp, addrdom).String()
1835 dest := config.Destination{
1836 Mailbox: dmarc.Mailbox,
1837 DMARCReports: true,
1838 }
1839 checkMailboxNormf(dmarc.Mailbox, "DMARC mailbox for account", addDomainErrorf)
1840 accDests[addrFull] = AccountDestination{false, lp, dmarc.Account, dest}
1841 }
1842
1843 // Set TLSRPT destinations.
1844 for d, domain := range c.Domains {
1845 addDomainErrorf := func(format string, args ...any) {
1846 addErrorf("domain %s: %s", d, fmt.Sprintf(format, args...))
1847 }
1848
1849 tlsrpt := domain.TLSRPT
1850 if tlsrpt == nil {
1851 continue
1852 }
1853 if _, ok := c.Accounts[tlsrpt.Account]; !ok {
1854 addDomainErrorf("TLSRPT account %q does not exist", tlsrpt.Account)
1855 }
1856
1857 // Note: For backwards compabilitiy, TLS reporting localparts can contain catchall separators.
1858 lp, err := smtp.ParseLocalpart(tlsrpt.Localpart)
1859 if err != nil {
1860 addDomainErrorf("invalid TLSRPT localpart %q: %s", tlsrpt.Localpart, err)
1861 }
1862 if lp.IsInternational() {
1863 // Does not appear documented in ../rfc/8460, but similar to DMARC it makes sense
1864 // to keep this ascii-only addresses.
1865 addDomainErrorf("TLSRPT localpart %q is an internationalized address, only conventional ascii-only address allowed for interopability", lp)
1866 }
1867 addrdom := domain.Domain
1868 if tlsrpt.Domain != "" {
1869 addrdom, err = dns.ParseDomain(tlsrpt.Domain)
1870 if err != nil {
1871 addDomainErrorf("TLSRPT domain %q: %s", tlsrpt.Domain, err)
1872 } else if adomain, ok := c.Domains[addrdom.Name()]; !ok {
1873 addDomainErrorf("unknown domain %q for TLSRPT address", tlsrpt.Domain)
1874 } else if !adomain.LocalpartCaseSensitive {
1875 lp = smtp.Localpart(strings.ToLower(string(lp)))
1876 }
1877 } else if !domain.LocalpartCaseSensitive {
1878 lp = smtp.Localpart(strings.ToLower(string(lp)))
1879 }
1880 if addrdom == domain.Domain {
1881 domainHasAddress[addrdom.Name()] = true
1882 }
1883
1884 domain.TLSRPT.ParsedLocalpart = lp
1885 domain.TLSRPT.DNSDomain = addrdom
1886 c.Domains[d] = domain
1887 addrFull := smtp.NewAddress(lp, addrdom).String()
1888 dest := config.Destination{
1889 Mailbox: tlsrpt.Mailbox,
1890 DomainTLSReports: true,
1891 }
1892 checkMailboxNormf(tlsrpt.Mailbox, "TLSRPT mailbox", addDomainErrorf)
1893 accDests[addrFull] = AccountDestination{false, lp, tlsrpt.Account, dest}
1894 }
1895
1896 // Set ReportsOnly for domains, based on whether we have seen addresses (possibly
1897 // from DMARC or TLS reporting).
1898 for d, domain := range c.Domains {
1899 domain.ReportsOnly = !domainHasAddress[domain.Domain.Name()]
1900 c.Domains[d] = domain
1901 }
1902
1903 // Aliases, per domain. Also add references to accounts.
1904 for d, domain := range c.Domains {
1905 for lpstr, a := range domain.Aliases {
1906 addAliasErrorf := func(format string, args ...any) {
1907 addErrorf("domain %s: alias %s: %s", d, lpstr, fmt.Sprintf(format, args...))
1908 }
1909
1910 var err error
1911 a.LocalpartStr = lpstr
1912 var clp smtp.Localpart
1913 lp, err := smtp.ParseLocalpart(lpstr)
1914 if err != nil {
1915 addAliasErrorf("parsing alias: %v", err)
1916 continue
1917 } else {
1918 var hasSep bool
1919 for _, sep := range domain.LocalpartCatchallSeparatorsEffective {
1920 if strings.Contains(string(lp), sep) {
1921 addAliasErrorf("alias contains localpart catchall separator")
1922 hasSep = true
1923 }
1924 }
1925 if hasSep {
1926 continue
1927 }
1928 clp = CanonicalLocalpart(lp, domain)
1929 }
1930
1931 addr := smtp.NewAddress(clp, domain.Domain).Pack(true)
1932 if _, ok := aliases[addr]; ok {
1933 addAliasErrorf("duplicate alias address %q", addr)
1934 continue
1935 }
1936 if _, ok := accDests[addr]; ok {
1937 addAliasErrorf("alias %q already present as regular address", addr)
1938 continue
1939 }
1940 if len(a.Addresses) == 0 {
1941 // Not currently possible, Addresses isn't optional.
1942 addAliasErrorf("alias %q needs at least one destination address", addr)
1943 continue
1944 }
1945 a.ParsedAddresses = make([]config.AliasAddress, 0, len(a.Addresses))
1946 seen := map[string]bool{}
1947 for _, destAddr := range a.Addresses {
1948 da, err := smtp.ParseAddress(destAddr)
1949 if err != nil {
1950 addAliasErrorf("parsing destination address %q: %v", destAddr, err)
1951 continue
1952 }
1953 dastr := da.Pack(true)
1954 accDest, ok := accDests[dastr]
1955 if !ok {
1956 addAliasErrorf("references non-existent address %q", destAddr)
1957 continue
1958 }
1959 if seen[dastr] {
1960 addAliasErrorf("duplicate address %q", destAddr)
1961 continue
1962 }
1963 seen[dastr] = true
1964 aa := config.AliasAddress{Address: da, AccountName: accDest.Account, Destination: accDest.Destination}
1965 a.ParsedAddresses = append(a.ParsedAddresses, aa)
1966 }
1967 a.Domain = domain.Domain
1968 c.Domains[d].Aliases[lpstr] = a
1969 aliases[addr] = a
1970
1971 for _, aa := range a.ParsedAddresses {
1972 acc := c.Accounts[aa.AccountName]
1973 var addrs []string
1974 if a.ListMembers {
1975 addrs = make([]string, len(a.ParsedAddresses))
1976 for i := range a.ParsedAddresses {
1977 addrs[i] = a.ParsedAddresses[i].Address.Pack(true)
1978 }
1979 }
1980 // Keep the non-sensitive fields.
1981 accAlias := config.Alias{
1982 PostPublic: a.PostPublic,
1983 ListMembers: a.ListMembers,
1984 AllowMsgFrom: a.AllowMsgFrom,
1985 LocalpartStr: a.LocalpartStr,
1986 Domain: a.Domain,
1987 }
1988 acc.Aliases = append(acc.Aliases, config.AddressAlias{SubscriptionAddress: aa.Address.Pack(true), Alias: accAlias, MemberAddresses: addrs})
1989 c.Accounts[aa.AccountName] = acc
1990 }
1991 }
1992 }
1993
1994 // Check webserver configs.
1995 if (len(c.WebDomainRedirects) > 0 || len(c.WebHandlers) > 0) && !haveWebserverListener {
1996 addErrorf("WebDomainRedirects or WebHandlers configured but no listener with WebserverHTTP or WebserverHTTPS enabled")
1997 }
1998
1999 c.WebDNSDomainRedirects = map[dns.Domain]dns.Domain{}
2000 for from, to := range c.WebDomainRedirects {
2001 addRedirectErrorf := func(format string, args ...any) {
2002 addErrorf("web redirect %s to %s: %s", from, to, fmt.Sprintf(format, args...))
2003 }
2004
2005 fromdom, err := dns.ParseDomain(from)
2006 if err != nil {
2007 addRedirectErrorf("parsing domain for redirect %s: %v", from, err)
2008 }
2009 todom, err := dns.ParseDomain(to)
2010 if err != nil {
2011 addRedirectErrorf("parsing domain for redirect %s: %v", to, err)
2012 } else if fromdom == todom {
2013 addRedirectErrorf("will not redirect domain %s to itself", todom)
2014 }
2015 var zerodom dns.Domain
2016 if _, ok := c.WebDNSDomainRedirects[fromdom]; ok && fromdom != zerodom {
2017 addRedirectErrorf("duplicate redirect domain %s", from)
2018 }
2019 c.WebDNSDomainRedirects[fromdom] = todom
2020 }
2021
2022 for i := range c.WebHandlers {
2023 wh := &c.WebHandlers[i]
2024
2025 addHandlerErrorf := func(format string, args ...any) {
2026 addErrorf("webhandler %s %s: %s", wh.Domain, wh.PathRegexp, fmt.Sprintf(format, args...))
2027 }
2028
2029 if wh.LogName == "" {
2030 wh.Name = fmt.Sprintf("%d", i)
2031 } else {
2032 wh.Name = wh.LogName
2033 }
2034
2035 dom, err := dns.ParseDomain(wh.Domain)
2036 if err != nil {
2037 addHandlerErrorf("parsing domain: %v", err)
2038 }
2039 wh.DNSDomain = dom
2040
2041 if !strings.HasPrefix(wh.PathRegexp, "^") {
2042 addHandlerErrorf("path regexp must start with a ^")
2043 }
2044 re, err := regexp.Compile(wh.PathRegexp)
2045 if err != nil {
2046 addHandlerErrorf("compiling regexp: %v", err)
2047 }
2048 wh.Path = re
2049
2050 var n int
2051 if wh.WebStatic != nil {
2052 n++
2053 ws := wh.WebStatic
2054 if ws.StripPrefix != "" && !strings.HasPrefix(ws.StripPrefix, "/") {
2055 addHandlerErrorf("static: prefix to strip %s must start with a slash", ws.StripPrefix)
2056 }
2057 for k := range ws.ResponseHeaders {
2058 xk := k
2059 k := strings.TrimSpace(xk)
2060 if k != xk || k == "" {
2061 addHandlerErrorf("static: bad header %q", xk)
2062 }
2063 }
2064 }
2065 if wh.WebRedirect != nil {
2066 n++
2067 wr := wh.WebRedirect
2068 if wr.BaseURL != "" {
2069 u, err := url.Parse(wr.BaseURL)
2070 if err != nil {
2071 addHandlerErrorf("redirect: parsing redirect url %s: %v", wr.BaseURL, err)
2072 }
2073 switch u.Path {
2074 case "", "/":
2075 u.Path = "/"
2076 default:
2077 addHandlerErrorf("redirect: BaseURL must have empty path: %s", wr.BaseURL)
2078 }
2079 wr.URL = u
2080 }
2081 if wr.OrigPathRegexp != "" && wr.ReplacePath != "" {
2082 re, err := regexp.Compile(wr.OrigPathRegexp)
2083 if err != nil {
2084 addHandlerErrorf("compiling regexp %s: %v", wr.OrigPathRegexp, err)
2085 }
2086 wr.OrigPath = re
2087 } else if wr.OrigPathRegexp != "" || wr.ReplacePath != "" {
2088 addHandlerErrorf("redirect: must have either both OrigPathRegexp and ReplacePath, or neither")
2089 } else if wr.BaseURL == "" {
2090 addHandlerErrorf("must at least one of BaseURL and OrigPathRegexp+ReplacePath")
2091 }
2092 if wr.StatusCode != 0 && (wr.StatusCode < 300 || wr.StatusCode >= 400) {
2093 addHandlerErrorf("redirect: invalid redirect status code %d", wr.StatusCode)
2094 }
2095 }
2096 if wh.WebForward != nil {
2097 n++
2098 wf := wh.WebForward
2099 u, err := url.Parse(wf.URL)
2100 if err != nil {
2101 addHandlerErrorf("forward: parsing url %s: %v", wf.URL, err)
2102 }
2103 wf.TargetURL = u
2104
2105 for k := range wf.ResponseHeaders {
2106 xk := k
2107 k := strings.TrimSpace(xk)
2108 if k != xk || k == "" {
2109 addHandlerErrorf("forrward: bad header %q", xk)
2110 }
2111 }
2112 }
2113 if wh.WebInternal != nil {
2114 n++
2115 wi := wh.WebInternal
2116 if !strings.HasPrefix(wi.BasePath, "/") || !strings.HasSuffix(wi.BasePath, "/") {
2117 addHandlerErrorf("internal service: base path %q must start and end with /", wi.BasePath)
2118 }
2119 // todo: we could make maxMsgSize and accountPath configurable
2120 const isForwarded = false
2121 switch wi.Service {
2122 case "admin":
2123 wi.Handler = NewWebadminHandler(wi.BasePath, isForwarded)
2124 case "account":
2125 wi.Handler = NewWebaccountHandler(wi.BasePath, isForwarded)
2126 case "webmail":
2127 accountPath := ""
2128 wi.Handler = NewWebmailHandler(config.DefaultMaxMsgSize, wi.BasePath, isForwarded, accountPath)
2129 case "webapi":
2130 wi.Handler = NewWebapiHandler(config.DefaultMaxMsgSize, wi.BasePath, isForwarded)
2131 default:
2132 addHandlerErrorf("internal service: unknown service %q", wi.Service)
2133 }
2134 wi.Handler = SafeHeaders(http.StripPrefix(wi.BasePath[:len(wi.BasePath)-1], wi.Handler))
2135 }
2136 if n != 1 {
2137 addHandlerErrorf("must have exactly one handler, not %d", n)
2138 }
2139 }
2140
2141 c.MonitorDNSBLZones = nil
2142 for _, s := range c.MonitorDNSBLs {
2143 d, err := dns.ParseDomain(s)
2144 if err != nil {
2145 addErrorf("dnsbl %s: parsing dnsbl zone: %v", s, err)
2146 continue
2147 }
2148 if slices.Contains(c.MonitorDNSBLZones, d) {
2149 addErrorf("dnsbl %s: duplicate zone", s)
2150 continue
2151 }
2152 c.MonitorDNSBLZones = append(c.MonitorDNSBLZones, d)
2153 }
2154
2155 return
2156}
2157
2158func loadPrivateKeyFile(keyPath string) (crypto.Signer, error) {
2159 keyBuf, err := os.ReadFile(keyPath)
2160 if err != nil {
2161 return nil, fmt.Errorf("reading host private key: %v", err)
2162 }
2163 b, _ := pem.Decode(keyBuf)
2164 if b == nil {
2165 return nil, fmt.Errorf("parsing pem block for private key: %v", err)
2166 }
2167 var privKey any
2168 switch b.Type {
2169 case "PRIVATE KEY":
2170 privKey, err = x509.ParsePKCS8PrivateKey(b.Bytes)
2171 case "RSA PRIVATE KEY":
2172 privKey, err = x509.ParsePKCS1PrivateKey(b.Bytes)
2173 case "EC PRIVATE KEY":
2174 privKey, err = x509.ParseECPrivateKey(b.Bytes)
2175 default:
2176 err = fmt.Errorf("unknown pem type %q", b.Type)
2177 }
2178 if err != nil {
2179 return nil, fmt.Errorf("parsing private key: %v", err)
2180 }
2181 if k, ok := privKey.(crypto.Signer); ok {
2182 return k, nil
2183 }
2184 return nil, fmt.Errorf("parsed private key not a crypto.Signer, but %T", privKey)
2185}
2186
2187func loadTLSKeyCerts(configFile, kind string, ctls *config.TLS) error {
2188 certs := []tls.Certificate{}
2189 for _, kp := range ctls.KeyCerts {
2190 certPath := configDirPath(configFile, kp.CertFile)
2191 keyPath := configDirPath(configFile, kp.KeyFile)
2192 cert, err := loadX509KeyPairPrivileged(certPath, keyPath)
2193 if err != nil {
2194 return fmt.Errorf("tls config for %q: parsing x509 key pair: %v", kind, err)
2195 }
2196 certs = append(certs, cert)
2197 }
2198 ctls.Config = &tls.Config{
2199 Certificates: certs,
2200 }
2201 ctls.ConfigFallback = ctls.Config
2202 return nil
2203}
2204
2205// load x509 key/cert files from file descriptor possibly passed in by privileged
2206// process.
2207func loadX509KeyPairPrivileged(certPath, keyPath string) (tls.Certificate, error) {
2208 certBuf, err := readFilePrivileged(certPath)
2209 if err != nil {
2210 return tls.Certificate{}, fmt.Errorf("reading tls certificate: %v", err)
2211 }
2212 keyBuf, err := readFilePrivileged(keyPath)
2213 if err != nil {
2214 return tls.Certificate{}, fmt.Errorf("reading tls key: %v", err)
2215 }
2216 return tls.X509KeyPair(certBuf, keyBuf)
2217}
2218
2219// like os.ReadFile, but open privileged file possibly passed in by root process.
2220func readFilePrivileged(path string) ([]byte, error) {
2221 f, err := OpenPrivileged(path)
2222 if err != nil {
2223 return nil, err
2224 }
2225 defer f.Close()
2226 return io.ReadAll(f)
2227}
2228