Ubuntu

Ubuntu 12.04 Zarafa設定の後で・・・Postfixで外部サーバーとやり取りする

お知らせ
Ubuntu18.04でKopanoを設定する記事を公開しました。

サーバーは立ち上がったけれども、世の中のサーバーに紛れ込むのはなかなか大変なんじゃないかと情報収集。

やはり、Postfixそのものは素晴らしいものだが、設定を間違えればスパムの踏み台にされるって話がいたるところに書かれている。それは本意ではないし、うちにスパムが届くのも嫌だなぁ。

広告


※今回の設定で外部とのメールのやり取りができるようになった。ただ、正直なところ勉強が不足しており、セキュリティ的に万全かどうか、あるいはリスクが許容範囲にあるのか判断ができない。設定後3日間のログを見た限りでは、白黒はっきりしないサーバーからの接続要求はあるが、接続後に何もせずに切断されているように見えている。


以下を盛り込んだ main.cf を作ってみた。

  • プロバイダーは25番ポートブロック対策を実施している。従って、外部へのメール送信にはサブミッションポートを使う必要がある。
  • でも、ちょっとしたテストはローカルで行いたい。この場合は、25番ポートで直接メールをリレーさせたい。
  • スパムメールの踏み台にならない、また、スパムメールを受け取らない設定にしたい。

設定内容についてはファイル内容の後に記す。

中身はこれ。更に、これに対応したファイルもいくつか作成。

/etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = smtp.hogeserver.hogeddns.jp
mydomain = hogeserver.hogeddns.jp
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = hogeserver.hogeddns.jp, localhost.hogeserver.hogeddns.jp, localhost
#relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

#
# Security Setting.
#
smtpd_client_restrictions =
    permit_mynetworks
    check_client_access regexp:/etc/postfix/white_list
    check_client_access regexp:/etc/postfix/rejections
smtpd_helo_required = yes
smtpd_helo_restrictions =
    permit_mynetworks
    reject_invalid_hostname
    check_helo_access regexp:/etc/postfix/helo_restrictions
smtpd_sender_restrictions =
    permit_mynetworks
    reject_non_fqdn_sender
    reject_unknown_sender_domain

#
# Outbound Port25 Blocking.
#
relayhost = smtp.provider.ne.jp
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/isp_passwd
smtp_sasl_security_options = noanonymous, noplaintext
smtp_sasl_mechanism_filter = plain, login, cram-md5
transport_maps = hash:/etc/postfix/transport

#
# Zarafa Settings.
#
virtual_alias_maps = mysql:/etc/postfix/mysql-aliases.cf
mailbox_transport = zarafa: zarafa_destination_recipient_limit = 1
mailbox_command = /usr/bin/zarafa-dagent "$USER"
debugger_command =
    PATH=/bin:/usr/bin:/usr/local/bin:/usr/bin/X11
    ddd $daemon_directory/$process_name $process_id & sleep 5


smtpd_banner = $myhostname ESMTP

