Login

オンライン中

We have 15 guests and no members online

eagle1jp

POP,IMAPサーバ(dovecot)の構築

POP・IMAPサーバ(dovecot)の構築

1.dovecotの特徴

Postfixやqmailといった比較的新しいMailサーバでは、メールの保存形式としてMaildirという方式で保存できるようになっています。 Maildir形式はそれまでのmbox形式に比べ

  • 届いたメールをユーザのホームディレクトリに保存させる
  • メールは1件毎に1つのファイルで管理される。
  • ファイルの保存時にロックの必要が無い

といった特徴があります。 特にメール1件に対しファイルが1つ、かつロックが不要という特性から、ユーザのホームディレクトリがNFSなどでマウントされたネットワード上のドライブに対してマウントされているような場合であっても、不整合を起こすことなくメールの保存が可能になっています。
また、mbox形式ではすべてのメールデータがただ1つのファイルに保存されるため、保存データが肥大するとメールの読み取りが遅くなったり、何らかの不整合が発生するとすべてのメールが読めなくなるという危険性がありますが、Maildirではそういう心配は不要になります。 反面、mboxにしか対応できないsendmailは古くから利用されてきたこともあり、sendmailの動作を期待するようなプログラムも多く存在するので悩むところですが、POPやIMAPといったMRA(Mail Retrieval Agent)に関しては「courier-imap、courier-pop」やここで紹介する「dovecot」を利用することで簡単にサービスの提供が可能になっています。
dovecot は比較的新しいMRAで最近になって多くのLinuxディスリビューションでも標準で組み込まれるようになってきており、特徴として

  • 高速
  • IMAPの標準互換テストにパス
  • 高いセキュリティ
  • mbox、Maildirのどちらの形式でもOK

といった事が挙げられます。

2.dovecotのインストール

 (Debian(Ubuntu)でのパッケージのインストール方法)

 次のコマンドを実行します。

$ sudo apt-get install dovecot-common dovecot-imapd dovecot-pop3d

 

 (Redhat(CentOS)でのパッケージのインストール方法)

 次のコマンドを実行します。(スーパーユーザ権限で実行します)

# yum install dovecot

3.dovecotの設定

インストールができたなら、利用する環境に合わせた設定を行います。
以下では次のようなシナリオで設定を行うこととします。

設定内容
パラメータ
提供するサーバサービスをPOPとIMAPにするprotocols = imap pop3
プレーンテキストでのパスワードを許可するdisable_plaintext_auth =no
ログファイルを記録する
log_path = /var/log/dovecot.log
ログのタイムスタンプ形式をyy-mm-dd HH:MM:SS形式にする
log_timestamp = "%Y-%m-%d %H:%M:%S "
メールデータの保存形式をMaildirとする。
mail_location = maildir:~/Maildir

 

dovecotのメインの設定ファイルは /etc/dovecot/dovecot.conf になりますので、このファイルを編集していきます。

/etc/dovecot/dovecot.conf

protocols = imap pop3
disable_plaintext_auth = no
log_path = /var/log/dovecot.log
log_timestamp = "%Y-%m-%d %H:%M:%S "
mail_location = maildir:~/Maildir
mail_privileged_group = mail

##
## IMAP specific settings
##
protocol imap {
    #login_executable = /usr/lib/dovecot/imap-login
    #mail_executable = /usr/lib/dovecot/imap
    #imap_max_line_length = 65536
    #mail_max_userip_connections = 10
    #mail_plugins =
    #mail_plugin_dir = /usr/lib/dovecot/modules/imap
    #login_greeting_capability = no
    #imap_logout_format = bytes=%i/%o
    #imap_capability =
    #imap_client_workarounds =
}

##
## POP3 specific settings
##
protocol pop3 {
    #login_executable = /usr/lib/dovecot/pop3-login
    #mail_executable = /usr/lib/dovecot/pop3
    #pop3_no_flag_updates = no
    #pop3_enable_last = no
    #pop3_reuse_xuidl = no
    #pop3_lock_session = no
    pop3_uidl_format = %08Xu%08Xv
    #pop3_logout_format = top=%t/%p, retr=%r/%b, del=%d/%m, size=%s
    #mail_max_userip_connections = 3
    #mail_plugins =
    #mail_plugin_dir = /usr/lib/dovecot/modules/pop3
    #pop3_client_workarounds =
}

