トップ > Ubuntu 8.04 によるサーバーの構築 >

Postfix (SMTPサーバー)の構築

Postfix のインストールと設定

 メール送信用のサーバー (SMTP サーバー) を, Postfix により構築します。

  1. 以下のコマンドを実行して, Postfix をインストールする。
    $ sudo apt-get install postfix
  2. /etc/postfix/main.cf を以下のように作成する。
    mail_owner = postfix
    # ホスト名の設定
    myhostname = mail.ems.jp
    # ドメインの無いアドレスに付加する値の設定
    myorigin = ems.jp
    # ローカル配送するドメインを指定
    mydestination = $myhostname, ems.jp, war.jp
    # メール中継が可能な IP アドレスを制限
    mynetworks = 192.168.0.0/24, 127.0.0.0/8
    # ".domain"情報のないアドレスに文字列 ".$myorigin" を付加しない
    append_dot_mydomain = no
    # メールのスプールディレクトリの指定
    mail_spool_directory = /var/spool/mail
    # メールボックスファイルの最大サイズ(単位は bye。 0 は無制限)
    mailbox_size_limit = 51200000
    # 送信メールの最大サイズ(単位は byte。 0 は無制限)
    message_size_limit = 10240000
    # ヘッダをチェックするファイルの場所を指定
    header_checks = regexp:/etc/postfix/header_checks
    • /etc/postfix/main.cf に以下の行を追加することにより,中継する SMTP サーバーを指定することができる。
      # 中継するSMTPサーバーを指定
      relayhost = chuukei.ems.jp
  3. /etc/postfix/header_checks を以下のように作成して,メールヘッダに「192.168.*.*」又は「127.0.0.1」を含む行を削除する。
    /^Received:.*192\.168\..*/ IGNORE
    /^Received:.*127\.0\.0\.1.*/ IGNORE
  4. 以下のコマンドを実行して, Postfix を再起動する。
    $ sudo /etc/init.d/postfix restart
    • 以下のコマンドを実行することにより,設定ファイルが正しいかどうかを確認することが出来る。
      $ sudo /etc/init.d/postfix check ←/etc/postfix/main.cfの文法チェック
    • 以下のコマンドを実行することにより, MX レコードを問い合わせることが出来る。
      $ dig ems.jp MX
  5. root のメールをアカウント abc と xyz で共有する場合, /etc/aliases に以下の内容を追加する。
    root: abc, xyz
  6. アカウント abc のメールを abe@hoge.test.com 宛てに転送する場合, /etc/aliases に以下の内容を追加する。
    abc: abc@hoge.test.com
  7. 以下のコマンドを実行して, /etc/aliases の設定を有効にする。
    $ sudo postalias /etc/aliases
  8. 以下のコマンドを実行して,メール送信テストをする。
    $ telnet 192.168.0.1 25 ←telnet コマンドで25番ポートに接続
    Trying 192.168.0.1...
    Connected to 192.168.0.1.
    Escape character is '^]'.
    220 mail.ems.jp ESMTP Postfix
    helo mail.ems.jp ←サーバーへ挨拶
    250 mail.ems.jp
    mail from:root@ems.jp ←メールの送信元
    250 2.1.0 Ok
    rcpt to:xxx@test.test ←メールの送信先(中継拒否)
    550 <xxx@test.test>: User unknown in local recipient table
    rcpt to:root@ems.jp ←メールの送信先
    250 2.1.5 Ok
    data ←内容の書き込み開始を宣言
    354 End data with <CR><LF>.<CR><LF>
    subject:test ←表題
    abc ←本文
    DEF
    . ←ビリオドで本文の終了
    250 Ok: queued as 487582C4037
    quit ←メールの送信
    221 Bye
    Connection closed by foreign host.

サイト証明書署名要求と秘密鍵の作成

  1. 以下のコマンドを実行して,サーバーのサイト証明書署名要求 (csr.pem) と秘密鍵 (server.key) を作成する。
    $ cd /tmp
    $ openssl req -new -out csr.pem
  2. 続いて,以下のように入力する。
    Generating a 1024 bit RSA private key
    .......................++++++
    ............++++++
    writing new private key to 'privkey.pem'
    Enter PEM pass phrase:[パスワードを入力]
    Verifying password - Enter PEM pass phrase:[パスワードを再入力]
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [AU]:JP
    State or Province Name (full name) [Some-State]:Miyagi
    Locality Name (eg, city) []:Sendai
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:Self
    Organizational Unit Name (eg, section) []:[リターンキーを押す]
    Common Name (eg, YOUR name) []:192.168.0.1
    Email Address []:[リターンキーを押す]

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:[リターンキーを押す]
    An optional company name []:[リターンキーを押す]
  3. 以下のコマンドを実行することにより,秘密鍵 (privkey.pem) から,パスフレーズを削除した秘密鍵 (server.key) を作成する。
    $ sudo openssl rsa -in /tmp/privkey.pem -out /etc/ssl/private/server.key
  4. 以下のコマンドを実行して, postfix アカウントを ssl-cert グループに追加する。
    $ sudo usermod -G ssl-cert postfix

サーバー証明書の作成

  1. 以下のコマンドを実行して,前に構築した CA とサイト証明書署名要求 (csr.pem) から,サーバー証明書(自己署名証明書)を作成する。
    $ cd /usr/lib/ssl/misc/
    $ sudo openssl ca -out /etc/ssl/certs/server.crt -infiles /tmp/csr.pem

