■概要

Clam AntiVirusSpamAssassin+maildropを使用してメールサーバー側でメールに対するウィルスチェックとスパムチェックを行う。
なお、メールサーバー(qmail)とClam AntiVirus、SpamAssassinとのやりとりはQmail-Scannerを仲介して行なう。
ここでは、自宅サーバーに構築したメールサーバーに外部から送られてきたメール及び、内部から送り出すメールのウィルスチェックを行い、ウィルスを検出したらメールを破棄するようにする。
また、外部から送られてきたメールについてはスパムチェックも行ない、スパムメールであると判断した場合、受信メールサーバーがIMAPの場合はスパムメール専用のメールボックスへ配送するようにし、受信メールサーバーがPOPの場合はメールクライアントで振分けられるようにメール件名に「***SPAM***」という文字列を付加するようにする。

メールサーバー(qmail)アンチウィルス(Clam AntiVirus)を導入済であること


■SpamAssassinインストール

[root@centos ~]# yum -y install spamassassin ← SpamAssassinインストール

■SpamAssassin起動

[root@centos ~]# /etc/rc.d/init.d/spamassassin start ← SpamAssassin起動
spamd を起動中: [15125] warn: config: created user preferences file: /root/.spamassassin/user_prefs

[root@centos ~]# chkconfig spamassassin on ← SpamAssassin自動起動設定

[root@centos ~]# chkconfig --list spamassassin ← SpamAssassin自動起動設定確認
spamassassin    0:off   1:off   2:on    3:on    4:on    5:on    6:off ← ランレベル2~5のonを確認

■SpamAssassin設定

SpamAssassinの初期設定では日本語のスパムメールに対応していないため、TLECで公開されている日本語のスパムメールに対応したSpamAssassin設定ファイルをセットアップする。
[root@centos ~]# vi spamassassin-update ← SpamAssassin設定ファイル最新化スクリプト作成
#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# TLEC(http://tlec.linux.or.jp/)よりSpamAssassin設定ファイル最新版ダウンロード
cd /etc/mail/spamassassin
wget -qN http://tlec.linux.or.jp/docs/user_prefs
cp user_prefs local.cf

# スパム判断したメールを添付形式にしないように設定
echo "report_safe 0" >> local.cf

# 受信メールサーバーがPOPの場合(IMAPサービスが未起動の場合)はメール件名に「***SPAM***」の文字列を付加するように設定
if [ -z "$(/usr/sbin/lsof -i:imap)" ] && [ -z "$(/usr/sbin/lsof -i:imaps)" ]; then
    echo "rewrite_header Subject ***SPAM***" >> local.cf
fi

# SpamAssassin再起動
/etc/rc.d/init.d/spamassassin restart > /dev/null

[root@centos ~]# chmod +x spamassassin-update ← SpamAssassin設定ファイル最新化スクリプトへ実行権限付加

[root@centos ~]# ./spamassassin-update ← SpamAssassin設定ファイル最新化スクリプト実行

[root@centos ~]# ll /etc/mail/spamassassin ← SpamAssassin設定ファイル確認
合計 352
-rw-r--r--  1 root root    935  6月 24 04:37 init.pre
-rw-r--r--  1 root root 164329 11月 28 14:01 local.cf ← SpamAssassin設定ファイル
-rw-r--r--  1 root root     62  6月 24 04:37 spamassassin-default.rc
-rwxr-xr-x  1 root root     35  6月 24 04:37 spamassassin-helper.sh
-rw-r--r--  1 root root     55  6月 24 04:37 spamassassin-spamc.rc
-rw-r--r--  1 root root 164315 11月 26 20:00 user_prefs

[root@centos ~]# mv spamassassin-update /etc/cron.daily/
 ← SpamAssassin設定ファイル最新化スクリプトを毎日自動実行されるディレクトリへ移動

[root@centos ~]# rm -f /etc/cron.d/sa-update ← デフォルトのSpamAssassin設定ファイル最新化スクリプトを削除

■Qmail-Scannerインストール

(1)maildropインストール
Qmail-Scannerがメールを解析するのに必要なreformimeを含んでいるmaildropをインストールする
[root@centos ~]# wget http://prdownloads.sourceforge.net/courier/maildrop-2.0.4.tar.bz2 ← maildropダウンロード

※最新版のURLはダウンロードページで確認すること

[root@centos ~]# rpmbuild -tb --clean maildrop-2.0.4.tar.bz2 ← maildropのRPMパッケージ作成
※時間がかかる
rpmbuildが「ビルド依存性の失敗」で異常終了した場合