protocol managesieve {
  #login_executable = /usr/libexec/dovecot/managesieve-login
  #mail_executable = /usr/libexec/dovecot/managesieve
  #managesieve_max_line_length = 65536
  sieve=~/.dovecot.sieve
  sieve_storage=~/sieve
  # mail_location = mbox:~/mail
  #managesieve_implementation_string = Cyrus timsieved v2.2.13

}
#protocol lda {
  #postmaster_address = This email address is being protected from spambots. You need JavaScript enabled to view it.
  #hostname =
  #mail_plugins =
  #mail_plugin_dir = /usr/lib/dovecot/modules/lda
  #quota_full_tempfail = no
  #deliver_log_format = msgid=%m: %$
  #sendmail_path = /usr/lib/sendmail
  #rejection_reason = Your message to was automatically rejected:%n%r
  #auth_socket_path = /var/run/dovecot/auth-master
#}

# Executable location
#auth_executable = /usr/lib/dovecot/dovecot-auth

# Set max. process size in megabytes.
#auth_process_size = 256
#auth_cache_size = 0
#auth_cache_ttl = 3600
#auth_cache_negative_ttl = 3600

#auth_realms =
#auth_default_realm =
#auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@
#auth_username_translation =

#auth_username_format =

#auth_master_user_separator =
#auth_anonymous_username = anonymous
#auth_verbose = no
#auth_debug = no

#auth_debug_passwords = no

#auth_worker_max_count = 30
#auth_worker_max_request_count = 0
#auth_gssapi_hostname =
#auth_krb5_keytab =
#auth_ntlm_use_winbind = no
#auth_winbind_helper_path = /usr/bin/ntlm_auth

#auth_failure_delay = 2

