Files
selfpost/internal/health/milter.go
T
mixandClaude Opus 5.5 1c6e04b7d3
test / test (push) Canceled after 0s
base-trixie: the image on Debian trixie, Postfix 3.10.13; Received: hidden on 465/587
The runtime base is debian:trixie-slim, the build stage golang:1.26-trixie:
Postfix 3.10.13 with upstream's 2026 security fixes up to the August
batch, libmilter 8.18.1, OpenSSL 3.5. Every citation of another program in
code, scripts and documents was re-read against the trixie sources
(dev/upstream; bookworm's kept in dev/upstream-bookworm) and updated;
development.md's cited-package table holds the trixie versions.

What the re-reading changed in behaviour:
- logtail: reject lines with sasl_method/sasl_username/sasl_sender
  (smtpd_check.c:1019-1026); cleanup's "removed (canceled)" forgets a
  port-25 id (cleanup_api.c:363-364).
- internal/postfix/retry.go reads postconf's stdout only: since 3.9 a
  deprecated-parameter warning on stderr made one value too many and the
  panel fell back to the compiled-in retry policy.
- logrotate-loop.sh: logrotate 3.22 exits 1 when it ignores a config;
  the loop refuses on that output whatever the status.
- postfix-config.sh: cyrus_sasl_config_path set explicitly (bookworm's
  07_sasl_config.diff is gone); maillog_file_permissions=0640; mynetworks
  explicit; Dockerfile: SYNC_CHROOT= in /etc/default/postfix,
  opendkim-tools dropped, openssl named, systemd-standalone-sysusers so
  cron does not bring the whole of systemd.
- The selfpost group is 10998, outside the system range 100-999: in
  trixie a package took 998 and the build failed (owner's decision:
  a conflict-free id, the distribution's own ids untouched). The
  entrypoint hands /data back by name when the user or the group differs.
  Guide, backup refusal text, help (design commit 68ec8b2), e2e.
- smtpd_hide_client_session=yes on smtps and submission (owner's
  decision): Received: names no client, TLS, login or protocol; port 25
  keeps the full header. e2e checks both.
- test/upstream/opendkim-probe.sh makes keys with openssl genrsa.

Tests: on the server, make e2e exit 0 (674 s, the image built on trixie),
go vet + go test 27 packages ok. CHANGELOG [Unreleased], guide,
architecture.md, security.md, roadmap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:31:35 +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.10.13, 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)
}