Procmail

提供: ArchWiki
2017年10月12日 (木) 23:15時点におけるKusakata.bot (トーク | 投稿記録)による版 (文字列「Tips and tricks」を「ヒントとテクニック」に置換)
ナビゲーションに移動 検索に移動

Procmail はメールをフィルタリング・ソート・保存するためのプログラムです。メールクライアントとメールサーバーの両方で使うことができます。使い方次第でスパムをフィルタリングしたり、ウィルスチェックを行ったり、自動返信メールを送信したりすることができます。

この記事では procmail の設定方法を説明します。procmail を使用するメールクライアント (mutt, nmh など) あるいはメールサーバー (sendmail, postfix など) は既に使える状態になっていることが前提です。また、正規表現に関する基本的な知識も必要です。あくまで最小限の説明だけしかここでは説明しません。詳しい解説は man procmailrc を見てください。

インストール

公式リポジトリから procmail パッケージをインストールしてください。

設定

メールクライアントの設定の場合は ~/.procmailrc に、メールサーバーが使用する場合は /etc/procmailrc に設定を保存します。

ノート: Be careful when testing your procmail configuration on sample messages you have received, as messages can be automatically bounced back to sender if there is an error in your configuration file. To avoid this potentially embarrassing situation, turn off bounces by passing -t to procmail, or setting the DELIVERED environment variable (man procmailrc).

設定は2つのセクションで構成されています: assignmentsrecipes です。

アサインメント

アサインメントセクションでは procmail の変数を定義します:

PATH=/bin:/usr/bin
MAILDIR=$HOME/Mail
DEFAULT=$HOME/Mail/inbox
LOGFILE=/dev/null
SHELL=/bin/sh

レシピ

レシピはメインのセクションであり、実際にアクションを実行するフィルターを定義します。レシピのフォーマットは以下のとおりです:

:0 [flags] [ : [locallockfile] ]
<zero or more conditions (one per line)>
<exactly one action line>

条件は正規表現で記述します。特殊なオプションが多少追加されています。条件には一番最初に必ずアスタリスクを書きます。

アクションに指定できるのはメールボックスの名前か外部プログラムだけです。

フラグとロックファイル

基礎的なレシピではフラグは必要ありません。ただし外部コマンドを呼び出す場合は必要になるかもしれません。以下は最もよく使われているフラグです:

  • f: パイプをフィルターとして扱います。
  • w: フィルターやプログラムが終了するのを待機して終了コードを確認します (通常は無視されます)。フィルターの終了コードが異常だった場合、テキストはフィルタリングされません。

:0 の後に : を使うことでロックファイルが使用できます。(ほぼ同時にメールが複数届いて) 同時に複数の procmail のインスタンスが動作した場合に問題が発生するようなときはロックファイルが必要です。

ロックファイルの名前は : の後ろに設定できます。

条件

条件は先頭にアスタリスクが付き、以下のように正規表現で記述します:

* ^From.* someperson@\w+.com

アクション

アクションに指定するのは以下のような文字列だけでも機能します:

work

上記の場合、条件を満たしたメールは work 受信箱に保存されます。

アクションの先頭文字はパイプにすることもでき、その場合はコマンドの標準入力にメッセージが渡されます。例:

| /usr/bin/vendor_perl/spamc 

デフォルトでは、レシピのアクションが実行されたら、処理は終了です。

f フラグを使用した場合、コマンドによってメッセージが改変されレシピの処理は続きます。例えば、spamassassin コマンドでメールにスパムのレベルを示すヘッダーが追加され、他のレシピがそのヘッダーを使用して、メールをブロックしたり別のメールボックスにメールを保存することができます。

ヒントとテクニック

Spamassassin

以下は spamassassin を使ってスパムをブロックする例です。あらかじめ spamassassin をインストールして機能する状態にする必要があります。

# By using the f and w flags and no condition, spamassassin is going add the X-Spam headers to every single mail, and then process other recipes.
# No lockfile is used.
:0fw
| /usr/bin/vendor_perl/spamc

# Messages with a 5 stars or higher spam level are going to be deleted right away
# And since we never touch any inbox, no lockfile is needed.
:0
* ^X-Spam-Level: \*\*\*\*\*
/dev/null

# If a mail with spam-status:yes was not deleted by previous line, it could be a false positive. So its going to be sent to an spam mailbox instead.
# Since we do not want the possibility of one procmail instance messing with another procmail instance, we use a lockfile
:0:
* ^X-Spam-Status: Yes
$HOME/mail/Spam

ClamAV

ClamAV アンチウイルスを使用する例:

# We make sure its going to use the sh shell. Mostly needed for mail-only account that have /sbin/nologin as shell
SHELL=/bin/sh

# We will scan the mail with clam using the standard input, and saving the result on the AV_REPORT variable
AV_REPORT=`clamdscan --stdout --no-summary - |sed 's/^stream: //'`

# We check if the word FOUND was in the result and save "Yes" or "No" according to that
VIRUS=`echo $AV_REPORT|sed '/FOUND/ { s/.*/Yes/; q  };  /FOUND/  !s/.*/No/'`

# formail is a filter that can alter a mail message, while keeping the correct format. We use it here to add/alter a header called 
# X-Virus with either value Yes or No
:0fw
| formail -i "X-Virus: $VIRUS"

# And if we just added "X-Virus: Yes", we will also add another header with the scan result, and alter the subject, again, with the scan result.
# Since we are using the f flag, the mail is going to be delivered anyway.
:0fw
* ^X-Virus: Yes
| formail -i "X-Virus-Report: $AV_REPORT" -i "Subject: [Virus] $AV_REPORT"

別のメールボックスにメールをフィルタリング

あなたの同僚がくだらないジョークや不真面目なメールは転送で送ってくるが、仕事に関するメールは直接送信してくるような場合、以下の設定で転送メール(つまりほとんどは仕事と無関係なメール)を別のメールボックスに保存できます:

:0:
* ^From.*coworker@domain.com
* ^Subject.*FW:
$HOME/mail/jokes

Postfix パイプ

postfix からパイプするには /etc/postfix/main.cf を開いて以下を追加してください:

mailbox_command = /usr/bin/procmail -a "$EXTENSION"

postfix.serviceリロードすると、メールは procmail に送信されるようになります。

Dovecot に送信

Dovecot にメールを転送するには以下のアサインメントを変更してください。

DELIVER が最初に試行され、その後に DEFAULT が使われます。

DELIVER="/usr/lib/dovecot/deliver -d $LOGNAME"
DEFAULT="$HOME/Maildir/"
MAILDIR="$HOME/Maildir/"

上記の設定で Dovecot にはいつでも最新のデータベースが作られるようになります。

それからスパムメールを弾くには:

# deliver spam to spam folder
:0 w
* ^X-Spam-Status: Yes
| $DELIVER -m Spam

# deliver to INBOX and stop
:0 w
| $DELIVER

詳しくは http://wiki2.dovecot.org/procmail を見てください。

参照