Configuration
The proxy reads one TOML file, whose path is given as the first command-line argument or in the PROXY_CONFIG environment variable. TOML is the proxy’s own configuration format, and has no relation to how Stalwart is configured. The file can be edited by hand, or through the web interface and the management API, which rewrite it in place and keep its comments and formatting.
Every section of the file is optional, so a proxy can start with only the management interface enabled, as the configuration written by proxy init does. Startup fails only when the file declares neither a listener nor an [admin] table. A listener needs a [routing] table that names a default destination (listener.<name>: a listener requires a [routing] section with default_destination), and so does a [mapping] table.
The file is parsed and validated in full at startup, and again before every change made through the interface or a reload is applied; the proxy does not start, and a change is not applied, when validation fails. Among other things, validation checks the following:
- The default destination offers every protocol that a listener accepts, other than the SMTP and LMTP pass-through protocols.
- No destination sends credentials over a plaintext connection without an explicit override.
- A TLS connection to a backend addressed by IP address has a server name.
- No two listeners, and no listener and the management interface, bind the same or overlapping addresses.
- Listener addresses and destination hosts and ports stay within the operator allowlist, when one is set.
- The selected mapping store has its configuration section.
Keys that the proxy does not know are rejected: a misspelled key stops startup with an unknown field error that names the key and the line, instead of silently taking its default value. Validation reports every problem it finds at once, each prefixed with the dotted path of the offending key, for example listener.imap.max_connections: must be between 1 and 1000000. Problems found only when the configuration is put to use, such as a certificate file that cannot be read or an address that cannot be bound, are reported one at a time.
This page covers the layout of the file, the server options, inbound TLS and how changes take effect. Backend destinations, listeners, mappings and routing each have their own page.
File layout
Section titled “File layout”The file is divided into TOML tables, each for one part of the proxy:
| Table | Content |
|---|---|
[server] | Process-wide options |
[tls] | Inbound certificates ([tls.certificate.<name>]) and protocol policy ([tls.protocols]) |
[listener.<name>] | The sockets the proxy accepts connections on |
[destination.<name>] | The backends |
[routing] | The default destination, the pass-through destination and the master user separators |
[mapping] | The mapping store and its cache |
[capabilities.<protocol>] | What the proxy advertises to clients before authentication |
[http] and [[http.route]] | HTTP options and the ordered list of HTTP routing rules |
[oauth] | The token claim used as routing identifier |
[admin] | The management interface and API, and the operator allowlist ([admin.allow]) |
The examples contain annotated configurations for each use.
Server options
Section titled “Server options”The [server] table holds the process-wide options. It is optional, and every key has a default:
| Key | Default | Description |
|---|---|---|
threads | 0 | Number of worker threads. 0 uses one per CPU core. |
hostname | unset | Host name in the proxy’s own submission greeting and replies. When unset, the greeting uses the local address. The proxy’s EHLO to the backend uses the name the client announced, or this host name when the client announced none (proxy when hostname is unset). |
shutdown_grace | 30s | Time during which active sessions may finish after a shutdown signal. |
bridge_idle | 1800s | Maximum idle time of an established session before it is closed. |
backend_timeout | 10s | Time limit for each attempt to connect to a backend, and for the authentication on the backend. |
backend_connect_retries | 1 | Number of additional attempts after a backend connection fails for a reason that can be retried. |
host_down_threshold | 5 | Consecutive backend failures after which a destination is marked down. 0 disables the circuit breaker. |
host_down_min_clients | 2 | Number of distinct client addresses that a streak of failures attributable to clients must span before it marks a destination down. 0 removes the requirement. |
host_down_cooldown | 30s | Time a destination stays marked down before connections are let through again. |
proxy_ttl | 7 | Forwarding time-to-live sent to backends announced with XCLIENT or IMAP ID, decremented by one. A value of 1 or less makes the proxy refuse such connections. |
log_level | info | Log verbosity. The RUST_LOG environment variable overrides it. |
Durations are a number followed by a unit, one of ms, s, m, h or d, for example shutdown_grace = "45s" or bridge_idle = "1h".
bridge_idle applies after authentication, while client and backend exchange data. It must be long enough for IMAP IDLE sessions, which stay quiet for long periods. backend_timeout, the retries and the circuit breaker interact with backend health, as described under Destinations.
Inbound TLS
Section titled “Inbound TLS”The proxy terminates TLS for clients, both on listeners that use implicit TLS or STARTTLS and on the management interface. Two parts of the configuration govern it: the certificates, declared under [tls.certificate.<name>], and the protocol policy, under [tls.protocols].
Certificates
Section titled “Certificates”Each certificate entry names a PEM certificate chain and its private key. It can also name the host names it serves, and whether it is the default:
[tls.certificate.primary]cert = "/etc/proxy/certs/fullchain.pem"key = "/etc/proxy/certs/privkey.pem"subjects = ["mail.example.com", "*.example.com"]default = trueThe cert file holds the leaf certificate followed by the intermediate certificates, and the key file holds the matching private key. The subjects list names the host names that the certificate is presented for. When the list is omitted, the proxy reads the subject alternative names, or else the common name, from the certificate. default = true makes the certificate the one presented when no other entry matches. The name of the table (primary above) is a label only.
With a single certificate, that certificate is presented for every connection. When several certificates are declared, the proxy selects one per connection, from the Server Name Indication of the client, in this order:
- An exact match on the requested name, ignoring case
- A wildcard subject:
*.example.comserveshost.example.com, but neitherexample.comitself nora.host.example.com - The default certificate, set with
default = trueor with the subject*
When two certificates claim the same subject, or both are marked as the default, the one whose table name sorts first is used, and the proxy logs a warning at startup.
When nothing matches and no default is set, the handshake fails, because the proxy has no certificate to present. This includes clients that send no SNI name, for example when they connect by IP address. A production configuration therefore sets a default, and the proxy logs a warning at startup when several certificates are declared and none is the default.
The entry named admin has a special role: when it exists, the management interface always presents it, whatever name the browser requests, and never falls back to another certificate. proxy init creates this entry with a self-signed certificate. Keeping the management certificate separate means that adding, replacing or changing the default of the certificates for the mail host names never locks administrators out of the interface. The admin entry still takes part in the selection for client listeners like any other. proxy init marks it as the default, so client listeners present it too until another certificate is made the default; uploading a certificate as the default clears the flag on every other entry. Without an admin entry, the management interface presents one fixed certificate, whatever name the browser requests: the default certificate, or the only certificate when exactly one is declared, or otherwise a self-signed certificate that the proxy generates. A generated certificate changes every time the configuration is loaded, so a browser that trusted the previous one warns again; keeping an admin entry avoids this.
Certificates are usually uploaded through the web interface or the certificate endpoint of the API. The proxy checks that the key matches the certificate, writes both files into the certs/ directory next to the configuration file, readable by the proxy’s user only, and references them from the entry. Files referenced by hand keep working wherever they are, but a change made through the interface or the API may only point cert and key at a file directly inside certs/, as described under path confinement.
A listener that uses implicit TLS or STARTTLS, and the management interface, need at least one certificate: without one, startup fails with an error at tls.certificate. For tests and first trials, allow_self_signed lets the proxy generate a self-signed certificate for localhost, which clients do not trust:
[tls]allow_self_signed = trueWith this option set, the self-signed certificate is also presented when several certificates are declared and none of them matches, instead of failing the handshake. A new key pair is generated every time the configuration is loaded, including on each reload, so a client that accepted the self-signed certificate sees a different one afterwards.
Protocol policy
Section titled “Protocol policy”The [tls.protocols] table sets the handshake policy for all inbound TLS:
[tls.protocols]min_version = "1.2"ignore_client_order = falsedisable_cipher_suites = []min_version is "1.2" (the default, which allows TLS 1.2 and 1.3) or "1.3". With ignore_client_order = true, the server’s cipher suite order takes precedence over the client’s. disable_cipher_suites lists cipher suites, by their standard names, to remove from the negotiated set. An unknown name, or a list that removes every suite, is rejected at startup.
The policy applies to client connections and to the management interface, not to backends. Connections to backends always use TLS 1.2 or later, and their TLS is configured per destination, as described under Destinations.
Applying changes
Section titled “Applying changes”A change to the configuration takes effect without a restart, whether it is saved from the web interface, sent to the API or made by hand and followed by a reload. The proxy builds the complete new configuration first, including certificates and the mapping store, and swaps it in only when everything succeeded; a change that fails at any point leaves the running configuration as it was. Sessions already established keep the configuration they started with, and new connections use the new one.
Listeners follow the configuration live. A new listener binds its addresses before the change is applied, so an address that cannot be bound rejects the whole change and nothing is saved. A listener whose protocol, TLS mode, bind addresses and connection limit are unchanged keeps running untouched; a changed listener is replaced, reusing the sockets of addresses it was already bound to, so clients see no gap. A removed listener stops accepting new connections, while its open sessions continue until they end.
Two consequences of live binding are worth planning around:
- A replaced listener counts only the connections it accepted after the change against
max_connections. Sessions accepted before still hold their place in the previous count, so after several changes in quick succession the listener can briefly carry up to N+1 times its limit, where N is the number of changes, until the older sessions end. - Moving a listener between a wildcard address and a specific address on the same port, such as from
0.0.0.0:993to127.0.0.1:993, takes two changes: the first removes the old bind, and the second adds the new one. Applied at once, the new address cannot be bound while the old socket still holds the port, and the change is rejected with an error that says to apply the removal first.
Only a few settings are read once at startup. A change to server.threads, server.log_level or admin.bind, or adding or removing the [admin] table, is saved and validated but takes effect only at the next restart. The web interface shows these settings with a restart badge and a banner until the proxy restarts, and the API lists them in restart_required.
Environment overrides
Section titled “Environment overrides”Three environment variables take precedence over the file or replace a value in it:
PROXY_CONFIGnames the configuration file.RUST_LOGoverridesserver.log_level.PROXY_ADMIN_TOKENoverrides the management token in[admin].
They are described in Environment overrides.