Files
Georg Lauterbach 08b3cf3bb2 breaking(networking): replace PERMIT_DOCKER with DMS_TRUSTED_NETWORKS (#4848)
`_setup_docker_permit` always parsed an IPv4 address from
`NETWORK_INTERFACE` and aborted startup when there was none, even for the
default `PERMIT_DOCKER=none`, which does not need it. DMS therefore
failed to start in IPv6-only networks.

Replace `PERMIT_DOCKER` with `DMS_TRUSTED_NETWORKS`, which no longer
implies Docker, carries the `DMS_` prefix and supports IPv6:

- `none` (default): trust no client
- `host`, `subnet`: delegate to Postfix `mynetworks_style`, so no
  `ip addr` parsing is needed
- a list of CIDR ranges (IPv4 and IPv6, separated by spaces or commas)

The ranges Postfix derives or the user provides are normalized (lower
case, compressed IPv6, host bits masked) before they are added to the
OpenDKIM and OpenDMARC trust lists, as both only match that text form.
An invalid value now stops the startup instead of being cleared with a
warning.

`NETWORK_INTERFACE` is removed together with `helpers/network.sh`.
`PERMIT_DOCKER` and `NETWORK_INTERFACE` log an error when still set and
are ignored, so no client is trusted until the user migrates:

- `container` => `host`
- `host`, `connected-networks` => `subnet`
- `network` => `172.16.0.0/12` (or the exact range of the network)

- docs: document `DMS_TRUSTED_NETWORKS` with a migration table and update
  all pages and demo setups that used the old variables
- tests: add `trusted_networks.bats` (no IPv4 address, `host`, CIDR list,
  invalid value, legacy variables), rename `permit_docker.bats` and
  migrate all tests to the new variable
- changelog: add `Breaking` and `Fixed` entries

Closes #4634
Closes #3478
2026-11-11 19:50:14 +01:00

86 lines
3.6 KiB
Bash

#!/bin/bash
function _setup_mailname() {
_log 'debug' "Setting up mailname and creating '/etc/mailname'"
echo "${DOMAINNAME}" >/etc/mailname
}
# Prints the IPv4 or IPv6 address / network given as `${1}` as a CIDR range in the text form that OpenDKIM and
# OpenDMARC match client addresses against (lower case, compressed IPv6, host bits masked), e.g. `2001:DB8:0::5/32`
# becomes `2001:db8::/32`. Fails for anything else. The regex keeps out IPv6 zone IDs, which `ipaddress` accepts.
function __trusted_networks__normalize() {
[[ ${1} =~ ^[[:xdigit:]:.]+(/[[:digit:]]+)?$ ]] || return 1
python3 -c '
import ipaddress, sys
print(ipaddress.ip_network(sys.argv[1], strict=False))
' "${1}" 2>/dev/null
}
# Makes the OpenDKIM and OpenDMARC milters treat clients in the network `${1}` (CIDR, no brackets) as trusted.
function __trusted_networks__add_to_milters() {
if [[ ${ENABLE_OPENDMARC} -eq 1 ]]; then echo "${1}" >>/etc/opendmarc/ignore.hosts; fi
if [[ ${ENABLE_OPENDKIM} -eq 1 ]]; then echo "${1}" >>/etc/opendkim/TrustedHosts; fi
}
# Postfix `mynetworks` defines the clients that are allowed to relay without authentication.
# ref: https://www.postfix.org/postconf.5.html#mynetworks
function _setup_trusted_networks() {
_log 'debug' "Setting up trusted networks (DMS_TRUSTED_NETWORKS='${DMS_TRUSTED_NETWORKS}')"
local ENTRY NETWORK
local -a NETWORKS=()
case "${DMS_TRUSTED_NETWORKS}" in
( 'none' )
_log 'trace' "Clearing Postfix's 'mynetworks'"
postconf 'mynetworks ='
;;
( 'host' | 'subnet' )
_log 'trace' "Letting Postfix derive 'mynetworks' from the interface addresses (mynetworks_style=${DMS_TRUSTED_NETWORKS})"
_adjust_mtime_for_postfix_maincf
postconf -X mynetworks
postconf "mynetworks_style = ${DMS_TRUSTED_NETWORKS}"
# The warnings (e.g. IPv6 being unavailable) are not relevant here, Postfix repeats them when it starts.
read -r -a NETWORKS < <(postconf -h mynetworks 2>/dev/null)
for ENTRY in "${NETWORKS[@]}"; do
if NETWORK=$(__trusted_networks__normalize "${ENTRY//[\[\]]/}"); then
__trusted_networks__add_to_milters "${NETWORK}"
else
_log 'warn' "Trusted network '${ENTRY}' (derived by Postfix) is not understood and was not added to OpenDKIM / OpenDMARC"
fi
done
;;
( * ) # A list of CIDR ranges, separated by spaces and / or commas. The ranges extend Postfix's default `mynetworks`.
local POSTFIX_MYNETWORKS
POSTFIX_MYNETWORKS=$(postconf -h mynetworks)
local -a ENTRIES
IFS=', ' read -r -a ENTRIES <<< "${DMS_TRUSTED_NETWORKS}"
for ENTRY in "${ENTRIES[@]}"; do
[[ -z ${ENTRY} ]] && continue
NETWORK=$(__trusted_networks__normalize "${ENTRY}") || _dms_panic__invalid_value "DMS_TRUSTED_NETWORKS ('${ENTRY}' is neither 'none', 'host', 'subnet' nor a CIDR range)" 'Network Setup [trusted_networks]'
NETWORKS+=("${NETWORK}")
# Postfix expects IPv6 addresses in square brackets:
if [[ ${NETWORK} == *:* ]]; then
POSTFIX_MYNETWORKS+=" [${NETWORK%/*}]/${NETWORK#*/}"
else
POSTFIX_MYNETWORKS+=" ${NETWORK}"
fi
done
[[ ${#NETWORKS[@]} -eq 0 ]] && _dms_panic__invalid_value "DMS_TRUSTED_NETWORKS ('${DMS_TRUSTED_NETWORKS}' contains no CIDR range)" 'Network Setup [trusted_networks]'
_log 'trace' "Adding '${NETWORKS[*]}' to Postfix 'main.cf:mynetworks'"
_adjust_mtime_for_postfix_maincf
postconf "mynetworks = ${POSTFIX_MYNETWORKS}"
for NETWORK in "${NETWORKS[@]}"; do
__trusted_networks__add_to_milters "${NETWORK}"
done
;;
esac
}