rspamd.local.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 = 'BCC',
  284. type = 'postfilter',
  285. callback = function(task)
  286. local util = require("rspamd_util")
  287. local rspamd_http = require "rspamd_http"
  288. local rspamd_logger = require "rspamd_logger"
  289. local from_table = {}
  290. local rcpt_table = {}
  291. if task:has_symbol('ENCRYPTED_CHAT') then
  292. return -- stop
  293. end
  294. local send_mail = function(task, bcc_dest)
  295. local lua_smtp = require "lua_smtp"
  296. local function sendmail_cb(ret, err)
  297. if not ret then
  298. rspamd_logger.errx(task, 'BCC SMTP ERROR: %s', err)
  299. else
  300. rspamd_logger.infox(rspamd_config, "BCC SMTP SUCCESS TO %s", bcc_dest)
  301. end
  302. end
  303. if not bcc_dest then
  304. return -- stop
  305. end
  306. lua_smtp.sendmail({
  307. task = task,
  308. host = os.getenv("IPV4_NETWORK") .. '.253',
  309. port = 591,
  310. from = task:get_from(stp)[1].addr,
  311. recipients = bcc_dest,
  312. helo = 'bcc',
  313. timeout = 10,
  314. }, task:get_content(), sendmail_cb)
  315. end
  316. -- determine from
  317. local from = task:get_from('smtp')
  318. if from then
  319. for _, a in ipairs(from) do
  320. table.insert(from_table, a['addr']) -- add this rcpt to table
  321. table.insert(from_table, '@' .. a['domain']) -- add this rcpts domain to table
  322. end
  323. else
  324. return -- stop
  325. end
  326. -- determine rcpts
  327. local rcpts = task:get_recipients('smtp')
  328. if rcpts then
  329. for _, a in ipairs(rcpts) do
  330. table.insert(rcpt_table, a['addr']) -- add this rcpt to table
  331. table.insert(rcpt_table, '@' .. a['domain']) -- add this rcpts domain to table
  332. end
  333. else
  334. return -- stop
  335. end
  336. local action = task:get_metric_action('default')
  337. rspamd_logger.infox("metric action now: %s", action)
  338. local function rcpt_callback(err_message, code, body, headers)
  339. if err_message == nil and code == 201 and body ~= nil then
  340. if action == 'no action' or action == 'add header' or action == 'rewrite subject' then
  341. send_mail(task, body)
  342. end
  343. end
  344. end
  345. local function from_callback(err_message, code, body, headers)
  346. if err_message == nil and code == 201 and body ~= nil then
  347. if action == 'no action' or action == 'add header' or action == 'rewrite subject' then
  348. send_mail(task, body)
  349. end
  350. end
  351. end
  352. if rcpt_table then
  353. for _,e in ipairs(rcpt_table) do
  354. rspamd_logger.infox(rspamd_config, "checking bcc for rcpt address %s", e)
  355. rspamd_http.request({
  356. task=task,
  357. url='http://nginx:8081/bcc.php',
  358. body='',
  359. callback=rcpt_callback,
  360. headers={Rcpt=e}
  361. })
  362. end
  363. end
  364. if from_table then
  365. for _,e in ipairs(from_table) do
  366. rspamd_logger.infox(rspamd_config, "checking bcc for from address %s", e)
  367. rspamd_http.request({
  368. task=task,
  369. url='http://nginx:8081/bcc.php',
  370. body='',
  371. callback=from_callback,
  372. headers={From=e}
  373. })
  374. end
  375. end
  376. return true
  377. end,
  378. priority = 20
  379. })
  380. rspamd_config:register_symbol({
  381. name = 'DYN_RL_CHECK',
  382. type = 'prefilter',
  383. callback = function(task)
  384. local util = require("rspamd_util")
  385. local redis_params = rspamd_parse_redis_server('dyn_rl')
  386. local rspamd_logger = require "rspamd_logger"
  387. local envfrom = task:get_from(1)
  388. local uname = task:get_user()
  389. if not envfrom or not uname then
  390. return false
  391. end
  392. local uname = uname:lower()
  393. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  394. local function redis_cb_user(err, data)
  395. if err or type(data) ~= 'string' then
  396. 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)
  397. local function redis_key_cb_domain(err, data)
  398. if err or type(data) ~= 'string' then
  399. 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)
  400. else
  401. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  402. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  403. end
  404. end
  405. local redis_ret_domain = rspamd_redis_make_request(task,
  406. redis_params, -- connect params
  407. env_from_domain, -- hash key
  408. false, -- is write
  409. redis_key_cb_domain, --callback
  410. 'HGET', -- command
  411. {'RL_VALUE', env_from_domain} -- arguments
  412. )
  413. if not redis_ret_domain then
  414. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  415. end
  416. else
  417. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  418. task:insert_result('DYN_RL', 0.0, data, uname)
  419. end
  420. end
  421. local redis_ret_user = rspamd_redis_make_request(task,
  422. redis_params, -- connect params
  423. uname, -- hash key
  424. false, -- is write
  425. redis_cb_user, --callback
  426. 'HGET', -- command
  427. {'RL_VALUE', uname} -- arguments
  428. )
  429. if not redis_ret_user then
  430. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  431. end
  432. return true
  433. end,
  434. flags = 'empty',
  435. priority = 20
  436. })
  437. rspamd_config:register_symbol({
  438. name = 'NO_LOG_STAT',
  439. type = 'postfilter',
  440. callback = function(task)
  441. local from = task:get_header('From')
  442. if from and (monitoring_hosts:get_key(from) or from == "watchdog@localhost") then
  443. task:set_flag('no_log')
  444. task:set_flag('no_stat')
  445. end
  446. end
  447. })