Computer
Postfix+TLS SpamAssassin

Postfix with TLSのインストール

基本的にはPostfixのインストール方法と同じ.
参考:
[Secure MTA(Postfix-TLS)] Debian Linux Server
Vine Linux いじり
(4月27日に設定したものを5月4日に大幅更新.)

まず,postfixのインストール作業.今回はSASL2を使うことにした.(4月27日の時点ではSASLだったが.)
$ tar zxvf postfix-2.2.x.tar.gz
$ cd postfix-2.2.x
$ make makefiles CCARGS="-DUSE_SASL_AUTH -I/usr/include/sasl -DUSE_TLS -I/usr/include" AUXLIBS="-L/usr/lib/sasl -lsasl2 -L/usr/lib -lssl -lcrypto"
$ make
$ su
$ su -
エラーが出る場合,db4-develを入れる必要がある.(2007年9月13日追記.apt-get install db*-devel で60個も余計なものを入れてしまった・・・しかもdb4は入ってないし・・・)
次にpostfixとpostdropというユーザとグループを作る.
# adduser -u 465 postfix
# passwd postfix
# groupadd -g 466 postdrop
# vipw

postfix:x:(適当なuid):(適当なgid):Postfix:/nonexistent:/sbin/nologin
と後半部分をいじる.
「保存時に「このシステムではシャドウパスワードが使われています。 /etc/shadowを今すぐ編集しますか[y/n]?」 と聞かれるので「y」を押して,postfixの部分を以下の様に変更する.
postfix:!!:(これ以降はいじらないでよい)
で,
# exit
# make install
する.
いろいろ聞かれるが,すべてEnterで.
/etc/postfix/master.cf の
smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes
という部分のコメントアウトを取る.(なければ追加.)

次に/etc/postfix/main.cfの編集.
# ホスト名の設定
myhostname = silvia.satze.net

# ドメイン名の設定
mydomain = satze.net

# メール送信の設定
myorigin = $mydomain

# メール受信の設定
#inet_interfaces = $myhostname, localhost

# メールの最終目的地とみなす範囲の指定
mydestination = $mydomain

# 信頼できるクライアントのネットワークを指定
mynetworks = 192.168.1.0/24, 127.0.0.0/8

# リレーを許可するドメインを指定
relay_domains = $mydestination

home_mailbox = Maildir/

smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
#smtpd_sasl_security_options = noanonymous, noplaintext
smtpd_sasl_local_domain = $mydomain
smtpd_recipient_restrictions = permit_sasl_authenticated,reject_unauth_destination
#smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
broken_sasl_auth_clients = no

smtpd_tls_cert_file = /etc/postfix/server.crt
smtpd_tls_key_file = /etc/postfix/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
smtpd_use_tls = yes
smtpd_tls_loglevel = 1
こんな感じ.(Docomo対策もする必要はあるが.)

SSLの証明書はApacheのを流用する.(作り直しても良い.)
ln -s /usr/local/apache2/conf/server.crt /etc/postfix/server.crt
ln -s /usr/local/apache2/conf/server.key /etc/postfix/server.key
当然cpしてもよい.


ここから2つの方法がある.
1つ目は認証用のパスワードを設定をする方法.
# saslpasswd2 -c -u (host名) ユーザ名
とし,認証用DBのアクセス権変更をする(初回のみ).
# chgrp postfix /etc/sasldb2
# chmod 640 /etc/sasldb2
/usr/lib/sasl2/ に新規に smtpd.conf というファイルを作成し,
pwcheck_method: auxprop
を記述.(saslのときはsasldbだったが.)
この場合は認証はPLAINやLOGINである必要はないので, /etc/postfix/master.cf では
smtpd_sasl_security_options = noanonymous,noplaintext
としてよい.
(1つ目の方法終わり.)

2つ目はsaslauthd を利用する方法.
/usr/lib/sasl2/smtpd.conf には
pwcheck_method: saslauthd
mech_list: login plain
を記述.(CRAM-MD5などには非対応らしい.)
さらに
# chkconfig --level 345 saslauthd on
# service saslauthd start
とsaslauthd を自動起動&起動する.
(2つ目の方法終わり.)

これで設定が終わったので,Postfixを起動させる.
# /usr/sbin/postfix start
エラーが出るなら
# chmod -R 755 /var/spool/postfix
# chown postfix /var/spool/postfix/*
# chgrp postdrop /var/spool/postfix/*

これでOK.

# telnet localhost 25
をすると
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 silvia.satze.net ESMTP Postfix
EHLO localhost <-入力
250-silvia.satze.net
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH DIGEST-MD5 CRAM-MD5(2つ目の方法の場合は250-AUTH PLAIN LOGIN)
250 8BITMIME
STARTTLS <-入力
220 Ready to start TLS
となればOK.

SpamAssassin

スパムメールが多くなってきたので,SpamAssassinを導入してみました.
参考:http://www.miloweb.net/spamassassin.html

まず,apt-getでSpamAssassinをDL&インストール.
apt-get update
apt-get install spamassassin
インストールが完了したら,設定ファイルをコピー.
cp /usr/share/doc/spamassassin-2.64/sample-japanese-local.cf /etc/mail/spamassassin/local.cf
(参考サイトは/usr/docになっているがshareが入る)
Spamの学習結果を全ユーザ共通にするために,local.cfの最後の2行
auto_whitelist_path     /var/spool/spamassassin/auto-whitelist
bayes_path              /var/spool/spamassassin/bayes

report_safe 0
のコメントアウトを外し,
mkdir -m 777 /var/spool/spamassassin
を行う.
後は,(すでにMailboxはMaildirになっているし,procmailの設定もすんでいるので)SpamAssassinを起動して,Poxtfixを再起動.
/etc/rc.d/init.d/spamassassin start
/etc/rc.d/init.d/postfix restart
ついでに起動後にSpamAssassinが起動するように(Webminから)設定.
あ,/sbin/chkconfig spamassassin on でもよいのか・・・.

そして,(とりあえず今回は各ユーザごとに設定をしたいので).procmailrcの頭のほうに
:0 fw
| /usr/bin/spamassassin
を記述.
これによって送られてきたメールに,
X-Spam-Checker-Version: 
X-Spam-Level: 
X-Spam-Status: 
のヘッダが追加される.
あとはX-Spam-StatusがYESなものや,Lecelに*がたくさんあるものを.procmailrcや各メーラーで取り除く設定をすればよい.

スパムメールの学習は管理者権限で (といっても,/var/spool/spamassassinとその中にあるファイルのパーミッションが開いていればユーザ権限でも良いが),
/usr/bin/sa-learn --spam スパムが入っているcurディレクトリかスパムメールそのもの
/usr/bin/sa-learn --ham スパムじゃないメールが入っているcurディレクトリかスパムじゃないメールそのもの
で行える.

追記(2006/11/13):Version3.1.7にしました.基本的にはまったく同じです.
ただ,Spam Assassinを止め,local.cfを別の名前にしてからじゃないと(学習のときに)失敗するかも.
結局/usr/lib/perl5/site_perl/5.8.2/Mail/SpamAssassin/BayesStore/DBM.pmを消す羽目になったし・・・.
Valid HTML 4.01 Transitional Valid CSS!
Copyright© Satze.net All rights reserved.