rspamd.local.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. local function remove_moo_tag()
  202. local moo_tag_header = task:get_header('X-Moo-Tag', false)
  203. if moo_tag_header then
  204. task:set_milter_reply({
  205. remove_headers = {['X-Moo-Tag'] = 0},
  206. })
  207. end
  208. return true
  209. end
  210. if tagged_rcpt and tagged_rcpt[1].options and mailcow_domain then
  211. local tag = tagged_rcpt[1].options[1]
  212. rspamd_logger.infox("found tag: %s", tag)
  213. local action = task:get_metric_action('default')
  214. rspamd_logger.infox("metric action now: %s", action)
  215. if action ~= 'no action' and action ~= 'greylist' then
  216. rspamd_logger.infox("skipping tag handler for action: %s", action)
  217. remove_moo_tag()
  218. return true
  219. end
  220. local function http_callback(err_message, code, body, headers)
  221. if body ~= nil and body ~= "" then
  222. rspamd_logger.infox(rspamd_config, "expanding rcpt to \"%s\"", body)
  223. local function tag_callback_subject(err, data)
  224. if err or type(data) ~= 'string' then
  225. 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)
  226. local function tag_callback_subfolder(err, data)
  227. if err or type(data) ~= 'string' then
  228. rspamd_logger.infox(rspamd_config, "subfolder tag handler for rcpt %s returned invalid or empty data (\"%s\") or error (\"%s\")", body, data, err)
  229. remove_moo_tag()
  230. else
  231. rspamd_logger.infox("Add X-Moo-Tag header")
  232. task:set_milter_reply({
  233. add_headers = {['X-Moo-Tag'] = 'YES'}
  234. })
  235. end
  236. end
  237. local redis_ret_subfolder = rspamd_redis_make_request(task,
  238. redis_params, -- connect params
  239. body, -- hash key
  240. false, -- is write
  241. tag_callback_subfolder, --callback
  242. 'HGET', -- command
  243. {'RCPT_WANTS_SUBFOLDER_TAG', body} -- arguments
  244. )
  245. if not redis_ret_subfolder then
  246. rspamd_logger.infox(rspamd_config, "cannot make request to load tag handler for rcpt")
  247. remove_moo_tag()
  248. end
  249. else
  250. rspamd_logger.infox("user wants subject modified for tagged mail")
  251. local sbj = task:get_header('Subject')
  252. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  253. task:set_milter_reply({
  254. remove_headers = {
  255. ['Subject'] = 1,
  256. ['X-Moo-Tag'] = 0
  257. },
  258. add_headers = {['Subject'] = new_sbj}
  259. })
  260. end
  261. end
  262. local redis_ret_subject = rspamd_redis_make_request(task,
  263. redis_params, -- connect params
  264. body, -- hash key
  265. false, -- is write
  266. tag_callback_subject, --callback
  267. 'HGET', -- command
  268. {'RCPT_WANTS_SUBJECT_TAG', body} -- arguments
  269. )
  270. if not redis_ret_subject then
  271. rspamd_logger.infox(rspamd_config, "cannot make request to load tag handler for rcpt")
  272. remove_moo_tag()
  273. end
  274. end
  275. end
  276. if rcpts and #rcpts == 1 then
  277. for _,rcpt in ipairs(rcpts) do
  278. local rcpt_split = rspamd_str_split(rcpt['addr'], '@')
  279. if #rcpt_split == 2 then
  280. if rcpt_split[1] == 'postmaster' then
  281. rspamd_logger.infox(rspamd_config, "not expanding postmaster alias")
  282. remove_moo_tag()
  283. else
  284. rspamd_http.request({
  285. task=task,
  286. url='http://nginx:8081/aliasexp.php',
  287. body='',
  288. callback=http_callback,
  289. headers={Rcpt=rcpt['addr']},
  290. })
  291. end
  292. end
  293. end
  294. end
  295. else
  296. remove_moo_tag()
  297. end
  298. end,
  299. priority = 19
  300. })
  301. rspamd_config:register_symbol({
  302. name = 'BCC',
  303. type = 'postfilter',
  304. callback = function(task)
  305. local util = require("rspamd_util")
  306. local rspamd_http = require "rspamd_http"
  307. local rspamd_logger = require "rspamd_logger"
  308. local from_table = {}
  309. local rcpt_table = {}
  310. if task:has_symbol('ENCRYPTED_CHAT') then
  311. return -- stop
  312. end
  313. local send_mail = function(task, bcc_dest)
  314. local lua_smtp = require "lua_smtp"
  315. local function sendmail_cb(ret, err)
  316. if not ret then
  317. rspamd_logger.errx(task, 'BCC SMTP ERROR: %s', err)
  318. else
  319. rspamd_logger.infox(rspamd_config, "BCC SMTP SUCCESS TO %s", bcc_dest)
  320. end
  321. end
  322. if not bcc_dest then
  323. return -- stop
  324. end
  325. -- dot stuff content before sending
  326. local email_content = tostring(task:get_content())
  327. email_content = string.gsub(email_content, "\r\n%.", "\r\n..")
  328. -- send mail
  329. lua_smtp.sendmail({
  330. task = task,
  331. host = os.getenv("IPV4_NETWORK") .. '.253',
  332. port = 591,
  333. from = task:get_from(stp)[1].addr,
  334. recipients = bcc_dest,
  335. helo = 'bcc',
  336. timeout = 20,
  337. }, email_content, sendmail_cb)
  338. end
  339. -- determine from
  340. local from = task:get_from('smtp')
  341. if from then
  342. for _, a in ipairs(from) do
  343. table.insert(from_table, a['addr']) -- add this rcpt to table
  344. table.insert(from_table, '@' .. a['domain']) -- add this rcpts domain to table
  345. end
  346. else
  347. return -- stop
  348. end
  349. -- determine rcpts
  350. local rcpts = task:get_recipients('smtp')
  351. if rcpts then
  352. for _, a in ipairs(rcpts) do
  353. table.insert(rcpt_table, a['addr']) -- add this rcpt to table
  354. table.insert(rcpt_table, '@' .. a['domain']) -- add this rcpts domain to table
  355. end
  356. else
  357. return -- stop
  358. end
  359. local action = task:get_metric_action('default')
  360. rspamd_logger.infox("metric action now: %s", action)
  361. local function rcpt_callback(err_message, code, body, headers)
  362. if err_message == nil and code == 201 and body ~= nil then
  363. if action == 'no action' or action == 'add header' or action == 'rewrite subject' then
  364. send_mail(task, body)
  365. end
  366. end
  367. end
  368. local function from_callback(err_message, code, body, headers)
  369. if err_message == nil and code == 201 and body ~= nil then
  370. if action == 'no action' or action == 'add header' or action == 'rewrite subject' then
  371. send_mail(task, body)
  372. end
  373. end
  374. end
  375. if rcpt_table then
  376. for _,e in ipairs(rcpt_table) do
  377. rspamd_logger.infox(rspamd_config, "checking bcc for rcpt address %s", e)
  378. rspamd_http.request({
  379. task=task,
  380. url='http://nginx:8081/bcc.php',
  381. body='',
  382. callback=rcpt_callback,
  383. headers={Rcpt=e}
  384. })
  385. end
  386. end
  387. if from_table then
  388. for _,e in ipairs(from_table) do
  389. rspamd_logger.infox(rspamd_config, "checking bcc for from address %s", e)
  390. rspamd_http.request({
  391. task=task,
  392. url='http://nginx:8081/bcc.php',
  393. body='',
  394. callback=from_callback,
  395. headers={From=e}
  396. })
  397. end
  398. end
  399. return true
  400. end,
  401. priority = 20
  402. })
  403. rspamd_config:register_symbol({
  404. name = 'DYN_RL_CHECK',
  405. type = 'prefilter',
  406. callback = function(task)
  407. local util = require("rspamd_util")
  408. local redis_params = rspamd_parse_redis_server('dyn_rl')
  409. local rspamd_logger = require "rspamd_logger"
  410. local envfrom = task:get_from(1)
  411. local uname = task:get_user()
  412. if not envfrom or not uname then
  413. return false
  414. end
  415. local uname = uname:lower()
  416. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  417. local function redis_cb_user(err, data)
  418. if err or type(data) ~= 'string' then
  419. 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)
  420. local function redis_key_cb_domain(err, data)
  421. if err or type(data) ~= 'string' then
  422. 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)
  423. else
  424. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  425. task:insert_result('DYN_RL', 0.0, data, env_from_domain)
  426. end
  427. end
  428. local redis_ret_domain = rspamd_redis_make_request(task,
  429. redis_params, -- connect params
  430. env_from_domain, -- hash key
  431. false, -- is write
  432. redis_key_cb_domain, --callback
  433. 'HGET', -- command
  434. {'RL_VALUE', env_from_domain} -- arguments
  435. )
  436. if not redis_ret_domain then
  437. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  438. end
  439. else
  440. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", uname, data)
  441. task:insert_result('DYN_RL', 0.0, data, uname)
  442. end
  443. end
  444. local redis_ret_user = rspamd_redis_make_request(task,
  445. redis_params, -- connect params
  446. uname, -- hash key
  447. false, -- is write
  448. redis_cb_user, --callback
  449. 'HGET', -- command
  450. {'RL_VALUE', uname} -- arguments
  451. )
  452. if not redis_ret_user then
  453. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  454. end
  455. return true
  456. end,
  457. flags = 'empty',
  458. priority = 20
  459. })
  460. rspamd_config:register_symbol({
  461. name = 'NO_LOG_STAT',
  462. type = 'postfilter',
  463. callback = function(task)
  464. local from = task:get_header('From')
  465. if from and (monitoring_hosts:get_key(from) or from == "watchdog@localhost") then
  466. task:set_flag('no_log')
  467. task:set_flag('no_stat')
  468. end
  469. end
  470. })
  471. rspamd_config:register_symbol({
  472. name = 'MOO_FOOTER',
  473. type = 'prefilter',
  474. callback = function(task)
  475. local cjson = require "cjson"
  476. local lua_mime = require "lua_mime"
  477. local lua_util = require "lua_util"
  478. local rspamd_logger = require "rspamd_logger"
  479. local rspamd_http = require "rspamd_http"
  480. local envfrom = task:get_from(1)
  481. local uname = task:get_user()
  482. if not envfrom or not uname then
  483. return false
  484. end
  485. local uname = uname:lower()
  486. local env_from_domain = envfrom[1].domain:lower()
  487. local env_from_addr = envfrom[1].addr:lower()
  488. -- determine newline type
  489. local function newline(task)
  490. local t = task:get_newlines_type()
  491. if t == 'cr' then
  492. return '\r'
  493. elseif t == 'lf' then
  494. return '\n'
  495. end
  496. return '\r\n'
  497. end
  498. -- retrieve footer
  499. local function footer_cb(err_message, code, data, headers)
  500. if err or type(data) ~= 'string' then
  501. rspamd_logger.infox(rspamd_config, "domain wide footer request for user %s returned invalid or empty data (\"%s\") or error (\"%s\")", uname, data, err)
  502. else
  503. -- parse json string
  504. local footer = cjson.decode(data)
  505. if not footer then
  506. rspamd_logger.infox(rspamd_config, "parsing domain wide footer for user %s returned invalid or empty data (\"%s\") or error (\"%s\")", uname, data, err)
  507. else
  508. if footer and type(footer) == "table" and (footer.html and footer.html ~= "" or footer.plain and footer.plain ~= "") then
  509. rspamd_logger.infox(rspamd_config, "found domain wide footer for user %s: html=%s, plain=%s, vars=%s", uname, footer.html, footer.plain, footer.vars)
  510. if footer.skip_replies ~= 0 then
  511. in_reply_to = task:get_header_raw('in-reply-to')
  512. if in_reply_to then
  513. rspamd_logger.infox(rspamd_config, "mail is a reply - skip footer")
  514. return
  515. end
  516. end
  517. local envfrom_mime = task:get_from(2)
  518. local from_name = ""
  519. if envfrom_mime and envfrom_mime[1].name then
  520. from_name = envfrom_mime[1].name
  521. elseif envfrom and envfrom[1].name then
  522. from_name = envfrom[1].name
  523. end
  524. -- default replacements
  525. local replacements = {
  526. auth_user = uname,
  527. from_user = envfrom[1].user,
  528. from_name = from_name,
  529. from_addr = envfrom[1].addr,
  530. from_domain = envfrom[1].domain:lower()
  531. }
  532. -- add custom mailbox attributes
  533. if footer.vars and type(footer.vars) == "string" then
  534. local footer_vars = cjson.decode(footer.vars)
  535. if type(footer_vars) == "table" then
  536. for key, value in pairs(footer_vars) do
  537. replacements[key] = value
  538. end
  539. end
  540. end
  541. if footer.html and footer.html ~= "" then
  542. footer.html = lua_util.jinja_template(footer.html, replacements, true)
  543. end
  544. if footer.plain and footer.plain ~= "" then
  545. footer.plain = lua_util.jinja_template(footer.plain, replacements, true)
  546. end
  547. -- add footer
  548. local out = {}
  549. local rewrite = lua_mime.add_text_footer(task, footer.html, footer.plain) or {}
  550. local seen_cte
  551. local newline_s = newline(task)
  552. local function rewrite_ct_cb(name, hdr)
  553. if rewrite.need_rewrite_ct then
  554. if name:lower() == 'content-type' then
  555. local nct = string.format('%s: %s/%s; charset=utf-8',
  556. 'Content-Type', rewrite.new_ct.type, rewrite.new_ct.subtype)
  557. out[#out + 1] = nct
  558. -- update Content-Type header
  559. task:set_milter_reply({
  560. remove_headers = {['Content-Type'] = 0},
  561. })
  562. task:set_milter_reply({
  563. add_headers = {['Content-Type'] = string.format('%s/%s; charset=utf-8', rewrite.new_ct.type, rewrite.new_ct.subtype)}
  564. })
  565. return
  566. elseif name:lower() == 'content-transfer-encoding' then
  567. out[#out + 1] = string.format('%s: %s',
  568. 'Content-Transfer-Encoding', 'quoted-printable')
  569. -- update Content-Transfer-Encoding header
  570. task:set_milter_reply({
  571. remove_headers = {['Content-Transfer-Encoding'] = 0},
  572. })
  573. task:set_milter_reply({
  574. add_headers = {['Content-Transfer-Encoding'] = 'quoted-printable'}
  575. })
  576. seen_cte = true
  577. return
  578. end
  579. end
  580. out[#out + 1] = hdr.raw:gsub('\r?\n?$', '')
  581. end
  582. task:headers_foreach(rewrite_ct_cb, {full = true})
  583. if not seen_cte and rewrite.need_rewrite_ct then
  584. out[#out + 1] = string.format('%s: %s', 'Content-Transfer-Encoding', 'quoted-printable')
  585. end
  586. -- End of headers
  587. out[#out + 1] = newline_s
  588. if rewrite.out then
  589. for _,o in ipairs(rewrite.out) do
  590. out[#out + 1] = o
  591. end
  592. else
  593. out[#out + 1] = task:get_rawbody()
  594. end
  595. local out_parts = {}
  596. for _,o in ipairs(out) do
  597. if type(o) ~= 'table' then
  598. out_parts[#out_parts + 1] = o
  599. out_parts[#out_parts + 1] = newline_s
  600. else
  601. local removePrefix = "--\x0D\x0AContent-Type"
  602. if string.lower(string.sub(tostring(o[1]), 1, string.len(removePrefix))) == string.lower(removePrefix) then
  603. o[1] = string.sub(tostring(o[1]), string.len("--\x0D\x0A") + 1)
  604. end
  605. out_parts[#out_parts + 1] = o[1]
  606. if o[2] then
  607. out_parts[#out_parts + 1] = newline_s
  608. end
  609. end
  610. end
  611. task:set_message(out_parts)
  612. else
  613. rspamd_logger.infox(rspamd_config, "domain wide footer request for user %s returned invalid or empty data (\"%s\")", uname, data)
  614. end
  615. end
  616. end
  617. end
  618. -- fetch footer
  619. rspamd_http.request({
  620. task=task,
  621. url='http://nginx:8081/footer.php',
  622. body='',
  623. callback=footer_cb,
  624. headers={Domain=env_from_domain,Username=uname,From=env_from_addr},
  625. })
  626. return true
  627. end,
  628. priority = 1
  629. })