MTA 上でのウイルス対策

流通数は少ないしクライアント側でのウイルス対策は行っているので急いで必要でもないのだが、この手のものは設定しておく。なお、スパム対策については導入だけ行ってまともなテストをしていないので概要だけ載せる。

手順
amavisd-new をデーモン起動して postfix で受けたメールのウイルススキャンを行う。その際のウイルススキャンには ClamAV を使用するよう設定する。 amavisd-new は同時に SpamAssassin によるスパムチェックを行うようにすることも可能であるため、こちらのインストールも行っておく。
  1. USE フラグの設定とインストール
  2. amavisd-new / clamav の設定
  3. postfix の設定

USE フラグの設定とインストール

まず USE フラグを設定する。

パッケージ バージョン USE
app-antivirus/clamav 0.97
mail-filter/amavisd-new 2.6.3-r2 spamassassin
mail-filter/spamassassin 3.3.1-r4


spamassassin は単体でインストール可能であるが、 amavisd-new にフラグを立てると一緒に入れてくれる。


次にインストール --emerge-- する。

$ emerge -pv clamav amavisd-new

[ebuild  N    ] app-antivirus/clamav-0.97  USE="bzip2 iconv ipv6 -clamdtop -milter (-selinux)" 0 kB
[ebuild  N    ] mail-filter/spamassassin-3.3.1-r4  USE="berkdb ipv6 sqlite ssl -doc -ldap -mysql -postgres -qmail" 0 kB
[ebuild  N    ] mail-filter/amavisd-new-2.6.3-r2  USE="spamassassin -courier -dkim -ldap -milter -mysql -postgres -qmail -razor" 0 kB

$ sudo emerge -av clamav amavisd-new

amavisd-new / clamav の設定

amavisd の設定を行っていく。この設定ファイルちょっとわかりにくくて苦戦…。

$ sudo vi /etc/amavisd.conf


--- amavisd.conf.orig	2011-03-30 00:10:28.231744823 +0900
+++ amavisd.conf	2011-03-30 02:29:33.324318482 +0900
@@ -70,7 +70,7 @@
 # $mydomain is never used directly by the program.
 $mydomain = 'kakitsubata-ayame.jp';      # (no useful default)
 
# ホスト名を入れる。入れないと立ち上がらない…
-# $myhostname = 'host.example.com';  # fqdn of this host, default by uname(3)
+$myhostname = 'niboshi.kakitsubata-ayame.jp';  # fqdn of this host, default by uname(3)
 
 # Set the user and group to which the daemon will change if started as root
 # (otherwise just keeps the UID unchanged, and these settings have no effect):
@@ -111,8 +111,8 @@
 # POSTFIX, or SENDMAIL in dual-MTA setup, or EXIM V4
 # (set host and port number as required; host can be specified
 # as an IP address or a DNS name (A or CNAME, but MX is ignored)

# postfix との連携用にここを有効にしておく
-#$forward_method = 'smtp:[127.0.0.1]:10025';  # where to forward checked mail
-#$notify_method = $forward_method;            # where to submit notifications
+$forward_method = 'smtp:[127.0.0.1]:10025';  # where to forward checked mail
+$notify_method = $forward_method;            # where to submit notifications
 
 #$os_fingerprint_method = 'p0f:127.0.0.1:2345';  # query p0f-analyzer.pl
 
@@ -157,7 +157,7 @@
 # feeding amavisd, e.g. with Postfix the 'Max procs' field in the
 # master.cf file, like the '2' in the:  smtp-amavis unix - - n - 2 smtp
 #

# 適当でいいと思う
-$max_servers = 2;     # num of pre-forked children (2..30 is common), -m
+$max_servers = 10;     # num of pre-forked children (2..30 is common), -m
 $max_requests = 20;   # retire a child after that many accepts (default 20)
 
 $child_timeout=5*60;  # abort child if it does not complete its processing in
@@ -216,7 +216,7 @@
 # Section II - MTA specific (defaults should be ok)
 #
 
# 1ライン入れる…デフォルトが信用できないので明示的に
-#$insert_received_line = 1;       # behave like MTA: insert 'Received:' header
+$insert_received_line = 1;       # behave like MTA: insert 'Received:' header
 			          # (does not apply to sendmail/milter)
 			          # (default is true)
 
@@ -316,7 +316,7 @@
 # 3: server, client
 # 4: decompose parts
 # 5: more debug details

