| 1 |
--[[ |
| 2 |
Copyright (c) 2016, Andrew Lewis <[email protected]> |
| 3 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 4 |
|
| 5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
you may not use this file except in compliance with the License. |
| 7 |
You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
]] -- |
| 17 |
|
| 18 |
local exports = {} |
| 19 |
|
| 20 |
local E = {} |
| 21 |
local lua_util = require "lua_util" |
| 22 |
local rspamd_util = require "rspamd_util" |
| 23 |
local logger = require "rspamd_logger" |
| 24 |
local fun = require "fun" |
| 25 |
|
| 26 |
local function check_violation(N, task, domain) |
| 27 |
-- Check for DKIM_REJECT |
| 28 |
local sym_check = 'R_DKIM_REJECT' |
| 29 |
|
| 30 |
if N == 'arc' then |
| 31 |
sym_check = 'ARC_REJECT' |
| 32 |
end |
| 33 |
if domain then |
| 34 |
domain = tostring(domain) |
| 35 |
end |
| 36 |
if task:has_symbol(sym_check) then |
| 37 |
local sym = task:get_symbol(sym_check)[1] |
| 38 |
logger.infox(task, 'skip signing for %s: violation %s found: %s', |
| 39 |
domain, sym_check, sym.options) |
| 40 |
return false |
| 41 |
end |
| 42 |
|
| 43 |
return true |
| 44 |
end |
| 45 |
|
| 46 |
local function insert_or_update_prop(N, task, p, prop, origin, data) |
| 47 |
if #p == 0 then |
| 48 |
local k = {} |
| 49 |
k[prop] = data |
| 50 |
table.insert(p, k) |
| 51 |
lua_util.debugm(N, task, 'add %s "%s" using %s', prop, data, origin) |
| 52 |
else |
| 53 |
for _, k in ipairs(p) do |
| 54 |
if not k[prop] then |
| 55 |
k[prop] = data |
| 56 |
lua_util.debugm(N, task, 'set %s to "%s" using %s', prop, data, origin) |
| 57 |
end |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
local function get_mempool_selectors(N, task) |
| 63 |
local p = {} |
| 64 |
local key_var = "dkim_key" |
| 65 |
local selector_var = "dkim_selector" |
| 66 |
if N == "arc" then |
| 67 |
key_var = "arc_key" |
| 68 |
selector_var = "arc_selector" |
| 69 |
end |
| 70 |
|
| 71 |
p.key = task:get_mempool():get_variable(key_var) |
| 72 |
p.selector = task:get_mempool():get_variable(selector_var) |
| 73 |
|
| 74 |
if (not p.key or not p.selector) then |
| 75 |
return false, {} |
| 76 |
end |
| 77 |
|
| 78 |
lua_util.debugm(N, task, 'override selector and key to %s:%s', p.key, p.selector) |
| 79 |
return true, p |
| 80 |
end |
| 81 |
|
| 82 |
local function parse_dkim_http_headers(N, task, settings) |
| 83 |
-- Configure headers |
| 84 |
local headers = { |
| 85 |
sign_header = settings.http_sign_header or "PerformDkimSign", |
| 86 |
sign_on_reject_header = settings.http_sign_on_reject_header_header or 'SignOnAuthFailed', |
| 87 |
domain_header = settings.http_domain_header or 'DkimDomain', |
| 88 |
selector_header = settings.http_selector_header or 'DkimSelector', |
| 89 |
key_header = settings.http_key_header or 'DkimPrivateKey' |
| 90 |
} |
| 91 |
|
| 92 |
if task:get_request_header(headers.sign_header) then |
| 93 |
local domain = task:get_request_header(headers.domain_header) |
| 94 |
local selector = task:get_request_header(headers.selector_header) |
| 95 |
local key = task:get_request_header(headers.key_header) |
| 96 |
|
| 97 |
if not (domain and selector and key) then |
| 98 |
logger.errx(task, 'missing required headers to sign email') |
| 99 |
return false, {} |
| 100 |
end |
| 101 |
|
| 102 |
domain = tostring(domain) |
| 103 |
selector = tostring(selector) |
| 104 |
key = tostring(key) |
| 105 |
|
| 106 |
-- Now check if we need to check the existing auth |
| 107 |
local hdr = task:get_request_header(headers.sign_on_reject_header) |
| 108 |
if not hdr or tostring(hdr) == '0' or tostring(hdr) == 'false' then |
| 109 |
if not check_violation(N, task, domain, selector) then |
| 110 |
return false, {} |
| 111 |
end |
| 112 |
end |
| 113 |
|
| 114 |
local p = {} |
| 115 |
local k = { |
| 116 |
domain = domain, |
| 117 |
rawkey = key, |
| 118 |
selector = selector, |
| 119 |
} |
| 120 |
table.insert(p, k) |
| 121 |
return true, p |
| 122 |
end |
| 123 |
|
| 124 |
lua_util.debugm(N, task, 'no sign header %s', headers.sign_header) |
| 125 |
return false, {} |
| 126 |
end |
| 127 |
|
| 128 |
local function prepare_dkim_signing(N, task, settings) |
| 129 |
local is_local, is_sign_networks, is_authed |
| 130 |
|
| 131 |
if settings.use_http_headers then |
| 132 |
local res, tbl = parse_dkim_http_headers(N, task, settings) |
| 133 |
|
| 134 |
if not res then |
| 135 |
if not settings.allow_headers_fallback then |
| 136 |
return res, {} |
| 137 |
else |
| 138 |
lua_util.debugm(N, task, 'failed to read http headers, fallback to normal schema') |
| 139 |
end |
| 140 |
else |
| 141 |
return res, tbl |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
if settings.sign_condition and type(settings.sign_condition) == 'function' then |
| 146 |
-- Use sign condition only |
| 147 |
local ret = settings.sign_condition(task) |
| 148 |
|
| 149 |
if not ret then |
| 150 |
return false, {} |
| 151 |
end |
| 152 |
|
| 153 |
if ret[1] then |
| 154 |
return true, ret |
| 155 |
else |
| 156 |
return true, { ret } |
| 157 |
end |
| 158 |
end |
| 159 |
|
| 160 |
local auser = task:get_user() |
| 161 |
local ip = task:get_from_ip() |
| 162 |
|
| 163 |
if ip and ip:is_local() then |
| 164 |
is_local = true |
| 165 |
end |
| 166 |
|
| 167 |
local has_pre_result = task:has_pre_result() |
| 168 |
if has_pre_result then |
| 169 |
local metric_action = task:get_metric_action() |
| 170 |
|
| 171 |
if metric_action == 'reject' or metric_action == 'drop' then |
| 172 |
-- No need to sign what we are already rejecting/dropping |
| 173 |
lua_util.debugm(N, task, 'task result is already %s, no need to sign', metric_action) |
| 174 |
return false, {} |
| 175 |
end |
| 176 |
|
| 177 |
if metric_action == 'soft reject' then |
| 178 |
-- Same here, we are going to delay an email, signing is just a waste of time |
| 179 |
lua_util.debugm(N, task, 'task result is %s, skip signing', metric_action) |
| 180 |
return false, {} |
| 181 |
end |
| 182 |
|
| 183 |
-- For spam actions, there is no clear distinction |
| 184 |
if metric_action ~= 'no action' and type(settings.skip_spam_sign) == 'boolean' and settings.skip_spam_sign then |
| 185 |
lua_util.debugm(N, task, 'task result is %s, no need to sign', metric_action) |
| 186 |
return false, {} |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
if settings.sign_authenticated and auser then |
| 191 |
lua_util.debugm(N, task, 'user is authenticated') |
| 192 |
is_authed = true |
| 193 |
elseif (settings.sign_networks and settings.sign_networks:get_key(ip)) then |
| 194 |
is_sign_networks = true |
| 195 |
lua_util.debugm(N, task, 'mail is from address in sign_networks') |
| 196 |
elseif settings.sign_local and is_local then |
| 197 |
lua_util.debugm(N, task, 'mail is from local address') |
| 198 |
elseif settings.sign_inbound and not is_local and not auser then |
| 199 |
lua_util.debugm(N, task, 'mail was sent to us') |
| 200 |
else |
| 201 |
lua_util.debugm(N, task, |
| 202 |
'mail is ineligible for signing: auser=%s, ip=%s, is_local=%s, ' .. |
| 203 |
'sign_authenticated=%s, sign_networks=%s, sign_local=%s, sign_inbound=%s', |
| 204 |
auser, tostring(ip), is_local, |
| 205 |
settings.sign_authenticated, settings.sign_networks ~= nil, |
| 206 |
settings.sign_local, settings.sign_inbound) |
| 207 |
return false, {} |
| 208 |
end |
| 209 |
|
| 210 |
local efrom = task:get_from('smtp') |
| 211 |
local empty_envelope = false |
| 212 |
if #(((efrom or E)[1] or E).addr or '') == 0 then |
| 213 |
if not settings.allow_envfrom_empty then |
| 214 |
lua_util.debugm(N, task, 'empty envelope from not allowed') |
| 215 |
return false, {} |
| 216 |
else |
| 217 |
empty_envelope = true |
| 218 |
end |
| 219 |
end |
| 220 |
|
| 221 |
local hfrom = task:get_from('mime') |
| 222 |
if not settings.allow_hdrfrom_multiple and (hfrom or E)[2] then |
| 223 |
lua_util.debugm(N, task, 'multiple header from not allowed') |
| 224 |
return false, {} |
| 225 |
end |
| 226 |
|
| 227 |
local eto = task:get_recipients(0) |
| 228 |
|
| 229 |
local dkim_domain |
| 230 |
local hdom = ((hfrom or E)[1] or E).domain |
| 231 |
local edom = ((efrom or E)[1] or E).domain |
| 232 |
local tdom = ((eto or E)[1] or E).domain |
| 233 |
local udom = string.match(auser or '', '.*@(.*)') |
| 234 |
|
| 235 |
local function get_dkim_domain(dtype) |
| 236 |
if settings[dtype] == 'header' then |
| 237 |
return hdom |
| 238 |
elseif settings[dtype] == 'envelope' then |
| 239 |
return edom |
| 240 |
elseif settings[dtype] == 'auth' then |
| 241 |
return udom |
| 242 |
elseif settings[dtype] == 'recipient' then |
| 243 |
return tdom |
| 244 |
else |
| 245 |
return settings[dtype]:lower() |
| 246 |
end |
| 247 |
end |
| 248 |
|
| 249 |
local function is_skip_sign() |
| 250 |
return not (settings.sign_networks and is_sign_networks) and |
| 251 |
not (settings.sign_authenticated and is_authed) and |
| 252 |
not (settings.sign_local and is_local) |
| 253 |
end |
| 254 |
|
| 255 |
if hdom then |
| 256 |
hdom = hdom:lower() |
| 257 |
end |
| 258 |
if edom then |
| 259 |
edom = edom:lower() |
| 260 |
end |
| 261 |
if udom then |
| 262 |
udom = udom:lower() |
| 263 |
end |
| 264 |
if tdom then |
| 265 |
tdom = tdom:lower() |
| 266 |
end |
| 267 |
|
| 268 |
if settings.signing_table and (settings.key_table or settings.use_vault) then |
| 269 |
-- OpenDKIM style |
| 270 |
if is_skip_sign() then |
| 271 |
lua_util.debugm(N, task, |
| 272 |
'skip signing: is_sign_network: %s, is_authed: %s, is_local: %s', |
| 273 |
is_sign_networks, is_authed, is_local) |
| 274 |
return false, {} |
| 275 |
end |
| 276 |
|
| 277 |
if not hfrom or not hfrom[1] or not hfrom[1].addr then |
| 278 |
lua_util.debugm(N, task, |
| 279 |
'signing_table: cannot get data when no header from is presented') |
| 280 |
return false, {} |
| 281 |
end |
| 282 |
local sign_entry = settings.signing_table:get_key(hfrom[1].addr:lower()) |
| 283 |
|
| 284 |
if sign_entry then |
| 285 |
-- Check opendkim style entries |
| 286 |
lua_util.debugm(N, task, |
| 287 |
'signing_table: found entry for %s: %s', hfrom[1].addr, sign_entry) |
| 288 |
if sign_entry == '%' then |
| 289 |
sign_entry = hdom |
| 290 |
end |
| 291 |
|
| 292 |
if settings.key_table then |
| 293 |
-- Now search in key table |
| 294 |
local key_entry = settings.key_table:get_key(sign_entry) |
| 295 |
|
| 296 |
if key_entry then |
| 297 |
local parts = lua_util.str_split(key_entry, ':') |
| 298 |
|
| 299 |
if #parts == 2 then |
| 300 |
-- domain + key |
| 301 |
local selector = settings.selector |
| 302 |
|
| 303 |
if not selector then |
| 304 |
logger.errx(task, 'no selector defined for sign_entry %s, key_entry %s', |
| 305 |
sign_entry, key_entry) |
| 306 |
return false, {} |
| 307 |
end |
| 308 |
|
| 309 |
local res = { |
| 310 |
selector = selector, |
| 311 |
domain = parts[1]:gsub('%%', hdom) |
| 312 |
} |
| 313 |
|
| 314 |
local st = parts[2]:sub(1, 2) |
| 315 |
|
| 316 |
if st:sub(1, 1) == '/' or st == './' or st == '..' then |
| 317 |
res.key = parts[2]:gsub('%%', hdom) |
| 318 |
lua_util.debugm(N, task, 'perform dkim signing for %s, selector=%s, domain=%s, key file=%s', |
| 319 |
hdom, selector, res.domain, res.key) |
| 320 |
else |
| 321 |
res.rawkey = parts[2] -- No sanity check here |
| 322 |
lua_util.debugm(N, task, 'perform dkim signing for %s, selector=%s, domain=%s, raw key used', |
| 323 |
hdom, selector, res.domain) |
| 324 |
end |
| 325 |
|
| 326 |
return true, { res } |
| 327 |
elseif #parts == 3 then |
| 328 |
-- domain, selector, key |
| 329 |
local selector = parts[2] |
| 330 |
|
| 331 |
local res = { |
| 332 |
selector = selector, |
| 333 |
domain = parts[1]:gsub('%%', hdom) |
| 334 |
} |
| 335 |
|
| 336 |
local st = parts[3]:sub(1, 2) |
| 337 |
|
| 338 |
if st:sub(1, 1) == '/' or st == './' or st == '..' then |
| 339 |
res.key = parts[3]:gsub('%%', hdom) |
| 340 |
lua_util.debugm(N, task, 'perform dkim signing for %s, selector=%s, domain=%s, key file=%s', |
| 341 |
hdom, selector, res.domain, res.key) |
| 342 |
else |
| 343 |
res.rawkey = parts[3] -- No sanity check here |
| 344 |
lua_util.debugm(N, task, 'perform dkim signing for %s, selector=%s, domain=%s, raw key used', |
| 345 |
hdom, selector, res.domain) |
| 346 |
end |
| 347 |
|
| 348 |
return true, { res } |
| 349 |
else |
| 350 |
logger.errx(task, 'invalid key entry for sign entry %s: %s; when signing %s domain', |
| 351 |
sign_entry, key_entry, hdom) |
| 352 |
return false, {} |
| 353 |
end |
| 354 |
elseif settings.use_vault then |
| 355 |
-- Sign table is presented, the rest is covered by vault |
| 356 |
lua_util.debugm(N, task, 'check vault for %s, by sign entry %s, key entry is missing', |
| 357 |
hdom, sign_entry) |
| 358 |
return true, { |
| 359 |
domain = sign_entry, |
| 360 |
vault = true |
| 361 |
} |
| 362 |
else |
| 363 |
logger.errx(task, 'missing key entry for sign entry %s; when signing %s domain', |
| 364 |
sign_entry, hdom) |
| 365 |
return false, {} |
| 366 |
end |
| 367 |
else |
| 368 |
logger.errx(task, 'cannot get key entry for signing entry %s, when signing %s domain', |
| 369 |
sign_entry, hdom) |
| 370 |
return false, {} |
| 371 |
end |
| 372 |
else |
| 373 |
lua_util.debugm(N, task, |
| 374 |
'signing_table: no entry for %s', hfrom[1].addr) |
| 375 |
return false, {} |
| 376 |
end |
| 377 |
else |
| 378 |
if settings.use_domain_sign_networks and is_sign_networks then |
| 379 |
dkim_domain = get_dkim_domain('use_domain_sign_networks') |
| 380 |
lua_util.debugm(N, task, |
| 381 |
'sign_networks: use domain(%s) for signature: %s', |
| 382 |
settings.use_domain_sign_networks, dkim_domain) |
| 383 |
elseif settings.use_domain_sign_local and is_local then |
| 384 |
dkim_domain = get_dkim_domain('use_domain_sign_local') |
| 385 |
lua_util.debugm(N, task, 'local: use domain(%s) for signature: %s', |
| 386 |
settings.use_domain_sign_local, dkim_domain) |
| 387 |
elseif settings.use_domain_sign_inbound and not is_local and not auser then |
| 388 |
dkim_domain = get_dkim_domain('use_domain_sign_inbound') |
| 389 |
lua_util.debugm(N, task, 'inbound: use domain(%s) for signature: %s', |
| 390 |
settings.use_domain_sign_inbound, dkim_domain) |
| 391 |
elseif settings.use_domain_custom then |
| 392 |
if type(settings.use_domain_custom) == 'string' then |
| 393 |
-- Load custom function |
| 394 |
local loadstring = loadstring or load |
| 395 |
local chunk, err = loadstring(settings.use_domain_custom) |
| 396 |
local ret, res_or_err |
| 397 |
if chunk then |
| 398 |
ret, res_or_err = pcall(chunk) |
| 399 |
else |
| 400 |
ret = false |
| 401 |
res_or_err = err |
| 402 |
end |
| 403 |
|
| 404 |
if ret then |
| 405 |
if type(res_or_err) == 'function' then |
| 406 |
settings.use_domain_custom = res_or_err |
| 407 |
dkim_domain = settings.use_domain_custom(task) |
| 408 |
lua_util.debugm(N, task, 'use custom domain for signing: %s', |
| 409 |
dkim_domain) |
| 410 |
else |
| 411 |
logger.errx(task, 'cannot load dkim domain custom script: invalid type: %s, expected function', |
| 412 |
type(res_or_err)) |
| 413 |
settings.use_domain_custom = nil |
| 414 |
end |
| 415 |
else |
| 416 |
logger.errx(task, 'cannot load dkim domain custom script: %s', res_or_err) |
| 417 |
settings.use_domain_custom = nil |
| 418 |
end |
| 419 |
else |
| 420 |
dkim_domain = settings.use_domain_custom(task) |
| 421 |
lua_util.debugm(N, task, 'use custom domain for signing: %s', |
| 422 |
dkim_domain) |
| 423 |
end |
| 424 |
else |
| 425 |
dkim_domain = get_dkim_domain('use_domain') |
| 426 |
lua_util.debugm(N, task, 'use domain(%s) for signature: %s', |
| 427 |
settings.use_domain, dkim_domain) |
| 428 |
end |
| 429 |
end |
| 430 |
|
| 431 |
if not dkim_domain then |
| 432 |
lua_util.debugm(N, task, 'could not extract dkim domain') |
| 433 |
return false, {} |
| 434 |
end |
| 435 |
|
| 436 |
if settings.use_esld then |
| 437 |
dkim_domain = rspamd_util.get_tld(dkim_domain) |
| 438 |
if hdom then |
| 439 |
hdom = rspamd_util.get_tld(hdom) |
| 440 |
end |
| 441 |
if edom then |
| 442 |
edom = rspamd_util.get_tld(edom) |
| 443 |
end |
| 444 |
end |
| 445 |
|
| 446 |
lua_util.debugm(N, task, 'final DKIM domain: %s', dkim_domain) |
| 447 |
|
| 448 |
-- Sanity checks |
| 449 |
if edom and hdom and not settings.allow_hdrfrom_mismatch and hdom ~= edom then |
| 450 |
if settings.allow_hdrfrom_mismatch_local and is_local then |
| 451 |
lua_util.debugm(N, task, 'domain mismatch allowed for local IP: %1 != %2', hdom, edom) |
| 452 |
elseif settings.allow_hdrfrom_mismatch_sign_networks and is_sign_networks then |
| 453 |
lua_util.debugm(N, task, 'domain mismatch allowed for sign_networks: %1 != %2', hdom, edom) |
| 454 |
else |
| 455 |
if empty_envelope and hdom then |
| 456 |
lua_util.debugm(N, task, 'domain mismatch allowed for empty envelope: %1 != %2', hdom, edom) |
| 457 |
else |
| 458 |
lua_util.debugm(N, task, 'domain mismatch not allowed: %1 != %2', hdom, edom) |
| 459 |
return false, {} |
| 460 |
end |
| 461 |
end |
| 462 |
end |
| 463 |
|
| 464 |
if auser and not settings.allow_username_mismatch then |
| 465 |
if not udom then |
| 466 |
lua_util.debugm(N, task, 'couldnt find domain in username') |
| 467 |
return false, {} |
| 468 |
end |
| 469 |
if settings.use_esld then |
| 470 |
udom = rspamd_util.get_tld(udom) |
| 471 |
end |
| 472 |
if udom ~= dkim_domain then |
| 473 |
lua_util.debugm(N, task, 'user domain mismatch') |
| 474 |
return false, {} |
| 475 |
end |
| 476 |
end |
| 477 |
|
| 478 |
local p = {} |
| 479 |
|
| 480 |
if settings.use_vault then |
| 481 |
if settings.vault_domains then |
| 482 |
if settings.vault_domains:get_key(dkim_domain) then |
| 483 |
table.insert(p, { |
| 484 |
domain = dkim_domain, |
| 485 |
vault = true, |
| 486 |
}) |
| 487 |
else |
| 488 |
lua_util.debugm(N, task, 'domain %s is not designated for vault', |
| 489 |
dkim_domain) |
| 490 |
end |
| 491 |
else |
| 492 |
-- TODO: try every domain in the vault |
| 493 |
table.insert(p, { |
| 494 |
domain = dkim_domain, |
| 495 |
vault = true, |
| 496 |
}) |
| 497 |
end |
| 498 |
end |
| 499 |
|
| 500 |
if settings.domain[dkim_domain] then |
| 501 |
-- support old style selector/paths |
| 502 |
if settings.domain[dkim_domain].selector or |
| 503 |
settings.domain[dkim_domain].path then |
| 504 |
local k = {} |
| 505 |
k.selector = settings.domain[dkim_domain].selector |
| 506 |
k.key = settings.domain[dkim_domain].path |
| 507 |
table.insert(p, k) |
| 508 |
end |
| 509 |
for _, s in ipairs((settings.domain[dkim_domain].selectors or {})) do |
| 510 |
lua_util.debugm(N, task, 'adding selector: %1', s) |
| 511 |
local k = {} |
| 512 |
k.selector = s.selector |
| 513 |
k.key = s.path |
| 514 |
table.insert(p, k) |
| 515 |
end |
| 516 |
end |
| 517 |
|
| 518 |
if #p == 0 then |
| 519 |
local ret, k = get_mempool_selectors(N, task) |
| 520 |
if ret then |
| 521 |
table.insert(p, k) |
| 522 |
lua_util.debugm(N, task, 'using mempool selector %s with key %s', |
| 523 |
k.selector, k.key) |
| 524 |
end |
| 525 |
end |
| 526 |
|
| 527 |
if settings.selector_map then |
| 528 |
local data = settings.selector_map:get_key(dkim_domain) |
| 529 |
if data then |
| 530 |
insert_or_update_prop(N, task, p, 'selector', 'selector_map', data) |
| 531 |
else |
| 532 |
lua_util.debugm(N, task, 'no selector in map for %s', dkim_domain) |
| 533 |
end |
| 534 |
end |
| 535 |
|
| 536 |
if settings.path_map then |
| 537 |
local data = settings.path_map:get_key(dkim_domain) |
| 538 |
if data then |
| 539 |
insert_or_update_prop(N, task, p, 'key', 'path_map', data) |
| 540 |
else |
| 541 |
lua_util.debugm(N, task, 'no key in map for %s', dkim_domain) |
| 542 |
end |
| 543 |
end |
| 544 |
|
| 545 |
if #p == 0 and not settings.try_fallback then |
| 546 |
lua_util.debugm(N, task, 'dkim unconfigured and fallback disabled') |
| 547 |
return false, {} |
| 548 |
end |
| 549 |
|
| 550 |
if not settings.use_redis then |
| 551 |
insert_or_update_prop(N, task, p, 'key', |
| 552 |
'default path', settings.path) |
| 553 |
end |
| 554 |
|
| 555 |
insert_or_update_prop(N, task, p, 'selector', |
| 556 |
'default selector', settings.selector) |
| 557 |
|
| 558 |
if settings.check_violation then |
| 559 |
if not check_violation(N, task, p.domain) then |
| 560 |
return false, {} |
| 561 |
end |
| 562 |
end |
| 563 |
|
| 564 |
insert_or_update_prop(N, task, p, 'domain', 'dkim_domain', |
| 565 |
dkim_domain) |
| 566 |
|
| 567 |
return #p > 0 and true or false, p |
| 568 |
end |
| 569 |
|
| 570 |
exports.prepare_dkim_signing = prepare_dkim_signing |
| 571 |
|
| 572 |
exports.sign_using_redis = function(N, task, settings, selectors, sign_func, err_func) |
| 573 |
local lua_redis = require "lua_redis" |
| 574 |
|
| 575 |
local function try_redis_key(selector, p) |
| 576 |
p.key = nil |
| 577 |
p.selector = selector |
| 578 |
local rk = string.format('%s.%s', p.selector, p.domain) |
| 579 |
local function redis_key_cb(err, data) |
| 580 |
if err then |
| 581 |
err_func(string.format("cannot make request to load DKIM key for %s: %s", |
| 582 |
rk, err)) |
| 583 |
elseif type(data) ~= 'string' then |
| 584 |
lua_util.debugm(N, task, "missing DKIM key for %s", rk) |
| 585 |
else |
| 586 |
p.rawkey = data |
| 587 |
lua_util.debugm(N, task, 'found and parsed key for %s:%s in Redis', |
| 588 |
p.domain, p.selector) |
| 589 |
sign_func(task, p) |
| 590 |
end |
| 591 |
end |
| 592 |
local rret = lua_redis.redis_make_request(task, |
| 593 |
settings.redis_params, -- connect params |
| 594 |
rk, -- hash key |
| 595 |
false, -- is write |
| 596 |
redis_key_cb, --callback |
| 597 |
'HGET', -- command |
| 598 |
{ settings.key_prefix, rk } -- arguments |
| 599 |
) |
| 600 |
if not rret then |
| 601 |
err_func(task, |
| 602 |
string.format("cannot make request to load DKIM key for %s", rk)) |
| 603 |
end |
| 604 |
end |
| 605 |
|
| 606 |
for _, p in ipairs(selectors) do |
| 607 |
if settings.selector_prefix then |
| 608 |
logger.infox(task, "using selector prefix '%s' for domain '%s'", |
| 609 |
settings.selector_prefix, p.domain); |
| 610 |
local function redis_selector_cb(err, data) |
| 611 |
if err or type(data) ~= 'string' then |
| 612 |
err_func(task, string.format("cannot make request to load DKIM selector for domain %s: %s", |
| 613 |
p.domain, err)) |
| 614 |
else |
| 615 |
try_redis_key(data, p) |
| 616 |
end |
| 617 |
end |
| 618 |
local rret = lua_redis.redis_make_request(task, |
| 619 |
settings.redis_params, -- connect params |
| 620 |
p.domain, -- hash key |
| 621 |
false, -- is write |
| 622 |
redis_selector_cb, --callback |
| 623 |
'HGET', -- command |
| 624 |
{ settings.selector_prefix, p.domain } -- arguments |
| 625 |
) |
| 626 |
if not rret then |
| 627 |
err_func(task, string.format("cannot make Redis request to load DKIM selector for domain %s", |
| 628 |
p.domain)) |
| 629 |
end |
| 630 |
else |
| 631 |
try_redis_key(p.selector, p) |
| 632 |
end |
| 633 |
end |
| 634 |
end |
| 635 |
|
| 636 |
exports.sign_using_vault = function(N, task, settings, selector, sign_func, err_func) |
| 637 |
local http = require "rspamd_http" |
| 638 |
local ucl = require "ucl" |
| 639 |
|
| 640 |
local vault_path = settings.vault_path or 'dkim' |
| 641 |
local vault_kv_version = settings.vault_kv_version or 1 |
| 642 |
|
| 643 |
-- For KV v2, we need to add 'data' to the path for read operations |
| 644 |
if vault_kv_version == 2 then |
| 645 |
local mount_point = vault_path:match('^([^/]+)') |
| 646 |
local subpath = vault_path:match('^[^/]+/?(.*)') |
| 647 |
if subpath and subpath ~= '' then |
| 648 |
vault_path = mount_point .. '/data/' .. subpath |
| 649 |
else |
| 650 |
vault_path = mount_point .. '/data' |
| 651 |
end |
| 652 |
end |
| 653 |
|
| 654 |
local full_url = string.format('%s/v1/%s/%s', |
| 655 |
settings.vault_url, vault_path, selector.domain) |
| 656 |
local upstream_list = lua_util.http_upstreams_by_url(rspamd_config:get_mempool(), settings.vault_url) |
| 657 |
|
| 658 |
local function vault_callback(err, code, body, _) |
| 659 |
if code ~= 200 then |
| 660 |
err_func(task, string.format('cannot request data from the vault url: %s; %s (%s)', |
| 661 |
full_url, err, body)) |
| 662 |
else |
| 663 |
local parser = ucl.parser() |
| 664 |
local res, parser_err = parser:parse_string(body) |
| 665 |
if not res then |
| 666 |
err_func(task, string.format('vault reply for %s (data=%s) cannot be parsed: %s', |
| 667 |
full_url, body, parser_err)) |
| 668 |
else |
| 669 |
local obj = parser:get_object() |
| 670 |
|
| 671 |
if not obj or not obj.data then |
| 672 |
err_func(task, string.format('vault reply for %s (data=%s) is invalid, no data', |
| 673 |
full_url, body)) |
| 674 |
else |
| 675 |
-- For KV v2, data is nested under obj.data.data |
| 676 |
-- For KV v1, data is under obj.data |
| 677 |
local vault_data = vault_kv_version == 2 and obj.data.data or obj.data |
| 678 |
local elts = vault_data and vault_data.selectors or {} |
| 679 |
local errs = {} |
| 680 |
local nvalid = 0 |
| 681 |
|
| 682 |
-- Filter selectors by time/sanity |
| 683 |
local function is_selector_valid(p) |
| 684 |
if not p.key or not p.selector then |
| 685 |
table.insert(errs, { "missing key/selector", p }) |
| 686 |
return false |
| 687 |
end |
| 688 |
|
| 689 |
if p.valid_start then |
| 690 |
-- Check start time |
| 691 |
if rspamd_util.get_time() < tonumber(p.valid_start) then |
| 692 |
table.insert(errs, { "start time is in the future", p }) |
| 693 |
return false |
| 694 |
end |
| 695 |
end |
| 696 |
|
| 697 |
if p.valid_end then |
| 698 |
if rspamd_util.get_time() >= tonumber(p.valid_end) then |
| 699 |
table.insert(errs, { "end time is in the past", p }) |
| 700 |
return false |
| 701 |
end |
| 702 |
end |
| 703 |
|
| 704 |
return true |
| 705 |
end |
| 706 |
fun.each(function(p) |
| 707 |
local dkim_sign_data = { |
| 708 |
rawkey = p.key, |
| 709 |
selector = p.selector, |
| 710 |
domain = p.domain or selector.domain, |
| 711 |
alg = p.alg, |
| 712 |
} |
| 713 |
lua_util.debugm(N, task, 'found and parsed key for %s:%s in Vault', |
| 714 |
dkim_sign_data.domain, dkim_sign_data.selector) |
| 715 |
nvalid = nvalid + 1 |
| 716 |
sign_func(task, dkim_sign_data) |
| 717 |
end, fun.filter(is_selector_valid, elts)) |
| 718 |
for _, e in ipairs(errs) do |
| 719 |
lua_util.debugm(N, task, 'error found during processing Vault selectors: %s:%s', |
| 720 |
e[1], e[2]) |
| 721 |
end |
| 722 |
|
| 723 |
if nvalid == 0 then |
| 724 |
lua_util.debugm(N, task, 'no valid selectors have been returned from the Vault, skip signing') |
| 725 |
end |
| 726 |
end |
| 727 |
end |
| 728 |
end |
| 729 |
end |
| 730 |
|
| 731 |
local ret = http.request { |
| 732 |
task = task, |
| 733 |
url = full_url, |
| 734 |
callback = vault_callback, |
| 735 |
timeout = settings.http_timeout or 5.0, |
| 736 |
no_ssl_verify = settings.no_ssl_verify, |
| 737 |
keepalive = true, |
| 738 |
upstream = upstream_list and upstream_list:get_upstream_round_robin() or nil, |
| 739 |
headers = { |
| 740 |
['X-Vault-Token'] = settings.vault_token, |
| 741 |
}, |
| 742 |
} |
| 743 |
|
| 744 |
if not ret then |
| 745 |
err_func(task, string.format("cannot make HTTP request to load DKIM data domain %s", |
| 746 |
selector.domain)) |
| 747 |
end |
| 748 |
end |
| 749 |
|
| 750 |
exports.validate_signing_settings = function(settings) |
| 751 |
return settings.use_redis or |
| 752 |
settings.path or |
| 753 |
settings.domain or |
| 754 |
settings.path_map or |
| 755 |
settings.selector_map or |
| 756 |
settings.use_http_headers or |
| 757 |
(settings.signing_table and settings.key_table) or |
| 758 |
(settings.use_vault and settings.vault_url and settings.vault_token) or |
| 759 |
settings.sign_condition |
| 760 |
end |
| 761 |
|
| 762 |
exports.process_signing_settings = function(N, settings, opts) |
| 763 |
local lua_maps = require "lua_maps" |
| 764 |
-- Used to convert plain options to the maps |
| 765 |
local maps_opts = { |
| 766 |
sign_networks = { 'radix', 'DKIM signing networks' }, |
| 767 |
path_map = { 'map', 'Paths to DKIM signing keys' }, |
| 768 |
selector_map = { 'map', 'DKIM selectors' }, |
| 769 |
signing_table = { 'glob', 'DKIM signing table' }, |
| 770 |
key_table = { 'glob', 'DKIM keys table' }, |
| 771 |
vault_domains = { 'glob', 'DKIM signing domains in vault' } |
| 772 |
} |
| 773 |
for k, v in pairs(opts) do |
| 774 |
local maybe_map = maps_opts[k] |
| 775 |
if maybe_map then |
| 776 |
settings[k] = lua_maps.map_add_from_ucl(v, maybe_map[1], maybe_map[2]) |
| 777 |
elseif k == 'sign_condition' then |
| 778 |
local ret, f = lua_util.callback_from_string(v) |
| 779 |
if ret then |
| 780 |
settings[k] = f |
| 781 |
else |
| 782 |
logger.errx(rspamd_config, 'cannot load sign condition %s: %s', v, f) |
| 783 |
end |
| 784 |
else |
| 785 |
settings[k] = v |
| 786 |
end |
| 787 |
end |
| 788 |
end |
| 789 |
|
| 790 |
return exports |