2013年4月21日日曜日

Postfwdでmynetworksからの接続情報をログに出力

postfwdを使ってもう※ひとねた※書いてみます。

postfixの場合、mynetwoksからの接続は、基本的にpermit_mynetwoksで無条件にリレーを許可されます。
mynetwoksに記述されているネットワークあるいはIPアドレスが少ないなら、mynetwoksからの接続である事を判断出来ますが、その数が非常に多くなった時はログを見てもmynetworksからの接続だとすぐに判断がつかないケースが出てきます(扱うドメインが多ければさらに判断が難しくなります)。

前置きが長くなりなしたが、今回は通常ログに出力されないそれらの情報を一手間かけてログに出してみます。



postfwdのルールセット


itemのclient_addressに、postfixのmynetworksパラメータで指定している値を指定します。
※ネットワークアドレス、IPアドレスはご自身の環境で読みかえてください

ログに出力するだけなので、actionには全てdunnoを指定しています。



 例1:カンマ区切りで複数指定する場合
id=MYNETWORKS
  action=dunno permit_mynetworks was matched
  protocol_state==RCPT
  client_address=192.168.0.0/24, 192.168.1.0/.24, 10.0.x.x


例2: リストを含むテキストを指定する場合
id=MYNETWORKS
  action=dunno permit_mynetworks was matched
  protocol_state==RCPT
  client_address==file:/etc/postfix/mynetworks.txt
  client_address==file:/etc/postfix/mynetworks2.txt
  …
/etc/postfix/mynetworks.txt:
192.168.0.0/24
192.168.1.0/24
10.0.x.x

 例3: key=value形式のリストを含むテキストを指定する場合
      ※postfixで既に利用しているマップテーブルを再利用するような場合に便利です
id=MYNETWORKS
  action=dunno permit_mynetworks was matched
  protocol_state==RCPT
  client_address==table:/etc/postfix/mynetworks.txt
  client_address==table:/etc/postfix/mynetworks2.txt
  …
/etc/postfix/mynetworks.txt:
192.168.0.0/24    OK
192.168.1.0/24    OK
10.0.x.x           OK


postfix側での設定

smtpd_client_restrictionsでposfwdでの評価を行うように設定します。

smtpd_client_restrictions = check_policy_service inet:127.0.0.1:10045
smtpd_recipient_restrictions = permit_mynetworks
                               reject_unauth_destination
                          …

(注意)以下のように指定した場合、permit_mynetwoksでpostfixでの評価が完了してしまうので、その下のpostfwdによる評価は行われず、ログが出力されません。

smtpd_recipient_restrictions = permit_mynetworks
                               reject_unauth_destination
                               check_policy_service inet:127.0.0.1:10045




動作確認

これで以下のようにログが出力されるようになります。
※青字の部分が新たに出力されるようになった部分です


Apr 21 16:37:00 cent6 postfix/smtpd[2279]: connect from unknown[192.168.0.11]
Apr 21 16:37:00 cent6 postfwd2/policy[2148]: [RULES] rule=0, id=MYNETWORKS, client=unknown[192.168.0.11], sender=<user01@example.com>, recipient=<testuser@example.net>, helo=<mail.example.com>, proto=SMTP, state=RCPT, delay=0s, hits=MYNETWORKS, action=dunno permit_mynetworks was matched
Apr 21 16:37:00 cent6 postfix/smtpd[2279]: 8A5A321A6E: client=unknown[192.168.0.11]
Apr 21 16:37:01 cent6 postfix/cleanup[2285]: 8A5A321A6E: message-id=<F2CE3E63028A7Buser01@example.com>
Apr 21 16:37:01 cent6 postfix/qmgr[1801]: 8A5A321A6E: from=<user01@example.com>, size=487, nrcpt=1 (queue active)
Apr 21 16:37:01 cent6 postfix/smtpd[2279]: disconnect from unknown[192.168.0.11]
Apr 21 16:37:02 cent6 postfix/smtp[2287]: 8A5A321A6E: to=<testuser@example.net>, relay=mail.example.net[xx.xx.xx.xx]:25, delay=1.5, delays=1.4/0.02/0.04/0.12, dsn=2.0.0, status=sent (250 2.0.0 r3L7b27w006351 Message accepted for delivery)
Apr 21 16:37:02 cent6 postfix/qmgr[1801]: 8A5A321A6E: removed




2013年4月17日水曜日

Postfwd(postfix用ポリシーサーバ) その他メモ


前回までにまとめられなかった事を追記します。


ルールセットの記述の方法について(補足)

前回、ルールセットは下記のように";"(セミコロン)で区切って記述すると書きましたが、
id=SIZE001; protocol_state==END-OF-MESSAGE; action=DUNNO; size<=1000000
id=SIZE100; protocol_state==END-OF-MESSAGE; action=HOLD message too large

これを下記のように記述する事もできます。
id=SIZE001
action=DUNNO
protocol_state==END-OF-MESSAGE
size<=1000000
id=SIZE100
action=HOLD message too large
protocol_state==END-OF-MESSAGE 

好みの問題だとは思いますが、ぱっと見てわかりやすい記述をルールセット毎に採用すれば良いと思います。



postfwd2コマンドのオプションについて


良く利用するオプションをあげてみます。

●マニュアルを表示させる

# postfwd2 -m


※その際にpostfwd2コマンド内部で指定されているpagerを変更しておくと便利だと思います
 (マニュアルが長いので、lessの方が使い勝手が良いと思います)

(変更前)
our($cmd_pager)     = "more";

(変更後)
our($cmd_pager)  = "less";


●postfwd2デーモンの停止

# postfwd2 -k


●設定ファイルの読み直し

# postfwd2 --reload