[root@centos ~]# rpm -Uvh /usr/src/redhat/RPMS/i386/maildrop-2.0.4-1.i386.rpm ← 作成したRPMパッケージ(maildrop)をインストール
Preparing...                ########################################### [100%]
   1:maildrop               ########################################### [100%]

[root@centos ~]# rpm -Uvh /usr/src/redhat/RPMS/i386/maildrop-man-2.0.4-1.i386.rpm ← 作成したRPMパッケージ(maildrop-man)をインストール
Preparing...                ########################################### [100%]
   1:maildrop-man           ########################################### [100%]

[root@centos ~]# rm -f maildrop-2.0.4.tar.bz2 ← ダウンロードしたmaildropを削除

(2)qmailのQMAILQUEUE環境変数対応
qmailとQmail-Scannerの連携は、qmailのQMAILQUEUE環境変数を介して行われるため、qmailにQMAILQUEUE環境変数対応パッチを施行する。
※qmailにQMAILQUEUE環境変数対応パッチを施行したqmailを再インストールするため、先にqmail本体、各種必要パッチの入手及び、各種必要パッチの施行まで=qmailインストールの直前までの手順を行っておくこと⇒メールサーバー構築(qmail編)の「■qmailインストール」の「(2)qmailインストール」を参照
[root@centos qmail-1.03]# wget http://www.qmail.org/qmailqueue-patch ← QMAILQUEUE環境変数対応パッチダウンロード

[root@centos qmail-1.03]# patch < qmailqueue-patch ← QMAILQUEUE環境変数対応パッチ施行
patching file Makefile
Hunk #1 succeeded at 1487 (offset 4 lines).
patching file qmail.c

[root@centos qmail-1.03]# /etc/rc.d/init.d/qmail stop ← qmail停止
Shutting down qmail

[root@centos qmail-1.03]# make setup check ← qmail上書きインストール
・
・
・
./install
./instcheck

[root@centos qmail-1.03]# cd ← qmail展開先ディレクトリを抜ける

[root@centos ~]# /etc/rc.d/init.d/qmail start ← qmail起動
Starting qmail

※qmailインストール後始末(ダウンロードしたファイル、展開先ディレクトリ等を削除)
⇒メールサーバー構築(qmail編)の「■qmailインストール」の「(2)qmailインストール」を参照

(3)setuidgidインストール
Qmail-Scannerインストールに必要なsetuidgidコマンドをインストールする。
[root@centos ~]# wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz

[root@centos ~]# tar zxvf daemontools-0.76.tar.gz 

[root@centos ~]# cd admin/daemontools-0.76/

[root@centos daemontools-0.76]# sed -i 's/gcc -O2/gcc -O2 --include \/usr\/include\/errno\.h/g' src/conf-cc 

[root@centos daemontools-0.76]# ./package/compile 

[root@centos daemontools-0.76]# cp command/setuidgid /usr/local/bin/

[root@centos daemontools-0.76]# cd

[root@centos ~]# rm -rf admin/

[root@centos ~]# rm -f daemontools-0.76.tar.gz 

(4)Qmail-Scannerインストール
[root@centos ~]# yum -y install perl-suidperl ← Qmail-Scannerインストールに必要なperl-suidperlをインストール

[root@centos ~]# useradd -M -d /tmp -s /sbin/nologin qscand ← qscandユーザ作成

[root@centos ~]# wget http://jaist.dl.sourceforge.net/sourceforge/qmail-scanner/qmail-scanner-2.01.tgz ← Qmail-Scannerダウンロード
※最新版のURLはダウンロードページで確認すること

[root@centos ~]# tar zxvf qmail-scanner-2.01.tgz ← Qmail-Scanner展開

[root@centos ~]# cd qmail-scanner-2.01 ← Qmail-Scanner展開先ディレクトリへ移動

[root@centos qmail-scanner-2.01]# ./configure --install --scanners clamdscan,verbose_spamassassin --notify none ← configure
Building Qmail-Scanner 2.01...


                ***** NOTE ******

Qmail-Scanner doesn't have language translations for ja_JP.UTF-8, 
- so defaulting to english...

[Hit <RETURN> to continue] ← 空ENTER



This script will search your system for the virus scanners it knows
about, and will ensure that all external programs
qmail-scanner-queue.pl uses are explicitly pathed for performance
reasons.

Continue? ([Y]/N)
y ← y応答


/usr/bin/uudecode works as expected on system...



The following binaries and scanners were found on your system:

mimeunpacker=/usr/bin/reformime
uudecode=/usr/bin/uudecode

Content/Virus Scanners installed on your System

