comparison mod_register_dnsbl/mod_register_dnsbl.lua @ 2890:6412595e2046

mod_register_dnsbl: Use async support in trunk to actually block registration if a positive match is found in the DNSBL
author Kim Alvefur <zash@zash.se>
date Fri, 23 Feb 2018 21:56:42 +0100
parents 2dcc3079572c
children 84670bac7348
comparison
equal deleted inserted replaced
2889:7fb82481b3db 2890:6412595e2046
1 local adns = require "net.adns"; 1 local adns = require "net.adns";
2 local async = require "util.async";
3
2 local rbl = module:get_option_string("registration_rbl"); 4 local rbl = module:get_option_string("registration_rbl");
3 5
4 local function reverse(ip, suffix) 6 local function reverse(ip, suffix)
5 local a,b,c,d = ip:match("^(%d+).(%d+).(%d+).(%d+)$"); 7 local a,b,c,d = ip:match("^(%d+).(%d+).(%d+).(%d+)$");
6 if not a then return end 8 if not a then return end
7 return ("%d.%d.%d.%d.%s"):format(d,c,b,a, suffix); 9 return ("%d.%d.%d.%d.%s"):format(d,c,b,a, suffix);
8 end 10 end
9 11
10 -- TODO async 12 module:hook("user-registering", function (event)
11 -- module:hook("user-registering", function (event) end); 13 local session, ip = event.session, event.ip;
14 if not ip then
15 session.log("debug", "Unable to check DNSBL when IP is unknown");
16 return;
17 end
18 local rbl_ip, err = reverse(ip, rbl);
19 if not rbl_ip then
20 session.log("debug", "Unable to check DNSBL for ip %s: %s", ip, err);
21 return;
22 end
12 23
13 module:hook("user-registered", function (event) 24 local wait, done = async.waiter();
14 local session = event.session; 25 adns.lookup(function (reply)
15 local ip = session and session.ip; 26 if reply and reply[1] and reply[1].a then
16 local rbl_ip = ip and reverse(ip, rbl); 27 session.log("debug", "DNSBL response: %s IN A %s", rbl_ip, reply[1].a);
17 if rbl_ip then 28 session.log("info", "Blocking %s from registering %s (dnsbl hit)", ip, event.username);
18 local log = session.log; 29 event.allowed = false;
19 adns.lookup(function (reply) 30 event.reason = "Blocked by DNSBL";
20 if reply and reply[1] then 31 end
21 log("warn", "Account %s@%s registered from IP %s found in RBL (%s)", event.username, event.host or module.host, ip, reply[1].a); 32 done();
22 end 33 end, rbl_ip);
23 end, rbl_ip); 34 wait();
24 end
25 end); 35 end);