SMTP listeners
Karta's SMTP server is written in Elixir (Karta.SMTP.Session,
Karta.SMTP.Handler, on ThousandIsland). It binds no port unless listeners
are configured. In a release, KARTA_SMTP_ENABLED=true configures them from
the environment (Configuration reference).
The three listeners
| Listener | Default port | TLS | AUTH | What it accepts |
|---|---|---|---|---|
mx | 25 | STARTTLS offered when a certificate is configured | never | mail for the addresses that servers route; relayed mail from an IP range only with KARTA_SMTP_MX_IP_RELAY=true |
submission | 587 | STARTTLS, required before AUTH | required before MAIL, or an address in a server's range | mail from the server's verified domains |
submissions | 465 | implicit TLS (RFC 8314) | required before MAIL, or an address in a server's range | the same |
- Without
KARTA_SMTP_CERTFILE, only themxlistener runs, without TLS. KARTA_SMTP_LISTENERS=mxwith a certificate runs themxlistener alone, with STARTTLS: an instance that only receives mail opens no submission port.- The
mxlistener receives mail for the addresses that mail servers route (Karta.SMTP.Policy.Inbound), answered from the routes in memory, and refuses everything else: see Receiving mail for its replies.Karta.SMTP.Policy.Closed(a listener'spolicy) refuses every message. - The submission listeners are described from the client's side in SMTP submission.
Relayed mail from an address range
A server's IP range credential (Karta.SMTP.Relay) lets a client whose
address is in the range send through that server without AUTH. The range
is matched against the connection's peer address — from the socket, or
from the PROXY protocol header of a trusted proxy — at the first
MAIL FROM; from then on the connection follows the submission rules
(verified domains of the server's organisation, the credential's rate
limit, the server's suspension and TLS requirement). The submission
listeners always accept such clients; the mx listener does so only with
KARTA_SMTP_MX_IP_RELAY=true (or the listener option
policy_opts: [ip_relay: true]). With the option on, a database failure
answers MAIL with 451 4.3.0 rather than guess; with it off, MAIL
reads no credential at all.
Ranges are refused at creation when they are wider than
KARTA_SMTP_IP_MIN_PREFIX_V4 (default /16) or
KARTA_SMTP_IP_MIN_PREFIX_V6 (default /48), and the whole address
space is refused always. See
SMTP submission.
Extensions
EHLO advertises PIPELINING, SIZE 26214400, 8BITMIME, SMTPUTF8,
ENHANCEDSTATUSCODES, CHUNKING (BDAT), STARTTLS before TLS where a
certificate is configured, and AUTH PLAIN LOGIN SCRAM-SHA-256 on the
submission listeners after TLS.
REQUIRETLS (RFC 8689) is offered after STARTTLS on the submission
listeners (requiretls: true; KARTA_SMTP_REQUIRETLS=false turns it
off): outbound delivery honours it (Delivery).
The mx listener does not offer it, since Karta does not pass on mail it
receives yet. Over /api/v2 the option is require_tls. MAIL FROM with REQUIRETLS before TLS gets
530 5.7.10 REQUIRETLS needs a TLS connection.
DSN (RFC 3461) is offered on the submission listeners and not on the mx
listener. A client may send RET, ENVID, NOTIFY and ORCPT; Karta
delivers the message itself and writes the delivery status notification
for a recipient whose delivery succeeded (NOTIFY=SUCCESS) or failed for
good (the default, unless NOTIFY=NEVER), with the original's header or,
for RET=FULL, the message, and the ORCPT and ENVID it was given.
NOTIFY=DELAY is accepted and no notification is written for a delay. The
mx listener does not offer DSN because Karta does not pass the requests
on for the mail it receives and forwards: a client there gets 555 5.5.4 Unsupported parameter. KARTA_SMTP_DSN=false stops offering it on the
submission listeners.
VRFY answers 252; EXPN is not implemented (502); HELP points to
RFC 5321.
TLS
- TLS 1.3 and TLS 1.2 only. TLS 1.2 suites use ECDHE key exchange with AEAD
ciphers (AES-GCM, ChaCha20-Poly1305): no static RSA or ECDH key exchange,
no CBC, no SHA-1 MACs. No client-initiated renegotiation.
(
Karta.SMTP.TLSConfig; tested intest/karta/smtp/listener/tls_policy_test.exs.) - After
STARTTLS, everything the client sent before the handshake is discarded and the session starts again (RFC 3207). - Certificates are read from PEM files and checked: the key must match the certificate. Karta does not start when the initial files are missing or invalid.
Certificate reload
The files are read again every 60 seconds
(certificate_reload_interval). When their content changed and the new
pair is valid, new handshakes use it; connections in progress keep theirs,
and no listener restarts. An invalid new pair is logged and the previous one
stays in use. So a certificate renewed in place (for example by an ACME
client writing the same paths) takes effect within a minute.
Through the environment Karta takes one pair (KARTA_SMTP_CERTFILE,
KARTA_SMTP_KEYFILE). The configuration files accept several, such as an
RSA and an ECDSA certificate.
PROXY protocol
Behind a TCP load balancer, set KARTA_SMTP_TRUSTED_PROXIES to the
balancer's address ranges (comma-separated CIDR, e.g.
192.0.2.10/32,2001:db8::/64). Then every listener:
- accepts connections only from those ranges (others are closed and logged);
- reads a PROXY protocol version 2 header (the binary form) first,
within 10 seconds, and takes the client address from it. Version 1 (text)
is not supported. A
LOCALheader (the balancer's own health check) uses the balancer's address.
A balancer that fronts the mx listener alone (the usual case: port 25 comes
in through a relay, while an application on the same host submits mail
directly) sets KARTA_SMTP_SUBMISSION_TRUSTED_PROXIES to the submission
listeners' own list; empty, they take connections without a header. Publish
a submission port only where its clients are: credentials and TLS protect
it, but every client behind one published port may share one address in the
per-client limits.
The client address is used for connection limits and in the Received:
field. With implicit TLS (port 465) the header is read before the TLS
handshake. Headers carrying TLVs (cloud load balancers add them) are read.
The header was checked against the one HAProxy 3.1 writes
(send-proxy-v2, with and without a custom TLV): the opt-in test
test/karta/smtp/haproxy_test.exs (KARTA_TEST_HAPROXY=1) puts the real
HAProxy in front of a listener and checks that the listener sees the
client's address, not HAProxy's. Other balancers (the cloud ones: AWS NLB,
GCP, DigitalOcean) were not tried; each has its own switch for sending the
header, and a balancer that does not send it makes every connection fail,
because the listeners accept nothing else.
Limits
Per listener (defaults; configurable only in the configuration files):
| Limit | Default | Reply |
|---|---|---|
| Connections | max_connections 1024 for the listener (see below) | a new connection waits up to 5 seconds for room, then is closed |
| Connections per client (IPv4 address or IPv6 /64) | 10 | 421 4.7.0 Too many connections, try again later |
| New connections per client per minute | 60 | the same |
| Message size | 25 MiB (26,214,400 bytes) | 552 5.3.4 |
| Recipients per message | 100 | 452 4.5.3 Too many recipients |
| Command line length | 2048 bytes | 500 5.5.2 Line too long, connection closed |
| Header block | 1 MiB | 552 5.3.4 Message header too large |
| Protocol errors per connection | 10 | 421 4.7.0 Too many errors, closing connection |
| Policy rejections per connection | 20 | 421 4.7.0 Too many rejected commands, closing connection |
| Failed AUTH attempts per connection | 3 | 421 4.7.0 Too many authentication failures |
| Idle time waiting for a command or data | 5 minutes | 421 4.4.2 ... Error: timeout exceeded |
| Idle time when the listener holds 3/4 of its connections | 10 seconds | the same |
| Connection lifetime | 1 hour | 421 4.4.2 Connection time limit exceeded |
max_connections is shared out among the listener's acceptor processes
(ThousandIsland limits each acceptor), so a listener holds at most
max_connections connections: 1020 with the defaults (10 acceptors of 102
each). The stress threshold (3/4 of max_connections, 768) counts all of
the listener's connections.
Addresses in a listener's exempt ranges are counted but never limited.
Each connection runs in its own process with a memory cap, and message
data goes to the spool directory, not to memory, so one client cannot
exhaust the node.
On shutdown, connected clients get 421 4.3.2 ... Service shutting down.
The spool directory
KARTA_SPOOL_DIR holds the data of messages being received, one file per
message (msg-<random>.eml, mode 0600, in a directory of mode 0700). A file
is deleted when its connection ends, whatever the reason; files left by a
crashed node are removed at the next start. Use a disk-backed, private
volume.
Scanning the TLS settings
dev/tls-scan.sh starts test listeners and scans them with testssl.sh
(needs Docker); CI runs the same scan. See Development.