auth default {
  mechanisms = login plain
  #passdb passwd-file {
    # File contains a list of usernames, one per line
    #args = /etc/dovecot.deny
    #deny = yes
  #}

  passdb pam {
    # Some examples:
    #   args = session=yes %Ls
    #   args = cache_key=%u dovecot
    #args = dovecot
  }

  # System users (NSS, /etc/passwd, or similiar)
  # In many systems nowadays this uses Name Service Switch, which is
  # configured in /etc/nsswitch.conf.
  #passdb passwd {
    # [blocking=yes] - See userdb passwd for explanation
    #args =
  #}
  # Shadow passwords for system users (NSS, /etc/shadow or similiar).
  # Deprecated by PAM nowadays.
  #
  #passdb shadow {
    # [blocking=yes] - See userdb passwd for explanation
    #args =
  #}

  # PAM-like authentication for OpenBSD.
  #
  #passdb bsdauth {
    # [cache_key=] - See cache_key in PAM for explanation.
    #args =
  #}

  # passwd-like file with specified location
  #
  #passdb passwd-file {
    # [scheme=] [username_format=]
    #
    #args =
  #}
  # checkpassword executable authentication
  # NOTE: You will probably want to use "userdb prefetch" with this.
  #
  #passdb checkpassword {
    # Path for checkpassword binary
    #args =
  #}

  # SQL database
  #passdb sql {
    # Path for SQL configuration file
    #args = /etc/dovecot/dovecot-sql.conf
  #}

  # LDAP database
  #passdb ldap {
    # Path for LDAP configuration file
    #args = /etc/dovecot/dovecot-ldap.conf
  #}

  # vpopmail authentication
  #passdb vpopmail {
    # [cache_key=] - See cache_key in PAM for explanation.
    # [quota_template=] - %q expands to Maildir++ quota
    #   (eg. quota_template=quota_rule=*:backend=%q)
    #args =
  #}

  #
  # User database specifies where mails are located and what user/group IDs
  # own them. For single-UID configuration use "static".
  #
  #
  #

  # System users (NSS, /etc/passwd, or similiar). In many systems nowadays this
  # uses Name Service Switch, which is configured in /etc/nsswitch.conf.
  #
  userdb passwd {
    # [blocking=yes] - By default the lookups are done in the main dovecot-auth
    # process. This setting causes the lookups to be done in auth worker
    # proceses. Useful with remote NSS lookups that may block.
    # NOTE: Be sure to use this setting with nss_ldap or users might get
    # logged in as each others!
    #args =
  }

  # passwd-like file with specified location
  #
  #userdb passwd-file {
    # [username_format=]
    #args =
  #}
  # static settings generated from template
  #userdb static {
    # Template for the fields. Can return anything a userdb could normally
    # return. For example:
    #
    #  args = uid=500 gid=500 home=/var/mail/%u
    #
    # If you use deliver, it needs to look up users only from the userdb. This
    # of course doesn't work with static because there is no list of users.
    # Normally static userdb handles this by doing a passdb lookup. This works
    # with most passdbs, with PAM being the most notable exception. If you do
    # the user verification another way, you can add allow_all_users=yes to
    # the args in which case the passdb lookup is skipped.
    #
    #args =
  #}
  # SQL database
  #userdb sql {
    # Path for SQL configuration file
    #args = /etc/dovecot/dovecot-sql.conf
  #}

  # LDAP database
  #userdb ldap {
    # Path for LDAP configuration file
    #args = /etc/dovecot/dovecot-ldap.conf
  #}

  # vpopmail
  #userdb vpopmail {
  #}

  # "prefetch" user database means that the passdb already provided the
  # needed information and there's no need to do a separate userdb lookup.
  # This can be made to work with SQL and LDAP databases, see their example
  # configuration files for more information how to do it.
  #
  #userdb prefetch {
  #}
  # User to use for the process. This user needs access to only user and
  # password databases, nothing else. Only shadow and pam authentication
  # requires roots, so use something else if possible. Note that passwd
  # authentication with BSDs internally accesses shadow files, which also
  # requires roots. Note that this user is NOT used to access mails.
  # That user is specified by userdb above.
  user = root

  # Directory where to chroot the process. Most authentication backends don't
  # work if this is set, and there's no point chrooting if auth_user is root.
  # Note that valid_chroot_dirs isn't needed to use this setting.
  #chroot =

  # Number of authentication processes to create
  #count = 1

  # Require a valid SSL client certificate or the authentication fails.
  #ssl_require_client_cert = no

  # Take the username from client's SSL certificate, using
  # X509_NAME_get_text_by_NID() which returns the subject's DN's
  # CommonName.
  #ssl_username_from_cert = no

  # It's possible to export the authentication interface to other programs:
  #socket listen {
    #master {
      # Master socket provides access to userdb information. It's typically
      # used to give Dovecot's local delivery agent access to userdb so it
      # can find mailbox locations.
      #path = /var/run/dovecot/auth-master
      #mode = 0600
      # Default user/group is the one who started dovecot-auth (root)
      #user =
      #group =
    #}
    #client {
      # The client socket is generally safe to export to everyone. Typical use
      # is to export it to your SMTP server so it can do SMTP AUTH lookups
      # using it.
      #path = /var/run/dovecot/auth-client
      #mode = 0660
    #}
  #}
}
#auth external {
#  socket connect {
#    master {
#      path = /var/run/dovecot/auth-master
#    }
#  }
#}
dict {
  #quota = mysql:/etc/dovecot-dict-quota.conf
}

# Path to Berkeley DB's configuration file. See doc/dovecot-db-example.conf
#dict_db_config =

