Skip to content

Sender rewriting with srs-milter ​

srs-milter is a Postfix milter for the Sender Rewriting Scheme (SRS). A Sieve redirect sends a message on with its original envelope sender, and the receiver's SPF check then fails: the forwarder is not a server of the sender's domain. srs-milter rewrites the envelope sender into the forwarder's own domain, so the forward passes SPF, and turns a bounce to that address back to the original sender.

Where it runs ​

srs-milter runs on relay-fwd-out of yarilo-postfix, in mode both. relay-fwd-out carries the mail yarilo sends to other domains, and it is also the MX of the SRS domain:

  • Port 587, forward. First of the milters, before dkim-milter-fwd and arc-milter. It changes the envelope sender only, which neither the DKIM signature nor the ARC seal covers; being first, a forward it cannot decide is held before anything is signed.
  • Port 25, reverse. A listener of its own on a dedicated external address, the one the SRS domain's MX record names. It takes recipients in the SRS domain and nothing else, with no login, and srs-milter is its only milter.

relay-mx and relay-fwd do not run it. A redirect to a mailbox of a local domain is delivered by relay-fwd and never leaves the installation, so it needs no SRS.

Forwarding ​

The envelope sender user@sender.example leaves as:

SRS0=HHHH=TT=sender.example=user@fwd.example.org

HHHH is the hash, an HMAC of the rest under the SRS secret, and TT the day the address was made. Left alone:

  • the null sender;
  • a sender of a local domain (see below);
  • an address already in the SRS domain.

Another forwarder's SRS0 or SRS1 address becomes an SRS1 address that leads back to that forwarder, so the chain does not grow with each hop.

Local domains ​

A sender of a domain hosted here keeps its address: SPF passes for it as it is. The domains are a static list, a database query, or both. Answers of the database are cached; while the database is down an answer once read stands, and a forward from a domain never seen is tempfailed and waits in the queue rather than leaving without SRS.

Bounces ​

A recipient in the SRS domain is checked at RCPT TO:

  • a valid address is accepted and replaced by the original sender at the end of the message;
  • a forged, malformed or expired one is refused with 550.

An address turns bounces back for validityDays, 21 by default, the day it was made not counted.

Bounding replies ​

A valid SRS address is a relay to the original sender for anyone who has seen it once, for as long as it is valid. srs-milter can bound that:

  • Reply count. With a reply store, it counts the messages each address turned back and refuses the next one past replies.max with 550. The address of one sender is the same for a whole day, so the limit is per original sender and day of forwarding. A recipient check that sends no message is not counted, and a message refused at its end takes no reply from any of its addresses.
  • Size. replies.maxBytes refuses with 552 a message too large to be a delivery status notification.

The store is none by default: no count, as in other SRS implementations. sqlite keeps the count in one pod's file, redis shares it between replicas. The count is dropped two days after the address expires. While the store does not answer, mail to SRS addresses gets 451 and the sending server retries.

With replies.maxBytes set, a message whose recipients are both SRS addresses and others gets 452 for the later kind, so the sender delivers them apart: the size limit is the whole message's. Without it they share a message.

Compatibility ​

The address format and the defaults (HMAC-SHA1, four characters of hash, 21 days, no reply count, no size limit) are those of libsrs2. With the same secret, addresses another SRS daemon issued still turn back.

TIP

Four characters of hash give about 16 million values, and nothing bounds guessing at the MX. A longer hash, such as 8, is the safer choice for a new installation; raise hashLength and set hashMinLength to the old length for validityDays, so addresses issued before still turn back.

Secrets ​

The signing secret comes from a Secret. A second key, previousKey, holds the secret before it: both verify, the first signs. To rotate, put the old secret under the previous key and a new one under the first, and remove the previous one after validityDays.

Postfix configuration ​

yarilo-postfix sets both sides through fwdOut.srsMilter.addr and fwdOut.srsMilter.domain, which go together: one without the other does not render. On 587:

smtpd_milters = { inet:srs-milter:8894, default_action=tempfail },
                { inet:dkim-milter-fwd:8891, default_action=tempfail },
                { inet:arc-milter:8893, default_action=tempfail }

