{"id":1464,"date":"2016-07-03T21:18:45","date_gmt":"2016-07-03T20:18:45","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=1464"},"modified":"2017-09-17T17:34:23","modified_gmt":"2017-09-17T16:34:23","slug":"header_checks-and-spamassassin-headers-in-postfix-2-6","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2016\/07\/header_checks-and-spamassassin-headers-in-postfix-2-6\/","title":{"rendered":"header_checks and Spamassassin headers in Postfix 2.6"},"content":{"rendered":"<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-1472 size-medium\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-300x159.png\" alt=\"images.duckduckgo.com\" width=\"300\" height=\"159\" srcset=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-300x159.png 300w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-100x53.png 100w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-150x80.png 150w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-200x106.png 200w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_-450x239.png 450w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/images.duckduckgo.com_.png 545w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>I&#8217;ve had this mail server of mine for some time. I was an early adopter of Gmail back then, but as years went on and it became obvious that messages were data-mined by Gmail, I eventually started running my own Postfix server. Not just for me, but for family and eventually other people. Now, the thing is that some folks insist on having their emails forwarded to another service, like Gmail, Yahoo, etc. I can understand that. The problem is that if such mailbox receives a lot of spam messages, those messages get forwarded to Gmail and Yahoo as well, and as a result, my mail server can get bad reputation because of that &#8211; I can&#8217;t just explain to the other side that those spams are only forwarded.<\/p>\n<p style=\"text-align: justify;\">I&#8217;m using Spamassassin to mark spam, but all it can do is to mark the messages for users&#8217; MUAs, it can&#8217;t do anything else, like drop or reject unsolicited bulk email so that it doesn&#8217;t get forwarded. I&#8217;ve used Amavis somewhere in the past and it could have solved the probl\u00e9m here, too, but here it felt as too large a gun for the task. All I wanted was to prevent the most obvious spam with high score points from being forwarded. So I created a file with a regular expression to catch all messages marked as Spamassassin with help of X-Spam-Level header.<\/p>\n<pre style=\"text-align: justify;\">#cat \/etc\/postfix\/header_checks\r\n\/^X-Spam-Level: \\*\\*\\*\\*\\*\\*\\*.*\/ HOLD custom spam rule<\/pre>\n<p style=\"text-align: justify;\">and uncommented this line in:<\/p>\n<pre style=\"text-align: justify;\">#grep header_checks \/etc\/postfix\/main.cf\r\nheader_checks = regexp:\/etc\/postfix\/header_checks<\/pre>\n<p style=\"text-align: justify;\">The above mentioned HOLD action will put the messages into the hold queue for further inspection. Other option is to REJECT the messages (<a href=\"http:\/\/www.postfix.org\/header_checks.5.html\">more here <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/external_link.png\" alt=\"external_link\" width=\"10\" height=\"10\" \/><\/a>).<\/p>\n<p style=\"text-align: justify;\">To my dismay, it just didn&#8217;t work when I gave it a try with help of Gtube. What I didn&#8217;t realize was that <em>header_checks<\/em> happen while message is being received. Spamassassin, however, works as a milter that adds extra headers later, so it couldn&#8217;t work. There is a Postfix feature designed to solve this problem &#8211; <a href=\"http:\/\/www.postfix.org\/postconf.5.html#milter_header_checks\">milter_header_checks<\/a> &#8211; which does the same thing, except it takes headers added by milters into account, too. The only tiny drawback was that this feature was added to Postfix 2.7 and my Centos 6 had Postfix 2.6 running. There was a patch on Postfix page which backported <em>milter_header_checks<\/em> into version 2.6, but I just didn&#8217;t have enough courage to go for it. Instead I used the workaround discussed <a href=\"http:\/\/postfix.1071664.n5.nabble.com\/problem-with-smtpd-milter-and-header-checks-td35027.html\">here<\/a> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/external_link.png\" alt=\"external_link\" width=\"10\" height=\"10\" \/> (many thanks). The trick is to create another service in <em>master.cf<\/em> and use it as a content filter for the main smtp service. That way, the Spamassassin headers get applied and on the second run, they get noticed by <em>header_checks<\/em>.<\/p>\n<pre>#master.cf\r\nsmtp      inet  n       -       n       -       -       smtpd\r\n  -o content_filter=smtp:127.0.0.1:10025\r\n127.0.0.1:10025 inet  n -       n       -       -       smtpd\r\n  -o content_filter=\r\n<\/pre>\n<p>I also had to add permit_mynetworks in recipient restrictions, but that was probably relevant just to my particular setup.<\/p>\n<pre>#main.cf\r\nsmtpd_recipient_restrictions = permit_mynetworks, \r\n<\/pre>\n<p>After the postfix reload, the spam messages with score higher than defined in <em>\/etc\/postfix\/header_checks<\/em> finally got caught and stopped.<\/p>\n<p><strong>Update 21. 2. 2017:<\/strong><\/p>\n<p>There was one unintended side effect with the above mentioned solution &#8211; all forwarded emails were duplicated and arrived into the final mailbox twice. When there was forwarding set in the <em>aliases<\/em> file, the rule got applied on both smtpd service, the external one, as well as the internal one, where the <em>header_checks<\/em> happened. So this behaviour had to be suppressed using <em>receive_override_options=no_address_mappings<\/em> option to prevent this unintended duplication:<\/p>\n<pre>smtp      inet  n       -       n       -       -       smtpd\r\n  -o content_filter=smtp:127.0.0.1:10025\r\n  -o receive_override_options=no_address_mappings\r\n127.0.0.1:10025 inet  n -       n       -       -       smtpd\r\n  -o content_filter=\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had this mail server of mine for some time. I was an early adopter of Gmail back then, but as years went on and it became obvious that messages were data-mined by Gmail, I eventually started running my own &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2016\/07\/header_checks-and-spamassassin-headers-in-postfix-2-6\/\"> <span class=\"screen-reader-text\">header_checks and Spamassassin headers in Postfix 2.6<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[39,18,38],"class_list":["post-1464","post","type-post","status-publish","format-standard","hentry","category-operatingsystems","tag-centos","tag-linux","tag-networking"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=1464"}],"version-history":[{"count":27,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1564,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1464\/revisions\/1564"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}