Postfixのインストール

Postfix 構築ログ

Postfix が 64bit + IPv6 環境で動作するかの検証です

検証環境
CPU PhenomII X4 905e (2.5GHz 4コア)
マザーボード GA-880GM-USB3
メモリ 16GB (DDR3-1066)
イーサネット Realtek8111D (R8168 GbE)
Linuxカーネルバージョン 3.0 (x86_64)
GCC 4.5.2
glibc 2.13.1
BerkeleyDB db-5.1.25.NC
LDAP OpenLDAP-2.4.23
Postfix 2.9.2
SASL Cyrus-SASL-2.1.25

インストール

まず前準備として 必要なアカウントを作成します
通常は postfix:postfix アカウント と postdrop グループを作成しますが
検証環境は Gentoo Stage3 ベースのシステムで既に mailアカウントが存在するのでこれを流用し
mail:mail アカウントは流用 maildrop グループを新設します

$ su
# groupadd -g 13 maildrop
# exit
$
 

13は GID(グループID)ですシステムで未割り当ての値を選定してください
グループの一覧は /etc/group で管理されています

では Postfix本体のインストールに入ります
ここから最新安定版のソースを取得して下記のようにインストールします

$ tar -xzf postfix-2.9.2.tar.gz
$ cd postfix-2.9.2
$ make makefiles AUXLIBS='-lnsl'
make -f Makefile.in MAKELEVEL= Makefiles
(echo "# Do not edit -- this file documents how Postfix was built for your machine."; /bin/sh makedefs) >makedefs.tmp

〜

[src/tlsproxy]
cat ../../conf/makedefs.out Makefile.in >Makefile
rm -f Makefile; (cat conf/makedefs.out Makefile.in) >Makefile
$
 

検証環境では AUXLIBSに -lnsl と -lresolv の記載が必要でした
make時に必要なライブラリがリンクされず 未解決シンボルエラーが出たためです

