comparison mod_dnsbl/mod_dnsbl.lua @ 6499:1213145e27fc

mod_dnsbl: Support for anonymous hosts
author Matthew Wild <mwild1@gmail.com>
date Wed, 25 Mar 2026 18:13:00 +0000
parents ccbba57cacb5
children f61564e11d3b
comparison
equal deleted inserted replaced
6498:4226e6aadbd8 6499:1213145e27fc
1 local lfs = require "lfs"; 1 local lfs = require "lfs";
2 2
3 local adns = require "net.adns"; 3 local adns = require "net.adns";
4 local async = require "util.async";
4 local it = require "util.iterators"; 5 local it = require "util.iterators";
5 local hex = require "util.hex"; 6 local hex = require "util.hex";
6 local parse_cidr = require "util.ip".parse_cidr; 7 local parse_cidr = require "util.ip".parse_cidr;
7 local parse_ip = require "util.ip".new_ip; 8 local parse_ip = require "util.ip".new_ip;
8 local promise = require "util.promise"; 9 local promise = require "util.promise";
21 22
22 if not dnsbls_config_raw then 23 if not dnsbls_config_raw then
23 module:log_status("error", "No 'dnsbls' in config file"); 24 module:log_status("error", "No 'dnsbls' in config file");
24 return; 25 return;
25 end 26 end
27
28 local is_host_anonymous = module:get_option_string("authentication") == "anonymous";
26 29
27 local dnsbls = set.new(); 30 local dnsbls = set.new();
28 local dnsbls_config = {}; 31 local dnsbls_config = {};
29 32
30 for k, v in ipairs(dnsbls_config_raw) do 33 for k, v in ipairs(dnsbls_config_raw) do
178 for dnsbl in dnsbls do 181 for dnsbl in dnsbls do
179 check_dnsbl(ip, dnsbl, handle_dnsbl_register_result, event); 182 check_dnsbl(ip, dnsbl, handle_dnsbl_register_result, event);
180 end 183 end
181 end); 184 end);
182 185
186 if is_host_anonymous then
187 module:hook("resource-bind", function (event)
188 local session = event.session;
189 local username, ip = session.username, session.ip;
190 if not ip then return; end
191
192 local queries = {};
193 for dnsbl in dnsbls do
194 queries[dnsbl] = promise.new(function (resolve)
195 check_dnsbl(ip, dnsbl, function (_, hit)
196 resolve(hit);
197 end);
198 end);
199 end
200 local results, err = async.wait_for(promise.all_settled(queries));
201 if not results then
202 module:log("warn", "Failed to query dnsbls: %s", err);
203 return;
204 end
205 local any_match, any_error;
206 for dnsbl, result in pairs(results) do
207 if result.status == "fulfilled" and result.value == true then
208 local flag = dnsbls_config[dnsbl].flag or default_dnsbl_flag;
209 module:log("info", "Flagging %s for anonymous user %s connecting from %s matching %s", flag, username, ip, dnsbl);
210 mod_flags:add_flag(username, flag, "Matched "..dnsbl);
211 any_match = true;
212 elseif result.status == "rejected" then
213 any_error = true;
214 end
215 end
216 if not any_match then
217 if not any_error then
218 module:log("debug", "DNSBLs all clear for this IP address");
219 else
220 module:log("debug", "Errors were encountered while querying DNSBLs");
221 end
222 end
223 end);
224 end
225
183 module:add_item("account-trait", { 226 module:add_item("account-trait", {
184 name = "register-dnsbl-hit"; 227 name = "register-dnsbl-hit";
185 prob_bad_true = 0.6; 228 prob_bad_true = 0.6;
186 prob_bad_false = 0.4; 229 prob_bad_false = 0.4;
187 }); 230 });