Postfix+dovecot 部署
Postfix: 是一个标准的MTA(Mail Transfer Agent)服务器,它负责通过SMTP协议管理发送到本机的邮件以及由本机发向外界的邮件。在本文中,Postfix角色1:作为用户邮件服务,即把邮件的本地投递工作(接受到邮件之后将邮件存档到本地磁盘)交给Dovecot的LMTP服务(Local Mail Transfer Protocol service)处理,本域用户通过服务器向外界发送邮件时,Postfix还将负责验证权限以确保服务器不被滥用;Postfix角色2:做邮件中继服务,在不改变用户邮件地址的前提下,将用户邮件通过国内外链路Smtp邮件转发服务器投递到收件人邮件服务器,实现全球邮件投递
Dovecot: 是一个非常优秀的IMAP/POP服务器用以接收外界发送到本机的邮件。通常,Dovecot的工作内容包括:验证用户身份以确保邮件不会被泄露。在本文中,Dovecot将负责所有的(身份验证)工作,我们配置Dovecot查询本地的MySQL数据库以确认用户身份
Postfix+dovecot 实现邮件服务的 MTA及用户验证
安装包:使用源码编译安装或yum安装
系统环境: Centos7
需要依赖的 lib
yum -y install libdb*-devel openssl-develmysql-devel cyrus-sasl-* libcurl*
本文中使用了源码编译安装
系统需要预先安装好make及gcc 相关软件
备注:如果使用yum安装,postfix和dovecot的配置文件路径分别为/etc/postfix、/etc/dovecot
而源码编译安装可以在编译时指定路径,也可以使用默认路径即:/etc/postfix、/usr/local/etc/dovecot
[root@root ~] groupadd -g 2000 vmail ( 指定新建用户组vmail的ID为2000 )
[root@root ~] useradd -g vmail -u 2000vmail -d/var/vmail ( 指定新建用户vmail的ID为2000,用户根目录为/var/vmail )
下载地址:
ftp://ftp.cuhk.edu.hk/pub/packages/mail-server/postfix/experimental/postfix-3.4-20180222.tar.gz
Postfix官网源码包postfix-3.4-20180222.tar.gz
将源码解压后进行编译安装
make makefiles CCARGS='-DUSE_SASL_AUTH \
-DDEF_SERVER_SASL_TYPE=\"dovecot\" \
-DCURL_STATICLIB \
-DHAS_MYSQL -I/usr/local/mysql/include' \
'AUXLIBS_MYSQL=-L/usr/local/mysql/lib -lmysqlclient -lz -lrt -lm-L/usr/lib64/sasl2 -lsasl2 -lssl-lcrypto'
make && make install
注:DHAS_MYSQL及AUXLIBS_MYSQL为mysql-dev安装路径,源码安装和yum安装路径不同,具体根据机器mysql-dev安装路径而定
编译安装后,配置文件将默认路径在 /etc/postfix
配置文件配置说明:
main.c --主配置文件、 master.cf -- maste服务配置文件
以下为几个主要配置项,做了主要说明,其他配置参考原main.cf文件或者官网main.cf配置参数说明进行参数配置(官网文档地址:http://www.postfix.org/postconf.5.html)
1、 相关网络参数配置
myhostname = mail.test.com
mydomain = test.com
myorigin = $myhostname
inet_interfaces = all
inet_protocols = ipv4
##将mydestination的值修改为localhost,以便Postfix能够通过MySQL表中相关数据决定需要接受/发送邮件的域名,这样更具有通用性
mydestination = $myhostname, localhost.$mydomain, localhost
2、 用户的权限验证
#不使用Postfix做用户的权限验证,因此我们把Postfix默认的用户验证参数屏蔽,因此请在如下几行前边加入#符号以注释:
# TLS parameters
#smtpd_tls_cert_file =/etc/dovecot/dovecot.pem
#smtpd_tls_key_file = /etc/dovecot/private/dovecot.pem
#smtpd_use_tls = yes
#smtpd_tls_auth_only = yes
#Enabling SMTP for authenticated users, andhanding off authentication to Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks,reject_unauth_destination
smtpd_client_restrictions =permit_sasl_authenticated
#在文档种加入以下内容,以便告诉Postfix不要使用LDA「LocalDelivery Agent」转而使用
#dovecot lmtp
virtual_transport =lmtp:unix:private/dovecot-lmtp
3、 虚拟用户配置
#加入以下内容,以便Postfix去MySQL数据库种寻找域名、用户帐号密码及邮件别名等信息:
#Virtual domains, users, and aliases
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps =mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps =mysql:/etc/postfix/mysql-virtual-alias-maps.cf
virtual_uid_maps =mysql:/etc/postfix/mysql-virtual-uid-maps.cf
virtual_gid_maps =mysql:/etc/postfix/mysql-virtual-gid-maps.cf
virtual_mailbox_base = /var/mail/
将如下行的#去掉
smtp inet n - n - - smtpd
并且加入
submission inet n - n - - smtpd
-osyslog_name=postfix/submission
-osmtpd_tls_wrappermode=no
-osmtpd_tls_security_level=encrypt
-osmtpd_sasl_auth_enable=yes
-osmtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
-omilter_macro_daemon_name=ORIGINATING
-osmtpd_sasl_type=dovecot
-osmtpd_sasl_path=private/auth
注意一下mysql相关参数配置的含义(官网文档地址: http://www.postfix.org/mysql_table.5.html)
几个常用参数(与dovecot的有区别)
%s – user@domain,整个邮件用户名
%u – user@domain @前面的 user 部分
%d – user@domain @后面的 domain 部分
配置后的cf文件是否可以正常输出结果,可以使用
postmap -q [email protected] mysql:/etc/postfix/mysql-xxxx.cf
进行验证
修改配置文件,需要postfix重启或者reload 配置
1、 virtual_mailbox_domains(虚拟网域域名)
创建/etc/postfix/mysql-virtual-mailbox-domains.cf,内容如下:
user = root
password = 123456
hosts = 127.0.0.1
dbname = mail_virtual
query = SELECT 1 FROM virtual_domains WHEREdomain='%d'
2、 virtual_mailbox_maps(虚拟用户及其邮件目录列表)
创建/etc/postfix/mysql-virtual-mailbox-maps.cf,内容如下:
user = root
password = 123456
hosts = 127.0.0.1
dbname = mail_virtual
query = SELECT d.domain as domain FROM virtual_users as u,virtual_domains as d WHERE d.id=u.domain_id and u.email='%u'
3、 virtual_alias_maps(虚拟用户alias列表)
创建/etc/postfix/mysql-virtual-alias-maps.cf,内容如下:
user = root
password = 123456
hosts = 127.0.0.1
dbname = mail_virtual
query = SELECT alias FROM virtual_aliasesWHERE init_email='%s'
4、 virtual_uid_maps(虚拟用户内容本地存放权限的归属userid)
创建/etc/postfix/mysql-virtual-uid-maps.cf,内容如下:
user = root
password = 123456
hosts = 127.0.0.1
dbname = mail_virtual
query = SELECT gid FROM virtual_users WHEREemail='%s'
5、 virtual_gid_maps(虚拟用户内容本地存放权限的归属groupid)
创建/etc/postfix/mysql-virtual-gid-maps.cf,内容如下:
user = root
password = 123456
hosts = 127.0.0.1
dbname = mail_virtual
query = SELECT gid FROM virtual_users WHEREemail='%s'
DROP TABLE IF EXISTS `virtual_aliases`;
CREATE TABLE `virtual_aliases` (
`id` int(11) NOT NULL COMMENT '自增主键',
`init_email` varchar(200) NOT NULL COMMENT '原始邮箱',
`alias` varchar(200) NOT NULL COMMENT '别名邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `virtual_domains`;
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL COMMENT '虚拟网域的id(自增主键)',
`domain` varchar(100) DEFAULT NULL COMMENT '虚拟网域的值',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for virtual_users
-- ----------------------------
DROP TABLE IF EXISTS `virtual_users`;
CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL COMMENT '主键自增',
`domain_id` int(11) NOT NULL COMMENT '虚拟网域的id',
`username` varchar(100) NOT NULL COMMENT '虚拟用户名(@前面那部分)',
`password` varchar(100) NOT NULL COMMENT '用户密码(用于sasl认证)',
`email` varchar(200) NOT NULL COMMENT '用户邮箱全称(用户名@网域)',
`uid` int(11) NOT NULL COMMENT 'MDA用于访问邮件存放目录的uid',
`gid` int(11) NOT NULL COMMENT 'MDA用于访问邮件存放目录的gid',
`home` varchar(200) NOT NULL COMMENT 'MUA读取邮件时的根目录',
`location`varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY`username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
该启动脚本在centos系统通用
/etc/init.d/postfix start|stop|restart|reload
#!/bin/bash
#
# postfix Postfix Mail Transger Agent
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail TransportAgent, 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.
source /etc/rc.d/init.d/functions
# Source networking configuation.
source /etc/sysconfig/network
# Check that networking is up.
[ $NETWORKING = "no" ] &&exit 3
[ -x /usr/local/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/local/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/local/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/local/sbin/postfix reload 2>/dev/null 1>&2 && success|| failure $"$prog reload"
RETVAL=$?
echo
return $RETVAL
}
abort() {
/usr/local/sbin/postfix abort 2>/dev/null 1>&2 &&success || failure $"$prog abort"
return $?
}
flush() {
/usr/local/sbin/postfix flush 2>/dev/null 1>&2 && success|| failure $"$prog flush"
return $?
}
check() {
/usr/local/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 $?
为Centos7系统启动方式之一,即在/usr/lib/systemd/system/ 下创建postfix.service文件,使用 systemctlstart|stop|restart|reload postfix.service 启动
[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service
[Service]
Type=forking
PIDFile=/var/spool/postfix/pid/master.pid
EnvironmentFile=-/etc/sysconfig/network
ExecStartPre=-/usr/libexec/postfix/aliasesdb
ExecStartPre=-/usr/libexec/postfix/chroot-update
ExecStart=/usr/local/sbin/postfix start
ExecReload=/usr/local/sbin/postfix reload
ExecStop=/usr/local/sbin/postfix stop
[Install]
WantedBy=multi-user.target
Dovecot-SASL: 这个软件是dovecot对SASL的一个实现,Dovecot官网中提到,Dovecot也实现了SASL。
Dovcecot-SASL有两个部分,一个是passdb,一个是userdb, passdb用来实现认证,userdb用来实现提取用户的一些信息,比如邮件存储目录,uid等。
官网下载地址
https://dovecot.org/releases/2.2/dovecot-2.2.35.tar.gz
将源码解压后进行编译安装
CPPFLAGS=-I/usr/local/mysql/lib/includeLDFLAGS=-L/usr/local/mysql/lib ./configure --with-mysql --with-sql=yes
make && make install
编译安装后,配置文件默认路径为:/usr/local/etc/dovecot
相关配置文件说明:
/usr/local/etc/dovecot/dovecot.conf Dovecot的主配置文件
/usr/local/etc/dovecot/conf.d/10-auth.conf 用户验证相关配置信息
/usr/local/etc/dovecot/conf.d/10-mail.conf Dovecot将要操作的磁盘路径相关配置信息
/usr/local/etc/dovecot/conf.d/10-master.conf Dovecot本地socket相关配置信息
/usr/local/etc/dovecot/conf.d/10-ssl.conf 关于SSL的相关配置信息
/usr/local/etc/dovecot/conf.d/20-pop3.conf 关于POP3的相关配置信息
/usr/local/etc/dovecot/conf.d/auth-sql.conf.ext SQL-Type验证相关配置信息
/usr/local/etc/dovecot/dovecot-sql.conf.ext Dovecot与数据库连接相关配置信息。
密码加密方式使用CRAM-MD5方式,采用doveadm生成
doveadm pw -s CRAM-MD5 -u [email protected] dahai\$2018
配置文件配置说明
1、 /usr/local/etc/dovecot/dovecot.conf 配置
在配置文件中添加
#protocols = imap pop3 lmtp(该项可加或不加,默认 protocols= imap pop3 lmtp )
!include conf.d/*.conf
listen = *
如果要更方便调试,则加上
auth_debug_passwords=yes
mail_debug=yes
auth_verbose=yes
auth_verbose_passwords=plain
2、 /usr/local/etc/dovecot/conf.d/10-auth.conf配置
a) 将#disable_plaintext_auth = yes #号去掉,改成
disable_plaintext_auth = no
#默认情况下,Dovecot是允许Ubuntu系统用户登录使用的,我们需要将其禁用。找到文件中如下内容并将其注释:
#!include auth-system.conf.ext
并将 #!include auth-sql.conf.ext 的#号去掉,采用读取数据库方式认证
!include auth-sql.conf.ext
3、 /usr/local/etc/dovecot/conf.d/10-mail.conf配置
mail_location = maildir:/var/mail/%d/%n/Maildir
mail_privileged_group = postfix
并将/var/mail 文件夹chown 给postfix
chown –R postfix:postfix /var/mail
4、 /usr/local/etc/dovecot/conf.d/10-master.conf配置
在service lmtp和service auth 中添加红色字体部分内容
service lmtp {
unix_listener/var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user= postfix
group =postfix
}
}
5、 /usr/local/etc/dovecot/conf.d/20-pop3.conf配置
设置pop3_uidl_format如下:
pop3_uidl_format = %08Xu%08Xv
6、 /usr/local/etc/dovecot/conf.d/auth-sql.conf.ext配置
设置passdb 与userdb 的 args 参数方式
passdb {
driver = sql
args = /usr/local/etc/dovecot/dovecot-sql.conf.ext
}
userdb {
driver = sql
args = /usr/local/etc/dovecot/dovecot-sql.conf.ext
}
7、 /usr/local/etc/dovecot/dovecot-sql.conf.ext配置
driver = mysql
connect = host=127.0.0.1dbname=mail_virtual user=root password=123456
default_pass_scheme = CRAM-MD5
password_query = SELECT email AS user,password AS password \
FROM virtual_users WHERE email = '%u'
user_query = SELECT home, uid, gid FROMvirtual_users WHERE email = '%u'
该启动脚本在centos系统通用
/etc/init.d/dovecot start|stop|restart|reload
#!/bin/bash
# Source function library.
. /etc/init.d/functions
if [ -f /etc/sysconfig/dovecot ]; then
./etc/sysconfig/dovecot
fi
RETVAL=0
prog="Dovecot Imap"
exec="/usr/local/sbin/dovecot"
config="/usr/local/etc/dovecot/dovecot.conf"
pidfile="/var/run/dovecot/master.pid"
lockfile="/var/lock/subsys/dovecot"
start() {
[ -x $exec ] || exit 5
[ -f $config ] || exit 6
echo -n $"Starting $prog: "
daemon --pidfile $pidfile $exec $OPTIONS
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
echo
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $exec
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $lockfile
echo
}
reload() {
echo -n $"Reloading $prog: "
killproc -p $pidfile $exec -HUP
RETVAL=$?
echo
}
#
# See how we were called.
#
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
force-reload|restart)
stop
start
RETVAL=$?
;;
condrestart|try-restart)
if [ -f $lockfile ]; then
stop
start
fi
;;
status)
status -p $pidfile $exec
RETVAL=$?
;;
*)
echo $"Usage: $0{condrestart|try-restart|start|stop|restart|reload|force-reload|status}"
exit 2
esac
exit $RETVAL
为Centos7系统启动方式之一,即在/usr/lib/systemd/system/ 下创建dovecot.service文件,使用 systemctlstart|stop|restart|reload dovecot.service 启动
[Unit]
Description=Dovecot IMAP/POP3 email server
After=local-fs.target network.targetnetwork-online.target
[Service]
Type=simple
ExecStartPre=/usr/libexec/dovecot/prestartscript
ExecStart=/usr/local/sbin/dovecot -F
ExecReload=/bin/kill -HUP $MAINPID
PrivateTmp=true
NonBlocking=yes
[Install]
WantedBy=multi-user.target