-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
simple_policy.lua
377 lines (333 loc) · 10.4 KB
/
simple_policy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
-- THIS IS NOT THE FILE YOU ARE LOOKING FOR!
-- This file is wez's local hacking/testing config.
-- You do not want to use this. It is not appropriate
-- for your production needs.
local kumo = require 'kumo'
package.path = 'assets/?.lua;' .. package.path
local shaping = require 'policy-extras.shaping'
local listener_domains = require 'policy-extras.listener_domains'
kumo.on('pre_init', function()
kumo.set_httpinject_recipient_rate_limit 'local:6,000/s'
kumo.set_httpinject_threads(math.ceil(kumo.available_parallelism() / 2))
kumo.set_readyq_threads(math.ceil(kumo.available_parallelism() / 2))
end)
kumo.on(
'get_listener_domain',
listener_domains:setup {
{
['auth-send.example.com'] = {
relay_from_authz = { 'scott' },
},
},
}
)
local shaper = shaping:setup_with_automation {
no_default_files = true,
extra_files = { 'assets/policy-extras/shaping.toml' },
}
local sources = require 'policy-extras.sources'
sources:setup {
{
pool = {
pool0 = {
source1 = { weight = 10 },
source2 = { weight = 20 },
source3 = { weight = 30 },
},
},
source = {
source1 = {},
source2 = {},
source3 = {},
},
},
}
local queue_module = require 'policy-extras.queue'
local queue_helper = queue_module:setup {
-- '/tmp/invalid/file.toml',
{
scheduling_header = 'X-Schedule',
tenant = {
mytenant = {
egress_pool = 'pool0',
},
},
queue = {
default = {
egress_pool = 'pool0',
-- refresh_interval = '2 hours',
strategy = 'SingletonTimerWheel',
retry_interval = '5m',
-- reap_interval = '10s',
},
},
},
}
local dkim_sign = require 'policy-extras.dkim_sign'
local dkim_signer = dkim_sign:setup {
{
base = {
selector = 'woot',
headers = { 'From', 'To', 'Subject' },
additional_signatures = { 'MyEsp' },
},
domain = {
['example.com'] = {
policy = 'Always',
filename = 'example-private-dkim-key.pem',
-- algo = 'sha256',
-- policy = "SignOnlyIfInDNS",
},
},
signature = {
MyEsp = {
domain = 'example.com',
policy = 'OnlyIfMissingDomainBlock',
filename = 'example-private-dkim-key.pem',
},
},
},
}
-- Called on startup to initialize the system
kumo.on('init', function()
kumo.set_config_monitor_globs {
'/home/wez/kumocorp/kumomta/**/*.{lua,toml}',
}
kumo.set_lua_gc_on_put(1)
kumo.configure_accounting_db_path(os.tmpname())
kumo.configure_bounce_classifier {
files = {
'/home/wez/kumocorp/kumomta/assets/community/bounces.toml',
'/home/wez/kumocorp/kumomta/assets/bounce_classifier/iana.toml',
},
}
-- Define a listener.
-- Can be used multiple times with different parameters to
-- define multiple listeners!
kumo.start_esmtp_listener {
listen = '0.0.0.0:2025',
-- Override the hostname reported in the banner and other
-- SMTP responses:
-- hostname="mail.example.com",
-- override the default set of relay hosts
relay_hosts = { '127.0.0.1', '192.168.1.0/24' },
-- Customize the banner.
-- The configured hostname will be automatically
-- prepended to this text.
banner = 'Welcome to KumoMTA!',
-- Unsafe! When set to true, don't save to spool
-- at reception time.
-- Saves IO but may cause you to lose messages
-- if something happens to this server before
-- the message is spooled.
-- deferred_spool = true,
-- max_recipients_per_message = 1024
-- max_messages_per_connection = 10000,
}
local do_logging = true
if do_logging then
kumo.configure_local_logs {
log_dir = '/var/tmp/kumo-logs',
max_segment_duration = '1s',
back_pressure = 512 * 1024,
}
end
kumo.start_http_listener {
listen = '0.0.0.0:8000',
-- allowed to access any http endpoint without additional auth
trusted_hosts = { '127.0.0.1', '::1', '192.168.1.0/24', '10.0.0.0/8' },
}
kumo.start_http_listener {
use_tls = true,
listen = '0.0.0.0:8001',
-- allowed to access any http endpoint without additional auth
trusted_hosts = { '127.0.0.1', '::1' },
}
-- Define the default "data" spool location; this is where
-- message bodies will be stored
--
-- 'flush' can be set to true to cause fdatasync to be
-- triggered after each store to the spool.
-- The increased durability comes at the cost of throughput.
--
-- kind can be 'LocalDisk' (currently the default) or 'RocksDB'.
--
-- LocalDisk stores one file per message in a filesystem hierarchy.
-- RocksDB is a key-value datastore.
--
-- RocksDB has >4x the throughput of LocalDisk, and enabling
-- flush has a marginal (<10%) impact in early testing.
kumo.define_spool {
name = 'data',
path = '/var/tmp/kumo-spool/data',
flush = false,
kind = 'RocksDB',
}
-- Define the default "meta" spool location; this is where
-- message envelope and metadata will be stored
kumo.define_spool {
name = 'meta',
path = '/var/tmp/kumo-spool/meta',
flush = false,
kind = 'RocksDB',
}
-- Use shared throttles rather than in-process throttles
-- kumo.configure_redis_throttles { node = 'redis://127.0.0.1/' }
end)
--[[
-- Called to validate the helo and/or ehlo domain
kumo.on('smtp_server_ehlo', function(domain)
-- print('ehlo domain is', domain)
-- Use kumo.reject to return an error to the EHLO command
-- kumo.reject(420, 'wooooo!')
end)
-- Called to validate the sender
kumo.on('smtp_server_mail_from', function(sender)
-- print('sender', tostring(sender))
-- kumo.reject(420, 'wooooo!')
end)
-- Called to validate a recipient
kumo.on('smtp_server_rcpt_to', function(rcpt)
-- print('rcpt', tostring(rcpt))
end)
]]
local function common_processing(msg)
local from_header = msg:from_header()
if not from_header then
kumo.reject(
552,
'5.6.0 DKIM signing requires a From header, but it is missing from this message'
)
end
-- local verify = msg:dkim_verify()
-- print('dkim', kumo.json_encode_pretty(verify))
-- msg:add_authentication_results(msg:get_meta 'hostname', verify)
-- print(msg:get_first_named_header_value 'Authentication-Results')
-- print(msg:get_data())
--[[
local failed = msg:check_fix_conformance(
-- check for and reject messages with these issues:
'MISSING_COLON_VALUE',
-- fix messages with these issues:
'LINE_TOO_LONG|NAME_ENDS_WITH_SPACE|NEEDS_TRANSFER_ENCODING|NON_CANONICAL_LINE_ENDINGS|MISSING_DATE_HEADER|MISSING_MESSAGE_ID_HEADER|MISSING_MIME_VERSION'
)
if failed then
kumo.reject(552, string.format('5.6.0 %s', failed))
end
]]
-- print('id', msg:id(), 'sender', tostring(msg:sender()))
-- print(msg:get_meta 'authn_id')
-- msg:set_meta('routing_domain', 'outlook.com')
-- Import scheduling information from X-Schedule and
-- then remove that header from the message
msg:import_scheduling_header('X-Schedule', true)
msg:set_meta('tenant', 't' .. tostring(math.random(1000)))
msg:set_meta('campaign', 'c' .. tostring(math.random(1000)))
local do_signing = true
if do_signing then
local signer = kumo.dkim.rsa_sha256_signer {
domain = msg:from_header().domain,
selector = 'default',
headers = { 'From', 'To', 'Subject' },
-- Using a file:
key = 'example-private-dkim-key.pem',
-- Using HashiCorp Vault:
--[[
key = {
vault_mount = "secret",
vault_path = "dkim/" .. msg:sender().domain
}
]]
}
msg:dkim_sign(signer)
end
-- msg:set_meta('queue', 'null')
-- set/get metadata fields
-- msg:set_meta('X-TestMSG', 'true')
-- print('meta X-TestMSG is', msg:get_meta 'X-TestMSG')
end
-- Called once the body has been received.
-- For multi-recipient mail, this is called for each recipient.
kumo.on('smtp_server_message_received', function(msg)
common_processing(msg)
end)
kumo.on('http_message_generated', function(msg)
common_processing(msg)
end)
-- Not the final form of this API, but this is currently how
-- we retrieve configuration used when making outbound
-- connections
kumo.on(
'get_egress_path_config',
function(routing_domain, egress_source, site_name)
if routing_domain == 'generator.kumomta.internal' then
return kumo.make_egress_path {
connection_limit = kumo.available_parallelism(),
refresh_strategy = 'Epoch',
max_ready = 80000,
}
end
local skip_make = true
local params = shaper.get_egress_path_config(
routing_domain,
egress_source,
site_name,
skip_make
)
-- print('get_egress_path_config', routing_domain, egress_source, site_name)
-- enable_tls = 'OpportunisticInsecure',
params.enable_tls = 'Disabled'
params.enable_mta_sts = false
-- max_message_rate = '5/min',
params.connection_limit = 300
-- max_connection_rate = '1/s',
params.max_ready = 80000
-- smtp_port = 2026,
-- max_deliveries_per_connection = 5,
-- hosts that we should consider to be poison because
-- they are a mail loop. The default for this is
-- { "127.0.0.0/8", "::1" }, but it is emptied out
-- in this config because we're using this to test
-- with fake domains that explicitly return loopback
-- addresses!
params.prohibited_hosts = {}
refresh_strategy = 'Epoch'
return kumo.make_egress_path(params)
end
)
-- A really simple inline auth "database" for very basic HTTP authentication
function simple_auth_check(user, password)
local password_database = {
['scott'] = 'tiger',
}
if password == '' then
return false
end
return password_database[user] == password
end
-- Consult a hypothetical sqlite database that has an auth table
-- with user and pass fields
function sqlite_auth_check(user, password)
local sqlite = require 'sqlite'
local db = sqlite.open '/tmp/auth.db'
local result = db:execute(
'select user from auth where user=? and pass=?',
user,
password
)
return result[1] == user
end
-- Use this to lookup and confirm a user/password credential
-- used with the http endpoint
kumo.on('http_server_validate_auth_basic', function(user, password)
return simple_auth_check(user, password)
-- or use sqlite
-- return sqlite_auth_check(user, password)
end)
-- Use this to lookup and confirm a user/password credential
-- when the client attempts SMTP AUTH PLAIN
kumo.on('smtp_server_auth_plain', function(authz, authc, password)
return simple_auth_check(authc, password)
-- or use sqlite
-- return sqlite_auth_check(authc, password)
end)