本文源出處:http://www.lustlost.com/?p=180html
郵件系統能夠算是目前比較麻煩的互聯網應用。因爲各類垃圾郵件的橫行,ISP和其餘的郵箱提供者對郵件都有着不少限制。要玩轉郵件系統,對於DNS系統,openrelay,網絡黑名單,垃圾郵件過慮等各類規則都須要有必定程度的瞭解。 mysql
這是一副經典的郵件收發的流程圖,清晰的描述了整個郵件收發的過程linux
總體架構簡介web
本文基於postfix+cyrus-sasl+extmail+mysql來架設一個功能較爲完整的企業級郵件系統。sql
筆者水平有限,有不對之處還請路過的高手不吝賜教數據庫
在MTA系統中,sendmaill因爲其苦澀的配置文件,安全性,效率。。。更多的缺點就不一一道來了。。。如今用的比較多的MTA系統仍是當屬改良sendmail以後的postfixapache
而postfix是不支持認證功能的,須要藉助saslauthd 認證框架,結合pam的認證功能實現用戶的認證。瀏覽器
這裏藉助cyrus-sasl來進行用戶的認證。 cyrus-sasl自己不支持mysql和ldap的,要結合mysql須要還藉助courier-authlib庫來實現mysql的虛擬用戶認證。安全
最後用extmail這個開源免費的webmail來作一個網頁接口,讓郵件系統可使用web的方式訪問 bash
整個實現的流程圖
實現過程
step1:環境安裝
首先要安裝整個開發環境,此外postfix和其它組件還須要依賴如下幾個包 tcl,tcl-devel,libart_lgpl,libart_lgpl-devel,libtool-ltdl,libtool-ltdl-devel,expect
爲extmail實現服務還須要安裝web服務,這裏使用apache 這些包用yum安裝便可
[root@mail ~]# yum install tcl tcl-devel libart_lgpl libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect httpd –y
step2:安裝mysql
因爲在編譯安裝postfix的時候要指定mysql的路徑,因此先安裝好mysql,這裏用綠色安裝的方式安裝mysql
添加mysql用戶和用戶組
[root@mail ~]# groupadd -r mysql
[root@mail ~]# useradd -r -d /mysql/data -s /sbin/nologin -g mysql -M mysql
解壓安裝mysql
[root@mail ~]# tar xf mysql-5.5.22-linux2.6-i686.tar.gz -C /usr/local/
[root@mail local]# ln -sv mysql-5.5.22-linux2.6-i686 mysql
[root@mail mysql]# scripts/mysql_install_db --user=mysql --datadir=/mysql/data
在此以前不要忘記建立數據目錄並把所屬用戶和用戶組改成mysql 複製啓動腳本和配置文件
[root@mail mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@mail mysql]# cp support-files/my-large.cnf /etc/my.cnf 加載頭文件和庫文件(關鍵,安裝postfix須要讀取相關庫文件)
[root@mail mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
[root@mail mysql]# echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf 添加mysql的PATH命令路徑
[root@mail mysql]# vi /etc/profile PATH=$PATH:/usr/local/mysql/bin
[root@mail mysql]# . /etc/profile
step3:安裝postfix 添加對應用戶
[root@mail ~]# groupadd -g 1024 postfix
[root@mail ~]# useradd -g postfix -u 1024 -s /sbin/nologin -M postfix
[root@mail ~]# groupadd -g 1025 postdrop
[root@mail ~]# useradd -g postdrop -u 1025 -s /sbin/nologin -M postdrop
解壓編譯安裝 安裝以前確保安裝開發包時已經安裝cyrus-sasl相關包
[root@mail ~]# tar xf postfix-2.9.1.tar.gz
[root@mail ~]# cd postfix-2.9.1
[root@mail postfix-2.9.1]# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2 -lssl –lcrypto'
[root@mail postfix-2.9.1]# make
[root@mail postfix-2.9.1]# make install
安裝的時候會要求輸入相關選項 通常按照默認就好了,
html_directory: [no] /var/www/html/postfix 此選項能夠指定apache的網頁路徑,能夠經過web查看postfix幫助文檔
添加SysV服務腳本
#!/bin/bash
#
# 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
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ $NETWORKING = "no" ] && exit 3
[ -x /usr/sbin/postfix ] || exit 4
[ -d /etc/postfix ] || exit 5
[ -d /var/spool/postfix ] || exit 6
RETVAL=0
prog=」postfix」
start() {
# Start daemons.
echo -n $」Starting postfix: 」
/usr/bin/newaliases >/dev/null 2>&1
/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $」$prog start」
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 $」$prog stop」
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 $」$prog reload」
RETVAL=$?
echo
return $RETVAL
}
abort() {
/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $」$prog abort」
return $?
}
flush() {
/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $」$prog flush」
return $?
}
check() {
/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $」$prog check」
return $?
}
restart() {
stop
start
}
# See how we were called.
case 「$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
abort)
abort
;;
flush)
flush
;;
check)
check
;;
status)
status master
;;
condrestart)
[ -f /var/lock/subsys/postfix ] && restart || :
;;
*)
echo $」Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}」
exit 1
esac
exit $?
step4:構建DNS服務
爲了方便我就直接在本機安裝dns服務了
[root@mail ~]# yum install bind –y
具體的dns配置步驟再也不列出,這裏給出的我配置
name.conf
lustlost.com.zone
172.16.zone
修改resolv.conf ,dns服務器爲本機 測試下MX解析
step6:安裝dovecot做爲MRA
[root@mail ~]# yum install dovecot –y
編輯/etc/dovecot.conf 修改protocol = imap pop3 service dovecot start 就OK了
step7:整合SASL實現虛擬用戶認證
postfix自己沒有SASL模塊,須要整合cyrus-sasl來實現認證,而cyrus-sasl對mysql的認證功能比較簡陋,因此還須要藉助courier-authlib庫和mysql交互。
而dovecot能夠直接和mysql交互,而不須要藉助於cyrus-sasl
總體的流程在本文開頭的圖中有詳細的描述
先安裝courier-authlib讓cyrus-sasl更好的支持mysql
[root@mail ~]# tar xf courier-authlib-0.62.4.tar.bz2
[root@mail ~]# cd courier-authlib-0.62.4
[root@mail courier-authlib-0.62.4]# ./configure \
–prefix=/usr/local/courier-authlib \
–sysconfdir=/etc \
–without-authpam \
–without-authshadow \
–without-authvchkpw \
–without-authpgsql \
–with-authmysql \
–with-mysql-libs=/usr/local/mysql/lib \
–with-mysql-includes=/usr/local/mysql/include \
–with-redhat \
–with-authmysqlrc=/etc/authmysqlrc \
–with-authdaemonrc=/etc/authdaemonrc \
–with-mailuser=postfix \
–with-mailgroup=postfix \
–with-ltdl-lib=/usr/lib
[root@mail courier-authlib-0.62.4]# make&&make install
提供courier-authlib的SysV風格腳本,安裝目錄下有此腳本,cp過去就OK了
[root@mail courier-authlib-0.62.4]# cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
加入服務列表並開機自啓動
[root@mail courier-authlib-0.62.4]# chkconfig –add courier-authlib
[root@mail courier-authlib-0.62.4]# chkconfig !$ on
能夠將庫文件載入系統庫
[root@mail courier-authlib-0.62.4]# echo 「/usr/local/courier-authlib/lib/courier-authlib」 > /etc/ld.so.conf.d/local.conf
[root@mail courier-authlib-0.62.4]# ldconfig
提供配置文件
[root@mail courier-authlib-0.62.4]# cp /etc/authdaemonrc.dist /etc/authdaemonrc
[root@mail courier-authlib-0.62.4]# cp /etc/authmysqlrc.dist /etc/authmysqlrc
編輯authmysqlrc修改如下內容(各個字段和extmail建立的數據庫是相連的,因此都要按照extmail的數據庫字段來修改)
注意指定好mysql.sock文件路徑 而後修改authdaemonrc讓courier-authlib使用mysql來認證(courier-authlib單單隻可使用mysql,也可使用ldap)
[root@mail ~]# service courier-authlib start 而後配置SASL來支持courier-authlib
step8:配置postfix支持SASL認證和虛擬用戶
[root@mail ~]# grep -v 「^#」 /etc/postfix/main.cf | grep -v 「^$」
######全局的配置項########
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
mail.$mydomain, www.$mydomain, ftp.$mydomain
unknown_local_recipient_reject_code = 550
mynetworks = 127.0.0.0/8
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = postdrop
html_directory = /var/www/html/postfix
manpage_directory = /usr/local/man
sample_directory = /etc/postfix
readme_directory = no
inet_protocols = ipv4
#####支持CYRUS-SASL認證配置項#####
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_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!
######虛擬用戶配置項###########
virtual_mailbox_base = /var/mailbox
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_alias_domains =
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:2525
virtual_gid_maps = static:2525
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
########磁盤配額配置項##############
message_size_limit = 14336000
virtual_mailbox_limit = 20971520
virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = mysql:/etc/postfix/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 Tidy your mailbox and try again later.
virtual_overquota_bounce = yes
step9:整合extman到postfix
extman是extmail的一個管理組件
[root@mail ~]# tar xf extman-1.1.tar.gz
[root@mail ~]# cd extman-1.1/docs/
導入數據庫
因爲extmail好久不更新了,因此數據庫腳本在新的mysql5.5上語法有點問題,vi打開數據庫腳本修改。(在mysql5.1上沒有問題)
:%s@TYPE=MyISAM@ENGINE=MyISAM@g
[root@mail docs]# mysql -uroot -p < extmail.sql
[root@mail docs]# mysql -uroot -p < init.sql
建立extman相關的數據庫用戶
mysql> grant all privileges on extmail.* to extmail@localhost identified by ‘extmail’;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on extmail.* to extmail@127.0.0.1 identified by ‘extmail’;
Query OK, 0 rows affected (0.00 sec)
提供extman的配置文件
[root@mail docs]# cp mysql_virtual_* /etc/postfix/
step10:配置dovecot支持mysql認證
dovecot自己就支持mysql認證,因此直接配置就行
修改/etc/dovecot/conf.d/10-mail.conf 中郵件目錄
mail_location = maildir:/var/mailbox/%d/%n/Maildir
修改/etc/dovecot/conf.d/auth-sql.conf中使用mysql驗證用戶的配置文件位置
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
userdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
而後建立/etc/dovecot/dovecot-sql.conf,配置以下
driver = mysql
connect = host=/tmp/mysql.sock dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = ‘%u’
user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = ‘%u’
step11:安裝extmail和extman
安裝extmail
[root@mail ~]# tar xf extmail-1.2.tar.gz
[root@mail ~]# mkdir /var/www/extsuite -pv
[root@mail ~]# cp extmail-1.2 /var/www/extsuite/extmail -R
修改配置文件
[root@mail extmail]# vi webmail.cf
這裏grep出來的配置太長了,主要修改這幾項
SYS_USER_LANG = zh_CN
SYS_MAILDIR_BASE = /var/mailbox
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_SOCKET = /tmp/mysql.sock
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
其它的默認便可
然後修改extmail目錄下cgi的屬主爲postfix
[root@mail extmail]# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/
安裝extman
[root@mail ~]# cp extman-1.1 /var/www/extsuite/extman -R
修改配置文件
[root@mail extman]# vi webman.cf
主要是下面這幾項
SYS_MAILDIR_BASE = /var/mailbox
SYS_DEFAULT_UID = 1024
SYS_DEFAULT_GID = 1024
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_SOCKET = /tmp/mysql.sock
然後修改extman目錄下cgi的屬主爲postfix
[root@mail extman]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
step12:整合將extmail和extman整合進apache
extmail和extman都是基於perl腳本實現的,因此要開機apache的perl的cgi
而它還須要perl的Unix::syslogd功能,因此須要編譯安裝Unix::syslogd
[root@mail ~]# tar xf Unix-Syslog-1.1.tar.gz
[root@mail ~]# cd Unix-Syslog-1.1
[root@mail Unix-Syslog-1.1]# perl Makefile.PL
[root@mail Unix-Syslog-1.1]# make && make install
配置1個虛擬主機
加入以下配置
User postfix
Group postfix
<VirtualHost *:80>
ServerName mail.magedu.com
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
Alias /extman /var/www/extsuite/extman/html
</VirtualHost>
step13:完結。。。。
OK ,瀏覽器輸入地址
而後登入系統建立虛擬域和虛擬用戶,整個環境就此作好。