Contents
1.送信サーバーPostfix の導入
Postfix はメールを送信するSMTP サーバーです。
このPostfixにSMTP-AUTH を実装してやれば、比較的安全に SMTP サーバを外部から利用可能になります。なぜなら通常の SMTP は、メール送信時にはアカウント名、パスワードを必要としません。その為、悪意ある人がスパム目的で不正利用する事も可能でした。メールの受信時(POP サーバ)では普通に認証します。
しかし、SMTP-AUTH はメール送信時にもアカウント名、パスワードで認証する事により、SMTP サーバの利用者をサーバに登録してあるアカウント名、パスワードと照合する事で利用者を限定できる様になるのです。
そのため、外部からメールを送信する為に、SMTP認証機能が付いたメーラーを構築いたします。
SMTP-AUTH を実現するに当たり、以下のソフトウェアを使用しました。
cyrus-sasl-md5
cyrus-sasl-2.1.26-23
cyrus-sasl-devel-2.1.26-23
1.1 Postfix のインストール
①Postfix がインストールされているか確認の上、自動起動登録
1 2 3 |
# rpm -qa | grep postfix postfix-2.10.1-7.el7.x86_64 # systemctl enable postfix.service |
②Cyrus SASLのインストール
SMTP-AUTH を実現させる上で必須のパッケージです。
1 2 3 4 |
# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/cyrus-sasl-2.1.26-23.el7.x86_64.rpm # rpm -Uvh cyrus-sasl-2.1.26-23.el7.x86_64.rpm # wget http://mirror.centos.org/centos/7/os/x86_64/Packages/cyrus-sasl-devel-2.1.26-23.el7.x86_64.rpm # rpm -Uvh cyrus-sasl-devel-2.1.26-23.el7.x86_64.rpm |
③cyrus-sasl-md5のインストール
個別の認証処理を担うパッケージです。
1 2 |
# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/cyrus-sasl-md5-2.1.26-23.el7.x86_64.rpm # rpm -Uvh cyrus-sasl-md5-2.1.26-23.el7.x86_64.rpm |
④smtpd.confの編集
SMTP認証に個別のユーザー名、パスワードを使用する場合
1 2 3 4 |
# vi /etc/sasl2/smtpd.conf pwcheck_method: auxprop auxprop_plugin: sasldb mech_list: cram-md5 digest-md5 plain login |
Postfixのメール格納形式を共有ディレクトリ形式からアクセス性能改善及びセキュリティ強化の面でMaildir形式へ移行する。
【新規ユーザー対処】
新規ユーザー追加時に自動でホームディレクトリにMaildir形式のメールボックスが作成されるようにする
新規ユーザー追加時に自動でMaildir形式メールボックス作成
1 |
# mkdir -p /etc/skel/Maildir/{new,cur,tmp} |
1 |
# chmod -R 700 /etc/skel/Maildir/ |
ユーザー追加※SSHによるリモート接続はできないようにする場合
1 2 3 4 5 6 |
# useradd -s /sbin/nologin exampleuser # passwd exampleuser Changing password for user exampleuser. New UNIX password: ← 任意パスワード(このパスワードは後に導入する受信サーバーのパスワードとなる) Retype new UNIX password: ← 再度パスワード確認 passwd: all authentication tokens updated successfully. |
1 2 |
# echo "パスワード" | saslpasswd2 -p -u ドメイン名 -c exapleuser ← SMTP認証用パスワード登録 exampleuser@ドメイン名: userPassword |
1 2 |
# chgrp postfix /etc/sasldb2 # chmod 640 /etc/sasldb2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# vi /etc/postfix/main.cf # myhostnameにホスト名を指定 myhostname = mail.ドメイン名 # mydomainにドメイン名を指定 mydomain =ドメイン名 # myoriginのコメント削除 myorigin = $mydomain # inet_protocolをipv4に限定 net_protocols = ipv4 #inet_interfaces = localhost外部からのメール受信を許可コメント削除 inet_interfaces = localhost # mydestination設定変更 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain #存在しないユーザー宛てのメールを拒否 unknown_local_recipient_reject_code = 550 # mynetworksを自環境に合わせ変更 (サーバーのローカールIPアドレス) mynetworks = 127.0.0.0/8,192.168.0.0/24,10.0.0.0/8 メールの格納形式 home_mailbox = Maildir/ # relay_domainsのコメント削除 relay_domains = $mydestination # header_checksのコメント削除 header_checks = regexp:/etc/postfix/header_checks メールサーバーソフト名を隠す smtpd_banner = $myhostname ESMTP unknown SMTP Authetication関係の設定 最終行に SASLによるSMTP認証を使用 smtpd_sasl_auth_enable = yes 暗号化されないパスワードがネットワーク上に流れないようにします。noanonymous は「匿名を許可しない」、noplaintextは、「平文認証を許可しない」という意味 smtpd_sasl_security_options = noanonymous, noplaintext AUTHコマンドのサポートを認識できないクライアントへの対応(OutlookExpressを使う場合は付け加えます) broken_sasl_auth_clients = yes SMTP認証をするローカルドメインを指定します smtpd_sasl_local_domain = $mydomain リレーを許可するものを設定 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination ※ permit_mynetworks 指定されているネットワークからはリレーを許可 ※ SMTP-AUTHで認証を通過した接続はリレーを許可 ※ SMTP-AUTHで認証が通らなかったものはリレーを拒否 メールの受信サイズ(20Mの場合) message_size_limit = 20971520 |
サブミッションポート587番ポートを開く設定をmaster.cfに記述します
1 2 3 4 5 6 7 |
# vi /etc/postfix/master.cf コメントアウトを一部解除する submission inet n - n - - smtpd # -o syslog_name=postfix/submission # -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes |
1 |
# systemctl start postfix.service |
2.受信サーバーDovecot の導入
2.1 Dovecot をインストールします
①ダウンロードとインストール
1 2 |
# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/dovecot-2.2.36-3.el7.x86_64.rpm # rpm -Uvh dovecot-2.2.36-3.el7.x86_64.rpm |
②依存パッケージのダウンロードインストール
1 2 |
# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/clucene-core-2.3.3.4-11.el7.x86_64.rpm # rpm -Uvh clucene-core-2.3.3.4-11.el7.x86_64.rpm |
③dovecot.conf ファイルを編集
1 2 3 4 5 6 7 8 |
# vi /etc/dovecot/dovecot.conf コメントアウトを外し、lmtpは使用しない # protocols = imap pop3 lmtp → protocols = imap pop3 以下追加 mail_location = maildir:~/Maildir ← メール格納形式をMaildir形式にする disable_plaintext_auth = no ← プレインテキスト認証を許可 |
④10-ssl.confファイルの編集
SSLに関する設定ファイルは”/etc/dovecot/conf.d”ディレクトリの中に含まれる10-ssl.confになります。このファイルを変更
1 2 3 4 5 6 7 8 |
# vi /etc/dovecot/conf.d/10-ssl.conf SSLを使用する # SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt> # disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps # plain imap and pop3 are still allowed for local connections # ssl = required ssl = yes |
⑤dovecot を自動起動と設定反映
1 2 |
# systemctl start dovecot # systemctl enable dovecot |
3.ファイアウォールへの接続許可設定
3.1 接続許可サービスにSMTP、POP3を追加します
「CentOS7.6サーバー構築 OSインストール後するべき初期設定」のiptablesshサンプルの中で次の箇所を確認しすべてコメントを解除する
/sbin/iptables -A INPUT -p tcp --dport 25 -j LOG
/sbin/iptables -A INPUT -p tcp --sport 25 -j LOG
/sbin/iptables -A INPUT -p tcp --dport 25 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --sport 25 -j ACCEPT
# pop3
/sbin/iptables -A INPUT -p tcp --dport 110 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --sport 110 -j ACCEPT
4.メールソフト設定(POP使用)
メールソフトとしてThunderbirdを使用する場合
メニューの「ファイル」⇒「新規作成」⇒「既存のメールアカウント」でメールアカウント設定を行う
「接続する上での危険性を理解しました」をチェックして「完了」