Debian Codegraph @

lualib/rspamadm/dns_tool.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
18 local argparse = require "argparse"
19 local rspamd_logger = require "rspamd_logger"
20 local ansicolors = require "ansicolors"
21 local bit = require "bit"
22
23 local parser = argparse()
24 :name "rspamadm dnstool"
25 :description "DNS tools provided by Rspamd"
26 :help_description_margin(30)
27 :command_target("command")
28 :require_command(true)
29
30 parser:option "-c --config"
31 :description "Path to config file"
32 :argname("<cfg>")
33 :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
34
35 local spf = parser:command "spf"
36 :description "Extracts spf records"
37 spf:mutex(
38 spf:option "-d --domain"
39 :description "Domain to use"
40 :argname("<domain>"),
41 spf:option "-f --from"
42 :description "SMTP from to use"
43 :argname("<from>")
44 )
45
46 spf:option "-i --ip"
47 :description "Source IP address to use"
48 :argname("<ip>")
49 spf:flag "-a --all"
50 :description "Print all records"
51
52 local spf_flatten = parser:command "spf-flatten"
53 :description "Flattens SPF records by resolving all includes and optimizing"
54 spf_flatten:argument "domain"
55 :description "Domain to flatten SPF for"
56 :argname("<domain>")
57 spf_flatten:option "-f --format"
58 :description "Output format: default, json, compact"
59 :argname("<format>")
60 :default("default")
61
62 local function printf(fmt, ...)
63 if fmt then
64 io.write(string.format(fmt, ...))
65 end
66 io.write('\n')
67 end
68
69 local function highlight(str)
70 return ansicolors.white .. str .. ansicolors.reset
71 end
72
73 local function green(str)
74 return ansicolors.green .. str .. ansicolors.reset
75 end
76
77 local function red(str)
78 return ansicolors.red .. str .. ansicolors.reset
79 end
80
81 local function load_config(opts)
82 local _r, err = rspamd_config:load_ucl(opts['config'])
83
84 if not _r then
85 rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
86 os.exit(1)
87 end
88
89 _r, err = rspamd_config:parse_rcl({ 'logging', 'worker' })
90 if not _r then
91 rspamd_logger.errx('cannot process %s: %s', opts['config'], err)
92 os.exit(1)
93 end
94 end
95
96 local function spf_handler(opts)
97 local rspamd_spf = require "rspamd_spf"
98 local rspamd_task = require "rspamd_task"
99 local rspamd_ip = require "rspamd_ip"
100
101 local task = rspamd_task.create(rspamd_config, rspamadm_ev_base)
102 task:set_session(rspamadm_session)
103 task:set_resolver(rspamadm_dns_resolver)
104
105 if opts.ip then
106 opts.ip = rspamd_ip.fromstring(opts.ip)
107 task:set_from_ip(opts.ip)
108 else
109 opts.all = true
110 end
111
112 if opts.from then
113 local rspamd_parsers = require "rspamd_parsers"
114 local addr_parsed = rspamd_parsers.parse_mail_address(opts.from)
115 if addr_parsed then
116 task:set_from('smtp', addr_parsed[1])
117 else
118 io.stderr:write('Invalid from addr\n')
119 os.exit(1)
120 end
121 elseif opts.domain then
122 task:set_from('smtp', { user = 'user', domain = opts.domain })
123 else
124 io.stderr:write('Neither domain nor from specified\n')
125 os.exit(1)
126 end
127
128 local function flag_to_str(fl)
129 if bit.band(fl, rspamd_spf.flags.temp_fail) ~= 0 then
130 return "temporary failure"
131 elseif bit.band(fl, rspamd_spf.flags.perm_fail) ~= 0 then
132 return "permanent failure"
133 elseif bit.band(fl, rspamd_spf.flags.na) ~= 0 then
134 return "no spf record"
135 end
136
137 return "unknown flag: " .. tostring(fl)
138 end
139
140 local function display_spf_results(elt, colored)
141 local dec = function(e)
142 return e
143 end
144 local policy_decode = function(e)
145 if e == rspamd_spf.policy.fail then
146 return 'reject'
147 elseif e == rspamd_spf.policy.pass then
148 return 'pass'
149 elseif e == rspamd_spf.policy.soft_fail then
150 return 'soft fail'
151 elseif e == rspamd_spf.policy.neutral then
152 return 'neutral'
153 end
154
155 return 'unknown'
156 end
157
158 if colored then
159 dec = function(e)
160 return highlight(e)
161 end
162
163 if elt.result == rspamd_spf.policy.pass then
164 dec = function(e)
165 return green(e)
166 end
167 elseif elt.result == rspamd_spf.policy.fail then
168 dec = function(e)
169 return red(e)
170 end
171 end
172
173 end
174 printf('%s: %s', highlight('Policy'), dec(policy_decode(elt.result)))
175 printf('%s: %s', highlight('Network'), dec(elt.addr))
176
177 if elt.str then
178 printf('%s: %s', highlight('Original'), elt.str)
179 end
180 end
181
182 local function cb(record, flags, err)
183 if record then
184 local result, flag_or_policy, error_or_addr
185 if opts.ip then
186 result, flag_or_policy, error_or_addr = record:check_ip(opts.ip)
187 elseif opts.all then
188 result = true
189 end
190 if opts.ip and not opts.all then
191 if result then
192 display_spf_results(error_or_addr, true)
193 else
194 printf('Not matched: %s', error_or_addr)
195 end
196
197 os.exit(0)
198 end
199
200 if result then
201 printf('SPF record for %s; digest: %s',
202 highlight(opts.domain or opts.from), highlight(record:get_digest()))
203 for _, elt in ipairs(record:get_elts()) do
204 if result and error_or_addr and elt.str and elt.str == error_or_addr.str then
205 printf("%s", highlight('*** Matched ***'))
206 display_spf_results(elt, true)
207 printf('------')
208 else
209 display_spf_results(elt, false)
210 printf('------')
211 end
212 end
213 else
214 printf('Error getting SPF record: %s (%s flag)', err,
215 flag_to_str(flag_or_policy or flags))
216 end
217 else
218 printf('Cannot get SPF record: %s', err)
219 end
220 end
221 rspamd_spf.resolve(task, cb)
222 end
223
224 local function spf_flatten_handler(opts)
225 local rspamd_spf = require "rspamd_spf"
226 local rspamd_task = require "rspamd_task"
227
228 if not opts.domain then
229 io.stderr:write('Domain is required\n')
230 os.exit(1)
231 end
232
233 local task = rspamd_task.create(rspamd_config, rspamadm_ev_base)
234 task:set_session(rspamadm_session)
235 task:set_resolver(rspamadm_dns_resolver)
236 task:set_from('smtp', { user = 'user', domain = opts.domain })
237
238 local function has_macro(str)
239 return str and str:find('%%')
240 end
241
242 local function result_to_qualifier(result)
243 if result == rspamd_spf.policy.pass then
244 return '+'
245 elseif result == rspamd_spf.policy.fail then
246 return '-'
247 elseif result == rspamd_spf.policy.soft_fail then
248 return '~'
249 elseif result == rspamd_spf.policy.neutral then
250 return '?'
251 end
252 return '+'
253 end
254
255 local function is_all_mechanism(str)
256 return str and str:match('^[+-~?]?all$')
257 end
258
259 local function is_valid_ip_net(addr)
260 if not addr or addr == '' or addr == 'any' then
261 return false
262 end
263 return addr:match('^[0-9]') or addr:match('^[a-fA-F0-9]*:')
264 end
265
266 local function has_macro_unresolved_flag(elt)
267 if not elt.flags then
268 return false
269 end
270 local RSPAMD_SPF_FLAG_MACRO_UNRESOLVED = bit.lshift(1, 14)
271 return bit.band(elt.flags, RSPAMD_SPF_FLAG_MACRO_UNRESOLVED) ~= 0
272 end
273
274 local function collect_mechanisms(elts)
275 local ipv4_nets = {}
276 local ipv6_nets = {}
277 local dynamic_mechanisms = {}
278 local other_mechanisms = {}
279 local seen_all = false
280
281 for _, elt in ipairs(elts) do
282 local processed = false
283
284 if elt.str and is_all_mechanism(elt.str) then
285 local qualifier = result_to_qualifier(elt.result)
286 local all_mech = (qualifier == '+' and 'all') or (qualifier .. 'all')
287 table.insert(other_mechanisms, all_mech)
288 seen_all = true
289 processed = true
290 elseif elt.str and (has_macro(elt.str) or elt.str:match('^[+-~?]?redirect')) then
291 table.insert(other_mechanisms, elt.str)
292 processed = true
293 elseif has_macro_unresolved_flag(elt) then
294 table.insert(other_mechanisms, elt.str)
295 processed = true
296 elseif elt.addr and not seen_all and is_valid_ip_net(elt.addr) then
297 local qualifier = result_to_qualifier(elt.result)
298 local net = elt.addr
299
300 if net:find(':') then
301 table.insert(ipv6_nets, { net = net, qual = qualifier })
302 else
303 table.insert(ipv4_nets, { net = net, qual = qualifier })
304 end
305 processed = true
306 end
307
308 if not processed and elt.str and not elt.str:match('^[+-~?]?include:') then
309 table.insert(other_mechanisms, elt.str)
310 end
311 end
312
313 return ipv4_nets, ipv6_nets, dynamic_mechanisms, other_mechanisms
314 end
315
316 local function optimize_ip_net(net, is_ipv6)
317 local default_mask = is_ipv6 and '/128' or '/32'
318 if net:sub(-#default_mask) == default_mask then
319 return net:sub(1, -#default_mask - 1)
320 end
321 return net
322 end
323
324 local function build_spf_record(ipv4_nets, ipv6_nets, dynamic_mechanisms, other_mechanisms, includes)
325 local parts = { 'v=spf1' }
326
327 if includes then
328 for _, inc in ipairs(includes) do
329 table.insert(parts, 'include:' .. inc)
330 end
331 end
332
333 for _, mech in ipairs(dynamic_mechanisms) do
334 table.insert(parts, mech)
335 end
336
337 for _, item in ipairs(ipv4_nets) do
338 local prefix = item.qual == '+' and '' or item.qual
339 local optimized_net = optimize_ip_net(item.net, false)
340 table.insert(parts, prefix .. 'ip4:' .. optimized_net)
341 end
342
343 for _, item in ipairs(ipv6_nets) do
344 local prefix = item.qual == '+' and '' or item.qual
345 local optimized_net = optimize_ip_net(item.net, true)
346 table.insert(parts, prefix .. 'ip6:' .. optimized_net)
347 end
348
349 for _, mech in ipairs(other_mechanisms) do
350 table.insert(parts, mech)
351 end
352
353 return table.concat(parts, ' ')
354 end
355
356 local function split_networks_into_chunks(ipv4_nets, ipv6_nets, base_domain, all_mechanism)
357 local max_record_length = 450
358 local chunks = {}
359 local current_chunk_v4 = {}
360 local current_chunk_v6 = {}
361 local all_v4 = {}
362 local all_v6 = {}
363
364 local all_mechs = all_mechanism and {all_mechanism} or {}
365
366 for _, item in ipairs(ipv4_nets) do
367 table.insert(all_v4, item)
368 end
369 for _, item in ipairs(ipv6_nets) do
370 table.insert(all_v6, item)
371 end
372
373 local chunk_idx = 1
374 local function finalize_chunk()
375 if #current_chunk_v4 > 0 or #current_chunk_v6 > 0 then
376 local record = build_spf_record(current_chunk_v4, current_chunk_v6, {}, all_mechs, nil)
377 table.insert(chunks, {
378 name = string.format('%d._spf.%s', chunk_idx, base_domain),
379 record = record
380 })
381 chunk_idx = chunk_idx + 1
382 current_chunk_v4 = {}
383 current_chunk_v6 = {}
384 end
385 end
386
387 for _, item in ipairs(all_v4) do
388 local single_item_test = build_spf_record({item}, {}, {}, all_mechs, nil)
389 if #single_item_test > max_record_length then
390 printf('Warning: IPv4 network %s is too large to fit in a single SPF record, skipping', item.net)
391 else
392 table.insert(current_chunk_v4, item)
393 local test_record = build_spf_record(current_chunk_v4, current_chunk_v6, {}, all_mechs, nil)
394 if #test_record > max_record_length then
395 table.remove(current_chunk_v4)
396 finalize_chunk()
397 table.insert(current_chunk_v4, item)
398 end
399 end
400 end
401
402 for _, item in ipairs(all_v6) do
403 local single_item_test = build_spf_record({}, {item}, {}, all_mechs, nil)
404 if #single_item_test > max_record_length then
405 printf('Warning: IPv6 network %s is too large to fit in a single SPF record, skipping', item.net)
406 else
407 table.insert(current_chunk_v6, item)
408 local test_record = build_spf_record(current_chunk_v4, current_chunk_v6, {}, all_mechs, nil)
409 if #test_record > max_record_length then
410 table.remove(current_chunk_v6)
411 finalize_chunk()
412 table.insert(current_chunk_v6, item)
413 end
414 end
415 end
416
417 finalize_chunk()
418 return chunks
419 end
420
421 local function cb(record, flags, err)
422 if not record then
423 printf('Cannot get SPF record: %s', err)
424 os.exit(1)
425 end
426
427 local elts = record:get_elts()
428 local ipv4_nets, ipv6_nets, dynamic_mechanisms, other_mechanisms = collect_mechanisms(elts)
429
430 local all_mechanism = nil
431 local other_without_all = {}
432 for _, mech in ipairs(other_mechanisms) do
433 if is_all_mechanism(mech) then
434 all_mechanism = mech
435 else
436 table.insert(other_without_all, mech)
437 end
438 end
439
440 local test_record = build_spf_record(ipv4_nets, ipv6_nets, dynamic_mechanisms, other_mechanisms, nil)
441 local needs_split = #test_record > 450
442
443 if opts.format == 'json' then
444 local ucl = require "ucl"
445 local result = {
446 domain = opts.domain,
447 ipv4_count = #ipv4_nets,
448 ipv6_count = #ipv6_nets,
449 dynamic_mechanisms = dynamic_mechanisms,
450 other_mechanisms = other_mechanisms,
451 needs_split = needs_split
452 }
453
454 if needs_split then
455 local chunks = split_networks_into_chunks(ipv4_nets, ipv6_nets, opts.domain, all_mechanism)
456 local include_names = {}
457 for _, chunk in ipairs(chunks) do
458 table.insert(include_names, chunk.name)
459 end
460 local main_record = build_spf_record({}, {}, dynamic_mechanisms, other_without_all, include_names)
461 if all_mechanism then
462 main_record = main_record .. ' ' .. all_mechanism
463 end
464
465 result.main_record = main_record
466 result.additional_records = {}
467 for _, chunk in ipairs(chunks) do
468 table.insert(result.additional_records, {
469 name = chunk.name,
470 value = chunk.record
471 })
472 end
473 else
474 result.record = test_record
475 end
476
477 printf('%s', ucl.to_format(result, 'json'))
478 elseif opts.format == 'compact' then
479 if needs_split then
480 local chunks = split_networks_into_chunks(ipv4_nets, ipv6_nets, opts.domain, all_mechanism)
481 local include_names = {}
482 for _, chunk in ipairs(chunks) do
483 table.insert(include_names, chunk.name)
484 end
485 local main_record = build_spf_record({}, {}, dynamic_mechanisms, other_without_all, include_names)
486 if all_mechanism then
487 main_record = main_record .. ' ' .. all_mechanism
488 end
489
490 printf('%s. IN TXT "%s"', opts.domain, main_record)
491 for _, chunk in ipairs(chunks) do
492 printf('%s. IN TXT "%s"', chunk.name, chunk.record)
493 end
494 else
495 printf('%s. IN TXT "%s"', opts.domain, test_record)
496 end
497 else
498 printf('Flattened SPF record for %s:', highlight(opts.domain))
499 printf('')
500 printf('Found %s IPv4 networks, %s IPv6 networks, %s dynamic mechanisms, %s other mechanisms',
501 highlight(tostring(#ipv4_nets)),
502 highlight(tostring(#ipv6_nets)),
503 highlight(tostring(#dynamic_mechanisms)),
504 highlight(tostring(#other_mechanisms)))
505 printf('')
506
507 if needs_split then
508 printf('%s: Needs splitting (full length: %d)', red('Result'), #test_record)
509 printf('')
510
511 local chunks = split_networks_into_chunks(ipv4_nets, ipv6_nets, opts.domain, all_mechanism)
512 local include_names = {}
513 for _, chunk in ipairs(chunks) do
514 table.insert(include_names, chunk.name)
515 end
516
517 local main_record = build_spf_record({}, {}, dynamic_mechanisms, other_without_all, include_names)
518 if all_mechanism then
519 main_record = main_record .. ' ' .. all_mechanism
520 end
521
522 printf('%s:', highlight('Main record'))
523 printf('%s', main_record)
524 printf('')
525
526 for _, chunk in ipairs(chunks) do
527 printf('%s:', highlight('TXT record for ' .. chunk.name))
528 printf('%s', chunk.record)
529 printf('')
530 end
531 else
532 printf('%s: Single record (length: %d)', green('Result'), #test_record)
533 printf('')
534 printf('%s', test_record)
535 end
536 end
537 end
538
539 rspamd_spf.resolve(task, cb)
540 end
541
542 local function handler(args)
543 local opts = parser:parse(args)
544 load_config(opts)
545
546 local command = opts.command
547
548 if command == 'spf' then
549 spf_handler(opts)
550 elseif command == 'spf-flatten' then
551 spf_flatten_handler(opts)
552 else
553 parser:error('command %s is not implemented', command)
554 end
555 end
556
557 return {
558 name = 'dnstool',
559 aliases = { 'dns', 'dns_tool' },
560 handler = handler,
561 description = parser._description
562 }