在Debian 4.0 rc3上安裝postfix+sasl 驗證及防垃圾SPAM機制

作者:何祖彬[RobinHe] Mail:[email protected]
始於2008年8月4日 PM

Blog: http://blog.csdn.net/zubin006/

WebSite : http://www.wearelinuxer.com/

版本號:Debian-Postfix-SASL-SPAM-

V1.0-20080804, 2008年8月4日首版

轉載請註明出處,本文也是採用兩篇網友的大部分內容,因此轉載時,請也將下面“參考文章”的出處也一併加入!謝謝!
本文中難免有不足、遺漏、錯別字和句子不通之處,如發現、或者有更新改變之處,請與我聯繫以及時修正和完善!

系統信息:
OS : Debian 4.0 rc3

使用說明:
綠色加粗字體的絕大部分是輸入的命令和系統輸出顯示的結果。

參考文章:
POSTFIX+SASL:
http://www.debian.org.tw/index.php/Postfix-SASL

http://community.itbbs.cn/showthread.php?t=7395



讓 Postfix 支持 Cyrus-SASL© V2 認證


第一步,安裝所需套件並進行相關的配置:

#apt-get install openssh-blacklist postfix-tls sasl2-bin libsasl2-modules spamassassin popa3d

修改/etc/default/saslauthd文件,讓START=yes,即,啓動SASL認證:
#vi /etc/default/saslauthd

# This needs to be uncommented before saslauthd will be run automatically
START=yes

修改 /etc/postfix/sasl/smtpd.conf,設定 PostFix 使用 Saslauthd
#mkdir -p /etc/postfix/sasl
#echo "pwcheck_method: saslauthd" > /etc/postfix/sasl/smtpd.conf
再編輯這個文件:/etc/postfix/sasl加入下面這一句:
mech_list:plain login 將postfix 加入 sasl 羣組中
#addgroup postfix sasl

修改 /etc/postfix/main.cf ,使其使用 SMTP AUTH 及 SASL Authenticate 以及 加入spam防垃圾郵件機制

#vi /etc/postfix/main.cf
# Enable SASL Auth

# 設定 Postfix 使用 SASL 認証。
smtpd_sasl_auth_enable = yes

# 設定 SASL 支援非標準 E-mail Client 的認証動作。
broken_sasl_auth_clients = yes

# 不使用 ANONYMOUS 這個認証。
smtpd_sasl_security_options = noanonymous

# 設定 SASL 的認証方法
# permit_sasl_authenticated → 如果用戶端可通過 SASL 認証則可 Relay Mail。
# permit_mynetworks → 如果用戶端的位址為 $mynetworks 則可 Relay Mail。
# check_relay_domains
# reject_unauth_destination
smtpd_client_restrictions =

smtpd_helo_required = yes
smtpd_helo_restrictions =

smtpd_sender_restrictions =

smtpd_recipient_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination,
    reject_invalid_hostname,
    check_sender_access  hash:/etc/postfix/sender_access,
    reject_unauth_pipelining,
    reject_non_fqdn_sender,
    reject_unknown_sender_domain,
    reject_unverified_sender,
    reject_multi_recipient_bounce,
    reject_non_fqdn_recipient,
    reject_unknown_recipient_domain,
    reject_rbl_client      cbl.abuseat.org ,
    reject_rbl_client      dul.dnsbl.sorbs.net ,
    reject_rbl_client      zen.spamhaus.org ,
    reject_rbl_client      list.dsbl.org ,
    reject_rbl_client      opm.blitzed.org ,
    reject_rhsbl_sender    dsn.rfc-ignorant.org ,
    permit

保存退出。

同時重啓SASL:
#/etc/init.d/saslauthd restart

驗證SASL是否正常工作:
#testsaslauthd -u username -p password

重啓postfix:
#/etc/init.d/postfix restart

第二步,修正調整Debian中Postfix+SASL的一些配置[謝謝HENRY的幫忙解決以下的問題,以下是我的總結]:

因爲Debian的postfix套件預設用chroot安裝在/var/spool/postfix。因此, /etc/sasldb2要搬到/var/spool/postfix/etc/sasldb2,否則在非本地IP用SASL驗證時 會出現以下錯誤:
Jun 24 11:12:36 www last message repeated 2 times
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication failure: no secret in database
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: unknown[222.222.222.222 : SASL NTLM authentication failed: authentication failure
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication problem: unable to open Berkeley db /etc/sasldb2: No such file or directory
Jun 24 11:12:36 www last message repeated 5 times
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication failure: Password verification failed
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: unknown[222.222.222.222]: SASL PLAIN authentication failed: authentication failure
因爲它找不到sasldb2這件相對於/var/spool/postfix下的/etc/sasldb2文件,log中的/etc/sasldb2 其實 就是/var/spool/postfix/etc/sasldb2,現在這個目錄下找不到這個文件,因此會提示:No such file or directory

因此:
#cp /etc/sasldb2 /var/spool/postfix/etc/.
同時在/etc/group中加入相應的組:
mail:x:8:postfix
sasl:x:45:postfix

但是在Debian/Ubuntu環境下,因爲chroot的關係,所以還要將saslauthd的PIDFILE目錄修改爲 /var/spool/postfix/var/run/saslauthd,所以請先建立這個目錄:
#mkdir /var/spool/postfix/var/run/saslauthd

然後修改/etc/default/saslauthd文件:
START=yes
MECHANISMS="pam"
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd"

重啓saslauthd:

#/etc/init.d/saslauthd restart

確認一下saslauthd.pid不在/var/run下,而是在新的目錄 /var/spool/postfix/var/run/saslauthd下面,

然後在利用遠程電腦發一送需要SMTP驗證的MAIL,看一下是事正常,同時檢查/var/log/mail.log


完成...

接下去做SSL收發MAIL的配置,期待中...









-------------------------------------------------------------------------------------
參考資料:
http://www.debian.org.tw/index.php/Postfix-SASL

Postfix-SASL

出自DebianWiki

目錄

[隱藏 ]

[編輯 ] 所需套件

postfix-tls sasl2-bin libsasl2-modules

[編輯 ] Mail Relay 簡介

所謂的 Mail Relay,指的就是 Mail Server 會不會替使用者將信轉寄到外部去。

比如說,我將信寄給 [email protected] , 如果 [email protected] 這個信箱並不是位於本機上,那麼這封信將會轉給PCHome 的 Mail Server,而這個將郵件轉寄的動作就是 Mail Relay。

Open Relay 就是不設限地完全開放 Mail Relay 的功能,它會替所有的人進行郵件轉寄。

架設 Mail Server 的第一件事,就是防止不速之客利用您的 Mail Server 來亂發郵件,也就是關掉 Open Relay 這個功能。(基本上,幾乎所有的 Mail Server 預設上都已把 Open Relay 給關掉了) 取而代之的,是使用 SMTP 認証 (SMTP AUTH) 來驗証使用者可否進行 Mail Relay。

在這裡所要介紹的,是如何使用 Cyrus-SASL© V2 來實作 SMTP 認証的功能。 其中的 SASL (Simple Authentication and Security Layer) 就是用來提供符合 RFC 2222 標準的各種認証機制的 Daemon。

[編輯 ] 讓 Postfix 支援 Cyrus-SASL© V2 認証:(Debian Sarge)

  • 修改 /etc/default/saslauthd,設定 START=yes
# This needs to be uncommented before saslauthd will be run automatically
START=yes

# You must specify the authentication mechanisms you wish to use.
# This defaults to "pam" for PAM support, but may also include
# "shadow" or "sasldb", like this:
# MECHANISMS="pam shadow"

MECHANISMS="pam"
  • 修改 /etc/postfix/sasl/smtpd.conf,設定 PostFix 使用 Saslauthd
mkdir -p /etc/postfix/sasl
echo "pwcheck_method: saslauthd" > /etc/postfix/sasl/smtpd.conf
  • 將 postfix 加入 sasl 羣組中
addgroup postfix sasl
  • 修改 /etc/postfix/main.cf , 使其使用 SMTP AUTH 及 SASL Authenticate
(上略)

# Enable SASL Auth

# 設定 Postfix 使用 SASL 認証。
smtpd_sasl_auth_enable = yes

# 設定 SASL 支援非標準 E-mail Client 的認証動作。
broken_sasl_auth_clients = yes

# 不使用 ANONYMOUS 這個認証。
smtpd_sasl_security_options = noanonymous

# 設定 SASL 的認証方法
# permit_sasl_authenticated → 如果用戶端可通過 SASL 認証則可 Relay Mail。
# permit_mynetworks → 如果用戶端的位址為 $mynetworks 則可 Relay Mail。
# check_relay_domains
# reject_unauth_destination
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, check_relay_domains, reject_unauth_destination

smtpd_client_restrictions = permit_sasl_authenticated
  • 修改 /etc/postfix/master.cf , 讓 Postfix 不要以 chroot 啟動。
    後面的 -v 參數可以用來 Debug,但正式上線時最好拿掉,以免系統被 Log 塞爆了。
(上略)

# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n - n - - smtpd -v

(下略)

或者是新增一個 listen 在 smtps (port 465) 上的 service

# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtps inet n - n - - smtpd
# 強迫 tls 連線 (?)
-o smtpd_tls_wrappermode=yes
# 使用 sasl auth (如果在 main.cf 已經設定了,這行可以省略)
-o smtpd_sasl_auth_enable=yes
# 所有連線到這裡的都必須通過 sasl auth
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
  • 重新啟動 SASL Daemon
/etc/init.d/saslauthd restart

跑起來後,可以用

testsaslauthd -u username -p password

來測試 saslauthd 是否有正常運作

  • 重新啟動 Postfix
/etc/init.d/postfix restart

[編輯 ] 檢測方式

首先把帳號及密碼轉換成 base64 的編碼:(可能需要安裝 libmime-base64-perl 套件)

perl -MMIME::Base64 -e 'print encode_base64("/0username/0password");'

登入 Postfix:

$ telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 Virtual ESMTP Postfix (Debian/GNU)
EHLO qemu (這裡要輸入的是 HostName)
250-Virtual
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-XVERP
250 8BITMIME
auth plain AHXXXXXXXXXXXXXXXXXXXX8/ (剛才 perl 指令計算的結果)
235 Authentication successful
quit
221 Bye
Connection closed by foreign host.

如果認証成功即表示 SASL 已經正確啟動了。











postfix無法使用sasl認證

warning: SASL authentication problem: unable to open Berkeley db /etc/sasldb2: No such file or directory

原因: 這是因為Debian的postfix套件是用 chroot,因此錯誤訊息中所說的/etc/sasldb2是指/var/spool/postfix/etc/sasldb2。
解決方法: cp /etc/sasldb2 /var/spool/postfix/etc/sasldb2。還要chmod o+r /var/spool/postfix/etc/sasldb2,否則postfix沒有讀的權限。

warning: SASL authentication failure: no secret in database

原因: 這是因為/var/spool/postfix/etc/sasldb2裏面沒有你用的帳號密 碼。
解決方法: 需要安裝sasl2-bin套件,用saslpasswd2 -c -f /var/spool/postfix/etc/sasldb2 新增帳號密碼。然後用sasldblistusers2 -f /var/spool/postfix/etc/sasldb2檢視有那些帳號。


祖彬:

帳號不知為何/var/spool/postfix/etc/sasldb2裏面的帳號會變成 [email protected]
只要你用這個帳號,密碼用zubin就一定能通過認證

新增帳號:
saslpasswd2 -c -f /var/spool/postfix/etc/sasldb2 -u 指定域名 帳號
不指定域名就是www.huiying-group.com , 晚上我再看看在那裏設定

檢視有那些帳號:
sasldblistusers2 -f /var/spool/postfix/etc/sasldb2

--
Henry


在Debian上安裝 Postfix+SASL的問題解決

上 篇 提到Debian的postfix套件預設用chroot安裝在/var/spool/postfix。因此, /etc/sasldb2要搬到/var/spool/postfix/etc/sasldb2。但是忘了提權限 問 題,一定要將postfix這個用戶加入sasl羣組。我在 /etc/group中修改了mail和sasl兩個羣組:

mail:x:8:dovecot,postfix
sasl:x:45:postfix

要讓員工在公司網路外使用SMTP服務器寄信,需要在 /etc/postfix/main.cf 中增加設定:

smtpd_sasl_auth_enable = yes //開啟sasl認證功能
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated, //如果不在公司網路內,能通過sasl認證者也行

然後在/etc/postfix/sasl目錄內增加一個新檔: smtpd.conf

pwcheck_method: saslauthd
mech_list:plain login

這是告訴postfix我們使用saslauthd來負責檢驗密碼。saslauthd在sasl2-bin套件中,必須安裝(要 安裝後纔有 /etc/sasldb2):

sudo apt-get install sasl2-bin

但是在Debian/Ubuntu環境下,因為chroot的關係,所以還要麻煩將saslauthd的PIDFILE目錄修 改為/var /spool/postfix/var/run/saslauthd。所以請先建立這個目錄,然後修改/etc/default/saslauthd:

START=yes //開機自動執行
MECHANISMS="pam” //使用pam就不需要sasldb
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd”

啟動saslauthd:

sudo /etc/init.d/saslauthd start

確認一下saslauthd.pid不在/var/run,而是在/var/spool/postfix/var/run/saslauthd/ 目 錄。否則可能是因為原saslauthd沒有停就修改/etc/default/saslauthd,這樣會造成saslauthd restart時因為找不到/var/spool/postfix/var/run/saslauthd/saslauthd.pid 而無法重啟。

tail -f /var/log/mail log,在公司網路外寄一封郵件看看,應該是沒有問題了。

 

聊天記錄:
[2008-6-24 10:49:54] CHT IT Henry 說: 我看一下再說
[2008-6-24 10:50:49] CHT IT ZubinHe 說: OK,TKS
[2008-6-24 12:03:18] CHT IT Henry 說: 我移除了postfix openssh-client openssh-server
[2008-6-24 12:03:35] CHT IT Henry 說: 現在正在重裝
[2008-6-24 12:04:03] CHT IT ZubinHe 說: OK,先喫飯吧!
[2008-6-24 12:04:09] CHT IT Henry 說: openssh-blacklist openssh-client openssh-server
[2008-6-24 12:04:31] CHT IT ZubinHe 說: 跟ssh也有關係?
[2008-6-24 12:04:37] CHT IT Henry 說: 你去喫吧
[2008-6-24 12:04:49] CHT IT Henry 說: 現在正在裝postfix
[2008-6-24 12:05:04] CHT IT ZubinHe 說: 我在電腦前邊喫,邊看
[2008-6-24 12:05:09] CHT IT ZubinHe 說: :)
[2008-6-24 12:05:39] CHT IT Henry 說: domain name是huiying.com
[2008-6-24 12:05:45] CHT IT ZubinHe 說: no
[2008-6-24 12:06:22] CHT IT ZubinHe 說: huiying-group.com
[2008-6-24 12:06:29] CHT IT Henry 說: ok
[2008-6-24 12:07:08] CHT IT Henry 說: 裝好了
[2008-6-24 12:12:57] CHT IT Henry 說: 看看Ubuntu 8.04上裝的是什麼pop3套件
[2008-6-24 12:13:13] CHT IT ZubinHe 說: 我現在在WINDOWS上面
[2008-6-24 12:13:27] CHT IT ZubinHe 說: 應該沒有預裝吧!
[2008-6-24 12:13:40] CHT IT ZubinHe 說: 要通過APT-GET裝的吧
[2008-6-24 12:13:59] CHT IT Henry 說: dovecot
[2008-6-24 12:14:21] CHT IT ZubinHe 說: 沒見過,有空裝一下試試
[2008-6-24 12:15:03] CHT IT ZubinHe 說: 對了,現在HY主機上的POSTFIX可以進行SASL認證發MAIL了?
[2008-6-24 12:16:01] CHT IT Henry 說: 移除了
[2008-6-24 12:16:07] CHT IT Henry 說: popa3d
[2008-6-24 12:16:17] CHT IT Henry 說: 開始安裝dovedot
[2008-6-24 12:16:25] CHT IT ZubinHe 說: OK
[2008-6-24 12:17:33] CHT IT Henry 說: 沒有dovecot只有dovecot-pop3d和dovecot-imapd
[2008-6-24 12:19:31] CHT IT Henry 說: 裝好了 dovecot
[2008-6-24 12:21:44] CHT IT Henry 說: 安裝spamassassin
[2008-6-24 12:24:34] CHT IT Henry 說: 啟動了spamd
[2008-6-24 12:25:27] CHT IT Henry 說: 目前沒遇上問題,你試試
[2008-6-24 12:25:39] CHT IT ZubinHe 說: OK
[2008-6-24 12:31:36] CHT IT ZubinHe 說: Jun 24 08:23:01 www spamd[14628]: prefork: child states: II
Jun 24 08:27:16 www postfix/smtpd[14636]: connect from unknown[59.58.97.2]
Jun 24 08:27:16 www postfix/smtpd[14636]: NOQUEUE: reject: RCPT from unknown[59.58.97.2]: 554 5.7.1 <[email protected]> : Relay access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<[192.168.0.172]>
Jun 24 08:27:19 www postfix/smtpd[14636]: disconnect from unknown[59.58.97.2]
Jun 24 08:29:47 www postfix/smtpd[14642]: connect from unknown[59.58.97.2]
Jun 24 08:29:47 www postfix/smtpd[14642]: NOQUEUE: reject: RCPT from unknown[59.58.97.2]: 554 5.7.1 <[email protected]> : Relay access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<[192.168.0.172]>
Jun 24 08:29:50 www spamd[14628]: spamd: server killed by SIGTERM, shutting down
[2008-6-24 12:31:44] CHT IT ZubinHe 說: 沒有提示我輸入密碼?
[2008-6-24 12:33:31] CHT IT ZubinHe 說: 你飯還沒喫吧,先喫吧,我也要出去去拿修理的機箱了,回頭再弄
[2008-6-24 12:34:12] CHT IT Henry 說: 必須將huiying-group.3322.org 列 入mydestination,現在應該可以了
[2008-6-24 12:35:23] CHT IT Henry 說: 我再看一下
[2008-6-24 12:36:18] CHT IT ZubinHe 說: 應該跟那個沒有關係
[2008-6-24 12:36:31] CHT IT ZubinHe 說: OK,我先出去先...
[2008-6-24 12:43:14] CHT IT Henry 說: 原來是你要用這主機來轉信,ok. 加上 mynetworks就可以了
[2008-6-24 12:49:14] CHT IT Henry 說: 加上一些smtpd_recipient_restrictions
[2008-6-24 12:51:02] CHT IT Henry 說: 喫飯囉
[2008-6-24 14:41:46] CHT IT ZubinHe 說: 我的目的是要讓用戶通過輸入用戶名和密碼認證後才能用這臺機器的SMTP發信
[2008-6-24 14:41:56] CHT IT ZubinHe 說: 加了mynetworks就不要認證了
[2008-6-24 14:48:32] CHT IT Henry 說: smtpd_recipient_restrictions =
        permit_mynetworks,
        permit_sasl_authenticated,