On 25, the SRS domain alone:

relay_domains = fwd.example.org
smtpd_recipient_restrictions = reject_non_fqdn_recipient,
    reject_unauth_destination,
    check_recipient_access regexp:/etc/postfix/srs-recipients.regexp,
    reject
smtpd_milters = { inet:srs-milter:8894, default_action=tempfail }

The recipient table takes @fwd.example.org and refuses everything else with 550, a subdomain of the SRS domain and localhost included.

tempfail keeps forwards and bounces queued while srs-milter is down; nothing else goes through it. Run more than one replica, with the redis store when replies are counted.

Environment ​

VariableDefault
MODEboth (or forward, reverse)
SRS_DOMAINnone, required
SRS_SECRETnone, required
SRS_SECRET_PREVIOUSnone
SRS_HASH_ALGORITHMsha1 (or sha256, sha512)
SRS_HASH_LENGTH4
SRS_HASH_MIN_LENGTHSRS_HASH_LENGTH
SRS_SEPARATOR= (or +, -)
SRS_VALIDITY_DAYS21
MAX_REPLY_BYTES0: no limit
REPLY_STOREnone (or sqlite, redis)
MAX_REPLIES10, read with a store
SQLITE_PATH/data/replies.db
REDIS_ADDR, or REDIS_SENTINEL_ADDRS with REDIS_SENTINEL_MASTERnone, one required with redis
REDIS_USERNAME, REDIS_PASSWORD, REDIS_DBnone, none, 0
REDIS_SENTINEL_USERNAME, REDIS_SENTINEL_PASSWORDnone
REDIS_KEY_PREFIXnone, required with redis
LOCAL_DOMAINSnone; comma-separated
DB_DRIVER, DB_DSNmysql (or postgres), none
LOCAL_DOMAIN_SQL_QUERYSELECT 1 FROM domain WHERE domain = ? AND active = 1; one placeholder, any row means local
DB_MAX_OPEN_CONNS4
DOMAIN_CACHE_SIZE, DOMAIN_CACHE_TTL_SECONDS, DOMAIN_CACHE_MISS_TTL_SECONDS10000, 300, 300
LISTEN_ADDR, METRICS_ADDR0.0.0.0:8894, 0.0.0.0:8081
MILTER_READ_TIMEOUT_SECONDS, MILTER_WRITE_TIMEOUT_SECONDS3600, 60
LOG_LEVELnone (debug for more)

Helm chart ​

The chart is published at oci://ghcr.io/yarilomail/charts/srs-milter and signed with cosign; its README shows how to verify it. It sets every variable above from its values, and the chart's defaults are the code's: mode, srs.* (srs.domain and srs.secret.name are required), replies.* with replies.store.*, localDomains.* and logLevel. replicaCount above 1 needs the none or redis store: the sqlite file is one pod's. networkPolicy.postfix opens the milter port to the Postfix pods named there; on yarilo-postfix that is relay-fwd-out alone.

sh
helm install srs-milter oci://ghcr.io/yarilomail/charts/srs-milter \
  --version <version> -n mail -f my-values.yaml

WARNING

A NetworkPolicy is enforced only by a CNI that implements it, such as Calico, Cilium, or the kube-router built into k3s. With flannel alone it is accepted and silently ignored.

Readiness and metrics ​

/readyz answers 200 once the domain query and the reply store answer; /healthz and /metrics are on the same port. Logs are JSON on stdout.

MetricMeaning
srs_forward_total{result}envelope senders seen: rewritten, relayed (another forwarder's SRS address, wrapped), long (rewritten past 64 octets), null, own, local, tempfail
srs_reverse_total{result}recipients in the SRS domain: reversed, not_srs, malformed, bad_hash, expired, over_limit, too_large, mixed, tempfail
srs_reply_store_errors_totalcalls to the reply store that failed; the message was tempfailed
srs_local_domain_lookups_total{result}lookups past the static list: hit, miss, stale (database down, old answer used), error