Postfix郵箱(四):安裝Cyrus-SASL使Postfix支持SMTP認證

說明:Postfix的SMTP認證需要透過Cyrus-SASL,連接到authdaemon獲取認證信息;

      本節後面將講解Postfix自帶的限制、過濾功能。


一、安裝配置Cyrus-SASL

1、系統已默認安裝Cyrus-SASL:

[root@mail ~]# rpm -aq|grep cyrus-sasl
cyrus-sasl-gssapi-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-plain-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-lib-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-md5-2.1.23-13.el6_3.1.x86_64


2、配置cyrus-sasl

[root@mail ~]# vi /etc/sasl2/smtpd.conf
#pwcheck_method: saslauthd
#mech_list: plain login
pwcheck_method: authdaemond 
log_level: 3 
mech_list: PLAIN LOGIN 
authdaemond_path:/usr/local/var/spool/authdaemon/socket
[root@mail ~]# chmod 755 /usr/local/var/spool/authdaemon

說明:有的系統authdaemond路徑爲/var/spool/authdaemon/socket。


3、配置postfix

[root@mail ~]# vi /etc/postfix/main.cf
# SMTP 收件方限制
smtpd_recipient_restrictions =
        permit_mynetworks,       
        permit_sasl_authenticated,       
        reject_non_fqdn_hostname,
        reject_non_fqdn_sender,
        reject_non_fqdn_recipient,
        reject_unauth_destination,
        reject_unauth_pipelining,
        reject_invalid_hostname       
# SMTP 發件方限制      
smtpd_sender_restrictions =
        permit_mynetworks,        
        reject_sender_login_mismatch,       
        reject_authenticated_sender_login_mismatch,
        reject_unauthenticated_sender_login_mismatch
# SMTP 用戶登陸限制
smtpd_sender_login_maps =
        mysql:/etc/postfix/mysql_virtual_sender_maps.cf,
        mysql:/etc/postfix/mysql_virtual_alias_maps.cf
# SMTP 認證配置
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
[root@mail ~]# service postfix restart

說明:以上限制方式是postfix自帶的一種訪問限制方式,下文會詳細說明。


4、測試SMTP認證

    使用MIME::Base64將郵箱賬號和密碼進行Base64編碼:

[root@mail ~]# perl -e 'use MIME::Base64; print encode_base64("postmaster\@yourmail.com")'
cG9zdG1hc3RlckB5b3VybWFpbC5jb20=
[root@mail ~]# perl -e 'use MIME::Base64; print encode_base64("extmail")'
ZXh0bWFpbA==

    本地測試需要安裝telnet工具:

[root@mail ~]# yum install -y telnet
[root@mail ~]# telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.eplantstore.com ESMTP Postfix - by eplantstore.com
ehlo localhost                        #輸入hello內容
250-mail.eplantstore.com
250-PIPELINING
250-SIZE 10485760
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN                  #顯示認證登陸表示Postfix成功
250-AUTH=PLAIN LOGIN                  #調用Cyrus-SASL進行SMTP認證
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login                            #輸入認證登陸命令
334 VXNlcm5hbWU6
cG9zdG1hc3RlckB5b3VybWFpbC5jb20=      #輸入賬號的Base64編碼
334 UGFzc3dvcmQ6
ZXh0bWFpbA==                          #輸入密碼的Base64編碼
235 2.7.0 Authentication successful   #顯示認證成功
quit
221 2.0.0 Bye
Connection closed by foreign host.


結論:以上信息表示postfix成功使用並通過SMTP認證(cyrus-sasl)




二、Postfix本身的郵件過濾

1、訪問表

(1)訪問表類型

  •     check_client_access maptype:mapname

      檢查主機名和從屬網域

      192.168.1.100

      10.188

  •     check_helo_access maptype:mapname

      檢查HELO命令中顯示的主機名

  •     check_recipient_access maptype:mapname

      檢查收件地址

      [email protected]

      example.com

      user@

  •     check_sender_access maptype:mapname

      檢查發件地址

      [email protected]

      example.com

      user@

