投げ銭

★当サイトへの投げ銭(PayPal)★

LINK


(無償、寄付歓迎) logo
世界中で使われるISO標準オフィスソフト(MSオフィス互換)

★LibreOfficeの導入事例★
詳細について

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

★当サイトへの投げ銭(PayPal)★

2016年1月31日日曜日

【Fetchmail 6.3.17】【Postfix 2.6.6 x86_64】POP3サーバ内メールを取得してメールボックスに配信するまでの方法【Linux CentOS6.7 64bit minimal】

(追加)
2018/11/26 環境変数 FETCHMAILHOMEについて


「POPサーバからFetchmailで受信しPostfixを経てバーチャルユーザーメールボックスへ配送する」


インターネット上にある複数のPOPサーバから、FetchmailというプログラムでPOPでメールを取得し、
ローカルで動作しているPostfixを経由して、メールボックス(MailDir)に保存する。


[ POPサーバ ] ====Internet===> [ (Fectchmail)--> (Postfix)--> メールボックス(MailDir) ]


このメールボックスへは、Dovecotなどを通してメールクライアントからアクセス可能である。
Dovecotの設定(IMAPサーバの構築)については、下記ページに記載している。
http://akira-arets.blogspot.jp/2016/01/dovecot-basic-imap-setting.html


以下では、上図のアンダーラインを引いた部分の設定について記載している。

最初に、Postfixの設定を行う。
Fetchmailから転送されてくるメールをメールボックスに保存できるようにする。
次に、Fetchmaiの設定を行い、POPサーバからメールを受信してPostfixへ転送できるようにする。



◆Postfixの設定
(バーチャルドメイン・バーチャルユーザー)

今回は、CentOS 6.7 64bit minimalを用いて構築を行った。
このシステムには、最初からPostfixがインストールされていて動作している。


(1)バーチャルドメインの設定を行った。

fetchmail(後で設定を行う)がPOPサーバから取得したメールを、Postfixが受け取るようにする。
このとき、複数のPOPサーバから宛先ドメインの異なるメールを受信したとしても
ローカルメールボックスに保存できるようにする。

このためにはまず、受信したいメールの宛先ドメインであるようにPostfixを設定する必要がある。
「バーチャルドメイン」という機能を使ってこれら複数のドメインを設定する。

# postconf -e virtual_mailbox_domains=example.com


(2)バーチャルユーザー用メールボックスの設置先となるベースディレクトリの作成と設定

