一.安装配置dovecotvim
1.dovecot简介
bash
Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,支持四种协议:pop3(110/tcp), imap4(143/tcp), pop3s, imaps。POP / IMAP 是 MUA 从邮件服务器中读取邮件时使用的协议。其中,与 POP3 是从邮件服务器中下载邮件存起来,IMAP4 则是将邮件留在服务器端直接对邮件进行管理、操做。服务器
2.安装dovecot
网络
# yum install dovecot -y
3.配置dovecotapp
# vim /etc/dovecot.conf
启用并修改如下参数dom
!include conf.d/*.conf //该配置项说明conf.d下的因此conf结尾的文件均有效,注意最前面的"!"号tcp
protocols = imap pop3ide
login_trusted_networks = 192.168.50.0/24 //指定容许登陆的网段地址oop
若未设置login_trusted_networks参数值,使用telnet登陆110端口,将会出现以下错误,不知道这个值能不能设置为四个零,这个还有待测试…
post
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections. pass openstack -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections. quit +OK Logging out Connection closed by foreign host. [root@mail ~]# tail -f /var/log/maillog Dec 21 16:51:00 rhel6 dovecot: pop3-login: Aborted login (tried to use disabled plaintext auth): rip=192.168.50.211, lip=192.168.50.24, mpid=0
[root@mail ~]# vim /etc/dovecot/conf.d/10-mail.conf
设置/etc/dovecot/conf.d/10-mail.conf配置文件以下:
mail_location = mbox:~/mail:INBOX=/var/mail/%u
mbox_write_locks = fcntl
如未设置mail_location参数值,将会出现以下错误:
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack +OK pass openstack Connection closed by foreign host. [root@mail ~]# tail /var/log/maillog Jul 11 11:57:48 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34823, secured Jul 11 11:57:48 localhost dovecot: pop3(openstack): Error: user openstack: Initialization failed: mail_location not set and autodetection failed: Mail storage autodetection failed with home=/home/openstack Jul 11 11:57:48 localhost dovecot: pop3(openstack): Error: Invalid user settings. Refer to server log for more information.
设置完mail_location值后,须要重启dovecot服务,同时须要建立相关的目录,不然还会出现以下错误:
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. USER openstack +OK PASS openstack -ERR [IN-USE] Couldn't open INBOX: Internal error occurred. Refer to server log for more information. [2015-07-11 12:13:35] Connection closed by foreign host. [root@mail ~]# tail /var/log/maillog Jul 11 12:04:27 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34951, secured Jul 11 12:04:27 localhost dovecot: pop3(openstack): Error: user openstack: Initialization failed: mail_location not set and autodetection failed: Mail storage autodetection failed with home=/home/openstack Jul 11 12:04:27 localhost dovecot: pop3(openstack): Error: Invalid user settings. Refer to server log for more information. Jul 11 12:13:08 localhost dovecot: master: Warning: Killed with signal 15 (by pid=34962 uid=0 code=kill) Jul 11 12:13:09 localhost dovecot: master: Dovecot v2.0.9 starting up (core dumps disabled) Jul 11 12:13:35 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34984, secured Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: chown(/home/openstack/mail/.imap/INBOX, -1, 12(mail)) failed: Operation not permitted (egid=2528(openstack), group based on /var/mail/openstack) Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: mkdir(/home/openstack/mail/.imap/INBOX) failed: Operation not permitted Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: Couldn't open INBOX: Internal error occurred. Refer to server log for more information. [2015-07-11 12:13:35] Jul 11 12:13:35 localhost dovecot: pop3(openstack): Couldn't open INBOX top=0/0, retr=0/0, del=0/0, size=0 [root@mail ~]# su - openstack [openstack@mail ~]$ mkdir mail/.imap/INBOX [openstack@mail ~]$ exit
4.启动服务并让其开机自启动
# service dovecot start # chkconfig dovecot on
5.测试
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack +OK pass openstack +OK Logged in. list +OK 1 messages: 1 428 . retr 1 +OK 428 octets Return-Path: <hadoop@wybedu.com> X-Original-To: openstack Delivered-To: openstack@wybedu.com Received: from mail.wybedu.com (ns.wybedu.com [192.168.220.111]) by mail.wybedu.com (Postfix) with SMTP id 265C162B for <openstack>; Sat, 11 Jul 2015 11:54:57 +0800 (CST) Message-Id: <20150711035509.265C162B@mail.wybedu.com> Date: Sat, 11 Jul 2015 11:54:57 +0800 (CST) From: hadoop@wybedu.com SUBJECT how are you! hello . quit +OK Logging out. Connection closed by foreign host.
二.为postfix开启基于cyrus-sasl的认证功能
1.启动sasl服务并让其开机自启动
# vim /etc/sysconfig/saslauthd
修改sasl服务配置文件,将sasl认证机制修改成shadow
MECH=shadow
注:saslauthd -v: 显示当前主机saslauthd服务所支持的认证机制,默认为pam
# service saslauthd start # chkconfig saslauthd on
2.测试是否能实现用户认证
# testsaslauthd -u username -p password
3.实现postfix基于客户端的访问控制
1)、基于客户端的访问控制概览
postfix内置了多种反垃圾邮件的机制,其中就包括“客户端”发送邮件限制。客户端判别机制能够设定一系列客户信息的判别条件:
smtpd_client_restrictions:定义客户端链接限定
smtpd_data_restrictions:定义是否容许发送数据(即data阶段)
smtpd_helo_restrictions:定义是否容许发送hello信息(即helo阶段)
smtpd_recipient_restrictions:定义是否容许向谁发送邮件(即rcpt to阶段)
smtpd_sender_restrictions:定义是否容许谁向外发送邮件(即mail from阶段)
上面的每一项参数分别用于检查SMTP会话过程当中的特定阶段,即客户端提供相应信息的阶段,如当客户端发起链接请求时,postfix就能够根据配置文件中定义的smtpd_client_restrictions参数来判别此客户端IP的访问权限。相应地,smtpd_helo_restrictions则用于根据用户的helo信息判别客户端的访问能力等等。
若是DATA命令以前的全部内容都被接受,客户端接着就能够开始传送邮件内容了。邮件内容一般由两部分组成,前半部分是标题(header),其能够由header_check过滤,后半部分是邮件正文(body),其能够由check_body过滤。这两项实现的是邮件“内容检查”。
postfix的默认配置以下:
smtpd_client_restrictions =
smtpd_data_restrictions =
smtpd_end_of_data_restrictions =
smtpd_etrn_restrictions =
smtpd_helo_restrictions =
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
smtpd_sender_restrictions =
这限制了只有mynetworks参数中定义的本地网络中的客户端才能经过postfix转发邮件,其它客户端则不被容许,从而关闭了开放式中继(open relay)的功能。
Postfix有多个内置的限制条件,如上面的permit_mynetworks和reject_unauth_destination,但管理员也能够使用访问表(access map)来自定义限制条件。自定义访问表的条件一般使用check_client_access, check_helo_access, check_sender_access, check_recipient_access进行,它们后面一般跟上type:mapname格式的访问表类型和名称。其中,check_sender_access和check_recipient_access用来检查客户端提供的邮件地址,所以,其访问表中能够使用完整的邮件地址,如admin@magedu.com;也能够只使用域名,如magedu.com;还能够只有用户名的部分,如marion@。
2)、实现示例1
这里以禁止172.16.100.200这台主机经过工做在172.16.100.1上的postfix服务发送邮件为例演示说明其实现过程。访问表使用hash的格式。
(1)首先,编辑/etc/postfix/access文件,以之作为客户端检查的控制文件,在里面定义以下一行:
172.16.100.200 REJECT
(2)将此文件转换为hash格式
# postmap /etc/postfix/access
(3)配置postfix使用此文件对客户端进行检查
编辑/etc/postfix/main.cf文件,添加以下参数:
smtpd_client_restrictions = check_client_access hash:/etc/postfix/access
(4)让postfix从新载入配置文件便可进行发信控制的效果测试了。
3)、实现示例2
这里以禁止经过本服务器向microsoft.com域发送邮件为例演示其实现过程。访问表使用hash的格式。
(1)首先,创建/etc/postfix/denydstdomains文件(文件名任取),在里面定义以下一行:
microsoft.com REJECT
(2)将此文件转换为hash格式
# postmap /etc/postfix/denydstdomains
(3)配置postfix使用此文件对客户端进行检查
编辑/etc/postfix/main.cf文件,添加以下参数:
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/denydstdomains, permit_mynetworks, reject_unauth_destination(顺序对结果会有影响)
(4)让postfix从新载入配置文件便可进行发信控制的效果测试了。
4)、检查表格式的说明
hash类的检查表都使用相似以下的格式:
pattern action
检查表文件中,空白行、仅包含空白字符的行和以#开头的行都会被忽略。以空白字符开头后跟其它非空白字符的行会被认为是前一行的延续,是一行的组成部分。
(1)关于pattern
其pattern一般有两类地址:邮件地址和主机名称/地址。
邮件地址的pattern格式以下:
user@domain 用于匹配指定邮件地址;
domain.tld 用于匹配以此域名做为邮件地址中的域名部分的全部邮件地址;
user@ 用于匹配以此做为邮件地址中的用户名部分的全部邮件地址;
主机名称/地址的pattern格式以下:
domain.tld 用于匹配指定域及其子域内的全部主机;
.domain.tld 用于匹配指定域的子域内的全部主机;
net.work.addr.ess
net.work.addr
net.work
net 用于匹配特定的IP地址或网络内的全部主机;
network/mask CIDR格式,匹配指定网络内的全部主机;
(2)关于action
接受类的动做:
OK 接受其pattern匹配的邮件地址或主机名称/地址;
所有由数字组成的action 隐式表示OK;
拒绝类的动做(部分):
4NN text
5NN text
其中4NN类表示过一下子重试;5NN类表示严重错误,将中止重试邮件发送;421和521对于postfix来讲有特殊意义,尽可能不要自定义这两个代码;
REJECT optional text... 拒绝;text为可选信息;
DEFER optional text... 拒绝;text为可选信息;
4.为postfix开启基于cyrus-sasl的认证功能
使用如下命令验正postfix是否支持cyrus风格的sasl认证,若是您的输出为如下结果,则是支持的:
# /usr/local/postfix/sbin/postconf -a
cyrus
dovecot
# vim /etc/postfix/main.cf
添加如下内容:
############################CYRUS-SASL############################
broken_sasl_auth_clients = yes:是否经过sasl验证客户端身份
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:启用sasl认证功能
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous:不支持匿名用户
smtpd_sasl_application_name = smtpd:哪个应用程序使用sasl功能(2.3版本之后已废弃,改用smtpd_sasl_path = smtpd)
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!:telnet链接时显示信息
# vim /usr/lib(64)/sasl2/smtpd.conf
添加以下内容:
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
让postfix从新加载配置文件
[root@mail ~]# /usr/sbin/postfix reload [root@mail ~]# telnet 192.168.220.111 25 Trying 192.168.220.111... Connected to 192.168.220.111. Escape character is '^]'. 220 Welcome to our mail.wybedu.com ESMTP,Warning: Version not Available! ehlo mail.wybedu.com 250-mail.wybedu.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN (请确保您的输出以相似两行) 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
5.测试
[root@mail ~]# telnet 192.168.220.111 25 Trying 192.168.220.111... Connected to 192.168.220.111. Escape character is '^]'. 220 Welcome to our mail.wybedu.com ESMTP,Warning: Version not Available! ehlo mail.wybedu.com 250-mail.wybedu.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN auth login 334 VXNlcm5hbWU6 aGFkb29w (用户名密码采用base64编码:echo -n 'hadoop' | openssl base64) 334 UGFzc3dvcmQ6 aGFkb29w 235 2.7.0 Authentication successful mail from:root@wybedu.com 250 2.1.0 Ok rcpt to:hello@aol.com 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> hello . 250 2.0.0 Ok: queued as B6C9962B quit 221 2.0.0 Bye Connection closed by foreign host.