新聞中心
Postfix基于虛擬用戶虛擬域的郵件架構(gòu)

創(chuàng)新互聯(lián)建站總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計、網(wǎng)站維護(hù)、公眾號搭建、微信小程序定制開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價值而不懈努力!
上圖是一個幾乎完整的郵件系統(tǒng)架構(gòu)圖,這里基于Mysql數(shù)據(jù)庫進(jìn)行用戶認(rèn)證,不管是Postfix、Dovecot、webmail都需要去Mysql數(shù)據(jù)庫中進(jìn)行用戶認(rèn)證。
1、用戶可以基于outlook連接postfix然后通過Courier-authlib連接到Mysql進(jìn)行認(rèn)證,認(rèn)證成功就可以發(fā)送郵件。前面我們也是用Cyrus-sasl進(jìn)行shadow用戶認(rèn)證,其實(shí)Cyrus-sasl也同樣支持Mysql認(rèn)證。但是由于驅(qū)動比較底層配置起來比較繁瑣,所以選用Courier-authlib進(jìn)行認(rèn)證,但是從上圖可以看出我們是postfix還是借用Cyrus-sasl函數(shù)庫與Courier-authlib進(jìn)行連接。同樣的道理Postfix也可以直接連接到Courier-authlib但是比較麻煩。
2、用戶可以基于outlook連接Dovecot然后直接到mysql進(jìn)行認(rèn)證,認(rèn)證成功就可以接收Mailbox中的郵件。這里比較簡單,因為Dovecot自身支持到Mysql的認(rèn)證。
3、用戶可以基于webmail進(jìn)行收發(fā)郵件,同樣通過Extmail/Extman到Mysql中認(rèn)證,認(rèn)證成功。就可以基于postfix發(fā)郵件,基于Dovecot收郵件。但是Extmail/Extman自身就可以到Mailbox中收取郵件。
Courier-authlib
Courier 是一個優(yōu)秀的電子信件系統(tǒng),擁有一個完整的郵件系統(tǒng):其提供MTA(Courier-MTA),MDA(Maildrop),MUA,MRA(Courier-IMAP),SASL(Courier-authlib)WebMail(sqwebmail)等這些組件。
Courier-authlib是Courier組件中的認(rèn)證庫,它是courier組件中一個獨(dú)立的子項目,用于為Courier的其它組件提供認(rèn)證服務(wù)。其認(rèn)證功能通常包括驗正登錄時的帳號和密碼、獲取一個帳號相關(guān)的家目錄或郵件目錄等信息、改變帳號的密碼等。而其認(rèn)證的實(shí)現(xiàn)方式也包括基于PAM通過/etc/passwd和/etc/shadow進(jìn)行認(rèn)證,基于GDBM或DB進(jìn)行認(rèn)證,基于LDAP/MySQL/PostgreSQL進(jìn)行認(rèn)證等。因此,courier-authlib也常用來與courier之外的其它郵件組件(如postfix)整合為其提供認(rèn)證服務(wù)。
虛擬用戶虛擬域配置
安裝ltdl動態(tài)模塊加載器
[root@localhost ~]# yum install libtool-ltdl libtool-ltdl-devel
安裝expect主機(jī)間通信
[root@localhost ~]# yum install expect
創(chuàng)建用戶
[root@localhost ~]# groupadd -g 1001 vmail
[root@localhost ~]# useradd vmail -u 1001 -g 1001
安裝Courier-authlib
[root@localhost ~]# tar xvf courier-authlib-0.66.1.tar.bz2 -C /usr/src/
[root@localhost ~]# cd /usr/src/courier-authlib-0.66.1
[root@localhost courier-authlib-0.66.1]# ./configure \
--prefix=/usr/local/courier-authlib \
--sysconfdir=/etc \
--without-authpam \
--without-authshadow \
--without-authvchkpw \
--without-authpgsql \
#以上without是不支持此類認(rèn)證免得需要安裝依賴的數(shù)據(jù)包
--with-authmysql \
#基于mysql認(rèn)證
--with-mysql-libs=/usr/lib64/mysql \
--with-mysql-includes=/usr/include/mysql \
#需要mysql的頭文件和庫文件路徑一定要正確
--with-redhat \
#如果是redhat系統(tǒng)會實(shí)現(xiàn)自我優(yōu)化;如果不是就不要加了
--with-authmysqlrc=/etc/authmysqlrc \
#提供給mysql的配置文件,記錄認(rèn)證怎樣跟數(shù)據(jù)進(jìn)行交互
--with-authdaemonrc=/etc/authdaemonrc \
#courier-authlib自身是一個服務(wù)進(jìn)程所以也需要一個配置文件
--with-mailuser=vmail \
--with-mailgroup=vmail \
#用戶郵件收發(fā)管理的用戶和組
[root@smtp ~]# make && make install
調(diào)整配置文件
[root@localhost ~]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
#調(diào)整一下authdaemon的權(quán)限(存放進(jìn)程套接字)
[root@localhost ~]# cp -p /etc/authdaemonrc.dist /etc/authdaemonrc
#調(diào)整courier文件名,因--with-authdaemonrc=/etc/authdaemonrc指定了文件名
[root@localhost ~]# cp -p /etc/authmysqlrc.dist /etc/authmysqlrc
#調(diào)整跟mysql交互文件名,因--with-authmysqlrc=/etc/authmysqlrc指定了文件名
調(diào)整courier-authlib配置文件
[root@localhost ~]# vim /etc/authdaemonrc
authmodulelist="authmysql"
#指定認(rèn)證模塊為authmysql
authmodulelistorig="authmysql"
#認(rèn)證的原始模塊只保留authmysql
daemons=10
#修改默認(rèn)開啟進(jìn)程
#DEBUT-LOGIN=2
#如果使用虛擬用戶登錄有問題就打開調(diào)試功能看看日志;不然不要打開
調(diào)整通過MYSQL進(jìn)行郵件賬號認(rèn)證文件
[root@localhost ~]# vim /etc/authmysqlrc
MYSQL_SERVER localhost
#指定Mysql服務(wù)器地址
MYSQL_USERNAME extmail
#連接數(shù)據(jù)庫的用戶名(如果不使用extmail,那么在extman中需要重新指定賬號)
MYSQL_PASSWORD extmail
#用戶密碼
MYSQL_PORT 3306
#指定你的mysql的端口(使用socket通信就不用端口)
MYSQL_SOCKET /var/lib/mysql/mysql.sock
#Mysql的套接字文件
MYSQL_DATABASE extmail
#存儲用戶的庫(如果不使用extmail,那么在extman中都要更改)
MYSQL_USER_TABLE mailbox
#存儲用戶的表(mailbox是extman幫我們自動生成的不能改)
MYSQL_CRYPT_PWFIELD password
#認(rèn)證密碼字段
MYSQL_UID_FIELD '1001'
#vmail用戶的UID
MYSQL_GID_FIELD '1001'
#vmail用戶的GID
MYSQL_LOGIN_FIELD username
#認(rèn)證賬號字段
MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)
#concat是mysql的一個函數(shù)用來把/var/mailbox/跟homedir連接成一個路徑(homedir是mysql的一個變量值為每一個用戶名)
MYSQL_NAME_FIELD name
#用戶全名字段,默認(rèn)
MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)
#虛擬用戶的郵件目錄
Courier-authlib提供SysV服務(wù)腳本
[root@localhost ~]# cd /usr/src/courier-authlib-0.66.1/
[root@localhost courier-authlib-0.66.1]# cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
[root@localhost ~]# chmod 755 /etc/init.d/courier-authlib
[root@localhost ~]# chkconfig --add courier-authlib
[root@localhost ~]# chkconfig courier-authlib on
[root@localhost ~]# service courier-authlib start
Starting Courier authentication services: authdaemond
PS:可以使用pstree -a可以看到authdaemond已經(jīng)生成了11個進(jìn)程,我們開啟了10個,但是有一個主進(jìn)程負(fù)責(zé)生成其他進(jìn)程。
建立虛擬用戶郵箱目錄
[root@localhost ~]# mkdir -pv /var/mailbox
[root@localhost ~]# chown -R vmail /var/mailbox
PS:郵箱目錄屬主為vmail
更改SASL認(rèn)證模式為authdaemond
Postfix的SMTP認(rèn)證需要透過Cyrus-SASL連接到authdaemon獲取認(rèn)證信息
[root@localhost ~]# vim /usr/lib64/sasl2/smtpd.conf
#pwcheck_method: saslauthd
#mech_list: PLAIN LOGIN
#注釋前面實(shí)驗使用SASL認(rèn)證的參數(shù)
pwcheck_method: authdaemond
log_level: 3
mech_list:PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
讓Postfix支持虛擬用戶及虛擬域
[root@localhost ~]# vim /etc/postfix/main.cf
#################Virtual Mailbox Settings###################
virtual_mailbox_base = /var/mailbox
#用戶郵箱目錄(跟這個參數(shù)MYSQL_HOME_FIELD定義的要一致)
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
#用來查詢用戶賬號信息(這個配置文件中定義了SQL語句,使用extmail用戶)
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:1001
virtual_gid_maps = static:1001
#每一個虛擬用戶都映射為系統(tǒng)用戶vmail
virtual_transport = virtual
#指定MDA專門為虛擬用戶投遞代理
virtual_mailbox_limit = 20971520
#磁盤配額
安裝Httpd
[root@localhost ~]# yum install httpd
安裝extman
需要使用源碼extman目錄下docs目錄中的extmail.sql和init.sql建立數(shù)據(jù)庫extmail
[root@localhost ~]# tar zxvf extman-1.1.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/extman-1.1/docs
[root@localhost docs]# service mysqld restart
[root@localhost docs]# mysql -u root [root@localhost docs]# mysql -u root [root@localhost docs]# mysql -u root -B -e "show databases;" Database information_schema extmail mysql test
#對于MySQL-5.1以后版本,其中的服務(wù)腳本extmail.sql執(zhí)行會有語法錯誤,因為MySQL-5.1使用的默認(rèn)存儲引擎是MyISAM,而在MySQL-5.1之后使用的是InnoDB存儲引擎??上仁褂萌缦旅钚薷膃xtmail.sql配置文件而后再執(zhí)行修改方法如下:
sed -i ‘s@TYPE=MyISAM@ENGINE=InnoDB@g’ extmail.sql
#這里我使用的是PRM包,所以MySQL是5.1版本的,不用修改SQL腳本。
這兩個SQL腳本會創(chuàng)建extmail數(shù)據(jù)庫和表,以及extmail、webman用戶。所以需要授予用戶extmail訪問extmail數(shù)據(jù)庫的權(quán)限(生產(chǎn)環(huán)境中extmail給select權(quán)限,因為它只需要檢索用戶即可)。同時這里的密碼設(shè)置為extmail同上面的配置文件一樣,不然都要改。
mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
mysql> GRANT all privileges on extmail.* TO extmail@127.0.0.1 IDENTIFIED BY 'extmail';
mysql> flush privileges;
從Extman中復(fù)制Postfix支持虛擬用戶和虛擬域及連接mysql的文件
[root@localhost ~]# cd /usr/src/extman-1.1/docs
[root@localhost docs]# cp mysql_virtual_mailbox_maps.cf /etc/postfix/
[root@localhost docs]# cp mysql_virtual_domains_maps.cf /etc/postfix/
[root@localhost docs]# cp mysql_virtual_alias_maps.cf /etc/postfix/
[root@localhost docs]# cp mysql_virtual_limit_maps.cf /etc/postfix/
打開一個文件看看
[root@localhost ~]# cat /etc/postfix/mysql_virtual_domains_maps.cf
user = extmail
password = extmail
hosts = localhost
dbname = extmail
table = domain
select_field = domain
where_field = domain
additional_conditions = AND active = ‘1’
PS:所以在通過mysql認(rèn)證配置文件/etc/authmysqlrc中的賬號密碼很多地方都要用到,生產(chǎn)環(huán)境中全部都要統(tǒng)一修改。
取消前面配置的中心域
使用虛擬域的時候,就需要取消中心域的使用,myhostname、mydomain、myorigin、mydestination,所以現(xiàn)在main.cf配置文件需要添加的參數(shù)如下所示:
[root@localhost ~]# vim /etc/postfix/main.cf
#################Center Domain Settings###########
mynetworks = 127.0.0.0/8
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#myhostname = smtp.ywnds.com
#mydomain = ywnds.com
#myorigin = $mydomain
#home_mailbox = Maildir/
配置Dovecot基于MySQL認(rèn)證
[root@localhost ~]# vim /etc/dovecot/dovecot.conf
#Ssl = no
#disable_plaintext_auth = no
#mail_location = maildir:~/Maildir
#把dovecot主配置文件/etc/dovecot/dovecot.conf中剛開始添加的幾行數(shù)據(jù)注釋掉
[root@localhost ~]# vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/var/mailbox/%d/%n/Maildir
#添加此行指定郵件的提取位置
[root@localhost ~]# vim /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
#開啟明文驗證(可以選擇把dovecot.conf配置文件中的此參數(shù)關(guān)閉)
auth_mechanisms = plain login
#支持驗證方法
#!include auth-system.conf.ext
#取消默認(rèn)系統(tǒng)驗證
!include auth-sql.conf.ext
#開啟mysql驗證
#auth_verbose = yes
#認(rèn)證詳細(xì)日志,調(diào)試可以打開
[root@localhost ~]# vim /etc/dovecot/conf.d/auth-sql.conf.ext
passdb {
driver = sql
#args = /etc/dovecot/dovecot-sql.conf.ext
args = /etc/dovecot/dovecot-sql.conf
}
userdb {
driver = sql
#args = /etc/dovecot/dovecot-sql.conf.ext
args = /etc/dovecot/dovecot-sql.conf
}
PS:在這個MySQL驗證文件中指定了用戶的賬號和密碼需要通過/etc/dovecot/dovecot-sql.conf這個文件去數(shù)據(jù)庫中取。
[root@localhost ~]# vim /etc/dovecot/dovecot-sql.conf
driver = mysql
connect = host=localhost 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'
解釋:
Driver
#Dovecot使用自帶的驅(qū)動連接MySQL
Connect
#連接本地mysql;數(shù)據(jù)庫extmail;用戶extmail;密碼extmail。
#說明:如果mysql服務(wù)器是本地主機(jī),即host=localhost時,如果mysql.sock文件不是默認(rèn)的/var/lib/mysql/mysql.sock,可以使用host=“sock文件的路徑”來指定新位置;
#例如,使用通用二進(jìn)制格式安裝的MySQL,其sock文件位置為/tmp/mysql.sock,相應(yīng)地,connect應(yīng)按如下方式定義connect = host=/tmp/mysql.sock dbname=extmail user=extmail password=extmail。
Default_pass_scheme
#表明我們的密碼為加密存放
Password_query
#找密碼查詢哪個字段
User_query
#找賬號查詢哪個字段
PS:dovecot其實(shí)也提供了這么一個配置文件模板,在/usr/share/doc/dovecot-2.0.9/example-config/dovecot-sql.conf.ext
啟動所有服務(wù)
[root@localhost ~]# service dovecot restart
[root@localhost ~]# service postfix restart
[root@localhost ~]# service courier-authlib restart
[root@localhost ~]# service mysqld restart
測試虛擬用戶
[root@localhost ~]# /usr/local/courier-authlib/sbin/authtest -s login postmaster@extmail.org extmail
Authentication succeeded.
網(wǎng)站名稱:Linux下安裝Postfix郵件虛擬用戶及虛擬域
網(wǎng)頁URL:http://m.5511xx.com/article/dhsdeho.html


咨詢
建站咨詢
