// Package app wires the store, router, LMTP server and REST API together. package app import ( "context" "errors" "fmt" "log" "net" "net/http" "os" "sync" "time" "github.com/emersion/go-smtp" "codeberg.org/squidd/parsimony/internal/adminui" "codeberg.org/squidd/parsimony/internal/api" "codeberg.org/squidd/parsimony/internal/config" "codeberg.org/squidd/parsimony/internal/dkimsign" "codeberg.org/squidd/parsimony/internal/ingest" "codeberg.org/squidd/parsimony/internal/outbound" "codeberg.org/squidd/parsimony/internal/queue" "codeberg.org/squidd/parsimony/internal/router" "codeberg.org/squidd/parsimony/internal/store" "codeberg.org/squidd/parsimony/internal/web" ) // shutdownTimeout bounds how long Close waits for the sessions in flight before it cuts them. var shutdownTimeout = 30 * time.Second // nowFunc is the clock of the store (queue schedules, token expiry). Tests move it. var nowFunc = time.Now // HTTP timeouts of the API + admin UI server and of the public web server, so // a slow or silent client cannot pin a goroutine. The archive import extends // its own deadlines (see api.importArchive). Variables so tests can shorten them. var ( httpReadTimeout = 30 * time.Second httpWriteTimeout = 60 * time.Second httpIdleTimeout = 120 * time.Second ) func newHTTPServer(h http.Handler) *http.Server { return &http.Server{ Handler: h, ReadHeaderTimeout: 10 * time.Second, ReadTimeout: httpReadTimeout, WriteTimeout: httpWriteTimeout, IdleTimeout: httpIdleTimeout, } } // heloName is the EHLO name: the configured one, else this machine's host name. func heloName(configured string) string { if configured != "" { return configured } if h, err := os.Hostname(); err == nil && h != "" { return h } return "localhost" } // allowListener drops connections from clients that are not in the allow list, // before the LMTP greeting: LMTP has no authentication of its own. type allowListener struct { net.Listener nets []*net.IPNet } func (l allowListener) Accept() (net.Conn, error) { for { c, err := l.Listener.Accept() if err != nil { return nil, err } if l.allowed(c.RemoteAddr()) { return c, nil } log.Printf("lmtp: refused a connection from %s (not in lmtp.allow)", c.RemoteAddr()) c.Close() } } func (l allowListener) allowed(addr net.Addr) bool { tcp, ok := addr.(*net.TCPAddr) if !ok { return false } for _, n := range l.nets { if n.Contains(tcp.IP) { return true } } return false } // lmtpIsOpen reports whether the LMTP listener is reachable beyond this machine. func lmtpIsOpen(network, addr string) bool { if network != "tcp" && network != "tcp4" && network != "tcp6" { return false // a unix socket is protected by the permissions of its file } host, _, err := net.SplitHostPort(addr) if err != nil { return true } if host == "localhost" { return false } ip := net.ParseIP(host) return ip == nil || !ip.IsLoopback() } // connTracker remembers the open connections of a listener, so Close can cut the ones still open // when the grace period ends: once Shutdown has begun, smtp.Server.Close does nothing. type connTracker struct { net.Listener mu sync.Mutex conns map[*trackedConn]struct{} } func (t *connTracker) Accept() (net.Conn, error) { c, err := t.Listener.Accept() if err != nil { return nil, err } tc := &trackedConn{Conn: c, t: t} t.mu.Lock() t.conns[tc] = struct{}{} t.mu.Unlock() return tc, nil } // closeAll cuts every connection still open. func (t *connTracker) closeAll() { t.mu.Lock() open := make([]*trackedConn, 0, len(t.conns)) for c := range t.conns { open = append(open, c) } t.mu.Unlock() for _, c := range open { c.Close() } } type trackedConn struct { net.Conn t *connTracker } func (c *trackedConn) Close() error { c.t.mu.Lock() delete(c.t.conns, c) c.t.mu.Unlock() return c.Conn.Close() } // App is a running daemon. type App struct { // LMTPAddr and APIAddr are the addresses actually bound. // APIAddr is empty when the API is disabled. LMTPAddr string APIAddr string WebAddr string // empty when the public web is disabled store *store.Store lmtp *smtp.Server lmtpOpen *connTracker // the LMTP connections, cut by Close when the grace period ends lmtpDone chan struct{} // closed when the LMTP Serve returns lns []net.Listener // closed by Close so Serve returns even if Close wins the race with it web *http.Server pub *http.Server wg sync.WaitGroup stop context.CancelFunc // stops the queue worker once sync.Once } // releaser lets the API approve held messages through the router. type releaser struct{ rt *router.Router } func (r releaser) Release(_ context.Context, id int64) error { return r.rt.Release(id) } // Start opens the database and begins serving. The REST API runs only when // apiToken is not empty. func Start(cfg config.Config, apiToken string) (*App, error) { // Load the signing keys first: a broken key stops the daemon now, not at the first send. var sign func([]byte) ([]byte, error) if len(cfg.DKIM) > 0 { keys := make([]dkimsign.Key, 0, len(cfg.DKIM)) for _, k := range cfg.DKIM { key, err := dkimsign.LoadKey(k.Domain, k.Selector, k.KeyFile) if err != nil { return nil, fmt.Errorf("dkim key for %s: %w", k.Domain, err) } keys = append(keys, key) } signer, err := dkimsign.New(keys) if err != nil { return nil, err } sign = signer.Sign } // Same for the relay secrets: read them now, not at the first send. relayPassword, err := cfg.RelayPassword() if err != nil { return nil, err } relayCAs, err := cfg.RelayRootCAs() if err != nil { return nil, err } st, err := store.Open(cfg.DB) if err != nil { return nil, fmt.Errorf("store: %w", err) } st.Now = nowFunc if cfg.Limits.PendingPerHour > 0 { // zero: a Config not made by config.Load, keep the default st.PendingPerHour = int(cfg.Limits.PendingPerHour) } a := &App{store: st} rt := router.New(st) rt.BaseURL = cfg.BaseURL worker := &queue.Worker{ Store: st, Sender: &outbound.SMTPRelay{ Addr: cfg.Relay.Addr, TLS: cfg.Relay.TLS, ServerName: cfg.Relay.ServerName, RootCAs: relayCAs, Username: cfg.Relay.Username, Password: relayPassword, Helo: heloName(cfg.Relay.Helo), }, Concurrency: cfg.Relay.Concurrency, Rate: float64(cfg.Relay.RatePerSecond), Sign: sign, } rt.OnEnqueue = worker.Wake a.lmtp = ingest.NewServer(rt) if cfg.LMTP.Network == "unix" { os.Remove(cfg.LMTP.Addr) } lmtpLn, err := net.Listen(cfg.LMTP.Network, cfg.LMTP.Addr) if err != nil { st.Close() return nil, fmt.Errorf("listen lmtp: %w", err) } a.LMTPAddr = lmtpLn.Addr().String() a.lns = append(a.lns, lmtpLn) serveLn := net.Listener(lmtpLn) if allow := cfg.LMTPAllowed(); len(allow) > 0 && (cfg.LMTP.Network == "tcp" || cfg.LMTP.Network == "tcp4" || cfg.LMTP.Network == "tcp6") { serveLn = allowListener{Listener: lmtpLn, nets: allow} } else if lmtpIsOpen(cfg.LMTP.Network, cfg.LMTP.Addr) { log.Printf("warning: LMTP on %s accepts any client and has no authentication, so anyone who can reach it can post as any member: set lmtp.allow or listen on 127.0.0.1", cfg.LMTP.Addr) } var apiLn net.Listener if apiToken != "" { apiLn, err = net.Listen("tcp", cfg.API.Listen) if err != nil { lmtpLn.Close() st.Close() return nil, fmt.Errorf("listen api: %w", err) } a.APIAddr = apiLn.Addr().String() a.lns = append(a.lns, apiLn) // The admin UI and the API share a listener, so the UI needs no CORS. mux := http.NewServeMux() mux.Handle("/api/", api.New(st, apiToken, releaser{rt})) mux.Handle("/", adminui.Embedded()) a.web = newHTTPServer(mux) } var pubLn net.Listener if cfg.Web.Listen != "" { pubLn, err = net.Listen("tcp", cfg.Web.Listen) if err != nil { for _, ln := range a.lns { ln.Close() } st.Close() return nil, fmt.Errorf("listen web: %w", err) } a.WebAddr = pubLn.Addr().String() a.lns = append(a.lns, pubLn) a.pub = newHTTPServer(web.New(st, rt, cfg.WebTrustedProxies())) } a.lmtpOpen = &connTracker{Listener: serveLn, conns: map[*trackedConn]struct{}{}} a.lmtpDone = make(chan struct{}) var workerCtx context.Context workerCtx, a.stop = context.WithCancel(context.Background()) a.wg.Add(1) go func() { defer a.wg.Done() worker.Run(workerCtx) }() a.wg.Add(1) go func() { defer a.wg.Done() defer close(a.lmtpDone) if err := a.lmtp.Serve(a.lmtpOpen); err != nil && !errors.Is(err, smtp.ErrServerClosed) && !errors.Is(err, net.ErrClosed) { log.Printf("lmtp: %v", err) } }() if a.web != nil { a.wg.Add(1) go func() { defer a.wg.Done() if err := a.web.Serve(apiLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) { log.Printf("api: %v", err) } }() } if a.pub != nil { a.wg.Add(1) go func() { defer a.wg.Done() if err := a.pub.Serve(pubLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) { log.Printf("web: %v", err) } }() } return a, nil } // Close stops the daemon and closes the database. It is safe to call twice. It stops accepting // connections, then lets the LMTP sessions and HTTP requests in flight finish, so a post being // received is queued and gets its 250 (a cut reply makes the MTA send the post again). After // shutdownTimeout it cuts whatever is still open. func (a *App) Close() { a.once.Do(func() { ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) defer cancel() for _, ln := range a.lns { ln.Close() } <-a.lmtpDone // Serve has returned: no connection is added while Shutdown waits if err := a.lmtp.Shutdown(ctx); errors.Is(err, context.DeadlineExceeded) { log.Printf("lmtp: sessions still open after %v, closing them", shutdownTimeout) a.lmtpOpen.closeAll() } for _, srv := range []*http.Server{a.web, a.pub} { if srv != nil && srv.Shutdown(ctx) != nil { srv.Close() } } a.stop() // in-flight sends are interrupted and stay queued a.wg.Wait() a.store.Close() }) }