You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mid.lua 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --[[
  2. Copyright (c) 2016, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Copyright (c) 2016, Steve Freegard <steve@freegard.name>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ]]--
  14. local function mid_check_func(task)
  15. local mid = task:get_header('Message-ID')
  16. if not mid then return false end
  17. -- Check for 'bare' IP addresses in RHS
  18. if mid:find("@%d+%.%d+%.%d+%.%d+>$") then
  19. task:insert_result('MID_BARE_IP', 1.0)
  20. end
  21. -- Check for non-FQDN RHS
  22. if mid:find("@[^%.]+>?$") then
  23. task:insert_result('MID_RHS_NOT_FQDN', 1.0)
  24. end
  25. -- Check for missing <>'s
  26. if not mid:find('^<[^>]+>$') then
  27. task:insert_result('MID_MISSING_BRACKETS', 1.0)
  28. end
  29. -- Check for IP literal in RHS
  30. if mid:find("@%[%d+%.%d+%.%d+%.%d+%]") then
  31. task:insert_result('MID_RHS_IP_LITERAL', 1.0)
  32. end
  33. -- Check From address attributes against MID
  34. local from = task:get_from(2)
  35. local fd
  36. if (from and from[1] and from[1].domain and from[1].domain ~= '') then
  37. fd = from[1].domain:lower()
  38. local _,_,md = mid:find("@([^>]+)>?$")
  39. -- See if all or part of the From address
  40. -- can be found in the Message-ID
  41. if (mid:lower():find(from[1].addr:lower(),1,true)) then
  42. task:insert_result('MID_CONTAINS_FROM', 1.0)
  43. elseif (md and fd == md:lower()) then
  44. task:insert_result('MID_RHS_MATCH_FROM', 1.0)
  45. end
  46. end
  47. -- Check To address attributes against MID
  48. local to = task:get_recipients(2)
  49. if (to and to[1] and to[1].domain and to[1].domain ~= '') then
  50. local td = to[1].domain:lower()
  51. local _,_,md = mid:find("@([^>]+)>?$")
  52. -- Skip if from domain == to domain
  53. if ((fd and fd ~= td) or not fd) then
  54. -- See if all or part of the To address
  55. -- can be found in the Message-ID
  56. if (mid:lower():find(to[1].addr:lower(),1,true)) then
  57. task:insert_result('MID_CONTAINS_TO', 1.0)
  58. elseif (md and td == md:lower()) then
  59. task:insert_result('MID_RHS_MATCH_TO', 1.0)
  60. end
  61. end
  62. end
  63. end
  64. -- MID checks from Steve Freegard
  65. local check_mid_id = rspamd_config:register_symbol({
  66. name = 'CHECK_MID',
  67. score = 0.0,
  68. group = 'mid',
  69. type = 'callback',
  70. callback = mid_check_func
  71. })
  72. rspamd_config:register_virtual_symbol('MID_BARE_IP', 1.0, check_mid_id)
  73. rspamd_config:set_metric_symbol('MID_BARE_IP', 2.0, 'Message-ID RHS is a bare IP address', 'default', 'Message ID')
  74. rspamd_config:register_virtual_symbol('MID_RHS_NOT_FQDN', 1.0, check_mid_id)
  75. rspamd_config:set_metric_symbol('MID_RHS_NOT_FQDN', 0.5,
  76. 'Message-ID RHS is not a fully-qualified domain name', 'default', 'Message ID')
  77. rspamd_config:register_virtual_symbol('MID_MISSING_BRACKETS', 1.0, check_mid_id)
  78. rspamd_config:set_metric_symbol('MID_MISSING_BRACKETS', 0.5, 'Message-ID is missing <>\'s', 'default', 'Message ID')
  79. rspamd_config:register_virtual_symbol('MID_RHS_IP_LITERAL', 1.0, check_mid_id)
  80. rspamd_config:set_metric_symbol('MID_RHS_IP_LITERAL', 0.5, 'Message-ID RHS is an IP-literal', 'default', 'Message ID')
  81. rspamd_config:register_virtual_symbol('MID_CONTAINS_FROM', 1.0, check_mid_id)
  82. rspamd_config:set_metric_symbol('MID_CONTAINS_FROM', 1.0, 'Message-ID contains From address', 'default', 'Message ID')
  83. rspamd_config:register_virtual_symbol('MID_RHS_MATCH_FROM', 1.0, check_mid_id)
  84. rspamd_config:set_metric_symbol('MID_RHS_MATCH_FROM', 0.0,
  85. 'Message-ID RHS matches From domain', 'default', 'Message ID')
  86. rspamd_config:register_virtual_symbol('MID_CONTAINS_TO', 1.0, check_mid_id)
  87. rspamd_config:set_metric_symbol('MID_CONTAINS_TO', 1.0, 'Message-ID contains To address', 'default', 'Message ID')
  88. rspamd_config:register_virtual_symbol('MID_RHS_MATCH_TO', 1.0, check_mid_id)
  89. rspamd_config:set_metric_symbol('MID_RHS_MATCH_TO', 1.0, 'Message-ID RHS matches To domain', 'default', 'Message ID')