rspamd.local.lua 6.1 KB

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