Category: Postfix

8月 30 2010

Postfixの.forwardでphp起動

Postfixで.forwardを使いメールの受信でPHPを起動させる。
ここではuser1でメール受信したら/var/www/htm/test.phpを起動させる
user1のhomeに.forwardを作る

# vi /home/user1/.forward

以下のを記載

|/usr/bin/php /var/www/html/test.php

でもいいのだが、実行者がpostfixなので面倒
そこで

|/home/user1/forward.sh

とでもしてshを実行させる
/home/user1/forward.shの内容は

#!/bin/sh
`cd /var/www/html/;   source test.sh`

とする
/var/www/html/にtest.shを作成

# /var/www/html/test.sh

test.shに

#!/bin/bash
`php test.php`

とする。これでブラウザでtest.phpへアクセスしたのと同じように実行できる。

phpファイルには

#!/usr/bin/php
<?php

?>

と頭に#!/usr/bin/phpをつけて書くのを忘れずに
/usr/bin/phpの部分は

# which php

で確認。

受信したメールを残しておきたい時は
mailboxの場合

\user1
|/usr/bin/php /var/www/html/test.php

Maildirの場合

~/Maildir/
|/usr/bin/php /var/www/html/test.php

mailboxとMaildirで違うので注意

8月 27 2010

Postfix aliases の設定

/etc/aliasesを編集

# vi /etc/aliases

/etc/aliases内にユーザーと転送先を指定

user: root
root: ***@***.***

などと指定

# newaliases←設定を反映
# echo test|mail rootテストメール送信

《参考サイト》
postfixのaliasでメールを転送してみよう。
CentOSインストール直後の初期設定を行う。

8月 26 2010

Posfixでワイルドカード

/etc/postfix/main.cfの設定

# vi /etc/postfix/main.cf

virtual_maps = hash:/etc/postfix/virtual

この記述を/etc/postfix/main.cfに追記。
どこでもいいが最後がわかりやすい。

続いて/etc/postfix/virtualの設定

# vi /etc/postfix/virtual

root 宛ての以外*@sub.sample.com 宛てのメールをuser1で受信

root@sub.sample.com   root
@sub.sample.com   user1

