什么是邮件?

邮件系统的介绍

  • 简单邮件传输协议(Simple Mail Transfer Protocol,SMTP):用于发送和中转发出的电子邮件,占用服务器的25/TCP端口。
  • 邮局协议版本3(Post Office Protocol 3):用于将电子邮件存储到本地主机,占用服务器的110/TCP端口。
  • Internet消息访问协议版本4(Internet Message Access Protocol 4):用于在本地主机上访问邮件,占用服务器的143/TCP端口。
  • 一个最基础的电子邮件系统肯定要能提供发件服务和收件服务,为此需要使用基于SMTP协议的Postfix服务程序提供发件服务功能,并使用基于POP3协议的Dovecot服务程序提供收件服务功能。这样一来,用户就可以使用Outlook Express或Foxmail等客户端服务程序正常收发邮件了。

SMTP

postfix

Postfix 是一种电子邮件服务器,它是由任职于IBM华生研究中心(T.J. Watson Research Center)的荷兰籍研究员Wietse Venema为了改良sendmail邮件服务器而产生的。最早在1990年代晚期出现,是一个开放源代码的软件。

特点介绍
  1. postfix是免费的:

postfix想要作用的范围是广大的Internet用户,试图影响大多数的Internet上的电子邮件系统,因此它是免费的。

  1. 更快:

postfix在性能上大约比sendmail快三倍。一部运行postfix的台式PC每天可以收发上百万封邮件。

  1. 兼容性好:

postfix是sendmail兼容的,从而使sendmail用户可以很方便地迁移到postfix。Postfix支持/var[/spool]/mail、/etc/aliases、 NIS、和 ~/.forward 文件。

  1. 更健壮:

postfix被设计成在重负荷之下仍然可以正常工作。当系统运行超出了可用的内存或磁盘空间时,postfix会自动减少运行进程的数目。当处理的邮件数目增长时,postfix运行的进程不会跟着增加。

  1. 更灵活:

postfix是由超过一打的小程序组成的,每个程序完成特定的功能。你可以通过配置文件设置每个程序的运行参数。

  1. 安全性

postfix具有多层防御结构,可以有效地抵御恶意入侵者。如大多数的postfix程序可以运行在较低的权限之下,不可以通过网络访问安全性相关的本地投递程序等等。

Qmail

qmail是一个因特网邮件传送代理(简写为MTA),它运行在linux/Unix兼容系统下,是一个直接代替UNIX下Sendmail软件的邮件传送程序。qmail使用SMTP协议与其它系统上的MTA交换邮件。

sendmail

  • sendmail,计算机用语,是一种最重要的邮件传输代理程序。
  • 人们在互联网上最常使用的就是电子邮件,很多企业用户也经常使用免费电子邮件系统。本文就以step by step的方式引导用户从sendmail源代码开始构建一个可以满足基本工作需要的邮件系统。

比较

Sendmail

毫无疑问,sendmail是最古老的MTA之一。它比qmail和postfix要古老得多。最早它诞生的时候,Internet还没有被标准化,当时主机之间使用的是UUCP技术来交换邮件。

它被设计得比较灵活,便于配置和运行于各种类型的机器。

Qmail

qmail 是新生一代的MTA代表,它以速度快、体积小、易配置安装等特性而著称。作者D. J. Bernstein(djb)是一个数学教授,富有传奇色彩。djb于1995年开发qmail,1996年发布0.70版,并使用了多种当时比较先进的 技术,包括Maildir,与sendmail单个binary不同的模块化设计,权限分离,以及使用了大量由djb编写的配套工具,如 daemontools,ucsip-tcp等。

qmail迅速成为了Internet上最有名的MTA,使用者众。

Postfix

Postfix作者是Wietse Venema,一名著名的安全专家。最早postfix起源于1996年,当时venema 在美国IBM研究中心负责研究更安全的邮件系统,当时称为Vmailer。后因为商标问题于1998年11月正式更名为Postfix