SMTP認証の利用設定

 SMTP 認証 (SMTP-AUTH) を, STARTTLS (25 番ポート) を使用した pam によるユーザ認証によって,実現します。

  1. 以下のコマンドを実行して, sasl2-bin をインストールする。
    $ sudo apt-get install sasl2-bin
  2. /etc/default/saslauthd の以下の行を変更する。
    START=yes
  3. 以下のコマンドを実行して, saslauthd を起動する。
    $ sudo /etc/init.d/saslauthd restart
  4. 以下のコマンドを実行して,ユーザ abc の SMTP 認証のテストをする。
    $ sudo testsaslauthd -s smtp -u abc -p [abcの平文パスワード]
    0: OK "Success." ←このように表示されたら大丈夫
  5. /etc/postfix/sasl/smtpd.conf を以下のように作成する。
    pwcheck_method: saslauthd
    mech_list: plain login
  6. /etc/postfix/main.cf を以下のように作成する。
    mail_owner = postfix
    myhostname = mail.ems.jp
    myorigin = ems.jp
    mydestination = $myhostname, ems.jp, war.jp
    mynetworks = 192.168.0.0/24, 127.0.0.0/8
    append_dot_mydomain = no
    mail_spool_directory = /var/spool/mail
    mailbox_size_limit = 51200000
    message_size_limit = 10240000
    header_checks = regexp:/etc/postfix/header_checks

    # TLS は有効
    smtpd_use_tls = yes
    # サーバー証明書
    smtpd_tls_cert_file = /etc/ssl/certs/server.crt
    # 秘密鍵
    smtpd_tls_key_file = /etc/ssl/private/server.key

    # SMTP 認証は有効
    smtpd_sasl_auth_enable = yes
    # SMTP 認証の Outlook への対応
    broken_sasl_auth_clients = yes
    # メールリレー許可の条件(ホワイトリスト方式)
    smtpd_recipient_restrictions = permit_sasl_authenticated, permit_auth_destination, reject
  7. 以下のコマンドを実行して, Postfix を再起動する。
    $ sudo /etc/init.d/postfix restart
  8. 以下のように,メーラー (Outlook Express) のアカウント abc の詳細設定をする。
    Outlook Expressの利用設定(その1)/Postfix/Fedora Core
  9. 続いて,以下のようにサーバー設定をする。
    Outlook Expressの利用設定(その2)/Postfix/Fedora Core
  10. 続いて,[設定]ボタンをクリックして,以下のように設定する。
    Outlook Expressの利用設定(その3)/Postfix/Fedora Core

スパムメール対策

  1. /etc/postfix/main.cf を以下のように作成する。
    mail_owner = postfix
    myhostname = mail.ems.jp
    myorigin = ems.jp
    mydestination = $myhostname, ems.jp, war.jp
    mynetworks = 192.168.0.0/24, 127.0.0.0/8
    append_dot_mydomain = no
    mail_spool_directory = /var/spool/mail
    mailbox_size_limit = 51200000
    message_size_limit = 10240000
    header_checks = regexp:/etc/postfix/header_checks

    smtpd_use_tls = yes
    smtpd_tls_cert_file = /usr/share/ssl/certs/server.crt
    smtpd_tls_key_file = /usr/share/ssl/private/server.key

    smtpd_sasl_auth_enable = yes
    broken_sasl_auth_clients = yes
    smtpd_recipient_restrictions = permit_sasl_authenticated, permit_auth_destination, reject

    # メール受信許可の条件(ブラックリスト方式)
    smtpd_client_restrictions = permit_mynetworks, check_client_access regexp:/etc/postfix/access_client, reject_unknown_client, permit
  2. /etc/postfix/access_client を,以下のように作成する(参考:阻止率99%のスパム対策方式の研究報告)。
    # 許可するアドレス
    /^61\.115\.197\.9[0-1]$/ OK
    /^64\.56\.166\.8[0-1]$/ OK
    /^210\.155\.68\.186$/ OK
    /^220\.213\.226\.141$/ OK
    /^210\.174\.207\.38$/ OK
    /^210\.188\.236\.215$/ OK

    # 拒否するアドレス
    /^[^\.]*[0-9][^0-9\.]+[0-9]/ REJECT
    /^[^\.]*[0-9]{5}/ REJECT
    /^([^\.]+\.)?[0-9][^\.]*\.[^\.]+\..+\.[a-z]/ REJECT
    /^[^\.]*[0-9]\.[^\.]*[0-9]-[0-9]/ REJECT
    /^[^\.]*[0-9]\.[^\.]*[0-9]\.[^\.]+\..+\./ REJECT
    /^(dhcp|dialup|ppp|adsl)[^\.]*[0-9]/ REJECT
    • 正規メールであるにもかかわらず「reject_unknown_client」等で引っかかってしまう場合,その送信ホストのIPアドレスを「/IPアドレス(正規表現)/ OK」のように記述する。
  3. 以下のコマンドを実行して, Postfix をリロードする。
    $ sudo /etc/init.d/postfix reload

 このサイトに書かれていることが原因でトラブルが発生しても,私は責任を取ることができません。したがいまして,ここで述べられていることに関する実行は,自己責任でお願いいたします。なお,このサイトはリンクフリーで,リンク時の連絡も不要です。

サイト内関連リンク:Apache, Dovecot