]> source.dussan.org Git - rspamd.git/commitdiff
IP netmask support for multimap
authorMichael Kuron <mkuron@users.noreply.github.com>
Sat, 15 Jul 2017 14:11:51 +0000 (16:11 +0200)
committerMichael Kuron <mkuron@users.noreply.github.com>
Sat, 15 Jul 2017 14:20:55 +0000 (16:20 +0200)
src/plugins/lua/multimap.lua

index 0e7da23e10f7dfe75f984629517fb069ecdc6527..e6f6f044f2dd1e0a415a253cfb2e5e31fb32df18 100644 (file)
@@ -35,7 +35,12 @@ local urls = {}
 
 local value_types = {
   ip = {
-    get_value = function(ip) return ip:to_string() end,
+    get_value = function(ip)
+      if type(ip) == 'string' then
+        return ip
+      end
+      return ip:to_string()
+    end,
   },
   from = {
     get_value = function(val) return val end,
@@ -373,14 +378,14 @@ local function multimap_callback(task, rule)
     if r['cdb'] then
       local srch = value
       if r['type'] == 'ip' then
-        srch = value:to_string()
+        srch = value_types['ip'].get_value(value)
       end
       ret = r['cdb']:lookup(srch)
     elseif r['redis_key'] then
       local srch = value
       if r['type'] == 'ip' or (r['type'] == 'received' and
         (r['filter'] == 'real_ip' or r['filter'] == 'from_ip' or not r['filter'])) then
-        srch = value:to_string()
+        srch = value_types['ip'].get_value(value)
       end
       ret = rspamd_redis_make_request(task,
         redis_params, -- connect params
@@ -636,6 +641,14 @@ local function multimap_callback(task, rule)
       if ip:is_valid() then
         if rt == 'ip' then
           match_rule(rule, ip)
+          local bits = 128
+          if ip:get_version() == 4 then
+            bits = 32
+          end
+          for i=bits,1,-1 do
+            local nip = ip:apply_mask(i):to_string() .. "/" .. i
+            match_rule(rule, nip)
+          end
         else
           local cb = function (_, to_resolve, results, err)
             task:inc_dns_req()