Postfix 以替代sendmail为目的,并提供了一个更安全、更高性能的灵活的体系。它同样也采用模块化设计,使用了大量优秀的技术,以达到安全的目的。由于作者 的设计理念独到,经过7,8年时间,Postfix现今已发展成为功能非常丰富,扩展性和安全性强的优秀MTA。

pop3

dovecot

Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,支持 Linux/Unix 系统。

简介

  • POP / IMAP 是 MUA 从邮件服务器中读取邮件时使用的协议。其中, POP3协议是从邮件服务器中下载邮件存起来,IMAP4 则是将邮件留在服务器端直接对邮件进行管理、操作。Dovecot 是一个比较新的软件,由 Timo Sirainen 开发,最初发布于 2002年7月。作者将安全性考虑在第一,所以 Dovecot 在安全性方面比较出众。另外,Dovecot 支持多种认证方式,所以在功能方面也比较符合一般的应用。 [1]

优势

  • Dovecot 是一个比较新的软件,由 Timo Sirainen 开发,最初发布于 2002年7月。作者将安全性考虑在第一,所以 Dovecot 在安全性方面比较出众。另外,Dovecot 支持多种认证方式,所以在功能方面也比较符合一般的应用。

缺陷

  • Dovecot邮件系统没有正确地解析某些邮件头。如果远程攻击者发送了带有无效消息地址的恶意邮件的话,则IMAP客户端使用FETCH ENVELOPE命令从服务器收取邮件会导致之后的收件人无法接收邮件,因为进程会一直试图解析无效的邮件头直到崩溃。

经典搭配

  • postfix(作为发送邮件服务器)+dovecot(作为接收邮件服务器)+mysql(作为数据库)

搭建postfix+dovecot+ssl

环境

主机名

ip地址

角色

系统

mail.skills.com

10.10.20.101

邮件服务器

Rocky linux

搭建DNS

#安装服务
[root@mail ~]# yum install -y bind
#编辑主配置文件
[root@mail ~]# vim /etc/named.conf 
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

