Linux下開源郵件系統Postfix+Extmail+Extman環境部署記錄


 

一、基礎知識梳理
MUA (Mail User Agent) 
MUA 既是"郵件使用者代理人",因為除非你可以直接利用類似 telnet 之類的軟件登入郵件主機來主動發出信件,否則您就得要透過 MUA 來幫你送信到郵件主機上頭去。 最常見的 MUA 像是 Mozilla 推出的Thunderbird ( 雷鳥 ) 自由軟件, 或者是 Linux 桌面 KDE 常見的 Kmail ,及Windows 內件的 Outlook Express (OE) 等 。MUA 主要的功能就是收受郵件主機的電子郵件,以及提供用戶瀏覽與編寫郵件的功能!

MTA (Mail Transfer Agent)
MUA 幫用戶傳送郵件到郵件主機上,那這部郵件主機如果能夠幫用戶將這封信寄出去, 那它就是一部郵件傳送主機 (MTA) 啦!這個 MTA 就是『郵件傳送代理人』的意思。也來顧名思義一下,既然是『傳送 代理人』, 那么使用者寄出的信,與使用者要收信時,就是找它 (MTA) 就對啦!基本上, MTA 的功能有這些:
1)收受信件:使用簡單郵件傳送協議 (SMTP)
MTA 主機最主要的功能就是將來自客戶端或者是其它 MTA 的來信收下來,這個時候 MTA 使用的是 Simple Mail Transfer Protocol (SMTP) ,它使用的是25端口。
2) 轉遞信件
如果該封信件的目的地並不是本身用戶,且該封信的相關數據符合使用 MTA 的權力, 那么MTA 就會將該封信再傳送到下一部主機上。這即是所謂的轉遞 (Relay) 的功能。
3)響應使用者的收信要求
POP 或 IMAP 協定用戶可以透過 MTA 主機提供的郵政服務協議 (Post Office Protocol, POP) 來收下自己的信件, 也可以透過IMAP (Internet Message Access Protocol) 協議將自己的信件保留在郵件主機上面, 並進一步建立郵件數據匣等進階工作。

總之,一般提到的 Mail Server 就是 MTA !而嚴格來說, MTA 其實僅是指 SMTP 這個協議而已。 而達成 MTA的 SMTP 功能的主要套件包括老牌的 sendmail ,后起之秀的 postfix ,還有qmail等等。

MDA (Mail Delivery Agent) 
字面上的意思是『郵件遞送代理人』的意思。事實上,這個 MDA 是掛在 MTA 底下的一個小程序, 最主要的功能就是: 分析由 MTA 所收到的信件表頭或內容等數據, 來決定這封郵件的去向。 所以說,上面提到的MTA 的信件轉遞功能,其實是由 MDA 達成的。 舉例來說,如果 MTA 所收到的這封信目標是自己,那么MDA 會將這封信給它轉到使用者的信箱 (Mailbox) 去, 如果不是呢?那就准備要轉遞出去了。此外, MDA 還有分析與過濾郵件等功能喔!如:過濾垃圾郵件,自動回復,自動轉發等……。

各主要的 MTA 程序 (sendmail,postfix...) 都有自己的 MDA 功能,不過有些外掛的程序功能更強大, 舉例來說 procmail就是一個過濾的好幫手,另外 Mailscanner + Spamassassion 也是可以使用的一些 MDA 喔。

Mailbox 
就是電子郵件信箱!簡單的說,就是某個賬號專用的信件收受檔案。我們的 Linux 系統默認的信箱都是放在 /var/spool/mail/ 使用者賬號 中! 若 MTA 所收到的信件是本機的使用者, MDA 就會將信件送到該 mailbox 當中去!

POP3
(Post Office Protocol 3)即郵局協議的第3個版本,它規定怎樣將個人計算機連接到Internet的郵件服務器和下載電子郵件的電子協議。它是因特網電子郵件的第一個離線協議標准,POP3允許用戶從服務器上把郵件存儲到本地主機(即自己的計算機)上,同時刪除保存在郵件服務器上的郵件,而POP3服務器則是遵循POP3協議的接收郵件服務器,用來接收電子郵件的。

IMAP
Interactive Mail Access Protocol(交互式郵件存取協議)是由美國華盛頓大學所研發的一種郵件獲取協議。它的主要作用是郵件客戶端(例如MS Outlook Express)可以通過這種協議從郵件服務器上獲取郵件的信息,下載郵件等。無論是POP3還是IMAP都是描述如何從郵箱取出郵件。

請注意:POP3/IMAP和SMTP可以組建在不同的服務器上,經常使用MUA的用戶肯定記得軟件的設置中經常將POP3/IMAP和SMTP進行分開設置。

——————————SMTP、POP3、IPMAP三者說明——————————

簡單來說:SMTP是郵件發送協議;POP3和IMAP是郵件接收協議。其中:

1)SMTP
全稱是"Simple Mail Transfer Protocol",目標是向用戶提供高效、可靠的郵件傳輸。它是一組用於由源地址到目的地址傳送郵件的規則,
通過它來控制郵件的中轉方式。SMTP協議屬於TCP/IP 協議簇,它幫助每台計算機在發送或中轉信件時找到下一個目的地。

SMTP服務器就是遵循SMTP協議的發送郵件服務器。 SMTP認證,簡單地說就是要求必須在提供了賬戶名和密碼之后才可以登錄SMTP 服務器,這就使得那些垃圾郵件的散播者無可乘之機。 

增加SMTP認證的目的是為了使用戶避免受到垃圾郵件的侵擾。

2)POP3
POP3是Post Office Protocol 3的簡稱,即郵局協議的第3個版本,它規定怎樣將個人計算機連接到Internet的郵件服務器和下載電子郵件的電子協議。
它是因特網電子郵件的第一個離線協議標准,POP3允許用戶從服務器上把郵件存儲到本地主機(即自己的計算機)上,同時刪除保存在郵件服務器上的
郵件,而POP3服務器則是遵循POP3協議的接收郵件服務器,用來接收電子郵件的

3)IMAP
IMAP全稱是Internet Mail Access Protocol,即交互式郵件存取協議,它是跟POP3類似郵件訪問標准協議之一。不同的是,開啟了IMAP后,您在電子
郵件客戶端收取的郵件仍然保留在服務器上,同時在客戶端上的操作都會反饋到服務器上,如:刪除郵件,標記已讀等,服務器上的郵件也會做相應
的動作。所以無論從瀏覽器登錄郵箱或者客戶端軟件登錄郵箱,看到的郵件以及狀態都是一致的。

====================POP3和IMAP的區別====================
POP3協議允許電子郵件客戶端下載服務器上的郵件,但是在客戶端的操作(如移動郵件、標記已讀等),不會反饋到服務器上,比如通過客戶端收取了
郵箱中的3封郵件並移動到其他文件夾,郵箱服務器上的這些郵件是沒有同時被移動的 。

而IMAP提供webmail 與電子郵件客戶端之間的雙向通信,客戶端的操作都會反饋到服務器上,對郵件進行的操作,服務器上的郵件也會做相應的動作。
同時,IMAP像POP3那樣提供了方便的郵件下載服務,讓用戶能進行離線閱讀。IMAP提供的摘要瀏覽功能可以讓你在閱讀完所有的郵件到達時間、主題、
發件人、大小等信息后才作出是否下載的決定。此外,IMAP 更好地支持了從多個不同設備中隨時訪問新郵件。

總之:
IMAP 整體上為用戶帶來更為便捷和可靠的體驗。POP3 更易丟失郵件或多次下載相同的郵件,但 IMAP 通過郵件客戶端與webmail 之間的雙向同步功能很好地避免了這些問題。

注意:
若在web郵箱中設置了“保存到已發送”,使用客戶端POP服務發信時,已發郵件也會自動同步到網頁端“已發送”文件夾內。

網易163免費郵箱相關服務器信息:

 

Maildirs
Maildirs是使用非常廣泛的e-mail郵件存儲格式。也可以說是一種基於目錄的郵件存儲格式。它在添加,移動或刪除時並不依賴於應用程序級的文件鎖定來維護消息的完成性。每一個消息(每一封郵件)被保存在一個獨立的且名稱唯一的文件中。所有的更改均使用基於文件系統的原子操作(atomic filesystem operations )因此文件系統來控制文件鎖定從而避免一致性問題。通常Maildir為一個目錄(名稱為Maildir)其下包含三個子目錄,分別為tmp,new和cur。

Courier IMAP
Courier IMAP server 是使用Maildir存儲格式的高速,可擴展,企業級 IMAP 服務器。許多E-mail提供商使用Courier IMAP server來處理幾十萬的郵件用戶,使用它建立IMAP和POP3集合代理,可以說Courier IMAP server 簡直具有無限的水平擴展能力。在代理配置環境中,一些Courier 服務器提供IMAP和POP3服務,它們等待客戶端登陸請求,查找並操作郵件用戶的mailbox,與服務器建立代理連接,所有的這些操作都在一個單獨的,無縫連接的進程中。

Courier-IMAP主要特點:
- 小巧而高效;
- 提供多種用戶認證模塊和方式;
- 支持虛擬郵箱;
- 可限制IMAP同時登錄的總數目及同一個IP地址同時登錄的數目,能有效保護系統在受到拒絕服務(Denial-of-service)攻擊時不致因超載而癱瘓;

maildrop
具有過濾功能的郵件投遞代理(MDA)。

Courier-Authlib
Courier authentication library 為其他 Courier 應用程序提供驗證服務。

SASL
SASL的英文全稱是Simple Authentication and Security Layer,即簡單驗證和安全層。SMTP 協議並沒有提供用戶驗證功能,很容易匿名中轉郵件。即使限制了可以轉發的網段,也不安全。他的定義是: a method for adding authentication support to connection-based protocols,為基於連接的協議提供認證功能。SASL是一個膠合(glue)庫,通過這個庫把應用層 與 形式多樣的認證系統整合在一起。這有點類似於PAM,但是后者是認證方式,決定什么人可以訪問什么服務,而SASL是認證過程,側重於信任建立過程,這個過程可以調用PAM來建立信任關系。

Open-Relay是什么?
Open-Relay(開放轉發或匿名轉發)是指由於郵件服務器不理會郵件發送者或郵件接受者的是否為系統所設定的用戶,而對所有的入站郵件一律進行轉發(RELAY)的功能。通常,若郵件服務器的此功能開放,則我們一般稱此郵件服務器是Open-Relay的。

由於Internet E-mail采用開放式標准,所以MTA、MDA、MUA等不同角色,可分別由許多不同的軟件包來扮演。實現相同協議的不同包,可以彼此互相交流,而不管它們是在什么系統上運行。如果將一個完整的E-mail郵件系統集中在一起,可以發現的是處理SMTP的是一套軟件,處理POP/IMAP的是另一套軟件。但郵件系統中的每一種角色,都有許多不同的軟件可以選擇。

二、Postfix+Extmail+Extman環境部署過程
1)基礎環境准備

[root@mail ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)

[root@mail ~]# ifconfig|grep addr|grep Bcast|cut -d":" -f2|awk -F" " '{print $1}'
192.168.10.202

[root@mail ~]# hostname                                                          
mail.kevin.com

