溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux中Postfix虛擬用戶及虛擬域的示例分析

發(fā)布時(shí)間:2021-10-26 09:54:24 來(lái)源:億速云 閱讀:191 作者:小新 欄目:建站服務(wù)器

這篇文章主要為大家展示了“Linux中Postfix虛擬用戶及虛擬域的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Linux中Postfix虛擬用戶及虛擬域的示例分析”這篇文章吧。

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

Linux中Postfix虛擬用戶及虛擬域的示例分析

上圖是一個(gè)幾乎完整的郵件系統(tǒng)架構(gòu)圖,這里基于Mysql數(shù)據(jù)庫(kù)進(jìn)行用戶認(rèn)證,不管是Postfix、Dovecot、webmail都需要去Mysql數(shù)據(jù)庫(kù)中進(jìn)行用戶認(rèn)證。

1、用戶可以基于outlook連接postfix然后通過(guò)Courier-authlib連接到Mysql進(jìn)行認(rèn)證,認(rèn)證成功就可以發(fā)送郵件。前面我們也是用Cyrus-sasl進(jìn)行shadow用戶認(rèn)證,其實(shí)Cyrus-sasl也同樣支持Mysql認(rèn)證。但是由于驅(qū)動(dòng)比較底層配置起來(lái)比較繁瑣,所以選用Courier-authlib進(jìn)行認(rèn)證,但是從上圖可以看出我們是postfix還是借用Cyrus-sasl函數(shù)庫(kù)與Courier-authlib進(jìn)行連接。同樣的道理Postfix也可以直接連接到Courier-authlib但是比較麻煩。

2、用戶可以基于outlook連接Dovecot然后直接到mysql進(jìn)行認(rèn)證,認(rèn)證成功就可以接收Mailbox中的郵件。這里比較簡(jiǎn)單,因?yàn)镈ovecot自身支持到Mysql的認(rèn)證。

3、用戶可以基于webmail進(jìn)行收發(fā)郵件,同樣通過(guò)Extmail/Extman到Mysql中認(rèn)證,認(rèn)證成功。就可以基于postfix發(fā)郵件,基于Dovecot收郵件。但是Extmail/Extman自身就可以到Mailbox中收取郵件。

Courier-authlib

Courier 是一個(gè)優(yōu)秀的電子信件系統(tǒng),擁有一個(gè)完整的郵件系統(tǒng):其提供MTA(Courier-MTA),MDA(Maildrop),MUA,MRA(Courier-IMAP),SASL(Courier-authlib)WebMail(sqwebmail)等這些組件。

Courier-authlib是Courier組件中的認(rèn)證庫(kù),它是courier組件中一個(gè)獨(dú)立的子項(xiàng)目,用于為Courier的其它組件提供認(rèn)證服務(wù)。其認(rèn)證功能通常包括驗(yàn)正登錄時(shí)的帳號(hào)和密碼、獲取一個(gè)帳號(hào)相關(guān)的家目錄或郵件目錄等信息、改變帳號(hào)的密碼等。而其認(rèn)證的實(shí)現(xiàn)方式也包括基于PAM通過(guò)/etc/passwd和/etc/shadow進(jìn)行認(rèn)證,基于GDBM或DB進(jìn)行認(rèn)證,基于LDAP/MySQL/PostgreSQL進(jìn)行認(rèn)證等。因此,courier-authlib也常用來(lái)與courier之外的其它郵件組件(如postfix)整合為其提供認(rèn)證服務(wù)。

虛擬用戶虛擬域配置

安裝ltdl動(dòng)態(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的頭文件和庫(kù)文件路徑一定要正確
--with-redhat \
  #如果是redhat系統(tǒng)會(huì)實(shí)現(xiàn)自我優(yōu)化;如果不是就不要加了
--with-authmysqlrc=/etc/authmysqlrc \
  #提供給mysql的配置文件,記錄認(rèn)證怎樣跟數(shù)據(jù)進(jìn)行交互
--with-authdaemonrc=/etc/authdaemonrc \
  #courier-authlib自身是一個(gè)服務(wù)進(jìn)程所以也需要一個(gè)配置文件
--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)開(kāi)啟進(jìn)程
#DEBUT-LOGIN=2
  #如果使用虛擬用戶登錄有問(wèn)題就打開(kāi)調(diào)試功能看看日志;不然不要打開(kāi)

調(diào)整通過(guò)MYSQL進(jìn)行郵件賬號(hào)認(rèn)證文件

[root@localhost ~]# vim /etc/authmysqlrc
MYSQL_SERVER localhost
  #指定Mysql服務(wù)器地址
MYSQL_USERNAME extmail
  #連接數(shù)據(jù)庫(kù)的用戶名(如果不使用extmail,那么在extman中需要重新指定賬號(hào))
MYSQL_PASSWORD extmail
  #用戶密碼
MYSQL_PORT 3306
  #指定你的mysql的端口(使用socket通信就不用端口)
MYSQL_SOCKET /var/lib/mysql/mysql.sock
  #Mysql的套接字文件
MYSQL_DATABASE  extmail
  #存儲(chǔ)用戶的庫(kù)(如果不使用extmail,那么在extman中都要更改)
MYSQL_USER_TABLE  mailbox
  #存儲(chǔ)用戶的表(mailbox是extman幫我們自動(dòng)生成的不能改)
