Archive for the ‘postfix’ Category

7月
02

今回は構築したメールサーバーに新しいメールアカウントを作成したいと思います。
※複数回に渡ってご紹介しているメールサーバー構築の記事のところどころでユーザアカウントの設定をしているのですが、後でメールアカウントだけを追加したい時にあちこちの記事を見直すのは大変なので今回メールアカウント追加の手順をご紹介したいと思います。

ユーザアカウント作成

ログイン後、rootユーザにスイッチします
[centos@centos ~]$ su -
パスワード: rootユーザーのパスワード

ホームディレクトリを作成せずにユーザアカウント[usertest]を作成します
[root@centos ~]# useradd -s /sbin/nologin usertest

メール受信用パスワード設定
ここではusertestユーザのメール受信用パスワードをpopusertestとします。

パスワード管理ファイルにあるusertestユーザの設定を確認します
[root@centos ~]# cat /etc/passwd|grep usertest
usertest:x:502:502::/home/usertest:/sbin/nologin
結果のusertestユーザの情報をメモ帳やテキストエディタに控えます

チャレンジ・レスポンス型認証のパスワードを発行します
[root@centos ~]# dovecotpw -s CRAM-MD5
Enter new password:popusertest
Retype new password:popusertest
{CRAM-MD5}fb2b32a0a063400765d103c1770c8bc6ccddc5d9fd5d0c9bd0fe5a2dccb3c24a

チャレンジ・レスポンス型認証のパスワード管理ファイルを作成します
[root@centos ~]# vi /etc/dvc_passwd
viエディタが起動するので入力します。
事前に控えたパスワード管理ファイルのusertestの情報を貼り付け、ユーザ名の次のxの部分をチャレンジ・レスポンス型認証のパスワードに置き換えます

usertest:{CRAM-MD5}fb2b32a0a063400765d103c1770c8bc6ccddc5d9fd5d0c9bd0fe5a2dccb3c24a:502:502::/home/usertest:/sbin/nologin

内容を保存しviエディタを終了します

メール送信用パスワード設定
ここではusertestユーザのメール送信用パスワードをsmtpusertestとします。

[root@centos ~]# saslpasswd2 -u sa-sa-ki.jp -c usertest
Password: smtpusertest
Again (for verification): smtpusertest

centosユーザにSMTP-Authパスワードが設定されたか確認します
[root@centos ~]# sasldblistusers2
usertest@sa-sa-ki.jp: userPassword

