こんにちは、鯨井貴博@opensourcetechです。
今回はIMAPを使用して大量のユーザーのメール通信を処理する場合などに有効と思われる、
Nginxを使用してメールプロキシを構築してみようと思います。
Nginx以外にも使用するサービスとして、
ネームサービス(bind)、認証サービス(apache&PHP)、SMTPサービス(postfix)、POP3/IMAPサービス(dovecot)があり、以下のような構成としました。
また、サーバOSとしては、CentOS6.6(32bits)を使用しました。
※IPアドレスや使用ドメインなどは、ご自身の環境に合わせて変更下さい。
CentOS6.6のインストール
Virtualbox上の仮想マシンに、CentOS6.6をインストールします。
設定等はお使いの環境に合わせて頂ければいいと思います。
インストール手順等を再確認したい方は、以下のURLをご覧下さい。
http://opensourcetech.hatenablog.jp/entry/2014/02/24/231440
ネームサーバ(bind)の構築
bindをインストールし、設定(/etc/named.conf)を行います。
/etc/named.confの内容は、以下となります。
※赤字が変更・追記箇所。
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
options {
listen-on port 53 { 192.168.11.202; 127.0.0.1; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "kujirai.test.local" IN {
type master;
file "kujirai.test.local.zone";
};
zone "11.168.192.in-addr.arpa" IN {
type master;
file "kujirai.test.local.rev";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
そして、ゾーンファイルについては、
/var/namedにあるnamed.emptyをコピーして作成します。
cp -p named.empty kujirai.test.local.zone
cp -p named.empty kujirai.test.local.rev
kujirai.test.local.zoneの内容
$TTL 3H
@ IN SOA ns.kujirai.test.local. root.kujirai.test.local. (
20150129 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.kujirai.test.local.
ns IN A 192.168.11.202
www IN A 192.168.11.202
mail IN A 192.168.11.202
proxy IN A 192.168.11.202
kujirai.test.local.revの内容
$TTL 3H
@ IN SOA ns.kujirai.test.local. root.kujirai.test.local. (
20150129 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.kujirai.test.local.
202 IN PTR ns.kujirai.test.local.
202 IN PTR www.kujirai.test.local.
202 IN PTR mail.kujirai.test.local.
202 IN PTR proxy.kujirai.test.local.
その後、/etc/init.d/named start
chkconfig named onで自動起動設定をすれば完了です。
iptables -I INPUT 5 -p udp --dport 53 -j ACCEPT
iptables -I INPUT 6 -p tcp --dport 53 -j ACCEPT
として、iptablesにも通信許可ルールを追加します。
また、念の為クライアントからnslookupしておくといいでしょう。
メールプロキシ(Nginx)の構築
まず、 http://nginx.org/en/download.htmlにあるnginx-1.7.9.tar.gzを取得し、
任意の場所(今回は/tmp)に解凍します。
cd /tmp
wget http://nginx.org/download/nginx-1.7.9.tar.gz
tar zxvf nginx-1.7.9.tar.gz
続いて、configureスクリプトを実施するのですが、
今回は「--with-mail」、「--with-mail_ssl_module」オプションを使用しますので、
依存関係にあるソフトウェアを予めインストールしておきます。
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
その後、configureスクリプトを実施します。
./configure --with-mail --with-mail_ssl_module
そして、make、make installと実行します。
/usr/local/nginx/sbin/nginxに本体が配置され、
/usr/local/nginx/sbin/nginx -vや-V とすることで、
コンパイルされているモジュール情報などが確認出来ます。
続いて設定(/usr/local/nginx/conf/nginx.conf)を行います。
/usr/local/nginx/conf/nginx.confの内容(その他の設定は、コメントアウト)
worker_processes 1;
events {
worker_connections 1024;
}
mail {
server_name mail.kujirai.test.local;・・・・・サーバ名
auth_http localhost:9000/auth.php;・・・・・認証サービス
proxy on;
pop3_capabilities TOP USER UIDL LIST QUIT PASS;
imap_capabilities IMAP4rev1 UIDPLUS QUOTA AUTHENTICATE;
smtp_capabilities PIPELINING 8BITMIME DSN ;・・・・・許可コマンド指定
pop3_auth plain;
imap_auth plain;
smtp_auth none;・・・・・認証方式
server {
listen 192.168.11.202:25;
protocol smtp;
xclient off;
proxy_pass_error_message on;
}・・・・・STMP関連の設定
server {
listen 192.168.11.202:110;
protocol pop3;
}・・・・・POP3関連の設定
server {
listen 192.168.11.202:143;
protocol imap;
} ・・・・・IMAP関連の設定
}
/usr/local/nginx/sbin/nginx で起動します。
(ちなみに、 /usr/local/nginx/sbin/nginx -s stop で停止出来ます。)
また、netstat -tanで該当IPアドレスのTCP25、110、143がListenとなっていることを
確認しておきます。
iptables -I INPUT 7 -p tcp --dport 25 -j ACCEPT
※同様に110、143も通信許可ルールを追加します。
Nginxのinitスクリプトの作成
ソースパッケージからインストールした状態では手動で起動しなければなりませんので、
/etc/init.dに起動スクリプトを作成し、自動起動等が出来るようにします。
cd /etc/init.d
vi nginx
起動スクリプトの内容
#!/bin/bash
#chkconfig: 2345 80 30
#description: nginx
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin/:/usr/sbin:/usr/bin
DESC="nginx deamon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start() {
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON -s stop || echo -n " not running"
}
d_reload() {
$DAEMON -s reload || echo -n " could not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
※※以下にあるスクリプトも参考になりそうです。
http://wiki.nginx.org/RedHatNginxInitScript
実行権限を付与
chmod +x /etc/init.d/nginx
chkconfigにnginxを追加
chkconfig --add nginx
chkconfig --list nginx
これで自動起動とともに、
/etc/init.d/nginx {start|stop|restart} で起動・停止が出来るようになりました。
postfixについては、CentOS6.6インストール時にインストール済みの為、
今回の構成用に設定を変更するのみとなります。
/etc/postfix/main.cfの内容(変更部分のみ)
※メールのリレー設定などは考慮していないので、必要であれば別途設定して下さい。
myhostname = mail.kujirai.test.local
mydomain = kujirai.test.local
myorigin = $mydomain
inet_interfaces = localhost
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
mail.$mydomain, www.$mydomain, ftp.$mydomain
mynetworks = 192.168.11.0/24, 127.0.0.0/8
home_mailbox = Maildir/
設定が出来たら、postfixデーモンを再起動して完了です。
また、netstat -tan でlocalhostのTCP25がListenかチェックもしておくとよいでしょう。
インストールが完了したら、
dovecotの設定を行います。
設定を行うファイルはインクルードされているものも含めて、3つあります。
※その他設定が必要な場合、ご自身で設定追加をして下さい。
/etc/dovecot/dovecot.confの内容(変更箇所のみ)
protocols = imap pop3 lmtp
listen = localhost
/etc/dovecot/conf.d/10-mail.confの内容(変更箇所のみ)
mail_location = maildir:~/Maildir
/etc/dovecot/conf.d/10-auth.confの内容(変更箇所のみ)
disable_plaintext_auth = no
その後、dovecotを起動して完了です。
また、netstat -tan でlocalhostのTCP110、143がListenとなっていることを確認しておくといいでしょう。
コンテンツファイル(auth.php)を、ドキュメントルート(/var/www/html)に配置します。
auth.phpの内容
<?php
if (!isset($_SERVER["HTTP_AUTH_USER"] ) || !isset($_SERVER["HTTP_AUTH_PASS"] )){
fail();
}
$username=$_SERVER["HTTP_AUTH_USER"] ;
$userpass=$_SERVER["HTTP_AUTH_PASS"] ;
$protocol=$_SERVER["HTTP_AUTH_PROTOCOL"] ;
$backend_port=110;
if ($protocol=="imap") {
$backend_port=143;
}
if ($protocol=="smtp") {
$backend_port=25;
}
$backend_ip["mail.kujirai.test.local"] ="127.0.0.1";
if (!authuser($username,$userpass)){
fail();
exit;
}
$userserver=getmailserver($username);
$server_ip=(isset($backend_ip[$userserver]))?$backend_ip[$userserver] :$userserver;
pass($server_ip, $backend_port);
function authuser($user,$pass){
return true;
}
function getmailserver($user){
return "mail.kujirai.test.local";
}
function fail(){
header("Auth-Status: Invalid login or password");
exit;
}
function pass($server,$port){
header("Auth-Status: OK");
header("Auth-Server: $server");
header("Auth-Port: $port");
exit;
}
?>
また、httpdの設定ファイル(/etc/httpd/conf/httpd.conf)も設定を変更します。
httpd.confの内容(変更箇所のみ)
Listen 127.0.0.1:9000
ServerName www.kujirai.test.local
/etc/httpd/conf.d/php.confの内容(変更箇所のみ)
DirectoryIndex auth.php index.php
httpdを起動し、netstat -tan でlocalhostのTCP9000がListenになっていることを確認します。
クライアントからの動作確認
メールを使用する一般ユーザーをLinuxへ作成します。
今回は、mailuser1、mailuser2の2名を作成しました。
useradd mailuser1
passwd mailuser1
あとは、クライアントのネットワーク設定(参照DNSなど)を行い、
メーラー等にてメール送信等を確認します。
※以下では、sylpheedを使用しています。
また、Linux上で以下のようにパケットキャプチャーしてやれば、
各サービス間の通信も確認出来ます。
tcpdump -i eth0 tcp port 25 or port 110 or port 143 -w /tmp/mail_eth0.pcap
tcpdump -i lo tcp port 25 or port 110 or port 143 or port 9000 -w /tmp/mail_lo.pcap
以上、長編となりましたがNginxのメールプロキシ構築でした。