rspamd.local.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = 'TAG_MOO',
  11. type = 'postfilter',
  12. callback = function(task)
  13. local util = require("rspamd_util")
  14. local rspamd_logger = require "rspamd_logger"
  15. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  16. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  17. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  18. local tag = tagged_rcpt[1].options[1]
  19. rspamd_logger.infox("found tag: %s", tag)
  20. local action = task:get_metric_action('default')
  21. rspamd_logger.infox("metric action now: %s", action)
  22. if action ~= 'no action' and action ~= 'greylist' then
  23. rspamd_logger.infox("skipping tag handler for action: %s", action)
  24. task:set_metric_action('default', action)
  25. return true
  26. end
  27. local wants_subject_tag = task:get_symbol("RCPT_WANTS_SUBJECT_TAG")
  28. local wants_subfolder_tag = task:get_symbol("RCPT_WANTS_SUBFOLDER_TAG")
  29. if wants_subject_tag then
  30. rspamd_logger.infox("user wants subject modified for tagged mail")
  31. local sbj = task:get_header('Subject')
  32. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  33. task:set_milter_reply({
  34. remove_headers = {['Subject'] = 1},
  35. add_headers = {['Subject'] = new_sbj}
  36. })
  37. elseif wants_subfolder_tag then
  38. rspamd_logger.infox("Add X-Moo-Tag header")
  39. task:set_milter_reply({
  40. add_headers = {['X-Moo-Tag'] = 'YES'}
  41. })
  42. end
  43. end
  44. end,
  45. priority = 11
  46. })
  47. rspamd_config:register_symbol({
  48. name = 'DYN_RL_CHECK',
  49. type = 'prefilter',
  50. callback = function(task)
  51. local util = require("rspamd_util")
  52. local redis_params = rspamd_parse_redis_server('dyn_rl')
  53. local rspamd_logger = require "rspamd_logger"
  54. local envfrom = task:get_from(1)
  55. if not envfrom then
  56. return false
  57. end
  58. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  59. local env_from_addr = envfrom[1].addr:lower() -- get smtp from addr in lower case
  60. local function redis_cb_user(err, data)
  61. if err or type(data) ~= 'string' then
  62. 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...", env_from_addr, data, err)
  63. local function redis_key_cb_domain(err, data)
  64. if err or type(data) ~= 'string' then
  65. 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)
  66. else
  67. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  68. task:insert_result('DYN_RL', 0.0, data)
  69. end
  70. end
  71. local redis_ret_domain = rspamd_redis_make_request(task,
  72. redis_params, -- connect params
  73. env_from_domain, -- hash key
  74. false, -- is write
  75. redis_key_cb_domain, --callback
  76. 'HGET', -- command
  77. {'RL_VALUE', env_from_domain} -- arguments
  78. )
  79. if not redis_ret_domain then
  80. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  81. end
  82. else
  83. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", env_from_addr, data)
  84. task:insert_result('DYN_RL', 0.0, data)
  85. end
  86. end
  87. local redis_ret_user = rspamd_redis_make_request(task,
  88. redis_params, -- connect params
  89. env_from_addr, -- hash key
  90. false, -- is write
  91. redis_cb_user, --callback
  92. 'HGET', -- command
  93. {'RL_VALUE', env_from_addr} -- arguments
  94. )
  95. if not redis_ret_user then
  96. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  97. end
  98. return true
  99. end,
  100. priority = 20
  101. })
  102. rspamd_config:register_symbol({
  103. name = 'NO_LOG_STAT',
  104. type = 'postfilter',
  105. callback = function(task)
  106. local from = task:get_header('From')
  107. if from and (from == 'monitoring-system@everycloudtech.us' or from == 'watchdog@localhost') then
  108. task:set_flag('no_log')
  109. task:set_flag('no_stat')
  110. end
  111. end
  112. })