rspamd.local.lua 6.1 KB

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