收发信服务(Postfix+Dovecot)SMTP认证控制
#########################################################
一:准备案例环境
邮件服务器:mail.xxx.com 192.168.x.x邮件域:@xxx.com邮件帐号:服务器的系统用户发信服务软件:postfix收信服务软件:dovecot
提供DNS域名解析:
[root@www ~]# yum -y install bind bind-chroot[root@www ~]# mv /etc/named.conf /etc/named.conf.origin[root@www ~]# vim /var/named/chroot/etc/named.conf[root@www ~]# cat /var/named/chroot/var/named/xxx.com.zone.. ..@ IN MX 5 mail.xxx.com.mail IN A 192.168.4.5.. ..[root@www ~]# service named restart[root@www ~]# chkconfig named on[root@www named]# nslookup MX xxx.com //客户端检查MX记录
二:构建 postfix 发信服务器【SMTP协议,TCP 25端口】
1. 安装 postfix 软件包
[root@svr5 ~]# yum -y install postfix[root@svr5 ~]# chkconfig postfix on
2. 简化、调整主配置文件,启动 postfix 服务
[root@svr5 ~]# cd /etc/postfix/[root@svr5 postfix]# postconf -n > tmp.txtaq[root@svr5 postfix]# mv main.cf main.cf.origin[root@svr5 postfix]# mv tmp.txt main.cf[root@svr5 ~]# vim /etc/postfix/main.cf#inet_interfaces = localhost //注释掉此行myhostname = mail.tarena.commydomain = xxx.com //邮件域myorigin = $mydomain //显示的发件域mydestination = $mydomain, $myhostname //本地投递域home_mailbox = Maildir/ //邮箱类型.. ..[root@svr5 ~]# service postfix restart[root@svr5 ~]# chkconfig postfix on[root@svr5 ~]# netstat -antp | grep :25tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 5927/master
3. 添加电子邮箱账号
[root@svr5 ~]# useradd user1[root@svr5 ~]# echo 1234567 | passwd --stdin user1[root@svr5 ~]# useradd user2[root@svr5 ~]# echo 1234567 | passwd --stdin user2
4. 使用 telnet 测试发信(user1给user2)
[root@svr6 ~]# telnet mail.xxx.com 25 //连接邮件服务器的25端口Trying 192.168.4.5...Connected to mail.xxx.com (192.168.4.5).Escape character is '^]'.220 mail.xxx.com.com ESMTP PostfixHELO localhost //宣告客户端的主机地址250 mail.xxx.com.comMAIL FROM:user1@xxx.com //指定发件人地址250 2.1.0 OkRCPT TO:user2@user2.com //指定收件人地址250 2.1.5 OkDATA //表示要开始写邮件内容了354 End data with. Subject:Test mail 1. //指定邮件标题 No.1 mail document.. .. //输入文本邮件内容. //独立的 . 表示输入完毕250 2.0.0 Ok: queued as D4B5131D8B2quit //断开telnet连接221 2.0.0 ByeConnection closed by foreign host.[root@svr6 ~]#
5. 检查邮件投递结果
[root@svr5 ~]# ls ~user2/Maildir/new/ //新邮件列表1379059530.V802I3ec129M716267.svr5.xxx.com[root@svr5 ~]# cat ~user2/Maildir/new/1379059530.*
三:构建 dovecot 收信服务器【POP3/IMAP4协议,TCP 110/143端口】
1. 安装 dovecot 软件包
[root@svr5 ~]# yum -y install dovecot[root@svr5 ~]# chkconfig dovecot on
3. 使用 telnet 测试收信(user2)
[root@svr6 ~]# telnet mail.xxx.com 110 //连接邮件服务器的110端口Trying 192.168.4.5...Connected to mail.xxx.com (192.168.4.5).Escape character is '^]'.+OK Dovecot ready.USER user2 //以用户user2登录+OKPASS 1234567 //密码为1234567+OK Logged in.LIST //查看邮件列表+OK 6 messages:1 451RETR 1 //获取编号为1的邮件+OK 451 octetsReturn-Path:X-Original-To: user2@xxx.comDelivered-To: user2@xxx.com…QUIT //断开telnet连接+OK Logging out.Connection closed by foreign host.[root@svr6 ~]#
四:实现 SMTP 发信认证
1. 启动 saslauthd 认证服务
[root@svr5 ~]# yum -y install cyrus-sasl //此包默认通常已安装 [root@svr5 ~]# service saslauthd start[root@svr5 ~]# chkconfig saslauthd on [root@svr5 ~]# testsaslauthd -u user2 -p 1234567 -s smtp0: OK "Success." //检查saslauthd服务
2. 调整 postfix 配置,启用SMTP认证
[root@svr5 ~]# vim /etc/postfix/main.cfmynetworks = 127.0.0.1 //设置本地网络smtpd_sasl_auth_enable = yes //启用SASL认证smtpd_sasl_security_options = noanonymous //阻止匿名发信smtpd_recipient_restrictions = //设置收件人过滤 permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination //拒绝向未授权的目标域发信[root@svr5 ~]# service postfix restart
1)以用户user1为例,未经过认证登录时,向外域发邮件会被拒绝
[root@svr6 ~]# telnet mail.xxx.com 25Trying 192.168.4.5...Connected to mail.xxx.com (192.168.4.5).Escape character is '^]'.220 mail.xxx.com ESMTP PostfixHELO localhost //宣告本机地址250 mail.xxx.comMAIL FROM:user1@xxx.com //指定发件人地址250 2.1.0 OkRCPT TO:TsengYia@126.com //指定收件人地址454 4.7.1: Relay access denied //发送外域的发信请求被拒绝quit //断开telnet连接221 2.0.0 ByeConnection closed by foreign host.
2)为用户nick为例,生成用户名、密码的加密字串
[root@svr5 ~]# printf "user1" | openssl base64bmljaw==[root@svr5 ~]# printf "1234567" | openssl base64MTIzNDU2Nw==
3)认证登录通过以后,才允许向外域发邮件
[root@svr6 ~]# telnet mail.xxx.com 25
Trying 192.168.4.5...Connected to mail.xxx.com (192.168.4.5).Escape character is '^]'.220 mail.xxx.com ESMTP PostfixEHLO localhost //加密宣告本机地址250-mail.xxx.com250-PIPELINING250-SIZE 10240000250-VRFY250-ETRN250-AUTH PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSNAUTH LOGIN //声明要执行认证登录334 VXNlcm5hbWU6bmljaw== //输入用户名xxx的BASE64编码334 UGFzc3dvcmQ6MTIzNDU2Nw== //输入密码1234567的BASE64编码235 2.7.0 Authentication successfulMAIL FROM:user1@xxx.com //指定发件人地址250 2.1.0 OkRCPT TO:TsengYia@126.com //指定收件人地址250 2.1.5 OkDATA //开始编写邮件内容354 End data with. Subject:SMTP Auth Test //指定邮件标题 Hello, here is a test mail. //输入文本邮件内容. //独立的 . 表示输入完毕250 2.0.0 Ok: queued as 8C48431D8B2quit //断开telnet连接221 2.0.0 ByeConnection closed by foreign host.[root@svr6 ~]#
#########################################################