Add optional inbound relay (backup-MX) behind INBOUND_RELAY_ENABLE.
test / test (push) Waiting to run
test / test (push) Waiting to run
Port 25 accepts only configured domains and listed recipients, then forwards to an upstream; the outbound path is unchanged when the flag is off. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
// Package inbound owns backup-MX / relay-forwarder domains: the SQLite
|
||||
// registry and the Postfix lookup tables (relay_domains, transport_maps,
|
||||
// relay_recipient_maps, smtp_tls_policy_maps). It does not listen on port 25
|
||||
// itself — postfix-config.sh does that when INBOUND_RELAY_ENABLE is true.
|
||||
package inbound
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/postfix"
|
||||
"github.com/mixeme/selfpost/internal/store"
|
||||
)
|
||||
|
||||
// Maps is the slice of the Postfix manager the inbound service needs.
|
||||
type Maps interface {
|
||||
RebuildInboundMaps(routes []postfix.InboundRoute) error
|
||||
}
|
||||
|
||||
// Service coordinates inbound-domain state across SQLite and the Postfix maps.
|
||||
type Service struct {
|
||||
store *store.Store
|
||||
maps Maps
|
||||
}
|
||||
|
||||
// NewService builds the inbound service over the shared store and Postfix maps.
|
||||
func NewService(st *store.Store, maps Maps) *Service {
|
||||
return &Service{store: st, maps: maps}
|
||||
}
|
||||
|
||||
// List returns every inbound domain.
|
||||
func (s *Service) List() ([]store.InboundDomain, error) {
|
||||
return s.store.ListInboundDomains()
|
||||
}
|
||||
|
||||
// Get returns one inbound domain by id.
|
||||
func (s *Service) Get(id int64) (store.InboundDomain, error) {
|
||||
return s.store.GetInboundDomain(id)
|
||||
}
|
||||
|
||||
// Add validates the domain name, inserts it, and rebuilds the maps.
|
||||
func (s *Service) Add(name string) (store.InboundDomain, error) {
|
||||
name = normalizeDomain(name)
|
||||
if err := checkDomain(name); err != nil {
|
||||
return store.InboundDomain{}, err
|
||||
}
|
||||
d, err := s.store.AddInboundDomain(name)
|
||||
if err != nil {
|
||||
return store.InboundDomain{}, err
|
||||
}
|
||||
if err := s.Resync(); err != nil {
|
||||
_ = s.store.DeleteInboundDomain(d.ID)
|
||||
return store.InboundDomain{}, err
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// SetTransport validates and saves the upstream, then rebuilds the maps.
|
||||
func (s *Service) SetTransport(id int64, host, portRaw, tlsMode string) error {
|
||||
if _, err := s.store.GetInboundDomain(id); err != nil {
|
||||
return err
|
||||
}
|
||||
host = normalizeHost(host)
|
||||
if err := checkHost(host); err != nil {
|
||||
return err
|
||||
}
|
||||
port, err := parsePort(portRaw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkTLSMode(tlsMode); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.store.UpdateInboundTransport(id, host, port, tlsMode); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Resync()
|
||||
}
|
||||
|
||||
// SetRecipients validates the mode and, in list mode, every address, then
|
||||
// rebuilds the maps.
|
||||
func (s *Service) SetRecipients(id int64, mode string, rawAddresses []string) error {
|
||||
d, err := s.store.GetInboundDomain(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkRecipientMode(mode); err != nil {
|
||||
return err
|
||||
}
|
||||
var addrs []string
|
||||
if mode == store.RecipientModeList {
|
||||
addrs, err = parseRecipientAddresses(rawAddresses, d.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := s.store.UpdateInboundRecipients(id, mode, addrs); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Resync()
|
||||
}
|
||||
|
||||
// Delete removes the domain and rebuilds the maps.
|
||||
func (s *Service) Delete(id int64) error {
|
||||
if err := s.store.DeleteInboundDomain(id); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Resync()
|
||||
}
|
||||
|
||||
// Resync rebuilds the inbound Postfix maps from SQLite.
|
||||
func (s *Service) Resync() error {
|
||||
list, err := s.store.ListInboundDomains()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
routes := make([]postfix.InboundRoute, 0, len(list))
|
||||
for _, d := range list {
|
||||
full, err := s.store.GetInboundDomain(d.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
routes = append(routes, postfix.InboundRoute{
|
||||
Domain: full.Name,
|
||||
Host: full.Host,
|
||||
Port: full.Port,
|
||||
TLSMode: full.TLSMode,
|
||||
RecipientMode: full.RecipientMode,
|
||||
Recipients: full.Recipients,
|
||||
})
|
||||
}
|
||||
return s.maps.RebuildInboundMaps(routes)
|
||||
}
|
||||
|
||||
func parseRecipientAddresses(raw []string, domain string) ([]string, error) {
|
||||
seen := make(map[string]bool)
|
||||
out := make([]string, 0, len(raw))
|
||||
for _, r := range raw {
|
||||
addr := strings.ToLower(strings.TrimSpace(r))
|
||||
if addr == "" {
|
||||
continue
|
||||
}
|
||||
if err := checkMailbox(addr, domain); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if seen[addr] {
|
||||
continue
|
||||
}
|
||||
seen[addr] = true
|
||||
out = append(out, addr)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("listed-recipients mode requires at least one address")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package inbound
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/postfix"
|
||||
"github.com/mixeme/selfpost/internal/store"
|
||||
)
|
||||
|
||||
type fakeMaps struct {
|
||||
routes []postfix.InboundRoute
|
||||
err error
|
||||
rebuild int
|
||||
}
|
||||
|
||||
func (f *fakeMaps) RebuildInboundMaps(routes []postfix.InboundRoute) error {
|
||||
f.rebuild++
|
||||
f.routes = append([]postfix.InboundRoute(nil), routes...)
|
||||
return f.err
|
||||
}
|
||||
|
||||
func testService(t *testing.T) (*Service, *store.Store, *fakeMaps) {
|
||||
t.Helper()
|
||||
st, err := store.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { st.Close() })
|
||||
m := &fakeMaps{}
|
||||
return NewService(st, m), st, m
|
||||
}
|
||||
|
||||
func TestAddAndSetTransport(t *testing.T) {
|
||||
s, _, m := testService(t)
|
||||
d, err := s.Add("Lists.Example.COM")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if d.Name != "lists.example.com" {
|
||||
t.Fatalf("name = %q", d.Name)
|
||||
}
|
||||
if m.rebuild != 1 {
|
||||
t.Fatalf("rebuild after add = %d", m.rebuild)
|
||||
}
|
||||
// Empty host is omitted from maps.
|
||||
if len(m.routes) != 1 || m.routes[0].Host != "" {
|
||||
t.Fatalf("routes after add: %+v", m.routes)
|
||||
}
|
||||
|
||||
if err := s.SetTransport(d.ID, "10.0.0.8", "25", store.TLSModeEncrypt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.routes[0].Host != "10.0.0.8" || m.routes[0].TLSMode != store.TLSModeEncrypt {
|
||||
t.Fatalf("routes after transport: %+v", m.routes[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRecipientsValidatesDomain(t *testing.T) {
|
||||
s, _, _ := testService(t)
|
||||
d, err := s.Add("lists.example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = s.SetRecipients(d.ID, store.RecipientModeList, []string{"staff@other.com"})
|
||||
if err == nil {
|
||||
t.Fatal("expected foreign-domain error")
|
||||
}
|
||||
err = s.SetRecipients(d.ID, store.RecipientModeList, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected empty-list error")
|
||||
}
|
||||
if err := s.SetRecipients(d.ID, store.RecipientModeList, []string{"staff@lists.example.com"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectsUnsafeHost(t *testing.T) {
|
||||
s, _, _ := testService(t)
|
||||
d, err := s.Add("lists.example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.SetTransport(d.ID, "10.0.0.8; rm", "25", store.TLSModeMay); err == nil {
|
||||
t.Fatal("expected unsafe host to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteResyncs(t *testing.T) {
|
||||
s, _, m := testService(t)
|
||||
d, err := s.Add("lists.example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Delete(d.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !errors.Is(mustGet(t, s, d.ID), store.ErrInboundDomainNotFound) {
|
||||
t.Fatal("domain still present")
|
||||
}
|
||||
if len(m.routes) != 0 {
|
||||
t.Fatalf("maps after delete: %+v", m.routes)
|
||||
}
|
||||
}
|
||||
|
||||
func mustGet(t *testing.T, s *Service, id int64) error {
|
||||
t.Helper()
|
||||
_, err := s.Get(id)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package inbound
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func normalizeDomain(name string) string {
|
||||
return strings.ToLower(strings.TrimSpace(name))
|
||||
}
|
||||
|
||||
func normalizeHost(host string) string {
|
||||
host = strings.ToLower(strings.TrimSpace(host))
|
||||
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
|
||||
host = host[1 : len(host)-1]
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func checkDomain(name string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("domain is required")
|
||||
}
|
||||
if len(name) > 253 {
|
||||
return fmt.Errorf("domain must be at most 253 characters")
|
||||
}
|
||||
labels := strings.Split(name, ".")
|
||||
if len(labels) < 2 {
|
||||
return fmt.Errorf("domain must include at least one dot (e.g. example.com)")
|
||||
}
|
||||
for _, label := range labels {
|
||||
if err := checkLabel(label); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkHost(host string) error {
|
||||
if host == "" {
|
||||
return fmt.Errorf("host is required")
|
||||
}
|
||||
if len(host) > 253 {
|
||||
return fmt.Errorf("host must be at most 253 characters")
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return nil
|
||||
}
|
||||
for _, label := range strings.Split(host, ".") {
|
||||
if err := checkLabel(label); err != nil {
|
||||
return fmt.Errorf("host is invalid: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkLabel(label string) error {
|
||||
if len(label) == 0 {
|
||||
return fmt.Errorf("must not contain an empty label")
|
||||
}
|
||||
if len(label) > 63 {
|
||||
return fmt.Errorf("each label must be at most 63 characters")
|
||||
}
|
||||
if label[0] == '-' || label[len(label)-1] == '-' {
|
||||
return fmt.Errorf("labels must not start or end with '-'")
|
||||
}
|
||||
for i := 0; i < len(label); i++ {
|
||||
c := label[i]
|
||||
lower := c >= 'a' && c <= 'z'
|
||||
digit := c >= '0' && c <= '9'
|
||||
if !lower && !digit && c != '-' {
|
||||
return fmt.Errorf("may contain only lower-case letters, digits, '.' and '-'")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parsePort(raw string) (int, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return 0, fmt.Errorf("port is required")
|
||||
}
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 || n > 65535 {
|
||||
return 0, fmt.Errorf("port must be between 1 and 65535")
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func checkTLSMode(mode string) error {
|
||||
switch mode {
|
||||
case "may", "encrypt", "none":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid TLS mode")
|
||||
}
|
||||
}
|
||||
|
||||
func checkRecipientMode(mode string) error {
|
||||
switch mode {
|
||||
case "list", "any":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid recipient mode")
|
||||
}
|
||||
}
|
||||
|
||||
func checkMailbox(addr, domain string) error {
|
||||
at := strings.LastIndexByte(addr, '@')
|
||||
if at <= 0 || at >= len(addr)-1 {
|
||||
return fmt.Errorf("%q is not a valid email address", addr)
|
||||
}
|
||||
local, host := addr[:at], addr[at+1:]
|
||||
if host != domain {
|
||||
return fmt.Errorf("%q does not belong to domain %s", addr, domain)
|
||||
}
|
||||
if local == "" || local[0] == '.' || local[len(local)-1] == '.' {
|
||||
return fmt.Errorf("%q: invalid local part", addr)
|
||||
}
|
||||
for i := 0; i < len(local); i++ {
|
||||
c := local[i]
|
||||
lower := c >= 'a' && c <= 'z'
|
||||
digit := c >= '0' && c <= '9'
|
||||
if !lower && !digit && c != '.' && c != '-' && c != '_' && c != '+' {
|
||||
return fmt.Errorf("%q: local part contains invalid characters", addr)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user