From 0f943788427a6e7eefd963e17822952ad0e99a79 Mon Sep 17 00:00:00 2001 From: angus Date: Fri, 1 Apr 2016 17:18:13 +0200 Subject: [PATCH] All email-related services now logs in /var/log/mail/ and fail2ban config has been updated accordingly. Added also a check before copying spamassassin rule file. --- start-mailserver.sh | 23 ++++++++++++++++------- test/tests.bats | 22 +++++++++++----------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/start-mailserver.sh b/start-mailserver.sh index 010e09b8..45ca5259 100644 --- a/start-mailserver.sh +++ b/start-mailserver.sh @@ -222,7 +222,6 @@ fi echo "Fixing permissions" chown -R 5000:5000 /var/mail -mkdir -p /var/log/clamav && chown -R clamav:root /var/log/clamav chown postfix.sasl /etc/sasldb2 echo "Creating /etc/mailname" @@ -232,14 +231,16 @@ echo "Configuring Spamassassin" SA_TAG=${SA_TAG:="2.0"} && sed -i -r 's/^\$sa_tag_level_deflt (.*);/\$sa_tag_level_deflt = '$SA_TAG';/g' /etc/amavis/conf.d/20-debian_defaults SA_TAG2=${SA_TAG2:="6.31"} && sed -i -r 's/^\$sa_tag2_level_deflt (.*);/\$sa_tag2_level_deflt = '$SA_TAG2';/g' /etc/amavis/conf.d/20-debian_defaults SA_KILL=${SA_KILL:="6.31"} && sed -i -r 's/^\$sa_kill_level_deflt (.*);/\$sa_kill_level_deflt = '$SA_KILL';/g' /etc/amavis/conf.d/20-debian_defaults -cp /tmp/spamassassin/rules.cf /etc/spamassassin/ +test -e /tmp/spamassassin/rules.cf && cp /tmp/spamassassin/rules.cf /etc/spamassassin/ echo "Configuring fail2ban" # enable filters -perl -i -0pe 's/(\[postfix\]\n\n).*\n/\1enabled = true\n/' /etc/fail2ban/jail.conf -perl -i -0pe 's/(\[couriersmtp\]\n\n).*\n/\1enabled = true\n/' /etc/fail2ban/jail.conf -perl -i -0pe 's/(\[courierauth\]\n\n).*\n/\1enabled = true\n/' /etc/fail2ban/jail.conf -perl -i -0pe 's/(\[sasl\]\n\n).*\n/\1enabled = true\n/' /etc/fail2ban/jail.conf +awk 'BEGIN{unit=0}{if ($1=="[postfix]" || $1=="[couriersmtp]" || $1=="[courierauth]" || $1=="[sasl]") {unit=1;} + if ($1=="enabled" && unit==1) $3="true"; + else if ($1=="logpath" && unit==1) $3="/var/log/mail/mail.log"; + print; + if (unit==1 && $1~/\[/ && $1!~/postfix|couriersmtp|courierauth|sasl/) unit=0; +}'/etc/fail2ban/jail.conf > /tmp/jail.conf.new && mv /tmp/jail.conf.new /etc/fail2ban/jail.conf && rm -f /tmp/jail.conf.new # increase ban time and find time to 3h sed -i "/^bantime *=/c\bantime = 10800" /etc/fail2ban/jail.conf @@ -251,6 +252,14 @@ echo "ignoreregex =" >> /etc/fail2ban/filter.d/postfix-sasl.conf # continue to write the log information in the newly created file after rotating the old log file sed -i -r "/^#?compress/c\compress\ncopytruncate" /etc/logrotate.conf +# Setup logging +mkdir -p /var/log/mail && chown syslog:root /var/log/mail +touch /var/log/mail/clamav.log && chown -R clamav:root /var/log/mail/clamav.log +touch /var/log/mail/freshclam.log && chown -R clamav:root /var/log/mail/freshclam.log +sed -i -r 's|/var/log/mail|/var/log/mail/mail|g' /etc/rsyslog.d/50-default.conf +sed -i -r 's|LogFile /var/log/clamav/|LogFile /var/log/mail/|g' /etc/clamav/clamd.conf +sed -i -r 's|UpdateLogFile /var/log/clamav/|UpdateLogFile /var/log/mail/|g' /etc/clamav/freshclam.conf + echo "Starting daemons" cron /etc/init.d/rsyslog start @@ -285,4 +294,4 @@ echo "Listing SASL users" sasldblistusers2 echo "Starting..." -tail -f /var/log/mail.log +tail -f /var/log/mail/mail.log diff --git a/test/tests.bats b/test/tests.bats index 72c569b5..3aadc551 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -140,13 +140,13 @@ } @test "checking smtp: delivers mail to existing account" { - run docker exec mail /bin/sh -c "grep 'status=sent (delivered to maildir)' /var/log/mail.log | wc -l" + run docker exec mail /bin/sh -c "grep 'status=sent (delivered to maildir)' /var/log/mail/mail.log | wc -l" [ "$status" -eq 0 ] [ "$output" -eq 2 ] } @test "checking smtp: delivers mail to existing alias" { - run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail.log | grep 'status=sent' | wc -l" + run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" [ "$status" -eq 0 ] [ "$output" = 1 ] } @@ -158,25 +158,25 @@ } @test "checking smtp: rejects mail to unknown user" { - run docker exec mail /bin/sh -c "grep ': Recipient address rejected: User unknown in virtual mailbox table' /var/log/mail.log | wc -l" + run docker exec mail /bin/sh -c "grep ': Recipient address rejected: User unknown in virtual mailbox table' /var/log/mail/mail.log | wc -l" [ "$status" -eq 0 ] [ "$output" = 1 ] } @test "checking smtp: redirects mail to external alias" { - run docker exec mail /bin/sh -c "grep -- '-> ' /var/log/mail.log | wc -l" + run docker exec mail /bin/sh -c "grep -- '-> ' /var/log/mail/mail.log | wc -l" [ "$status" -eq 0 ] [ "$output" = 1 ] } @test "checking smtp: rejects spam" { - run docker exec mail /bin/sh -c "grep 'Blocked SPAM' /var/log/mail.log | grep spam@external.tld | wc -l" + run docker exec mail /bin/sh -c "grep 'Blocked SPAM' /var/log/mail/mail.log | grep spam@external.tld | wc -l" [ "$status" -eq 0 ] [ "$output" = 1 ] } @test "checking smtp: rejects virus" { - run docker exec mail /bin/sh -c "grep 'Blocked INFECTED' /var/log/mail.log | grep virus@external.tld | wc -l" + run docker exec mail /bin/sh -c "grep 'Blocked INFECTED' /var/log/mail/mail.log | grep virus@external.tld | wc -l" [ "$status" -eq 0 ] [ "$output" = 1 ] } @@ -349,14 +349,14 @@ [ "$output" = "0 1 * * * /usr/bin/freshclam --quiet" ] } -@test "checking system: /var/log/mail.log is error free" { - run docker exec mail grep 'non-null host address bits in' /var/log/mail.log +@test "checking system: /var/log/mail/mail.log is error free" { + run docker exec mail grep 'non-null host address bits in' /var/log/mail/mail.log [ "$status" -eq 1 ] - run docker exec mail grep ': error:' /var/log/mail.log + run docker exec mail grep ': error:' /var/log/mail/mail.log [ "$status" -eq 1 ] - run docker exec mail_pop3 grep 'non-null host address bits in' /var/log/mail.log + run docker exec mail_pop3 grep 'non-null host address bits in' /var/log/mail/mail.log [ "$status" -eq 1 ] - run docker exec mail_pop3 grep ': error:' /var/log/mail.log + run docker exec mail_pop3 grep ': error:' /var/log/mail/mail.log [ "$status" -eq 1 ] }