rspamd.local.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. local monitoring_hosts = rspamd_config:add_map{
  10. url = "/etc/rspamd/custom/monitoring_nolog.map",
  11. description = "Monitoring hosts",
  12. type = "regexp"
  13. }
  14. rspamd_config:register_symbol({
  15. name = 'SMTP_ACCESS',
  16. type = 'postfilter',
  17. callback = function(task)
  18. local util = require("rspamd_util")
  19. local rspamd_logger = require "rspamd_logger"
  20. local rspamd_ip = require 'rspamd_ip'
  21. local uname = task:get_user()
  22. local limited_access = task:get_symbol("SMTP_LIMITED_ACCESS")
  23. if not uname then
  24. return false
  25. end
  26. if not limited_access then
  27. return false
  28. end
  29. local hash_key = 'SMTP_ALLOW_NETS_' .. uname
  30. local redis_params = rspamd_parse_redis_server('smtp_access')
  31. local ip = task:get_from_ip()
  32. if ip == nil or not ip:is_valid() then
  33. return false
  34. end
  35. local from_ip_string = tostring(ip)
  36. smtp_access_table = {from_ip_string}
  37. local maxbits = 128
  38. local minbits = 32
  39. if ip:get_version() == 4 then
  40. maxbits = 32
  41. minbits = 8
  42. end
  43. for i=maxbits,minbits,-1 do
  44. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  45. table.insert(smtp_access_table, nip)
  46. end
  47. local function smtp_access_cb(err, data)
  48. if err then
  49. rspamd_logger.infox(rspamd_config, "smtp_access query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  50. return false
  51. else
  52. rspamd_logger.infox(rspamd_config, "checking ip %s for smtp_access in %s", from_ip_string, hash_key)
  53. for k,v in pairs(data) do
  54. if (v and v ~= userdata and v == '1') then
  55. rspamd_logger.infox(rspamd_config, "found ip in smtp_access map")
  56. task:insert_result(true, 'SMTP_ACCESS', 0.0, from_ip_string)
  57. return true
  58. end
  59. end
  60. rspamd_logger.infox(rspamd_config, "couldnt find ip in smtp_access map")
  61. task:insert_result(true, 'SMTP_ACCESS', 999.0, from_ip_string)
  62. return true
  63. end
  64. end
  65. table.insert(smtp_access_table, 1, hash_key)
  66. local redis_ret_user = rspamd_redis_make_request(task,
  67. redis_params, -- connect params
  68. hash_key, -- hash key
  69. false, -- is write
  70. smtp_access_cb, --callback
  71. 'HMGET', -- command
  72. smtp_access_table -- arguments
  73. )
  74. if not redis_ret_user then
  75. rspamd_logger.infox(rspamd_config, "cannot check smtp_access redis map")
  76. end
  77. end,
  78. priority = 10
  79. })
  80. rspamd_config:register_symbol({
  81. name = 'POSTMASTER_HANDLER',
  82. type = 'prefilter',
  83. callback = function(task)
  84. local rcpts = task:get_recipients('smtp')
  85. local rspamd_logger = require "rspamd_logger"
  86. local lua_util = require "lua_util"
  87. local from = task:get_from(1)
  88. -- not applying to mails with more than one rcpt to avoid bypassing filters by addressing postmaster
  89. if rcpts and #rcpts == 1 then
  90. for _,rcpt in ipairs(rcpts) do
  91. local rcpt_split = rspamd_str_split(rcpt['addr'], '@')
  92. if #rcpt_split == 2 then
  93. if rcpt_split[1] == 'postmaster' then
  94. task:set_pre_result('accept', 'whitelisting postmaster smtp rcpt')
  95. return
  96. end
  97. end
  98. end
  99. end
  100. if from then
  101. for _,fr in ipairs(from) do
  102. local fr_split = rspamd_str_split(fr['addr'], '@')
  103. if #fr_split == 2 then
  104. if fr_split[1] == 'postmaster' and task:get_user() then
  105. -- no whitelist, keep signatures
  106. task:insert_result(true, 'POSTMASTER_FROM', -2500.0)
  107. return
  108. end
  109. end
  110. end
  111. end
  112. end,
  113. priority = 10
  114. })
  115. rspamd_config:register_symbol({
  116. name = 'KEEP_SPAM',
  117. type = 'prefilter',
  118. callback = function(task)
  119. local util = require("rspamd_util")
  120. local rspamd_logger = require "rspamd_logger"
  121. local rspamd_ip = require 'rspamd_ip'
  122. local uname = task:get_user()
  123. if uname then
  124. return false
  125. end
  126. local redis_params = rspamd_parse_redis_server('keep_spam')
  127. local ip = task:get_from_ip()
  128. if ip == nil or not ip:is_valid() then
  129. return false
  130. end
  131. local from_ip_string = tostring(ip)
  132. ip_check_table = {from_ip_string}
  133. local maxbits = 128
  134. local minbits = 32
  135. if ip:get_version() == 4 then
  136. maxbits = 32
  137. minbits = 8
  138. end
  139. for i=maxbits,minbits,-1 do
  140. local nip = ip:apply_mask(i):to_string() .. "/" .. i
  141. table.insert(ip_check_table, nip)
  142. end
  143. local function keep_spam_cb(err, data)
  144. if err then
  145. rspamd_logger.infox(rspamd_config, "keep_spam query request for ip %s returned invalid or empty data (\"%s\") or error (\"%s\")", ip, data, err)
  146. return false
  147. else
  148. for k,v in pairs(data) do
  149. if (v and v ~= userdata and v == '1') then
  150. rspamd_logger.infox(rspamd_config, "found ip in keep_spam map, setting pre-result")
  151. task:set_pre_result('accept', 'ip matched with forward hosts')
  152. end
  153. end
  154. end
  155. end
  156. table.insert(ip_check_table, 1, 'KEEP_SPAM')
  157. local redis_ret_user = rspamd_redis_make_request(task,
  158. redis_params, -- connect params
  159. 'KEEP_SPAM', -- hash key
  160. false, -- is write
  161. keep_spam_cb, --callback
  162. 'HMGET', -- command
  163. ip_check_table -- arguments
  164. )
  165. if not redis_ret_user then
  166. rspamd_logger.infox(rspamd_config, "cannot check keep_spam redis map")
  167. end
  168. end,
  169. priority = 19
  170. })
  171. rspamd_config:register_symbol({
  172. name = 'TLS_HEADER',
  173. type = 'postfilter',
  174. callback = function(task)
  175. local rspamd_logger = require "rspamd_logger"
  176. local tls_tag = task:get_request_header('TLS-Version')
  177. if type(tls_tag) == 'nil' then
  178. task:set_milter_reply({
  179. add_headers = {['X-Last-TLS-Session-Version'] = 'None'}
  180. })
  181. else
  182. task:set_milter_reply({
  183. add_headers = {['X-Last-TLS-Session-Version'] = tostring(tls_tag)}
  184. })
  185. end
  186. end,
  187. priority = 12
  188. })
  189. rspamd_config:register_symbol({
  190. name = 'TAG_MOO',
  191. type = 'postfilter',
  192. callback = function(task)
  193. local util = require("rspamd_util")
  194. local rspamd_logger = require "rspamd_logger"
  195. local redis_params = rspamd_parse_redis_server('taghandler')
  196. local rspamd_http = require "rspamd_http"
  197. local rcpts = task:get_recipients('smtp')
  198. local lua_util = require "lua_util"
  199. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  200. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  201. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  202. local tag = tagged_rcpt[1].options[1]
  203. rspamd_logger.infox("found tag: %s", tag)
  204. local action = task:get_metric_action('default')
  205. rspamd_logger.infox("metric action now: %s", action)
  206. if action ~= 'no action' and action ~= 'greylist' then
  207. rspamd_logger.infox("skipping tag handler for action: %s", action)
  208. return true
  209. end
  210. local function http_callback(err_message, code, body, headers)
  211. if body ~= nil and body ~= "" then
  212. rspamd_logger.infox(rspamd_config, "expanding rcpt to \"%s\"", body)
  213. local function tag_callback_subject(err, data)
  214. if err or type(data) ~= 'string' then
  215. rspamd_logger.infox(rspamd_config, "subject tag handler rcpt %s returned invalid or empty data (\"%s\") or error (\"%s\") - trying subfolder tag handler...", body, data, err)
  216. local function tag_callback_subfolder(err, data)
  217. if err or type(data) ~= 'string' then
  218. rspamd_logger.infox(rspamd_config, "subfolder tag handler for rcpt %s returned invalid or empty data (\"%s\") or error (\"%s\")", body, data, err)
  219. else
  220. rspamd_logger.infox("Add X-Moo-Tag header")
  221. task:set_milter_reply({
  222. add_headers = {['X-Moo-Tag'] = 'YES'}
  223. })
  224. end
  225. end
  226. local redis_ret_subfolder = rspamd_redis_make_request(task,
  227. redis_params, -- connect params
  228. body, -- hash key
  229. false, -- is write
  230. tag_callback_subfolder, --callback
  231. 'HGET', -- command
  232. {'RCPT_WANTS_SUBFOLDER_TAG', body} -- arguments
  233. )
  234. if not redis_ret_subfolder then
  235. rspamd_logger.infox(rspamd_config, "cannot make request to load tag handler for rcpt")
  236. end
  237. else
  238. rspamd_logger.infox("user wants subject modified for tagged mail")
  239. local sbj = task:get_header('Subject')
  240. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  241. task:set_milter_reply({
  242. remove_headers = {['Subject'] = 1},
  243. add_headers = {['Subject'] = new_sbj}
  244. })
  245. end
  246. end
  247. local redis_ret_subject = rspamd_redis_make_request(task,
  248. redis_params, -- connect params
  249. body, -- hash key
  250. false, -- is write
  251. tag_callback_subject, --callback
  252. 'HGET', -- command
  253. {'RCPT_WANTS_SUBJECT_TAG', body} -- arguments
  254. )
  255. if not redis_ret_subject then
  256. rspamd_logger.infox(rspamd_config, "cannot make request to load tag handler for rcpt")
  257. end
  258. end
  259. end
  260. if rcpts and #rcpts == 1 then
  261. for _,rcpt in ipairs(rcpts) do
  262. local rcpt_split = rspamd_str_split(rcpt['addr'], '@')
  263. if #rcpt_split == 2 then
  264. if rcpt_split[1] == 'postmaster' then
  265. rspamd_logger.infox(rspamd_config, "not expanding postmaster alias")
  266. else
  267. rspamd_http.request({
  268. task=task,
  269. url='http://nginx:8081/aliasexp.php',
  270. body='',
  271. callback=http_callback,
  272. headers={Rcpt=rcpt['addr']},
  273. })
  274. end
  275. end
  276. end
  277. end
  278. end
  279. end,
  280. priority = 19
  281. })
  282. rspamd_config:register_symbol({
  283. name = 'DYN_RL_CHECK',
  284. type = 'prefilter',
  285. callback = function(task)
  286. local util = require("rspamd_util")
  287. local redis_params = rspamd_parse_redis_server('dyn_rl')
  288. local rspamd_logger = require "rspamd_logger"
  289. local envfrom = task:get_from(1)
  290. local uname = task:get_user()
  291. if not envfrom or not uname then
  292. return false
  293. end
  294. local uname = uname:lower()
  295. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  296. local function redis_cb_user(err, data)
  297. if err or type(data) ~= 'string' then
  298. 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)
  299. local function redis_key_cb_domain(err, data)
  300. if err or type(data) ~= 'string' then
  301. 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)
  302. else
  303. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  304. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  305. end
  306. end
  307. local redis_ret_domain = rspamd_redis_make_request(task,
  308. redis_params, -- connect params
  309. env_from_domain, -- hash key
  310. false, -- is write
  311. redis_key_cb_domain, --callback
  312. 'HGET', -- command
  313. {'RL_VALUE', env_from_domain} -- arguments
  314. )
  315. if not redis_ret_domain then
  316. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  317. end
  318. else
  319. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  320. task:insert_result('DYN_RL', 0.0, data, uname)
  321. end
  322. end
  323. local redis_ret_user = rspamd_redis_make_request(task,
  324. redis_params, -- connect params
  325. uname, -- hash key
  326. false, -- is write
  327. redis_cb_user, --callback
  328. 'HGET', -- command
  329. {'RL_VALUE', uname} -- arguments
  330. )
  331. if not redis_ret_user then
  332. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  333. end
  334. return true
  335. end,
  336. flags = 'empty',
  337. priority = 20
  338. })
  339. rspamd_config:register_symbol({
  340. name = 'NO_LOG_STAT',
  341. type = 'postfilter',
  342. callback = function(task)
  343. local from = task:get_header('From')
  344. if from and monitoring_hosts:get_key(from) then
  345. task:set_flag('no_log')
  346. task:set_flag('no_stat')
  347. end
  348. end
  349. })