(2)處理動作

  •     OK

      通過

  •     REJECT

      拒絕

  •     REJECT message-text

      messsage-text用來設置一條消息;

      消息會連同拒收郵件一起發給客戶端,並記錄在log中(下同)

  •     DUNNO

      暫停檢查

  •     HOLD message-text

      保留在隊列中

  •     DISCARD message-text

      postfix丟棄郵件,但讓客戶端誤以爲發送成功

  •     4.xx message-text

      返回指定的拒絕碼與信息給客戶端,暫時拒收

  •     5.xx message-text

      返回指定的拒絕碼與信息給客戶端,徹底拒收

(3)使用舉例

    設置訪問表限制:

[root@mail ~]# vi /etc/postfix/main.cf
smtpd_client_restrictions = check_client_access hash:/etc/postfix/client_access
smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender_access
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient_access

    創建訪問表

[root@mail ~]# vi /etc/postfix/client_access
10.188  REJECT 
192.168.1.100 REJECT 
[root@mail ~]# vi /etc/postfix/sender_access
[email protected] 
marketing@ 
abc.example.com

    轉化爲數據庫格式 (只要修改了訪問表都要進行轉化)

[root@mail ~]# postmap /etc/postfix/client_access
[root@mail ~]# postmap /etc/postfix/sender_access


2、內置限制條件

  •   permit_auth_destination

    放行mydestination\inet_interfaces\virtual_alias_maps\virtual_mailbox_maps\relay_domain

    設置的範圍,不符合時返回DUNNO

  •   permit_mynetworks

    放行客戶端IP位於mynetworks設置的範圍

  •   reject_unauth_destination

    和第一條相反,拒絕以上所設範圍

  •   reject_invalid_hostname

    HELO命令中的主機名稱無效時返回501

  •   reject_non_fqdn_hostname

    HELO命令中的主機名稱不是FQDN形式則返回504

  •   reject_non_fqdn_recipient

    收件地址不是FQDN則返回504

  •   reject_non_fqdn_sender

    發件地址不是FQDN則返回504

  •   reject_unauth_pipelining

    拒絕不守規定的流水線操作

  •   reject_unknown_client

    DNS查不出客戶端IP的PTR記錄時拒絕

  •   reject_unknown_hostname

    HELO命令中的主機名稱沒有A和MX記錄時拒絕

  •   reject_unknown_recipient_domain

    收件人地址的網域部分查不出有效的A或MX記錄時拒絕

  •   reject_unknown_sender_domain

    發件人地址的網域部分查不出有效的A或MX記錄時拒絕


3、RBL實時黑名單

  •   reject_rbl_client rblprovider.domain

    客戶端IP地址黑名單庫

  •   reject_rhsbl_client rblprovider.domain

    客戶端主機名稱黑名單庫

  •   reject_rhsbl_sender rblprovider.domain

    發件人黑名單庫


4、內容過濾

(1)類型

  •     header_checks = /etc/postfix/header_checks

        檢查標題

  •     body_checks = /etc/postfix/body_checks

        檢查正文

  •     nested_header_checks

        檢查附件標題

  •     mime_header checks

        檢查標題的MIME字段

(2)動作

  •     REJECT message-text

      拒收

  •     WARN message-text

      不拒絕,記錄message到log,可用於測試

  •     IGNORE

      刪除符合模式的內容

  •     HOLD message-text

      保留到隊列中

  •     DISCARD message-text

      偷偷丟棄郵件

(3)樣例

    運用header_checks進行簡單的病毒過濾

/name ?="?.*\.(bat|scr|com|dll|exe|hta|pif|vbs)"?/  REJECT

    運用body_checks進行內容過濾

/increase your sales by /   REJECT 
/in compliance (with|of) strict/  REJECT 
/lowest rates.*!/  REJECT
/[:alpha:]<!--.*-->[:alpha:]/ REJECT

        第三個模式挑出任何含有lowest rates字樣且其後跟着任何文字和!的字符串

        最後一個模式檢查是否有HTML註釋嵌在字句中間

        最好是使用外部內容過濾器spamassassin或DSPAM

wKiom1SPicPDPNe8AAINEKfDH4M829.jpg

   上圖是SMTP對話過程以及各階段對應的限制條件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章