ソフトウェアエンジニア現役続行

雑多なことを綴ります

自宅メールサーバーの構築

勉強を兼ねて、自宅でメールサーバーを構築してみました。1台のlinuxマシンにpostfixdovecotを入れました。

主な作業は以下の3点です。

  1. dovecotの設定(/etc/dovecot.conf)
  2. postfixの設定(/etc/postfix/main.cf)
  3. OP25B対応(/etc/postfix/main.cf)

OP25B対応は、自分の契約しているプロバイダーでOP25Bが実施されている場合のみ必要です。

dovecotの設定(/etc/dovecot.conf)

dovecot.confの設定はほとんど必要ありません。そのままでも動きます。僕はMailbox形式ではなくMaildir形式にしたかったので、1行だけ変更しました↓

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

postfixの設定(/etc/postfix/main.cf)

main.cfはいろいろと設定する必要があります。ここでは、ドメイン名はexample.jp、LANのネットワークアドレスは192.168.0.0/24とします。
まず、ホスト名をFQDNで指定します↓

# 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 = localhost.example.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 = example.jp ← 変更部分

インターネット上に公開するので、インタフェースはallにします↓

# 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 ← 変更部分

mydestinationに指定したドメイン宛のメールは、どのクライアントからでも受け付けます。なのでここではlocalhostと自分のドメイン以外は指定しません。さもないとスパムメール送信者の踏み台にされてしまいます↓

# 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.
#
# (省略)
#
#mydestination = $myhostname, localhost.$mydomain, localhost
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ← 変更部分
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
# mail.$mydomain, www.$mydomain, ftp.$mydomain

mynetworksを指定すると、そのネットワークのクライアントからは、メールの宛先のドメインが何であろうと許可します。これにより、LANネットワークのクライアントから外部のメールアドレスに、メールを送ることができます↓

# Alternatively, you can specify the mynetworks list by hand, in
# which case Postfix ignores the mynetworks_style setting.
#
mynetworks = 192.168.0.0/24, 127.0.0.0/8 ← 変更部分
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table

メールサーバーを外部に公開するため、メールサーバーアプリケーション名とソフトウェアバージョンは公開しないようにします↓

# 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)
smtpd_banner = $myhostname ESMTP unknown ← 変更部分

dovecot.confでMaildir方式を選択したので、ここでもMaildir方式を選択します↓

# 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/ ← 変更部分

OP25B対応(/etc/postfix/main.cf)

利用しているプロバイダーがOP25Bを実施している場合のみ設定する必要があります。OP25Bを実施している場合、メール宛先のメールサーバーに直接送ることができません。そこでプロバイダーの提供しているメールサーバーをリレーホストとして、ここを経由してメール宛先のメールサーバーに送ります。

ここでは、以下のような例とします↓

SMTPサーバー:smtp.provider.jp:587
アカウント:ozwald
パスワード:abcdefg

main.cfには以下のように設定します↓

# The relayhost parameter specifies the default host to send mail to
# when no entry is matched in the optional transport(5) table. When
# no relayhost is given, mail is routed directly to the destination.
#
# (省略)
#
#relayhost = $mydomain
#relayhost = [gateway.my.domain]
#relayhost = [mailserver.isp.tld]
#relayhost = uucphost
#relayhost = [an.ip.add.ress]
relayhost = [smtp.provider.jp]:587 ← 変更部分
smtp_sasl_auth_enable = yes ← 変更部分
smtp_sasl_password_maps = hash:/etc/postfix/saslpw ← 変更部分1
smtp_sasl_security_options = noanonymous ← 変更部分
smtp_sasl_mechanism_filter = LOGIN ← 変更部分2

「変更部分1」のパスワードファイルを作ります↓

$ echo [smtp.provider.jp]:587 ozwald:abcdefg > /etc/postfix/saslpw
$ chmod 640 /etc/postfix/saslpw
$ postmap /etc/postfix/saslpw

「変更部分2」は、プロバイダーのSMTPサーバーの認証メカニズムを入れます。SMTPサーバーで利用できる認証メカニズムは次のようにして知ることができます↓

$ telnet smtp.example.jp 587 [Enter] ← 自分で入力します
Trying xxx.xxx.xxx.xxx...
Connected to smtp.example.jp.
Escape character is '^]'.
220 mail.example.com ESMTP Postfix
EHLO mail.example.com [Enter] ← 自分で入力します
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-STARTTLS
250-ETRN
250-AUTH PLAIN LOGIN ←この内容に合わせます
250-AUTH=PLAIN LOGIN ←この内容に合わせます
250 8BITMIME
QUIT

この場合だと、PLAINまたはLOGINが使えます。