●統計情報を見る

※ログにも同じ情報が定期的に出力されています

# postfwd2 --dumpstats

[STATS] postfwd2::cache 1.34: 8 queries since 0 days, 00:25:18 hours
[STATS] Requests: 0.0/min last, 0.3/min overall, 6.2/min top
[STATS] Hitrates: 40.0% requests, 0.0% dns, 0.0% rates
[STATS] Contents: request=2

[STATS] postfwd2::policy 1.34: 6 requests since 0 days, 00:25:18 hours
[STATS] Requests: 0.00/min last, 0.24/min overall, 6.15/min top
[STATS] Dnsstats: 0.00/min last, 0.00/min overall, 0.00/min top
[STATS] Hitrates: 50.0% ruleset, 33.3% parent, 16.7% child, 0.0% rates
[STATS] Timeouts: 0.0% (0 of 0 dns queries)
[STATS]   4 matches for id:  SIZE001
[STATS]   2 matches for id:  SIZE100


2013年4月13日土曜日

Postfwd(postfix用ポリシーサーバ) ルールセットの作成

前回の続きで、ルールセットを実際に作成してみます。

オンラインドキュメントのEXAMPLEに設定例がたくさん載っているので、そちらも参考にされてください。

書式

[ <item1>=<value>; <item2>=<value>; ... ] action=<result>

  • item=値と指定し、1つのルールセットに複数の条件を※1行で※指定する場合はセミコロンで区切ります
  • 必ず、actionを一つ含む必要があります
  • itemとactionの記述の順番は特に決まりがありません(どのような順番で指定しても大丈夫です)

itemについて

itemとしてしては、以下のものが指定できます。



actionについて

actionとしては、以下のものが指定できます。





ルールセットの作成例

ここでは、メッセージサイズをトリガーにメールをHOLDする例をあげてみます。

  /etc/postfix/postfwd.conf:
# 1. 10MB以下だったら何もしない
# 2. 10MBを超えたら、HOLDする
id=SIZE001; protocol_state==END-OF-MESSAGE; action=DUNNO; size<=10000000
id=SIZE100; protocol_state==END-OF-MESSAGE; action=HOLD message too large

 設定を反映するには、次のように実行します。
/usr/local/sbin/postfwd2 --reload

 連携するpostfixでは以下のように設定を追加します。

  /etc/postfix/main.cf:
  ※メッセージサイズのルールのなので、smtpd_end_of_data_restrictionsで指定します
smtpd_end_of_data_restrictions = check_policy_service inet:127.0.0.1:10045



動作確認

実際にメールを配送して動作を確認します。

<id=SIZE001のルールが適用された場合: holdされずに配送された場合>
青字の箇所が実際の判定部分で、actionとしてDUNNOが適用されて、その後、配送処理が行われています。

Apr  8 00:40:32 cent6 postfix/smtpd[1731]: connect from unknown[192.168.0.11]
Apr  8 00:40:33 cent6 postfix/smtpd[1731]: 095AF2267C: client=unknown[192.168.0.11]
Apr  8 00:40:34 cent6 postfwd2/policy[1719]: [RULES] rule=0, id=SIZE001, queue=095AF2267C, client=unknown[192.168.0.11], sender=<user01@example.com>, recipient=<testuser@example.jp>, helo=<example.com>, proto=SMTP, state=END-OF-MESSAGE, delay=0s, hits=SIZE001, action=DUNNO
Apr  8 00:40:34 cent6 postfix/cleanup[1737]: 095AF2267C: message-id=<ACCE33A63DDA9Ftoshiaki@example.com>
Apr  8 00:40:34 cent6 postfix/qmgr[1671]: 095AF2267C: from=<user01@example.com>, size=475, nrcpt=1 (queue active)
Apr  8 00:40:34 cent6 postfix/smtpd[1731]: disconnect from unknown[192.168.0.11]
Apr  8 00:40:36 cent6 postfix/smtp[1740]: 095AF2267C: to=<testuser@example.jp>, relay=mail.example.jp[1xx.33.69.11]:25, delay=3.5, delays=1.4/0.01/2/0.03, dsn=2.0.0, status=sent (250 ok:  Message 12768477 accepted)
Apr  8 00:40:36 cent6 postfix/qmgr[1671]: 095AF2267C: removed



<id=SIZE100のルールが適用された場合: holdされた場合>
青字の最初の1行目が判定部分で、actionとしてHOLDが適用され、次の2行目でpostfixがHOLDの処理を実行しています(メールの配送は行われていません)。

Apr  8 00:41:14 cent6 postfix/smtpd[1731]: connect from unknown[192.168.0.11]
Apr  8 00:41:14 cent6 postfix/smtpd[1731]: 1F7262267C: client=unknown[192.168.0.11]
Apr  8 00:41:14 cent6 postfix/cleanup[1737]: 1F7262267C: message-id=<ADCE33A6566291toshiaki@example.com>
Apr  8 00:41:15 cent6 postfwd2/policy[1719]: [RULES] rule=1, id=SIZE100, queue=1F7262267C, client=unknown[192.168.0.11], sender=<user01@example.com>, recipient=<testuser@example.jp>, helo=<example.com>, proto=SMTP, state=END-OF-MESSAGE, delay=0s, hits=SIZE100, action=HOLD message too large
Apr  8 00:41:15 cent6 postfix/smtpd[1731]: 1F7262267C: hold: END-OF-MESSAGE from unknown[192.168.0.11]: <END-OF-MESSAGE>: End-of-data message too large; from=<user01@example.com> to=<testuser@example.jp> proto=SMTP helo=<example.com>

Apr  8 00:41:15 cent6 postfix/smtpd[1731]: disconnect from unknown[192.168.0.11]