次回はメールクライアントに今回作成したメールアカウントの設定を行って、メールの送受信ができるようにしたいと思います。

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その23 CentOS 5.3 メールサーバー構築(SSL導入編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その22 CentOS 5.3 メールサーバー構築(公開編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その21 CentOS 5.3 メールサーバー構築(Dovecot設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その20 CentOS 5.3 メールサーバー構築(Postfix設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

5月
02

前回に引き続いてメールサーバーの設定を行っていきます。
今回はSSLの設定を行います。
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入するでWebサーバー用にサーバー証明書を作成済みですが、yasuの自宅サーバーはWebサーバー(www.sa-sa-ki.jp)とメールサーバー(mail.sa-sa-ki.jp)で名前が異なりますので、サーバー証明書もあえて作りわけたいと思います。

ちなみにメールサーバーにSSLを導入した場合の暗号化の範囲ですが、メールを送受信するクライアントと自宅サーバー間だけで、自宅サーバーから相手サーバー、相手サーバーから相手クライアントの間はSSLの暗号化対象ではありません。

自宅にあるPCから相手にメールを送る場合は緑線の1→2→3→4の順番でメールが送信されるのですが、暗号化されるのは1の区間だけです。
同様に外部のPCから相手にメールを送る場合は紫線のa→b→c→d→eの順番でメールが送信されるのですが、暗号化されるのはaとbの区間だけです。

OpenSSLの設定ファイル編集およびサーバー証明書作成準備

以前作成したWebサーバー用サーバー証明書作成ワークディレクトリ直下にメールサーバー用サーバー証明書作成ワークディレクトリを作成します。
[root@centos ~]# cd /etc/pki/tls/yasu
[root@centos yasu]# mkdir mailssl

以前作成したOpenSSLの設定ファイルをコピーして再利用します
[root@centos yasu]# cp openssl.cnf ./mailssl
[root@centos yasu]# cd mailssl
[root@centos mailssl]# ls -la
合計 32
drwxr-xr-x 2 root root 4096 4月 5 12:51 .
drw——- 3 root root 4096 4月 5 12:50 ..
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf

中身が空っぽのテキストファイル index.txt と01と書いた serial ファイルを作成します
[root@centos mailssl]# touch index.txt
[root@centos mailssl]# echo 01 > serial
[root@centos mailssl]# ls -la
合計 44
drwxr-xr-x 2 root root 4096 4月 5 12:51 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial

サーバー秘密鍵作成
サーバー秘密鍵を作成します。

[root@centos mailssl]# openssl genrsa -out ./server.key -des3 1024
Generating RSA private key, 1024 bit long modulus
….++++++
……….++++++
e is 65537 (0×10001)
Enter pass phrase for ./server.key:任意のパスフレーズを入力
Verifying – Enter pass phrase for ./server.key:もう一度任意のパスフレーズを入力

メールサーバーにSSLでアクセスした時に毎回パスフレーズを聞かれないようにパスフレーズ付きサーバー秘密鍵を作成します
[root@centos mailssl]# openssl rsa -in ./server.key -out ./server.key
Enter pass phrase for ./server.key:上記パスフレーズを応答
writing RSA key

[root@centos mailssl]# ls -la
合計 52
drwxr-xr-x 2 root root 4096 4月 5 12:52 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial
-rw-r–r– 1 root root 963 4月 5 12:52 server.key

server.key というファイルが作成されました

CSR(証明書署名要求)作成
「サーバー秘密鍵作成」で作成したサーバー秘密鍵ファイルを使ってCSR(証明書署名要求)を作成します。

[root@centos mailssl]# openssl req -new -days 3650 -config ./openssl.cnf -key ./server.key -out ./server.csr
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) [GB]:JP
State or Province Name (full name) [Berkshire]:chiba
Locality Name (eg, city) [Newbury]:urayasu
Organization Name (eg, company) [My Company Ltd]:sa-sa-ki.jp
Organizational Unit Name (eg, section) []:何も入力しないで[Enter]キーを押下
Common Name (eg, your name or your server’s hostname) []:mail.sa-sa-ki.jp
Email Address []:何も入力しないで[Enter]キーを押下

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:何も入力しないで[Enter]キーを押下
An optional company name []:何も入力しないで[Enter]キーを押下
[root@centos mailssl]# ls -la
合計 60
drwxr-xr-x 2 root root 4096 4月 5 12:53 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial
-rw-r–r– 1 root root 639 4月 5 12:53 server.csr
-rw-r–r– 1 root root 887 4月 5 12:52 server.key

server.csr というファイルが作成されました

CSRに署名
CSRファイルに署名を行い、サーバー証明書を作成します。

[root@centos mailssl]# openssl ca -config ./openssl.cnf -days 3650 -in ./server.csr -keyfile ../cakey.pem -cert ../cacert.pem -out ./server.pem
※サーバー証明書を作成するのに必要な認証局の秘密鍵と認証局の証明書は以前Webサーバーのサーバー証明書を作成した時に作ったものを指定します
Using configuration from ./openssl.cnf
Enter pass phrase for ../cakey.pem:以前Webサーバーのサーバー証明書を作成した時に作った認証局のパスフレーズを応答
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0×1)
Validity
Not Before: Apr 5 04:01:14 2009 GMT
Not After : Apr 3 04:01:14 2019 GMT
Subject:
countryName = JP
stateOrProvinceName = chiba
organizationName = sa-sa-ki.jp
commonName = mail.sa-sa-ki.jp
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
38:88:E0:47:C2:D6:C0:2F:C7:95:8A:DF:D1:BB:76:61:AB:47:CC:96
X509v3 Authority Key Identifier:
keyid:FE:40:F5:F1:60:78:3F:F4:BF:63:2B:FE:DE:CE:D9:32:5B:44:05:52

Certificate is to be certified until Apr 3 04:01:14 2019 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

各ファイルをrootユーザ読み取り専用に権限を変更します
[root@centos mailssl]# chmod 400 *
[root@centos mailssl]# ls -l
合計 48
-r——– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-r——– 1 root root 0 4月 5 12:55 serial
-r——– 1 root root 639 4月 5 12:53 server.csr
-r——– 1 root root 887 4月 5 12:52 server.key
-r——– 1 root root 830 4月 5 12:54 server.pem

server.pemというファイルが作成されました

サーバー証明書ファイルとサーバー秘密鍵ファイルコピー
サーバー証明書ファイルとサーバー秘密鍵ファイルを所定の場所にコピーします。

[root@centos mailssl]# cp server.key /etc/postfix
[root@centos mailssl]# cp server.pem /etc/postfix
[root@centos mailssl]# cp server.key /etc/pki/dovecot
[root@centos mailssl]# cp server.pem /etc/pki/dovecot

Postfix(送信サーバー)のSSL用設定ファイルを変更
Postfix(送信サーバー)用設定ファイルを変更します。

[root@centos mailssl]# cd
[root@centos ~]# vi /etc/postfix/main.cf

viエディタが起動するので追加します

# SMTP SSL
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/server.pem
smtpd_tls_key_file = /etc/postfix/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache

内容を保存しviエディタを終了します

[root@centos ~]# vi /etc/postfix/master.cf

viエディタが起動するので修正します

#
# Postfix master process configuration file. For details on the format
# of the file, see the master(5) manual page (command: “man 5 master”).
#
# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n – n – – smtpd
#submission inet n – n – – smtpd
# -o smtpd_enforce_tls=yes
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_client_restrictions=permit_sasl_authenticated,reject
以下4行の先頭の#を外してコメントを解除します
smtps inet n – n – – smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject

#628 inet n – n – – qmqpd
pickup fifo n – n 60 1 pickup
cleanup unix n – n – 0 cleanup
qmgr fifo n – n 300 1 qmgr
#qmgr fifo n – n 300 1 oqmgr
tlsmgr unix – – n 1000? 1 tlsmgr
rewrite unix – – n – – trivial-rewrite
bounce unix – – n – 0 bounce
defer unix – – n – 0 bounce
trace unix – – n – 0 bounce
verify unix – – n – 1 verify
flush unix n – n 1000? 0 flush
proxymap unix – – n – – proxymap
smtp unix – – n – – smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay unix – – n – – smtp
-o fallback_relay=
# -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq unix n – n – – showq
error unix – – n – – error
discard unix – – n – – discard
local unix – n n – – local
virtual unix – n n – – virtual
lmtp unix – – n – – lmtp
anvil unix – – n – 1 anvil
scache unix – – n – 1 scache

内容を保存しviエディタを終了します

Dovecot(受信サーバー)のSSL用設定ファイルを変更
Dovecot(受信サーバー)用設定ファイルを変更します。

[root@centos ~]# vi /etc/dovecot.conf

viエディタが起動するので追加します

# Disable SSL/TLS support.
#ssl_disable = no
ssl_disable = no

# PEM encoded X.509 SSL/TLS certificate and private key. They’re opened before
# dropping root privileges, so keep the key file unreadable by anyone but
# root. Included doc/mkcert.sh can be used to easily generate self-signed
# certificate, just make sure to update the domains in dovecot-openssl.cnf
#ssl_cert_file = /etc/pki/dovecot/certs/dovecot.pem
#ssl_key_file = /etc/pki/dovecot/private/dovecot.pem
ssl_cert_file = /etc/pki/dovecot/server.pem
ssl_key_file = /etc/pki/dovecot/server.key

内容を保存しviエディタを終了します

ファイアウォールの設定変更

[root@centos ~]# vi /etc/sysconfig/iptables

viエディタが起動するので追加します

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp –dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 110 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 123 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 123 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 143 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
下記6行を追加してTCPの465番ポート(smtps)、993番ポート(imaps)、995番ポート(pop3s)を開放します
IP制御をしてローカルネットワーク内と特定の外部からのみ接続できるようにします

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 465 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 465 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 993 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 993 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 995 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 995 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT

-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT

内容を保存しviエディタを終了します

設定内容を反映します
[root@centos ~]# /etc/rc.d/init.d/iptables restart
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]
iptables モジュールを読み込み中ip_conntrack_netbios_ns [ OK ]

ルータの設定を変更変更
ルータの設定を変更してTCPの465番ポート(smtps)、993番ポート(imaps)、995番ポート(pop3s)を開放します。
開放方法はルータの設定手順に従って実施してください。

次回はThunderbirdの設定を行い、メールの送受信を行います。

■ 参考サイト ■
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
メールサーバー間通信内容暗号化(OpenSSL+Postfix+Dovecot) – CentOSで自宅サーバー構築
はじめての自宅サーバ構築 Fedora/CentOS – 通信内容暗号化(OpenSSL & mod_SSL) 〜 メール編 〜

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その21 CentOS 5.3 メールサーバー構築(Dovecot設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その20 CentOS 5.3 メールサーバー構築(Postfix設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

4月
21

前回に引き続いてメールサーバーの設定を行っていきます。
今回はメールサーバーの外部公開を行います。

SendMailサービス停止とデフォルトメールサーバー変更
CentOSのデフォルトメールサーバーはSendMailですので、Postfixに切り替えます。

SendMailサービスを停止します
[root@centos ~]# /etc/rc.d/init.d/sendmail stop
sendmailを停止中: [ OK ]
sm-clientを停止中: [ OK ]

SendMailサービスの自動起動設定を解除します
[root@centos ~]# chkconfig sendmail off
[root@centos ~]# chkconfig –list sendmail
sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

デフォルトメールサーバーをsendmailからpostfixに変更します
[root@centos ~]# alternatives –config mta
2 プログラムがあり ‘mta’ を提供します。

選択 コマンド
———————————————–
*+ 1 /usr/sbin/sendmail.sendmail
2 /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:2

メールサーバーを外部公開する為に、CentOSのファイアウォールの設定を変更します。

[root@centos ~]# vi /etc/sysconfig/iptables
viエディタが起動するので設定ファイルを変更します。
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp –dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
下記1行を追加してTCPの25番ポート(smtp)を開放します
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
下記4行を追加してTCPの110番ポート(pop)とTCPの143番ポート(imap)を開放します。
IP制御をしてローカルネットワーク内と特定の外部からのみ接続できるようにします

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 110 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 110 -s xxx.xxx.xx.xx(外部IPアドレス)/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 143 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 143 -s xxx.xxx.xx.xx(外部IPアドレス)/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited

内容を保存しviエディタを終了します

設定内容を反映します
[root@centos ~]# /etc/rc.d/init.d/iptables restart
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]
iptables モジュールを読み込み中ip_conntrack_netbios_ns [ OK ]

PostfixとSMTP-Auth、Dovecotサービスの起動およびOS再起動時の自動起動設定
Postfix(送信メールサーバー)、SMTP-Auth(送信メールサーバーユーザ認証)、Dovecot(受信メールサーバー)のサービスを起動し、OS再起動時にもサービスを自動起動するようにします。

Postfixの起動
[root@centos ~]# /etc/rc.d/init.d/postfix start
postfix を起動中: [ OK ]

OS再起動時にPostfixを自動起動するようにします
[root@centos ~]# chkconfig postfix on
[root@centos ~]# chkconfig –list postfix
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off

SMTP-Authの起動
[root@centos ~]# /etc/rc.d/init.d/saslauthd start
saslauthd を起動中: [ OK ]

OS再起動時にSMTP-Authを自動起動するようにします
[root@centos ~]# chkconfig saslauthd on
[root@centos ~]# chkconfig –list saslauthd
saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Dovecotの起動
[root@centos ~]# /etc/rc.d/init.d/dovecot start
Dovecot Imap を起動中: [ OK ]

OS再起動時にDovecotを自動起動するようにします
[root@centos ~]# chkconfig dovecot on
[root@centos ~]# chkconfig –list dovecot
dovecot 0:off 1:off 2:on 3:on 4:on 5:on 6:off

telnetでSMTP-Authの設定を確認
telnetコマンドでSMTP-Authが正しく設定されているか確認します。

[root@centos ~]# telnet localhost 25
Trying 192.168.0.20…
Connected to mail.sa-sa-ki.jp (192.168.0.20).
Escape character is ‘^]’.
Connection closed by foreign host.
[root@centos ~]#

telnetで25番ポートに接続した瞬間に切断されてしまいました。
ログを調べてみます。

[root@centos ~]# view /var/log/maillog
Apr 3 17:05:24 centos postfix/smtpd[9767]: warning: xsasl_cyrus_server_get_mechanism_list: no applicable SASL mechanisms
Apr 3 17:05:24 centos postfix/smtpd[9767]: fatal: no SASL authentication mechanisms
Apr 3 17:05:25 centos postfix/master[9762]: warning: process /usr/libexec/postfix/smtpd pid 9767 exit status 1
Apr 3 17:05:25 centos postfix/master[9762]: warning: /usr/libexec/postfix/smtpd: bad command startup — throttling

・warning: xsasl_cyrus_server_get_mechanism_list: no applicable SASL mechanisms
・fatal: no SASL authentication mechanisms
警告やエラーが発生していました。
インターネットで調べてみたところDevelog – Linux,MySQL,PHP,SEO,Debian,PEAR,Smarty,Blog -:【CentOS5 Postfix】SASLを利用したSMTP-AUTHで認証エラーに対応策が出ていました。
SMTP-AUTHに必要なモジュールが足りなかったようなので追加します。

現在インストールされているSMTP-Auth関連のパッケージを調べます
[root@centos ~]# yum list | grep sasl*
cyrus-sasl.i386 x.x.xx-x installed
cyrus-sasl-lib.i386 x.x.xx-x installed
cyrus-sasl-plain.i386 x.x.xx-x installed
cyrus-sasl-devel.i386 x.x.xx-x base
cyrus-sasl-gssapi.i386 x.x.xx-x base
cyrus-sasl-ldap.i386 x.x.xx-x base
cyrus-sasl-md5.i386 x.x.xx-x base
cyrus-sasl-ntlm.i386 x.x.xx-x base
cyrus-sasl-sql.i386 x.x.xx-x base

メール送信の認証方法はチャレンジ・レスポンス型認証に指定しているのですが、チャレンジ・レスポンス型認証に必要な[cyrus-sasl-md5]がインストールされていませんでしたので追加します。

cyrus-sasl-md5をインストールします
[root@centos ~]# yum -y install cyrus-sasl-md5
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* base: www.ftp.ne.jp
* updates: ftp.twaren.net
* addons: www.ftp.ne.jp
* extras: www.ftp.ne.jp
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
–> Running transaction check
—> Package cyrus-sasl-md5.i386 0:x.x.xx-x set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Installing:
cyrus-sasl-md5 i386 x.x.xx-x base 45 k

Transaction Summary
====================================================================================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)