MYSQL_CRYPT_PWFIELD  password
  #認(rèn)證密碼字段
MYSQL_UID_FIELD  '1001'
  #vmail用戶的UID
MYSQL_GID_FIELD  '1001'
  #vmail用戶的GID
MYSQL_LOGIN_FIELD  username
  #認(rèn)證賬號(hào)字段
MYSQL_HOME_FIELD  concat('/var/mailbox/',homedir)
  #concat是mysql的一個(gè)函數(shù)用來(lái)把/var/mailbox/跟homedir連接成一個(gè)路徑(homedir是mysql的一個(gè)變量值為每一個(gè)用戶名)
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個(gè)進(jìn)程,我們開(kāi)啟了10個(gè),但是有一個(gè)主進(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)證需要透過(guò)Cyrus-SASL連接到authdaemon獲取認(rèn)證信息

[root@localhost ~]# vim /usr/lib64/sasl2/smtpd.conf
#pwcheck_method: saslauthd
#mech_list: PLAIN LOGIN
#注釋前面實(shí)驗(yàn)使用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
  #用戶郵箱目錄(跟這個(gè)參數(shù)MYSQL_HOME_FIELD定義的要一致)
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
  #用來(lái)查詢用戶賬號(hào)信息(這個(gè)配置文件中定義了SQL語(yǔ)句,使用extmail用戶)
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
  #用來(lái)查詢虛擬域
virtual_alias_domains =
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
  #用來(lái)查詢用戶別名
virtual_uid_maps = static:1001
virtual_gid_maps = static:1001
  #每一個(gè)虛擬用戶都映射為系統(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ù)庫(kù)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 < extmail.sql
[root@localhost docs]# mysql -u root < init.sql
[root@localhost docs]# mysql -u root -B -e "show databases;"
Database
information_schema
extmail
mysql
test

#對(duì)于MySQL-5.1以后版本,其中的服務(wù)腳本extmail.sql執(zhí)行會(huì)有語(yǔ)法錯(cuò)誤,因?yàn)镸ySQL-5.1使用的默認(rèn)存儲(chǔ)引擎是MyISAM,而在MySQL-5.1之后使用的是InnoDB存儲(chǔ)引擎??上仁褂萌缦旅钚薷膃xtmail.sql配置文件而后再執(zhí)行修改方法如下:

sed -i ‘s@TYPE=MyISAM@ENGINE=InnoDB@g’ extmail.sql

#這里我使用的是PRM包,所以MySQL是5.1版本的,不用修改SQL腳本。

這兩個(gè)SQL腳本會(huì)創(chuàng)建extmail數(shù)據(jù)庫(kù)和表,以及extmail、webman用戶。所以需要授予用戶extmail訪問(wèn)extmail數(shù)據(jù)庫(kù)的權(quán)限(生產(chǎn)環(huán)境中extmail給select權(quán)限,因?yàn)樗恍枰獧z索用戶即可)。同時(shí)這里的密碼設(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/

打開(kāi)一個(gè)文件看看

[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:所以在通過(guò)mysql認(rèn)證配置文件/etc/authmysqlrc中的賬號(hào)密碼很多地方都要用到,生產(chǎn)環(huán)境中全部都要統(tǒng)一修改。

取消前面配置的中心域

使用虛擬域的時(shí)候,就需要取消中心域的使用,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中剛開(kāi)始添加的幾行數(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
  #開(kāi)啟明文驗(yàn)證(可以選擇把dovecot.conf配置文件中的此參數(shù)關(guān)閉)
auth_mechanisms = plain login
  #支持驗(yàn)證方法
#!include auth-system.conf.ext
  #取消默認(rèn)系統(tǒng)驗(yàn)證
!include auth-sql.conf.ext
  #開(kāi)啟mysql驗(yàn)證
#auth_verbose = yes
  #認(rèn)證詳細(xì)日志,調(diào)試可以打開(kāi)
[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:在這個(gè)MySQL驗(yàn)證文件中指定了用戶的賬號(hào)和密碼需要通過(guò)/etc/dovecot/dovecot-sql.conf這個(gè)文件去數(shù)據(jù)庫(kù)中取。

[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ū)動(dòng)連接MySQL
Connect
  #連接本地mysql;數(shù)據(jù)庫(kù)extmail;用戶extmail;密碼extmail。
  #說(shuō)明:如果mysql服務(wù)器是本地主機(jī),即host=localhost時(shí),如果mysql.sock文件不是默認(rèn)的/var/lib/mysql/mysql.sock,可以使用host=“sock文件的路徑”來(lái)指定新位置;
  #例如,使用通用二進(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
  #找密碼查詢哪個(gè)字段
User_query
  #找賬號(hào)查詢哪個(gè)字段
 PS:dovecot其實(shí)也提供了這么一個(gè)配置文件模板,在/usr/share/doc/dovecot-2.0.9/example-config/dovecot-sql.conf.ext

啟動(dòng)所有服務(wù)

[root@localhost ~]# service dovecot restart
[root@localhost ~]# service postfix restart
[root@localhost ~]# service courier-authlib restart
[root@localhost ~]# service mysqld restart

測(cè)試虛擬用戶

[root@localhost ~]# /usr/local/courier-authlib/sbin/authtest -s login postmaster@extmail.org extmail
Authentication succeeded.

以上是“Linux中Postfix虛擬用戶及虛擬域的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI