Postfix

提供: ArchWiki
2015年11月23日 (月) 19:27時点におけるKusakata (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

関連記事

Postfix のサイト より:

Postfix は高速で管理しやすくセキュアでありながら、同時に既存のユーザーを使えるように sendmail と互換性を保っています。そのため、外から見ると sendmail のようですが、中身は完全に別物です。

この記事では Postfix をセットアップする方法と基本的な設定ファイルの説明をします。ローカルのシステムユーザーだけが使える設定の手順と、仮想ユーザーを使う方法のリンクが存在します。

インストール

postfix パッケージをインストールしてください。

DNS レコード

メールホストを MX レコードに設定する必要があります。通常はドメインプロバイダの設定インターフェイスから設定できます。

メール交換レコード (MX レコード) は Domain Name System におけるリソースレコードの一つで、受取人のドメインを代表してメールメッセージを受け取るメールサーバーを指定します。

メールメッセージがインターネットから送られると、メール転送エージェントが Domain Name System から受取人のドメイン名の MX レコードを聞き出します。このクエリに対して、メール交換サーバーのホスト名のリストが返ってきます。次に、エージェントはそれらのサーバーのどれか一つと (設定番号が一番若いサーバーから順番に) SMTP 接続を確立して、接続が出来た最初のサーバーにメールを配送します。

ノート: Some mail servers will not deliver mail to you if your MX record points to a CNAME. For best results, always point an MX record to an A record definition. For more information, see e.g. Wikipedia's List of DNS Record Types.

設定

master.cf

/etc/postfix/master.cf は利用するプロトコルの種類を指定できるマスター設定ファイルです。また、パイプを設定することでスパムのチェックなどを行うこともできます。

#Secure SMTP に書かれているように Secure SMTP を有効化することを推奨します。

main.cf

/etc/postfix/main.cf はあらゆることを設定できるメイン設定ファイルです。仮想ローカルのみの配送をする場合、以下の設定を推奨します。

  • myhostname はメールサーバーが複数のドメインを持っている場合に、プライマリドメインをメールホストにしたくないときに設定してください。DNS の A レコードと MX レコードの両方がこのホスト名を指定している必要があります。
myhostname = mail.nospam.net
  • mydomain は通常 myhostname の値から最初の部分を除いた部分になります。ドメインが信頼できないときは、手動で設定してください。
mydomain = nospam.net
  • myorigin はメールの送信元になります。通常は mydomain の値に設定します。単純なサーバーなら、これで十分です。ローカルアカウントからのメール用です。ローカル転送はしないため (送信は除く)、基本的にあまり重要ではありません。
myorigin = $mydomain
  • mydestination はローカルユーザーのルックアップです。
mydestination = $myhostname, localhost.$mydomain, localhost
  • mynetworksmynetwork_style はリレーを制御し、許可するユーザーを指定します。リレーは設定しません。
mynetwork_style は host に設定して、スタンドアロンの Postfix ホストを作成し、ウェブメールを利用できるようにします。リレーは行わず、他の MTA も使いません。ただのウェブメールです。
mynetworks_style = host
  • relaydomains は Postfix のリレー先を指定します。デフォルトの値は空です。空のままでかまいません。
relay_domains =
  • home_mailbox or mail_spool_directory control how mail is delivered/stored for the users.
If set, mail_spool_directory specifies an absolute path where mail gets delivered. By default Postfix stores mails in /var/spool/mail.
home_spool_directory = /home/vmailer
Alternatively, if set, home_mailbox specifies a mailbox relative to the user's home directory where mail gets delivered (eg: /home/vmailer).
Courier-IMAP requires "Maildir" format, so you must set it like the following example with trailing slash:
home_mailbox = Maildir/
警告: If you plan on implementing SSL/TLS, please respond safely to POODLE and FREAK/Logjam by adding the following to your configuration:
smtpd_tls_mandatory_protocols=!SSLv2,!SSLv3
smtp_tls_mandatory_protocols=!SSLv2,!SSLv3
smtpd_tls_protocols=!SSLv2,!SSLv3
smtp_tls_protocols=!SSLv2,!SSLv3
smtpd_tls_exclude_ciphers = aNULL, eNULL, EXPORT, DES, RC4, MD5, PSK, aECDH, EDH-DSS-DES-CBC3-SHA, EDH-RSA-DES-CDC3-SHA, KRB5-DE5, CBC3-SHA

Then, generate a dhparam file by following these instructions and then adding the following to your configuration:

smtpd_tls_dh1024_param_file = ${config_directory}/dhparams.pem

デフォルトメッセージとメールボックスの容量制限

Postfix imposes both message and mailbox size limits by default. The message_size_limit controls the maximum size in bytes of a message, including envelope information. (default 10240000) The mailbox_size_limit controls the maximum size of any local individual mailbox or maildir file. This limits the size of any file that is written to upon local delivery, including files written by external commands (i.e. procmail) that are executed by the local delivery agent. (default is 51200000, set to 0 for no limit) If bounced message notifications are generated, check the size of the local mailbox under /var/spool/mail and use postconf to check these size limits:

# postconf -d mailbox_size_limit
mailbox_size_limit = 51200000
# postconf -d message_size_limit
message_size_limit = 10240000

エイリアス

エイリアス (別名フォワーダー) は /etc/postfix/aliases で指定できます。

root でメールを読むのはよろしくないので root に届いたメールは全て他のアカウントにマッピングするべきでしょう。

以下の行をアンコメントして、you を実際のアカウントに置き換えてください:

root: you

/etc/postfix/aliases の編集を終えたら postalias コマンドを実行してください:

postalias /etc/postfix/aliases

後で変更するときは:

newaliases
ヒント: Alternatively you can create the file ~/.forward, e.g. /root/.forward for root. Specify the user to whom root mail should be forwarded, e.g. user@localhost.
/root/.forward
user@localhost

ローカルメール

To only deliver mail to local system users (that are in /etc/passwd), you only need to change the following lines in /etc/postfix/main.cf. Uncomment them and modify them to the specifics listed below. Everything else can be left as installed.

mydestination = $myhostname, localhost.$mydomain, localhost
inet_interfaces = loopback-only
mynetworks_style = host
append_dot_mydomain = no
default_transport = error: Local delivery only!

バーチャルメール

バーチャルメールはユーザーアカウントにマッピングされないメールです (/etc/passwd)。

セットアップする方法は Virtual user mail system を見て下さい。

Postfix check

postfix check コマンドを実行してください。設定ファイルで間違っている部分が出力されます。

全ての設定を確認したいときは、postconf と入力します。デフォルト設定との差異点を確認したいときは、postconf -n を実行してみてください。

Postfix の起動とテスト

postfix.service起動・有効化してください。

Now lets see if Postfix is going to deliver mail for our test user.

nc servername 25
helo testmail.org
mail from:<test@testmail.org>
rcpt to:<cactus@virtualdomain.tld>
data
This is a test email.
.
quit

エラーレスポンス

451 4.3.0 <lisi@test.com>:Temporary lookup failure

Maybe you have entered the wrong user/password for MySQL or the MySQL socket is not in the right place.

550 5.1.1 <email@spam.me>: Recipient address rejected: User unknown in virtual mailbox table.

Double check content of mysql_virtual_mailboxes.cf and check the main.cf for mydestination

メールを受け取ったことを確認

$ find /home/vmailer と入力してください。

以下のように表示されるはずです:

/home/vmailer/virtualdomain.tld/cactus@virtualdomain.tld
/home/vmailer/virtualdomain.tld/cactus@virtualdomain.tld/tmp
/home/vmailer/virtualdomain.tld/cactus@virtualdomain.tld/cur
/home/vmailer/virtualdomain.tld/cactus@virtualdomain.tld/new
/home/vmailer/virtualdomain.tld/cactus@virtualdomain.tld/new/1102974226.2704_0.bonk.testmail.org

最後のエントリが実際のメールです。これで動作していることが確認できます。

Extra

PostfixAdmin

PostfixAdmin を使うには、Apache HTTP Server に書かれているように Apache/MySQL/PHP のセットアップが必要です。

IMAP を機能させるには、php-imap をインストールして /etc/php/php.iniimap.so をアンコメントしてください。

そして、postfixadminインストールしてください。

PostfixAdmin の設定ファイルを編集:

/etc/webapps/postfixadmin/config.inc.php
$CONF['configured'] = true;
// correspond to dovecot maildir path /home/vmail/%d/%u 
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix_user';
$CONF['database_password'] = 'hunter2';
$CONF['database_name'] = 'postfix_db';

// globally change all instances of ''change-this-to-your.domain.tld'' 
// to an appropriate value

If installing dovecot and you changed the password scheme in dovecot (to SHA512-CRYPT for example), reflect that with postfix

/etc/webapps/postfixadmin/config.inc.php
$CONF['encrypt'] = 'dovecot:SHA512-CRYPT';

As of dovecot 2, dovecotpw has been deprecated. You will also want to ensure that your config reflects the new binary name.

/etc/webapps/postfixadmin/config.inc.php
$CONF['dovecotpw'] = "/usr/sbin/doveadm pw";

Create the Apache configuration file:

/etc/httpd/conf/extra/httpd-postfixadmin.conf
Alias /postfixadmin "/usr/share/webapps/postfixAdmin"
<Directory "/usr/share/webapps/postfixAdmin">
    DirectoryIndex index.html index.php
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>

To only allow localhost access to postfixadmin (for heightened security), add this to the previous <Directory> directive:

   Order Deny,Allow
   Deny from all
   Allow from 127.0.0.1

Now, include httpd-postfixadmin.conf to /etc/httpd/conf/httpd.conf:

# PostfixAdmin configuration
Include conf/extra/httpd-postfixadmin.conf
ノート: If you go to yourdomain/postfixadmin/setup.php and it says do not find config.inc.php, add /etc/webapps/postfixadmin to the open_basedir line in /etc/php/php.ini.
ノート: If you get a blank page check the syntax of the file with php -l /etc/webapps/postfixadmin/config.inc.php.

Secure SMTP

STARTTLS over SMTP (ポート 587)

To enable STARTTLS over SMTP (port 587, the proper way of securing SMTP), uncomment these lines in

/etc/postfix/master.cf
smtp      inet  n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes

If you need support for the deprecated SMTPS port 465, read the next section.

SMTPS (ポート 465)

The deprecated method of securing SMTP is using the wrapper mode which uses the system service smtps as a non-standard service and runs on port 465.

To enable it uncomment the following lines in

/etc/postfix/master.cf
smtps     inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes

And verify that these lines are in /etc/services:

smtps 465/tcp # Secure SMTP
smtps 465/udp # Secure SMTP

If they are not there, go ahead and add them (replace the other listing for port 465). Otherwise Postfix will not start and you will get the following error:

postfix/master[5309]: fatal: 0.0.0.0:smtps: Servname not supported for ai_socktype

SpamAssassin

spamassassin パッケージをインストールしてください。

/etc/mail/spamassassin/local.cf を開いて必要に応じて設定して下さい。

SpamAssassin のマッチパターンを更新:

# sa-update
ノート: If you want to combine SpamAssassin and Dovecot Mail Filtering, ignore the next two lines and continue further down instead.

/etc/postfix/master.cf を編集して smtp の下にコンテンツ・フィルタを追加:

smtp      inet  n       -       n       -       -       smtpd
  -o content_filter=spamassassin

以下の SpamAssassin のサービスエントリも追加:

spamassassin unix -     n       n       -       -       pipe
  flags=R user=spamd argv=/usr/bin/vendor_perl/spamc -e /usr/bin/sendmail -oi -f ${sender} ${recipient}

上記の設定をしたら spamassassin.service起動できます。

SpamAssassin と Dovecot LDA / Sieve を組み合わせる (Mailfiltering)

Dovecot#Sieve に書かれているように LDA と Sieve-Plugin をセットアップします。ただし最後の行 mailbox_command... は無視してください。

代わりに /etc/postfix/master.cf にパイプを追加:

dovecot   unix  -       n       n       -       -       pipe
      flags=DRhu user=vmail:vmail argv=/usr/bin/vendor_perl/spamc -u spamd -e /usr/lib/dovecot/dovecot-lda -f ${sender} -d ${recipient}

そして /etc/postfix/main.cf で有効化:

virtual_transport = dovecot

SpamAssassin と Dovecot LMTP / Sieve を組み合わせる

Dovecot#Sieve に書かれているように LMTP と Sieve をセットアップします。

/etc/dovecot/conf.d/90-sieve.conf を編集して以下を追加:

sieve_before = /etc/dovecot/sieve.d/
sieve_extensions = +vnd.dovecot.filter
sieve_plugins = sieve_extprograms

ディレクトリを作成:

# mkdir /etc/dovecot/sieve.d/

新しいファイル /etc/dovecot/sieve.d/spamassassin.sieve を以下の内容で作成:

require [ "vnd.dovecot.filter" ];
filter "spamc" [ "--no-safe-fallback" ];

sieve ルール spamassassin.svbin をコンパイル:

# cd /etc/dovecot/sieve.d
# sievec spamassassin.sieve

最後に、dovecot.service再起動してください。

Razor を使う

先に SpamAssassin をインストールしてから、razor パッケージをインストールしてください。

Razor の登録:

# mkdir /etc/mail/spamassassin/razor
# chown spamd:spamd /etc/mail/spamassassin/razor
# sudo -u spamd -s
$ razor-admin -home=/etc/mail/spamassassin/razor -register
$ razor-admin -home=/etc/mail/spamassassin/razor -create
$ razor-admin -home=/etc/mail/spamassassin/razor -discover

/etc/mail/spamassassin/local.cf に以下を追加:

razor_config /etc/mail/spamassassin/razor/razor-agent.conf

/etc/mail/spamassassin/razor/razor-agent.conf に以下を追加:

razorhome = /etc/mail/spamassassin/razor/

最後に、spamassassin.service再起動してください。

送信者の IP とユーザーエージェントを Received ヘッダに載せない

This is a privacy concern mostly, if you use Thunderbird and send an email. The received header will contain your LAN and WAN IP and info about the email client you used. (Original source: AskUbuntu) What we want to do is remove the Received header from outgoing emails. This can be done by the following steps:

Add this line to main.cf

smtp_header_checks = regexp:/etc/postfix/smtp_header_checks

Create /etc/postfix/smtp_header_checks with this content:

/^Received: .*/     IGNORE
/^User-Agent: .*/   IGNORE

Finally, restart postfix.service

参照