トップ > Fedora Core4によるサーバーの構築 >

Postfix(SMTPサーバー)の構築

Postfixのインストールと設定

  1. rootでログインする。
  2. 以下のコマンドを実行して,Postfixをインストールする。
    # yum install postfix
  3. /etc/pam.d/smtpを以下のように作成する。
    
    auth	required	pam_nologin.so
    auth	required	pam_ldap.so
    account	required	pam_ldap.so
    
  4. /etc/postfix/main.cfを以下のように作成する。
    mail_owner = postfix
    # ホスト名の設定
    myhostname = mail.shitomi.jp
    # ドメインの無いアドレスに付加する値の設定
    myorigin = shitomi.jp
    # ローカル配送するドメインを指定
    mydestination = $myhostname, shitomi.jp, shitomi.net
    # メール中継が可能なIPアドレスを制限
    mynetworks = 192.168.0.0/24, 127.0.0.0/8
    # ".domain"情報のないアドレスに文字列 ".$myorigin" を付加しない
    append_dot_mydomain = no
    # メールのスプールディレクトリの指定
    mail_spool_directory = /var/spool/mail
    # メールボックスファイルの最大サイズ(単位はbyte。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.shitomi.jp
  5. /etc/postfix/header_checksを以下のように作成して,メールヘッダに「192.168.*.*」又は「127.0.0.1」を含む行を削除する。
    /^Received:.*192\.168\..*/ IGNORE
    /^Received:.*127\.0\.0\.1.*/ IGNORE
  6. 以下のコマンドを実行して,Postfixを起動する。
    # /etc/init.d/postfix start
    • 以下のコマンドを実行することにより,設定ファイルが正しいかどうかを確認することが出来る。
      # /etc/rc.d/init.d/postfix check ←/etc/postfix/main.cfの文法チェック
      [  OK  ] ←このように表示されたら大丈夫
    • 以下のコマンドを実行することにより,MXレコードを問い合わせることが出来る。
      # dig shitomi.jp MX
  7. 以下のコマンドを実行して,Postfixの標準のラン・レベルをonにする。
    # chkconfig postfix on
  8. rootのメールをアカウントabcとxyzで共有する場合,/etc/aliasesに以下の内容を追加する。
    root: abc, xyz
  9. アカウントabcのメールをabe@hoge.test.com宛てに転送する場合,/etc/aliasesに以下の内容を追加する。
    abc: abc@hoge.test.com
  10. 以下のコマンドを実行して,/etc/aliasesの設定を有効にする。
    # postalias /etc/aliases
  11. 以下のコマンドを実行して,メール送信テストをする。
    # telnet 192.168.0.1 25 ←telnetコマンドで25番ポートに接続
    Trying 192.168.0.1...
    Connected to 192.168.0.1.
    Escape character is '^]'.
    220 mail.shitomi.jp ESMTP Postfix
    helo mail.shitomi.jp ←サーバーへ挨拶
    250 mail.shitomi.jp
    mail from:root@shitomi.jp ←メールの送信元
    250 Ok
    rcpt to:xxx@test.test ←メールの送信先(中継拒否)
    550 <xxx@test.test>: User unknown in local recipient table
    rcpt to:root@shitomi.jp ←メールの送信先
    250 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.

SMTP認証の利用設定

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

  1. rootでログインする。
  2. 以下のコマンドを実行して,cyrus-sasl-plainをインストールする。
    # yum install cyrus-sasl-plain
  3. /etc/sysconfig/saslauthdを以下のように作成する。
    SOCKETDIR=/var/run/saslauthd
    MECH=pam
  4. 以下のコマンドを実行して,saslauthdを起動する。
    # /etc/init.d/saslauthd start
  5. 以下のコマンドを実行して,saslauthdの標準のラン・レベルをonにする。
    # chkconfig saslauthd on
  6. SELinuxのPermissiveモードで以下のコマンドを実行して,ユーザabcのSMTP認証のテストをする。
    # testsaslauthd -s smtp -u abc -p [abcの平文パスワード]
    0: OK "Success." ←このように表示されたら大丈夫
  7. /usr/lib/sasl2/smtpd.confを以下のように作成する。
    pwcheck_method: saslauthd
    mech_list: plain login
  8. 前に作成した秘密鍵サーバー証明書を利用して,/etc/postfix/main.cfを以下のように作成する。
    mail_owner = postfix
    myhostname = mail.shitomi.jp
    myorigin = shitomi.jp
    mydestination = $myhostname, shitomi.jp, shitomi.net
    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/pki/tls/certs/server.crt
    # 秘密鍵
    smtpd_tls_key_file = /etc/pki/tls/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
  9. 以下のコマンドを実行して,Postfixを再起動する。
    # /etc/init.d/postfix restart
  10. 以下のように,メーラー(Outlook Express)のアカウントabcの詳細設定をする。
    Outlook Expressの利用設定(その1)/Postfix/Fedora Core
  11. 続いて,以下のようにサーバー設定をする。
    Outlook Expressの利用設定(その2)/Postfix/Fedora Core
  12. 続いて,[設定]ボタンをクリックして,以下のように設定する。
    Outlook Expressの利用設定(その3)/Postfix/Fedora Core

スパムメール対策

  1. rootでログインする。
  2. /etc/postfix/main.cfを以下のように作成する。
    mail_owner = postfix
    myhostname = mail.shitomi.jp
    myorigin = shitomi.jp
    mydestination = $myhostname, shitomi.jp, shitomi.net
    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
  3. /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」のように記述する。
  4. 以下のコマンドを実行して,Postfixをリロードする。
    # /etc/init.d/postfix reload

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

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