plugin {
  # Quota limits are set using "quota_rule" parameters, either in here or in
  # userdb. It's also possible to give mailbox-specific limits, for example:
  #   quota_rule = *:storage=1048576
  #   quota_rule2 = Trash:storage=102400
  # User has now 1GB quota, but when saving to Trash mailbox the user gets
  # additional 100MB.
  #
  # Multiple quota roots are also possible, for example:
  #   quota = dict:user::proxy::quota
  #   quota2 = dict:domain:%d:proxy::quota_domain
  #   quota_rule = *:storage=102400
  #   quota2_rule = *:storage=1048576
  # Gives each user their own 100MB quota and one shared 1GB quota within
  # the domain.
  #
  # You can execute a given command when user exceeds a specified quota limit.
  # Each quota root has separate limits. Only the command for the first
  # exceeded limit is excecuted, so put the highest limit first.
  # Note that % needs to be escaped as %%, otherwise "% " expands to empty.
  #   quota_warning = storage=95%% /usr/local/bin/quota-warning.sh 95
  #   quota_warning2 = storage=80%% /usr/local/bin/quota-warning.sh 80
  #quota = maildir

  # ACL plugin. vfile backend reads ACLs from "dovecot-acl" file from maildir
  # directory. You can also optionally give a global ACL directory path where
  # ACLs are applied to all users' mailboxes. The global ACL directory contains
  # one file for each mailbox, eg. INBOX or sub.mailbox. cache_secs parameter
  # specifies how many seconds to wait between stat()ing dovecot-acl file
  # to see if it changed.
  #acl = vfile:/etc/dovecot-acls:cache_secs=300

  # Convert plugin. If set, specifies the source storage path which is
  # converted to destination storage (mail_location) when the user logs in.
  # The existing mail directory is renamed to

-converted.
  #convert_mail = mbox:%h/mail
  # Skip mailboxes which we can't open successfully instead of aborting.
  #convert_skip_broken_mailboxes = no
  # Skip directories beginning with '.'
  #convert_skip_dotdirs = no
  # If source storage has mailbox names with destination storage's hierarchy
  # separators, replace them with this character.
  #convert_alt_hierarchy_char = _

  # Trash plugin. When saving a message would make user go over quota, this
  # plugin automatically deletes the oldest mails from configured mailboxes
  # until the message can be saved within quota limits. The configuration file
  # is a text file where each line is in format:
  # Mails are first deleted in lowest -> highest priority number order
  #trash = /etc/dovecot-trash.conf

  # Expire plugin. Mails are expunged from mailboxes after being there the
  # configurable time. The first expiration date for each mailbox is stored in
  # a dictionary so it can be quickly determined which mailboxes contain
  # expired mails. The actual expunging is done in a nightly cronjob, which
  # you must set up:
  #   dovecot --exec-mail ext /usr/lib/dovecot/expire-tool
  #expire = Trash 7 Spam 30
  #expire_dict = db:/var/lib/dovecot/expire.db

  # Lazy expunge plugin. Currently works only with maildirs. When a user
  # expunges mails, the mails are moved to a mailbox in another namespace
  # (1st). When a mailbox is deleted, the mailbox is moved to another namespace
  # (2nd) as well. Also if the deleted mailbox had any expunged messages,
  # they're moved to a 3rd namespace. The mails won't be counted in quota,
  # and they're not deleted automatically (use a cronjob or something).
  #lazy_expunge = .EXPUNGED/ .DELETED/ .DELETED/.EXPUNGED/

  # Events to log. Default is all.
  #mail_log_events = delete undelete expunge copy mailbox_delete mailbox_rename
  # Group events within a transaction to one line.
  #mail_log_group_events =
  # Available fields: uid, box, msgid, size, vsize
  # size and vsize are available only for expunge and copy events.
  #mail_log_fields = uid box msgid size
}

 

 

dovecot は最低限上記の項目を設定すれば利用できる筈です。

 

4.システム起動時にdevcotを自動起動させる

 (Debian(Ubuntu)での場合)

 debianでは、パッケージでインストールした段階で自動起動する設定になっているので、特になにもする必要はありません。

 

 (Redhat(CentOS)での場合)

 次のコマンドを実行します。(スーパーユーザ権限で実行します)

# chkconfig dovecot on 

 

dovecotのTLS(SSL)化】に続く

 

Mailサーバ

この資料では「Postfix」というメールサーバをソフトを中心にインターネットメール環境をどのように構築するか説明を行います。 【旧、Postfixのページはこちらから】

1.e-mailの歴史的バックボーン

  現在、当たり前のように利用しているe-mailですが、1990年以前の日本ではごく限られた企業や研究機関、学校でしか利用できないものでした。 
