// Package config loads the YAML configuration. package config import ( "bytes" "crypto/x509" "errors" "fmt" "math" "net" "net/url" "os" "strings" "unicode" "gopkg.in/yaml.v3" ) // Config is the full daemon configuration. type Config struct { DB string `yaml:"db"` // SQLite path BaseURL string `yaml:"base_url"` // public web URL, enables one-click unsubscribe LMTP struct { Network string `yaml:"network"` // tcp or unix Addr string `yaml:"addr"` // Allow limits which clients may connect (CIDR ranges or single addresses, TCP only). // LMTP has no authentication: anyone who can connect can post as any member. Allow []string `yaml:"allow"` } `yaml:"lmtp"` API struct { Listen string `yaml:"listen"` // HTTP listen address; the bearer token comes from PARSIMONY_API_TOKEN } `yaml:"api"` Web struct { Listen string `yaml:"listen"` // public archive and unsubscribe pages; empty disables them // TrustedProxies are the reverse proxies (CIDR ranges or single addresses) whose // X-Forwarded-For names the client of the subscribe form's rate limit. Empty, the default: // the TCP peer is the client and the header is ignored. TrustedProxies []string `yaml:"trusted_proxies"` } `yaml:"web"` Limits struct { // PendingPerHour is the most subscribe and unsubscribe requests (each one sends a // confirmation mail) accepted in an hour, for all lists together. PendingPerHour Count `yaml:"pending_per_hour"` } `yaml:"limits"` // DKIM lists the signing keys, one per domain. The private keys stay in files. DKIM []DKIMKey `yaml:"dkim"` Relay struct { Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay Concurrency int `yaml:"concurrency"` // simultaneous deliveries // RatePerSecond is the most sends started per second, for relays that throttle (Amazon // SES). Zero, the default, is unlimited. RatePerSecond Rate `yaml:"rate_per_second"` // TLS is "none" (default: a local Postfix), "starttls" (required, never silently skipped) // or "tls" (encrypted from the first byte, port 465). TLS string `yaml:"tls"` ServerName string `yaml:"server_name"` // name the certificate must match; the host of addr by default CAFile string `yaml:"ca_file"` // PEM bundle of the CAs to trust; the system's by default Helo string `yaml:"helo"` // EHLO name; this machine's host name by default // Username and the content of PasswordFile are the SMTP AUTH PLAIN credentials. The password // is never in the config itself. Username string `yaml:"username"` PasswordFile string `yaml:"password_file"` } `yaml:"relay"` } // Rate is a number of sends per second. It exists so that a value that is not a number is reported // with the name of the key, as the other checks are. type Rate float64 // UnmarshalYAML reads a YAML number. func (r *Rate) UnmarshalYAML(n *yaml.Node) error { var f float64 if err := n.Decode(&f); err != nil { return fmt.Errorf("line %d: relay.rate_per_second must be a number of sends per second, not %q", n.Line, n.Value) } *r = Rate(f) return nil } // Count is limits.pending_per_hour. It exists so that a value that is not a whole number is reported // with the name of the key. type Count int // UnmarshalYAML reads a YAML integer. func (c *Count) UnmarshalYAML(n *yaml.Node) error { var i int if n.Tag != "!!int" || n.Decode(&i) != nil { return fmt.Errorf("line %d: limits.pending_per_hour must be a whole number, not %q", n.Line, n.Value) } *c = Count(i) return nil } // DKIMKey is the signing key of one domain. type DKIMKey struct { Domain string `yaml:"domain"` Selector string `yaml:"selector"` KeyFile string `yaml:"key_file"` // PEM private key } // LMTPAllowed returns the parsed lmtp.allow list (empty: every client is accepted). func (c Config) LMTPAllowed() []*net.IPNet { return parseNets(c.LMTP.Allow) } // WebTrustedProxies returns the parsed web.trusted_proxies list (empty: X-Forwarded-For is ignored). func (c Config) WebTrustedProxies() []*net.IPNet { return parseNets(c.Web.TrustedProxies) } // parseNets parses a list that validate has already checked. func parseNets(list []string) []*net.IPNet { nets := make([]*net.IPNet, 0, len(list)) for _, a := range list { if n, err := parseNet(a); err == nil { nets = append(nets, n) } } return nets } // parseNet accepts "10.0.0.0/8" or a single address ("10.1.2.3", "::1"). func parseNet(s string) (*net.IPNet, error) { s = strings.TrimSpace(s) if strings.Contains(s, "/") { _, n, err := net.ParseCIDR(s) return n, err } ip := net.ParseIP(s) if ip == nil { return nil, fmt.Errorf("not an IP address or CIDR range: %q", s) } bits := 128 if ip4 := ip.To4(); ip4 != nil { ip, bits = ip4, 32 } return &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)}, nil } func isLoopbackHost(host string) bool { if host == "localhost" { return true } ip := net.ParseIP(host) return ip != nil && ip.IsLoopback() } // RelayPassword reads the relay password from its file (only the line ending is removed). It is // empty when no username is configured. func (c Config) RelayPassword() (string, error) { if c.Relay.Username == "" { return "", nil } b, err := os.ReadFile(c.Relay.PasswordFile) if err != nil { return "", fmt.Errorf("relay.password_file: %w", err) } pw := strings.TrimRight(string(b), "\r\n") if pw == "" { return "", fmt.Errorf("relay.password_file %s is empty", c.Relay.PasswordFile) } return pw, nil } // RelayRootCAs reads relay.ca_file. It returns nil when none is set: the system roots apply. func (c Config) RelayRootCAs() (*x509.CertPool, error) { if c.Relay.CAFile == "" { return nil, nil } b, err := os.ReadFile(c.Relay.CAFile) if err != nil { return nil, fmt.Errorf("relay.ca_file: %w", err) } pool := x509.NewCertPool() if !pool.AppendCertsFromPEM(b) { return nil, fmt.Errorf("relay.ca_file %s contains no certificate", c.Relay.CAFile) } return pool, nil } func (c Config) validateRelay() error { r := c.Relay host, _, err := net.SplitHostPort(r.Addr) if err != nil { return fmt.Errorf("relay.addr %q must be host:port", r.Addr) } switch r.TLS { case "", "none", "starttls", "tls": default: return fmt.Errorf("relay.tls must be none, starttls or tls, not %q", r.TLS) } if math.IsNaN(float64(r.RatePerSecond)) || math.IsInf(float64(r.RatePerSecond), 0) || r.RatePerSecond < 0 { return fmt.Errorf("relay.rate_per_second must be a number of sends per second, 0 or more (0 is unlimited), not %v", r.RatePerSecond) } if (r.Username == "") != (r.PasswordFile == "") { return errors.New("relay.username and relay.password_file go together") } if r.Username != "" && (r.TLS == "" || r.TLS == "none") && !isLoopbackHost(host) { return fmt.Errorf("relay.username is set but relay.tls is not: the password would cross the network in clear to %s; set relay.tls to starttls or tls", host) } return nil } // validateBaseURL checks base_url, the public URL the one-click unsubscribe links are built from. // RFC 8058 needs https; http is accepted for a loopback host (development and tests). Empty is fine: // there is no one-click link then. func (c Config) validateBaseURL() error { if c.BaseURL == "" { return nil } u, err := url.Parse(c.BaseURL) switch { case err != nil || strings.ContainsFunc(c.BaseURL, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }): return fmt.Errorf("base_url %q is not a valid URL", c.BaseURL) case u.Hostname() == "": return fmt.Errorf("base_url %q needs a scheme and a host, such as https://lists.example.org", c.BaseURL) case u.Scheme != "https" && !(u.Scheme == "http" && isLoopbackHost(u.Hostname())): return fmt.Errorf("base_url %q must use https (one-click unsubscribe, RFC 8058); http is only accepted for localhost", c.BaseURL) case u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || strings.Contains(c.BaseURL, "#"): return fmt.Errorf("base_url %q must not contain credentials, a query or a fragment", c.BaseURL) } return nil } func (c Config) validate() error { if err := c.validateBaseURL(); err != nil { return err } if err := c.validateRelay(); err != nil { return err } for i, a := range c.LMTP.Allow { if _, err := parseNet(a); err != nil { return fmt.Errorf("lmtp.allow[%d]: %w", i, err) } } if c.Limits.PendingPerHour < 1 { return fmt.Errorf("limits.pending_per_hour must be 1 or more, not %d", c.Limits.PendingPerHour) } for i, a := range c.Web.TrustedProxies { if _, err := parseNet(a); err != nil { return fmt.Errorf("web.trusted_proxies[%d]: %w", i, err) } } seen := map[string]bool{} for i, k := range c.DKIM { switch { case strings.TrimSpace(k.Domain) == "": return fmt.Errorf("dkim[%d]: domain is required", i) case strings.TrimSpace(k.Selector) == "": return fmt.Errorf("dkim[%d] (%s): selector is required", i, k.Domain) case strings.TrimSpace(k.KeyFile) == "": return fmt.Errorf("dkim[%d] (%s): key_file is required", i, k.Domain) } d := strings.ToLower(k.Domain) if seen[d] { return fmt.Errorf("dkim[%d]: more than one key for %s", i, k.Domain) } seen[d] = true } return nil } // Load reads path and applies defaults. func Load(path string) (Config, error) { var c Config c.DB = "parsimony.db" c.LMTP.Network = "tcp" c.LMTP.Addr = "127.0.0.1:2424" c.Relay.Addr = "127.0.0.1:25" c.Relay.Concurrency = 4 c.API.Listen = "127.0.0.1:8080" c.Web.Listen = "127.0.0.1:8081" c.Limits.PendingPerHour = 300 b, err := os.ReadFile(path) if err != nil { return c, err } dec := yaml.NewDecoder(bytes.NewReader(b)) dec.KnownFields(true) if err := dec.Decode(&c); err != nil { return c, fmt.Errorf("parse %s: %w", path, err) } if err := c.validate(); err != nil { return c, fmt.Errorf("%s: %w", path, err) } return c, nil }