「Postfix」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
9行目: 9行目:
 
{{Related4|OpenSMTPD}}
 
{{Related4|OpenSMTPD}}
 
{{Related4|OpenDMARC}}
 
{{Related4|OpenDMARC}}
{{Related4|OpenDKIM}}
+
{{Related|OpenDKIM}}
 
{{Related articles end}}
 
{{Related articles end}}
 
[http://www.postfix.org/ Postfix のサイト] より:
 
[http://www.postfix.org/ Postfix のサイト] より:
251行目: 251行目:
 
==== STARTTLS over SMTP (ポート 587) ====
 
==== STARTTLS over SMTP (ポート 587) ====
   
To enable STARTTLS over SMTP (port 587, the proper way of securing SMTP), add the following lines to {{ic|main.cf}}
+
STARTTLS over SMTP (ポート 587, SMTP をセキュア化するプロトコル) を有効にするには、以下の行を {{ic|main.cf}} に追加してください:
   
 
{{hc|/etc/postfix/main.cf|2=
 
{{hc|/etc/postfix/main.cf|2=
259行目: 259行目:
 
}}
 
}}
   
  +
また {{ic|master.cf}} から以下の行のコメントを取り外してサービスを有効にしてください:
Also in {{ic|master.cf}} find and remove the comment from the following line to enable the service on that port:
 
   
 
{{hc|/etc/postfix/master.cf|2=
 
{{hc|/etc/postfix/master.cf|2=
265行目: 265行目:
 
}}
 
}}
   
  +
古い SMTPS ポートの 465 を使う必要がある場合、次のセクションを読んでください。
If you need support for the deprecated SMTPS port 465, read the next section.
 
   
 
==== SMTPS (ポート 465) ====
 
==== SMTPS (ポート 465) ====
383行目: 383行目:
 
sieve_filter_exec_timeout = 120s #this is often needed for the long running spamassassin scans, default is otherwise 10s
 
sieve_filter_exec_timeout = 120s #this is often needed for the long running spamassassin scans, default is otherwise 10s
   
ディレクトリを作成:
+
ディレクトリを作成して dovecot が実行できるように spamassassin をバイナリとして配置:
   
# mkdir /etc/dovecot/sieve.d/
+
# mkdir /etc/dovecot/sieve-filter
  +
# ln -s /usr/bin/vendor_perl/spamc /etc/dovecot/sieve-filter/spamc
   
 
新しいファイル {{ic|/etc/dovecot/sieve.before.d/spamassassin.sieve}} を以下の内容で作成:
 
新しいファイル {{ic|/etc/dovecot/sieve.before.d/spamassassin.sieve}} を以下の内容で作成:
   
 
require [ "vnd.dovecot.filter" ];
 
require [ "vnd.dovecot.filter" ];
filter "spamc" [ "-d", "127.0.0.1', "--no-safe-fallback" ];
+
filter "spamc" [ "-d", "127.0.0.1", "--no-safe-fallback" ];
   
 
sieve ルール {{ic|spamassassin.svbin}} をコンパイル:
 
sieve ルール {{ic|spamassassin.svbin}} をコンパイル:

2016年11月1日 (火) 23:18時点における版

関連記事

Postfix のサイト より:

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

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

インストール

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

設定

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, $mydomain
  • mynetworksmynetworks_style はリレーを制御し、許可するユーザーを指定します。リレーは設定しません。
mynetwork_style は host に設定して、スタンドアロンの Postfix ホストを作成し、ウェブメールを利用できるようにします。リレーは行わず、他の MTA も使いません。ただのウェブメールです。
mynetworks_style = host
  • relaydomains は Postfix のリレー先を指定します。デフォルトの値は空です。空のままでかまいません。
relay_domains =
  • home_mailboxmail_spool_directory はユーザーのメールの保存場所を指定します。
設定する場合、mail_spool_directory にはメールを保存するディレクトリを絶対パスで指定します。デフォルトでは Postfix は /var/spool/mail にメールを保存します。
mail_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 はデフォルトでメッセージとメールボックスの容量を制限します。message_size_limit にはメッセージの最大容量をバイト数で指定します (デフォルトは 10240000 です)。mailbox_size_limit はローカルのメールボックスや maildir ファイルの最大容量を決めます (デフォルトは 51200000 で、制限したくない場合は 0 に設定します)。配達時に書き出されるあらゆるファイルの容量を制限します。ローカルの配達エージェントによって実行された外部コマンド (procmail) で書き出されるファイルも含みます。バウンスメッセージの通知が生成される場合、/var/spool/mail 下のローカルのメールボックスの容量を確認して、postconf を使って容量制限を確認してください:

# postconf mailbox_size_limit
mailbox_size_limit = 51200000
# postconf 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

ローカルメール

(/etc/passwd に記述されている) ローカルのシステムユーザーにしかメールを配達しないようにするには、/etc/postfix/main.cf を以下のように変更してください:

myhostname = localhost
mydomain = localdomain
mydestination = $myhostname, localhost.$mydomain, localhost
inet_interfaces = $myhostname, localhost
mynetworks_style = host
default_transport = error: outside mail is not deliverable

他の設定を変更する必要はありません。上記のように設定したら、エイリアスを設定して Postfix を起動してください。

バーチャルメール

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

セットアップする方法は仮想ユーザーメールシステムを見てください。

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.

設定のチェック

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

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

Postfix の起動

ノート: エイリアスを全く設定していない場合でも Postfix を起動するには最低でも1回は newaliases を実行する必要があります。

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

テスト

Postfix がテストユーザーにメールを配達できるかどうか確認してみましょう:

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

MySQL のユーザー名やパスワードが間違っているか、あるいは MySQL のソケットが正しい場所に存在しません。

Postfix を起動する前に newaliases を実行していなかった場合も上記のエラーが発生します。Postfix をローカルのみで使用するのであれば MySQL は不要です。

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

mysql_virtual_mailboxes.cf の中身や main.cfmydestination をチェックしてください。

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

$ 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

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

Tips and tricks

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

詳しくは Postfix TLS Support を見てください。

STARTTLS over SMTP (ポート 587)

STARTTLS over SMTP (ポート 587, SMTP をセキュア化するプロトコル) を有効にするには、以下の行を main.cf に追加してください:

/etc/postfix/main.cf
smtpd_tls_security_level = may
smtpd_tls_cert_file = /path/to/cert.pem
smtpd_tls_key_file = /path/to/key.pem

また master.cf から以下の行のコメントを取り外してサービスを有効にしてください:

/etc/postfix/master.cf
submission inet n       -       n       -       -       smtpd

古い SMTPS ポートの 465 を使う必要がある場合、次のセクションを読んでください。

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 のルールの更新

SpamAssassin のマッチパターンを更新してコンパイル:

# sa-update
# sa-compile

上記のコマンドを定期的に実行したい場合、Systemd/タイマーを使用するのが良いでしょう。

以下のサービスを作成してください:

/etc/systemd/system/spamassassin-update.service
[Unit]
Description=spamassassin housekeeping stuff

[Service]
User=spamd
Group=spamd
Type=oneshot
ExecStart=-/usr/bin/vendor_perl/sa-update --allowplugins #You can remove the allowplugins options if you do not want direct plugin updates from SA.
ExecStart=-/usr/bin/vendor_perl/sa-compile
# You can automatically train SA's bayes filter by uncommenting this line and specifying the path to a mailbox where you store email that is spam (for ex this could be yours or your users manually reported spam)
#ExecStart=-/usr/bin/vendor_perl/sa-learn --spam <path to your spam>

そして、上記のサービスを毎日実行するタイマーを作成してください:

/etc/systemd/system/spamassassin-update.timer
[Unit]
Description=spamassassin house keeping

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

最後に SpamAssassin の systemd サービスを修正して新しいルールを読み込むために再起動するように設定してください。パッケージに含まれているサービスファイルをカスタムサービスファイルにコピー:

# cp /usr/lib/systemd/system/spamassassin.service /etc/systemd/system

そして新しく作成した /etc/systemd/system/spamassassin.service に以下を記述してください:

[Unit]
PartOf=spamassassin-update.service

This will ensure that Spamassassin's spamd is restarted just before the timer runs. This means the rules will be available the next day if your timer runs daily. This is so that there is no long service interruption while sa.service runs as it takes a while to compile rules.

そして spamassassin-update.service起動有効化してください。

SpamAssassin のスタンドアロン設定

ノート: 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 を組み合わせる (メールフィルタリング)

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-plugins.conf を編集して以下を追加:

sieve_before = /etc/dovecot/sieve.before.d/
sieve_extensions = +vnd.dovecot.filter
sieve_plugins = sieve_extprograms
sieve_filter_bin_dir = /etc/dovecot/sieve-filter
sieve_filter_exec_timeout = 120s #this is often needed for the long running spamassassin scans, default is otherwise 10s

ディレクトリを作成して dovecot が実行できるように spamassassin をバイナリとして配置:

# mkdir /etc/dovecot/sieve-filter
# ln -s /usr/bin/vendor_perl/spamc /etc/dovecot/sieve-filter/spamc

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

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

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

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

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

SpamAssassin から ClamAV を実行

ClamAV に書かれているように clamd をインストール・設定してください。

上記の設定のどれかにしたがってメールシステムから SpamAssassin が呼び出されるようにしてください。

perl-cpanplus-dist-arch パッケージをインストールして、以下のように ClamAV の perl ライブラリをインストールしてください:

# /usr/bin/vendor_perl/cpanp -i File::Scan::ClamAV

http://wiki.apache.org/spamassassin/ClamAVPlugin から2つのファイルを /etc/mail/spamassassin/ に追加してください。/etc/mail/spamassassin/clamav.pm を編集して $CLAM_SOCK で clamd のソケットを指定してください (デフォルトでは /var/lib/clamav/clamd.sock です)。

最後に、spamassassin.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

ルールベースのメール処理

ポリシーサービスを使うことで Postfix のメール処理を簡単に設定できます。postfwdpolicydAUR はそのようなポリシーサービスを提供して、SPF のポリシーチェックに加えて送信者や受信者のグレイリスト・ブラックリストを簡単に作ることができます。

ポリシーサービスはスタンドアロンなサービスであり、以下のようにして Postfix に接続します:

/etc/postfix/main.cf
smtpd_recipient_restrictions =
  ...
  check_policy_service unix:/run/policyd.sock
  check_policy_service inet:127.0.0.1:10040

最後にポリシーサービスを配置することで正当なメールだけでが処理されるようになり、負担が減ります。受信されたメッセージ全てが通るように permit ステートメントよりも前に配置してください。

DANE (DNSSEC)

リソースレコード

警告: This is not a trivial section. Be aware that you make sure you know what you are doing. You better read Common Mistakes before.

DANE は複数のタイプのレコードをサポートしていますが、全てが Postfix で使われるわけではありません。

Certificate Usage Field の0はサポートされておらず、1は3にマッピングされていて、2はオプションです。したがって "3" レコードを使用することが推奨されます。詳しくは DANE#リソースレコードを見てください。

設定

Opportunistic DANE is configured this way:

/etc/postfix/main.cf
smtpd_use_tls = yes
smtp_dns_support_level = dnssec
smtp_tls_security_level = dane
/etc/postfix/master.cf
dane       unix  -       -       n       -       -       smtp
  -o smtp_dns_support_level=dnssec
  -o smtp_tls_security_level=dane

To use per-domain policies, e.g. opportunistic DANE for example.org and mandatory DANE for example.com, use something like this:

/etc/postfix/main.cf
indexed = ${default_database_type}:${config_directory}/

# Per-destination TLS policy
#
smtp_tls_policy_maps = ${indexed}tls_policy

# default_transport = smtp, but some destinations are special:
#
transport_maps = ${indexed}transport
transport
example.com dane
example.org dane
tls_policy
example.com dane-only
ノート: For global mandatory DANE, change smtp_tls_security_level to dane-only. Be aware that this makes postfix tempfail on all delivieres that do not use DANE at all!

Full documentation is found here.

参照