当時はまだ商用のインターネットプロパイダというものもなく、特定の企業と大学、研究目的の大学間ネットワーク、そして企業内における日本と外国の支社間とのネットワークなどを有機的に結合したネットワークとして存在している程度で、しかも大半が電話回線を使ったモデム接続によって、メールのやりとりをしているような状況でした。 
この当時、e-mailのためのソフトウェアといえばUNIX OS上で動作していた『sendmail』が多くの組織で使われていたと思われます。 sendmailは元々はe-mail(SMTP)用としていたものではく、それまでに存在していた各種のメールサーバソフト専用のプロトコルのメッセージ交換を実現できるように開発されていた経緯があります。 このため、sendmail では、ルールセットと呼ばれるマクロを組み合わせてメールサーバ間のメッセージの変換を行ってメールを送受信するという仕組みで動作しています。
また、メッセージのやりとりの為の通信プロトコルも、外部の通信ソフトと組み合わせてことで、イーサネットを使ったsmtpだけでなくuucpやDECNetなどにも対応できる仕組みを持っています。(当時は、UUCPとモデムを使ったバケツリレー方式でのメール構築が一般的だったと思います。 当時の私の会社なども、ソニーさんにお願いしてUUCP接続環境を実現していたものです。)

e-mail環境が一変したのはやはり商用のプロパイダが現れた1992年以後でしょう、それまでメールサーバを構築する場合は既にインターネットに接続された組織にお願いしてバケツリレー方式で行ったいたものが、送受信したい相手のサーバに直接接続できる環境が実現できるようになりました。 これてよって、e-mail は素早く相手に届くようになり、バケツリレー方式あった中継サーバのトラブルによるメールデータの紛失や遅延といったことから解放されたのです。 また、直接相手に届けられるようになったことで、データを途中の経路で盗み見される可能性少なくなり、一気に利用環境が整ってきました。

しかし、世の中は良いこともあれば悪いことも存在します。 それは、商用プロパイダによる接続が簡単にできるようになってからはインターネットの世界にはそれまでのように 「相手に信頼してもらった団体だけが接続できた」 時代から、「誰でもが利用」 できる時代になったことにより、インターネット空間で不正を行う人が確実に増えてしまったことでしょう。 で、このような人たちの中には、自分以外の組織のサーバを乗っ取って自分たちが利用できるにしたいと考える輩が現れてきた訳ですが、このような人間がサーバの乗っ取りのために目を付けたのがメールサーバでした。 メールサーバは当時から多くの組織に存在しており、また、その目的から外部に存在が公開されているサーバであった為に、真っ先に攻撃の対象になってしまいました。 そして、以前から使われていた sendmail というサーバソフトには、潜在的にセキュリティ上の問題を多くもっていたために、その問題を突いて多くのサーバが乗っ取りの危険に晒されてしまい、結果としてして多くのサイトのサーバが乗っ取られるという事が発生しました。(当時は殆どが愉快犯的な目的だったので、まあ、救われましたが。。。) 結局、商用プロパイダによるサービス開始以後、暫く(数年)に渡って sendmail のセキュリティホールに対するアタックと、その対策パッチの適用という作業に多くのメール管理者はつきあわされることになったのでした。
(現在のsendmailはそれなりに安全と思いす。 また、閉鎖(牢獄)空間でsendmailを動かすなどの方法をとることで、かなり安全な環境で構築も可能です) 

