一:sendmail概述
Sendmail是目前使用最为广泛的一种E-mail服务器。当前其最新的稳定版本为8.14.3。 sendmail是最重要的邮件传输代理程序。因此理解电子邮件的工作模式是非常重要的。一般情况下,我们把电子邮件程序分解成传输代理,用户代理和投递代理三大部分。
1、电子邮件系统的专业术语
(1)邮件传输代理(Mail Transfer Agent):服务器执行的软件,用于在服务器之间传输邮件,一般每个系统只有一个MTA保持运行状态。
(2)邮件用户代理(Mail User Agent):客户端运行的软件,提供用户读、写、修改及处理邮件的功能,一个系统中可以有多个MUA在运行。
(3)邮件传递代理(Mail Delivery Agent):通常与MTA一同运行,将MTA接收的邮件按照目的位置作出相应处理,发往本机帐户下的邮箱或转发到另外的MTA。
(4)邮局协议(Post Office Protocol):是邮件系统接收电子邮件的通信协议,可以以“拉”的形式从服务器将邮件下载到本地进行阅读。
(5)Internet信息访问协议(Internet Message access Protocol):与POP一样是用来读取服务器上的邮件,但客户端必须登录服务器,属于在服务器上直接读取。
(6)简单邮件传输协议(Simple Mail Transfer Protocol):负责邮件传递的协议。
2、sendmail主要配置文件
(1)/etc/mail/sendmail.cf:sendmail的主要配置文件,管理所有参数。由于sendmail.cf中的语法很复杂,所以不要建议手工修改。由于我们可以以sendmail.mc文档配合sendmail-cf目录下的宏通过m4预处理器自动生成。
(2)/etc/mail/access.db:sendmail服务器的数据库,要转成这个数据库需要makemap命令和/etc/mail/access的配合
(3)/etc/mail/access:设置哪些地址的主机能够经过本地sendmail服务器进行收发邮件。
3、sendmail主要执行文件
(1)/usr/sbin/makemap:主要将access转成access.db的执行文件。
(2)/usr/sbin/mailstats:将/etc/mail/statistics配置文件读出来的执行文件,可以查看到目前为止sendmail共传送接受了多少邮件。
(3)/usr/bin/newaliases:使snedmail重新读取/etc/aliases的内容。
(4)/usr/bin/mailq:查看/var/spool/mqueue邮件暂存目录还有多少邮件没有发送。
4、邮件相关目录
(1)/var/spool/mail:每个使用者邮件存放目录,每个用户名为一个子目录。
(2)/var/spool/mqueue:没有发送出去的邮件暂存目录。
5:sendmail下载地址
sendmail.8.14.2.tar.gz http://www.sendmail.org/releases
二: sendmail的安全问题
随着Internet的爆炸性增长,像sendmail这样接受用户所提供的任意输入并把它投递给本地用户、文件或shell的应用常常会为黑客们提供一条实施攻击的坦途。sendmail和DNS甚至IP,正在尝试把身份验证和加密作为内建的解决方案以解决一些基本的安全性问题。
最近美国关于加密技术的出口法规开始松动,这使得sendmail在发布时可以内建对加密功能的挂接接口。版本8.11及其后继版本都支持用SSL(Secure Socket Layer,安全套接口协议层)进行SMTP身份验证和加密,SSL也称为TLS(Transport Layer Security,传输层加密)。sendmail在这里使用术语TLS,并且已经把它作为对SMTP协议的一种扩展―STARTTLS―实现了。TLS给它带来了6个用于鉴定文件和给文件上锁的新配置选项。对访问数据库进行匹配的新操作要求身份验证必须先取得成功。
三:各协议开放的端口
发送 Smtp :25 smtp+ssl(starttls) smtps: 465
接收 pop3 :110 pop3s: 995
Imap: 143 imap: 993
必要的实验环境:
1:操作系统:linux red hat enterprise server 内核版本 2.6.18-164.el5
2:本地的yum 服务器(在次不再演示如何搭建)
案例一:
需求分析:
某公司的总部位于北京,在其公司内部架设有一天dns服务器(192.168.10.100)负责bj.com的区域,而他的分公司上海也架设有一台dns服务器(192.168.10.101)负责sh.com,为了实现两个地方的邮件用户可以互发邮件,并且对所发的邮件进行加密。北京和上海也分别有自己的邮件服务器。
网络拓扑图:
步骤:
一:总部北京的配置
1:安装sendmail服务器的包
[root@mail ~]# rpm -qa|grep sendmail
sendmail-8.13.8-2.el5
sendmail-cf-8.13.8-2.el5
[root@mail ~]# rpm -qa|grep m4
m4-1.4.5-3.el5.1
2:编辑access文件
[root@mail ~]# cd /etc/mail
[root@mail mail]# vim access
Connect:localhost.localdomain RELAY
Connect:localhost RELAY
Connect:127.0.0.1 RELAY
Connect:192.168.10 RELAY 允许192.168.10.0 网段的主机通过本电子邮件服务器转发邮件
bj.com OK
sh.com RELAY
3:编辑local-host-names文件
[root@mail mail]# vim local-host-names
# local-host-names - include all aliases for your machine here.
bj.com 该sendmail服务器所管理的区域
4:配置北京的dns服务器
[root@mail Server]# rpm -ivh bind-9.3.6-4.P1.el5.i386.rpm
[root@mail Server]# rpm -ivh bind-chroot-9.3.6-4.P1.el5.i386.rpm
[root@mail Server]# rpm -ivh caching-nameserver-9.3.6-4.P1.el5.i386.rpm
5:编辑named.conf的配置文件
[root@mail Server]# cd /var/named/chroot/
[root@mail chroot]# ll
total 12
drwxr-x--- 2 root named 4096 Mar 4 22:21 dev
drwxr-x--- 2 root named 4096 Mar 4 22:26 etc
dr-xr-xr-x 135 root root 0 Mar 4 23:19 proc
drwxr-x--- 6 root named 4096 Mar 4 22:21 var
[root@mail chroot]# cd etc/
[root@mail etc]# cp -p named.caching-nameserver.conf named.conf
6:编辑区域声明文件
[root@mail etc]# vim named.rfc1912.zones
7:创建该区域的数据库文件bj.com.db
[root@mail etc]# cd ../var/named/
[root@mail named]# cp -p localhost.zone bj.com.db
[root@mail named]# ll
-rw-r----- 1 root named 355 Mar 4 22:38 bj.com.db
-rw-r----- 1 root named 198 Jul 30 2009 localdomain.zone
-rw-r----- 1 root named 195 Jul 30 2009 localhost.zone
8:编辑该文件
9:修改主机的dns指向
[root@mail named]# vim /etc/resolv.conf
search xyh.com
nameserver 192.168.10.100
10:修改主机的名字
[root@mail named]# vim /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=mail.bj.com
[root@mail named]# vim /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 mail.bj.com
::1 localhost6.localdomain6 localhost6
11:将sendmail和named设置为自动启动
[root@mail named]# chkconfig sendmail on
[root@mail named]# chkconfig named on
12:在本机上建立两个用户
[root@mail named]# useradd user1
[root@mail named]# echo "123' |passwd --stdin user1
[root@mail named]# useradd user2
[root@mail named]# echo "123' |passwd --stdin user2
13:安装邮件接收服务器dovecat,并将其设置为自动服务
[root@mail ~]# yum install dovecot
[root@mail ~]# chkconfig dovecot on
[root@mail ~]# service dovecot restart
Stopping Dovecot Imap: [FAILED]
Starting Dovecot Imap: [ OK ]
[root@mail ~]#
14:重新启动机器
[root@mail named]# init 6
15:测试北京本地的两个用户是否可以互发邮件
这样的话我们就实现了本地的两个用户之间的互发邮件
二:上海分公司的配置
我们再次对以上的虚拟机进行克隆,并对相关的文件进行修改。
并在上海的邮件服务器创建user3,user4.
[root@mail named]# useradd user3
useradd: user user3 exists
[root@mail named]# echo "123"|passwd --stdin user3
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@mail named]# useradd user4
useradd: user user4 exists
[root@mail named]# echo "123"|passwd --stdin user4
Changing password for user user4.
passwd: all authentication tokens updated successfully.
User3和user4之间发邮件
三:由于dns的工作原理,我们没有连接互联网,所以我们在这里开启dns的转发功能
1:北京的dns服务器
[root@mail ~]# vim /var/named/chroot/etc/named.conf
2:上海的dns服务器
[root@mail named]# vim /var/named/chroot/etc/named.conf
四:测试
使用上海的user3的账户给北京的user1用户发一份邮件
[root@mail ~]# dig -t mx bj.com
; <<>> DiG 9.3.6-P1-RedHat-9.3.6-4.P1.el5 <<>> -t mx bj.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36535
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; QUESTION SECTION:
;bj.com. IN MX
;; ANSWER SECTION:
bj.com. 86400 IN MX 10 mail.bj.com.
;; AUTHORITY SECTION:
bj.com. 86400 IN NS ns.bj.com.
;; ADDITIONAL SECTION:
mail.bj.com. 86400 IN A 192.168.10.100
ns.bj.com. 86400 IN A 192.168.10.100
;; Query time: 11 msec
;; SERVER: 192.168.10.100#53(192.168.10.100)
;; WHEN: Mon Mar 5 03:19:56 2012
;; MSG SIZE rcvd: 94
[root@mail ~]# dig -t mx sh.com
; <<>> DiG 9.3.6-P1-RedHat-9.3.6-4.P1.el5 <<>> -t mx sh.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1025
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; QUESTION SECTION:
;sh.com. IN MX
;; ANSWER SECTION:
sh.com. 85138 IN MX 10 mail.sh.com.
;; AUTHORITY SECTION:
sh.com. 85138 IN NS ns.sh.com.
;; ADDITIONAL SECTION:
mail.sh.com. 85138 IN A 192.168.10.101
ns.sh.com. 85138 IN A 192.168.10.101
;; Query time: 2 msec
;; SERVER: 192.168.10.100#53(192.168.10.100)
;; WHEN: Mon Mar 5 03:20:06 2012
;; MSG SIZE rcvd: 94
在北京的user1上查看自己的邮件
这样的话我们的总公司和分公司之间就能够互相的发送和接收电子邮件啦。
优化:在北京的dns上面配置dns反向解析,增加邮件的收发速度。
四:接下来我们就需要对服务器颁发证书,对数据进行加密
在北京那边搭建自己的CA服务器(我们使用openssl来进行搭建)
1:安装各个包,并修改配置文件
[root@mail Server]# cd /etc/pki/
[root@mail pki]# LL
-bash: LL: command not found
[root@mail pki]# ll
total 36
drwx------ 3 root root 4096 Feb 8 01:41 CA
drwxr-xr-x 4 root root 4096 Mar 4 22:48 dovecot
drwxr-xr-x 2 root root 4096 Feb 8 01:40 nssdb
drwxr-xr-x 2 root root 4096 Feb 8 01:41 rpm-gpg
drwxr-xr-x 5 root root 4096 Feb 8 01:41 tls
[root@mail pki]# vim tls/openssl.cnf
2:切换到CA 目录下面,建立相应的目录和文件
[root@mail pki]# cd CA/
[root@mail CA]# ll
total 8
drwx------ 2 root root 4096 Jun 30 2009 private
[root@mail CA]# mkdir certs newcerts crl 证书目录 ,证书的吊销列表目录
[root@mail CA]# touch index.txt index.txt文件
[root@mail CA]# echo "01" >serial serial文件
3:为ca服务器建立自己的钥匙和证书文件
[root@mail CA]# openssl genrsa 1024 >private/cakey.pem产生钥匙文件
Generating RSA private key, 1024 bit long modulus
.......++++++
............................++++++
e is 65537 (0x10001)
[root@mail CA]# chmod 600 private/cakey.pem 改变私钥的相应的权限,增加安全性
[root@mail CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
##这里我们可以指明证书的格式为-x509,有效日期10年
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:BEIJING
Locality Name (eg, city) [Newbury]:BEIJING
Organization Name (eg, company) [My Company Ltd]:TECCENTRE
Organizational Unit Name (eg, section) []:TEC
Common Name (eg, your name or your server's hostname) []:root.net.net
Email Address []:
[root@mail CA]# ll
-rw-r--r-- 1 root root 1147 Mar 5 03:41 cacert.pem
drwxr-xr-x 2 root root 4096 Mar 5 03:38 certs
drwxr-xr-x 2 root root 4096 Mar 5 03:38 crl
-rw-r--r-- 1 root root 0 Mar 5 03:38 index.txt
drwxr-xr-x 2 root root 4096 Mar 5 03:38 newcerts
drwx------ 2 root root 4096 Mar 5 03:39 private
-rw-r--r-- 1 root root 3 Mar 5 03:38 serial
五:在北京的服务器上请求自己的证书
1:请求证书
[root@mail mail]# cd /etc/mail
[root@mail mail]# mkdir certs
[root@mail mail]# cd certs/
[root@mail certs]# openssl genrsa 1024 > sendmail.key
Generating RSA private key, 1024 bit long modulus
....++++++
.............................................++++++
e is 65537 (0x10001)
[root@mail certs]# openssl req -new -key sendmail.key -out sendmail.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:BEIJING
Locality Name (eg, city) [Newbury]:BEIJING
Organization Name (eg, company) [My Company Ltd]:bjdx
Organizational Unit Name (eg, section) []:student
Common Name (eg, your name or your server's hostname) []:mail.bj.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@mail certs]# openssl ca -in sendmail.csr -out sendmail.cert
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Mar 4 19:50:34 2012 GMT
Not After : Mar 4 19:50:34 2013 GMT
Subject:
countryName = CN
stateOrProvinceName = BEIJING
organizationName = bjdx
organizationalUnitName = student
commonName = mail.bj.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
0F:9D:CE:88:E6:94:F2:C4:89:C9:E6:FA:C3:DA:A6:7F:AB:5B:8D:03
X509v3 Authority Key Identifier:
keyid:97:3E:00:0C:F4:BF:06:46:63:E5:87:BC:45:14:5C:1E:62:54:FA:39
Certificate is to be certified until Mar 4 19:50:34 2013 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
2:修改钥匙和证书的权限
[root@mail certs]# chmod 600 *
[root@mail certs]# ll
total 12
-rw------- 1 root root 3082 Mar 5 03:50 sendmail.cert
-rw------- 1 root root 651 Mar 5 03:49 sendmail.csr
-rw------- 1 root root 887 Mar 5 03:47 sendmail.key
3:将证书和sendmail服务器捆绑
将服务器的证书考到sendmail的certs目录下
[root@mail certs]# cp /etc/pki/CA/cacert.pem .
[root@mail certs]# ll
total 16
-rw-r--r-- 1 root root 1147 Mar 5 03:54 cacert.pem
-rw------- 1 root root 3082 Mar 5 03:50 sendmail.cert
-rw------- 1 root root 651 Mar 5 03:49 sendmail.csr
-rw------- 1 root root 887 Mar 5 03:47 sendmail.key
[root@mail certs]# vim /etc/mail/sendmail.mc
4:开启tls的验证功能
5:重新启动sendmail的服务,验证starttls服务是否已经开启
[root@mail ~]# service sendmail restart
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
[root@mail ~]# telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to mail.bj.com (127.0.0.1).
Escape character is '^]'.
220 mail.bj.com ESMTP Sendmail 8.13.8/8.13.8; Mon, 5 Mar 2012 04:29:51 +0800
ehlo 127.0.0.1
250-mail.bj.com Hello mail.bj.com [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-STARTTLS 这里我们看到我们的smtp协议已经增加了ssl的功能
250-DELIVERBY
250 HELP
6:对接收服务器dovecot同样的颁发证书文件
[root@mail ~]# mkdir -p /etc/dovecot/certs
[root@mail ~]#
[root@mail ~]#
[root@mail ~]# cd /etc/dovecot/certs/
[root@mail certs]# mkdir certs newcerts crl
[root@mail certs]# touch index.txt
[root@mail certs]# echo "01" >serial
[root@mail certs]# ll
total 16
drwxr-xr-x 2 root root 4096 Mar 5 04:35 certs
drwxr-xr-x 2 root root 4096 Mar 5 04:35 crl
-rw-r--r-- 1 root root 0 Mar 5 04:35 index.txt
drwxr-xr-x 2 root root 4096 Mar 5 04:35 newcerts
-rw-r--r-- 1 root root 3 Mar 5 04:35 serial
[root@mail certs]# openssl genrsa 1024 >dovecot.key
Generating RSA private key, 1024 bit long modulus
.......++++++
.................................++++++
e is 65537 (0x10001)
[root@mail certs]# openssl req -new -key dovecot.key -out dovecot.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:BEIJING
Locality Name (eg, city) [Newbury]:BEIJING
Organization Name (eg, company) [My Company Ltd]:BJDX
Organizational Unit Name (eg, section) []:SEC
Common Name (eg, your name or your server's hostname) []:mail.bj.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@mail certs]# openssl ca -in dovecot.csr -out dovecot.cert
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Mar 4 20:39:09 2012 GMT
Not After : Mar 4 20:39:09 2013 GMT
Subject:
countryName = CN
stateOrProvinceName = BEIJING
organizationName = BJDX
organizationalUnitName = SEC
commonName = mail.bj.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
75:37:BA:7A:1C:7D:FA:96:B6:0E:33:BF:C0:A9:85:21:FD:41:DC:BC
X509v3 Authority Key Identifier:
keyid:97:3E:00:0C:F4:BF:06:46:63:E5:87:BC:45:14:5C:1E:62:54:FA:39
Certificate is to be certified until Mar 4 20:39:09 2013 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
对证书进行绑定
[root@mail certs]# vim /etc/dovecot.conf
7:查看相应的加密的端口是否已经开放
Smtps 发送的加密
[root@mail certs]# netstat -tupln|grep sendmail
tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN 3614/sendmail: acce
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 3614/sendm
[root@mail certs]# grep 465 /etc/services
smtps 465/tcp # SMTP over SSL (TLS)
pop3s ,imaps接收的加密
root@mail certs]# netstat -tupln|grep dovecot
tcp 0 0 :::993 :::* LISTEN 2847/dovecot
tcp 0 0 :::995 :::* LISTEN 2847/dovecot
tcp 0 0 :::110 :::* LISTEN 2847/dovecot
tcp 0 0 :::143 :::* LISTEN 2847/dovec
[root@mail certs]# grep 995 /etc/services
pop3s 995/tcp # POP-3 over SSL
[root@mail certs]# grep 993 /etc/services
imaps 993/tcp # IMAP over SSL
8:安装wireshark的抓包工具,进行发送和接收的验证
[root@mail ~]# yum install wireshark*
验证明文的pop3协议
[root@mail ~]# tshark -ni eth0 -R "tcp.dstport eq 110"
3:smtps发送的加密验证
查看日志
在客户端软件上进行认证的设置
用tshark进行抓包,分析结果
[root@mail ~]# tshark -ni eth0 -R "tcp.dstport eq 993" imaps协议
Pop3s 协议
这样我们已经看到我们的电子邮件服务器已经启用了发送的和接收的加密功能。
六:在服务器上启用SASL功能 SASL (简单认证安全层)服务名为:saslauthd
默认已经进行了安装
1:[root@mail Server]# rpm -qa|grep sasl
cyrus-sasl-lib-2.1.22-5.el5
cyrus-sasl-2.1.22-5.el5
cyrus-sasl-devel-2.1.22-5.el5
cyrus-sasl-plain-2.1.22-5.el5:
2:[root@mail Server]# chkconfig saslauthd on 设置该服务为自动的启动
[root@mail Server]# chkconfig --list saslauthd
saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@mail Server]# service saslauthd start
Starting saslauthd: [ OK ]
3:[root@mail Server]# vim /etc/mail/sendmail.mc 修改sendmail的主配置文档
39 define(`confAUTH_OPTIONS', `A y')dnl
52 TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
53define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
116 DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA,M=Ea')dnl
[root@mail Server]# service sendmail restart
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
4:在北京的服务器上进行验证
mail from [email protected] 非注册的用户
530 5.7.0 Authentication required
mail from [email protected] 注册的用户
530 5.7.0 Authentication required
5:接下来我们对注册的用户的账号和密码进行base64的验证
6:再次验证
7:在服务器上查看邮件是否已经发送
8:[root@mail ~]# tail -f /var/log/maillog 查看服务器上的日志文件
小结:邮件服务器我们已经搭建成功了,并且实现了一定的安全功能。