| 1 |
--[[[ |
| 2 |
-- @module lua_maps_expressions |
| 3 |
-- This module contains routines to combine maps, selectors and expressions |
| 4 |
-- in a generic framework |
| 5 |
@example |
| 6 |
whitelist_ip_from = { |
| 7 |
rules { |
| 8 |
ip { |
| 9 |
selector = "ip"; |
| 10 |
map = "/path/to/whitelist_ip.map"; |
| 11 |
} |
| 12 |
from { |
| 13 |
selector = "from(smtp)"; |
| 14 |
map = "/path/to/whitelist_from.map"; |
| 15 |
} |
| 16 |
} |
| 17 |
expression = "ip & from"; |
| 18 |
} |
| 19 |
--]] |
| 20 |
|
| 21 |
--[[ |
| 22 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 23 |
|
| 24 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 |
you may not use this file except in compliance with the License. |
| 26 |
You may obtain a copy of the License at |
| 27 |
|
| 28 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 29 |
|
| 30 |
Unless required by applicable law or agreed to in writing, software |
| 31 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 32 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 |
See the License for the specific language governing permissions and |
| 34 |
limitations under the License. |
| 35 |
]]-- |
| 36 |
|
| 37 |
local lua_selectors = require "lua_selectors" |
| 38 |
local lua_maps = require "lua_maps" |
| 39 |
local rspamd_expression = require "rspamd_expression" |
| 40 |
local rspamd_logger = require "rspamd_logger" |
| 41 |
local fun = require "fun" |
| 42 |
local T = require "lua_shape.core" |
| 43 |
|
| 44 |
local exports = {} |
| 45 |
|
| 46 |
local function process_func(elt, task) |
| 47 |
local matched = {} |
| 48 |
local function process_atom(atom) |
| 49 |
local rule = elt.rules[atom] |
| 50 |
local res = 0 |
| 51 |
|
| 52 |
local function match_rule(val) |
| 53 |
local map_match = rule.map:get_key(val) |
| 54 |
if map_match then |
| 55 |
res = 1.0 |
| 56 |
matched[rule.name] = { |
| 57 |
matched = val, |
| 58 |
value = map_match |
| 59 |
} |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
local values = rule.selector(task) |
| 64 |
|
| 65 |
if values then |
| 66 |
if type(values) == 'table' then |
| 67 |
for _, val in ipairs(values) do |
| 68 |
if res == 0 then |
| 69 |
match_rule(val) |
| 70 |
end |
| 71 |
end |
| 72 |
else |
| 73 |
match_rule(values) |
| 74 |
end |
| 75 |
end |
| 76 |
|
| 77 |
return res |
| 78 |
end |
| 79 |
|
| 80 |
local res = elt.expr:process(process_atom) |
| 81 |
|
| 82 |
if res > 0 then |
| 83 |
return res, matched |
| 84 |
end |
| 85 |
|
| 86 |
return nil |
| 87 |
end |
| 88 |
|
| 89 |
-- Schema for maps expressions configuration |
| 90 |
-- Note: map field references lua_maps.map_schema which will be migrated separately |
| 91 |
-- Rules is a key-value table where each key is the rule name and value contains selector+map |
| 92 |
exports.schema = T.table({ |
| 93 |
expression = T.string(), |
| 94 |
rules = T.table({}, { |
| 95 |
extra = T.table({ |
| 96 |
selector = T.string(), |
| 97 |
map = lua_maps.map_schema, |
| 98 |
type = T.string():optional(), -- Optional map type override |
| 99 |
description = T.string():optional(), -- Optional description |
| 100 |
}), |
| 101 |
}), |
| 102 |
}) |
| 103 |
|
| 104 |
--[[[ |
| 105 |
-- @function lua_maps_expression.create(config, object, module_name) |
| 106 |
-- Creates a new maps combination from `object` for `module_name`. |
| 107 |
-- The input should be table with the following fields: |
| 108 |
-- |
| 109 |
-- * `rules` - kv map of rules where each rule has `map` and `selector` mandatory attribute, also `type` for map type, e.g. `regexp` |
| 110 |
-- * `expression` - Rspamd expression where elements are names from `rules` field, e.g. `ip & from` |
| 111 |
-- |
| 112 |
-- This function returns an object with public method `process(task)` that checks |
| 113 |
-- a task for the conditions defined in `expression` and `rules` and returns 2 values: |
| 114 |
-- |
| 115 |
-- 1. value returned by an expression (e.g. 1 or 0) |
| 116 |
-- 2. an map (rule_name -> table) of matches, where each element has the following fields: |
| 117 |
-- * `matched` - selector's value |
| 118 |
-- * `value` - map's result |
| 119 |
-- |
| 120 |
-- In case if `expression` is false a `nil` value is returned. |
| 121 |
-- @param {rspamd_config} cfg rspamd config |
| 122 |
-- @param {table} obj configuration table |
| 123 |
-- |
| 124 |
--]] |
| 125 |
local function create(cfg, obj, module_name) |
| 126 |
if not module_name then |
| 127 |
module_name = 'lua_maps_expressions' |
| 128 |
end |
| 129 |
|
| 130 |
if not obj or not obj.rules or not obj.expression then |
| 131 |
rspamd_logger.errx(cfg, 'cannot add maps combination for module %s: required elements are missing', |
| 132 |
module_name) |
| 133 |
return nil |
| 134 |
end |
| 135 |
|
| 136 |
local ret = { |
| 137 |
process = process_func, |
| 138 |
rules = {}, |
| 139 |
module_name = module_name |
| 140 |
} |
| 141 |
|
| 142 |
for name, rule in pairs(obj.rules) do |
| 143 |
local sel = lua_selectors.create_selector_closure(cfg, rule.selector) |
| 144 |
|
| 145 |
if not sel then |
| 146 |
rspamd_logger.errx(cfg, 'cannot add selector for element %s in module %s', |
| 147 |
name, module_name) |
| 148 |
end |
| 149 |
|
| 150 |
if not rule.type then |
| 151 |
-- Guess type |
| 152 |
if name:find('ip') or name:find('ipnet') then |
| 153 |
rule.type = 'radix' |
| 154 |
elseif name:find('regexp') or name:find('re_') then |
| 155 |
rule.type = 'regexp' |
| 156 |
elseif name:find('glob') then |
| 157 |
rule.type = 'regexp' |
| 158 |
else |
| 159 |
rule.type = 'set' |
| 160 |
end |
| 161 |
end |
| 162 |
local map = lua_maps.map_add_from_ucl(rule.map, rule.type, |
| 163 |
rule.description or obj.description or module_name) |
| 164 |
if not map then |
| 165 |
rspamd_logger.errx(cfg, 'cannot add map for element %s in module %s', |
| 166 |
name, module_name) |
| 167 |
end |
| 168 |
|
| 169 |
if sel and map then |
| 170 |
ret.rules[name] = { |
| 171 |
selector = sel, |
| 172 |
map = map, |
| 173 |
name = name, |
| 174 |
} |
| 175 |
else |
| 176 |
return nil |
| 177 |
end |
| 178 |
end |
| 179 |
|
| 180 |
-- Now process and parse expression |
| 181 |
local function parse_atom(str) |
| 182 |
local atom = table.concat(fun.totable(fun.take_while(function(c) |
| 183 |
if string.find(', \t()><+!|&\n', c, 1, true) then |
| 184 |
return false |
| 185 |
end |
| 186 |
return true |
| 187 |
end, fun.iter(str))), '') |
| 188 |
|
| 189 |
if ret.rules[atom] then |
| 190 |
return atom |
| 191 |
end |
| 192 |
|
| 193 |
rspamd_logger.errx(cfg, 'use of undefined element "%s" when parsing maps expression for %s', |
| 194 |
atom, module_name) |
| 195 |
|
| 196 |
return nil |
| 197 |
end |
| 198 |
local expr = rspamd_expression.create(obj.expression, parse_atom, |
| 199 |
rspamd_config:get_mempool()) |
| 200 |
|
| 201 |
if not expr then |
| 202 |
rspamd_logger.errx(cfg, 'cannot add map expression for module %s', |
| 203 |
module_name) |
| 204 |
return nil |
| 205 |
end |
| 206 |
|
| 207 |
ret.expr = expr |
| 208 |
|
| 209 |
if obj.symbol then |
| 210 |
rspamd_config:register_symbol { |
| 211 |
type = 'virtual,ghost', |
| 212 |
name = obj.symbol, |
| 213 |
score = 0.0, |
| 214 |
} |
| 215 |
end |
| 216 |
|
| 217 |
ret.symbol = obj.symbol |
| 218 |
|
| 219 |
return ret |
| 220 |
end |
| 221 |
|
| 222 |
exports.create = create |
| 223 |
|
| 224 |
return exports |