そのような中で、規模が大きくなりすぎた sendmai に変わって、主にSMTPプロトコルだけに特化したメールサーバソフトが現れてきて、その1つとして 『qmail』、もう1つがここで紹介する『Postfix』という訳です。 時代的には Postfix より qmail の方が少し先に現れていたと思いますが、qmail はライセンス的にGPLのライセンスに適合しない部分があったために、多くのLinuxでは標準ではバンドルされていないためにあまり一般的にはなり得なかったと考えられます。 それに対して Postfix は IBM Thomas J. Watson Research Centerで開発されたVMailerを基にオープンソース化されており、現在ではsendmailとともに多くのLinuxディストリビューションに標準で搭載されているメールサーバとなっています。
なお、sendmailやqmail,Postfixはメールデータを転送する仕組み(SMTP)を実現するソフトウェアでしかありません。 転送されてきたメールを一般的なメールクライアントソフトで読み出すには、POP3またはIMAPと呼ばれるプロトコルに対応したサービスが必要になりますが、これらについてはSMTPサーバとは別のソフトで実現されています。
本資料中では、セキュリティを考慮した最新のPOP3,IMAPサーバとして dovecot を紹介します。

 

Mailサーバ(Postfix)の構築に続く

 

 

dovecotのTLS(SSL)化

POP3・IMAPのどちらの方式においてもサーバとクライアント間のデータはクリアテキストでやり取りが行われています。このため、パケットキャプチャツールなどをサーバとクライアント間に介在させる事ができれば、通信の内容を容易に盗聴する事が可能になります。 このようなデータ盗聴への対策にはサーバとクライアント間のメールデータを暗号化する事が効果的です。
POP・IMAPのメールデータの暗号化には現在、SSL(Secure Sockets Layer)とTLS(Transport Layer Security)という2つの標準的な方法が存在します。
SSLは元々、インターネット創世期にWebブラウザのシェアトップであったNetscape社がWebブラウザの為の暗号化の仕組みとして開発したものです。 その後、SSLは多くのWebブラウザでも利用できるようになり、皆さんもご存じの通り現在もデファクトスタンダード(業界標準)の機能となっています。 (SSLには現在、Ver2とVer3が存在します)
しかし、SSL自体はあくまでNetscape社の独自の暗号化方法であるため、ライセンス的にも技術実装的にも中立的な機関で策定された仕様が求められ、SSL Ver3を基にIETFにて仕様が決められたものがTLSという暗号化の為の方法です。  TLSはSSLを基にはしていますが、互換という訳ではありません。以下ではdovecotのTLS(SSL)化の手順について説明を行います。

1.サーバ証明書(電子証明書)の入手

 最初にdovecotを動作させるマシンのためのサーバ証明書を入手する必要があります。 サーバ証明書書を入手するには入手したいサーバマシンで最初にプライベートキーを作成した後、署名要求書(CSR)を作成してそれを認証局に送付する事で入手できます。 プライベートキーの作成からCSRの作成までの手順はこちらを参照してください。
ここでは、署名要求書が "dovecot_csr.pem" という名前で作成されたものとして説明します。 なお、CSRの手順の中のプライベートキーの作成については、他のサーバサービス(例えばWWWなど)と同じマシンで動かすため、既にそちらのサーバサービス用にプライベートキーを作成してある場合には、同じプライ ベートキーをそのまま使っても構いません。もし違うプライベートキーを作成する場合は、どのサーバーサービス用かが判るようにファイル名を付けると良いで しょう。

出来上がったdovecot_csr.pem(署名要求書)を認証局に送ると、それを認証してサーバ証明書(ex. "server.pem")を送り返してきます。 なお、自己認証局でサーバ証明書を発行手順については「自己認証局によるサーバ証明」を参考にして証明書を作成してください。認証局から送られてきたサーバ証明書は /etc/ssl/certs ディレクトリに保存しておくと良いでしょう。

2.TLS(SSL)の設定

 /etc/dovecot/dovecot.conf を開き以下の部分を編集します。

protocols = imap pop3 imaps pop3s   ---(※1)
  ::
#ssl_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
#ssl_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
#
ssl_cert_file = /etc/ssl/certs/server.pem
ssl_key_file = /etc/ssl/private/nopass_server.key    ---(※2)
  ::

※1 protocols パラメータに" imaps","pop3s"を追加することで、TLS、SSLを使った接続が可能になります。 
※2 "nopass_server.key"ではなく "server.key"を指定した場合、dovecotの起動時にプライベートキーのパスフェーズの入力を求められます。パスフェースはdovecotの起動時オプション「-p」または設定ファイルの中で 「ssl_key_password」を設定することで与えることも可能です。
以上でTLS(SSL)の設定は完了です。 

