rspamd.local.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = 'KEEP_SPAM',
  16. type = 'prefilter',
  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. if uname then
  23. return false
  24. end
  25. local redis_params = rspamd_parse_redis_server('keep_spam')
  26. local ip = task:get_from_ip()
  27. if ip == nil or not ip:is_valid() then
  28. return false
  29. end
  30. local from_ip_string = tostring(ip)
  31. ip_check_table = {from_ip_string}
  32. local maxbits = 128
  33. local minbits = 32
  34. if ip:get_version() == 4 then
  35. maxbits = 32
  36. minbits = 8
  37. end
  38. for i=maxbits,minbits,-1 do
  39. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  40. table.insert(ip_check_table, nip)
  41. end
  42. local function keep_spam_cb(err, data)
  43. if err then
  44. rspamd_logger.infox(rspamd_config, "keep_spam query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  45. return false
  46. else
  47. for k,v in pairs(data) do
  48. if (v and v ~= userdata and v == '1') then
  49. rspamd_logger.infox(rspamd_config, "found ip in keep_spam map, setting pre-result", v)
  50. task:set_pre_result('accept', 'IP matched with forward hosts')
  51. end
  52. end
  53. end
  54. end
  55. table.insert(ip_check_table, 1, 'KEEP_SPAM')
  56. local redis_ret_user = rspamd_redis_make_request(task,
  57. redis_params, -- connect params
  58. 'KEEP_SPAM', -- hash key
  59. false, -- is write
  60. keep_spam_cb, --callback
  61. 'HMGET', -- command
  62. ip_check_table -- arguments
  63. )
  64. if not redis_ret_user then
  65. rspamd_logger.infox(rspamd_config, "cannot check keep_spam redis map")
  66. end
  67. end,
  68. priority = 19
  69. })
  70. rspamd_config:register_symbol({
  71. name = 'TLS_HEADER',
  72. type = 'postfilter',
  73. callback = function(task)
  74. local rspamd_logger = require "rspamd_logger"
  75. local tls_tag = task:get_request_header('TLS-Version')
  76. if type(tls_tag) == 'nil' then
  77. task:set_milter_reply({
  78. add_headers = {['X-Last-TLS-Session-Version'] = 'None'}
  79. })
  80. else
  81. task:set_milter_reply({
  82. add_headers = {['X-Last-TLS-Session-Version'] = tostring(tls_tag)}
  83. })
  84. end
  85. end,
  86. priority = 12
  87. })
  88. rspamd_config:register_symbol({
  89. name = 'TAG_MOO',
  90. type = 'postfilter',
  91. callback = function(task)
  92. local util = require("rspamd_util")
  93. local rspamd_logger = require "rspamd_logger"
  94. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  95. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  96. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  97. local tag = tagged_rcpt[1].options[1]
  98. rspamd_logger.infox("found tag: %s", tag)
  99. local action = task:get_metric_action('default')
  100. rspamd_logger.infox("metric action now: %s", action)
  101. if action ~= 'no action' and action ~= 'greylist' then
  102. rspamd_logger.infox("skipping tag handler for action: %s", action)
  103. task:set_metric_action('default', action)
  104. return true
  105. end
  106. local wants_subject_tag = task:get_symbol("RCPT_WANTS_SUBJECT_TAG")
  107. local wants_subfolder_tag = task:get_symbol("RCPT_WANTS_SUBFOLDER_TAG")
  108. if wants_subject_tag then
  109. rspamd_logger.infox("user wants subject modified for tagged mail")
  110. local sbj = task:get_header('Subject')
  111. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  112. task:set_milter_reply({
  113. remove_headers = {['Subject'] = 1},
  114. add_headers = {['Subject'] = new_sbj}
  115. })
  116. elseif wants_subfolder_tag then
  117. rspamd_logger.infox("Add X-Moo-Tag header")
  118. task:set_milter_reply({
  119. add_headers = {['X-Moo-Tag'] = 'YES'}
  120. })
  121. end
  122. end
  123. end,
  124. priority = 11
  125. })
  126. rspamd_config:register_symbol({
  127. name = 'DYN_RL_CHECK',
  128. type = 'prefilter',
  129. callback = function(task)
  130. local util = require("rspamd_util")
  131. local redis_params = rspamd_parse_redis_server('dyn_rl')
  132. local rspamd_logger = require "rspamd_logger"
  133. local envfrom = task:get_from(1)
  134. local uname = task:get_user()
  135. if not envfrom or not uname then
  136. return false
  137. end
  138. local uname = uname:lower()
  139. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  140. local function redis_cb_user(err, data)
  141. if err or type(data) ~= 'string' then
  142. 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)
  143. local function redis_key_cb_domain(err, data)
  144. if err or type(data) ~= 'string' then
  145. 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)
  146. else
  147. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  148. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  149. end
  150. end
  151. local redis_ret_domain = rspamd_redis_make_request(task,
  152. redis_params, -- connect params
  153. env_from_domain, -- hash key
  154. false, -- is write
  155. redis_key_cb_domain, --callback
  156. 'HGET', -- command
  157. {'RL_VALUE', env_from_domain} -- arguments
  158. )
  159. if not redis_ret_domain then
  160. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  161. end
  162. else
  163. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  164. task:insert_result('DYN_RL', 0.0, data, uname)
  165. end
  166. end
  167. local redis_ret_user = rspamd_redis_make_request(task,
  168. redis_params, -- connect params
  169. uname, -- hash key
  170. false, -- is write
  171. redis_cb_user, --callback
  172. 'HGET', -- command
  173. {'RL_VALUE', uname} -- arguments
  174. )
  175. if not redis_ret_user then
  176. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  177. end
  178. return true
  179. end,
  180. flags = 'empty',
  181. priority = 20
  182. })
  183. rspamd_config:register_symbol({
  184. name = 'NO_LOG_STAT',
  185. type = 'postfilter',
  186. callback = function(task)
  187. local from = task:get_header('From')
  188. if from and monitoring_hosts:get_key(from) then
  189. task:set_flag('no_log')
  190. task:set_flag('no_stat')
  191. end
  192. end
  193. })