DKIM(DomainKeys Identified Mail),电子邮件验证标准――域名密钥识别邮件标准。
采用DKIM能有效的提高信件发送的成功率。通常结合spf使用。
系统:rhel 6.3
邮件服务器:postfix 2.6.6, dovecot 2.0.9,cyrus-sasl 2.1.23
DKIM 版本:2.8.3
IP:10.40.212.8(内),210.10.166.166(外)
域名:mx.zhangdh.com
cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
vim CentOS6-Base-163.repo将文件里面的$releasever全替换为版本号6,保存退出。
yum install sendmail-milter #解决dkim的libmilter依赖问题
wget ftp://ftp.sunet.se/pub/Linux/distributions/fedora/epel/6/x86_64/dkim-milter-2.8.3-8.el6.x86_64.rpm
rpm -ivh dkim-milter-2.8.3-8.el6.x86_64.rpm
注:如果是Centos6.5及其以上版本可以直接使用yum安装,安装方法如下:
yum -y install sendmail-milter dkim-milter
遇到的坑:2014-12-11日,163镜像因系统硬件故障而服务暂停,所以不能使用yum方式进行安装,如何解决?
下载sendmail-milter-8.14.4-8.el6.x86_64.rpm,然后使用rpm的方式即可完成安装。
vim /etc/mail/dkim-milter/dkim-filter.conf
===========
AutoRestart yes
AutorestartRate 10/1h
AlwaysAddARHeader yes
Canonicalization relaxed/relaxed
Domain mx.zhangdh.com
Mode sv
KeyList /etc/mail/dkim-milter/keys/keylist
ExternalIgnoreList /etc/mail/dkim-milter/trusted-hosts
InternalHosts /etc/mail/dkim-milter/trusted-hosts
LogWhy yes
Subdomains no
Selector default
SignatureAlgorithm rsa-sha256
#Socket local:/var/run/dkim-milter/dkim-milter.sock
KeyFile /etc/mail/dkim-milter/keys/mx.zhangdh.com/default
Background yes
Socket inet:8891
Syslog yes
SyslogSuccess yes
UserID dkim-milter:dkim-milter
X-Header yes
注:遇到的坑有下面几个
1) 忘记创建文件:/etc/mail/dkim-milter/trusted-hosts
touch /etc/mail/dkim-milter/trusted-hosts
chown -R dkim-milter:dkim-milter/etc/mail/dkim-milter
2) 使用 Socket local:/var/run/dkim-milter/dkim-milter.sock遇到权限问题,“permission denied”,解决的办法就是:Socket inet:8891[@localhost]
3) 第二步的修改,也需要同步更新/etc/postfix/main.cf
4) 不同之处是:生成的密钥的命令里default._domainkey改成了default.domainkey;
现象是:mail postfix/smtpd[1827]:warning: connect to Milter service inet:8891: No route to host?
解决办法?
============
vim /etc/mail/dkim-milter/trusted-hosts #设置信任的发送主机
127.0.0.1
mx.zhangdh.com
#10.40.212.0/8
a) 创建一个脚本并命名为create_dkim,内容如下:
#!/bin/bash
usage() {
echo"Usage:"
echo" $0 <domain.com> [selector name]"
echo" default selector name =default"
exit 1
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
KEYDIR=/etc/mail/dkim-milter/keys/$1
sel=${2-default}
mkdir -p $KEYDIR
[[ $? -ne 0 ]] && exit 127
/usr/sbin/dkim-genkey -D $KEYDIR-r -d $1 -s $sel
mv $KEYDIR/${sel}.private$KEYDIR/$sel
chmod 700 $KEYDIR
chmod 600 $KEYDIR/${sel}*
chown -R dkim-milter:dkim-milter$KEYDIR
echo"*@$1:$1:/etc/mail/dkim-milter/keys/$1/$sel" >>/etc/mail/dkim-milter/keys/keylist
echo "Add this lines to $1DNS zone file"
cat $KEYDIR/$sel.txt
echo '_ssp._domainkey IN TXT"t=y; dkim=unknown"'
echo '_adsp._domainkey IN TXT"dkim=unknown"'
b) 执行脚本生成key
./create_dkim mx.zhangdh.com #mx.zhangdh.com为邮件的域,即@后面的内容。
Add thislines to mx.zhangdh.com DNS zone file
default._domainkeyIN TXT "v=DKIM1; g=*; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxP3g41Uf1jeCDp/slrzCuxs0vmaO7+Zgc1JC0NBDDxCirlPspz74kXettu8b2H/xE20gkjwsEIx2xgUcfzLnP3IgJETe5w1uc6fC91euaMtXNSRsOfkMKjjixCPnpy6TeYxFt6RhmJrERRCrRIaMFLusOKptd7WRFZ8WBhAWzXQIDAQAB"; ----- DKIM default for mx.zhangdh.com
_ssp._domainkeyIN TXT "t=y; dkim=unknown"
_adsp._domainkeyIN TXT "dkim=unknown"
pwd
/etc/mail/dkim-milter/keys
cat keylist
# sender-pattern:signing-domain:keypath
# *:example.com:selector
*@mx.zhangdh.com:mx.zhangdh.com:/etc/mail/dkim-milter/keys/mx.zhangdh.com/default
执行完以上脚本同时默认会在/etc/mail/dkim-milter/keys/下生成一个目录mx5.love21cn.net。
ll
total 8
-rw------- 1dkim-milter dkim-milter 887 Dec 8 18:26default
-rw------- 1dkim-milter dkim-milter 310 Dec 8 18:26default.txt
1)default._domainkey.mx.zhangdh.com IN TXT "v=DKIM1; g=*; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxP3g41Uf1jeCDp/slrzCuxs0vmaO7+Zgc1JC0NBDDxCirlPspz74kXettu8b2H/xE20gkjwsEIx2xgUcfzLnP3IgJETe5w1uc6fC91euaMtXNSRsOfkMKjjixCPnpy6TeYxFt6RhmJrERRCrRIaMFLusOKptd7WRFZ8WBhAWzXQIDAQAB"
2)_ssp._domainkey.mx.zhangdh.com IN TXT "t=y;dkim=unknown"
3)_adsp._domainkey.mx.zhangdh.com IN TXT"dkim=unknown"
4)mx.zhangdh.com IN TXT "v=spf1 +a +mx -all" #for spf
注:如果设置错误,将会出现类似下面的错误:
echo 'non_smtpd_milters = inet:8891' >>/etc/postfix/main.cf
echo 'smtpd_milters = inet:8891' >>/etc/postfix/main.cf
echo'milter_protocol = 2' >> /etc/postfix/main.cf
echo'milter_default_action = accept' >> /etc/postfix/main.cf
下面的两行在postfix调用的时候会遇到权限问题,所以不采用:
echo 'smtpd_milters= local:/var/run/dkim-milter/dkim.sock' >> /etc/postfix/main.cf
echo'non_smtpd_milters = local:/var/run/dkim-milter/dkim.sock' >>/etc/postfix/main.cf
a) method1:
dig +short default._domainkey.mx.zhangdh.com txt @8.8.8.8
dig -ttxt default._domainkey.mx.zhangdh.com @8.8.8.8
dig -ttxt _ssp._domainkey.mx.zhangdh.com @8.8.8.8
dig -ttxt _adsp._domainkey.mx.zhangdh.com @8.8.8.8
b) method2:
host -ttxt default._domainkey.mx.zhangdh.com
c) method3:
http://dkimcore.org/tools/keycheck.html
https://metacpan.org/pod/Mail::DKIM::Verifier
1)设置自启动:chkconfig--level 2345 dkim-milter on
2)启动:/etc/init.d/dkim-milterstart
3)关闭:/etc/init.d/dkim-milterstop
这里假设使用的是foxmail客户端,如何查看DKIM和SPF是否认证成功,步骤如下:
第一步:打开收到的邮件。
第二步:点击文件菜单并选择属性,会弹出一个对话框。
第三步:在对话框中选择“详细信息”->“邮件原信息(M)”,会弹出另一个对话框。
第四步:从原始信息的对话框中查找有关DKIM和SPF相关的信息,你会看到如:
“spf=pass”和“dkim=pass”的信息,则表示认证成功。
问题1. Dec 31 10:53:21 mail postfix/smtpd[29209]: warning: connect toMilter service inet:8891: No route to host
原因:由于开启了防火墙,导致postfix服务无法连到dkim-milter的8891端口
解决办法:
-A INPUT -p tcp -m state --state NEW -m tcp--dport 8891 -j ACCEPT
注:以上域名mx.zhangdh.com请用你真实的域名替代。