Files
selfpost/internal/health/milter.go
T
mixandClaude Opus 5.5 98e09fef4a
test / test (push) Canceled after 0s
health: sockets dialled, the certificate Postfix serves, "deferred" only for the queue
Audit 2026-10-12, stage 2c, "Health says what is served"; the pages of
ea8b6bf's mockup.

Sockets (A-38). A unix socket's file outlives a killed OpenDKIM or panel
and refuses every connect() (unix(7)); Health said "Listening" from a
stat. CheckSocket and CheckMilter's unix case now dial: refused is
"Nothing listens on the ... socket: the program that made it has
stopped", EACCES is unknown (the panel may not connect), anything else
"does not answer". The probe closes before a byte, which no milter logs:
libmilter takes the EOF as SMFIC_EOF without a line (sendmail
8.17.1.9-2+deb12u2, the image's libmilter1.0.1, libmilter/comm.c
mi_rd_cmd(), engine.c mi_engine() - its source is now in dev/upstream),
OpenDKIM's mlfi_close() has no connection to free, go-milter v0.4.1
returns quietly on io.EOF (session.go, HandleMilterCommands()). The page
polls every 5 s while in view, so this mattered.

Certificate (A-65). Postfix serves build/postfix-config.sh's copy,
refreshed by cert-reload every TLS_RELOAD_INTERVAL_SECONDS. The panel now
reads smtpd_tls_cert_file from postconf -h at its start (as the retry
policy) and TLS_RELOAD_INTERVAL_SECONDS; CheckServedCertificate checks the
served copy and, when the mounted file differs byte for byte (cert-reload's
own cmp -s), names it as Renewal: its expiry and mtime, two facts on the
card. Waiting is fine for the interval: a nearing expiry of the served one
is then no warning (an expiry still an error); past interval + 1 h it is a
warning pointing at cert-reload's lines. The notifier sends no expiry
notice while a renewal waits on time.

Wording (D-P1, D-P3, and the same for the spam filter): Sender check
note, help topics sender-check, upstream, filter, certificate - word for
word as in the mockup; the filter row's detail too.

Tests: stale socket file (Linux) refused for CheckSocket and CheckMilter;
CheckServedCertificate (same, waiting, late, expired + waiting, no served
path); intervalText; the card's facts; the notifier quiet while a renewal
waits, speaking once it is late. Stand (2026-10-15, make e2e, exit 0):
new health_shows_what_is_served - the three sockets dialled ok by the
panel's user; a certificate mounted without its key is refused by
cert-reload ("do not belong together") and the card shows the served
expiry, "Renewal expires" with the mounted one's, "Renewal mounted", and
"Postfix still serves the certificate above."; a matching pair is then
served again. go vet and go test ./... pass on the server (24 packages).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 00:44:21 +03:00

124 lines
3.9 KiB
Go

package health
import (
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
"syscall"
"time"
)
// milterDialTimeout caps the TCP probe. The Health page and Overview run every
// check on each poll, one after another, so this is the most a dead address can
// add to a poll.
const milterDialTimeout = time.Second
// CheckMilter probes a milter address in the two forms build/postfix-config.sh
// lets INBOUND_ANTISPAM_MILTER take (Postfix 3.7.11, src/milter/milter8.c,
// milter8_connect(), also accepts "local:", which that script refuses):
// unix:/path is stat-ed like any milter socket, and inet:host:port is dialled
// and the connection closed at once, with nothing written. required says what
// a failure costs, as for CheckSocket: an error when mail stops without the
// milter, a warning when it only goes through unscreened. An address in neither
// form is reported as not checkable (StatusUnknown), never as fine. Details say
// what failed in a sentence; they never carry the Go error.
func CheckMilter(name, addr string, required bool) Socket {
return checkMilter(name, addr, required, milterDialTimeout)
}
func checkMilter(name, addr string, required bool, timeout time.Duration) Socket {
s := Socket{Name: name, Path: addr}
addr = strings.TrimSpace(addr)
if addr == "" {
s.Status = StatusUnknown
s.Detail = "No address is configured."
return s
}
fail := func(detail string) Socket {
s.Status = missingStatus(required)
s.Detail = detail
return s
}
switch {
case strings.HasPrefix(addr, "unix:") && len(addr) > len("unix:"):
path := strings.TrimPrefix(addr, "unix:")
fi, err := os.Stat(path)
switch {
case err != nil:
return fail("The socket file does not exist.")
case fi.Mode()&os.ModeSocket == 0:
return fail("The path exists but is not a socket.")
}
// Dialled as an inet address is: a socket file outlives the
// filter that made it (CheckSocket; assumptions A-38).
switch err := dialUnix(path); {
case err == nil:
case errors.Is(err, syscall.EACCES):
s.Status = StatusUnknown
s.Detail = "The panel may not connect to the socket, so whether the filter listens is not known."
return s
case errors.Is(err, syscall.ECONNREFUSED):
return fail("Nothing listens on the socket: the filter that made it has stopped.")
default:
return fail("The socket does not answer.")
}
case strings.HasPrefix(addr, "inet:"):
host, port, ok := splitInet(strings.TrimPrefix(addr, "inet:"))
if !ok {
return notCheckable(s)
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
return fail(milterDialDetail(err, host, port))
}
_ = conn.Close()
default:
return notCheckable(s)
}
s.Present = true
s.Status = StatusOK
s.Detail = "Listening"
return s
}
func notCheckable(s Socket) Socket {
s.Status = StatusUnknown
s.Detail = "The address is not unix:/path or inet:host:port, so it cannot be checked."
return s
}
// splitInet reads host:port (or [v6]:port) with a numeric port.
func splitInet(hp string) (host, port string, ok bool) {
host, port, err := net.SplitHostPort(hp)
if err != nil || host == "" {
return "", "", false
}
if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 {
return "", "", false
}
return host, port, true
}
// milterDialDetail turns a failed dial into a sentence that names only host and
// port.
func milterDialDetail(err error, host, port string) string {
hp := net.JoinHostPort(host, port)
var dns *net.DNSError
var nerr net.Error
switch {
case errors.As(err, &dns):
return fmt.Sprintf("The name %s could not be resolved.", host)
case errors.As(err, &nerr) && nerr.Timeout():
return fmt.Sprintf("No answer from %s within %d s.", hp, int(milterDialTimeout/time.Second))
case errors.Is(err, syscall.ECONNREFUSED) || strings.Contains(strings.ToLower(err.Error()), "refused"):
return fmt.Sprintf("Connection to %s refused.", hp)
}
return fmt.Sprintf("Could not connect to %s.", hp)
}