gcc -Wmissing-prototypes -Wformat -DHAS_PCRE  -g -O -I. -I../../include -DLINUX3 -o sendmail sendmail.o ../../lib/libglobal.a ../../lib/libutil.a -L/usr/lib64 -lpcre -ldb
../../lib/libutil.a(dict_nis.o): In function `dict_nis_lookup':
/home/admin/postfix-2.9.2/src/util/dict_nis.c:173: undefined reference to `yp_match'
/home/admin/postfix-2.9.2/src/util/dict_nis.c:187: undefined reference to `yp_match'
../../lib/libutil.a(dict_nis.o): In function `dict_nis_init':
/home/admin/postfix-2.9.2/src/util/dict_nis.c:81: undefined reference to `yp_get_default_domain'
collect2: ld はステータス 1 で終了しました
make: *** [sendmail] エラー 1
make: *** [update] エラー 1
 

環境によっては さらに -lresolv も必要かもしれません
コンパイルエラーが出た場合試してみてください

ビルドを続けます

$ make
set -e; for i in src/util src/global src/dns src/tls src/xsasl src/milter src/master src/postfix src/fsstone src/smtpstone src/sendmail src/error src/pickup src/cleanup src/smtpd src/local src/trivial-rewrite src/qmgr src/oqmgr src/smtp src/bounce src/pipe src/showq src/postalias src/postcat src/postconf src/postdrop src/postkick src/postlock src/postlog src/postmap src/postqueue src/postsuper src/qmqpd src/spawn src/flush src/verify src/virtual src/proxymap src/anvil src/scache src/discard src/tlsmgr src/postmulti src/postscreen src/dnsblog src/tlsproxy; do

〜

gcc -Wmissing-prototypes -Wformat -DHAS_PCRE  -g -O -I. -I../../include -DLINUX3 -o tlsproxy tlsproxy.o tlsproxy_state.o ../../lib/libtls.a ../../lib/libmaster.a ../../lib/libglobal.a ../../lib/libutil.a -L/usr/lib64 -lpcre -ldb -lnsl -lresolv
cp tlsproxy ../../libexec
$
 

ビルドは完了です 各種インストール条件は make install 時に対話式で設定できます
main.cf などの設定を上書きしない(バージョンアップのみの)場合は make upgrade と打ちます

$ su
# make install

〜

    Warning: if you use this script to install Postfix locally,
    this script will replace existing sendmail or Postfix programs.
    Make backups if you want to be able to recover.

    Before installing files, this script prompts you for some definitions.
    Most definitions will be remembered, so you have to specify them
    only once. All definitions should have a reasonable default value.

Please specify the prefix for installed file names. Specify this ONLY
if you are building ready-to-install packages for distribution to OTHER
machines. See PACKAGE_README for instructions.
install_root: [/] (何も入力せずEnter)

Please specify a directory for scratch files while installing Postfix. You
must have write permission in this directory.
tempdir: [/home/admin/postfix-2.9.2] (何も入力せずEnter)

Please specify the final destination directory for installed Postfix
configuration files.
config_directory: [/etc/postfix] (何も入力せずEnter)

Please specify the final destination directory for installed Postfix
administrative commands. This directory should be in the command search
path of adminstrative users.
command_directory: [/usr/sbin] (何も入力せずEnter)

Please specify the final destination directory for installed Postfix
daemon programs. This directory should not be in the command search path
of any users.
daemon_directory: [/usr/libexec/postfix] (何も入力せずEnter)

Please specify the final destination directory for Postfix-writable
data files such as caches or random numbers. This directory should not
be shared with non-Postfix software.
data_directory: [/var/lib/postfix] /var/db/postfix

Please specify the destination directory for the Postfix HTML
files. Specify "no" if you do not want to install these files.
html_directory: [no] (何も入力せずEnter)

Please specify the owner of the Postfix queue. Specify an account with
numerical user ID and group ID values that are not used by any other
accounts on the system.
mail_owner: [postfix] mail

Please specify the final destination pathname for the installed Postfix
mailq command. This is the Sendmail-compatible mail queue listing command.
mailq_path: [/usr/bin/mailq] (何も入力せずEnter)

Please specify the destination directory for the Postfix on-line manual
pages. You can no longer specify "no" here.
manpage_directory: [/usr/local/man] /usr/share/man

Please specify the final destination pathname for the installed Postfix
newaliases command. This is the Sendmail-compatible command to build
alias databases for the Postfix local delivery agent.
newaliases_path: [/usr/bin/newaliases] (何も入力せずEnter)

Please specify the final destination directory for Postfix queues.
queue_directory: [/var/spool/postfix] (何も入力せずEnter)

Please specify the destination directory for the Postfix README
files. Specify "no" if you do not want to install these files.
readme_directory: [no] (何も入力せずEnter)

Please specify the final destination pathname for the installed Postfix
sendmail command. This is the Sendmail-compatible mail posting interface.
sendmail_path: [/usr/sbin/sendmail] (何も入力せずEnter)

Please specify the group for mail submission and for queue management
commands. Specify a group name with a numerical group ID that is
not shared with other accounts, not even with the Postfix mail_owner
account. You can no longer specify "no" here.
setgid_group: [postdrop] maildrop
Updating /ram/libexec/postfix/anvil...
Updating /ram/libexec/postfix/bounce...
Updating /ram/libexec/postfix/cleanup...
Updating /ram/libexec/postfix/discard...
Updating /ram/libexec/postfix/dnsblog...

〜

Updating /ram/share/man/man8/trace.8...
Updating /ram/share/man/man8/trivial-rewrite.8...
Updating /ram/share/man/man8/verify.8...
Updating /ram/share/man/man8/virtual.8...

    Warning: you still need to edit myorigin/mydestination/mynetworks
    parameter settings in /etc/postfix/main.cf.

    See also http://www.postfix.org/STANDARD_CONFIGURATION_README.html
    for information about dialup sites or about sites inside a firewalled
    network.

    BTW: Check your /etc/aliases file and be sure to set up aliases
    that send mail for root and postmaster to a real person, then run
    /usr/bin/newaliases.

#
 

各質問に対して ターゲットシステムの環境に応じて パラメータを設定していきます
何も入力せずに Enter を押すと大括弧で括られたデフォルト値が選択されます
質問が終わったらファイルがコピーされて 更新ファイルも画面上で分かるようになっています

syslogへの設定も行います 検証環境では syslog-ngを利用しているので
syslog-ng.conf に下記の設定を追加します

destination mail { file("/var/log/mail.log"); };
destination mailinfo { file("/var/log/mail.info"); };
destination mailwarn { file("/var/log/mail.warn"); };
destination mailerr { file("/var/log/mail.err"); };

filter f_mail { facility(mail); };

log { source(src); filter(f_mail); destination(mail); };
log { source(src); filter(f_mail); filter(f_info); destination(mailinfo); };
log { source(src); filter(f_mail); filter(f_warn); destination(mailwarn); };
log { source(src); filter(f_mail); filter(f_err); destination(mailerr); };
 

最後に Linuxシステムで Postfixを制御するための start/stopスクリプトを手動で作成します
(通常 Linuxディストリビューションのパッケージインストールすれば 自動作成されているはず)
検証環境は Gentooベースのシステムのため /etc/init.d/postfix を以下の内容で作成しました
(スクリプトへの実行権限の付与を忘れずに)

#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header$

depend() {
        need net.eth0
}

start() {
        ebegin "Starting Postfix"
        /usr/sbin/postfix start
        eend $?
}

stop() {
        ebegin "Stopping Postfix"
        /usr/sbin/postfix stop
        eend $?
}
 

以上のインストール過程は IPv4 IPv6 32bit 64bit いずれの環境でも同じ手順でした
ただし 後半になりますが SMTP AUTH化 TLS化 するためにオプションを追加再ビルドします

設定

Postfixサーバの動作設定を行います 主に /etc/postfix/main.cf が設定対象です
ここで IPv6に関する設定も行います 下記は修正箇所とその説明です

inet_protocols = ipv6
inet_interfaces = loopback-only
 

ネットワーク設定です 検証環境のため IPv6のみを扱うという強気な設定です
通常は inet_protocols = all と IPv4 IPv6 両方喋るようにします

inet_interfacesは メール配送要求を受け付けるインターフェースを指定します
まずはローカルからのみ配送を受けるため loopback-only を指定しています
(loopback-only は IPv6へも対応しています)

myhostname = phenom.mydomain
mydomain = mydomain
myorigin = $mydomain
append_at_myorigin = yes
append_dot_mydomain = no
 

メールサーバの名称と ローカルドメインの設定です
検証環境では mydomain というローカルなドメイン名を設定しています
たとえ自分が 独自ドメインを取得していようともとりあえずローカルなドメイン名を設定します
(独自ドメインは masquerade_domains で設定できるため)

myoriginは ローカル配送するメールについて送信元のドメイン部分に付与される名称です
今回 $mydomain としているためここでの例では アカウント@mydomain というアドレスになります
デフォルトの $myhostname を指定すると アカウント@phenom.mydomain アドレスになります

append_at_myorigin と append_dot_mydomain はメールアドレス操作(修正)機能です
append_at_myorigin は ドメイン部分のない宛先に $myorigin を追加します
(例えば admin 宛のメールは admin@mydomain 宛のメールに宛先修正されます 通常これは必須)
append_dot_mydomain は ドメイン部分がホスト名のみの場合 $mydomain を追加します
(例えば admin@phenom 宛のメールは admin@phenom.mydomain 宛のメールに宛先修正されます)

mydestination = $myhostname, localhost.$mydomain, localhost
 

ローカル配送対象となる 宛先ドメインの設定です
宛先ドメインが 指定ドメインのいずれかであればローカル配送が行われます
Postfixでは $myhostnameは必須で あとは localhost(::1)を追加しています

relayhost =
relay_domains =
 

リレー(メール転送)の設定ですが
ローカル配送のみとし転送自体を禁止するため relay_domains を空としています
外部へのメールやりとりが必要になったら設定します

home_mailbox = Maildir/
#mail_spool_directory = /var/spool/mail
 

ローカル配送時のメールボックスのフォーマット指定です
上記のように / を最後に使うと Maildir方式(1メール1ファイル)となります
この場合 ~/Maildir/ 配下に保存されることになります

mail_spool_directory は指定しません
これを指定すると メールスプール方式という最古の方式となり
/var/spool/mail/ にアカウント毎のメールファイル(1ファイル中に複数メールが含まれる)が作られます

メールのローカル配送方式が 3種類あるので紹介しておきます

メールローカル配送方式
メールスプール方式 インターネット黎明期のメール配送方式
/var/mail/ユーザ名/ に単一のメールスプールファイルを置いて
配送されてきたメールはそのファイルに追記
mbox形式 ホームディレクトリ ~/ にメールスプールファイルを置く
Sendmailで主に使われていた
Maildir形式 ホームディレクトリに Maildir/ などの専用ディレクトリを置いて
1メール=1ファイル で格納する最近の方式
スプールほどファイルを占有する必要がなく複数の MTA MUA(IMAPなど) と共用させやすい
専用のメールサーバを立てる場合 iノードの枯渇に注意する必要がある
alias_maps = hash:/etc/postfix/aliases
alias_database = $alias_maps
 

最後にエイリアスの設定です
エイリアスとはメール転送機能で 特定の宛先のローカル配送に対して 別の宛先に転送します

ここもカスタマイズしています
通常は /etc/aliases を設定するケースが多いのですが postfixディレクトリを指定しています
これにより newaliasesコマンドにより /etc/postfix/aliases.db が作成されます
aliases の作成については後述します

ひとまず main.cf の修正を終えたので内容をチェックします

# postconf -n
alias_database = $alias_maps
alias_maps = hash:/etc/postfix/aliases
append_at_myorigin = yes
append_dot_mydomain = no
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/db/postfix
debug_peer_level = 2
home_mailbox = Maildir/
html_directory = no
inet_interfaces = loopback-only
inet_protocols = ipv6
mail_owner = mail
mailq_path = /usr/bin/mailq
manpage_directory = /usr/share/man
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mydomain = mydomain
myhostname = phenom.mydomain
myorigin = $mydomain
newaliases_path = /usr/bin/newaliases
queue_directory = /var/spool/postfix
readme_directory = no
relay_domains = 
relayhost = 
sample_directory = /etc/postfix
sendmail_path = /usr/sbin/sendmail
setgid_group = maildrop
unknown_local_recipient_reject_code = 550
# postfix check
#
 

設定ファイルの誤りがあれば postfix check 時にメッセージが表示されます
設定に問題がなければ Postfixを起動します

# /etc/init.d/postfix start
 * Caching service dependencies ...                                       [ ok ]
 * Starting Postfix ...
postfix/postfix-script: starting the Postfix mail system                  [ ok ]
#
 

起動していれば netstat -a コマンドで TCP/SMTPのポートが開いていることが確認できます

$ netstat -a | grep smtp
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
unix  2      [ ACC ]     STREAM     LISTENING     494568   private/smtp
$
 

/etc/postfix/alaises への転送設定も追加しておきましょう

root:            admin
 

root宛へのメールを (普段ログインする)admin宛へ転送するための設定を追加しました
下記コマンドで aliases.db へ反映します

$ su
# newaliases
# ls -l /etc/postfix/aliases*
-rw-r--r-- 1 root root  8841  8月 28 17:38 /etc/postfix/aliases
-rw-r--r-- 1 root root 12288  9月  4 09:22 /etc/postfix/aliases.db
#
 

では ローカル配送の試験を行ってみます telnet コマンドで SMTPプロトコルを直接流し込みます

$ telnet -6 localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 phenom.mydomain ESMTP Postfix
EHLO mydomain
250-phenom.mydomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
MAIL FROM: root@mydomain
250 2.1.0 Ok
RCPT TO: admin@mydomain
250 2.1.5 Ok
DATA
354 End data with .
test mail
.
250 2.0.0 Ok: queued as 0250E24060
QUIT
221 2.0.0 Bye
Connection closed by foreign host.
$ su
# tail -f /var/log/mail.log
〜
Apr 28 13:45:33 phenom postfix/smtpd[3986]: connect from localhost[::1]
Apr 28 13:49:25 phenom postfix/smtpd[3986]: 0250E24060: client=localhost[::1]
Apr 28 13:49:45 phenom postfix/cleanup[4023]: 0250E24060: message-id=<20120428134925.0250E24060@phenom.mydomain>
Apr 28 13:49:45 phenom postfix/qmgr[3924]: 0250E24060: from=, size=286, nrcpt=1 (queue active)
Apr 28 13:49:45 phenom postfix/local[4025]: 0250E24060: to=, relay=local, delay=38, delays=38/0/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Apr 28 13:49:45 phenom postfix/qmgr[3924]: 0250E24060: removed
Apr 28 13:50:45 phenom postfix/smtpd[3986]: disconnect from localhost[::1]
#
 

SMTPは TCP/25ポートを使います
MAIL FROM: や RCPT TO: で送信元や宛先を指定し
DATA で本文を記載します 本文の終了として . ピリオドのみの行を入力します
telnet後は mail.logでも 正常にローカル配送されたことが記録されています

メールも確かに adminアカウント宛に配送されていました

Return-Path: 
X-Original-To: admin@mydomain
Delivered-To: admin@mydomain
Received: from mydomain (localhost [IPv6:::1])
	by phenom.mydomain (Postfix) with ESMTP id 0250E24060
	for ; Sat, 28 Apr 2012 13:49:07 +0000 (UTC)
Message-Id: <20120428134925.0250E24060@phenom.mydomain>
Date: Sat, 28 Apr 2012 13:49:07 +0000 (UTC)
From: root@mydomain

test mail
 

(書きかけ)

LAN内からの SMTP送信を受け付ける

IPv6による SMTP接続が動作することまで確認しました
次は localhost以外の 自宅LANからの接続を許可するように設定します

main.cf の設定変更箇所は下記の通りです

inet_interfaces = all
 

loopback-only から all に変更するだけです
Postfixを再起動してテストします Sylpheedメーラを別ホストから試します

# /etc/init.d/postfix restart
 * Stopping Postfix ...
postfix/postfix-script: stopping the Postfix mail system                  [ ok ]
 * Starting Postfix ...
postfix/postfix-script: starting the Postfix mail system                  [ ok ]
# netstat -a | grep smtp
tcp6       0      0 [::]:smtp               [::]:*                  LISTEN     
unix  2      [ ACC ]     STREAM     LISTENING     498865   private/smtp
#
 

SMTP Sylpheed設定画面1
Sylpheedの設定画面を開いたところです
SMTPはデフォルトの 25番のままです SSLも設定しません
SMTP Sylpheed送信1
普通にメールを記述して 送信すると

Return-Path: 
X-Original-To: admin@mydomain
Delivered-To: admin@mydomain
Received: from turion.mydomain (turion.mydomain [IPv6:fe80::2***:****:***2])
	by phenom.mydomain (Postfix) with ESMTP id 90A7824061
	for ; Sat, 28 Apr 2012 14:23:17 +0000 (UTC)
Date: Sun, 22 Apr 2012 01:42:58 +0900
From: テスト 
To: admin@mydomain
Subject: テストメール
Message-Id: <20120422014258.c87c82dd.admin@******.com>
X-Mailer: Sylpheed 3.1.3 (GTK+ 2.19.0; i686-pc-linux-gnu)
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit

テストです
 

正常に送信できています

SMTP AUTHに対応する

次の段階として SMTP AUTH を導入します
SMTP AUTHにより SMTP接続時に アカウント/パスワードでの認証が必須となり
未認証の SMTP接続はメールが送信できません

これにより メールサーバが不正中継の土台となってしまう事態を防ぎます
将来メールサーバをインターネットに接続する際に必須といえる仕組みです

認証基盤(アカウント パスワードの管理)として
SASL(Simple Authentication Security Layer) が使えます
ここでは Cyrus-SASL のインストール手順にあわせて BerkeleyDB5対応も行います

$ tar -xzf cyrus-sasl-2.1.25.tar.gz
$ cd cyrus-sasl-2.1.25
$
 

configureを始める前に BerkeleyDB5対応のための変更を行います
configureスクリプトの内容を書き換えて libdb-5.1.so をリンクするようにします

 7233 
 7234         saved_LIBS=$LIBS
 7235         for dbname in ${with_bdb} db-5.1 db-4.7 db4.7 db47 db-4.6 db4.6 db46 db-4.5 db4.5 db45 db-4.4 db4.4 db44 db-4.3 db4.3 db43 db-4.2 db4.2 db42 db-4.1 db4.1 db41 db-4.0 db4.0 db-4 db40 db4 db-3.3 db3.3 db33 db-3.2 db3.2 db32 db-3.1 db3.1 db31 db-3 db30 db3 db
 7236           do
 7237             LIBS="$saved_LIBS -l$dbname"
 7238             cat >conftest.$ac_ext <<_ACEOF
 

7235行目で dbのバージョン候補を指定して検索しているので
候補の最初に db-5.1 を追加します これで libdb-5.1.so を掴んでくれます

BerkeleyDBの関数に正しい引数を渡す必要があります
sasldb/db_berkeley.c も編集が必要です 変更箇所は 104行目です

104 #if DB_VERSION_MAJOR >= 5 || DB_VERSION_MINOR >= 1
105         ret = (*mbdb)->open(*mbdb, NULL, path, NULL, DB_HASH, flags, 0660);
106 #else
107         ret = (*mbdb)->open(*mbdb, path, NULL, DB_HASH, flags, 0660);
108 #endif
 

utils/dbconverter-2.c も編集します 217行目を同様に変更します

217 #if DB_VERSION_MAJOR >= 5 || DB_VERSION_MINOR >= 1
218         ret = (*mbdb)->open(*mbdb, NULL, path, NULL, DB_HASH, DB_CREATE, 0664    );
219 #else
220         ret = (*mbdb)->open(*mbdb, path, NULL, DB_HASH, DB_CREATE, 0664);
221 #endif
 
 
$ ./configure --prefix=/usr --enable-static=yes --enable-java --enable-ntlm --enable-login --enable-ldapdb --with-ldap=/usr --enable-sql
configure: creating cache ./config.cache
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
〜
config.status: creating saslauthd.h
config.status: executing depfiles commands
Configuration Complete. Type 'make' to build.
$ make
make  all-recursive
make[1]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' に入ります
Making all in include
〜
make[2]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' に入ります
make[2]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' から出ます
make[1]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' から出ます
$ su
# make install
Making install in include
make[1]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25/include' に入ります
make[2]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25/include' に入ります
〜
make[2]: `install-data-am' に対して行うべき事はありません.
make[2]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' から出ます
make[1]: ディレクトリ `/home/admin/cyrus-sasl-2.1.25' から出ます
#
 

saslauthd sasldblistusers2 saslpasswd2 といったコマンドが使えるようになります

Cyrus-SASLが入ったら Postfixの方も対応するために再ビルドが必要です
再度ビルドディレクトリで 下記のように実施してください

$ make clean
〜
$ make makefiles CCARGS='-DUSE_SASL_AUTH -DUSE_CYRUS_SASL -DUSE_TLS -I/usr/include/sasl' AUXLIBS='-lsasl2 -lssl -lnsl'
〜
$ make
〜
$ su
# make upgrade
〜
#
 

Cyrus-SASLを利用するためのオプションと TLSを利用するためのオプションも追加しています

Cyrus-SASLの設定に話を戻します
ここでは sasldb形式の認証データベースと LDAP形式の認証データベースを両方試します
まず sasldb形式から 下記コマンドでデータベースを作れます

# saslpasswd2 -c -u testtest.com mailtest
Password: パスワードを入力
Again (for verification): 同じパスワード
# chown mail /etc/sasldb2
# ls -l /etc/sasldb2
-rw-r----- 1 mail root 12288  4月 29 01:12 /etc/sasldb2
#
 

/etc/sasldb2 ができます これは BerkeleyDB形式のデータベースです
また Postfix smtpd から読み書きできるよう mailオーナーに変更しています
-u オプションでドメインを指定しています

# sasldblistusers2
mailtest@testtest.com: cmusaslsecretOTP
mailtest@testtest.com: userPassword
#
 

sasldblistusers2 で登録済アカウントを確認できます

sasldb形式を Postfixで扱えるよう /usr/lib/sasl2/smtpd.conf を作成します

pwcheck_method: auxprop
auxprop_method: sasldb
mech_list: cram-md5 digest-md5 plain login
 

Postfix側 main.cf には下記のように設定を追加します

smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = testtest.com
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
 

smtpd_sasl_local_domain には saslpasswd2の時と同じドメインを指定します
設定後 Postfixをリスタートしましょう

Sylpheed側は メール送信の設定を変更する必要があります
SMTP Sylpheed設定画面2
SMTP AUTHを有効にして sasldbに登録したアカウントパスワードを設定します
これで SMTP AUTH化は完了です
SMTP AUTHのチェックを外すと メール送信ができないことも確認しましょう

検証環境では OpenLDAPを使っているので
認証基盤を sasldbから 本命の LDAPに切り替えます
OpenLDAPは設定済でメール用アカウントパスワードが下記のように登録済とします

dn: uid=mailtest,ou=People,dc=local
objectClass: top
objectClass: posixAccount
objectClass: account
objectClass: postLdapAdmin
uid: mailtest
cn: MyName
uidNumber: 500
gidNumber: 100
homeDirectory: /home/admin
loginShell: /usr/bin/bash
userPassword:: **************==
gecos: MyName
mail: admin@testtest.com
mailDirectory: /home/admin/Maildir
 

Cyrus-SASLで sasldb以外のデーターベースを使うには saslauthdが必要です
まず /usr/etc/saslauthd.conf を下記のように設定します

ldap_servers: ldap://localhost
ldap_bind_dn: cn=Manager, dc=mydomain
ldap_password: ********
ldap_search_base: ou=People,dc=testest,dc=com
ldap_filter: (&(uid=%u)(objectClass=posixAccount))
 

ldap_bind_dn には データベースにアクセスするための LDAPユーザを指定します
(本当は Managerアカウントでなく 読み出し用アカウントを作るべきです)

設定完了したら saslauthdを起動しましょう

# saslauthd -a ldap
#
 

/usr/lib/sasl2/smtpd.conf を saslauthd に向くように設定変更します

pwcheck_method: saslauthd
mech_list: digest-md5 plain login
 

cram-md5 を入れると認証に失敗するようなので削除しています

Sylpheed側には LDAPに登録したアカウントパスワードを設定すれば
sasldbと同じようにメールが送れるはずです

LDAPデータベースが使えたら /etc/sasldb2 は不要になります
saslauthd はシステム起動時に自動起動するようにしておきましょう

TLS化する

SMTP AUTHを導入しましたが
インターネットにサーバ公開することを考えたら まだセキュリティに不安です
通信経路を暗号化するSSLを導入しましょう

OpenSSLがあれば 秘密鍵 と サイト証明書 が作れます

# openssl genrsa -des3 -out ./postfix.pem 2048
Generating RSA private key, 2048 bit long modulus
.........................+++
...+++
e is 65537 (0x10001)
Enter pass phrase for ./postfix.pem:適当なパスワード
Verifying - Enter pass phrase for ./postfix.pem:同じパスワード
# openssl rsa -in ./postfix.pem -out /usr/etc/ssl/private/postfix.pem
Enter pass phrase for /ram/postfix.pem:同じパスワード
writing RSA key
# rm ./postfix.pem
#
 

1回目のコマンドは秘密鍵(パスワード付)の作成
2回目のコマンドで鍵のパスワードを解除し 本命のディレクトリに出力

次のコマンドで 自己証明書を作成します

# openssl req -new -x509 -key /usr/etc/ssl/private/postfix.pem -out /usr/etc/ssl/certs/postfix.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Ekoda-ku
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyDomain
Organizational Unit Name (eg, section) []:Home
Common Name (eg, YOUR name) []:MyName
Email Address []:admin@mydomain
#
 

署名情報は適当でかまいません (公的認証局に登録するようなものでないので)
ファイル名や拡張子も 各システムのポリシーに従って変えてもかまいません

鍵ができたので Postfixの設定を行います main.cf に下記行を追加します

# TLS
smtpd_use_tls = yes
smtpd_tls_key_file = /usr/etc/ssl/private/postfix.pem
smtpd_tls_cert_file = /usr/etc/ssl/certs/postfix.pem
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_tls_scache
smtpd_tls_loglevel = 1
 

smtpd_tls_loglevel は必要な詳細度に応じて 0〜4 の値に調整してください

また TCPポート番号が変わるため master.cf への修正も必要です

#smtp      inet  n       -       n       -       -       smtpd

smtps     inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
 

smtp(TCP/25)の行をコメントアウトして smtps(TCP/465) の行を有効にするだけです

Postfixを再起動したら ポート番号465が開いているのが確認できるはずです

# netstat -a | grep urd
tcp6       0      0 [::]:urd                [::]:*                  LISTEN
#
 

Sylpheed等メールクライアントの設定を変えます
SMTP Sylpheed設定画面3 SSLオプション
「SSLを使用」にチェックを入れるだけです
高度な設定タブでは 送信ポート番号が 465番に変わっていることがわかります
初回メール送信で証明書の確認が求められますが 暗号化が完了したことが確認できます

インターネットへの接続

Postfixの IPv6動作は前半で完了し 認証や通信暗号化も行いました
最後の仕上げにインターネットへの接続も行います

外部ドメイン宛のメールをインターネットに飛ばす設定
Postfixの main.cf に下記更新を行います

inet_protocols = all
mydomain = testtest.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, mydomain
relayhost = [smtp.*****.ocn.ne.jp]:25
smtp_bind_address = 192.168.0.254
 

inet_protocols = all で IPv4も許可する設定としました
外出中メールを送信したいとき 自宅の PostfixにIPv4でつなげるためです
(早く IPv6が普及してほしい)

残りの設定は 外部ドメイン宛のメールをインターネットに飛ばす設定です
mydestination に該当しない宛先は relayhostにメールを配送する設定を行います

mydomain に独自ドメインがあればそれを設定します
relayhost は外部への飛ばし先ですが プロバイダが用意しているサーバを利用します
(MX検索を抑止するため 大括弧[]で括る必要があります)
smtp_bind_address は自サーバのインターフェースアドレスを指定します
SMTP 静的IPマスカレード設定 自宅サーバへの接続
ブロードバンドルータには上記の設定を入れました
IMAP接続用に ポート10993番を開けて IMAPサーバの993番に転送します
SMTP接続用に ポート10465番を開けて SMTPサーバの465番に転送します
これでメール受信送信が 外出中から可能になります

便利になる反面 セキュリティの確保にも十分注意する必要があります

IPv6サーバツールへ戻る