3. クライアント側の設定

TLS(SSL)を利用するにはクライアントソフトでTLS(SSL)を使用するように設定を行う必要があります。 代表的なメールクライアントであるOutlook ExpressとThunderbirdでのTLS(SSL)の設定方法を示しておきます。

Thunderbirdの場合

 Thunderbirdで受信(POP)の際にSSL(TLS)を利用するには、アカウント設定画面を開き、SSL(TLS)化したいサーバを選び、「TLSを使用する」または「SSLを使用する」のどちらかを選びます。 ポート番号は、TLSの場合には通常の110番を、またSSLの場合には995番を利用します。

 thunderbirdでのSSL指定

Outlook Express 6の場合

 [ツール]ー[アカウント]で[メール]のプロパティを開き、詳細タブの中の[このサーバーはセキュリティで保護された接続(SSL)が必要にチェックを入れ、ポートを995にします。

 

 

 ※最近のウィルスチェッカーではメールへの感染監視の為に、PC内でポートの110番を使ったアクセスを監視しているものがあります。 そのような 場合、暗号化されたパケットで通信を行おうとするとうまくいかないケースがあります。 TLSでうまく受信できない場合場合にはSSL(ポート995)を 使ってみてください。
なお、インターネット上から内部のメールサーバへSSLで利用させるような場合には、ファイアーウォールやNATルータなどで995番宛のパケットを内部のメールサーバへ転送させる設定を行ってください。

 


 

Mailサーバ(Postfix)の構築

1.Postfixの特徴

 Postfixは単体の1つのプログラムではなく、いくつかの単機能なプログラムを組み合わせたソフトの総称と云えます。 1つ1つのプログラムは特定の目的だけのために動作し、他のソフトの動作による影響を受けないように設計されおり、これによって例えば、「インターネットから最初にSMTPの電文を受ける」プログラムにセキュリティ上の問題が発生しても、「メールデータをファイルとして書き込む」プログラムが影響を受けないようになっています。 また、個々のプログラムはその実行ユーザが必要最小限の権限を持った状態でしか機能しないため、仮にプログラムを乗っ取られてもその権限では大したことができないようになっています。
Postfixに関する代表的な特徴としては

  • 単機能なプログラムを組み合わせて、個々のプログラムの視認性とセキュリティ上の弱点を少なくしている。
  • コンパクトで高速な処理
  • メールデータ保存形式にMaildir形式が利用可能
  • sendmail互換のコマンドを用意(ただし一部、機能制約あり)
  • オープンソース

 などがあります。

 2.Postfixのインストール

 Postfixの最新版をソースからインストールしたいなら、ダウンロードサイトからソースを入手してコンパイルすることもできますが、基本的にはパッケージとして供給されているものを使うことをお勧めします。理由は、ソースファイルからコンパイルした場合、インストール時に最新であっても結局はメンテナンスが追いつかずパッケージでのインストール状態よりもセキュリティ上の問題が取り残される危険が多いと個人的は思っているからです。 メールサーバのように常に攻撃の危険性に晒されるプログラムの場合、最新の状態に保つことが防御の基本と考えられますので最新のソースを追いかけられる時間的・金銭的余裕がある場合を除いてはパッケージの利用(自動でアップデートさせる事も可能になるので))を前提とした方が結局はセキュリティ上も運用コストも安く済むと思います。

 (Debian(Ubuntu)でのパッケージのインストール方法)

 次のコマンドを実行します。

$ sudo apt-get install postfix 

 

 (Redhat(CentOS)でのパッケージのインストール方法)

 次のコマンドを実行します。(スーパーユーザ権限で実行します)

# yum install postfix

 

 3.メールサーバの切り替え

Redhatでは、Postfixをパッケージでインストールしてもそのままでは従来のMTA(sendmail)が動作してしまい、Postfixに切り替わらないかも知れません。  利用中のMTAを切り替えるには、

# alternatives --config mta

 を実行してMTAを切り替える事ができます。 Debian(Ubuntu)ではパッケージとしてPostfixをインストールした段階でsendmailが削除されてMTAはPostfixに切り替わります。

 4.Postfixの設定

