|
| 1 | +# `kumo.on('smtp_server_get_dynamic_parameters', function(listener, conn_meta))` |
| 2 | + |
| 3 | +{{since('dev')}} |
| 4 | + |
| 5 | +!!! note |
| 6 | + This option is primarily intended to be used together with |
| 7 | + a wildcard `listen` value of `0.0.0.0` for an IPv4 listener |
| 8 | + or `::` for an IPv6 listener where you desire to dynamically |
| 9 | + configure IP based virtual MTA service. |
| 10 | + |
| 11 | +Called by the ESMTP server when a new server session has accepted |
| 12 | +a connection from a client, and offers a chance to update the |
| 13 | +configuration for the listener dynamically. This event triggers |
| 14 | +before [smtp_server_connection_accepted](smtp_server_connection_accepted.md). |
| 15 | + |
| 16 | +The parameters are: |
| 17 | + |
| 18 | +* `listener` - the stringified version of the listener address, such as `0.0.0.0:25` |
| 19 | +* `conn_meta` - the [Connection Metadata](../connectionmeta.md) object |
| 20 | + |
| 21 | +The return value must be a table holding ESMTP listener parameter *overrides* |
| 22 | +that you wish to apply to the existing listener parameters for this connection. |
| 23 | +The fields that you specify in the return value will override the fields that |
| 24 | +were already configured. Almost every field described under |
| 25 | +[kumo.start_esmtp_listener](../kumo/start_esmtp_listener/index.md) can be used; |
| 26 | +those that cannot will indicate it in their individual documentation pages. |
| 27 | + |
| 28 | +The following example is equivalent to the |
| 29 | +[via](../kumo/start_esmtp_listener/via.md) example, except that rather than the |
| 30 | +`via` parameters being statically configured during the `init` event, they are |
| 31 | +computed for every new connection: |
| 32 | + |
| 33 | +```lua |
| 34 | +kumo.on('init', function() |
| 35 | + kumo.start_esmtp_listener { |
| 36 | + listen = '0.0.0.0:25', |
| 37 | + } |
| 38 | +end) |
| 39 | + |
| 40 | +kumo.on('smtp_server_get_dynamic_parameters', function(listener, conn_meta) |
| 41 | + return { |
| 42 | + via = { |
| 43 | + -- When clients connect to this server via its 10.0.0.1 IP |
| 44 | + -- address, we will use the hostname and TLS parameters |
| 45 | + -- defined in this block |
| 46 | + ['10.0.0.1'] = { |
| 47 | + hostname = 'mx.example-customer.com', |
| 48 | + tls_certificate = '/path/to/customer1.cert', |
| 49 | + tls_private_key = '/path/to/customer1.key', |
| 50 | + }, |
| 51 | + -- When clients connect to this server via its 10.0.0.2 IP |
| 52 | + -- address, we will use the hostname and TLS parameters |
| 53 | + -- defined in this block |
| 54 | + ['10.0.0.2'] = { |
| 55 | + hostname = 'mx.other-customer.com', |
| 56 | + tls_certificate = '/path/to/customer2.cert', |
| 57 | + tls_private_key = '/path/to/customer2.key', |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +end) |
| 62 | +``` |
| 63 | + |
| 64 | +This late binding of the configuration can be used to aid in dynamically |
| 65 | +updating the set of listeners on a wildcard port. |
| 66 | + |
| 67 | +For example, you could put the listener overrides into a TOML or JSON |
| 68 | +file that has the same shape as the listener parameters: |
| 69 | + |
| 70 | +```toml |
| 71 | +[via.'10.0.0.1'] |
| 72 | +hostname = 'mx.example-customer.com' |
| 73 | +tls_certificate = '/path/to/customer1.cert' |
| 74 | +tls_private_key = '/path/to/customer1.key' |
| 75 | + |
| 76 | +[via.'10.0.0.2'] |
| 77 | +hostname = 'mx.other-customer.com' |
| 78 | +tls_certificate = '/path/to/customer2.cert' |
| 79 | +tls_private_key = '/path/to/customer2.key' |
| 80 | +``` |
| 81 | + |
| 82 | +Then change the policy code to load it: |
| 83 | + |
| 84 | +```lua |
| 85 | +kumo.on('init', function() |
| 86 | + kumo.start_esmtp_listener { |
| 87 | + listen = '0.0.0.0:25', |
| 88 | + } |
| 89 | +end) |
| 90 | + |
| 91 | +kumo.on('smtp_server_get_dynamic_parameters', function(listener, conn_meta) |
| 92 | + return kumo.serde.toml_load '/opt/kumometa/etc/policy/listener_params.toml' |
| 93 | +end) |
| 94 | +``` |
| 95 | + |
| 96 | +Depending upon the size of the data you are loading, and especially if you |
| 97 | +choose to load data from an external service, you should consider using |
| 98 | +[kumo.memoize](../kumo/memoize.md) to cache the data. |
0 commit comments