rspamd.local.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. modify_subject_map = rspamd_config:add_map({
  10. url = 'http://172.22.1.251:8081/tags.php',
  11. type = 'map',
  12. description = 'Map of users to use subject tags for'
  13. })
  14. auth_domain_map = rspamd_config:add_map({
  15. url = 'http://172.22.1.251:8081/authoritative.php',
  16. type = 'map',
  17. description = 'Map of domains we are authoritative for'
  18. })
  19. rspamd_config:register_symbol({
  20. name = 'TAG_MOO',
  21. type = 'postfilter',
  22. callback = function(task)
  23. local util = require("rspamd_util")
  24. local rspamd_logger = require "rspamd_logger"
  25. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  26. local user = task:get_recipients(0)[1]['user']
  27. local domain = task:get_recipients(0)[1]['domain']
  28. local rcpt = user .. '@' .. domain
  29. local authdomain = auth_domain_map:get_key(domain)
  30. if tagged_rcpt then
  31. local tag = tagged_rcpt[1].options[1]
  32. rspamd_logger.infox("found tag: %s", tag)
  33. local action = task:get_metric_action('default')
  34. rspamd_logger.infox("metric action now: %s", action)
  35. if action ~= 'no action' and action ~= 'greylist' then
  36. rspamd_logger.infox("skipping tag handler for action: %s", action)
  37. task:set_metric_action('default', action)
  38. return true
  39. end
  40. if authdomain then
  41. rspamd_logger.infox("found mailcow domain %s", domain)
  42. rspamd_logger.infox("querying tag settings for user %s", rcpt)
  43. if modify_subject_map:get_key(rcpt) then
  44. rspamd_logger.infox("user wants subject modified for tagged mail")
  45. local sbj = task:get_header('Subject')
  46. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  47. task:set_rmilter_reply({
  48. remove_headers = {['Subject'] = 1},
  49. add_headers = {['Subject'] = new_sbj}
  50. })
  51. else
  52. rspamd_logger.infox("Add X-Moo-Tag header")
  53. task:set_rmilter_reply({
  54. add_headers = {['X-Moo-Tag'] = 'YES'}
  55. })
  56. end
  57. else
  58. rspamd_logger.infox("skip delimiter handling for unknown domain")
  59. end
  60. return false
  61. end
  62. end,
  63. priority = 10
  64. })
  65. rspamd_config.MRAPTOR = {
  66. callback = function(task)
  67. local parts = task:get_parts()
  68. local rspamd_logger = require "rspamd_logger"
  69. local rspamd_regexp = require "rspamd_regexp"
  70. if parts then
  71. for _,p in ipairs(parts) do
  72. local mtype,subtype = p:get_type()
  73. local re = rspamd_regexp.create_cached('/(office|word|excel)/i')
  74. if re:match(subtype) then
  75. local content = tostring(p:get_content())
  76. local filename = p:get_filename()
  77. local file = os.tmpname()
  78. f = io.open(file, "a+")
  79. f:write(content)
  80. f:close()
  81. local scan = assert(io.popen('PATH=/usr/bin:/usr/local/bin mraptor ' .. file .. '> /dev/null 2>&1; echo $?', 'r'))
  82. local result = scan:read('*all')
  83. local exit_code = string.match(result, "%d+")
  84. rspamd_logger.infox(exit_code)
  85. scan:close()
  86. if exit_code == "20" then
  87. rspamd_logger.infox("Reject dangerous macro in office file " .. filename)
  88. task:set_pre_result(rspamd_actions['reject'], 'Dangerous macro in office file ' .. filename)
  89. end
  90. end
  91. end
  92. end
  93. end
  94. }