rspamd.local.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. rspamd_config.MAILCOW_AUTH = {
  2. callback = function(task)
  3. local uname = task:get_user()
  4. if uname then
  5. return 1
  6. end
  7. end
  8. }
  9. local monitoring_hosts = rspamd_config:add_map{
  10. url = "/etc/rspamd/custom/monitoring_nolog.map",
  11. description = "Monitoring hosts",
  12. type = "regexp"
  13. }
  14. rspamd_config:register_symbol({
  15. name = 'SMTP_ACCESS',
  16. type = 'postfilter',
  17. callback = function(task)
  18. local util = require("rspamd_util")
  19. local rspamd_logger = require "rspamd_logger"
  20. local rspamd_ip = require 'rspamd_ip'
  21. local uname = task:get_user()
  22. local limited_access = task:get_symbol("SMTP_LIMITED_ACCESS")
  23. if not uname then
  24. return false
  25. end
  26. if not limited_access then
  27. return false
  28. end
  29. local hash_key = 'SMTP_ALLOW_NETS_' .. uname
  30. local redis_params = rspamd_parse_redis_server('smtp_access')
  31. local ip = task:get_from_ip()
  32. if ip == nil or not ip:is_valid() then
  33. return false
  34. end
  35. local from_ip_string = tostring(ip)
  36. smtp_access_table = {from_ip_string}
  37. local maxbits = 128
  38. local minbits = 32
  39. if ip:get_version() == 4 then
  40. maxbits = 32
  41. minbits = 8
  42. end
  43. for i=maxbits,minbits,-1 do
  44. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  45. table.insert(smtp_access_table, nip)
  46. end
  47. local function smtp_access_cb(err, data)
  48. if err then
  49. rspamd_logger.infox(rspamd_config, "smtp_access query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  50. return false
  51. else
  52. rspamd_logger.infox(rspamd_config, "checking ip %s for smtp_access in %s", from_ip_string, hash_key)
  53. for k,v in pairs(data) do
  54. if (v and v ~= userdata and v == '1') then
  55. rspamd_logger.infox(rspamd_config, "found ip in smtp_access map")
  56. task:insert_result(true, 'SMTP_ACCESS', 0.0, from_ip_string)
  57. return true
  58. end
  59. end
  60. rspamd_logger.infox(rspamd_config, "couldnt find ip in smtp_access map")
  61. task:insert_result(true, 'SMTP_ACCESS', 999.0, from_ip_string)
  62. return true
  63. end
  64. end
  65. table.insert(smtp_access_table, 1, hash_key)
  66. local redis_ret_user = rspamd_redis_make_request(task,
  67. redis_params, -- connect params
  68. hash_key, -- hash key
  69. false, -- is write
  70. smtp_access_cb, --callback
  71. 'HMGET', -- command
  72. smtp_access_table -- arguments
  73. )
  74. if not redis_ret_user then
  75. rspamd_logger.infox(rspamd_config, "cannot check smtp_access redis map")
  76. end
  77. end,
  78. priority = 10
  79. })
  80. rspamd_config:register_symbol({
  81. name = 'KEEP_SPAM',
  82. type = 'prefilter',
  83. callback = function(task)
  84. local util = require("rspamd_util")
  85. local rspamd_logger = require "rspamd_logger"
  86. local rspamd_ip = require 'rspamd_ip'
  87. local uname = task:get_user()
  88. if uname then
  89. return false
  90. end
  91. local redis_params = rspamd_parse_redis_server('keep_spam')
  92. local ip = task:get_from_ip()
  93. if ip == nil or not ip:is_valid() then
  94. return false
  95. end
  96. local from_ip_string = tostring(ip)
  97. ip_check_table = {from_ip_string}
  98. local maxbits = 128
  99. local minbits = 32
  100. if ip:get_version() == 4 then
  101. maxbits = 32
  102. minbits = 8
  103. end
  104. for i=maxbits,minbits,-1 do
  105. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  106. table.insert(ip_check_table, nip)
  107. end
  108. local function keep_spam_cb(err, data)
  109. if err then
  110. rspamd_logger.infox(rspamd_config, "keep_spam query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  111. return false
  112. else
  113. for k,v in pairs(data) do
  114. if (v and v ~= userdata and v == '1') then
  115. rspamd_logger.infox(rspamd_config, "found ip in keep_spam map, setting pre-result")
  116. task:set_pre_result('accept', 'ip matched with forward hosts')
  117. end
  118. end
  119. end
  120. end
  121. table.insert(ip_check_table, 1, 'KEEP_SPAM')
  122. local redis_ret_user = rspamd_redis_make_request(task,
  123. redis_params, -- connect params
  124. 'KEEP_SPAM', -- hash key
  125. false, -- is write
  126. keep_spam_cb, --callback
  127. 'HMGET', -- command
  128. ip_check_table -- arguments
  129. )
  130. if not redis_ret_user then
  131. rspamd_logger.infox(rspamd_config, "cannot check keep_spam redis map")
  132. end
  133. end,
  134. priority = 19
  135. })
  136. rspamd_config:register_symbol({
  137. name = 'TLS_HEADER',
  138. type = 'postfilter',
  139. callback = function(task)
  140. local rspamd_logger = require "rspamd_logger"
  141. local tls_tag = task:get_request_header('TLS-Version')
  142. if type(tls_tag) == 'nil' then
  143. task:set_milter_reply({
  144. add_headers = {['X-Last-TLS-Session-Version'] = 'None'}
  145. })
  146. else
  147. task:set_milter_reply({
  148. add_headers = {['X-Last-TLS-Session-Version'] = tostring(tls_tag)}
  149. })
  150. end
  151. end,
  152. priority = 12
  153. })
  154. rspamd_config:register_symbol({
  155. name = 'TAG_MOO',
  156. type = 'postfilter',
  157. callback = function(task)
  158. local util = require("rspamd_util")
  159. local rspamd_logger = require "rspamd_logger"
  160. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  161. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  162. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  163. local tag = tagged_rcpt[1].options[1]
  164. rspamd_logger.infox("found tag: %s", tag)
  165. local action = task:get_metric_action('default')
  166. rspamd_logger.infox("metric action now: %s", action)
  167. if action ~= 'no action' and action ~= 'greylist' then
  168. rspamd_logger.infox("skipping tag handler for action: %s", action)
  169. task:set_metric_action('default', action)
  170. return true
  171. end
  172. local wants_subject_tag = task:get_symbol("RCPT_WANTS_SUBJECT_TAG")
  173. local wants_subfolder_tag = task:get_symbol("RCPT_WANTS_SUBFOLDER_TAG")
  174. if wants_subject_tag then
  175. rspamd_logger.infox("user wants subject modified for tagged mail")
  176. local sbj = task:get_header('Subject')
  177. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  178. task:set_milter_reply({
  179. remove_headers = {['Subject'] = 1},
  180. add_headers = {['Subject'] = new_sbj}
  181. })
  182. elseif wants_subfolder_tag then
  183. rspamd_logger.infox("Add X-Moo-Tag header")
  184. task:set_milter_reply({
  185. add_headers = {['X-Moo-Tag'] = 'YES'}
  186. })
  187. end
  188. end
  189. end,
  190. priority = 11
  191. })
  192. rspamd_config:register_symbol({
  193. name = 'DYN_RL_CHECK',
  194. type = 'prefilter',
  195. callback = function(task)
  196. local util = require("rspamd_util")
  197. local redis_params = rspamd_parse_redis_server('dyn_rl')
  198. local rspamd_logger = require "rspamd_logger"
  199. local envfrom = task:get_from(1)
  200. local uname = task:get_user()
  201. if not envfrom or not uname then
  202. return false
  203. end
  204. local uname = uname:lower()
  205. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  206. local function redis_cb_user(err, data)
  207. if err or type(data) ~= 'string' then
  208. rspamd_logger.infox(rspamd_config, "dynamic ratelimit request for user %s returned invalid or empty data (\"%s\") or error (\"%s\") - trying dynamic ratelimit for domain...", uname, data, err)
  209. local function redis_key_cb_domain(err, data)
  210. if err or type(data) ~= 'string' then
  211. rspamd_logger.infox(rspamd_config, "dynamic ratelimit request for domain %s returned invalid or empty data (\"%s\") or error (\"%s\")", env_from_domain, data, err)
  212. else
  213. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  214. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  215. end
  216. end
  217. local redis_ret_domain = rspamd_redis_make_request(task,
  218. redis_params, -- connect params
  219. env_from_domain, -- hash key
  220. false, -- is write
  221. redis_key_cb_domain, --callback
  222. 'HGET', -- command
  223. {'RL_VALUE', env_from_domain} -- arguments
  224. )
  225. if not redis_ret_domain then
  226. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  227. end
  228. else
  229. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  230. task:insert_result('DYN_RL', 0.0, data, uname)
  231. end
  232. end
  233. local redis_ret_user = rspamd_redis_make_request(task,
  234. redis_params, -- connect params
  235. uname, -- hash key
  236. false, -- is write
  237. redis_cb_user, --callback
  238. 'HGET', -- command
  239. {'RL_VALUE', uname} -- arguments
  240. )
  241. if not redis_ret_user then
  242. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  243. end
  244. return true
  245. end,
  246. flags = 'empty',
  247. priority = 20
  248. })
  249. rspamd_config:register_symbol({
  250. name = 'NO_LOG_STAT',
  251. type = 'postfilter',
  252. callback = function(task)
  253. local from = task:get_header('From')
  254. if from and monitoring_hosts:get_key(from) then
  255. task:set_flag('no_log')
  256. task:set_flag('no_stat')
  257. end
  258. end
  259. })