テスト用のsqueeze ローカルメールサーバ(プレインテキスト認証)

■必要なパッケージのインストール

$ sudo apt-get install postfix dovecot-common dovecot-pop3d dovecot-imapd sasl2-bin

■インストール時の警告に対する対処

To enable saslauthd, edit /etc/default/saslauthd and set START=yes (warning).

下記で見る通り、設定ファイルの「START=yes」であるべきと言ってるのでそうしよう。

$ whereis saslauthd
saslauthd: /usr/sbin/saslauthd /usr/share/man/man8/saslauthd.8.gz

$ ls /etc/default/saslauthd | xargs cat | grep -v "^#\|^\$"
START=no
DESC="SASL Authentication Daemon"
NAME="saslauthd"
MECHANISMS="pam"
MECH_OPTIONS=""
THREADS=5
OPTIONS="-c -m /var/run/saslauthd"

$ cd /etc/default; \
  sudo cp -p saslauthd saslauthd.back; \
  sudo sh -c 'sed s/"START=no"/START="yes"/ saslauthd.back > saslauthd'

$ sudo /etc/init.d/saslauthd restart

■送信メールサーバの認証について

$ apropos smtpd
smtpd (8postfix) - Postfix SMTP server

$ apropos sasl | grep -v "\:\:\|(3)\|ldap" | awk '{print $1}' | column
sasl-sample-client sasldbconverter2 saslpasswd2
sasl-sample-server sasldblistusers2 saslpluginviewer
saslauthd saslfinger testsaslauthd

■その他モジュールについても確認しておこう。

モジュールについて確認する

$ ls /usr/lib | grep "libsasl2\|libsasl2-modules"
libsasl2.so.2
libsasl2.so.2.0.22

$ apt-cache showpkg --installed libsasl2-modules | grep libsasl2-modules | grep postfix
postfix,libsasl2-modules
dtc-postfix-courier,libsasl2-modules

$ apropos sasl | grep -v "\:\:\|(3)\|ldap" | awk '{print $1}' | column
sasl-sample-client sasldbconverter2 saslpasswd2
sasl-sample-server sasldblistusers2 saslpluginviewer
saslauthd saslfinger testsaslauthd

■パスワード設定が空である事を確認する。

$ sudo sasldblistusers2

■FQDNを調べて、ユーザのパスワードを設定する。

$ MYNAME=`whoami`; \
sudo saslpasswd2 -c -u `hostname -f` $MYNAME
Password:
Again (for verification):

■パスワード設定が完了した事を確認

$ sudo sasldblistusers2

■パスワード設定を削除する場合

$ MYNAME=`whoami`; \
  sudo saslpasswd2 -d -u `hostname -f` $MYNAME

■SASL認証が通るかどうか確認する

$ MYNAME=`whoami`; \
  PASS=xxxx; \
  sudo testsaslauthd -u $MYNAME -s `hostname -f` -p $PASS
0: OK "Success."

■サポートされている認証方法を確認する

$ sudo saslpluginviewer | grep -A 1 Installed
Installed SASL (server side) mechanisms are:
ANONYMOUS PLAIN DIGEST-MD5 NTLM LOGIN CRAM-MD5 EXTERNAL
--
Installed auxprop mechanisms are:
sasldb
--
Installed SASL (client side) mechanisms are:
ANONYMOUS PLAIN DIGEST-MD5 NTLM LOGIN CRAM-MD5 EXTERNAL

■認証方法を確認する

$ sudo postconf | grep -i sasl | grep ^smtpd | grep enable
smtpd_sasl_auth_enable = no

■「main.cf」に設定を追加する。

$ sudo vim /etc/postfix/main.cf
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination

■送信メールサーバのtelnetチェック

コマンドは以下の通りです。

$ telnet localhost 25
ehlo localhost
mail from:user
rcpt to:user-mail-address
data
test mail from user
.
quit

■dovecot側でプレインテキスト認証に対応する

下記のようにデフォルトで無効になっているので、
「disable_plaintext_auth = no」に変更、
後で元に戻す事にする。

$ sudo dovecot -a | grep plain
disable_plaintext_auth: yes
mechanisms: plain

$ grep "disable_plaintext_auth" /etc/dovecot/dovecot.conf
#disable_plaintext_auth = yes

