internal/notify applies the owner's rules. Each subject has a row of state, so a thing is said once and a restart says nothing again: - DNS of a sending domain: said after two hourly checks in a row found a record wrong, said to be over after two found it right. A lookup that did not answer moves nothing; a warning (no DMARC record) is not a subject. - TLS certificate: at 14 days and at 3, per certificate. - Queue: a message older than three days, once; once more when none is. - Level-2 limit: from the rejected rows of the send log, every five minutes; at most once a day per domain and application. Global users are told everything, domain users about their domains; one message per recipient. With the instance switch off nothing is checked and no state moves. A message that could not be handed to Postfix is tried again at the next check. Mail leaves by sendmail(1) as postmaster@<hostname>, addresses after "--", no shell. For it to be believed the server's own name is signed like a sending domain: domain.Service keeps a key for it and a line in OpenDKIM's tables without a registry row, and non_smtpd_milters now names OpenDKIM. A sending domain of the same name shares its key with the server. dnscheck.ServerMail checks the name's DKIM and SPF records; postfix.QueueMessages reads postqueue -j for arrival times. The notifier is a sixth role of the panel process. Nothing in the panel shows or switches it yet. Checked on Windows: go vet clean; go test passes for notify, store, postfix, dnscheck, cmd, and domain apart from TestWriteLoadPrivateKeyRoundtrip, which fails there on file modes and always did. Not yet run in a container: whether the unprivileged panel may submit is for the e2e step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
5.6 KiB
Go
164 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/mixeme/selfpost/internal/app"
|
|
"github.com/mixeme/selfpost/internal/buildinfo"
|
|
"github.com/mixeme/selfpost/internal/dmarc"
|
|
"github.com/mixeme/selfpost/internal/domain"
|
|
"github.com/mixeme/selfpost/internal/inbound"
|
|
"github.com/mixeme/selfpost/internal/postfix"
|
|
"github.com/mixeme/selfpost/internal/store"
|
|
"github.com/mixeme/selfpost/internal/web"
|
|
)
|
|
|
|
// mailStack is the panel's domain and application services plus the on-disk
|
|
// mail-path adapters they write through.
|
|
type mailStack struct {
|
|
Domains *domain.Service
|
|
Apps *app.Service
|
|
Inbound *inbound.Service
|
|
Lists *inbound.Lists
|
|
DMARC *dmarc.Service
|
|
pf *postfix.Postfix
|
|
odk *domain.OpenDKIM
|
|
inboundEnabled bool
|
|
dmarcEnabled bool
|
|
}
|
|
|
|
func newMailStack(cfg config, st *store.Store) *mailStack {
|
|
pf := postfix.New(cfg.postfixDir)
|
|
odk := domain.NewOpenDKIM(cfg.opendkimDir)
|
|
apps := app.NewService(st, app.NewSASLDB(cfg.saslDBPath, cfg.saslRealm), pf)
|
|
domains := domain.NewService(st, odk, apps, cfg.dkimSelectorDef)
|
|
// The panel's notifications are sent as postmaster@ the server's own name,
|
|
// and signed as it.
|
|
domains.SetServerName(cfg.hostname)
|
|
inb := inbound.NewService(st, pf)
|
|
dmarcSvc := dmarc.NewService(st, pf, cfg.hostname, cfg.dmarcEnabled)
|
|
return &mailStack{
|
|
Domains: domains, Apps: apps, Inbound: inb, DMARC: dmarcSvc,
|
|
Lists: inbound.NewLists(st, cfg.antispamDir),
|
|
pf: pf, odk: odk, inboundEnabled: cfg.inboundEnabled, dmarcEnabled: cfg.dmarcEnabled,
|
|
}
|
|
}
|
|
|
|
// Resync rebuilds OpenDKIM's tables and Postfix's sender map from SQLite and
|
|
// reloads both daemons — the same work as the Status page's Reload button.
|
|
func (m *mailStack) Resync() error {
|
|
if err := m.Domains.Resync(); err != nil {
|
|
return fmt.Errorf("opendkim resync: %w", err)
|
|
}
|
|
if err := m.Apps.Resync(); err != nil {
|
|
return fmt.Errorf("postfix resync: %w", err)
|
|
}
|
|
if m.inboundEnabled {
|
|
if err := m.Inbound.Resync(); err != nil {
|
|
return fmt.Errorf("inbound maps resync: %w", err)
|
|
}
|
|
// The filter's configuration names all six list files; they exist from
|
|
// the first start, empty until an entry is added.
|
|
if err := m.Lists.Sync(); err != nil {
|
|
return fmt.Errorf("filter lists resync: %w", err)
|
|
}
|
|
}
|
|
if m.dmarcEnabled {
|
|
if err := m.DMARC.Resync(); err != nil {
|
|
return fmt.Errorf("dmarc maps resync: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *mailStack) skipReloadForTest() {
|
|
m.pf.SetReloadHook(func() error { return nil })
|
|
m.odk.SetReloadHook(func() error { return nil })
|
|
}
|
|
|
|
// resyncAfterRestore runs one mail-path Resync on the first boot after a
|
|
// backup restore. testNoReload skips the supervisord reload step so restore
|
|
// tests can verify file regeneration without a running mail stack.
|
|
func resyncAfterRestore(cfg config, st *store.Store, testNoReload bool) error {
|
|
ms := newMailStack(cfg, st)
|
|
if testNoReload {
|
|
ms.skipReloadForTest()
|
|
}
|
|
return ms.Resync()
|
|
}
|
|
|
|
// newPanel wires the panel's services over the shared database handle and
|
|
// builds the HTTP application from cfg. It is the composition of the panel as
|
|
// the environment describes it, with nothing bound to a port yet.
|
|
func newPanel(cfg config, st *store.Store) (*web.Server, error) {
|
|
ms := newMailStack(cfg, st)
|
|
// postfix-config.sh has already run (entrypoint). postconf -h is the
|
|
// effective config, including a manual override; the panel keeps this
|
|
// snapshot for the process lifetime (architecture.md).
|
|
retryPolicy := postfix.LoadRetryPolicy()
|
|
return web.New(st, ms.Domains, ms.Apps, ms.Inbound, ms.Lists, ms.DMARC, web.Config{
|
|
Hostname: cfg.hostname,
|
|
CookieSecure: cfg.cookieSecure,
|
|
SubmissionEnabled: cfg.submissionEnabled,
|
|
MailLogPath: cfg.mailLog,
|
|
DataDir: cfg.dataDir,
|
|
DBPath: cfg.dbPath,
|
|
DeployRoot: cfg.deployRoot,
|
|
Version: buildinfo.Version,
|
|
TrustedProxyCIDRs: cfg.trustedProxies,
|
|
TLSCertFile: cfg.tlsCertFile,
|
|
OpenDKIMSocket: cfg.opendkimSocket,
|
|
JournalSocket: cfg.journalSocket,
|
|
SessionIdleDays: cfg.sessionIdleDays,
|
|
DNSResolvers: cfg.dnsResolvers,
|
|
RateLimitMessagesPerIP: cfg.rateLimitMessagesPerIP,
|
|
RateLimitWindowSeconds: cfg.rateLimitWindowSeconds,
|
|
RetryPolicy: retryPolicy,
|
|
InboundEnabled: cfg.inboundEnabled,
|
|
InboundAntispamMilter: cfg.inboundAntispamMilter,
|
|
InboundAntispamAction: cfg.inboundAntispamAction,
|
|
DMARCEnabled: cfg.dmarcEnabled,
|
|
SendLogRetentionEnvDefault: cfg.retentionDays,
|
|
|
|
InboundSpamLogRetentionEnvDefault: cfg.inboundSpamLogRetentionDays,
|
|
}, cfg.setupTokenPath)
|
|
}
|
|
|
|
// serveHTTP runs the control-panel HTTP server until ctx is cancelled, using
|
|
// the database handle shared by all roles: setup, login and the authenticated
|
|
// panel surface (security.md).
|
|
func serveHTTP(ctx context.Context, cfg config, st *store.Store) error {
|
|
srvApp, err := newPanel(cfg, st)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := srvApp.Start(); err != nil {
|
|
return err
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Addr: cfg.httpAddr,
|
|
Handler: srvApp.Handler(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
}
|
|
|
|
// Shut the server down cleanly when the process is asked to stop.
|
|
go func() {
|
|
<-ctx.Done()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
}()
|
|
|
|
log.Printf("http panel listening on %s", cfg.httpAddr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|