Total download size: 45 k
Downloading Packages:
cyrus-sasl-md5-x.x.xx-x.i386.rpm | 45 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : cyrus-sasl-md5 [1/1]

Installed: cyrus-sasl-md5.i386 0:x.x.xx-x
Complete!

再度telnetコマンドでSMTP-Authが正しく設定されているか確認します
[root@centos ~]# telnet localhost 25
Trying 127.0.0.1…
Connected to centos.sa-sa-ki.jp (127.0.0.1).
Escape character is ‘^]’.
220 mail.sa-sa-ki.jp ESMTP unknown
EHLO local

EHLOコマンドを入力したのですが応答が返ってきません。
再度ログファイルを確認します。

[root@centos ~]# view /var/log/maillog
Apr 3 17:35:37 centos postfix/smtpd[4521]: fatal: open database /etc/postfix/access.db: No such file or directory
Apr 3 17:35:38 centos postfix/master[4451]: warning: process /usr/libexec/postfix/smtpd pid 4521 exit status 1
Apr 3 17:35:38 centos postfix/master[4451]: warning: /usr/libexec/postfix/smtpd: bad command startup — throttling

/etc/postfix/access.dbファイルが開けないと警告がでています。
これはpostfixの設定ファイルmain.cfに追記したsmtpd_client_restrictionsパラメタのcheck_client_access hash:/etc/postfix/accessのデータベースファイルがない為に警告がでているのでデータベースファイルを作成します。
同様にsmtpd_sender_restrictionsパラメタのcheck_sender_access hash:/etc/postfix/reject_senderもデータベースファイル(データベースファイルを作成する元のテキストファイルも)がないので作成します。