max-scan-size=100000000
clamdscan=/usr/bin/clamdscan (which means clamscan won't be used as clamdscan is better)
verbose_spamassassin=/usr/bin/spamc

Qmail-Scanner details.

log-details=syslog
log-crypto=0
fix-mime=2
ignore-eol-check=0
debug=1
notify=none
redundant-scanning=yes
virus-admin=System Anti-Virus Administrator 
local-domains='centos.wakuwakustation.com'
silent-viruses='klez','bugbear','hybris','yaha','braid','nimda','tanatos','sobig','winevar','palyh','fizzer','gibe','cailont',
'lovelorn','swen','dumaru','sober','hawawi','holar-i','mimail','poffer','bagle','worm.galil','mydoom','worm.sco','tanx','novarg','\@mm'
scanners="clamdscan","verbose_spamassassin"

If that looks correct, I will now generate qmail-scanner-queue.pl
for your system...
Continue? ([Y]/N) 
y ← y応答
Testing suid nature of /usr/local/bin/perl...
Looks OK...
Hit RETURN to create initial directory structure under /var/spool/qscan,
and install qmail-scanner-queue.pl under /var/qmail/bin: 
perlscanner: generate new DB file from /var/spool/qscan/quarantine-events.txt
perlscanner: total of 12 entries.

Finished installation of initial directory structure for Qmail-Scanner
under /var/spool/qscan and qmail-scanner-queue.pl under /var/qmail/bin. ← 空ENTER

Finished. Please read README(.html) and then go over the script
(/var/qmail/bin/qmail-scanner-queue.pl) to check paths/etc. 

"/var/qmail/bin/qmail-scanner-queue.pl -r" should return some well-known virus
definitions to show that the internal perlscanner component is working.

That's it!



              ****** FINAL TEST ******

Please log into an unpriviledged account and run 
/var/qmail/bin/qmail-scanner-queue.pl -g

If you see the error "Can't do setuid", or "Permission denied", then  
refer to the FAQ.

(e.g.  "setuidgid qmaild /var/qmail/bin/qmail-scanner-queue.pl -g")


That's it! To report success:

   % (echo 'First M. Last'; cat SYSDEF)|mail jhaar-s4vstats@crom.trimble.co.nz
Replace First M. Last with your name.

[root@centos qmail-scanner-2.01]# cd ← Qmail-Scanner展開先ディレクトリを抜ける

[root@centos ~]# mv ./qmail-scanner-2.01/contrib/test_installation.sh /root ← Qmail-Scannerテストツールを退避(後で使用するため)

[root@centos ~]# rm -rf qmail-scanner-2.01 ← Qmail-Scanner展開先ディレクトリ削除

[root@centos ~]# rm -f qmail-scanner-2.01.tgz ← ダウンロードしたQmail-Scanner削除

■Qmail-Scanner設定

[root@centos ~]# echo ":allow,QMAILQUEUE=\"/var/qmail/bin/qmail-scanner-queue.pl\"" >> /etc/tcp.smtp
 ← QMAILQUEUE環境変数を/etc/tcp.smtpへ追加

[root@centos ~]# tcprules /etc/tcp.smtp.cdb /etc/tcp.smtp.tmp < /etc/tcp.smtp ← /etc/tcp.smtpのcdb形式データベース再作成

[root@centos ~]# vi /etc/rc.d/init.d/qmail ← qmail起動スクリプト編集
#!/bin/bash
#
# qmail
#
# chkconfig: 2345 80 30
# description: qmail start/stop script

# Source function library.
. /etc/rc.d/init.d/functions

PATH=/var/qmail/bin:/usr/local/bin:/bin:/usr/bin
----追加(ここから)----
QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl"
export QMAILQUEUE
----追加(ここまで)----

[root@centos ~]# /etc/rc.d/init.d/qmail restart ← qmail再起動
Shutting down qmail
Starting qmail

■サーバー側でのスパムメール振分け設定(受信メールサーバーをIMAPにする場合)

SpamAssassinによりメールヘッダにスパムメールの印を付けられたメールはスパムメール用メールボックスへ、その他のメールは通常どおりのメールボックスへ配送するようにする

(1)スパムメール用メールボックス作成
既存ユーザについては、メールボックスにスパムメール用メールボックスを追加する
また、新規ユーザについては、ユーザ追加時に自動でスパムメール用メールボックスが作成されるようにする
【既存ユーザ対処】
既存ユーザについてはスクリプトで一括してメールボックスにスパムメール用メールボックスを追加する
[root@centos ~]# /etc/rc.d/init.d/qmail stop ← qmail停止
Shutting down qmail

[root@centos ~]# vi spamdir-create ← スパムメール用メールボックス作成スクリプト作成
#!/bin/bash

dirrecre()
{
    user=$1
    if [ $user = "root" ]; then
    	home=/root
    else
    	home=/home/$user
    fi
    
    if [ -d "$home/Maildir" ] && [ ! -d "$home/Maildir/.Spam" ]; then
        mv $home/Maildir $home/.old.Maildir
        maildirmake $home/Maildir
        /bin/cp -r $home/.old.Maildir/new $home/Maildir
        /bin/cp -r $home/.old.Maildir/cur $home/Maildir
        /bin/cp -r $home/.old.Maildir/tmp $home/Maildir
        [ -d "$home/.old.Maildir/.Sent" ] && \
        cp -r $home/.old.Maildir/.Sent $home/Maildir
        [ -d "$home/.old.Maildir/.Drafts" ] && \
        cp -r $home/.old.Maildir/.Drafts $home/Maildir
        [ -d "$home/.old.Maildir/.Trash" ] && \
        cp -r $home/.old.Maildir/.Trash $home/Maildir
        if [ -d "$home/.old.Maildir/.spam" ]; then
            cp -r $home/.old.Maildir/.spam $home/Maildir/.Spam
        else
            maildirmake -f Spam $home/Maildir
        fi
        chown -R $user. $home/Maildir
        echo $user
    fi
}

# スパムメール用メールボックス作成(一般ユーザ)
for user in `ls /home/`;
do
    dirrecre $user
done

# スパムメール用メールボックス作成(rootユーザ)
dirrecre root

[root@centos ~]# sh spamdir-create ← スパムメール用メールボックス作成スクリプト実行
user1
・
・
・
userx

[root@centos ~]# rm -f spamdir-create ← スパムメール用メールボックス作成スクリプト削除(後始末)

[root@centos ~]# /etc/rc.d/init.d/qmail start ← qmail起動
Starting qmail

【新規ユーザ対処】
新規ユーザについてはユーザ追加時に自動でスパムメール用メールボックスが作成されるようにする
[root@centos ~]# maildirmake -f Spam /etc/skel/Maildir

(2)qmail、maildrop設定
SpamAssassinがスパム判定したメールはスパムメール用メールボックスへ配送するようにする。
また、未承諾広告メール(件名に「未承諾広告※」が含まれているメール)は自動的に削除するようにする。
[root@router ~]# vi /etc/maildroprc ← maildrop設定ファイル新規作成
logfile "maildrop.log" # ログを記録する場合

# 未承諾広告メールの削除(ここから)
# 件名が日本語の場合
if (/^Subject:.*iso-2022-jp/:h)
{
    # 件名をUTF-8コードへ変換※システムの文字コードがUTF-8(デフォルト)の場合
    SUBJECT=`cat |grep Subject|nkf -mw`

    # 件名をEUCコードへ変換※システムの文字コードがEUCの場合
    SUBJECT=`cat |grep Subject|nkf -me`

    # 件名に未承諾広告※が含まれているメールを削除
    if ($SUBJECT =~ /.*未承諾広告※.*/)
    {
        to "/dev/null"
    }
}
# 未承諾広告メールの削除(ここまで)

# SpamAssassinによるスパムチェック
# ※fetchmail取り込みメールのスパム検査未実施対処
if (!/^X-Spam-/:h)
{
    exception {
        xfilter "/usr/bin/spamc"
    }
}

# SpamAssassinがスパム判定したメールはスパムメール用メールボックスへ配送
if (/^X-Spam-Flag: *YES/)
{
    to "./Maildir/.Spam/"
}

# その他のメールは通常どおりに配送
to "./Maildir/"

[root@centos ~]# vi /var/qmail/rc ← qmail起動コマンド編集
./Maildir/' splogger qmail
↓
|preline maildrop' splogger qmail ← qmailとmaildropが連携するように変更

[root@centos ~]# /etc/rc.d/init.d/qmail restart ← qmail再起動
Shutting down qmail
Starting qmail

[root@centos ~]# vi /etc/logrotate.d/maildrop ← maildropログローテーション設定ファイル作成
/home/*/maildrop.log {
    missingok
    nocreate
    notifempty
}

■メールソフト設定(受信メールサーバーをPOPにする場合)

SpamAssassinによりメール件名に「***SPAM***」を付けられたメールはスパムメール用フォルダへ保存するようにする

(1)スパムメール用フォルダ作成
Outlook Expressを起動し、「ローカル フォルダ」右クリック⇒「フォルダの作成」⇒「フォルダ名」に"Spam"と入力して「OK」ボタン押下

(2)スパムメール振分けルール追加
Outlook Expressを起動し、「ツール」⇒「メッセージルール」⇒「メール」



「件名に指定した言葉が含まれる場合」をチェック
「指定したフォルダに移動する」をチェック
「指定した言葉が含まれる場合」のリンクをクリック



「***SPAM***」と入力して「追加」ボタン押下、「OK」ボタン押下



「指定したフォルダ」のリンクをクリック



「Spam」フォルダを選択して「OK」ボタン押下



「OK」ボタン押下



「OK」ボタン押下

■メールソフト設定(受信メールサーバーをIMAPにする場合)

Spamフォルダを表示する



メールアカウント選択して「IMAPフォルダ」ボタン押下



「リセット」ボタン押下して「Spam」フォルダが表示されること



「Spam」フォルダを選択して「表示」ボタン押下、「OK」ボタン押下



「Spam」フォルダが表示されていること

■ウィルス&スパムチェック確認

Qmail-Scannerに付属していたツールを使用して、ウィルス感染メールの排除、スパムメールの検知及び、正常メールの正常配送を確認する
※テストメールはroot宛に送信されるので、あらかじめroot宛メールをメールクライアントに設定したユーザ宛に転送するようにしておくこと
[root@centos ~]# ./test_installation.sh -doit ← Qmail-Scannerテストツール実行
QMAILQUEUE was not set, defaulting to /var/qmail/bin/qmail-scanner-queue.pl for this test...

Sending standard test message - no viruses...
done!

Sending eicar test virus - should be caught by perlscanner module...
done!

Sending eicar test virus with altered filename - should only be caught by commercial anti-virus modules (if you have any)...

Sending bad spam message for anti-spam testing - In case you are using SpamAssassin...
Done!

Finished test. Now go and check Email for root@centos.wakuwakustation.com

「Qmail-Scanner test (1/4): inoffensive message」という件名のメール(正常テストメール)が受信トレイに配送されること
「Qmail-Scanner anti-spam test (4/4): checking SpamAssassin [if present] (There yours for FREE!)」という件名のメール(スパムテストメール)がSpamフォルダに配送されること
「Qmail-Scanner viral test (2/4): checking perlscanner...」という件名のメール(ウィルステストメール)が配送されないこと
「Qmail-Scanner viral test (3/4): checking non-perlscanner AV...」という件名のメール(ウィルステストメール)が配送されないこと

[root@centos ~]# rm -f test_installation.sh ← Qmail-Scannerテストツール削除

■スパムチェック誤認識対応(受信メールサーバーをIMAPにする場合)

SpamAssassinが誤って正常メールをスパムメールと判断したり、逆にスパムメールを正常メールと判断してしまった場合、以後の誤認識を防止するためにSpamAssassinに学習をさせ、チェック精度を上げる。

ここでは、受信トレイの既読メールを正常メールとして、Spamフォルダの既読メールをスパムメールとして、定期的にSpamAssassinに学習させるようにする。
※SpamAssassinが誤って配送したメールは、ユーザ自身の操作により正常メールなら受信トレイへ、スパムメールならSpamフォルダへ移動しておくこと
(放置しておくと正常メールをスパムメールとして学習したり、逆にスパムメールを正常メールとして学習してしまう)


[root@centos ~]# vi spamassassin-learn ← SpamAssassin学習スクリプト作成
#!/bin/bash

PATH=/usr/sbin:/usr/bin:/bin

for user in `ls /home/`
do
    # 正常メール
    hammail=/home/$user/Maildir/cur

    # 正常メール学習
    if [ -d "$hammail" ]; then
        # 正常メールをSpamAssassinに学習させる
        su $user -s "/bin/bash" -c "sa-learn --ham $hammail 2>&1| \
        logger -p mail.info -t 'sa-learn for $user'"
    fi

    # スパムメール
    spammail=/home/$user/Maildir/.Spam/cur

    # スパムメール学習
    if [ -d "$spammail" ]; then
        # スパムメールをSpamAssassinに学習させる
        su $user -s "/bin/bash" -c "sa-learn --spam $spammail 2>&1| \
        logger -p mail.info -t 'sa-learn for $user'"

        # 受信後一ヶ月経過したスパムメールを削除
        tmpwatch -m 720 $spammail
    fi
done

[root@centos ~]# chmod +x spamassassin-learn ← SpamAssassin学習スクリプトへ実行権限付加

[root@centos ~]# mv spamassassin-learn /etc/cron.daily ← SpamAssassin学習スクリプトを毎日自動実行されるディレクトリへ移動