comparison mod_register_dnsbl_firewall_mark/mod_register_dnsbl_firewall_mark.lua @ 4011:de40686ae9c8

mod_register_dnsbl_firewall_mark: introduce optional registration_rbl_message with mod_welcome inspired syntax
author Georg Lukas <georg@op-co.de>
date Wed, 06 May 2020 14:55:36 +0200
parents 76036fa34055
children
comparison
equal deleted inserted replaced
4010:ae27f3359df8 4011:de40686ae9c8
1 local adns = require "net.adns"; 1 local adns = require "net.adns";
2 local rbl = module:get_option_string("registration_rbl"); 2 local rbl = module:get_option_string("registration_rbl");
3 local rbl_message = module:get_option_string("registration_rbl_message");
4 local st = require "util.stanza";
5
6
7 local function cleanup_ip(ip)
8 if ip:sub(1,7):lower() == "::ffff:" then
9 return ip:sub(8);
10 end
11 return ip;
12 end
3 13
4 local function reverse(ip, suffix) 14 local function reverse(ip, suffix)
5 if ip:sub(1,7):lower() == "::ffff:" then
6 ip = ip:sub(8);
7 end
8 local a,b,c,d = ip:match("^(%d+).(%d+).(%d+).(%d+)$"); 15 local a,b,c,d = ip:match("^(%d+).(%d+).(%d+).(%d+)$");
9 if not a then return end 16 if not a then return end
10 return ("%d.%d.%d.%d.%s"):format(d,c,b,a, suffix); 17 return ("%d.%d.%d.%d.%s"):format(d,c,b,a, suffix);
11 end 18 end
12 19
13 module:hook("user-registered", function (event) 20 module:hook("user-registered", function (event)
14 local session = event.session; 21 local session = event.session;
15 local ip = session and session.ip; 22 local ip = session and session.ip and cleanup_ip(session.ip);
16 local rbl_ip = ip and reverse(ip, rbl); 23 local rbl_ip = ip and reverse(ip, rbl);
17 if rbl_ip then 24 if rbl_ip then
18 local registration_time = os.time(); 25 local registration_time = os.time();
19 local log = session.log; 26 local log = session.log;
20 adns.lookup(function (reply) 27 adns.lookup(function (reply)
24 if user and user.firewall_marks then 31 if user and user.firewall_marks then
25 user.firewall_marks.dnsbl_hit = registration_time; 32 user.firewall_marks.dnsbl_hit = registration_time;
26 else 33 else
27 module:open_store("firewall_marks", "map"):set(event.username, "dnsbl_hit", registration_time); 34 module:open_store("firewall_marks", "map"):set(event.username, "dnsbl_hit", registration_time);
28 end 35 end
36 if rbl_message then
37 module:log("debug", "Warning RBL registered user %s@%s", event.username, event.host);
38 event.ip = ip;
39 local rbl_stanza =
40 st.message({ to = event.username.."@"..event.host, from = event.host },
41 rbl_message:gsub("$(%w+)", event));
42 module:send(rbl_stanza);
43 end
29 end 44 end
30 end, rbl_ip); 45 end, rbl_ip);
31 end 46 end
32 end); 47 end);