メールサーバ構築:Postfix, dovecot, mutt

Table of Contents

OS:CentOS 7

以下のページを参考にしました Postfix http://www.sea-bird.org/pukiwiki/index.php?Postfix%A4%C7spam%A5%E1%A1%BC%A5%EB%C1%F7%BF%AE%B8%B5%A1%A6%C1%F7%BF%AE%BC%D4%A4%CB%A4%E8%A4%EB%C0%A9%B8%C2%A4%CE%B4%AC http://unixlife.jp/linux/centos-5/postfix-secure.html

dovecot http://www.aconus.com/~oyaji/centos/dovecot_centos.htm

mutt https://wiki.archlinuxjp.org/index.php/Mutt http://www.emaillab.org/mutt/ http://takachan.jra.net/blog/archives/476

サーバー証明書 http://www.aconus.com/~oyaji/www/certs_linux.htm

firewalldは以下を参考に開けておく http://qiita.com/kentakozuka/items/d32ace44299edf045c6b

#Postfix

smtp(25)とsubmission(587)のポートは開けておく

##インストール

yum -y install postfix cyrus-sasl cyrus-sasl-plain cyrus-sasl-md5   

##main.cf

vim /etc/postfix/main.cf
...

### ホスト名を設定
### e.g. mailgw.hogehoge.com
myhostname = mailgw.<ドメイン名>

...

### ドメイン名の設定
### e.g. hogehoge.com
mydomain = <ドメイン名>

...

###メールアドレスの[@]以降に使用するドメイン名を指定
myorigin = $myhostname

...

###SMTP接続を待ち受けるネットワークインターフェースを指定
inet_interfaces = all

...

###ローカル配送を行うドメイン名を指定
mydestination =
        localhost,
        $myhostname,
        $mydomain

...

###smtpd_*****_restrictionsでSMTPサーバがそれぞれのセクションでの接続可/否を設定
###smtpd_client_restrictions = クライアントからの接続要求時
###smtpd_helo_restrictions = HELOコマンドの場面

###RCPT TOコマンドの場面
smtpd_recipient_restrictions =
                             permit_mynetworks
                             reject_non_fqdn_recipient
                             reject_unauth_pipelining
                             permit_sasl_authenticated
                             reject_unauth_destination

###smtpd_sender_restrictions = MAIL FROMコマンドの場面
smtpd_sender_restrictions =
#                         hash:/etc/postfix/spamlist_address
                          reject_unknown_sender_domain

# with SASL2
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot

# Max TO
smtpd_recipient_limit = 5000

# Max Message Size
message_size_limit = 51200000

###ソフト名とバージョン番号を非表示にする
smtpd_banner = $myhostname ESMTP

master.cf

各フィールドは以下の通り service Postfixのサービス(デーモン)名の指定 type サービスのタイプの指定 private メールシステムへのアクセス制限の有無を指定 unpriv サービスをrootか所有者で動作させるかを指定 chroot chroot jail環境で動作の有無を指定 wakeup サービスを何秒後に起動させるかを指定 maxprox プロセスを同時に起動できる最大数の指定 command + args サービスの実態プログラムと、その引数の指定

vim /etc/postfix/master.cf
...
#tlsproxy  unix  -       -       n       -       0       tlsproxy
submission inet n       -       n       -       -       smtpd
#  -o smtpd_tls_security_level=encrypt
...

saslauthd

vim /etc/sysconfig/saslauthd
...
###MECH=pam
MECH=shadow
...

##バーチャルドメイン(virtual_maps)の設定

mv /etc/postfix/virtual /etc/postfix/virtual.org
vim /etc/postfix/virtual   
# 受信可能なドメイン名	    anything
<ドメイン名>	anything
# 受信可能なアドレス	    転送先アドレスor実ユーザー名
<アドレス>    <実ユーザ名>
/usr/sbin/postmap hash:/etc/postfix/virtual < /etc/postfix/virtual
systemctl restart postfix                   

#dovecot

##インストール

yum -y install dovecot

##設定

vim /etc/dovecot/dovecot.conf
...
#protocols = imap pop3 lmtp
protocols = imap imaps pop3 pop3s
...
vim /etc/dovecot/conf.d/10-mail.conf
...
#mail_location =
mail_location = maildir:~/Maildir
...

このへんはお好みで

vim  /etc/dovecot/conf.d/10-auth.conf
...
disable_plaintext_auth = yes
...

このへんはお好みで

vim /etc/dovecot/conf.d/10-ssl.conf
...
ssl = required
###ssl = 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 = <証明書のパス>
ssl_key = <キーのパス>
...
systemctl enable dovecot                                
systemctl start dovecot                                
systemctl status dovecot

もしくは

systemctl restart dovecot

##【任意】pop3とimpa4の接続を確認

POP3

$ telnet <ドメイン名> pop3
Trying XXX.XXX.XXX.XXX...
Connected to <ドメイン>.
Escape character is '^]'.
+OK Dovecot ready.
user <ユーザ名>
pass <パスワード>
+OK
+OK Logged in.
quit
+OK Logging out.
Connection closed by foreign host.

IMAP4

$ telnet <ドメイン名> 143
Trying XXX.XXX.XXX.XXX...
Connected to <ドメイン>.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN] Dovecot ready.
1 login <ユーザ名> <パスワード>
1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE BINARY MOVE] Logged in
1 logout
* BYE Logging out
1 OK Logout completed.
Connection closed by foreign host.

#mutt これはサーバ側でなくクライアントです。 リモートサーバに接続してメールをフェッチします。

##.mutrc

vim ~.muttrc
set folder=»»---imaps://<ユーザ名>@<ドメイン>/↲                                                                                                                                                                
set imap_user=»-<ユーザ名>
set spoolfile=»-+INBOX↲
↲
#色付け↲
color normal green default↲
color hdrdefault cyan default↲
color header yellow default ^(From|Subject):↲
color quoted cyan default↲
color signature red default↲
color attachment blue default↲
color body magenta default "(ftp|http)://[^ ]+" # point out URLs↲
color body magenta default [-a-z_0-9.]+@[-a-z_0-9.]+ # e-mail addresses↲
color search black yellow # how to hilite search patterns in the pager↲
alias 'flkj^[^[qqq' ^[ <^[>↲