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

 

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

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) 協議將本身的信件保留在郵件主機上面, 並進一步創建郵件數據匣等進階工做。html

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

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

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

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

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

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

請注意:POP3/IMAP和SMTP能夠組建在不一樣的服務器上,常用MUA的用戶確定記得軟件的設置中常常將POP3/IMAP和SMTP進行分開設置。數據庫

——————————SMTP、POP三、IPMAP三者說明——————————apache

簡單來講: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的是另外一套軟件。但郵件系統中的每一種角色,都有許多不一樣的軟件能夠選擇。

2、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、pop三、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
相關文章
相關標籤/搜索