的用意就是: 若不是在mynetwork,但是通過sasl認證也行
[2008-6-24 14:54:04] CHT IT ZubinHe 說: 我看一下
[2008-6-24 14:54:59] CHT IT ZubinHe 說: 有空的時候,把安裝過程簡單的寫一下,謝謝!
[2008-6-24 14:56:09] CHT IT Henry 說: 安裝不就是剛才apt-get install那些
[2008-6-24 14:56:43] CHT IT Henry 說: 你給我的感覺好像是第一次安裝 mail server
[2008-6-24 14:58:30] CHT IT ZubinHe 說: 沒有,我只是奇怪,我先前那樣都正常!
[2008-6-24 15:00:11] CHT IT ZubinHe 說: 剛纔我把莆田的 59.58.97.2去掉,然後重啓postfix,還是不能通過認證發的
[2008-6-24 15:00:57] CHT IT ZubinHe 說: 你把你當前的IP去除後是否可以通過認證發MAIL?
[2008-6-24 15:04:19] CHT IT Henry 說: 應該是這樣
[2008-6-24 15:05:34] CHT IT ZubinHe 說: 你試一下
[2008-6-24 15:05:52] CHT IT ZubinHe 說: 連跳出讓我輸用戶和密碼的對話框都沒有
[2008-6-24 15:07:15] CHT IT Henry 說: 你認為應該是那裏有問題呢?
[2008-6-24 15:10:21] CHT IT ZubinHe 說: postfix沒有調用sasl認證
[2008-6-24 15:10:24] CHT IT Henry 說: 對
[2008-6-24 15:10:28] CHT IT Henry 說: smtpd_sasl_auth_enable = yes
[2008-6-24 15:11:17] CHT IT Henry 說: 加三行上去:
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
[2008-6-24 15:11:19] CHT IT ZubinHe 說: 我來一步一步檢查一下,看還少了哪些
[2008-6-24 15:14:15] CHT IT ZubinHe 說: Jun 24 11:12:36 www last message repeated 2 times
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication failure: no secret in database
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: unknown[59.58.97.2]: SASL NTLM authentication failed: authentication failure
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication problem: unable to open Berkeley db /etc/sasldb2: No such file or directory
Jun 24 11:12:36 www last message repeated 5 times
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: SASL authentication failure: Password verification failed
Jun 24 11:12:36 www postfix/smtpd[15023]: warning: unknown[59.58.97.2]: SASL PLAIN authentication failed: authentication failure
Jun 24 11:12:37 www postfix/smtpd[15023]: warning: SASL authentication problem: unable to open Berkeley db /etc/sasldb2: No such file or directory
Jun 24 11:12:37 www last message repeated 5 times
Jun 24 11:12:37 www postfix/smtpd[15023]: warning: unknown[59.58.97.2]: SASL LOGIN authentication failed: authentication failure
[2008-6-24 15:14:50] CHT IT ZubinHe 說: 還是跟我上次一樣的問題,SASL會調用/etc/sasldb2就出錯沒找到這個文件!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章