rspamd.local.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = 'DIRECT_ALIAS_EXPANDER',
  82. type = 'prefilter',
  83. callback = function(task)
  84. local rspamd_http = require "rspamd_http"
  85. local rcpts = task:get_recipients('smtp')
  86. local rspamd_logger = require "rspamd_logger"
  87. local function http_callback(err_message, code, body, headers)
  88. if body ~= nil and body ~= "" then
  89. rspamd_logger.infox(rspamd_config, "expanding alias to \"%s\"", body)
  90. local final = {}
  91. local rcpt = {}
  92. final.addr = body
  93. table.insert(rcpt, final)
  94. task:set_recipients('smtp', rcpt)
  95. end
  96. end
  97. if rcpts and #rcpts == 1 then
  98. for _,rcpt in ipairs(rcpts) do
  99. rspamd_http.request({
  100. task=task,
  101. url='http://nginx:8081/aliasexp.php',
  102. body=task:get_content(),
  103. callback=http_callback,
  104. headers={Rcpt=rcpt['addr']},
  105. })
  106. end
  107. end
  108. end,
  109. priority = 19
  110. })
  111. rspamd_config:register_symbol({
  112. name = 'KEEP_SPAM',
  113. type = 'prefilter',
  114. callback = function(task)
  115. local util = require("rspamd_util")
  116. local rspamd_logger = require "rspamd_logger"
  117. local rspamd_ip = require 'rspamd_ip'
  118. local uname = task:get_user()
  119. if uname then
  120. return false
  121. end
  122. local redis_params = rspamd_parse_redis_server('keep_spam')
  123. local ip = task:get_from_ip()
  124. if ip == nil or not ip:is_valid() then
  125. return false
  126. end
  127. local from_ip_string = tostring(ip)
  128. ip_check_table = {from_ip_string}
  129. local maxbits = 128
  130. local minbits = 32
  131. if ip:get_version() == 4 then
  132. maxbits = 32
  133. minbits = 8
  134. end
  135. for i=maxbits,minbits,-1 do
  136. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  137. table.insert(ip_check_table, nip)
  138. end
  139. local function keep_spam_cb(err, data)
  140. if err then
  141. rspamd_logger.infox(rspamd_config, "keep_spam query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  142. return false
  143. else
  144. for k,v in pairs(data) do
  145. if (v and v ~= userdata and v == '1') then
  146. rspamd_logger.infox(rspamd_config, "found ip in keep_spam map, setting pre-result")
  147. task:set_pre_result('accept', 'ip matched with forward hosts')
  148. end
  149. end
  150. end
  151. end
  152. table.insert(ip_check_table, 1, 'KEEP_SPAM')
  153. local redis_ret_user = rspamd_redis_make_request(task,
  154. redis_params, -- connect params
  155. 'KEEP_SPAM', -- hash key
  156. false, -- is write
  157. keep_spam_cb, --callback
  158. 'HMGET', -- command
  159. ip_check_table -- arguments
  160. )
  161. if not redis_ret_user then
  162. rspamd_logger.infox(rspamd_config, "cannot check keep_spam redis map")
  163. end
  164. end,
  165. priority = 19
  166. })
  167. rspamd_config:register_symbol({
  168. name = 'TLS_HEADER',
  169. type = 'postfilter',
  170. callback = function(task)
  171. local rspamd_logger = require "rspamd_logger"
  172. local tls_tag = task:get_request_header('TLS-Version')
  173. if type(tls_tag) == 'nil' then
  174. task:set_milter_reply({
  175. add_headers = {['X-Last-TLS-Session-Version'] = 'None'}
  176. })
  177. else
  178. task:set_milter_reply({
  179. add_headers = {['X-Last-TLS-Session-Version'] = tostring(tls_tag)}
  180. })
  181. end
  182. end,
  183. priority = 12
  184. })
  185. rspamd_config:register_symbol({
  186. name = 'TAG_MOO',
  187. type = 'postfilter',
  188. callback = function(task)
  189. local util = require("rspamd_util")
  190. local rspamd_logger = require "rspamd_logger"
  191. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  192. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  193. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  194. local tag = tagged_rcpt[1].options[1]
  195. rspamd_logger.infox("found tag: %s", tag)
  196. local action = task:get_metric_action('default')
  197. rspamd_logger.infox("metric action now: %s", action)
  198. if action ~= 'no action' and action ~= 'greylist' then
  199. rspamd_logger.infox("skipping tag handler for action: %s", action)
  200. return true
  201. end
  202. local wants_subject_tag = task:get_symbol("RCPT_WANTS_SUBJECT_TAG")
  203. local wants_subfolder_tag = task:get_symbol("RCPT_WANTS_SUBFOLDER_TAG")
  204. if wants_subject_tag then
  205. rspamd_logger.infox("user wants subject modified for tagged mail")
  206. local sbj = task:get_header('Subject')
  207. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  208. task:set_milter_reply({
  209. remove_headers = {['Subject'] = 1},
  210. add_headers = {['Subject'] = new_sbj}
  211. })
  212. elseif wants_subfolder_tag then
  213. rspamd_logger.infox("Add X-Moo-Tag header")
  214. task:set_milter_reply({
  215. add_headers = {['X-Moo-Tag'] = 'YES'}
  216. })
  217. end
  218. end
  219. end,
  220. priority = 11
  221. })
  222. rspamd_config:register_symbol({
  223. name = 'DYN_RL_CHECK',
  224. type = 'prefilter',
  225. callback = function(task)
  226. local util = require("rspamd_util")
  227. local redis_params = rspamd_parse_redis_server('dyn_rl')
  228. local rspamd_logger = require "rspamd_logger"
  229. local envfrom = task:get_from(1)
  230. local uname = task:get_user()
  231. if not envfrom or not uname then
  232. return false
  233. end
  234. local uname = uname:lower()
  235. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  236. local function redis_cb_user(err, data)
  237. if err or type(data) ~= 'string' then
  238. 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)
  239. local function redis_key_cb_domain(err, data)
  240. if err or type(data) ~= 'string' then
  241. 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)
  242. else
  243. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  244. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  245. end
  246. end
  247. local redis_ret_domain = rspamd_redis_make_request(task,
  248. redis_params, -- connect params
  249. env_from_domain, -- hash key
  250. false, -- is write
  251. redis_key_cb_domain, --callback
  252. 'HGET', -- command
  253. {'RL_VALUE', env_from_domain} -- arguments
  254. )
  255. if not redis_ret_domain then
  256. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  257. end
  258. else
  259. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  260. task:insert_result('DYN_RL', 0.0, data, uname)
  261. end
  262. end
  263. local redis_ret_user = rspamd_redis_make_request(task,
  264. redis_params, -- connect params
  265. uname, -- hash key
  266. false, -- is write
  267. redis_cb_user, --callback
  268. 'HGET', -- command
  269. {'RL_VALUE', uname} -- arguments
  270. )
  271. if not redis_ret_user then
  272. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  273. end
  274. return true
  275. end,
  276. flags = 'empty',
  277. priority = 20
  278. })
  279. rspamd_config:register_symbol({
  280. name = 'NO_LOG_STAT',
  281. type = 'postfilter',
  282. callback = function(task)
  283. local from = task:get_header('From')
  284. if from and monitoring_hosts:get_key(from) then
  285. task:set_flag('no_log')
  286. task:set_flag('no_stat')
  287. end
  288. end
  289. })