○新しく作成する場合、次のようにした。
・ベースディレクトリは任意であるが、/home/VMAILとした。
# mkdir /home/VMAIL
・ベースディレクトリ以下にアクセスする時に使うLinuxユーザーを作成した。(詳細についてはこちら
Dovecotなどとバーチャルユーザー用メールボックスを共用するために、このユーザー情報は統一しておくこと。
useradd -u 5000 -s /sbin/nologin -M -U vmail
・アクセス権限の付与を行った。
chown vmail:vmail /home/VMAIL

○ベースディレクトリを登録した。

# postconf -e virtual_mailbox_base=/home/VMAIL

○ベースディレクトリ以下にアクセスする時のユーザーIDとグループIDを登録した。

# postconf -e virtual_uid_maps=static:5000
# postconf -e virtual_gid_maps=static:5000

○ベースディレクトリ以下にアクセスする時のUIDが5000以上であるように制限する。

これは、間違ったUID設定によって、想定しないディレクトリにメールが書き込まれることを防止する。

# postconf -e virtual_minimum_uid=5000


(3)バーチャルユーザー宛のメールと配信先バーチャルユーザーメールボックスとの関連付け

○関連付け用のファイルを作成し、ハッシュ化を行った。

# vi /etc/postfix/vmailbox
virtualuser1@example.com example.com/test1/
virtualuser2@example.com example.com/test2/
virtualuser0@example.com example.com/test0/

上の例の1行目は、配送先がvirtualuser1@example.com宛てのメールを、
example.com/test1/ にMaildir形式で保管するように設定している。

Maildir形式で保存するには、example.com/test1/ のように、最後にスラッシュを付ける

2件目以降も同様である。


作成したvmailboxファイルをハッシュ化した。

# postmap /etc/postfix/vmailbox
# ls /etc/postfix/vmailbox*
vmailbox  vmailbox.db
dbという拡張子のファイルが生成された。

○作成した関連付け用ファイルを登録した。

# postconf -virtual_mailbox_maps=hash:/etc/postfix/vmailbox


(3)特定のアドレス(あるいはドメイン)宛のメールを別のメールアドレスへ転送する設定

○転送設定用ファイルを作成した。

オリジナルの最初から存在しているファイルをリネームしておく。
# mv virtual virtual.orig

新しく作成する。
# vi virtual
postmaster@example.com postmaster
#@example.com postmaster@another.domain
スペースで区切られた最初の項目が転送を受けるメールの宛先アドレスやドメインである。
転送先アドレスの指定で、ドメイン名を省略するとローカルアカウントに転送される。
2行目では#(コメントアウト)を行っている。
@ドメイン名の指定で、このドメイン宛ての全メールが転送対象になる。


同様にハッシュ化を行った。

# postmap virtual
# ls virtual*
virtual  virtual.db  virtual.orig
○作成した関連付け用ファイルを登録した。

postconf -virtual_alias_maps=hash:/etc/postfix/virtual


(4)設定を反映させるためサービスをリスタート

Postfixへの設定は、サービスをリスタートすることによって反映される。

# service postfix restart


以上で、Postfix側の設定は完了である。



◇Postfixで設定した内容を確認した。

# postconf  -n

<設定前>
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
html_directory = no
inet_interfaces = localhost
inet_protocols = all
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
mydestination = $myhostname, localhost.$mydomain, localhost
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES
sample_directory = /usr/share/doc/postfix-2.6.6/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
unknown_local_recipient_reject_code = 550
<これまでの手順で、加えられた設定>
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_gid_maps = static:5000
virtual_mailbox_base = /home/VMAIL
virtual_mailbox_domains = example.com
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000



◆Postfixのその他の設定


(a)受信可能な最大サイズの設定

受信するメールのサイズが大きい場合に、次のようなエラーが発生して、Postfixは受け取りを拒否する。
552 5.3.4 Message size exceeds fixed limit
この制限値は、次のパラメーターで変更することが可能である。

初期値は次の様になっている。
# postconf message_size_limit
message_size_limit = 10240000
例えば、次の様にして、受信可能な最大サイズを20MBに変更できる。
# postconf -e message_size_limit=20480000

この値は、fetchmailが接続する上位サーバーの設定よりも大きくするべきだと思う
上位サーバーがメールを受け取るのならば、fetchmailよりも下位のPostfixも受け取れるようになる。


また、メールボックスに保存できるメールのサイズの制限値も考慮する必要がある。
初期値は、次の様になっている。
# postconf mailbox_size_limit
mailbox_size_limit = 51200000



続けて、POPサーバからメールを受信するFetchmailのインストールと設定を行った。

◆Fetchmailのインストール

○fetchmailについての情報を表示させた。

# yum info fetchmail
Name        : fetchmail
Arch        : x86_64
Version     : 6.3.17
Release     : 1.el6
Size        : 507 k
Repo        : base
Summary     : A remote mail retrieval and forwarding utility
URL         : http://fetchmail.berlios.de/
License     : GPL+ and Public Domain
Description : Fetchmail is a remote mail retrieval and forwarding utility
            : intended for use over on-demand TCP/IP links, like SLIP or PPP
            : connections. Fetchmail supports every remote-mail protocol
            : currently in use on the Internet (POP2, POP3, RPOP, APOP, KPOP,
            : all IMAPs, ESMTP ETRN, IPv6, and IPSEC) for retrieval
. Then
            : Fetchmail forwards the mail through SMTP so you can read it
            : through your favorite mail client.
            :
            : Install fetchmail if you need to retrieve mail over SLIP or PPP
            : connections.


○fetchmailをインストールした。

# yum install fetchmail
Installed:
  fetchmail.x86_64 0:6.3.17-1.el6

Dependency Installed:
  hesiod.x86_64 0:3.1.0-19.el6        procmail.x86_64 0:3.22-25.1.el6_5.1

Complete!


◆Fetchmailの設定

/etc/fetchmailrc ファイルを作成して、必要な設定を書き込んだ。

# vi /etc/fetchmailrc
set postmaster postmaster@example.com
set syslog
set no bouncemail

defaults:
timeout 300
antispam -1
batchlimit 100

poll pop.example.com protocol pop3:
       username "pop3userid1" password "pop3password1" is "virtualuser1@example.com" here
fetchall
smtphost 127.0.0.1/2525
#nokeep
#ssl
;
       username "pop3userid2" password "pop3password2" is "virtualuser2@example.com" here
fetchall
#nokeep
#ssl
;

poll  other.example.com with proto POP3
  user 'pop3userid0' there with password 'pop3password0' is virtualuser0@example.com

<各項目の説明>

(1)postmasterの設定

fetchmailが受信したメールをMTAに転送しようとして、いくつの場合で失敗したとき、
postmasterとして指定したアカウントやメールアドレスに代わりに転送する。
(例) ローカルのrootがポストマスターになる
set postmaster root
(例) postmaster@example.comがポストマスターになる
set postmaster postmaster@example.com

(2)bounce mailの送出先の設定

fetchmailが受信したメールをMTAに転送しようとして、いくつかの場合で失敗したとき、
その旨を通知するためのbounce mailを、メールの送信者に返すかどうか指定する。
( 転送に失敗したメールそのものとは違うので注意 )
・ set no bouncemail
(ポストマスターにはその旨を通知する。)

・ set bouncemail
(メールの送信者にその旨を通知する。)
bouncemailを有効化すると、あて先不明などでMTAが送達できない場合、送信者に対してbounce mailとなる。
そのため、送信元が偽られたメールを使ったなんらかの攻撃に加担することになってしまう可能性があるので注意する。



(3)ログの設定
set syslog
syslogを使ってロギングする。
/var/log/maillog に、fetchmailという名前で記録される。
(下記で挙げるように、fetchmailコマンドに -v オプションをつけることで、詳細が記録される。)



(4)POPサーバからメールを取得する際の共通設定

「default:」 に続けて「poll」」行が始まる前に記述する。
defaults:
timeout 300
antispam -1
batchlimit 100

・ timeout

メール受信時メールサーバーが応答を返さない場合に待機する時間(秒)を設定する。
指定した時間が経過すると、fetchmailは待機を止めてコネクションを切断する。
(この設定を行わなければ、tcpコネクションがタイムアウトするまで長時間待つことになってしまう。)


・ antispam

fetchmailがMTAにメールを転送するとき、このMTAから受け取るエラーコードのうち、
スパムメールの配送拒否を示すコードを設定する。
(この設定を行うのは、MTAによってコードが異なるためである。)

設定例のように、-1を設定すると、無効化される。

ところで、このantispamとして設定されたコードをMTAから受け取った場合、
fetchmailはメールを削除する。
他にも、メールが削除対象になるエラーコードには次のものがある。 
552 メッセージが設定された最大サイズを超過している(bounce mailが送られる)
553 送信元ドメインが不正である(bounce mailすら送らない。)

・ batchlimit

fetmailが取得したメールをMTAへ転送する処理で一度に取り扱えるメール数を設定する。
これを超える場合、MTAとのコネクションが再構築される。


(5)POPサーバに接続しメールを取得してMTA(Postfix)へ転送するための設定

上記の設定からの抜粋である。
poll pop.example.com protocol pop3
       username "pop3userid1" password "pop3password1" is "virtualuser1@example.com" here
fetchall
smtphost 127.0.0.1/2525
#nokeep
#ssl
;
poll に続けて、メールサーバを指定する。
protocol に続けて、使用するプロトコルを指定する。
そのときに使用するユーザー名とパスワードは、それぞれ、usernameとpasswordの後で指定する。

is と hereの間に指定しているメールアドレスは、受信したメールの転送先である。
(今回の一連の例では、Postfixのバーチャルメールユーザーとなっている。)

fetchall は、受信済みかどうかに関わらず、すべてのメールを受信対象とすることを示す。

nokeep は、受信したメールを同時に削除するための指定である。(例では、#でコメントアウトしている。)

ssl は、対象サーバが公開鍵証明書を使用するTSL/SSL通信を行う場合に指定する。(例では、#でコメントアウトしている。)

smtphost 127.0.0.1/2525 は、リレー先のsmtpサーバ(127.0.0.1の2525ポート)を明示的に指定している。

☆もし、Fetchmailで受信したメールについて、
ローカル配信担当のPostfixにおいて転送ループエラーが発生してバウンスされる場合には、
次のページで説明されるオプションをFetchmailに追加すると改善されるかもしれない。

http://akira-arets.blogspot.jp/2017/08/fetchmail-postfix-mailforwardingloop.html



◆Fetchmailの動作テスト

以上までの設定を完了させると、FetchmailはPOPサーバからメールを受信して、
ローカルのPostfixにメールを転送することができる。
また、転送を受け付けたPostfixは、ローカルのバーチャル・メールボックスに保存できる。


○動作テストのために、次のオプション付きでコマンドを実行した。
<テストで使用するオプション>
全て小文字であることに注意! (大文字だと全く異なる意味になるものがある。)

-v work noisily (diagnostic output)
ログにプロトコルでのやり取りなど詳細が記録される。

-a [fetch]all  retrieve old and new messages
受信済みかどうかにかかわらず受信する。

-k keep        save new messages after retrieval
受信後に削除しない。

-f fetchmailrc specify alternate run control file
設定ファイルの指定を行う。

(設定ファイルよりも、コマンドで指定したオプションが優先されるようである。)
# fetchmail -v -a -k -f /etc/fetchmailrc
fetchmail: WARNING: Running as root is discouraged.
File /etc/fetchmailrc must have no more than -rwx------ (0700) permissions.

ところが、以上のようにエラーが発生した。

1行目は、root権限で動作させることは非推奨だという警告である。
2行目は、/etc/fetchmailrcには「- rwx --- --- (0700)」より高いパーミッションを持たせるべきでないというものである。

とくに2行目の指摘は、/etc/fetchmailrcファイルには、メールサーバ用認証情報が含まれるので、
他のシステムユーザに見られる状態にするべきではないというものだろう。


そこで、次の考えのもとファイルの設定を修正した。

あくまでも、今回は動作テストなので、fetchmailコマンドをroot権限で実行する。
/etc/fetchmailrcファイルのパーミッションは、所有者(rootユーザー)のみが「rw」できるようにする。

パーミッションの変更を行った。

# chmod 600 /etc/fetchmailrc



○fetchmailコマンドを実行してPOPサーバからメールを受信する。


コマンドでのオプションを省略して次のように実行し、メールを受信した。

(設定ファイルで指定した方法で、受信された。)
# fetchmail -f /etc/fetchmailrc
fetchmail: WARNING: Running as root is discouraged.
これは先ほどと同じ警告であるが、メールの受信はできた。



○受信コマンド実行後、バーチャルメール・ボックスのあるディレクトリを確認した。

転送先のPostfixバーチャルユーザーのメールボックスが確認できた。

# ls /home/VMAIL/
example.com
# ls /home/VMAIL/example.com
virtualuser0  virtualuser1  virtualuser2
# ls /home/VMAIL/example.com/virtualuser1
cur  new  tmp


以上で、Fetchmailの動作テストは完了である。




◆Fetchmailをシステムユーザーアカウントで動作させ、定期的にメールを受信する設定

上記で行った方法では、次のエラーが生じた。
fetchmail: WARNING: Running as root is discouraged.

このエラーを避けるため、fetchmailコマンドをroot以外のシステムアカウントで動作させる。


○システムユーザーfetchmailを作成した。

# useradd -r -m -s /sbin/nologin fetchmail

fetchmailという名称のユーザーをシステムアカウントとして作成し、
なおかつ、/home/fetchmail というホームディレクトリを作成している。
このユーザーでのログインはできない。
オプションの意味は次の通りである。
-r, --system                  create a system account
-m, --create-home             create the user's home directory

ホームディレクトリが作成された。
# ls /home -Al
drwxr-xr-x  2 fetchmail fetchmail  4096 Mar  5 18:09 fetchmail

システムユーザとしてfetchmailが作成された。
# grep fetchmail /etc/passwd
fetchmail:x:496:496::/home/fetchmail:/sbin/nologin

○fetchmailrcの所有者と権限を変更した。

作成したfetchmailシステムアカウントが、fetchmailrcを読み取れるように設定する。

# chown fetchmail:fetchmail /etc/fetchmailrc
# chmod 400 /etc/fetchmailrc


○定期的にfetchmailを動作させてメールサーバから受信するようにした。

ユーザーfetchmailでfetchmailコマンドが3分毎に動作するようにした。

# crontab -u fetchmail -e
*/3    *       *       *       *       /usr/bin/fetchmail --invisible -f /etc/fetchmailrc
このfetchmailのファイルパスは、whichコマンドで調べた。

オプション --invisible を付けることによって、メールヘッダーに次のようなfetchmailの情報を排除できる。
Received: from example.com [....]
by mail-test with POP3 (fetchmail-6.3.17)
for <testto@example.com> (single-drop); Fri, 04 Mar 2016 20:10:00 +0900 (JST)

ところで、上記のようにfetchmaiユーザーでfetchmailが実行される場合、デフォルトで、/home/fetchmail にpidファイルが作成される。
しかし、環境変数 FETCHMAILHOME に別のディレクトリを設定することで変更も可能である。

たとえば、fetchmailを実行するスクリプト内に、次のように定義すればいい。
export FETCHMAILHOME=/somewhere/fetchmail


○設定した間隔に応じてfetchmailが自動実行されたことを確認した。

# tail /var/log/cron
Mar  5 18:48:01 test CROND[30258]: (fetchmail) CMD (/usr/bin/fetchmail --invisible -f /etc/fetchmailrc)

# tail /var/log/maillog
Mar  5 18:48:01 test fetchmail[30258]: 1 message for testmailuser at example.com (993 octets).
Mar  5 18:48:02 test postfix/smtpd[30259]: connect from localhost[::1]
Mar  5 18:48:02 test postfix/smtpd[30259]: 307EA9xxxx: client=localhost[::1]
Mar  5 18:48:02 test postfix/cleanup[30263]: 307EA9xxxx: message-id=<56DAxxxx.309xxxx@example.com>
Mar  5 18:48:02 test postfix/qmgr[17730]: 307EA9xxxx: from=<fromtestuser@example.com>, size=1198, nrcpt=1 (queue active)
Mar  5 18:48:02 test fetchmail[30258]: reading message testmailuser@example.com:1 of 1 (993 octets) flushed
Mar  5 18:48:02 test postfix/smtpd[30259]: disconnect from localhost[::1]
Mar  5 18:48:02 test postfix/virtual[30265]: 307EA9xxxx: to=<testmailuser@example.com>, relay=virtual, delay=0.14, delays=0.09/0.05/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Mar  5 18:48:02 test postfix/qmgr[17730]: 307EA9xxxx: removed

このログを見ると、最初にfetchmailがメールの有無をメールサーバーに確認した後に、postfixに転送している様子が見える。


☆もし、Fetchmailで受信したメールについて、
ローカル配信担当のPostfixにおいて転送ループエラーが発生してバウンスされる場合には、
次のページで説明されるオプションをFetchmailに追加すると改善されるかもしれない。

http://akira-arets.blogspot.jp/2017/08/fetchmail-postfix-mailforwardingloop.html



以下は、挙動を調べるテストなどである。


◇bounceメールの送出テスト

『例えばどういう場合に、fetchmailはbounce mailを送出するか』

以下の設定例では、fetchmailが mail.server.address.example.com からtestuserアカウントでメールを受信して、
test.kkk@example.com へ届けるために、Postfixへ転送しようとするものである。

(例↓)
poll mail.server.address.example.com with proto POP3
  user 'testuser' there with password 'passw0rd' is test.kkk@example.com
test.kkk@example.comという宛先が存在しない場合には、Postfixは送達することができない
Postfixは、fetchmailにエラーを通知する

そして、fetchmailはこのエラーについてのbounce mailを指定された方法で発信するわけである。


以下は、fetchmailがメール送信者へ向けて通知するbounce mailの例である。
転送先のアドレスが存在しないことについて、SMTPエラー情報が記載されている。
This message was created automatically by mail delivery software.

 A message that you sent could not be delivered to one or more of its
 recipients. This is a permanent error.


 Reason: Some addresses were rejected by the MDA fetchmail forwards to.

 The following address(es) failed:
test.kkk@example.com
    SMTP error: 550 5.1.1 <test.kkk@example.com>: Recipient address rejected: User unknown in virtual mailbox table

Final-Recipient: rfc822; test.kkk@example.com
 Last-Attempt-Date: Wed, 13 Jan 2016 21:19:49 +0900 (JST)
 Action: failed
 Status: 5.1.1
 Diagnostic-Code: 550 5.1.1 <test.kkk@example.com>: Recipient address rejected: User unknown in virtual mailbox table


◇fetchmailを経由したメールに追加されたヘッダーについて

fetchmailを経由してtest@example.comへ転送されたメールのヘッダーを見ると、
次のように、Receivedヘッダーに、「for」から始まる転送先情報が見られた
Received: from pop.example.com [ipアドレス]
    by fetchmail.myserver with POP3 (fetchmail-6.3.17)
    for <test@example.com> (single-drop); Fri, 15 Jan 2016 01:51:20 +0900 (JST)
ここに記載されているsingle-dropとは、
メールボックスから受信したメールを別のメールボックスへ1対1の関係で転送されるものを言う。



<参考>

「Postfixについて」

・Postfix Virtual Domain Hosting Howto
< http://www.postfix.org/VIRTUAL_README.html > 2016年1月24日

・What Those SMTP Error Codes Mean and Why You Should Care
< http://www.gettingemaildelivered.com/what-those-smtp-error-codes-mean-and-why-you-should-care > 2016年1月28日

・Postfix のサイズ 制限
< http://www.math.kobe-u.ac.jp/~kodama/tips-postfix-size.html > 2016年3月8日


「Fetchmailについて」

・Fetchmail / configuration of the .fetchmailrc
< https://calomel.org/fetchmailrc.html > 2016年1月30日

・Using Fetchmail to Retrieve Email
< https://www.linode.com/docs/email/clients/using-fetchmail-to-retrieve-email > 2016年1月28日

・Retrieving Emails From Remote Servers With fetchmail (Debian Etch)
< https://www.howtoforge.com/debian_etch_fetchmail > 2016年1月27日

・fetchmail reference manual
< https://www.freebsd.org/cgi/man.cgi?query=fetchmail&manpath=SuSE+Linux/i386+11.3 >  2016年1月27日

・Part Five: Configuring SpamAssassin and Fetchmail
< https://technpol.wordpress.com/2013/03/09/part-five-configuring-spamassassin-and-fetchmail/ > 2016年1月28日

・fetchmailrc
< http://d.hatena.ne.jp/iww/20120807/fetchmailrc > 2016年1月13日

・Administering A Mail Server
< http://thegoldenear.org/toolbox/unices/mail-server.html > 2016年1月15日



「電子メール一般について」

・メールを受け取る仕組みはどうなっていますか??
< http://ascii.jp/elem/000/000/439/439105/ > 2016年1月15日

・表4 メールヘッダ一覧
< http://www.atmarkit.co.jp/fnetwork/rensai/netpro03/mail-header.html > 2016年1月15日

投げ銭

★当サイトへの投げ銭(PayPal)★

Ad

Ad