# ログレベル。ファイルに書き出す場合は少し上の方も変更
-$log_level = 2;		   # (defaults to 0), -d
+$log_level = 0;		   # (defaults to 0), -d
 
 # Customizable template for the most interesting log file entry (e.g. with
 # $log_level=0) (take care to properly quote Perl special characters like '\')
@@ -527,11 +527,21 @@
 #######
 
 # to explicitly list all (or most) possible contents category (ccat) keys:
+#%final_destiny_by_ccat = (
+#  CC_VIRUS,      D_DISCARD,
+#  CC_BANNED,     D_BOUNCE,
+#  CC_UNCHECKED,  D_PASS,
+#  CC_SPAM,       D_DISCARD,
+#  CC_BADH,       D_PASS,
+#  CC_OVERSIZED,  D_BOUNCE,
+#  CC_CLEAN,      D_PASS,
+#  CC_CATCHALL,   D_PASS,
+#);
 %final_destiny_by_ccat = (
   CC_VIRUS,      D_DISCARD,
   CC_BANNED,     D_BOUNCE,
   CC_UNCHECKED,  D_PASS,
# デフォルトだとスパムメールが止まる。あまり信用できないので通す。
-  CC_SPAM,       D_DISCARD,
+  CC_SPAM,       D_PASS,
   CC_BADH,       D_PASS,
   CC_OVERSIZED,  D_BOUNCE,
   CC_CLEAN,      D_PASS,
@@ -1940,10 +1950,10 @@
 # ['Sophos SAVI', \&sophos_savi ],
 
 # ### http://www.clamav.net/
# ClamAVウイルススキャンとの連携設定。コメントアウトされているので外す。
# また、ソケットファイルを clamd の設定に合わせて変更。
# /etc/clamd.conf の LocalSocket /var/run/clamav/clamd.sock に合わせること。
-# ['ClamAV-clamd',
-#   \&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd"],
-#   qr/\bOK$/m, qr/\bFOUND$/m,
-#   qr/^.*?: (?!Infected Archive)(.*) FOUND$/m ],
+['ClamAV-clamd',
+  \&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd.sock"],
+  qr/\bOK$/m, qr/\bFOUND$/m,
+  qr/^.*?: (?!Infected Archive)(.*) FOUND$/m ],
 # # NOTE: run clamd under the same user as amavisd, or run it under its own
 # #   uid such as clamav, add user clamav to the amavis group, and then add
 # #   AllowSupplementaryGroups to clamd.conf;


その後 clamav を amavis グループに追加。

$ sudo gpasswd -a clamav amavis

$ id clamav
uid=101(clamav) gid=409(clamav) groups=409(clamav),410(amavis)

postfix の設定

postfix から amavis に投げられるように main.cf と master.cf に追記していく。

$ sudo vi /etc/postfix/main.cf

content_filter = amavis:[127.0.0.1]:10024
receive_override_options = no_address_mappings
$ sudo vi /etc/postfix/master.cf
amavis             unix  -      -       -       -       2      smtp
        -o smtp_data_done_timeout=1200
        -o smtp_send_xforward_command=yes
        -o disable_dns_lookups=yes
        -o max_use=20
127.0.0.1:10025    inet  n      -       -       -       -      smtpd
        -o content_filter=
        -o local_recipient_maps=
        -o relay_recipient_maps=
        -o smtpd_restriction_classes=
        -o smtpd_client_restrictions=
        -o smtpd_helo_restrictions=
        -o smtpd_sender_restrictions=
        -o smtpd_recipient_restrictions=permit_mynetworks,reject
        -o mynetworks=127.0.0.0/8
        -o strict_rfc821_envelopes=yes
        -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
        -o smtpd_bind_address=127.0.0.1
        -o smtpd_error_sleep_time=0
        -o smtpd_soft_error_limit=1001
        -o smtpd_hard_error_limit=1000


その後、各種サービスのスタートおよびリスタートを行う。必要なら起動時に立ち上がるようにセットしておく。

$ sudo /etc/init.d/clamd start
$ sudo /etc/init.d/amavisd start
$ sudo /etc/init.d/postfix restart

$ sudo rc-update add clamd default
$ sudo rc-update add amavisd default
$ sudo rc-update add postfix default
$ sudo rc-update add dovecot default

spamd は立ち上がっていなくてもスパムチェックをやってくれるようだ。

ここまでできたらもう一度テストする。設定をプラスしたので通らなくなったものもあるかもしれないからである。また リレイチェックウイルススキャンの稼働チェック も行っておくといいと思う。

最後に

参考にさせていただいたサイトはたくさんあるのだが、特に以下を参考にさせていただいた。感謝。