rootで次のコマンドを実行。postfixの再起動の必要はありません。 (main.cfの変更を行った場合は、# postfix reloadを行うようにしてください)

# /etc/init.d/postfix reload

テーブル作成

# postmap /etc/postfix/virtual

注意
main.cf”の“mydomain”に“virtual”ファイルで設定する“sub-domain”を指定しない
正常に動作しない場合は“/var/log/messages”や”/var/log/maillog”のログを確認

≪参考サイト≫
[Postfix] バーチャルドメイン・バーチャルホストの設定

8月 24 2010

Postfixインストールと設定

yumでインストール

# yum -y install postfix

/etc/postfix/main.cfの設定

cp -p /etc/postfix/main.cf /etc/postfix/main.cf.org
# vi /etc/postfix/main.cf

ドメイン関連の指定

myhostname = mail.aaa.com  # ホスト名指定

mydomain = aaa.com  # ドメイン指定

myorigin = $myhostname # @の右辺補完する設定

メールの受信を許可する範囲を設定
全て受信するのなら all

inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost

mydestination 他のサーバーへ配信せずこのホストで処理すべきドメインを設定

mydestination = $myhostname, localhost.$mydomain
#mydestination = $myhostname, localhost.$mydomain $mydomain
#mydestination = $myhostname, localhost.$mydomain, $mydomain,
#    mail.$mydomain, www.$mydomain, ftp.$mydomain

mynetworks_style
mynetworks
有効になっているネットワークからの送信要求に対してメールの中継を許可する設定 デフォルトsubnet
状況によって変更。 自ホスト以外からの送信がないのならhostで
mynetworks_styleよりmynetworksが優先される

#mynetworks_style = class
#mynetworks_style = subnet
#mynetworks_style = host
.
.
#mynetworks = 192.168.100.0/24, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table

//メールボックス形式をMaildirに

#home_mailbox = Mailbox
home_mailbox = Maildir/

//メールサーバーソフト名の隠蔽化

#smtpd_banner = $myhostname ESMTP $mail_name
#smtpd_banner = $myh:ostname ESMTP $mail_name ($mail_version)
#追記
smtpd_banner = $myhostname ESMTP unknown

問題報告
notify_classesの設定
デフォルト: notify_classes = resource, software
変更する場合は追記する
それぞれの値の意味は

bounce postmaster に配送できないメールのコピーを送ります
2bounce ダブルバウンスメッセージを postmaster に送ります
delay postmaster に遅延メールを通知します。この場合、postmaster はメッセージヘッダのみを受け取ります。
policy postmaster に (UCE) ポリシー制限によって拒否されたクライアントの要求を通知します。postmaster は SMTP セッション全体の写しを受け取ります。
protocol postmaster に (クライアントもしくはサーバサイドの) プロトコルエラーや、クライアントによる実装されていないコマンドの試行を通知します。postmaster は SMTP セッション全体の写しを受け取ります。
resource postmaster に (例えばキューファイルの書き込みエラーなど) リソース問題によってメールが配送できないことを通知します。
software postmaster にソフトウェア問題によってメールが配送できないことを通知します。
notify_classes = resource, software, policy

不正中継対策
設定する場合は追記

# vrfyコマンドを無効にして有効なアカウント流出を防ぐ
disable_vrfy_command = yes

#mynetworksの値のみ許可
smtpd_client_restrictions =
        permit_mynetworks,
        reject_unknown_client,
        permit

smtpd_recipient_restrictions =
       permit_mynetworks, # mynetworkの値を許可
       reject_non_fqdn_recipient, # 完全修飾ドメイン形式ではない場合に、要求を拒否
       permit_sasl_authenticated,
       reject_unauth_destination

SMTP-Auth設定

smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname

smtpd_recipient_limitの追記
一通のメールのコピー数。デフォルトは1000

smtpd_recipient_limit = 50

/etc/postfix/main.cfをチェック
エラーがあるとエラー部分が表示される

postfix check

Postfix起動

# service postfix start  /*Postfix起動*/
postfix を起動中:          [ OK ]

# chkconfig postfix on /*Postfix自動起動設定*/
# chkconfig --list postfix /*Postfix自動起動設定確認*/
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off

SMTP認証
cyrus-sasl,cyrus-sasl-plainをインストール

# yum -y install cyrus-sasl cyrus-sasl-plain

起動確認

# /etc/init.d/saslauthd status
saslauthd (pid 18465 18464 18463 18462 18461) を実行中...

起動してなれば起動する

# chkconfig saslauthd on /*自動起動の設定*/
# /etc/init.d/saslauthd start /*起動*/

認証を必要とする接続にsmtp(465)を使用する
/etc/postfix/master.cfの編集

# cp -p /etc/postfix/master.cf /etc/postfix/master.cf.org
# vi /etc/postfix/master.cf
smtps   inet  n    -      n    -    -    smtpd  /*コメント解除14行目*/
-o smitpd_tls_wrappermode=yes /*TLSの場合のみ解除*/
-o smtpd_sasl_auth_enable=yes /*コメント解除 SMTP認証の有効化16行目*/
-o smtpd_client_restrictions=permit_sasl_authenticated,reject /*コメント解除クライアントの接続制限17行目*/
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject /*追記 RCPT TOコマンド発行制限*/

/usr/lib/sasl2/smtpd.confの編集

# cp -p /usr/lib/sasl2/smtpd.conf /usr/lib/sasl2/smtpd.conf.org
# vi /usr/lib/sasl2/smtpd.conf
mech_list: plain login  /*追記*/

クライアントに提示する認証方式

//TLS通信の場合は
/etc/postfix/main.cf に以下の3行を追記

# TLS
smtpd_tls_cert_file = /usr/local/certs/server.crt
smtpd_tls_key_file = /usr/local/certs/server.key
smtpd_tls_loglevel = 1

//Postfix再起動

/etc/init.d/postfix restart

ポート25が空いてなかったらあける
nmapで確認

# nmap localhost

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2010-08-26 20:16 JST
Interesting ports on example.com (127.0.0.1):
Not shown: 1676 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
80/tcp  open  http

sendmail停止

# chkconfig sendmail off
# /etc/init.d/sendmail stop

送信サーバーを切り替える

# alternatives --config mta

  選択       コマンド
-----------------------------------------------
*+ 1         /usr/sbin/sendmail.sendmail
   2          /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:2 /*2を選ぶ*/

確認

# alternatives --config mta

  選択       コマンド
-----------------------------------------------
* 1           /usr/sbin/sendmail.sendmail
+ 2          /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:

postfixに+がついているのでOK

//ポートが開いていたら不正中継テスト

#  telnet relay-test.mail-abuse.org

途中で止まらずテスト19まで完了すればOK

《参考サイト》
Postfix の設定 – 基本
Postfix 設定パラメータ
Postfixについて
メールサーバー構築(Postfix+Dovecot)
「Postfix」の設定