Debian Codegraph @

lualib/lua_cfg_transform.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local logger = require "rspamd_logger"
18 local lua_util = require "lua_util"
19 local rspamd_util = require "rspamd_util"
20
21 -- Converts surbl module config to rbl module
22 local function surbl_section_convert(cfg, section)
23 local rbl_section = cfg.rbl.rbls
24 local wl = section.whitelist
25 if section.rules then
26 for name, value in section.rules:pairs() do
27 if rbl_section[name] then
28 logger.warnx(rspamd_config, 'conflicting names in surbl and rbl rules: %s, prefer surbl rule!',
29 name)
30 end
31 local converted = {
32 urls = true,
33 ignore_defaults = true,
34 }
35
36 if wl then
37 converted.whitelist = wl
38 end
39
40 for k, v in value:pairs() do
41 local skip = false
42 -- Rename
43 if k == 'suffix' then
44 k = 'rbl'
45 end
46 if k == 'ips' then
47 k = 'returncodes'
48 end
49 if k == 'bits' then
50 k = 'returnbits'
51 end
52 if k == 'noip' then
53 k = 'no_ip'
54 end
55 -- Crappy legacy
56 if k == 'options' then
57 if v == 'noip' or v == 'no_ip' then
58 converted.no_ip = true
59 skip = true
60 end
61 end
62 if k:match('check_') then
63 local n = k:match('check_(.*)')
64 k = n
65 end
66
67 if k == 'dkim' and v then
68 converted.dkim_domainonly = false
69 converted.dkim_match_from = true
70 end
71
72 if k == 'emails' and v then
73 -- To match surbl behaviour
74 converted.emails_domainonly = true
75 end
76
77 if not skip then
78 converted[k] = lua_util.deepcopy(v)
79 end
80 end
81 rbl_section[name] = lua_util.override_defaults(rbl_section[name], converted)
82 end
83 end
84 end
85
86
87 -- Converts surbl module config to rbl module
88 local function emails_section_convert(cfg, section)
89 local rbl_section = cfg.rbl.rbls
90 local wl = section.whitelist
91 if section.rules then
92 for name, value in section.rules:pairs() do
93 if rbl_section[name] then
94 logger.warnx(rspamd_config, 'conflicting names in emails and rbl rules: %s, prefer emails rule!',
95 name)
96 end
97 local converted = {
98 emails = true,
99 ignore_defaults = true,
100 }
101
102 if wl then
103 converted.whitelist = wl
104 end
105
106 for k, v in value:pairs() do
107 local skip = false
108 -- Rename
109 if k == 'dnsbl' then
110 k = 'rbl'
111 end
112 if k == 'check_replyto' then
113 k = 'replyto'
114 end
115 if k == 'hashlen' then
116 k = 'hash_len'
117 end
118 if k == 'encoding' then
119 k = 'hash_format'
120 end
121 if k == 'domain_only' then
122 k = 'emails_domainonly'
123 end
124 if k == 'delimiter' then
125 k = 'emails_delimiter'
126 end
127 if k == 'skip_body' then
128 skip = true
129 if v then
130 -- Hack
131 converted.emails = false
132 converted.replyto = true
133 else
134 converted.emails = true
135 end
136 end
137 if k == 'expect_ip' then
138 -- Another stupid hack
139 if not converted.return_codes then
140 converted.returncodes = {}
141 end
142 local symbol = value.symbol or name
143 converted.returncodes[symbol] = { v }
144 skip = true
145 end
146
147 if not skip then
148 converted[k] = lua_util.deepcopy(v)
149 end
150 end
151 rbl_section[name] = lua_util.override_defaults(rbl_section[name], converted)
152 end
153 end
154 end
155
156 local function group_transform(cfg, k, v)
157 if v:type() ~= 'object' then
158 logger.warnx(rspamd_config, 'invalid group definition for %s: expected object', k)
159 return
160 end
161
162 if v:at('name') then
163 k = v:at('name'):unwrap()
164 end
165
166 local new_group = {
167 symbols = {}
168 }
169
170 if v:at('enabled') then
171 new_group.enabled = v:at('enabled'):unwrap()
172 end
173 if v:at('disabled') then
174 new_group.disabled = v:at('disabled'):unwrap()
175 end
176 if v:at('max_score') then
177 new_group.max_score = v:at('max_score'):unwrap()
178 end
179
180 local symbol_section = v:at('symbol')
181 if symbol_section and symbol_section:type() == 'object' then
182 for sk, sv in symbol_section:pairs() do
183 if sv:type() == 'object' and sv:at('name') then
184 sk = sv:at('name'):unwrap()
185 sv.name = nil -- Remove field
186 end
187
188 new_group.symbols[sk] = sv
189 end
190 end
191
192 if not cfg:at('group') then
193 cfg.group = {}
194 end
195
196 local cfg_group = cfg:at('group')
197 if cfg_group:at(k) and cfg_group:at(k):type() == 'object' then
198 cfg_group[k] = lua_util.override_defaults(cfg_group:at(k):unwrap(), new_group)
199 else
200 cfg_group[k] = new_group
201 end
202
203 logger.infox("overriding group %s from the legacy metric settings", k)
204 end
205
206 local function symbol_transform(cfg, k, v)
207 local groups = cfg:at('group')
208 if groups and groups:type() == 'object' then
209 -- first try to find any group where there is a definition of this symbol
210 for gr_n, gr in groups:pairs() do
211 if gr:type() == 'object' then
212 local symbols = gr:at('symbols')
213 if symbols and symbols:type() == 'object' and symbols:at(k) then
214 -- We override group symbol with ungrouped symbol
215 logger.infox("overriding group symbol %s in the group %s", k, gr_n)
216 symbols[k] = lua_util.override_defaults(symbols:at(k):unwrap(), v:unwrap())
217 return
218 end
219 end
220 end
221 end
222 -- Now check what Rspamd knows about this symbol
223 local sym = rspamd_config:get_symbol(k)
224
225 if groups and groups:type() == 'object' and (not sym or not sym.group) then
226 -- Otherwise we just use group 'ungrouped'
227 local ungrouped = groups:at('ungrouped')
228 if not ungrouped or ungrouped:type() ~= 'object' then
229 groups.ungrouped = {
230 symbols = {
231 [k] = v
232 }
233 }
234 else
235 local ug_symbols = ungrouped:at('symbols')
236 if not ug_symbols or ug_symbols:type() ~= 'object' then
237 ungrouped.symbols = { [k] = v }
238 else
239 ug_symbols[k] = v
240 end
241 end
242
243 logger.debugx("adding symbol %s to the group 'ungrouped'", k)
244 end
245 end
246
247 local function convert_metric(cfg, metric)
248 if metric:type() ~= 'object' then
249 logger.errx('invalid metric definition: %s', metric)
250 return
251 end
252
253 if metric:at('actions') then
254 local existing_actions = cfg:at('actions') and cfg:at('actions'):unwrap() or {}
255 cfg.actions = lua_util.override_defaults(existing_actions, metric:at('actions'):unwrap())
256 logger.infox("overriding actions from the legacy metric settings")
257 end
258 if metric:at('unknown_weight') then
259 logger.infox("overriding unknown weight from the legacy metric settings")
260 cfg:at('actions').unknown_weight = metric:at('unknown_weight'):unwrap()
261 end
262
263 if metric:at('subject') then
264 logger.infox("overriding subject from the legacy metric settings")
265 cfg:at('actions').subject = metric:at('subject'):unwrap()
266 end
267
268 if metric:at('group') then
269 for k, v in metric:at('group'):pairs() do
270 group_transform(cfg, k, v)
271 end
272 end
273
274 if metric:at('symbol') then
275 for k, v in metric:at('symbol'):pairs() do
276 symbol_transform(cfg, k, v)
277 end
278 end
279 end
280
281 -- Checks configuration files for statistics
282 local function check_statistics_sanity()
283 local local_conf = rspamd_paths['LOCAL_CONFDIR']
284 local local_stat = string.format('%s/local.d/%s', local_conf,
285 'statistic.conf')
286 local local_bayes = string.format('%s/local.d/%s', local_conf,
287 'classifier-bayes.conf')
288
289 if rspamd_util.file_exists(local_stat) and
290 rspamd_util.file_exists(local_bayes) then
291 logger.warnx(rspamd_config, 'conflicting files %s and %s are found: ' ..
292 'Rspamd classifier configuration might be broken!', local_stat, local_bayes)
293 end
294 end
295
296 -- Helper to run a transform function with error handling
297 local function try_transform(name, func)
298 local ok, err = pcall(func)
299 if not ok then
300 logger.errx(rspamd_config, 'config transform %s failed: %s', name, err)
301 return false
302 end
303 return true
304 end
305
306 return function(cfg)
307 local ret = false
308
309 if cfg:at('metric') then
310 try_transform('metric', function()
311 local metric = cfg:at('metric')
312
313 -- There are two things that we can have (old `metric_pairs` logic)
314 -- 1. A metric is a single metric definition like: metric { name = "default", ... }
315 -- 2. A metric is a list of metrics like: metric { "default": ... }
316 if metric:at('actions') or metric:at('name') then
317 convert_metric(cfg, metric)
318 else
319 for _, v in cfg:at('metric'):pairs() do
320 if v:type() == 'object' then
321 logger.infox('converting metric element %s', v)
322 convert_metric(cfg, v)
323 end
324 end
325 end
326 ret = true
327 end)
328 end
329
330 if cfg:at('symbols') then
331 try_transform('symbols', function()
332 for k, v in cfg:at('symbols'):pairs() do
333 symbol_transform(cfg, k, v)
334 end
335 end)
336 end
337
338 check_statistics_sanity()
339
340 if not cfg:at('actions') then
341 logger.errx('no actions defined')
342 else
343 try_transform('actions', function()
344 -- Perform sanity check for actions
345 local actions_defs = { 'no action', 'no_action', -- In case if that's added
346 'greylist', 'add header', 'add_header',
347 'rewrite subject', 'rewrite_subject', 'quarantine',
348 'reject', 'discard' }
349
350 local actions = cfg:at('actions')
351 if not actions:at('no action') and not actions:at('no_action') and
352 not actions:at('accept') then
353 for _, d in ipairs(actions_defs) do
354 if actions:at(d) then
355
356 local action_score
357 local act = actions:at(d)
358 if act:type() ~= 'object' then
359 action_score = act:unwrap()
360 elseif act:type() == 'object' and act:at('score') then
361 action_score = act:at('score'):unwrap()
362 end
363
364 if act:type() ~= 'object' and not action_score then
365 actions[d] = nil
366 elseif type(action_score) == 'number' and action_score < 0 then
367 actions['no_action'] = actions:at(d):unwrap() - 0.001
368 logger.infox(rspamd_config, 'set no_action score to: %s, as action %s has negative score',
369 actions:at('no_action'):unwrap(), d)
370 break
371 end
372 end
373 end
374 end
375
376 local actions_set = lua_util.list_to_hash(actions_defs)
377
378 -- Now check actions section for garbage
379 actions_set['unknown_weight'] = true
380 actions_set['grow_factor'] = true
381 actions_set['subject'] = true
382
383 for k, v in cfg:at('actions'):pairs() do
384 if not actions_set[k] then
385 -- Check if this is a custom action with flags (e.g., no_threshold)
386 local is_custom_action = false
387 if v and v:type() == 'object' and v:at('flags') then
388 is_custom_action = true
389 end
390 if not is_custom_action then
391 logger.warnx(rspamd_config, 'unknown element in actions section: %s', k)
392 end
393 end
394 end
395
396 -- Performs thresholds sanity
397 -- We exclude greylist here as it can be set to whatever threshold in practice
398 local actions_order = {
399 'no_action',
400 'add_header',
401 'rewrite_subject',
402 'quarantine',
403 'reject',
404 'discard'
405 }
406 for i = 1, (#actions_order - 1) do
407 local act = actions_order[i]
408
409 if actions:at(act) and actions:at(act):type() ~= 'object' then
410 local score = actions:at(act):unwrap()
411
412 for j = i + 1, #actions_order do
413 local next_act = actions_order[j]
414 if actions:at(next_act) and actions:at(next_act):type() == 'number' then
415 local next_score = actions:at(next_act):unwrap()
416 if type(score) == 'number' and type(next_score) == 'number' and next_score <= score then
417 logger.errx(rspamd_config, 'invalid actions thresholds order: action %s (%s) must have lower ' ..
418 'score than action %s (%s)', act, score, next_act, next_score)
419 ret = false
420 end
421 end
422 end
423 end
424 end
425 end)
426 end
427
428 -- DKIM signing/ARC legacy
429 try_transform('dkim_arc_legacy', function()
430 for _, mod in ipairs({ 'dkim_signing', 'arc' }) do
431 if cfg:at(mod) then
432 if cfg:at(mod):at('auth_only') then
433 if cfg:at(mod):at('sign_authenticated') then
434 logger.warnx(rspamd_config,
435 'both auth_only (%s) and sign_authenticated (%s) for %s are specified, prefer auth_only',
436 cfg:at(mod):at('auth_only'):unwrap(), cfg:at(mod):at('sign_authenticated'):unwrap(), mod)
437 end
438 cfg:at(mod).sign_authenticated = cfg:at(mod):at('auth_only')
439 end
440 end
441 end
442
443 -- Deal with dkim settings
444 if not cfg.dkim then
445 cfg.dkim = {}
446 else
447 if cfg.dkim.sign_condition then
448 -- We have an obsoleted sign condition, so we need to either add dkim_signing and move it
449 -- there or just move sign condition there...
450 if not cfg.dkim_signing then
451 logger.warnx('obsoleted DKIM signing method used, converting it to "dkim_signing" module')
452 cfg.dkim_signing = {
453 sign_condition = cfg.dkim.sign_condition
454 }
455 else
456 if not cfg.dkim_signing.sign_condition then
457 logger.warnx('obsoleted DKIM signing method used, move it to "dkim_signing" module')
458 cfg.dkim_signing.sign_condition = cfg.dkim.sign_condition
459 else
460 logger.warnx('obsoleted DKIM signing method used, ignore it as "dkim_signing" also defines condition!')
461 end
462 end
463 end
464 end
465 end)
466
467 -- Try to find some obvious issues with configuration
468 try_transform('config_sanity', function()
469 for k, v in cfg:pairs() do
470 if v:type() == 'object' and v:at(k) and v:at(k):type() == 'object' then
471 logger.errx('nested section: %s { %s { ... } }, it is likely a configuration error',
472 k, k)
473 end
474 end
475 end)
476
477 -- If neural network is enabled we MUST have `check_all_filters` flag
478 if cfg:at('neural') then
479 try_transform('neural', function()
480 if cfg:at('options') then
481 if not cfg:at('options'):at('check_all_filters') then
482 logger.infox(rspamd_config, 'enable `options.check_all_filters` for neural network')
483 cfg:at('options')['check_all_filters'] = true
484 end
485 end
486 end)
487 end
488
489 if cfg.surbl then
490 try_transform('surbl', function()
491 if not cfg.rbl then
492 cfg.rbl = {
493 rbls = {}
494 }
495 end
496 if not cfg.rbl.rbls then
497 cfg.rbl.rbls = {}
498 end
499 surbl_section_convert(cfg, cfg.surbl)
500 logger.infox(rspamd_config, 'converted surbl rules to rbl rules')
501 cfg.surbl = nil
502 end)
503 end
504
505 if cfg.emails then
506 try_transform('emails', function()
507 if not cfg.rbl then
508 cfg.rbl = {
509 rbls = {}
510 }
511 end
512 if not cfg.rbl.rbls then
513 cfg.rbl.rbls = {}
514 end
515 emails_section_convert(cfg, cfg.emails)
516 logger.infox(rspamd_config, 'converted emails rules to rbl rules')
517 cfg.emails = nil
518 end)
519 end
520
521 -- Common misprint options.upstreams -> options.upstream
522 if type(cfg.options) == 'table' and type(cfg.options.upstreams) == 'table' and not cfg.options.upstream then
523 cfg.options.upstream = cfg.options.upstreams
524 end
525
526 -- fuzzy_check: max_score -> hits_limit rename with backward compatibility
527 -- If max_score is specified (e.g., from local.d overrides), it should override hits_limit
528 if cfg.fuzzy_check then
529 try_transform('fuzzy_check', function()
530 local dominated_options = function(rule)
531 -- max_score overrides hits_limit for backward compatibility
532 -- This ensures local.d overrides using legacy max_score still work
533 if rule.max_score then
534 -- Use tonumber() to extract the value, avoiding UCL object reference issues
535 -- that can cause circular linked list pointers and infinite loops
536 rule.hits_limit = tonumber(rule.max_score) or rule.max_score
537 rule.max_score = nil
538 end
539 -- Also transform fuzzy_map entries
540 if rule.fuzzy_map then
541 for _, map_entry in pairs(rule.fuzzy_map) do
542 if type(map_entry) == 'table' and map_entry.max_score then
543 map_entry.hits_limit = tonumber(map_entry.max_score) or map_entry.max_score
544 map_entry.max_score = nil
545 end
546 end
547 end
548 end
549
550 if cfg.fuzzy_check.rule then
551 for _, rule in pairs(cfg.fuzzy_check.rule) do
552 if type(rule) == 'table' then
553 dominated_options(rule)
554 end
555 end
556 end
557 -- Handle case where fuzzy_check itself has these options (single rule syntax)
558 dominated_options(cfg.fuzzy_check)
559 end)
560 end
561
562 return ret, cfg
563 end