options {
        listen-on port 53 { any; };   
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        secroots-file   "/var/named/data/named.secroots";
        recursing-file  "/var/named/data/named.recursing";
        allow-query     { any; };   #指明允许所有主机提出询问

        /* 
         - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
         - If you are building a RECURSIVE (caching) DNS server, you need to enable 
           recursion. 
         - If your recursive DNS server has a public IP address, you MUST enable access 
           control to limit queries to your legitimate users. Failing to do so will
           cause your server to become part of large scale DNS amplification 
           attacks. Implementing BCP38 within your network would greatly
           reduce such attack surface 
        */
        recursion yes;

        dnssec-enable yes;
        dnssec-validation yes;

        managed-keys-directory "/var/named/dynamic";

        pid-file "/run/named/named.pid";
        session-keyfile "/run/named/session.key";

        /* https://fedoraproject.org/wiki/Changes/CryptoPolicy */
        include "/etc/crypto-policies/back-ends/bind.config";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
        type hint;
        file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
#配置区域配置文件   #追加以下内容
[root@mail ~]# vim  /etc/named.rfc1912.zones 
zone "skills.com" IN {
        type master;
        file "skills.com.zone";
        allow-update { none; };
};
zone "20.10.10.in-addr.arpa" IN {
        type master;
        file "10.10.20.arpa";
        allow-update { none; };
};
#配置正反向解析\
[root@mail ~]# cd /var/named/
[root@mail named]# cp -p named.localhost skills.com.zone
[root@mail named]# cp -p named.loopback 10.10.20.arpa
[root@mail named]# vim skills.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      mail.skills.com.
        MX      10      mail.skills.com.  #邮件记录
        A       10.10.20.101
mail    A       10.10.20.101
[root@mail named]# vim 10.10.20.arpa 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      mail.skills.com.
        A       10.10.20.101
101     PTR     mail.skills.com.

SSL

用自签和颁发的都行,按照题目上的来,这里我就用自签的啦。

[root@mail ~]# cd /etc/ssl/
#创建私钥
[root@mail ssl]# openssl genrsa -out skills.key
Generating RSA private key, 2048 bit long modulus (2 primes)
................+++++
.......................................................................................................................................+++++
e is 65537 (0x010001)
#创建证书请求文件
[root@mail ssl]# openssl req -new -key skills.key -out skills.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) [XX]:CN
State or Province Name (full name) []:HN
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:skills
Organizational Unit Name (eg, section) []:system
Common Name (eg, your name or your server's hostname) []:skills.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 ssl]# openssl x509 -req -signkey skills.key -in skills.csr -out skills.crt
Signature ok
subject=C = CN, ST = HN, L = ZZ, O = skills, OU = system, CN = skills.com
Getting Private key

postfix

#安装服务
[root@mail ~]# yum install -y postfix
#修改配置
[root@mail ~]# cd /etc/postfix/
[root@mail postfix]# vim main.cf	
 94 myhostname = mail.skills.com  #设置主机名
 102 mydomain = skills.com		#设置域名
 118 myorigin = $mydomain		#设置组织域
 135 inet_interfaces = all   	#设置网络区域
 138 inet_protocols = all		#设置协议
 183 mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain  ##我们需要在mydestination的值后面添加 $mydomain ,如果不添加的话,收件人的地址会有问题,如果添加后,用户发送邮件直接会是xx@domain.com
 438 home_mailbox = Maildir/   	#配置邮件存储位置
 709 smtpd_tls_cert_file = /etc/ssl/skills.crt   #指定证书
 715 smtpd_tls_key_file = /etc/ssl/skills.key	#指定密钥
[root@mail postfix]# vim master.cf
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master" or
# on-line: http://www.postfix.org/master.5.html).
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (no)    (never) (100)
# ==========================================================================
#smtp      inet  n       -       n       -       -       smtpd          #注释
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
#submission inet n       -       n       -       -       smtpd
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_tls_auth_only=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       n       -       -       smtpd  #取消注释
  -o syslog_name=postfix/smtps    						#取消注释
  -o smtpd_tls_wrappermode=yes						#取消注释
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

dovecot

#安装服务
[root@mail ~]# yum install -y dovecot
[root@mail ~]# cd /etc/dovecot/
[root@mail dovecot]# vim dovecot.conf 
 24 protocols = imap pop3 lmtp submission  #取消注释,开启验证类型
 30 listen = *, ::  			#启用端口监听
 48 login_trusted_networks = 10.10.20.0/24   #允许指定网段使用,屏蔽掉默认all
 98 include conf.d/*.conf		#引用文件
[root@mail dovecot]# cd conf.d/  #修改4个子配置文件
[root@mail conf.d]# vim 10-auth.conf 
 10 disable_plaintext_auth = yes		#解除注释
[root@mail conf.d]# vim 10-mail.conf 
 24    mail_location = maildir:~/Maildir		#解除注释
[root@mail conf.d]# vim 10-ssl.conf 
  8 ssl = required     	  #指定开启SSL模式,并指定证书位置.
 14 ssl_cert = </etc/ssl/skills.crt
 15 ssl_key = </etc/ssl/skills.key
 [root@mail conf.d]# vim 10-logging.conf  #启用dovecot的日志
  10 info_log_path = /var/log/dovecot_info.log
  12 debug_log_path = /var/log/dovecot_debug.log

重启服务并设置为开机自启

[root@mail ~]# systemctl enable --now postfix.service dovecot.service

防火墙

[root@mail ~]# firewall-cmd --add-service=pop3s
[root@mail ~]# firewall-cmd --add-service=smtps
[root@mail ~]# setenforce 0

创建测试用户

[root@mail ~]# useradd mail1
[root@mail ~]# useradd mail2
#设置密码
[root@mail ~]# passwd mail1
[root@mail ~]# passwd mail2

设置别名

vim /etc/aliases  (给all发邮件,mail1和mail2同样会收到)
all:    mail1,mail2
#启用
mail:~# newaliases