| 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 |
--[[[ |
| 18 |
-- @module lua_settings |
| 19 |
-- This module contains internal helpers for the settings infrastructure in Rspamd |
| 20 |
-- More details at https://rspamd.com/doc/configuration/settings.html |
| 21 |
--]] |
| 22 |
|
| 23 |
local exports = {} |
| 24 |
local known_ids = {} |
| 25 |
local post_init_added = false |
| 26 |
local post_init_performed = false |
| 27 |
local all_symbols |
| 28 |
local default_symbols |
| 29 |
|
| 30 |
local fun = require "fun" |
| 31 |
local lua_util = require "lua_util" |
| 32 |
local rspamd_logger = require "rspamd_logger" |
| 33 |
|
| 34 |
local function register_settings_cb(from_postload) |
| 35 |
if not post_init_performed then |
| 36 |
all_symbols = rspamd_config:get_symbols() |
| 37 |
|
| 38 |
default_symbols = fun.totable(fun.filter(function(_, v) |
| 39 |
return not v.allowed_ids or #v.allowed_ids == 0 or v.flags.explicit_disable |
| 40 |
end, all_symbols)) |
| 41 |
|
| 42 |
local explicit_symbols = lua_util.keys(fun.filter(function(k, v) |
| 43 |
return v.flags.explicit_disable |
| 44 |
end, all_symbols)) |
| 45 |
|
| 46 |
local symnames = lua_util.list_to_hash(lua_util.keys(all_symbols)) |
| 47 |
|
| 48 |
for _, set in pairs(known_ids) do |
| 49 |
local s = set.settings.apply or {} |
| 50 |
set.symbols = lua_util.shallowcopy(symnames) |
| 51 |
local enabled_symbols = {} |
| 52 |
local seen_enabled = false |
| 53 |
local disabled_symbols = {} |
| 54 |
local seen_disabled = false |
| 55 |
|
| 56 |
-- Enabled map |
| 57 |
if s.symbols_enabled then |
| 58 |
-- Remove all symbols from set.symbols aside of explicit_disable symbols |
| 59 |
set.symbols = lua_util.list_to_hash(explicit_symbols) |
| 60 |
seen_enabled = true |
| 61 |
for _, sym in ipairs(s.symbols_enabled) do |
| 62 |
enabled_symbols[sym] = true |
| 63 |
set.symbols[sym] = true |
| 64 |
end |
| 65 |
end |
| 66 |
if s.groups_enabled then |
| 67 |
seen_enabled = true |
| 68 |
for _, gr in ipairs(s.groups_enabled) do |
| 69 |
local syms = rspamd_config:get_group_symbols(gr) |
| 70 |
|
| 71 |
if syms then |
| 72 |
for _, sym in ipairs(syms) do |
| 73 |
enabled_symbols[sym] = true |
| 74 |
set.symbols[sym] = true |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
-- Disabled map |
| 81 |
if s.symbols_disabled then |
| 82 |
seen_disabled = true |
| 83 |
for _, sym in ipairs(s.symbols_disabled) do |
| 84 |
disabled_symbols[sym] = true |
| 85 |
set.symbols[sym] = false |
| 86 |
end |
| 87 |
end |
| 88 |
if s.groups_disabled then |
| 89 |
seen_disabled = true |
| 90 |
for _, gr in ipairs(s.groups_disabled) do |
| 91 |
local syms = rspamd_config:get_group_symbols(gr) |
| 92 |
|
| 93 |
if syms then |
| 94 |
for _, sym in ipairs(syms) do |
| 95 |
disabled_symbols[sym] = true |
| 96 |
set.symbols[sym] = false |
| 97 |
end |
| 98 |
end |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
-- Deal with complexity to avoid mess in C |
| 103 |
if not seen_enabled then |
| 104 |
enabled_symbols = nil |
| 105 |
end |
| 106 |
if not seen_disabled then |
| 107 |
disabled_symbols = nil |
| 108 |
end |
| 109 |
|
| 110 |
if enabled_symbols or disabled_symbols then |
| 111 |
-- Specify what symbols are really enabled for this settings id |
| 112 |
set.has_specific_symbols = true |
| 113 |
end |
| 114 |
|
| 115 |
rspamd_config:register_settings_id(set.name, enabled_symbols, disabled_symbols) |
| 116 |
|
| 117 |
-- Note: we do NOT remove groups_*/symbols_* here anymore, as the C++ runtime |
| 118 |
-- needs them for processing settings when they are applied to tasks. |
| 119 |
-- The preprocessing above builds the allowed/forbidden symbol lists for the |
| 120 |
-- settings element, but the runtime also needs to mark symbols as disabled/enabled. |
| 121 |
end |
| 122 |
|
| 123 |
-- We now iterate over all symbols and check for allowed_ids/forbidden_ids |
| 124 |
for k, v in pairs(all_symbols) do |
| 125 |
if v.allowed_ids and not v.flags.explicit_disable then |
| 126 |
for _, id in ipairs(v.allowed_ids) do |
| 127 |
if known_ids[id] then |
| 128 |
local set = known_ids[id] |
| 129 |
if not set.has_specific_symbols then |
| 130 |
set.has_specific_symbols = true |
| 131 |
end |
| 132 |
set.symbols[k] = true |
| 133 |
else |
| 134 |
rspamd_logger.errx(rspamd_config, 'symbol %s is allowed at unknown settings id %s', |
| 135 |
k, id) |
| 136 |
end |
| 137 |
end |
| 138 |
end |
| 139 |
if v.forbidden_ids then |
| 140 |
for _, id in ipairs(v.forbidden_ids) do |
| 141 |
if known_ids[id] then |
| 142 |
local set = known_ids[id] |
| 143 |
if not set.has_specific_symbols then |
| 144 |
set.has_specific_symbols = true |
| 145 |
end |
| 146 |
set.symbols[k] = false |
| 147 |
else |
| 148 |
rspamd_logger.errx(rspamd_config, 'symbol %s is denied at unknown settings id %s', |
| 149 |
k, id) |
| 150 |
end |
| 151 |
end |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
-- Now we create lists of symbols for each settings and digest |
| 156 |
for _, set in pairs(known_ids) do |
| 157 |
set.symbols = lua_util.keys(fun.filter(function(_, v) |
| 158 |
return v |
| 159 |
end, set.symbols)) |
| 160 |
table.sort(set.symbols) |
| 161 |
set.digest = lua_util.table_digest(set.symbols) |
| 162 |
end |
| 163 |
|
| 164 |
post_init_performed = true |
| 165 |
end |
| 166 |
end |
| 167 |
|
| 168 |
-- Returns numeric representation of the settings id |
| 169 |
local function numeric_settings_id(str) |
| 170 |
local cr = require "rspamd_cryptobox_hash" |
| 171 |
local util = require "rspamd_util" |
| 172 |
local ret = util.unpack("I4", |
| 173 |
cr.create_specific('xxh64'):update(str):bin()) |
| 174 |
|
| 175 |
return ret |
| 176 |
end |
| 177 |
|
| 178 |
exports.numeric_settings_id = numeric_settings_id |
| 179 |
|
| 180 |
-- Used to do the following: |
| 181 |
-- If there is a group of symbols_allowed, it checks if that is an array |
| 182 |
-- If that is a hash table then we transform it to a normal list, probably adding symbols to adjust scores |
| 183 |
local function transform_settings_maybe(settings, name) |
| 184 |
if settings.apply then |
| 185 |
local apply = settings.apply |
| 186 |
|
| 187 |
if apply.symbols_enabled then |
| 188 |
local senabled = apply.symbols_enabled |
| 189 |
|
| 190 |
if not senabled[1] then |
| 191 |
-- Transform map to a list |
| 192 |
local nlist = {} |
| 193 |
if not apply.scores then |
| 194 |
apply.scores = {} |
| 195 |
end |
| 196 |
for k, v in pairs(senabled) do |
| 197 |
if tonumber(v) then |
| 198 |
-- Move to symbols as well |
| 199 |
apply.scores[k] = tonumber(v) |
| 200 |
lua_util.debugm('settings', rspamd_config, |
| 201 |
'set symbol %s -> %s for settings %s', k, v, name) |
| 202 |
end |
| 203 |
nlist[#nlist + 1] = k |
| 204 |
end |
| 205 |
-- Convert |
| 206 |
apply.symbols_enabled = nlist |
| 207 |
end |
| 208 |
|
| 209 |
local symhash = lua_util.list_to_hash(apply.symbols_enabled) |
| 210 |
|
| 211 |
if apply.symbols then |
| 212 |
-- Check if added symbols are enabled |
| 213 |
for k, v in pairs(apply.symbols) do |
| 214 |
local s |
| 215 |
-- Check if we have ["sym1", "sym2" ...] or {"sym1": xx, "sym2": yy} |
| 216 |
if type(k) == 'string' then |
| 217 |
s = k |
| 218 |
else |
| 219 |
s = v |
| 220 |
end |
| 221 |
if not symhash[s] then |
| 222 |
lua_util.debugm('settings', rspamd_config, |
| 223 |
'added symbol %s to symbols_enabled for %s', s, name) |
| 224 |
apply.symbols_enabled[#apply.symbols_enabled + 1] = s |
| 225 |
end |
| 226 |
end |
| 227 |
end |
| 228 |
end |
| 229 |
end |
| 230 |
|
| 231 |
return settings |
| 232 |
end |
| 233 |
|
| 234 |
local function register_settings_id(str, settings, from_postload) |
| 235 |
local numeric_id = numeric_settings_id(str) |
| 236 |
|
| 237 |
if known_ids[numeric_id] then |
| 238 |
-- Might be either rewrite or a collision |
| 239 |
if known_ids[numeric_id].name ~= str then |
| 240 |
local logger = require "rspamd_logger" |
| 241 |
|
| 242 |
logger.errx(rspamd_config, 'settings ID clash! id %s maps to %s and conflicts with %s', |
| 243 |
numeric_id, known_ids[numeric_id].name, str) |
| 244 |
|
| 245 |
return nil |
| 246 |
end |
| 247 |
else |
| 248 |
known_ids[numeric_id] = { |
| 249 |
name = str, |
| 250 |
id = numeric_id, |
| 251 |
settings = transform_settings_maybe(settings, str), |
| 252 |
symbols = {} |
| 253 |
} |
| 254 |
end |
| 255 |
|
| 256 |
if not from_postload and not post_init_added then |
| 257 |
-- Use high priority to ensure that settings are initialised early but not before all |
| 258 |
-- plugins are loaded |
| 259 |
rspamd_config:add_post_init(function() |
| 260 |
register_settings_cb(true) |
| 261 |
end, 150) |
| 262 |
rspamd_config:add_config_unload(function() |
| 263 |
if post_init_added then |
| 264 |
known_ids = {} |
| 265 |
post_init_added = false |
| 266 |
end |
| 267 |
post_init_performed = false |
| 268 |
end) |
| 269 |
|
| 270 |
post_init_added = true |
| 271 |
end |
| 272 |
|
| 273 |
return numeric_id |
| 274 |
end |
| 275 |
|
| 276 |
exports.register_settings_id = register_settings_id |
| 277 |
|
| 278 |
local function settings_by_id(id) |
| 279 |
if not post_init_performed then |
| 280 |
register_settings_cb(false) |
| 281 |
end |
| 282 |
return known_ids[id] |
| 283 |
end |
| 284 |
|
| 285 |
exports.settings_by_id = settings_by_id |
| 286 |
exports.all_settings = function() |
| 287 |
if not post_init_performed then |
| 288 |
register_settings_cb(false) |
| 289 |
end |
| 290 |
return known_ids |
| 291 |
end |
| 292 |
exports.all_symbols = function() |
| 293 |
if not post_init_performed then |
| 294 |
register_settings_cb(false) |
| 295 |
end |
| 296 |
return all_symbols |
| 297 |
end |
| 298 |
-- What is enabled when no settings are there |
| 299 |
exports.default_symbols = function() |
| 300 |
if not post_init_performed then |
| 301 |
register_settings_cb(false) |
| 302 |
end |
| 303 |
return default_symbols |
| 304 |
end |
| 305 |
|
| 306 |
exports.load_all_settings = register_settings_cb |
| 307 |
|
| 308 |
return exports |