[root@centos ~]# postmap /etc/postfix/access
[root@centos ~]# touch /etc/postfix/reject_sender
[root@centos ~]# postmap /etc/postfix/reject_sender

再度telnetコマンドでSMTP-Authが正しく設定されているか確認する前にpostfixとsaslauthdのサービスを再起動します
[root@centos ~]# /etc/rc.d/init.d/postfix start
postfix を停止中: [ OK ]
postfix を起動中: [ OK ]

[root@centos ~]# /etc/rc.d/init.d/saslauthd start
saslauthd を停止中: [ OK ]
saslauthd を起動中: [ OK ]

再度telnetコマンドでSMTP-Authが正しく設定されているか確認します
[root@centos ~]# telnet localhost 25
Trying 127.0.0.1…
Connected to centos.sa-sa-ki.jp (127.0.0.1).
Escape character is ‘^]’.
220 mail.sa-sa-ki.jp ESMTP unknown
EHLO local
250-mail.sa-sa-ki.jp
250-PIPELINING
250-SIZE 10240000
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5が表示されていればSMTP-Authの設定は正常です
250-AUTH DIGEST-MD5 CRAM-MD5
250-AUTH=DIGEST-MD5 CRAM-MD5
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

QUIT
221 2.0.0 Bye
Connection closed by foreign host.