■POP3への接続テスト

このままPOP3でログインしようとすると、syslogやmail.logに以下のようなエラーが出る。

$ sudo tail -f /var/log/syslog
 -> mail_location not set and autodetection failed:
Mail storage autodetection failed with home=/home/xxx 

下記の通り未設定。

$ sudo dovecot -a | grep -i mail_location
mail_location:

強引に探すと「FindMailLocation.txt」にありそうです。

$ ls /usr/share/doc/dovecot-common/wiki/*.txt | \
  for list in `xargs`;do grep "mail_location" "$list" > /dev/null 2>&1 && echo "$list";done | \
  awk -F\/ '{print $7}' | column
AuthDatabase.Passwd.txt MissingMailboxes.txt
AuthDatabase.PasswdFile.txt Plugins.Convert.txt
Authentication.MultipleDatabases.txt Plugins.MboxSnarf.txt
Design.Storage.MailStorage.txt Upgrading.1.2.txt
FindMailLocation.txt UserDatabase.txt
HowTo.Rootless.txt VirtualUsers.Home.txt
Migration.Linuxconf.txt

3つの方法があるが、2番目のホームディレクトリに作成するのが最も簡単そうです。

$ grep mail_location /usr/share/doc/dovecot-common/wiki/FindMailLocation.txt
mail_location = mbox:~/mail:INBOX=/var/mail/%u
mail_location = mbox:~/mail:INBOX=~/mbox
mail_location = maildir:~/Maildir

なければ作ります。まあ、ないんですけどね。。。

$ test -d ~/mail || mkdir ~/mail

■POP3にtelnet接続します。

コマンドは以下の通りです。

$ telnet localhost 110
user username
pass password
list
retr 1
dele 1
quit

■IMAPにもtelnet接続してみます。

コマンドから抜けるには、エンターキーを何度も押して、
サーバ側から締め出してもらいます。

$ telnet localhost 143 | tee imap.log
$ grep OK imap.log
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE STARTTLS AUTH=PLAIN] Dovecot ready.

■サーバ側にはsmtpd.confが無く、
 クライアント側にはsmtp_sasl_password_mapsが無いとのメッセージです。
 平文認証では問題ないので後回しにします。

$ sudo saslfinger -s | tail -3
There is no smtpd.conf that defines what SASL should do for Postfix.
SMTP AUTH can't work!
$ sudo saslfinger -c | tail -3
Cannot find the smtp_sasl_password_maps parameter in main.cf.
Client-side SMTP AUTH cannot work without this parameter!

■tcpdumpで確認すると、パスワードもすべて見えます。

$ sudo apt-get install tcpdump

すべて見れますが代表的なコマンドでgrepしてみます。

$ sudo tcpdump -X -vvv -i lo port 25 >> 25.log & sudo tcpdump -X -vvv -i lo port 110 >> 110.log &
$ sudo tail -f [0-9]*.log | grep --color -A 1 "from\:\|to\:\|user\|pass"

■OP25Bのブロックを外す

下記をコメントアウトして、ポート587で接続します。

# grep submission /etc/postfix/master.cf
#submission inet n - - - - smtpd

下記以降は、25番ポートへのアクセスと同じですので、省略します。

telnet localhost 587

■メールの統計情報を入手する

# apt-cache search pflogsumm
pflogsumm - Postfix log entry summarizer

# apt-get install pflogsumm
# whereis pflogsumm
pflogsumm: /usr/sbin/pflogsumm /usr/sbin/pflogsumm.pl /usr/share/man/man1/pflogsumm.1.gz

参照:http://fedorasrv.com/postfix-pflogsumm.shtml

「/var/log/mail.*」に変更したスクリプトでpostmaster宛てに届く。

以下、debian用です。「/var/log/mail.*」へのアクセスにはroot権限が必要です。

$ echo '#!/bin/bash

MAILLOG=`mktemp`
for log in `ls /var/log/mail.*|sort -r`
do
cat $log >> $MAILLOG
done
REPORT=`mktemp`
pflogsumm --problems_first --verbose_msg_detail --mailq -d yesterday $MAILLOG > $REPORT
cat $REPORT | mail -s "`head -1 $REPORT` in `uname -n`" postmaster
rm -f $MAILLOG $REPORT' > pflogsumm_report

$ chmod +x pflogsumm_report
$ sudo ./pflogsumm_report && echo "--> TEST OK"

CRONへの登録方法はどちらでも。
スクリプトの通り、postmaster宛てに届いたのを確認してから行います。

☆rootユーザとして指定時間に実行

$ sudo chown root:root pflogsumm_report
$ sudo mv pflogsumm_report /root
$ sudo sh -c 'export EDITOR=vim;crontab -e'
$ sudo sh -c 'crontab -l | tail -2'
SHELL=/bin/bash
00 00 * * * /root/pflogsumm_report

☆cronのスケジュールに合わせて自動的に実行

$ sudo chown root:root pflogsumm_report
$ sudo mv pflogsumm_report /etc/cron.daily/

さて、saslauthに対応していないので。。。

■サポートされている認証方法を確認する

$ sudo saslpluginviewer | grep -A 1 Installed
Installed SASL (server side) mechanisms are:
ANONYMOUS PLAIN DIGEST-MD5 NTLM LOGIN CRAM-MD5 EXTERNAL
--
Installed auxprop mechanisms are:
sasldb
--
Installed SASL (client side) mechanisms are:
ANONYMOUS PLAIN DIGEST-MD5 NTLM LOGIN CRAM-MD5 EXTERNAL

⇒最も簡単な方法として以下の手順が考えられる。

☆クライアント(plain)⇒auxprop(sasldb)⇒サーバ(plain)

■pwcheck_methodについて

$ man saslauthd | grep -A 7 " sasldb "
sasldb (All platforms)

Authenticate against the SASL authentication database. Note
that this is probably not what you want to use, and is even
disabled at compile-time by default. If you want to use
sasldb with the SASL library, you probably want to use the
pwcheck_method of "auxprop" along with the sasldb auxprop plu-
gin instead.

■SMTPサーバ側の設定

root権限で作業します。

# test -f /etc/postfix/sasl/smtpd.conf || \
 echo 'pwcheck_method: auxprop' > /etc/postfix/sasl/smtpd.conf; \
  ln -s /etc/sasldb2 /var/spool/postfix/etc

「smtpd.conf」が無いというメッセージが無くなればOKです。

■TLSについて

# saslfinger -s

■SMTPクライアント側の設定

$ sudo postconf | grep smtp_sasl_password_maps
smtp_sasl_password_maps =

参照1:http://www.uetyi.mydns.jp/wordpress/postfix-setting/entry-456.html
参照2:http://kazuizm.com/2006/12/28-072720.php
参照3;http://postfix.state-of-mind.de/patrick.koetter/smtpauth/smtp_auth_mailservers.html

パスワードDBを指定してあげれば良いようです。

$ apropos postconf
postconf (1) - Postfix configuration utility
postconf (5) - Postfix configuration parameters

$ man 5 postconf | grep -A 8 ^smtp_sasl_password_maps
smtp_sasl_password_maps (default: empty)
Optional SMTP client lookup tables with one username:password entry per
remote hostname or domain, or sender address when sender-dependent
authentication is enabled. If no username:password entry is found,
then the Postfix SMTP client will not attempt to authenticate to the
remote host.

The Postfix SMTP client opens the lookup table before going to chroot
jail, so you can leave the password file in /etc/postfix.

$ apropos postmap
postmap (1) - Postfix lookup table management

「メールサーバ アカウント パスワード」という形式のuserpasswdファイルを作成します。

# PASS=xxxx ; \
cd /etc/postfix; \
test -f userpasswd || \
echo `hostname -f` `whoami` > userpasswd; \
postmap userpasswd

必要なのは、「smtp_sasl_auth_enable = yes」と
「smtp_sasl_password_maps = hash:/etc/postfix/userpasswd」の設定です。
「/etc/postfix/main.cf」に追加します。

# postconf | grep smtp_sasl_
smtp_sasl_auth_cache_name =
smtp_sasl_auth_cache_time = 90d
smtp_sasl_auth_enable = no
smtp_sasl_auth_soft_bounce = yes
smtp_sasl_mechanism_filter =
smtp_sasl_password_maps =
smtp_sasl_path =
smtp_sasl_security_options = noplaintext, noanonymous
smtp_sasl_tls_security_options = $smtp_sasl_security_options
smtp_sasl_tls_verified_security_options = $smtp_sasl_tls_security_options
smtp_sasl_type = cyrus

■「main.cf」に2行追加。

# vim /etc/postfix/main.cf
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/userpasswd

■postfixを再起動して確認します。

# /etc/init.d/postfix restart

# postconf | grep smtp_sasl_
proxy_write_maps = $smtp_sasl_auth_cache_name $lmtp_sasl_auth_cache_name
smtp_sasl_auth_cache_name =
smtp_sasl_auth_cache_time = 90d
smtp_sasl_auth_enable = yes
smtp_sasl_auth_soft_bounce = yes
smtp_sasl_mechanism_filter =
smtp_sasl_password_maps = hash:/etc/postfix/userpasswd
smtp_sasl_path =
smtp_sasl_security_options = noplaintext, noanonymous
smtp_sasl_tls_security_options = $smtp_sasl_security_options
smtp_sasl_tls_verified_security_options = $smtp_sasl_tls_security_options
smtp_sasl_type = cyrus

■SMTPクライアント側でエラーが出なければOKです。

# saslfinger -c

■リッスンポートを確認する

登録されたポートを見ます。

$ grep -i "pop\|imap\|smtp" /etc/services
smtp 25/tcp mail
pop2 109/tcp postoffice pop-2 # POP version 2
pop2 109/udp pop-2
pop3 110/tcp pop-3 # POP version 3
pop3 110/udp pop-3
imap2 143/tcp imap # Interim Mail Access P 2 and 4
imap2 143/udp imap
imap3 220/tcp # Interactive Mail Access
imap3 220/udp # Protocol v3
imaps 993/tcp # IMAP over SSL
imaps 993/udp
pop3s 995/tcp # POP-3 over SSL
pop3s 995/udp
kpop 1109/tcp # Pop with Kerberos
poppassd 106/tcp # Eudora
poppassd 106/udp
ssmtp 465/tcp smtps # SMTP over SSL

上記のうちリッスンポートを確認します。

$ grep -i "pop\|imap\|smtp" /etc/services | \
awk '{print $2}' | awk -F\/ '{print $1}' | sort | uniq | \
for list in `xargs`;do netstat -an | grep "\:$list";done
tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN

さらにリッスンしているポートを登録されているポートで絞り込みます。

$ grep -i "pop\|imap\|smtp" /etc/services | \
  awk '{print $2}' | awk -F\/ '{print $1}' | sort | uniq | \
  for list in `xargs`;do netstat -an | grep "\:$list";done | \
  awk '{print $4}' | awk -F: '{print $2}' | \
  for next in `xargs`;do grep "$next\/" /etc/services | grep "pop\|smtp\|imap";done
pop3 110/tcp pop-3 # POP version 3
pop3 110/udp pop-3
imap2 143/tcp imap # Interim Mail Access P 2 and 4
imap2 143/udp imap
smtp 25/tcp mail
imaps 993/tcp # IMAP over SSL
imaps 993/udp
pop3s 995/tcp # POP-3 over SSL
pop3s 995/udp

■TLSについて

sudo postconf | grep tls | grep yes
lmtp_tls_enforce_peername = yes
smtp_tls_enforce_peername = yes
smtpd_tls_always_issue_session_ids = yes
smtpd_use_tls = yes

後は「icedove」(Thunderbird)あたりでTLS認証で設定し、送受信が行えればOKです。
今回は長かった。。。w

1. squeezeでも同じ方法でOKでした。
下記追加します。Lennyでも。。。

# mv /etc/sasldb2 /var/spool/postfix/etc 
# ln -s /var/spool/postfix/etc/sasldb2 /etc 
# chgrp postfix /var/spool/postfix/etc/sasldb2

$ perl -MMIME::Base64 -e 'print encode_base64("username\0$username\0password");'

$ telnet localhost 25
ehlo localhost
auth plain dXNlcm5hbWUAAHBhc3N3b3Jk
quit

「dXNlcm5hbWUAAHBhc3N3b3Jk」の部分はperlの実行結果のコピペ。
「Authentication successful」でOK。