PostfixによるSMTPサーバ構築メモ(http://www.deer-n-horse.jp/linux/tips/postfix.html ※リンク切れのためアーカイブへ)を参考にさせていただいた。
これにより、接続後にどうなるかというと・・・

$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 smtp.hogeserver.hogeddns.jp ESMTP

と、バージョン情報が表示されない。


myhostname = smtp.hogeserver.hogeddns.jp ←BIND9でsmtpでも名前解決できる&hogeddnsでもsmtpという名前をばらまいている
mydomain = hogeserver.hogeddns.jp

間違っていたり、なかったりしたので追加。


smtpd_client_restrictions =
    permit_mynetworks
    check_client_access regexp:/etc/postfix/white_list
    check_client_access regexp:/etc/postfix/rejections
smtpd_helo_required = yes
smtpd_helo_restrictions =
    permit_mynetworks
    reject_invalid_hostname
    check_helo_access regexp:/etc/postfix/helo_restrictions
smtpd_sender_restrictions =
    permit_mynetworks
    reject_non_fqdn_sender
    reject_unknown_sender_domain

これは、スパム対策技術 S25R(Selective SMTP Rejection:選択的SMTP拒絶)スパム対策方式 のレポートにあった設定を使わせていただいたもの。
概要を知るのにMAKIZOU.COM(http://makizou.com/1320/ ※リンク切れのためアーカイブへ)が大変参考になった。

最初にサーバの設定手順(http://humming.dip.jp/~tansy/server_setup.html ※リンク切れ)を見つけたのが幸いし、上記2つのページにたどり着くことができた。

それぞれの中身だが・・・
/etc/postfix/white_list

# *** PUBLISHED S25R WHITE LIST ***
# Last update: Jun 05, 2013
# (*): reported by a contributor
・・・

これは、スパム対策技術からダウンロードして使わせていただいている。

/etc/postfix/rejections

# S25R client rejection specifications for Postfix
# Contributed by ASAMI Hideo (Japan), Jun 2004; Jul 2007
# Refer to: http://www.gabacho-net.jp/en/anti-spam/
#
・・・

これは、スパム対策技術にあったファイルの中身をコピーして使わせていただいている。

/etc/postfix/helo_restrictions

# Illegal HELO command blocking specification
# Provided that your mail server's IP address is 223.12.34.56 and its
# acceptable domain name is "example.com", specify as follows:
・・・

これも、スパム対策技術にあったファイルの中身をコピーしたが全てコメントであった。スパマーを拒絶するために今後自分で追加していくことになる。


relayhost = smtp.provider.ne.jp
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/isp_passwd
smtp_sasl_security_options = noanonymous, noplaintext
smtp_sasl_mechanism_filter = plain, login, cram-md5
transport_maps = hash:/etc/postfix/transport

この設定はOP25B対策(Outbound Port 25 Blocking対策)の情報を活用させていただいた。

relayhost にはプロバイダーの SMTPサーバーを指定した。

smtp_sasl_auth_enable には yes を指定し、プロバイダーの SMTPサーバーに接続する際、SASL認証を使用するようにした。

smtp_sasl_password_maps には isp_passwd ファイルを指定した。isp_passwd ファイルは以下の内容にしている。

[smtp.provider.ne.jp] account:password

accountとpasswordにはそれぞれプロバイダーのSMTPへの接続情報を書いた。これを

$ sudo postmap /etc/postfix/isp_passwd
$ sudo chmod 600 /etc/postfix/isp_passwd

としてハッシュ化し、元ファイルへのアクセスをrootに限定した。

smtp_sasl_security_options には noanonymous, noplaintext を指定した。匿名ログインは許可されず、平文のパスワードは拒否されるような設定。

smtp_sasl_mechanism_filter については、以下を実行して設定すべき値を調べて設定する。

$ telnet smtp.provider.ne.jp 25
Trying nnn.nnn.nnn.nnn...
Connected to smtp.provider.ne.jp.
Escape character is '^]'.
220 smtp.provider.ne.jp ESMTP
EHLO smtp.provider.ne.jp
250-smtp.provider.ne.jp Hello smtp.provider.ne.jp [mmm.mmm.mmm.mmm], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE 20971520
250-DSN
250-AUTH=PLAIN LOGIN CRAM-MD5
250-AUTH PLAIN LOGIN CRAM-MD5
250-DELIVERBY
250 HELP
quit
221 2.0.0 smtp.provider.ne.jp closing connection
Connection closed by foreign host.

上記の AUTH の設定内容をそのまま使う。

transport_maps には以下のファイルを作成して設定した。

# ローカルネットワークについては通常配信
.hogeserver.hogeddns.jp smtp:

# 通常はプロバイダーを通じて配信(relayhostが設定してあるのでここではコメント化)
#. smtp:[smtp.provider.ne.jp]

これは、水銀室 CentOSをサーバOSとして素早く検証するを参考にさせていただいた。
作成したファイルは以下の通りハッシュ化した。

$ sudo postmap /etc/postfix/transport


設定が終わったら、以下を実行してチェックをする。

$ sudo postfix check

問題が内容なら、Postfixを再起動する。

$ sudo service postfix restart

以上で設定終わり。

広告

コメントはこちらから お気軽にどうぞ ~ 投稿に関するご意見・感想・他