[root@mail ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.202 mail.kevin.com

[root@mail ~]# /etc/init.d/iptables stop
[root@mail ~]# setenforce 0
[root@mail ~]# getenforce 
[root@mail ~]# cat /etc/sysconfig/selinux
......
SELINUX=disabled


軟件部署說明
------------------------------------------------------------------------------
軟件名稱                  功能
Postfix                  MTA
MySQL                    存儲postfix虛擬用戶信息
courier-authlib          搭配SASL做驗證
cyrus-sasl               把應用(E-Mail)與驗證(courier-authlib)結合在一起
courier-imap             結合postfix提供imap/imaps與pop3/pop3s服務
perl                     解釋器
pcre                     安裝Imap需要-- Perl兼容正則表達式
httpd                    web服務器
Extmail                  webmail服務器
Extman                   結合extmail管理郵件用戶
Clamav                   殺毒軟件
amavisd-new              掃描郵件病毒
Spamassassin             防垃圾郵件

2)軟件下載

web服務器軟件
這里我安裝了LAMP環境,其實只需要http、mysql,不需要php。
LAMP安裝包下載地址:https://pan.baidu.com/s/1pYo9X7f1vy5d70eV0RDYWA
提取密碼:ebj8

郵件服務器軟件
相關軟件下載地址:https://pan.baidu.com/s/1Ai6FlJoUzeWGLXjGjeV0sg
提取密碼:c4ft

3)LAMP環境安裝
安裝文檔:http://www.cnblogs.com/kevingrace/p/6395131.html

apache的配置
下載地址:https://pan.baidu.com/s/1f8kMhADiOzAYqxv990136g
提取密碼:g1vy


[root@mail extra]# pwd
/usr/local/apache/conf/extra

[root@mail extra]# cat extmail.conf 
<VirtualHost *:80>
ServerName mail.kevin.com
DocumentRoot /data/web/
</VirtualHost>

[root@mail extra]# cat /data/web/test.php 
<?php
phpinfo()
?>

如上,在后面的extmail和extman還沒安裝之前,extmail.conf配置暫時如上所示內容,這樣便於進行httpd服務的測試。

解析mail.kevin.com到192.168.10.202機器上(或做hosts綁定),然后訪問http://mail.kevin.com/test.php即可測試!

待安裝完extmail和extman之后,上面的extmail.conf文件配置如下:
[root@mail extra]# cat extmail.conf       
<VirtualHost *:80>
ServerName mail.kevin.com
DocumentRoot /data/web/
ScriptAlias /extmail/cgi  /data/web/extmail/cgi
ScriptAlias /extman/cgi  /data/web/extman/cgi
Alias /extmail  /data/web/extmail/html
Alias /extman  /data/web/extman/html
</VirtualHost>

4)Mysql安裝
安裝文檔:http://www.cnblogs.com/kevingrace/p/6109679.html
mysql一鍵安裝腳本下載地址:https://pan.baidu.com/s/1oRePULz4gu_y7oFuc4mB9A          提取密碼:bhcw

安裝mysql的時候,要卸載系統自帶的mysql,但要記住,最好不要卸載mysql-libs
[root@mail ~]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64

如果卸載了mysql-libs,那么就會把默認的libmysqlclient.so.16庫文件刪除,而編譯安裝的mysql,庫文件是libmysqlclient.so.18。
但是登錄extmail時會依賴libmysqlclient.so.16這個庫文件,這樣就會報如下錯:
install_driver(mysql) failed: Can't load '/usr/lib64/perl5/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.16: 
cannot open shared object file: No such file or directory at /usr/lib64/perl5/DynaLoader.pm line 200. at (eval 17) line 3 Compilation 
failed in require at (eval 17) line 3. Perhaps a required shared library or dll isn't installed where expected at 
/data/web/extmail/libs/Ext/Auth/MySQL.pm line 45

這時候解決辦法:https://www.cnblogs.com/kevingrace/p/9378776.html
其實也就是重新安裝mysql,重新生成libmysqlclient.so.16庫文件。

所以最好還是不要刪除mysql-libs,免得后面再重新安裝自帶的mysql,重新生成libmysqlclient.so.16的庫文件。

5)創建相關用戶

用戶      組        簡介                            備注
mysql    mysql     mysql用戶和組                    安裝mysql時創
vmail    vmail     Postfix虛擬賬戶所使用的用戶和組     無home目錄不可能登陸系統。uid: 1001,gid: 1001
無       postdrop  Postfix專用組                    該組不能包含任何成員,包括前面的postfix虛擬帳號也不例外。gid:1002
postfix  postfix   Postfix主程序使用的用戶和組        uid:1000,gid:1000
clamav   clamav    clamav 殺毒軟件用戶               無
amavis   amavis    郵件掃描用戶                      無
------------------------------------------------------------------------------------

[root@mail ~]# groupadd -g 1001 vmail
[root@mail ~]# useradd -g 1001 -u 1001 -s /sbin/nologin -M vmail
[root@mail ~]# groupadd -g 1002 postdrop
[root@mail ~]# yum remove postfix
[root@mail ~]# userdel -r postfix
[root@mail ~]# groupadd -g 1000 postfix 
[root@mail ~]# useradd -g 1000 -u 1000 -s /sbin/nologin -M postfix
[root@mail ~]# useradd clamav
[root@mail ~]# useradd amavis

6)卸載系統中相關軟件

如果系統自帶了postfix,就卸載了(上面已卸載)
[root@mail ~]# yum remove postfix
[root@mail ~]# userdel -r postfix

接着卸載sendmail
CentOS系統安裝后,默認是裝好了Sendmail服務,其目的是為root用戶發送一些有用的信息。那么,在安裝Postfix之前一定要先卸載Sendmail。
[root@mail ~]# service sendmail stop
[root@mail ~]# rpm -qa | grep sendmail
sendmail-8.13.8-2.el5
[root@mail ~]# rpm -e --nodeps sendmail-8.13.8-2.el5

卸載sasl
[root@mail ~]# rpm -qa |grep sasl
cyrus-sasl-lib-2.1.22-4
cyrus-sasl-devel-2.1.22-4
cyrus-sasl-2.1.22-4
cyrus-sasl-plain-2.1.22-4
[root@mail ~]# rpm -e --nodeps cyrus-sasl-lib-2.1.22-4 cyrus-sasl-devel-2.1.22-4 cyrus-sasl-2.1.22-4 cyrus-sasl-plain-2.1.22-4

7)配置相關系統環境

[root@mail ~]# vim /etc/ld.so.conf
......
/usr/local/lib
/usr/local/mysql/lib/
/usr/lib64/

[root@mail ~]# ldconfig

8)創建相應目錄

虛擬用戶的郵件存放路徑
[root@mail ~]# mkdir -p /var/mailbox
[root@mail ~]# chown vmail.vmail /var/mailbox
[root@mail ~]# chmod 700 /var/mailbox

apache的base目錄
[root@mail ~]# mkdir -p /data/web
[root@mail ~]# chown vmail.vmail /data/web

extman的臨時目錄
[root@mail ~]# mkdir /tmp/extman
[root@mail ~]# chown vmail.vmail /tmp/extman

clamav臨時目錄
[root@mail ~]# mkdir -p /dev/shm/clamav/tmp
[root@mail ~]# chown -R amavis.amavis /dev/shm/clamav/
[root@mail ~]# echo " mkdir -p /dev/shm/clamav/tmp" >>/etc/rc.local
[root@mail ~]# echo " chown -R amavis.amavis /dev/shm/clamav/" >>/etc/rc.local

9)安裝必要組件

先安裝epel源(百度雲盤下載地址:https://pan.baidu.com/s/1JucsbDuYwedEb4kGvN6MPg     提取密碼:pxn7)
[root@mail ~]# cd /usr/local/src/
[root@mail src]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
[root@mail src]# rpm -ivh epel-release-latest-6.noarch.rpm

[root@mail src]# yum install perl-Unix-Syslog perl-DBI perl-Class-DBI-mysql perl-DBD-MySQL rrdtool perl-rrdtool perl-File-Tail perl-IO-stringy perl-MIME-* perl-Net-Server perl-BerkeleyDB perl-Crypt-* perl-Mail-DKIM perl-Convert-TNEF perl-Convert-UUlib perl-Archive-Zip

===============================================================
在使用yum過程中,遇到下面問題:
libsasl2.so.2: cannot open shared object file: No such file or directory

這是因為在上面已經提前卸載了cyrus-sasl-lib所致!
解決辦法:
從別的機器上拷貝這個庫文件到本機上

比如從192.168.10.201機器上拷貝到本機:
[root@localhost ~]# find / -name libsasl2.so.2           
/usr/lib64/libsasl2.so.2
[root@localhost ~]# ll /usr/lib64/libsasl2.so.2          
lrwxrwxrwx. 1 root root 18 Jan 16  2018 /usr/lib64/libsasl2.so.2 -> libsasl2.so.2.0.23
[root@localhost ~]# ll /usr/lib64/libsasl2.so.2.0.23
-rwxr-xr-x. 1 root root 108728 Mar 25  2015 /usr/lib64/libsasl2.so.2.0.23

[root@localhost ~]# rsync -e "ssh -p22" -avpgolr /usr/lib64/libsasl2.so.2.0.23 root@192.168.10.202:/usr/lib64/
[root@localhost ~]# rsync -e "ssh -p22" -avpgolr /usr/lib64/libsasl2.so.2 root@192.168.10.202:/usr/lib64/

到本機上查看:
[root@mail ~]# ll /usr/lib64/libsasl2.so.2        
lrwxrwxrwx. 1 root root 18 Jan 16  2018 /usr/lib64/libsasl2.so.2 -> libsasl2.so.2.0.23

再次使用yum就正常了!
===============================================================

10)安裝配置courier-authlib