Postfixのインストールができたら、利用する環境に合わせた設定を行います。
以下では次のようなシナリオで設定を行うこととします。

設定内容
設定値
構築するサーバの名前ms.example.jp
受信対象とするアドレス(@以後の部分)
example.jp, ms.example.jp, localhost.example.jp, localhost
送信時に付加するアドレス
example.jp
サーバが配置されている組織のローカルネットワークアドレス
192.168.1.0/24, 192.168.10.0/24, 127.0.0.0/8
メールデータの保存形式
Maildir(ホームディレクトリ配下)
受信を排除するアドレス
spam-net.com, dsl.spam-net.co.jp 
受信メールボックスのサイズ制限
2GB
受信メッセージの最大サイズ
1GB
排除ルール

ローカルエリア以外でSMTPのHELO内のホスト名が不正な場合排除。
ローカルエリア以外で、DNSのMXまたはAレコードに登録されていない場合排除。

迷惑メールの検査

メールヘッダーの中で
 "from: *spam-net.com"
 "from: *dsl.spam-net.co.jp"
という送信元がある場合には排除

 

  • /etc/postfix/main.cf
    Postfixの設定の中心は /etc/postfix/main.cfファイルにて行います。
    上記のシナリオに合わせた設定は次の部分を変更していきます。
    #
    myhostname = ms.example.jp
    mydomain = example.jp
    myorigin = $mydomain

    #
    mynetworks_style = subnet
    mynetworks = 192.168.1.0/24, 192.168.10.0/24, 127.0.0.0/8


    mydestination  = allmydestination = $mydomain, $myhostname, localhost.$mydomain, localhost

    home_mailbox = Maildir/
    #
    smtpd_banner = $myhostname ESMTP $mail_name

    # mailbox_size_limit
    mailbox_size_limit = 2097152000
    # message_size_limit
    message_size_limit = 1048576000

    recipient_delimiter = +
    inet_interfaces = all

    # Spam message Reject
    header_checks = regexp:/etc/postfix/header_checks

    # Bad SMTP HELO Check
    smtpd_helo_required = yes
    smtpd_helo_restrictions = permit_mynetworks reject_unknown_hostname


    # Bat MX or A check
    smtpd_recipient_restrictions =
            permit_mynetworks,
            reject_unknown_sender_domain,
            reject

     

  • /etc/postfix/header_checks
    続いて、/etc/postfix/header_checksファイルに受信を排除したいアドレスを登録します。
    /^Received: from .*spam-net.com/ REJECT
    /^Received: from .*dsl.spam-net.co.jp/ REJECT 

以上で、基本的なPostfixの設定は完了です。 
設定項目ではありませんが、利用しているOSによっては、ユーザ登録時にホームディレクトリにMaildirディレクトリが作成されないものがあります。 Postfixの場合、Maildirが存在しなければ自動的にMaildirを作成してくれますが、Postfix以外の処理連携のために予めMaildirが作成されている事が望ましい場合もあるかもしれません。
ユーザ登録時に自動的にMaildirを作成したい場合にはスケルトンディレクトリを利用すると良いでしょう。 スケルトンディレクトリは、ユーザ登録時にそこに存在するファイルやディレクトリをコピーしてホームディレクトリとして利用するもので、通常は /etc/skel というフォルダ以下にコピーさせたいものと置いておきます。
Maildir をコピーさせたいなら以下の手順でスケルトンに作成しておきます。

(Redhat,CetOSの場合)
# cd /etc/skel
# mkdir Maildir Maildir/cur Maildir/new Maildir/tmp
# chmod -R 0700 /etc/skel/Maildir

(Debian, Ubuntuの場合)
$ cd /etc/skel
$ sudo mkdir Maildir Maildir/cur Maildir/new Maildir/tmp
$ sudo chmod -R 0700 /etc/skel/Maildir

 

 【POP・IMAPサーバ(dovecot)の構築】に続く

 

セキュリティ情報

JVN サイト新着ならびに更新情報

広告