ルータの設定を変更してTCPの25番、110番、143番ポートを開放します。
開放方法はルータの設定手順に従って実施してください。

次回はSSLの設定を行います。

■ 参考サイト ■
はじめての自宅サーバ構築 Fedora/CentOS – メールサーバ(SMTP)の構築(Postfix)
メールサーバー構築(Postfix+Dovecot) – CentOSで自宅サーバー構築
Postfix設定パラメータ
dovecotでCRAM-MD5認証を使えるようにする – あれだよ、あれ……なんだっけ?

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その21 CentOS 5.3 メールサーバー構築(Dovecot設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その20 CentOS 5.3 メールサーバー構築(Postfix設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

4月
19

前回に引き続いてメールサーバーの設定を行っていきます。
今回はDovecot(POP3/IMAP)の設定を行います。

Dovecot(POP3/IMAP)設定

Dovecot(POP3/IMAP)の設定ファイルを変更します
[root@centos ~]# vi /etc/dovecot.conf
viエディタが起動するので修正します

# Protocols we want to be serving: imap imaps pop3 pop3s
# If you only want to use dovecot-auth, you can set this to “none”.
#protocols = imap imaps pop3 pop3s
受信用メールサーバーとして使用するプロトコルを指定します
protocols = imap imaps pop3 pop3s

auth default {
# Space separated list of wanted authentication mechanisms:
# plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi
# NOTE: See also disable_plaintext_auth setting.
#mechanisms = plain
受信メールの認証をチャレンジ・レスポンス型対応に変更します
CRAM-MD5(シーアールエーエム・エムディーファイブ。Challenge-Response Authentication Mechanism Message Digest 5):RBB TODAY (ブロードバンド辞典)
チャレンジ・レスポンス型の認証にするとネットワーク上にパスワードが流れないので、セキュアな受信メールサーバーになります

mechanisms = cram-md5

# passwd-like file with specified location
#
#passdb passwd-file {
 # Path for passwd-file
 #args =
#}
チャレンジ・レスポンス型認証のパスワード管理ファイルを指定します
※後の手順で指定したチャレンジ・レスポンス型認証パスワード管理ファイルを指定したファイル名で作成するのでひとまず手順通りファイル名を指定してください

passdb passwd-file {
 args = /etc/dvc_passwd
}

# Location for users’ mailboxes. This is the same as the old default_mail_env
# setting. The default is empty, which means that Dovecot tries to find the
# mailboxes automatically. This won’t work if the user doesn’t have any mail
# yet, so you should explicitly tell Dovecot the full location.
#
# If you’re using mbox, giving a path to the INBOX file (eg. /var/mail/%u)
# isn’t enough. You’ll also need to tell Dovecot where the other mailboxes are
# kept. This is called the “root mail directory”, and it must be the first
# path given in the mail_location setting.
#
# There are a few special variables you can use, eg.:
#
# %u – username
# %n – user part in user@domain, same as %u if there’s no domain
# %d – domain part in user@domain, empty if there’s no domain
# %h – home directory
#
# See doc/wiki/Variables.txt for full list. Some examples:
#
# mail_location = maildir:~/Maildir
# mail_location = mbox:~/mail:INBOX=/var/mail/%u
# mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#
#
#
#mail_location =
受信メールの管理方法をPostfixの設定ファイルでも指定した1つのメール1ファイルで管理するMaildirに形式に変更します
mail_location = maildir:~/Maildir

内容を保存しviエディタを終了します

Dovecot(POP3/IMAP)チャレンジ・レスポンス型認証のパスワード管理ファイル作成
仮に認証させたいユーザをcentos、パスワードをcentosとします。

パスワード管理ファイルの内容を確認してcentosユーザの情報を控えます
more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin



centos:x:500:500::/home/centos:/bin/bash ←対象ユーザの設定を控えます

チャレンジ・レスポンス型認証のパスワードを発行します
※ここでは仮に認証させたいユーザcentosのパスワードをcentosとします

[root@centos ~]# dovecotpw -s CRAM-MD5
Enter new password:centos
Retype new password:centos
{CRAM-MD5}decc4130dbbe62bcea1a38ca799c4f32a8e5752065f007015383757b4bd3d3b2

チャレンジ・レスポンス型認証のパスワード管理ファイルを作成します
[root@centos ~]# vi /etc/dvc_passwd
viエディタが起動するので先の手順で控えた対象ユーザの設定(centos:x:500:500::/home/centos:/bin/bash)を入力して、さらにユーザ名の次のxの部分(下記赤字部分)をチャレンジ・レスポンス型認証のパスワードに置き換えます
centos:{CRAM-MD5}decc4130dbbe62bcea1a38ca799c4f32a8e5752065f007015383757b4bd3d3b2:500:500::/home/centos:/bin/bash

他のメールアカウントを追加する場合は、同様の手順でこのファイルにユーザ情報とパスワードを追加してください。

内容を保存しviエディタを終了します

次回は設定したSMTP、POP3/IMAPのサービスを公開します。

■ 参考サイト ■
はじめての自宅サーバ構築 Fedora/CentOS – メールサーバ(SMTP)の構築(Postfix)
メールサーバー構築(Postfix+Dovecot) – CentOSで自宅サーバー構築
Postfix設定パラメータ
dovecotでCRAM-MD5認証を使えるようにする – あれだよ、あれ……なんだっけ?

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その20 CentOS 5.3 メールサーバー構築(Postfix設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

4月
17

今回から複数回に渡ってメールサーバーを構築したいと思います。
今回はPostfix(SMTP)とDovecot(POP3/IMAP)のインストールとPostfix(SMTP)の設定を行います。
またPostfix(SMTP)は不正中継されないようCyrusSASLを使ってメール送信時にユーザ名とパスワードで認証が必要なSMTP-Auth機能を導入します。

Postfix、Dovecotインストール

ログイン後、rootユーザにスイッチします
[centos@centos ~]$ su -
パスワード: rootユーザーのパスワード

Postfix(SMTP)とDovecot(POP3/IMAP)をインストールします
[root@centos ~]# yum -y install postfix dovecot
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* updates: rsync.atworks.co.jp
* addons: www.ftp.ne.jp
* extras: www.ftp.ne.jp
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
–> Running transaction check
—> Package dovecot.i386 x:x.x.x-x.xxx set to be updated
—> Package postfix.i386 x:x.x.x-x.x.xxx_x set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================================
Installing:
dovecot i386 x.x.x-x.xxx base 1.6 M
postfix i386 x:x.x.x-x.x.xxx_x base 3.6 M

Transaction Summary
====================================================================================================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)

Total download size: 5.2 M
Downloading Packages:
(1/2): dovecot-x.x.x-x.xxx.i386.rpm | 1.6 MB 00:00
(2/2): postfix-x.x.x-x.x.xxx_x.i386.rpm | 3.6 MB 00:00
—————————————————————————————————————————————————-
Total 4.8 MB/s | 5.2 MB 00:01
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : dovecot [1/2]
Installing : postfix [2/2]

Installed: dovecot.i386 x:x.x.x-x.xxx postfix.i386 x:x.x.x-x.x.xxx_x
Complete!

Postfix(SMTP)設定

Postfix(SMTP)の設定ファイルを変更します
※設定ファイルの設定時参考サイト:Postfix設定パラメータ

[root@centos ~]# vi /etc/postfix/main.cf
viエディタが起動するので修正します

# INTERNET HOST AND DOMAIN NAMES
#
# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
#myhostname = host.domain.tld
#myhostname = virtual.domain.tld
FQDN名を入力します
myhostname = mail.sa-sa-ki.jp

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
#mydomain = domain.tld
ドメイン名を入力します
mydomain = sa-sa-ki.jp

# SENDING MAIL
#
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to append $myhostname,
# which is fine for small sites. If you run a domain with multiple
# machines, you should (1) change this to $mydomain and (2) set up
# a domain-wide alias database that aliases each user to
# user@that.users.mailhost.
#
# For the sake of consistency between sender and recipient addresses,
# myorigin also specifies the default domain name that is appended
# to recipient addresses that have no @domain part.
#
#myorigin = $myhostname
#myorigin = $mydomain
送信者メールアドレスの@以降をドメイン名にする
myorigin = $mydomain

# RECEIVING MAIL

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on. By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes.
#
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
外部からのメール受信を許可します
#inet_interfaces = localhost
inet_interfaces = all

# The mydestination parameter specifies the list of domains that this
# machine considers itself the final destination for.
#
# These domains are routed to the delivery agent specified with the
# local_transport parameter setting. By default, that is the UNIX
# compatible delivery agent that lookups all recipients in /etc/passwd
# and /etc/aliases or their equivalent.
#
# The default is $myhostname + localhost.$mydomain. On a mail domain
# gateway, you should also include $mydomain.
#
# Do not specify the names of virtual domains – those domains are
# specified elsewhere (see VIRTUAL_README).
#
# Do not specify the names of domains that this machine is backup MX
# host for. Specify those names via the relay_domains settings for
# the SMTP server, or use permit_mx_backup if you are lazy (see
# STANDARD_CONFIGURATION_README).
#
# The local machine is always the final destination for mail addressed
# to user@[the.net.work.address] of an interface that the mail system
# receives mail on (see the inet_interfaces parameter).
#
# Specify a list of host or domain names, /file/name or type:table
# patterns, separated by commas and/or whitespace. A /file/name
# pattern is replaced by its contents; a type:table is matched when
# a name matches a lookup key (the right-hand side is ignored).
# Continue long lines by starting the next line with whitespace.
#
# See also below, section “REJECTING MAIL FOR UNKNOWN LOCAL USERS”.
#
自分のドメイン宛メールを受信できるように設定します
#mydestination = $myhostname, localhost.$mydomain, localhost
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
# mail.$mydomain, www.$mydomain, ftp.$mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

# Alternatively, you can specify the mynetworks list by hand, in
# which case Postfix ignores the mynetworks_style setting.
#
# Specify an explicit list of network/netmask patterns, where the
# mask specifies the number of bits in the network part of a host
# address.
#
# You can also specify the absolute pathname of a pattern file instead
# of listing the patterns here. Specify type:table for table-based lookups
# (the value on the table right-hand side is not used).
#
ローカルホストとローカルネットワークを設定します
#mynetworks = 168.100.189.0/28, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table
mynetworks = 192.168.0.0/24, 127.0.0.0/8

# The relay_domains parameter restricts what destinations this system will
# relay mail to. See the smtpd_recipient_restrictions description in
# postconf(5) for detailed information.
#
# By default, Postfix relays mail
# – from “trusted” clients (IP address matches $mynetworks) to any destination,
# – from “untrusted” clients to destinations that match $relay_domains or
# subdomains thereof, except addresses with sender-specified routing.
# The default relay_domains value is $mydestination.
#
# In addition to the above, the Postfix SMTP server by default accepts mail
# that Postfix is final destination for:
# – destinations that match $inet_interfaces or $proxy_interfaces,
# – destinations that match $mydestination
# – destinations that match $virtual_alias_domains,
# – destinations that match $virtual_mailbox_domains.
# These destinations do not need to be listed in $relay_domains.
#
# Specify a list of hosts or domains, /file/name patterns or type:name
# lookup tables, separated by commas and/or whitespace. Continue
# long lines by starting the next line with whitespace. A file name
# is replaced by its contents; a type:name table is matched when a
# (parent) domain appears as lookup key.
#
# NOTE: Postfix will not automatically forward mail for domains that
# list this system as their primary or backup MX host. See the
# permit_mx_backup restriction description in postconf(5).
#
#relay_domains = $mydestination
中継するドメインを指定します
relay_domains = $mydestination

# DELIVERY TO MAILBOX
#
# The home_mailbox parameter specifies the optional pathname of a
# mailbox file relative to a user’s home directory. The default
# mailbox file is /var/spool/mail/user or /var/mail/user. Specify
# “Maildir/” for qmail-style delivery (the / is required).
#
#home_mailbox = Mailbox
#home_mailbox = Maildir/
Postfixの受信メールの管理方法として複数のメールを1ファイルで管理するMailboxと1つのメール1ファイルで管理するMaildirがあるのですが、ここでは仮にメールファイルが破損した場合に影響の少ないMaildir形式に変更します
home_mailbox = Maildir/

# SHOW SOFTWARE VERSION OR NOT
#
# The smtpd_banner parameter specifies the text that follows the 220
# code in the SMTP server’s greeting banner. Some people like to see
# the mail version advertised. By default, Postfix shows no version.
#
# You MUST specify $myhostname at the start of the text. That is an
# RFC requirement. Postfix itself does not care.
#
#smtpd_banner = $myhostname ESMTP $mail_name
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
Postfixのバージョン情報を隠蔽します
smtpd_banner = $myhostname ESMTP unknown

メールアカウントが存在か確認する為のSMTPのVRFYコマンド無効にします
disable_vrfy_command = yes

SMTP-Auth(ユーザとパスワードによる認証)を有効にします
smtpd_sasl_auth_enable = yes

SMTP-Authの認証制限を設定します
匿名ログイン(noanonymous)と平文パスワード認証(noplaintext)を不許可にします

smtpd_sasl_security_options = noanonymous, noplaintext

ドメイン名を指定します
※SMTP-Authのパスワード登録コマンド実行時に必要なパラメタがここで指定したドメイン名と一致している必要があります

smtpd_sasl_local_domain = $mydomain

ここからは不正中継防止の設定になります
SMTP接続の要求を行うクライアントのホスト名/IPアドレス制限を行います
 permit_mynetworks:
  クライアントのIPアドレスが$mynetworksにリストアップされている場合
  要求を許可します
 reject_rhsbl_client:
  クライアントのホスト名が指定したドメインにリストアップされている場合
  要求を拒否します
 reject_rbl_client:
  クライアントのIPアドレスが指定したドメインにリストアップされている場合
  要求を拒否します
 reject_unknown_client:
  クライアントのIPアドレスがDNSサーバーで名前解決できない場合
  要求を拒否します
 check_client_access:
  指定したファイルに記載されているクライアントのホスト名、IPアドレス、
  ドメイン名を個別に拒否します
 permit:
  要求を許可します

smtpd_client_restrictions = permit_mynetworks,
               reject_rhsbl_client rhsbl.ahbl.org,
               reject_rbl_client spamcop.net,
               reject_rbl_client dynablock.wirehub.net,
               reject_rbl_client sbl.spamhaus.org,
               check_client_access hash:/etc/postfix/access,
               permit

受信者アドレスの制限を行います
 permit_mynetworks:
  クライアントのIPアドレスが$mynetworksにリストアップされている場合
  要求を許可します
 permit_sasl_authenticated:
  クライアントSMTP-Auth認証に成功した場合に要求を許可します
 reject_unauth_destination:
  メールを転送する場合は送信先メールアドレスが $relay_domains または
  そのサブドメインにマッチし、送信者指定のルーティング (user@elsewhere@domain)
  を含まない場合、またはPostfixが最終配送先の場合は送信先メールアドレスが
  $mydestination や$inet_interfaces、$proxy_interfaces、$virtual_alias_domains、
  $virtual_mailbox_domains にマッチし、送信者指定の
  ルーティング (user@elsewhere@domain) を含まない場合に
  要求を拒否します

smtpd_recipient_restrictions = permit_mynetworks,
                 permit_sasl_authenticated,
                 reject_unauth_destination

送信者アドレスの制限を行います
 reject_unknown_sender_domain:
  Postfixが送信者アドレスの最終配送先ではなく、
  かつMAIL FROMアドレスにDNS AもしくはMXレコードがない場合、
  または長さがゼロのMXホスト名を持つような
  不正なMXレコードを持つ場合に要求を拒否します
 reject_non_fqdn_sender:
  送信元メールアドレスが FQDN 形式でない場合に要求を拒否します
 check_sender_access:
  指定したファイルに記載されているクライアントのホスト名、IPアドレス、
  ドメイン名を個別に拒否します

smtpd_sender_restrictions = reject_unknown_sender_domain,
               reject_non_fqdn_sender,
               check_sender_access hash:/etc/postfix/reject_sender

内容を保存しviエディタを終了します

SMTP-Auth パスワード設定

設定ファイルを変更します
[root@centos ~]# vi /usr/lib/sasl2/smtpd.conf
viエディタが起動するので修正します
pwcheck_method: auxprop saslauthd(Linuxユーザ認証)からauxprop(SASLユーザ認証)に変更します
~
内容を保存しviエディタを終了します

centosユーザにSMTP-Authパスワードを設定します
Linuxユーザ認証のパスワードと違うパスワードを設定するとセキュリティが向上します

※-uで指定する名前(ここではsa-sa-ki.jp)はPostfix(SMTP)の設定ファイルで指定したsmtpd_sasl_local_domainのドメイン名と一致している必要があります
[root@centos ~]# saslpasswd2 -u sa-sa-ki.jp -c centos
Password: 登録したいパスワードを入力します
Again (for verification): もう一度登録したいパスワードを入力します

centosユーザにSMTP-Authパスワードが設定されたか確認します
[root@centos ~]# sasldblistusers2
centos@sa-sa-ki.jp: userPassword

※centosユーザのSMTP-Authパスワードを削除するには・・・
[root@centos ~]# saslpasswd2 -d centos -u sa-sa-ki.jp

SMTP-Authのパスワードは/etc/sasldb2ファイルに管理されるのですが、postfixグループがそのファイルを読めるように所有グループを変更します
[root@centos ~]# chgrp postfix /etc/sasldb2
[root@centos ~]# ls -l /etc/sasldb2
-rw-r—– 1 root postfix 12288 4月 3 09:49 /etc/sasldb2

メールボックスの設定

新規ユーザ作成時にメールボックスを自動作成するよう設定します
[root@centos ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
[root@centos ~]# chmod -R 700 /etc/skel/Maildir

既存ユーザにメールボックスを作成します
SSHのRSAプロトコルを設定したユーザだと受信メールサーバーがうまく認証できず、メール受信ができませんでした。SSHのRSAプロトコルを設定していないユーザで設定をしてください
[root@centos ~]# mkdir -p /home/centos/Maildir/{new,cur,tmp}
[root@centos ~]# chmod -R 700 /home/centos/Maildir
[root@centos ~]# chown centos.centos /home/centos/Maildir

※新規ユーザを作成するには・・・
[root@centos ~]# useradd -s /bin/nologin test

※ホームディレクトリを削除しつつユーザを削除するには・・・
[root@centos ~]# userdel -r test

次回はDovecot(POP3/IMAP)の設定を行います。

■ 参考サイト ■
はじめての自宅サーバ構築 Fedora/CentOS – メールサーバ(SMTP)の構築(Postfix)
メールサーバー構築(Postfix+Dovecot) – CentOSで自宅サーバー構築
Postfix設定パラメータ
Postfixでspamメール送信元・送信者による制限の巻 – PukiWiki
access – Postfix アクセステーブルの書式
Postfix SASL Howto

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

  • Search:
  • flashcastとは?

    東京を中心に、現在フリーランスとして活動しているITエンジニア、および、かつてフリーランスとして活動していた起業家達が立ち上げたコミュニティーです。

    みんなで集まって面白いことをやろう!形に残そう!ということで、ブログをはじめました。

    技術情報や、フリーエンジニアに役立つ情報などを、ご紹介できたらと思っています。

    お問い合わせ:
    info@flashcast.jp
  • カレンダー

    2010年7月
    日曜日 月曜日 火曜日 水曜日 木曜日 金曜日 土曜日
    « 6月    
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • メンバー紹介

    もじゃもじゃ
    flashcastのリーダー

    3年ほどフリーのITエンジニアとして活動。現在は、社員2名の株式会社を経営しています。

    一攫千金を夢見る野心家です。

    ライブキャスト

    yasu
    ダイバー

    自宅サーバーでホームページを作り始めました。

    少しずつ記事を増やしていきますので足を運んでください。

    よろしくお願いします。

    sa-sa-ki.jp

    のら
    たびびと

    ねこ好きに悪人はいなーいっ!!

    バイクや車も好きです。

    めぐ
    デザイナーのたまご

    音楽とデザインとお酒をこよなく愛する永遠のダイエッター。

    現在ペンタブレットでイラストを勉強中。

    Hiro
    コンサル

    PMやSEの案件を業務委託で請けることが多いですが、小規模案件も受託でやっています。

    得意な分野はマイクロソフト製品や関連技術によるシステム構築です。

    KEI
    取締役の風格

    最年少なのに、メンバーで1番の貫禄の持ち主!?

    C#や.netなどサーバ側の開発が得意。

    ろっきー
    美食家★パパ

    自分にとっての息抜きは、ドライブして温泉に入って、美味しいご飯を食べる事。

    ココロとカラダのリフレッシュを大切にし、日々の仕事に励む一児の父親です。

    郵便番号検索

    my-hobby

    とのさま
    げーむのおうさま

    大人なのに好きなことしかやらない駄目人間。

    Web系が得意、アクセスは苦手><

    tonosamart.com

    セクレタリアト
    ギャンブラー

    フリーランス時代は仲間の現場を探すことが多く、それをきっかけに会社を設立。

    現在はSI業に特化せず、他の業種にも興味を持ち始めています。

    メドレー株式会社

  • 広告