[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -jvxf courier-authlib-0.62.4.tar.bz2 
[root@mail src]# cd courier-authlib-0.62.4
[root@mail courier-authlib-0.62.4]# ./configure --prefix=/usr/local/courier-authlib --with-mailuser=vmail --with-mailgroup=vmail --with-mysql-libs=/usr/local/mysql/lib/mysql --with-mysql-includes=/usr/local/mysql/include/mysql --without-stdheaderdir
[root@mail courier-authlib-0.62.4]# make && make install
[root@mail courier-authlib-0.62.4]# make install-configure
[root@mail courier-authlib-0.62.4]# ln -s /usr/local/courier-authlib/bin/courierauthconfig /usr/bin/

說明:
在指定編譯選項時需要指定mysql的include位置和lib位置,由於我mysql是源碼安裝的,所以這里一定要使用--with-mysql-includes和
--with-mysql-includes參數指定。--without-stdheaderdir 參數是說將頭文件安裝到我自己制定的目錄中,而非編譯器找到的默認目錄中。

===================需要注意===================
如果courier-authlib安裝到非標准位置(例如安裝到/usr/local下),一定記得在./configure時增加-- without-stdheaderdir,
這樣以后在編譯Courier-IMAP和maildrop的時候,不用特別指定courier- authlib的庫文件位置

--------------------------------------------------------------------------------------------------
安裝完成后需要修改/usr/local/courier-authlib/etc/authlib/目錄下的authdaemonrc 和 authmysqlrc 文件

authdaemonrc這個文件時告訴courier-authlib要使用什么驗證方法
[root@mail courier-authlib-0.62.4]# vim /usr/local/courier-authlib/etc/authlib/authdaemonrc
.......
# 驗證模塊只保留mysql
#authmodulelist="authuserdb authpam authmysql authcustom authpipe"
authmodulelist="authmysql"
.......
#authmodulelistorig="authuserdb authpam authmysql authcustom authpipe"
authmodulelistorig="authmysql"
......
# 打開debug日志和包括記錄驗證密碼,有助於以后的排錯
#DEBUG_LOGIN=0
DEBUG_LOGIN=2

--------------------------------------------------------------------------------------------------
authmysqlrc這個文件是courier-authlib程序通過mysql驗證時需要訪問數據庫的信息。
[root@mail courier-authlib-0.62.4]# vim /usr/local/courier-authlib/etc/authlib/authmysqlrc
......
# mysql數據庫地址
MYSQL_SERVER 192.168.10.202

# 訪問數據庫的用戶名
MYSQL_USERNAME extmail

# 訪問數據庫的密碼
MYSQL_PASSWORD extmail

# mysql 的socket路徑
MYSQL_SOCKET /usr/local/mysql/var/mysql.sock

# mysql開放的端口
MYSQL_PORT 3306

# 進行驗證的數據庫是哪個。這個在后面安裝extmail會有介紹
MYSQL_DATABASE extmail

# 進行驗證時要訪問那個表
MYSQL_USER_TABLE mailbox

# 取出密碼的字段是哪個
MYSQL_CRYPT_PWFIELD password

# 默認域名
DEFAULT_DOMAIN kevin.com

# 這里設定mail虛擬帳號的uid
MYSQL_UID_FIELD '1001'

# 虛擬帳號的gid
MYSQL_GID_FIELD '1001'

# 登錄名從哪個字段中取得
MYSQL_LOGIN_FIELD username

# 郵件存放路徑在哪里,這里是根目錄。
MYSQL_HOME_FIELD '/var/mailbox/'

# 郵件賬戶的描述信息字段
MYSQL_NAME_FIELD name

# 郵件賬戶的詳細郵件目錄,從maildir字段中取得。
MYSQL_MAILDIR_FIELD maildir

# MYSQL_USER_TABLE 表中的quota字段,這里concat函數取出S之前的字符
MYSQL_QUOTA_FIELD concat(quota,'S')

# 這里定義SQL語句中where的條件
MYSQL_WHERE_CLAUSE active='1'


===================需要注意===================
默認域名可有可無,如果設置了默認域名,則在courier-authlib連接mysql驗證時如果用戶只指定了username為test的話,則程序會自動
加上這里設置的默認域名,否則必須指定用戶名為test@example.com。這里看來還是設置默認域名比較方便。在設置多個虛擬域名的時候,
主域名的用戶可以不用輸入完整的用戶郵箱,其他虛擬域則必須設置完整的郵件地址。


配置好之后的文件,即如下:
[root@mail courier-authlib-0.62.4]# cat /usr/local/courier-authlib/etc/authlib/authmysqlrc|grep -v "#"|grep -v "^$"
MYSQL_SERVER            192.168.10.202
MYSQL_USERNAME          extmail
MYSQL_PASSWORD          extmail
MYSQL_SOCKET            /usr/local/mysql/var/mysql.sock
MYSQL_PORT              3306
MYSQL_OPT               0
MYSQL_DATABASE          extmail
MYSQL_USER_TABLE        mailbox
MYSQL_CRYPT_PWFIELD     password
DEFAULT_DOMAIN          kevin.com
MYSQL_UID_FIELD         1001
MYSQL_GID_FIELD         1001
MYSQL_LOGIN_FIELD       username
MYSQL_HOME_FIELD        '/var/mailbox'
MYSQL_NAME_FIELD        name
MYSQL_MAILDIR_FIELD     maildir
MYSQL_QUOTA_FIELD       concat(quota,'S')
MYSQL_WHERE_CLAUSE active='1'

加入啟動項(切換到源碼目錄下)
[root@mail courier-authlib-0.62.4]# pwd
/usr/local/src/courier-authlib-0.62.4
[root@mail courier-authlib-0.62.4]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib
[root@mail courier-authlib-0.62.4]# chmod 755 /etc/init.d/courier-authlib
[root@mail courier-authlib-0.62.4]# chkconfig --add courier-authlib
[root@mail courier-authlib-0.62.4]# service courier-authlib start
Starting Courier authentication services: authdaemond
[root@mail courier-authlib-0.62.4]# 

修改authdaemon 的socket目錄權限
如果該目錄權限不正確修改,maildrop及postfix等將無法正確獲取用戶的信息及密碼認證:
[root@mail courier-authlib-0.62.4]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
[root@mail courier-authlib-0.62.4]# ll -d /usr/local/courier-authlib/var/spool/authdaemon
drwxr-xr-x. 2 vmail vmail 4096 Jul 29 02:49 /usr/local/courier-authlib/var/spool/authdaemon
[root@mail courier-authlib-0.62.4]# 

系統集成
[root@mail courier-authlib-0.62.4]# ln -s /usr/local/courier-authlib/lib/courier-authlib/* /usr/lib
[root@mail courier-authlib-0.62.4]# echo "/usr/lib" >> /etc/ld.so.conf
[root@mail courier-authlib-0.62.4]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/mysql/lib/
/usr/lib64/
/usr/lib
[root@mail courier-authlib-0.62.4]# ldconfig 

11)安裝/配置Cyrus-SASL2

編譯安裝
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf cyrus-sasl-2.1.23.tar.gz 
[root@mail cyrus-sasl-2.1.23]# ./configure --disable-anon --disable-gssapi --disable-sample --disable-digest --enable-plain --enable-login --enable-sql --with-mysql=/usr/local/mysql --with-authdaemond=/usr/local/courier-authlib/var/spool/authdaemon/socket
[root@mail cyrus-sasl-2.1.23]# make && make install

特別注意:
--with-authdaemond標出的部分為先前安裝的courier-authlib啟動之后的socket文件位置。一定要指定,否則sasl無法和authdaemon連接!

--------------------------------------------------------------------------------------------
make安裝的時候,可能報如下錯誤:

1)error: mysql.h: No such file or directory
報錯信息大致如下:
......
sql.c:65:19: error: mysql.h: No such file or directory
sql.c: In function '_mysql_open':
......
make[2]: *** [sql.lo] Error 1
make[2]: Leaving directory `/usr/local/src/cyrus-sasl-2.1.23/plugins'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/cyrus-sasl-2.1.23'
make: *** [all] Error 2

解決辦法:
在你的mysql的安裝路徑:/usr/local/mysql/include/mysql 下面看看有沒有 .h的文件,沒有的話,在mysql是上一層目錄中,有這些文件,
拷貝一份到mysql下
[root@mail ~]# cd /usr/local/mysql/include/mysql
[root@mail mysql]# ll mysql.h   
-rw-r--r--. 1 root root 29129 Jul 29 02:59 mysql.h
[root@mail mysql]# ll ../mysql.h
-rw-r--r--. 1 mysql mysql 29129 Dec  9  2017 ../mysql.h
[root@mail mysql]# cd ../
[root@mail include]# \cp -f *.h mysql/
[root@mail include]# ll mysql.h       
-rw-r--r--. 1 mysql mysql 29129 Dec  9  2017 mysql.h

然后再接着make && make install編譯安裝就成功了!

2)error: des.h: No such file or direcctory
報錯信息大致如下:
auth_getpwent.c:48:20: error: des.h: No such file or direcctory 
make[3]: *** [auth_getpwent.o] Error 1
make[3]: Leaving directory '/usr/local/src/cyrus-sasl-2.1.23/saslauthd/'
make[2]: *** [all] Error 2

這個錯誤的原因是沒有找到des.h頭文件導致的,處理的方法很簡單,就是將cyrus-sasl源碼包文件夾下面的mac路徑里的des.h文件拷到錯誤提示的路徑下!

解決辦法:
[root@mail cyrus-sasl-2.1.23]# pwd
/usr/local/src/cyrus-sasl-2.1.23
[root@mail cyrus-sasl-2.1.23]# cp mac/libdes/public/des.h /usr/local/src/cyrus-sasl-2.1.23/saslauthd/
--------------------------------------------------------------------------------------------

系統集成
[root@mail cyrus-sasl-2.1.23]# ln -s /usr/local/lib/sasl2/ /usr/lib/
[root@mail cyrus-sasl-2.1.23]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/mysql/lib/
/usr/lib64/
/usr/lib
[root@mail cyrus-sasl-2.1.23]# ldconfig 

[root@mail cyrus-sasl-2.1.23]# ldconfig -v | grep sasl
ldconfig: Path `/usr/lib' given more than once
ldconfig: Path `/usr/lib64' given more than once
        libsasl2.so.2 -> libsasl2.so.2.0.23
        libcourierauthsasl.so -> libcourierauthsasl.so.0
        libcourierauthsaslclient.so -> libcourierauthsaslclient.so.0

配置
Postfix的SMTP認證需要透過Cyrus-SASL,連接到authdaemon獲取認證信息,這里我們需要配置smtp的驗證,后續的還需要修改postfix的main.cf文件。
這里就先不做說明,等到了配置postfix的時候再說下如何配置postfix的smtp認證的配置。

在/usr/local/lib/sasl2/目錄下創建smtpd.conf文件並添加如下內容
[root@mail cyrus-sasl-2.1.23]# ll /usr/local/lib/sasl2/smtpd.conf
ls: cannot access /usr/local/lib/sasl2/smtpd.conf: No such file or directory
[root@mail cyrus-sasl-2.1.23]# touch /usr/local/lib/sasl2/smtpd.conf
[root@mail cyrus-sasl-2.1.23]# vim /usr/local/lib/sasl2/smtpd.conf
pwcheck_method: authdaemond
mech_list: PLAIN LOGIN
log_level: 3

authdaemond_path: /usr/local/courier-authlib/var/spool/authdaemon/socket

12)Postfix安裝/配置

[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf postfix-2.8.7.tar.gz
[root@mail src]# cd postfix-2.8.7
[root@mail postfix-2.8.7]# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/local/include/sasl' 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm -L/usr/local/lib -lsasl2'
[root@mail postfix-2.8.7]# make && make install

編譯完成后程序會自動進入交互狀態,等待用戶的輸入,一般情況下,一路回車即可完成所有的安裝。
----------------------------------------------------------------------------------------------
可能報錯如下:
bin/postconf: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
......
postfix-install: Error: "" should be "no" or an absolute path name.
make: *** [install] Error 1

解決辦法:
[root@mail postfix-2.8.7]# find / -name libpcre.so.1
/usr/local/lib/libpcre.so.1                      #如果沒有,就執行"yum install -y pcre pcre-devel"(其實在安裝LAMP時已編譯安裝了pcre)

然后放到.so庫的引用文件里:
[root@mail postfix-2.8.7]# echo "/usr/local/lib/" >> /etc/ld.so.conf
[root@mail postfix-2.8.7]# cat /etc/ld.so.conf                      
include ld.so.conf.d/*.conf
/usr/local/mysql/lib/
/usr/lib64/
/usr/lib
/usr/local/lib/
[root@mail postfix-2.8.7]# ldconfig 

最后再執行即可成功了!
[root@mail postfix-2.8.7]# make && make install
----------------------------------------------------------------------------------------------
注意: 
make的參數可以參考postfix源碼目錄下README_FILES文件夾中的INSTALL、MYSQL_README、SASL_README 文件。

接着生成別名二進制文件
下面這條命令的作用就是將/etc/alias 文件利用工具轉為postfix可以讀取的二進制文件,存為/etc/postfix/ aliases
[root@mail postfix-2.8.7]# /usr/bin/newaliases

驗證安裝的Postfix是否支持SASL、MySQL
[root@mail postfix-2.8.7]# postconf -a
cyrus
dovecot
[root@mail postfix-2.8.7]# postconf -m
btree
cidr
environ
hash
internal
mysql
nis
pcre
proxy
regexp
static
tcp
texthash
unix

需要注意的是:
-- 如果沒有顯示出cyrus項說明你的postfix不支持SASL,需要檢查但前系統是否將libsasl庫加入到系統中,利用ldconfig -v | grep 查看。
   如果沒有libsasl2.so.2 -> libsasl2.so.2.0.23 需要將/usr/local/lib目錄添加到/etc/ld.so.conf文件中然后再執行ldconfig命令使其生效。
-- 如果沒有mysql項說明postfix不支持mysql協議的查詢,肯定是編譯時mysql的目錄指定錯誤,重新查看mysql的lib目錄和include目錄的確切
   位置后重新編譯安裝postfix

Postfix配置
先不着急進行Postfix的配置,這個放到最后等courier-imap、maildrop、extmail安裝完成之后做統一的配置。

創建虛擬用戶驗證配置文件的目錄
由於虛擬用戶是結合mysql進行驗證的,postfix的配置文件中可以針對虛擬用戶進行配置的定義,該目錄就是存儲mysql驗證的配置文件。
后文extman配置時會介紹。這里先創建相應目錄。
[root@mail postfix-2.8.7]# mkdir -p /etc/postfix/mysql

創建postfix啟動daemon控制腳本
[root@mail postfix-2.8.7]# ll /etc/init.d/postfix 
ls: cannot access /etc/init.d/postfix: No such file or directory
[root@mail postfix-2.8.7]# vim /etc/init.d/postfix
#!/bin/sh
#
# postfix Postfix Mail Transfer Agent
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail Transport Agent, which is the program \
# that moves mail from one machine to another.
# processname: master
# pidfile: /var/spool/postfix/pid/master.pid
# config: /etc/postfix/main.cf
# config: /etc/postfix/master.cf
#
# $Revision: 2.4 $
#
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x /usr/sbin/postfix ] || exit 0
[ -d /etc/postfix ] || exit 0
[ -d /var/spool/postfix ] || exit 0
RETVAL=0
start() {
# Start daemons.
echo -n "Starting postfix: "
/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
echo
return $RETVAL
}
stop() {
# Stop daemons.
echo -n "Shutting down postfix: "
/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
echo
return $RETVAL
}
reload() {
echo -n "Reloading postfix: "
/usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure
RETVAL=$?
echo
return $RETVAL
}
restart() {
stop
start
}
abort() {
/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure
return $?
}
flush() {
/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure
return $?
}
check() {
/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure
return $?
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
abort)
abort
;;
flush)
flush
;;
check)
check
;;
status)
status master
;;
condrestart)
# don't use /var/lock/subsys/postfix, check for postfix running directly
daemon_directory=$(postconf -h daemon_directory)
$daemon_directory/master -t 2>/dev/null && : || restart
;;
*)
echo "Usage: postfix {start|stop|restart|reload|abort|flush|check|status|condrestart}"
exit 1
esac
exit $?


添加服務,指定自動啟動
[root@mail postfix-2.8.7]# chmod +x /etc/init.d/postfix
[root@mail postfix-2.8.7]# chkconfig --add postfix
[root@mail postfix-2.8.7]# chkconfig postfix on

13)安裝/配置 courier-imap

[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -jvxf courier-imap-4.5.1.tar.bz2 && cd courier-imap-4.5.1
[root@mail courier-imap-4.5.1]# ./configure --prefix=/usr/local/courier-imap --with-redhat --disable-root-check --enable-unicode=utf-8,iso-8859-1,gb2312,gbk,gb18030 --with-trashquota --with-dirsync --with-mysql-libs=/usr/local/mysql/lib/mysql --with-mysql-includes=/usr/local/mysql/include/mysql --with-authmysql --with-authmysql=yes CPPFLAGS='-I/usr/local/courier-authlib/include' LDFLAGS='-L/usr/local/courier-authlib/lib/courier-authlib' COURIERAUTHCONFIG='/usr/local/courier-authlib/bin/courierauthconfig'

[root@mail courier-imap-4.5.1]# make && make install && make install-configure
[root@mail courier-imap-4.5.1]# cp courier-imap.sysvinit /etc/rc.d/init.d/imapd
[root@mail courier-imap-4.5.1]# chmod 755 /etc/rc.d/init.d/imapd
[root@mail courier-imap-4.5.1]# chkconfig --add imapd
[root@mail courier-imap-4.5.1]# chkconfig imapd on


配置
[root@mail courier-imap-4.5.1]# vim /usr/local/courier-imap/etc/imapd
......
IMAPDSTART=YES

[root@mail courier-imap-4.5.1]# vim /usr/local/courier-imap/etc/pop3d
......
POP3DSTART=YES

啟動服務
[root@mail courier-imap-4.5.1]# service imapd start
Starting Courier-IMAP server: imap pop3
[root@mail courier-imap-4.5.1]# netstat -tulnp | egrep "110|143"
tcp        0      0 :::110                      :::*                        LISTEN      12045/couriertcpd   
tcp        0      0 :::143                      :::*                        LISTEN      12039/couriertcpd  

14)安裝配置maildrop

編譯安裝pcre(這個在之前部署LAMP的時候就已經安裝了)

編譯安裝maildrop
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -jvxf maildrop-2.2.0.tar.bz2 
[root@mail src]# cd maildrop-2.2.0
[root@mail maildrop-2.2.0]# ./configure --enable-sendmail=/usr/sbin/sendmail --enable-trusted-users='root vmail' --enable-syslog=1 --enable-maildirquota --enable-maildrop-uid=1001 --enable-maildrop-gid=1001 --with-trashquota --with-dirsync
[root@mail maildrop-2.2.0]# make && make install

需要注意:
./configure --help 查看的編譯選項不全,查看目錄下的INSTALL文件獲得更多的編譯選項。

驗證安裝
[root@mail maildrop-2.2.0]# maildrop -v
maildrop 2.1.0 Copyright 1998-2005 Double Precision, Inc.
GDBM/DB extensions enabled.
Courier Authentication Library extension enabled.
Maildir quota extension enabled.
This program is distributed under the terms of the GNU General Public
License. See COPYING for additional information.

如果沒有顯示Courier Authentication Library extension enabled.說明你的maidrop還不支持courier auth,請仔細檢查,
重新編譯maildrop建議不要自定義目錄安裝,可以減少不必要的麻煩,刪除很方便:直接在已經編譯的源代碼目錄 make uninstall

知道maildrop把郵件移動到哪個用戶目錄還是非常有必要的,這個操作就是maildrop的日志記錄。這里我們創建一個新文件
/etc/maildroprc,這個文件就2行內容:如果加入反SPAM,需要更改以下內容:
[root@mail maildrop-2.2.0]# vim /etc/maildroprc
logfile "/var/log/maildrop.log" 
to "$HOME/$DEFAULT"

需要注意:
/etc/maildroprc 文件為maildrop工作在投遞模式下的全局過濾說明文檔。詳細的配置說明可以參考 man maildropfilter 文檔。

[root@mail maildrop-2.2.0]# touch /var/log/maildrop.log;chown vmail.vmail /var/log/maildrop.log
[root@mail maildrop-2.2.0]# ll /var/log/maildrop.log
-rw-r--r--. 1 vmail vmail 0 Jul 29 06:13 /var/log/maildrop.log

15)安裝extmail與extman

安裝extmail
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf extmail-1.1.0.tar.gz 
[root@mail src]# mv extmail-1.1.0 /data/web/
[root@mail src]# mv /data/web/extmail-1.1.0 /data/web/extmail
[root@mail src]# chown vmail.vmail -R /data/web
[root@mail src]# cd /data/web/extmail/
[root@mail extmail]# cp -p webmail.cf.default webmail.cf

配置extmail
[root@mail extmail]# vim webmail.cf
SYS_CONFIG = /data/web/extmail/
SYS_LANGDIR = /data/web/extmail/lang

SYS_TEMPLDIR = /data/web/extmail/html

# web頁面登陸語言設置
SYS_USER_LANG = zh_CN

#虛擬用戶mail存放的根目錄
SYS_MAILDIR_BASE = /var/mailbox

# 關於數據庫的設置,根據實際情況設置
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_DB = extmail
SYS_MYSQL_HOST = 192.168.10.202

SYS_MYSQL_SOCKET = /usr/local/mysql/var/mysql.sock

# courier-authlib驗證程序socket位置
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket

# 全局聯系人配置文件位置
SYS_G_ABOOK_FILE_PATH = /data/web/extmail/globabook.cf

---------------------------------------------------------------------------

安裝extman
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf extman-0.2.5.tar.gz 
[root@mail src]# mv extman-0.2.5 /data/web/
[root@mail src]# mv /data/web/extman-0.2.5 /data/web/extman
[root@mail src]# chown -R vmail.vmail /data/web/

配置extman
[root@mail src]# sed -i 's/extmail.org/kevin.com/g' /data/web/extman/docs/init.sql
[root@mail src]# cd /data/web/extman/
[root@mail extman]# vim webman.cf
SYS_CONFIG = /data/web/extman/

SYS_LANGDIR = /data/web/extman/lang

SYS_TEMPLDIR = /data/web/extman/html

SYS_MAILDIR_BASE = /var/mailbox

# sys_sess_dir, the session dir

SYS_SESS_DIR = /tmp/extman/

SYS_CAPTCHA_ON = 0                         #不需要認證碼

SYS_GROUPMAIL_SENDER = postmaster@kevin.com

# sys_default_uid, if not set, webman will ignore it

SYS_DEFAULT_UID = 1001

# sys_default_gid, if not set, webman will ignore it

SYS_DEFAULT_GID = 1001

# 數據庫相關設置

SYS_MYSQL_USER = extman

SYS_MYSQL_PASS = extman

SYS_MYSQL_DB = extmail

SYS_MYSQL_HOST = 192.168.10.202

SYS_MYSQL_SOCKET = /usr/local/mysql/var/mysql.sock


修改extmail創建表的mysql語句
[root@mail extman]# sed -i 's/TYPE=MyISAM/ENGINE=InnoDB DEFAULT CHARSET=utf8/g' /data/web/extman/docs/extmail.sql

初始化數據庫(mysql5.6.39在之前已經編譯安裝了,密碼為123456)
[root@mail extman]# mysql -u root -p123456 </data/web/extman/docs/extmail.sql
[root@mail extman]# mysql -u root -p123456 </data/web/extman/docs/init.sql

登錄mysql查看是否已經導入數據了:
[root@mail extman]# mysql -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 764
Server version: 5.6.39-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| extmail            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use extmail;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_extmail |
+-------------------+
| alias             |
| domain            |
| domain_manager    |
| mailbox           |
| manager           |
+-------------------+
5 rows in set (0.00 sec)

mysql> 

需要注意:
通過仔細的查看extmail.sql 文件,發現其sql語句中已經有添加用戶的操作。
因此,下面的通過grant命令來創建用戶並指定權限的操作可以省略。
另外可以根據需要修改extmail.sql中用戶的配置。但修改之后一定要將extmail/extman的配置也同步修改。

將虛擬用戶驗證配置文件復制到/etc/postfix/mysql目錄中
[root@mail extman]# cp /data/web/extman/docs/mysql_virtual_* /etc/postfix/mysql/

注意:這些文件是postfix的配置相關文件,其所有者必須為root。
[root@mail extman]# ll /etc/postfix/mysql/
total 20
-rw-r--r--. 1 root root 166 Jul 29 07:00 mysql_virtual_alias_maps.cf
-rw-r--r--. 1 root root 168 Jul 29 07:00 mysql_virtual_domains_maps.cf
-rw-r--r--. 1 root root 170 Jul 29 07:00 mysql_virtual_limit_maps.cf
-rw-r--r--. 1 root root 172 Jul 29 07:00 mysql_virtual_mailbox_maps.cf
-rw-r--r--. 1 root root 173 Jul 29 07:00 mysql_virtual_sender_maps.cf

針對extmail/extman 對mysql的配置
mysql> grant all on extmail.* to extmail@'192.168.10.202' identified by 'extmail';         
Query OK, 0 rows affected (0.03 sec)

mysql> grant all on extmail.* to extman@'192.168.10.202' identified by 'extman';         
Query OK, 0 rows affected (0.18 sec)

mysql> 


針對extmail/extman 對apache所做的配置
[root@mail extman]# vim /usr/local/apache/conf/httpd.conf
......
DirectoryIndex index.html index.cgi index.htm index.php index.html.var

[root@mail extman]# vim /usr/local/apache/conf/extra/extmail.conf
<VirtualHost *:80>
ServerName mail.kevin.com
DocumentRoot /data/web/
ScriptAlias /extmail/cgi  /data/web/extmail/cgi
ScriptAlias /extman/cgi  /data/web/extman/cgi
Alias /extmail  /data/web/extmail/html
Alias /extman  /data/web/extman/html
</VirtualHost>

重啟apache服務
[root@mail extman]# /usr/local/apache/bin/httpd -k restart
[root@mail extman]# lsof -i:80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   12744 vmail    4u  IPv6 323890      0t0  TCP *:http (LISTEN)
httpd   12745 vmail    4u  IPv6 323890      0t0  TCP *:http (LISTEN)
httpd   12746 vmail    4u  IPv6 323890      0t0  TCP *:http (LISTEN)
httpd   12747 vmail    4u  IPv6 323890      0t0  TCP *:http (LISTEN)
httpd   12748 vmail    4u  IPv6 323890      0t0  TCP *:http (LISTEN)
httpd   28707  root    4u  IPv6 323890      0t0  TCP *:http (LISTEN)

16)測試extmail/extman
解析mail.kevin.com到192.168.10.202機器上(或做hosts綁定),訪問http://mail.kevin.com/extmail/,如下圖所示,注意選擇的是"登錄郵箱管理"這一項,進入郵箱管理后台界面。默認用戶名和密碼分別是root@kevin.comextmail*123*

如果忘記了管理員密碼,則使用下面命令密碼重新修改為"extmail*123*"的初始默認密碼!

mysql> update manager set password='$1$BrT9qxfB$Ha81Mb5YVV6rNKNN5jmtj1' where username= "root@kevin.com";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select username,password from manager;
+----------------------------+------------------------------------+
| username                   | password                           |
+----------------------------+------------------------------------+
| root@kevin.com | $1$BrT9qxfB$Ha81Mb5YVV6rNKNN5jmtj1 |
+----------------------------+------------------------------------+
1 row in set (0.00 sec)

mysql> flush privileges;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

登錄的時候,可能會有報錯,總結如下:

報錯1:
Can't locate CGI.pm in @INC (@INC contains: /data/web/extmail/libs /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl 
/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5) at /data/web/extmail/libs/Ext/CGI.pm line 20. BEGIN failed--compilation aborted at /data/web/extmail/libs/Ext/CGI.pm line 20. Compilation failed in require at /data/web/extmail/libs/Ext/App.pm line 23. BEGIN failed--compilation aborted at /data/web/extmail/libs/Ext/App.pm line 23. Compilation failed in require at /data/web/extmail/libs/Ext/App/Login.pm line 16. BEGIN failed--compilation aborted at /data/web/extmail/libs/Ext/App/Login.pm line 16. Compilation failed in require at /data/web/extmail/cgi/index.cgi line 20.

解決辦法:
[root@mail ~]# yum install perl-CGI
=========================================
報錯2:
Unix::Syslog not found, please install it first! (in cleanup) Undefined subroutine &Ext::Logger::do_closelog called at /data/web/extmail/libs/Ext/Logger.pm line 86.

解決辦法:
[root@mail ~]#  perl Makefile.PL  
如果安裝這個包的報錯
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 1. BEGIN failed--compilation aborted at Makefile.PL line 1.

解決辦法:
[root@mail ~]# yum install -y perl-ExtUtils-MakeMaker
然后
[root@mail ~]# perl Makefile.PL 
[root@mail ~]# make install
=========================================
報錯3
DBI connect('database=extmail;host=localhost;mysql_socket=/usr/local/mysql/var/mysql.sock','db_user',...) failed: Access denied for user 'db_user'@'localhost' (using password: YES) at /data/web/extmail/libs/Ext/Auth/MySQL.pm line 45

解決辦法:
進入數據庫失敗,修改主配置文件。 SYS_MYSQL_USER = extmail ; SYS_MYSQL_PASS = extmail
=========================================

登錄郵箱后台后,可以進行添加用戶、添加管理員、修改管理員信息、添加別名、郵箱限額、網盤限額、禁止用戶自由注冊(在"域列表"中選中kevin.com進行修改)等操作。

特別注意:
用戶創建完成后查看/var/mailbox/目錄下是否自動創建了kevin.com/wangshibo目錄。如果沒有生成這個用戶目錄,說明創建用戶的時候沒有自動生成目錄,這種情況大多是因為權限不對。通過查看apache的日志排錯。同時將/var/mailbox的權限設置為700 所有者設置為 vmail。

[root@mail ~]# ll /var/mailbox/
total 4
drwx------. 3 vmail vmail 4096 Jul 29 07:31 kevin.com
[root@mail ~]# ll /var/mailbox/kevin.com/
total 4
drwx------. 3 vmail vmail 4096 Jul 29 07:31 wangshibo
[root@mail ~]# ll -d /var/mailbox/
drwx------. 3 vmail vmail 4096 Jul 29 07:31 /var/mailbox/

啟動圖形日志服務測試(不做下面一步,后台里的“圖像日志”將不會顯示圖片)

[root@mail ~]# cp -rp /data/web/extman/addon/mailgraph_ext/ /usr/local/
[root@mail ~]# /usr/local/mailgraph_ext/mailgraph-init start
Starting mail statistics grapher: mailgraph_ext
[root@mail ~]# /usr/local/mailgraph_ext/qmonitor-init start
Starting queue statistics grapher: qmonitor

加入開機啟動
[root@mail ~]# echo "/usr/local/mailgraph_ext/mailgraph-init start">>/etc/rc.local
[root@mail ~]# echo "/usr/local/mailgraph_ext/qmonitor-init start">>/etc/rc.local

登錄查看,是否出圖:

使用管理后台里創建的用戶(比如上面的wangshibo)登錄網頁版郵箱。注意登錄時,選中"登錄郵箱"這一選項:

17)cyrus-sasl +courier-authlib+ postfix+ courier-imap+ maildrop聯合配置與調試

完善配置postfix。本例中的main.cf文件下載地址:https://pan.baidu.com/s/176QEclvdvYtG37uQrW_yJg    提取密碼:qi8i     main.cf文件一定要配置正確,否則郵件收發都會出現問題!

注意下面第二行的格式,不能頂格寫。即將前面的注釋打開。
[root@mail ~]# cp /etc/postfix/master.cf /etc/postfix/master.cf.bak
[root@mail ~]# vim /etc/postfix/master.cf
.......
 maildrop  unix  -       n       n       -       -       pipe
   flags=DRhu user=vmail argv=/usr/local/bin/maildrop -d ${recipient}

[root@mail ~]# cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
[root@mail ~]# >/etc/postfix/main.cf 
[root@mail ~]# vim /etc/postfix/main.cf
myhostname = mail.kevin.com

mydomain = kevin.com         #郵箱域名

myorigin = $mydomain         #發件人郵箱域名

inet_interfaces = all

mynetworks = 192.168.0.0/16, 127.0.0.0/8

relay_domains = $mydestination     #給外部郵箱發件時的外部郵箱域名!可以跟多個。注意,此處$mydestination需注釋掉的!
      
alias_maps = $alias_database
alias_database = hash:/etc/aliases

mail_spool_directory = /var/spool/mail

default_recipient_refill_delay = 1s

#====================SASL ESMTP Authenticat=================

smtpd_sasl_auth_enable = yes

#smtpd_helo_required = yes

smtpd_sasl_local_domain = $mydomain

smtpd_sasl_security_options = noanonymous

broken_sasl_auth_clients = yes

smtpd_recipient_restrictions = permit_mynetworks,      #此處下面是一行的,下面要空格!

 permit_sasl_authenticated,

 reject_invalid_hostname,

 reject_non_fqdn_hostname,

 reject_unknown_sender_domain,

 reject_non_fqdn_sender,

 reject_non_fqdn_recipient,

 reject_unknown_recipient_domain,

 reject_unauth_pipelining,

 reject_unauth_destination

smtpd_sasl_application_name = smtpd

#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)

smtpd_banner = $myhostname ESMTP Jobkoo mail system (version:1.0)

#=====================Vritual Mailbox settings=========================

virtual_mailbox_base = /var/mailbox/

virtual_mailbox_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_maps.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql/mysql_virtual_domains_maps.cf

virtual_alias_domains =

virtual_alias_maps = mysql:/etc/postfix/mysql/mysql_virtual_alias_maps.cf

virtual_uid_maps = static:1001

virtual_gid_maps = static:1001

virtual_transport = maildrop

maildrop_destination_recipient_limit = 1

maildrop_destination_concurrency_limit = 1

#====================QUOTA============================================

message_size_limit = 20000000

mailbox_size_limit = 409600000

virtual_mailbox_limit = 20000000

virtual_create_maildirsize = yes

virtual_mailbox_extended = yes

virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_limit_maps.cf

virtual_mailbox_limit_override = yes

virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later.

virtual_overquota_bounce = yes


啟動postfix,啟動前先設置好一些權限
[root@mail ~]# chown -R postfix.postfix /var/lib/postfix
[root@mail ~]# chown -R postfix.postfix /var/spool/postfix
[root@mail ~]# /etc/init.d/postfix start                            
Starting postfix:                                          [  OK  ]
[root@mail ~]# /etc/init.d/postfix status
master (pid 13888) is running...

查看maillog日志,進行檢查(warning信息可以忽略)
[root@mail ~]# tail -f /var/log/maillog                   
[root@mail ~]# postfix check 

驗證courier-authlib認證

首先確認courier-authlib已經運行
[root@mail ~]# service courier-authlib restart
Stopping Courier authentication services: authdaemond
Starting Courier authentication services: authdaemond

使用上面在郵箱管理后台創建的wangshibo用戶進行認證測試(用戶名:wangshibo@kevin.com    密碼:wang@123456)
[root@mail ~]# /usr/local/courier-authlib/sbin/authtest -s loggin wangshibo@kevin.com wang@123456
Authentication succeeded.

     Authenticated: wangshibo@kevin.com  (uid 1001, gid 1001)
    Home Directory: /var/mailbox
           Maildir: kevin.com/wangshibo/Maildir/
             Quota: 524288000SS
Encrypted Password: $1$8qUqeb7z$dqiSMpnZJrpEyBkkC.bP4/
Cleartext Password: wang@123456
           Options: (none)

以上認證信息表明:ExtMan的正確安裝,數據庫也正確導入,courier-authlib能正確連接到mysql數據庫。

下面查看maillog日志看看courier-authlib都做了什么。
[root@mail ~]# tail -f /var/log/maillog
.......
#courier-authlib接到驗證請求類型為login
Jul 29 16:27:31 centos6-03 authdaemond: received auth request, service=loggin, authtype=login

#使用authmysql驗證模塊
Jul 29 16:27:31 centos6-03 authdaemond: authmysql: trying this module

#根據用戶提交的用戶名從數據庫中查詢信息
Jul 29 16:27:31 centos6-03 authdaemond: SQL query: SELECT username, password, "", 1001, 1001, '/var/mailbox', maildir, concat(quota,'S'), name, "" FROM mailbox WHERE username = 'wangshibo@kevin.com'  AND (active='1')

#驗證密碼成功
Jul 29 16:27:31 centos6-03 authdaemond: password matches successfully

#authmysql 模塊獲得查詢結果
Jul 29 16:27:31 centos6-03 authdaemond: authmysql: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname= 王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>

#authmysql 分析clearpasswd字段和passwd字段值
Jul 29 16:27:31 centos6-03 authdaemond: authmysql: clearpasswd=<null>, passwd=$1$8qUqeb7z$dqiSMpnZJrpEyBkkC.bP4/

#通過驗證顯示用戶信息
Jul 29 16:27:31 centos6-03 authdaemond: Authenticated: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname= 王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>

#驗證成功顯示用戶密碼信息
Jul 29 16:27:31 centos6-03 authdaemond: Authenticated: clearpasswd=wang@123456, passwd=$1$8qUqeb7z$dqiSMpnZJrpEyBkkC.bP4/


特別注意:
由於我的courier-authlib驗證服務將調試信息打開了,因此可以在maillog文件中查看這么詳細的日志信息。
配置文件中(/usr/local/courier-authlib/etc/authlib/authdaemonrc)關於調試信息的設置分為三個級別,分別為:(我的文件里面是DEBUG_LOGIN=2)
0 → 關閉調試信息
1 → 打開調試信息
2 → 打開調試信息並顯示密碼

測試SMTP認證

首先隨便測試一個賬號,生成存放client的文件,默認的位置在 /etc/sasldb2(這個文件很重要,否則會造成認證失敗)
[root@mail ~]# /usr/local/sbin/saslpasswd2 -c -u mail.kevin.com bobo
Password:                           #輸入兩次密碼
Again (for verification):
[root@mail ~]# chmod 777 /etc/sasldb2
[root@mail ~]# ll /etc/sasldb2
-rwxrwxrwx. 1 root root 12288 Jul 29 17:24 /etc/sasldb2
 
通過以下命令獲得wangshibo@kevin.com的用戶名及密碼的BASE64編碼:(分別是用戶名和密碼)
[root@mail ~]# perl -e 'use MIME::Base64; print encode_base64("wangshibo\@kevin.com")'
d2FuZ3NoaWJvQGtldmluLmNvbQ==
[root@mail ~]# perl -e 'use MIME::Base64; print encode_base64("wang@123456")'
aHVpMTk4NzUyMQ==
 
 
然后本機測試,其過程如下
[root@mail ~]# telnet localhost 25
Trying localhost...
Connected to localhost.
Escape character is '^]'.
220 mail.kevin.com ESMTP Jobkoo mail system (version:1.0)
ehlo mail.kevin.com              #輸入這個域名
250-mail.kevin.com
250-PIPELINING
250-SIZE 20000000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login                       #輸入登錄口令
334 VXNlcm5hbWU6
d2FuZ3NoaWJvQGtldmluLmNvbQ==     #輸入用戶名的編碼
334 UGFzc3dvcmQ6
aHVpMTk4NzUyMQ==                 #輸入密碼的編碼
 
235 2.7.0 Authentication successful
quit
221 2.0.0 Bye
Connection closed by foreign host.
 
 
如上,最后出現235 2.7.0 Authentication successful,表示認證成功了!

下面查看maillog日志看看:
[root@mail ~]# tail -f /var/log/maillog
.......
Jul 30 11:16:25 centos6-03 authdaemond: received auth request, service=smtp, authtype=login
Jul 30 11:16:25 centos6-03 authdaemond: authmysql: trying this module
Jul 30 11:16:25 centos6-03 authdaemond: authmysqllib: connected. Versions: header 50639, client 50639, server 50639
Jul 30 11:16:25 centos6-03 authdaemond: SQL query: SELECT username, password, "", 1001, 1001, '/var/mailbox', maildir, concat(quota,'S'), name, "" FROM mailbox WHERE username = 'wangshibo@kevin.com'  AND (active='1')
Jul 30 11:16:25 centos6-03 authdaemond: password matches successfully
Jul 30 11:16:25 centos6-03 authdaemond: authmysql: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname=王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>
Jul 30 11:16:25 centos6-03 authdaemond: authmysql: clearpasswd=<null>, passwd=$1$4o9ugUuB$kbzKQ6VTl03aHDfFQqHIy0
Jul 30 11:16:25 centos6-03 authdaemond: Authenticated: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname=王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>
Jul 30 11:16:25 centos6-03 authdaemond: Authenticated: clearpasswd=wang@123456, passwd=$1$4o9ugUuB$kbzKQ6VTl03aHDfFQqHIy0
Jul 30 11:16:49 centos6-03 postfix/smtpd[101444]: disconnect from localhost[127.0.0.1]

測試POP3

[root@mail ~]# telnet 192.168.10.202 110
Trying 192.168.10.202...
Connected to 192.168.10.202.
Escape character is '^]'.
+OK Hello there.
user wangshibo@kevin.com            #輸入用戶名
+OK Password required.
pass wang@123456                    #輸入密碼
+OK logged in.
list                                #輸入list
+OK POP3 clients that break here, they violate STD53.
.
quit                                #退出
+OK Bye-bye.
Connection closed by foreign host.


查看maillog日志
[root@mail postfix]# tail -f /var/log/maillog 
.......
#連接pop3 端口
Jul 29 17:42:13 centos6-03 pop3d: Connection, ip=[::ffff:192.168.10.202]

#需要驗證,服務類型為pop3
Jul 29 17:42:26 centos6-03 authdaemond: received auth request, service=pop3, authtype=login

#使用authmysql模塊認證
Jul 29 17:42:26 centos6-03 authdaemond: authmysql: trying this module

#與mysql連接獲得認證信息
Jul 29 17:42:26 centos6-03 authdaemond: authmysqllib: connected. Versions: header 50639, client 50639, server 50639
Jul 29 17:42:26 centos6-03 authdaemond: SQL query: SELECT username, password, "", 1001, 1001, '/var/mailbox', maildir, concat(quota,'S'), name, "" FROM mailbox WHERE username = 'wangshibo@kevin.com'  AND (active='1')
Jul 29 17:42:26 centos6-03 authdaemond: password matches successfully
Jul 29 17:42:26 centos6-03 authdaemond: authmysql: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname= 王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>
Jul 29 17:42:26 centos6-03 authdaemond: authmysql: clearpasswd=<null>, passwd=$1$8qUqeb7z$dqiSMpnZJrpEyBkkC.bP4/
Jul 29 17:42:26 centos6-03 authdaemond: Authenticated: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=wangshibo@kevin.com, fullname= 王士博, maildir=kevin.com/wangshibo/Maildir/, quota=524288000SS, options=<null>
Jul 29 17:42:26 centos6-03 authdaemond: Authenticated: clearpasswd=wang@123456, passwd=$1$8qUqeb7z$dqiSMpnZJrpEyBkkC.bP4/

#=============== 通過認證 ================
#通過pop3登陸
Jul 29 17:42:26 centos6-03 pop3d: LOGIN, user=wangshibo@kevin.com, ip=[::ffff:192.168.10.202], port=[44178]

#退出pop3
Jul 29 17:42:35 centos6-03 pop3d: LOGOUT, user=wangshibo@kevin.com, ip=[::ffff:192.168.10.202], port=[44178], top=0, retr=0, rcvd=12, sent=88, time=9


現在使用root用戶手動給wangshibo發2封郵件(可以邊查郵件日志/var/log/maillog邊排錯!如果日志里沒有什么錯誤,就去extmail的郵箱里去查看郵件)
[root@mail ~]# mail -s 'hello' -v wangshibo@kevin.com <~/.bashrc
[root@mail ~]# mail -s 'hello' -v wangshibo@kevin.com <~/.bashrc

查看maillog日志,看看有沒有報錯信息
[root@mail ~]# tail -f /var/log/maillog

[root@mail ~]# telnet 192.168.10.202 110
Trying 192.168.10.202...
Connected to 192.168.10.202.
Escape character is '^]'.
+OK Hello there.
user wangshibo@kevin.com            #輸入用戶名
+OK Password required.
pass wang@123456                    #輸入密碼
+OK logged in.
list                                #輸入list
+OK POP3 clients that break here, they violate STD53.
1 653                               #說明已經發現有一封郵件了。1代表第一封郵件,653為郵件大小
2 1849

quit                                #退出
+OK Bye-bye.
Connection closed by foreign host.

使用wangshibo用戶登錄extmail可以收到兩封郵件

 登錄extman管理后台,多創建幾個基於kevin.com域名的郵箱,相互之間就能正常收發郵件了!

18)通過MUA測試收發郵件
本次MUA軟件使用Foxmail,測試用戶為wangshibo@kevin.com。需要注意的是kevin.com郵箱域名解析時,解析要包括:mail、smtp、pop3、imap的A記錄以及MX記錄

打開Foxmail客戶端,創建新賬號,如下圖:

===============針對外部郵箱收發郵件的設置=================

按照上面的配置后,發現給外部郵箱發送和接收郵件有問題,/etc/postfix/main.cf文件修改如下:
[root@mail ~]# cat /etc/postfix/main.cf
myhostname = mail.kevin.com
  
mydomain = kevin.com
  
myorigin = $mydomain
  
inet_interfaces = all
  
mynetworks = 192.168.0.0/16, 127.0.0.0/8
  
relay_domains = $mydestination, grace.com
        
alias_maps = $alias_database
alias_database = hash:/etc/aliases
  
mail_spool_directory = /var/spool/mail
  
default_recipient_refill_delay = 1s
  
#====================SASL ESMTP Authenticat=================
  
smtpd_sasl_auth_enable = yes
  
#smtpd_helo_required = yes
  
smtpd_sasl_local_domain = $mydomain
  
smtpd_sasl_security_options = noanonymous
  
broken_sasl_auth_clients = yes
  
smtpd_recipient_restrictions = permit_mynetworks,
  
 permit_sasl_authenticated,
  
 reject_unauth_destination
  
smtpd_sasl_application_name = smtpd
  
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
  
smtpd_banner = $myhostname ESMTP Jobkoo mail system (version:1.0)
  
#=====================Vritual Mailbox settings=========================
  
virtual_mailbox_base = /var/mailbox/
  
virtual_mailbox_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_maps.cf
  
virtual_mailbox_domains = mysql:/etc/postfix/mysql/mysql_virtual_domains_maps.cf
  
virtual_alias_domains =
  
virtual_alias_maps = mysql:/etc/postfix/mysql/mysql_virtual_alias_maps.cf
  
virtual_uid_maps = static:1001
  
virtual_gid_maps = static:1001
  
virtual_transport = maildrop
  
maildrop_destination_recipient_limit = 1
  
maildrop_destination_concurrency_limit = 1
  
#====================QUOTA============================================
  
message_size_limit = 20000000
  
mailbox_size_limit = 409600000
  
virtual_mailbox_limit = 20000000
  
virtual_create_maildirsize = yes
  
virtual_mailbox_extended = yes
  
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_limit_maps.cf
  
virtual_mailbox_limit_override = yes
  
virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later.
  
virtual_overquota_bounce = yes
 
需要注意的是:
上面的main.cf文件配置相比於之前的配置,改動的地方:
1)relay_domains這一項追加了外部郵箱的域名,比如這個要發送或接收grace.com域名的郵件。
2)添加的外部郵箱地址,要保證postfix部署機能和外部郵箱地址通信!如果不能通信,則不能正常收發外部郵件!
3)smtp認證部分的smtpd_recipient_restrictions這一項只保留三個參數配置。
 
另外,由於grace.com域名也是公司內部域名,還需要在其正向解析配置中添加郵件的MX記錄,否則會出現發送郵件失敗的情況!
如下,192.168.10.29和192.168.10.27是負責解析grace.com域名的兩台DNS服務器地址;192.168.61.201是mail.grace.com郵箱服務器地址。
[root@mail ~]# cat /var/named/grace.com_zone
$TTL 600
@       IN SOA  ns.grace.com. root. (
                        2017071114
                         2H           
                         10M           
                         7D           
                         1D )
@                           IN      NS      ns1.graces.com.           
@                           IN      NS      ns2.grace.com.           
ns1                         IN      A       192.168.10.29
ns2                         IN      A       192.168.10.27
mail                        IN      A       192.168.61.201      
smtp                        IN      A       192.168.61.201
pop3                        IN      A       192.168.61.201
imap                        IN      A       192.168.61.201      
@                           IN      MX 10   mail.grace.com.
 
[root@mail ~]# cat /var/named/192.168.61.zone
$TTL 600
@       IN SOA  ns1.grace.com. root. (
                        2017071101
                         2H         
                         10M         
                         7D         
                         1D )
@               IN      NS      ns1.grace.com.
@               IN      NS      ns2.grace.com.
201             IN      PTR     smtp.grace.com.
201             IN      PTR     pop3.grace.com.
201             IN      PTR    mail.grace.com.
201             IN      PTR    imap.grace.com.
 
經過上面的調整后,kevin.com域名郵箱和grace.com域名郵箱就可以相互之間發送和接收郵件了!!!

19)郵件發垃圾和反病毒
殺毒軟件

殺毒軟件選用clamav,安裝clamav
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf clamav-0.94.1.tar.gz
[root@mail src]# cd clamav-0.94.1
[root@mail clamav-0.94.1]# ./configure --prefix=/usr/local/clamav --with-dbdir=/usr/local/share/clamav
[root@mail clamav-0.94.1]# make && make install

說明:
--with-dbdir 參數指定病毒庫位置,這個路徑在clamav的配置文件中也有出現。當指定了這個參數之后,編譯安裝后悔自動創建這個目錄。

配置clamav
Clamav有2個配置文件,一個主配置文件/usr/local/clamav/etc/clamd.conf,一個病毒更新配置文件/usr/local/clamav/etc/freshclam.conf。
下面分別進行配置:
[root@mail clamav-0.94.1]# cp /usr/local/clamav/etc/clamd.conf /usr/local/clamav/etc/clamd.conf.bak
[root@mail clamav-0.94.1]# vim /usr/local/clamav/etc/clamd.conf            #清空原文件,直接復制下面內容
# 將example注掉
# example

LogFile /var/log/clamd.log

LogSyslog yes

LogVerbose yes

PidFile /var/run/clamav/clamd.pid

TemporaryDirectory /dev/shm/clamav/tmp

DatabaseDirectory /usr/local/share/clamav

LocalSocket /tmp/clamd.socket

StreamMaxLength 20M #附件大小,超過20M不掃描

User amavis

ScanMail yes

ScanArchive yes

[root@mail clamav-0.94.1]# cp /usr/local/clamav/etc/freshclam.conf /usr/local/clamav/etc/freshclam.conf.bak
[root@mail clamav-0.94.1]# vim /usr/local/clamav/etc/freshclam.conf        #清空原文件,直接復制下面內容到文件中
# 將example注掉
# example

DatabaseDirectory /usr/local/share/clamav

UpdateLogFile /var/log/freshclam.log

PidFile /var/run/clamav/freshclam.pid

DatabaseOwner amavis

DatabaseMirror db.CN.clamav.net

DatabaseMirror database.clamav.net

說明:
在上面兩個配置文件中,都有用戶的設置,這里都設置成了amavis 。為什么不用默認的用戶clamav而使用amavis呢?這樣做的目的是為了與amavis-new結合在一起。
配置中的socker是殺毒程序的socket文件位置,后面與amavis-new結合使用的時候配置amavis-new需要指定這個文件。

修改相應目錄權限
[root@mail clamav-0.94.1]# chown -R amavis.amavis /usr/local/share/clamav
[root@mail clamav-0.94.1]# chown -R amavis.amavis /dev/shm/clamav/
[root@mail clamav-0.94.1]# touch /var/log/freshclam.log
[root@mail clamav-0.94.1]# chown amavis.amavis /var/log/freshclam.log
[root@mail clamav-0.94.1]# touch /var/log/clamd.log
[root@mail clamav-0.94.1]# chown amavis.amavis /var/log/clamd.log
[root@mail clamav-0.94.1]# mkdir -p /var/run/clamav/
[root@mail clamav-0.94.1]# chown amavis.amavis /var/run/clamav/ -R

手動更新病毒庫並運行殺毒程序(啟動后,執行ps -ef|grep clamd,確保/tmp/clamd.socket存在)
[root@mail clamav-0.94.1]# /usr/local/clamav/bin/freshclam --daemon
[root@mail clamav-0.94.1]# /usr/local/clamav/sbin/clamd
LibClamAV Warning: **************************************************
LibClamAV Warning: ***  The virus database is older than 7 days!  ***
LibClamAV Warning: ***   Please update it as soon as possible.    ***
LibClamAV Warning: **************************************************
[root@mail clamav-0.94.1]# 

將病毒庫升級和殺毒程序設置為隨系統啟動
[root@mail clamav-0.94.1]# echo "/usr/local/clamav/bin/freshclam --daemon" >> /etc/rc.local
[root@mail clamav-0.94.1]# echo "/usr/local/clamav/sbin/clamd" >> /etc/rc.local

郵件病毒掃描與垃圾郵件過濾

amavisd-new程序是提供postfix郵件掃描殺毒的,因此需要連接clamav殺毒軟件和postfix程序。

安裝
[root@mail ~]# cd /usr/local/src/
[root@mail src]# tar -zvxf amavisd-new-2.6.4.tar.gz 
[root@mail src]# cd amavisd-new-2.6.4
[root@mail amavisd-new-2.6.4]# mkdir -p /var/amavis/{tmp,var,db}
[root@mail amavisd-new-2.6.4]# chown -R amavis.amavis /var/amavis
[root@mail amavisd-new-2.6.4]# chmod -R 750 /var/amavis
[root@mail amavisd-new-2.6.4]# cp amavisd /usr/local/sbin/
[root@mail amavisd-new-2.6.4]# chown root /usr/local/sbin/amavisd
[root@mail amavisd-new-2.6.4]# chmod 755 /usr/local/sbin/amavisd
[root@mail amavisd-new-2.6.4]# cp amavisd.conf /etc/
[root@mail amavisd-new-2.6.4]# chown root /etc/amavisd.conf
[root@mail amavisd-new-2.6.4]# chmod 644 /etc/amavisd.conf
[root@mail amavisd-new-2.6.4]# mkdir /var/virusmails
[root@mail amavisd-new-2.6.4]# chown amavis.amavis /var/virusmails
[root@mail amavisd-new-2.6.4]# chmod 750 /var/virusmails

配置Amavisd與Clamav結合
[root@mail amavisd-new-2.6.4]# cp /etc/amavisd.conf /etc/amavisd.conf.bak
[root@mail amavisd-new-2.6.4]# vim /etc/amavisd.conf
$max_servers=8;
$daemon_user = 'amavis';

$daemon_group = 'amavis';

$mydomain = 'kevin.com';

$db_home = "$MYHOME/db";

$inet_socket_port = 10024;

$sa_tag_level_deflt = 5.0;

$sa_tag2_level_deflt = 6.2;

$sa_kill_level_deflt = $sa_tag2_level_deflt;

$virus_admin = "virusalert@$mydomain";

$sa_spam_subject_tag = '***SPAM*** ';

$forward_method = 'smtp:127.0.0.1:10025';

$notify_method = $forward_method;

$final_virus_destiny = D_DISCARD;

$final_banned_destiny = D_DISCARD;

$final_spam_destiny = D_PASS;

['ClamAV-clamd',

&ask_daemon, ["CONTSCAN {}n", "/tmp/clamd.socket"],

qr/bOK$/, qr/bFOUND$/,

qr/^.*?: (?!Infected Archive)(.*) FOUND$/ ],

# 在154行左右,修改投遞/攔截的方法:

$final_virus_destiny = D_DISCARD;

$final_banned_destiny = D_BOUNCE;

$final_spam_destiny = D_PASS;

$final_bad_header_destiny = D_PASS;


需要注意:
1)配置文件編寫完成后使用執行/usr/local/sbin/amavisd debug 進行調試。如果發現缺少perl組件就利用yum search 查找相關組件,
   最后用yum 安裝,這樣來的方便些!
2)配置中的socket部分是clamav 殺毒軟件的socket文件,這個文件的位置是在配置clamav的時候指定的,在這里配置amavis時一定要指定正確!
3)最后四行部分是amavisd運行時打開的端口號
4)forward_method部分是與postfix連接時需要連接的地址和端口號。這個地址是在postfix的master.cf文件中指定的。后文會介紹。
5)最后4個設置中,對spam(垃圾郵件)默認會直接反彈(BOUNCE),現在修改為繼續投遞(PASS)但在信頭中增加相關X-Spam- Status信息等。
   這樣可以很方便的在extmail中設置將被標記為垃圾郵件的mail投遞到"垃圾郵件箱"中,便於用戶翻查。


垃圾郵件過濾服務spamassassin安裝配置
[root@mail ~]# cd /usr/local/src/
[root@mail src]# wget http://archive.apache.org/dist/spamassassin/Mail-SpamAssassin-3.0.0.tar.gz
[root@mail src]# tar -zvxf Mail-SpamAssassin-3.0.0.tar.gz 
[root@mail src]# cd Mail-SpamAssassin-3.0.0
[root@mail Mail-SpamAssassin-3.0.0]# perl Makefile.PL             #一路回車,默認配置即可
[root@mail Mail-SpamAssassin-3.0.0]# make && make install

mail:: spamassassin配置
[root@mail Mail-SpamAssassin-3.0.0]# cp /etc/mail/spamassassin/local.cf /etc/mail/spamassassin/local.cf.bak
[root@mail Mail-SpamAssassin-3.0.0]# vim /etc/mail/spamassassin/local.cf     
#required_hits 5
#report_safe 0
#rewrite_header Subject [SPAM]
required_hits 5
use_bayes 1
bayes_auto_learn 1
skip_rbl_checks 0
use_razor2 1
use_pyzor 0

啟動spamassassin 並加入到系統啟動
[root@mail Mail-SpamAssassin-3.0.0]# /usr/local/bin/spamd --daemonize --pidfile /var/run/spamd.pid
[root@mail Mail-SpamAssassin-3.0.0]# echo "/usr/local/bin/spamd --daemonize --pidfile /var/run/spamd.pid" >> /etc/rc.local 

啟動amavisd並加入到系統啟動
[root@mail Mail-SpamAssassin-3.0.0]# /usr/local/sbin/amavisd start
[root@mail Mail-SpamAssassin-3.0.0]# echo "/usr/local/sbin/amavisd start ">>/etc/rc.local

配置Postfix 集成amavisd-new(注意下面的-o內容不能頂格寫,要空格,否則postfix服務會啟動失敗,通過/var/log/maillog日志能看出啟動信息)
[root@mail Mail-SpamAssassin-3.0.0]# cp /etc/postfix/master.cf /etc/postfix/master.cf.bak2
[root@mail Mail-SpamAssassin-3.0.0]# vim /etc/postfix/master.cf
127.0.0.1:10025 inet n - n - - smtpd
  -o content_filter=

  -o local_recipient_maps=

  -o relay_recipient_maps=

  -o smtpd_restriction_classes=

  -o smtpd_client_restrictions=

  -o smtpd_helo_restrictions=

  -o smtpd_sender_restrictions=

  -o smtpd_recipient_restrictions=permit_mynetworks,reject

  -o mynetworks=127.0.0.0/8

  -o strict_rfc821_envelopes=yes

  -o smtpd_error_sleep_time=0

  -o smtpd_soft_error_limit=1001

  -o smtpd_hard_error_limit=1000

  -o receive_override_options=


編輯/etc/postfix/main.cf
[root@mail Mail-SpamAssassin-3.0.0]# cp /etc/postfix/main.cf /etc/postfix/main.cf.bak2
[root@mail Mail-SpamAssassin-3.0.0]# vim /etc/postfix/main.cf
#在最后添加
# Content-Filter
content_filter = smtp:[127.0.0.1]:10024

receive_override_options = no_address_mappings


需要注意:
1)這里content_filter 選項指定的smtp:[127.0.0.1]:10024 端口正是配置amavisd-new時所指定的。
2)receive_override_options 這里必須增加,禁止地址展開/影射,否則如果遇到別名的時候會引起冗余郵件的產生。

重啟postfix
[root@mail Mail-SpamAssassin-3.0.0]# /etc/init.d/postfix restart
Shutting down postfix:                                     [  OK  ]
Starting postfix:                                          [  OK  ]

測試Clamav

先登錄extmail界面創建bobo@kevin.com賬號,利用wangshibo@kevin.com賬號給bobo@kevin.com賬號發郵件,因為遇到郵件中有病毒或垃圾郵件的話,
postfix會給wangshibo@kevin.com發一封郵件。

利用telnet測試25端口,過程如下:

[root@mail ~]# telnet localhost 25
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.kevin.com ESMTP Jobkoo mail system (version:1.0)
mail from:<wangshibo@kevin.com>                                          #輸入發件郵箱             
250 2.1.0 Ok
rcpt to:<bobo@kevin.com>                                                 #輸入目標郵箱
250 2.1.5 Ok
data                                                                     #輸入這個
354 End data with <CR><LF>.<CR><LF>
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*     #輸入這個
.                                                                        #輸入這個
250 2.0.0 Ok: queued as DD4A3A0673
quit                                                                     #輸入這個
221 2.0.0 Bye
Connection closed by foreign host.


查看郵件日志
[root@mail ~]# tail -f /var/log/maillog
.......
Jul 30 02:13:30 centos6-03 amavis[9707]: (09707-02) Passed CLEAN, [127.0.0.1] [127.0.0.1] <wangshibo@kevin.com> -> <bobo@kevin.com>, Message-ID: <20180729181322.70E8CA0681@mail.kevin.com>, mail_id: xvp4rzGLpwmT, Hits: -, size: 365, queued_as: 4A837A0689, 196 ms
.......
Jul 30 02:13:30 centos6-03 authdaemond: received userid lookup request: bobo@kevin.com
Jul 30 02:13:30 centos6-03 authdaemond: authmysql: trying this module
Jul 30 02:13:30 centos6-03 authdaemond: SQL query: SELECT username, password, "", 1001, 1001, '/var/mailbox', maildir, concat(quota,'S'), name, "" FROM mailbox WHERE username = 'bobo@kevin.com'  AND (active='1')
Jul 30 02:13:30 centos6-03 authdaemond: Authenticated: sysusername=<null>, sysuserid=1001, sysgroupid=1001, homedir=/var/mailbox, address=bobo@kevin.com, fullname=bobo, maildir=kevin.com/bobo/Maildir/, quota=524288000SS, options=<null>
Jul 30 02:13:30 centos6-03 authdaemond: Authenticated: clearpasswd=<null>, passwd=$1$Gj0qfSAN$iHQjS0BeNfbeArKL1MQ8D0
Jul 30 02:13:30 centos6-03 postfix/smtp[19451]: 70E8CA0681: to=<bobo@kevin.com>, relay=127.0.0.1[127.0.0.1]:10024, delay=8.6, delays=8.4/0/0/0.2, dsn=2.0.0, status=sent (250 2.0.0 Ok, id=09707-02, from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 4A837A0689)

說明:
從日志中可以清楚的看到,剛剛測試的郵件被amavis捕捉到了,上面maillog中第一部分信息表示被amavis攔截了。這時郵件經過postfix打包,發給了bobo@kevin.com
這個賬戶;從第二部分日志信息中可以很清楚的看到。通過extmail登陸bobo@kevin.com后可以看到警報郵件的內容,如下圖:

最終測試

現在通過簡單的email收發來確認系統是否正常。

發送測試郵件
輸入以下命令,通過telnet方式給wangshibo@kevin.com發送一封測試信件
[root@mail ~]# telnet localhost 25
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.kevin.com ESMTP Jobkoo mail system (version:1.0)
mail from:<wangshibo@kevin.com>                              #輸入這個
250 2.1.0 Ok
rcpt to:<bobo@kevin.com>                                     #輸入這個
250 2.1.5 Ok
data                                                         #輸入這個
354 End data with <CR><LF>.<CR><LF>                          
Subject: Hello World                                         #輸入這個
Hello World!                                                 #輸入這個
.                                                            #輸入這個
250 2.0.0 Ok: queued as 23EC7A0681
quit                                                         #輸入這個
221 2.0.0 Bye
Connection closed by foreign host.


此時通過另一個命令行窗口,觀察maillog,將看到如下信息:
[root@mail clamav-0.94.1]# tail -f /var/log/maillog
........
Jul 30 02:21:37 centos6-03 amavis[9708]: (09708-01) Passed CLEAN, [127.0.0.1] [127.0.0.1] <wangshibo@kevin.com> -> <bobo@kevin.com>, Message-ID: <20180729182117.23EC7A0681@mail.kevin.com>, mail_id: q8itRecEZJag, Hits: -, size: 332, queued_as: 5D4BBA068D, 1239 ms
Jul 30 02:21:37 centos6-03 postfix/smtp[19467]: 23EC7A0681: to=<bobo@kevin.com>, relay=127.0.0.1[127.0.0.1]:10024, delay=26, delays=25/0/0/1.2, dsn=2.0.0, status=sent (250 2.0.0 Ok, id=09708-01, from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 5D4BBA068D)
Jul 30 02:21:37 centos6-03 authdaemond: received userid lookup request: bobo@kevin.com
Jul 30 02:21:37 centos6-03 authdaemond: authmysql: trying this module
Jul 30 02:21:37 centos6-03 postfix/qmgr[10803]: 23EC7A0681: removed
.......


說明:
上述日志表示郵件經過了amavisd-new的掃描,並且已經通過maildrop投遞到用戶的maildir了,黃色字體表面通過檢查。

20)啟動服務項及進程整理

courier-authlib
[root@mail ~]# chkconfig courier-authlib on
[root@mail ~]# service courier-authlib start

postfix
[root@mail ~]# chkconfig --list postfix
[root@mail ~]# service postfix start

imapd
[root@mail ~]# chkconfig imapd on
[root@mail ~]# service imapd start

extmail 圖形監控
加入啟動項
[root@mail ~]# echo "/usr/local/mailgraph_ext/mailgraph-init start">>/etc/rc.local
[root@mail ~]# echo "/usr/local/mailgraph_ext/qmonitor-init start">>/etc/rc.local

啟動
/usr/local/mailgraph_ext/mailgraph-init start
/usr/local/mailgraph_ext/qmonitor-init start

clamav
加入啟動項
[root@mail ~]# echo "/usr/local/clamav/bin/freshclam --daemon" >> /etc/rc.local
[root@mail ~]# echo "/usr/local/clamav/sbin/clamd" >> /etc/rc.local

啟動
[root@mail ~]# /usr/local/clamav/bin/freshclam --daemon
[root@mail ~]# /usr/local/clamav/sbin/clamd

amavisd-new
[root@mail ~]# /usr/local/sbin/amavisd start
[root@mail ~]# echo "/usr/local/sbin/amavisd start ">>/etc/rc.local

啟動spamassassin 並加入到系統啟動
[root@mail ~]# /usr/bin/spamd --daemonize --pidfile /var/run/spamd.pid
[root@mail ~]# echo "/usr/bin/spamd --daemonize --pidfile /var/run/spamd.pid" >>/etc/rc.local

21)郵件服務器壓力測試-利用Postal工具

postal下載:http://doc.coker.com.au/projects/postal/

postal安裝
[root@mail ~]# tar -zxvf postal7.tgz
[root@mail ~]# cd postal7
[root@mail postal7]# ./configure
[root@mail postal7]# make
[root@mail postal7]# make install
[root@mail postal7]# make clean

壓力測試(maillist.txt文件中放入大量的郵件賬號,進行壓力測試)
[root@mail ~]# postal -m 100 -p 25 192.168.10.202 maillist.txt

================登錄extman的時候,報錯===============

Can't open /tmp/extman//sid_cdbff585272d01da9209e113acb4d90f, No such file or directory (2012-06-08 15:19:42)轉載▼

解決辦法:
[root@mail ~]# mkdir /tmp/extman
[root@mail ~]# chown -R postfix:postfix /tmp/extman
[root@mail ~]# chmod -R 777 /tmp/extman

可以寫一個腳本, 檢查/tmp下是否有extman目錄, 如果沒有, 就創建該目錄並授權, 腳本內容如下:
[root@mail ~]# cat /opt/tmp_monit_extman.sh 
#!/bin/bash

ls -l -d /tmp/extman

if [ $? -ne 0 ];then
    /bin/mkdir /tmp/extman
    /bin/chown -R postfix:postfix /tmp/extman
    /bin/chmod -R 777 /tmp/extman
else
   echo "it is ok" >/dev/null 2>&1
fi


添加腳本執行權限, 並設置計划任務, 每一分鍾執行一次
[root@mail ~]# chmod 755  /opt/tmp_monit_extman.sh
[root@mail ~]# crontab -l
* * * * * /bin/bash -x /opt/tmp_monit_extman.sh >/dev/null 2>&1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM