Dovecot
Dovecot 是一个开源的 IMAP 和 POP3 服务器,用于类 Linux/UNIX 系统,主要以安全性为设计理念编写。Dovecot 的主要目标是成为一个轻量级、快速且易于设置的开源邮件服务器。更多详细信息,请参阅官方 Dovecot Wiki。
本文介绍了如何为个人或小型办公室使用设置 Dovecot。
安装
配置
假设
- 由 Dovecot 服务的每个邮件帐户,在服务器上都有一个本地用户帐户定义。
- 服务器使用 PAM 来针对本地用户数据库(
/etc/passwd
)验证用户。 - 使用 TLS 加密身份验证密码。
- 通用的 Maildir 格式用于在用户的主目录中存储邮件。
- 已经设置了 MDA 以将邮件传递给本地用户。
创建 TLS 证书
要获取证书,请参阅 OpenSSL#用法。
或者,您可以使用 dovecot 软件包附带的脚本生成证书
- 复制示例配置文件:以 root 用户身份执行
cp /usr/share/doc/dovecot/dovecot-openssl.cnf /etc/ssl/dovecot-openssl.cnf
。 - 编辑
/etc/ssl/dovecot-openssl.cnf
以配置证书。 - 以 root 用户身份执行
/usr/lib/dovecot/mkcert.sh
以生成证书。
证书/密钥对创建为 /etc/ssl/certs/dovecot.pem
和 /etc/ssl/private/dovecot.pem
。
每当您更改证书时,以 root 用户身份运行 cp /etc/ssl/certs/dovecot.pem /etc/ca-certificates/trust-source/anchors/dovecot.crt
,然后运行 trust extract-compat
。
Dovecot 配置
- 创建 dovecot 配置文件目录
/etc/dovecot
。 - 将
dovecot.conf
和conf.d/*
配置文件从/usr/share/doc/dovecot/example-config
复制到/etc/dovecot
# mkdir /etc/dovecot # cp /usr/share/doc/dovecot/example-config/dovecot.conf /etc/dovecot/dovecot.conf # cp -r /usr/share/doc/dovecot/example-config/conf.d /etc/dovecot
默认配置对于大多数系统来说是正常的,但请务必通读配置文件以查看有哪些选项可用。请参阅 快速配置指南 和 dovecot 配置 以获取更多说明。
默认情况下,dovecot 将尝试检测系统上正在使用的邮件存储系统。要使用 Maildir 格式,请编辑 /etc/dovecot/conf.d/10-mail.conf
以设置 mail_location = maildir:~/Maildir
。
生成 DH 参数
要生成新的 DH 参数文件(这将花费很长时间)
# openssl dhparam -out /etc/dovecot/dh.pem 4096
然后将该文件添加到 /etc/dovecot/conf.d/10-ssl.conf
ssl_dh = </etc/dovecot/dh.pem
PAM 认证
要启用 Dovecot 的 PAM 身份验证,请按照本节操作,然后选择 #使用 LDAP 的 PAM 认证 或 #使用 SSSD 的 PAM 认证。
编辑 /etc/dovecot/conf.d/auth-system.conf.ext
,删除 PAM 身份验证部分前面的注释,如下所示
# PAM authentication. Preferred nowadays by most systems. # PAM is typically used with either userdb passwd or userdb static. # REMEMBER: You'll need /etc/pam.d/dovecot file created for PAM # authentication to actually work. <doc/wiki/PasswordDatabase.PAM.txt> passdb { driver = pam # [session=yes] [setcred=yes] [failure_show_msg=yes] [max_requests=<n>] # [cache_key=<key>] [<service name>] args = session=yes dovecot }
如果您还想记录登录失败,请将 failure_show_msg=yes
添加到 args。
通过使用 pam_mkhomedir.so
模块并在 passdb
指令中添加 session
部分,如果 LDAP 用户首次登录,将自动创建相应的家目录。
使用 LDAP 的 PAM 认证
如果您使用 OpenLDAP 或 389-ds-base 服务器进行身份验证,请务必首先能够使用您的 LDAP 用户登录,如 LDAP 身份验证 中所述。然后,您可以在 /etc/pam.d/dovecot
中编写以下内容,记住条目顺序非常重要
/etc/pam.d/dovecot
auth sufficient pam_ldap.so auth required pam_unix.so nullok account sufficient pam_ldap.so account required pam_unix.so session required pam_mkhomedir.so skel=/etc/skel umask=0022 session sufficient pam_ldap.so
这样,LDAP 用户和系统用户都拥有自己的邮箱。
使用 SSSD 的 PAM 认证
如果您使用 SSSD 进行身份验证,则可以在 /etc/pam.d/dovecot
中编写以下内容,记住条目顺序非常重要
/etc/pam.d/dovecot
auth sufficient pam_sss.so auth required pam_unix.so nullok account sufficient pam_sss.so account required pam_unix.so session required pam_mkhomedir.so skel=/etc/skel umask=0022 session sufficient pam_sss.so
这样,LDAP 用户和系统用户都拥有自己的邮箱。
Sieve
Sieve 是一种编程语言,可用于在邮件服务器上创建电子邮件过滤器。
Sieve 解释器插件
这有助于在传递时进行实际的 Sieve 过滤。
- 安装 pigeonhole。
- 根据您的使用情况,将
sieve
添加到/etc/dovecot/conf.d/15-lda.conf
protocol lda { mail_plugins = $mail_plugins sieve }
- 和/或
/etc/dovecot/conf.d/20-lmtp.conf
中的mail_plugins
。protocol lmtp { mail_plugins = $mail_plugins sieve }
- 可选地,在
plugin
部分添加配置。有关配置选项和默认值,请参阅 Sieve 解释器文档。
示例:运行cp /usr/share/doc/dovecot/example-config/conf.d/90-sieve.conf /etc/dovecot/conf.d/90-sieve.conf
,并在/etc/dovecot/conf.d/90-sieve.conf
中验证plugin { sieve = file:~/sieve;active=~/.dovecot.sieve }
/etc/dovecot/dovecot.conf
中没有类似 !include /etc/dovecot/conf.d/*.conf
的行,则不会读取 /etc/dovecot/conf.d/
中的配置文件。如果您正在遵循 虚拟用户邮件系统 指南,您可能需要添加此行。示例:SpamAssassin - 将垃圾邮件移动到 “Junk” 文件夹
- 添加 spamtest 配置
/etc/dovecot/conf.d/90-sieve.conf
plugin { sieve_extensions = +spamtest +spamtestplus sieve_spamtest_status_type = score sieve_spamtest_status_header = \ X-Spam_score: (-?[[:digit:]]+\.[[:digit:]]).* sieve_spamtest_max_value = 5.0 sieve_before = /var/lib/dovecot/sieve/global_sieves/move_to_spam_folder.sieve }
注意: 这会测试 “X-Spam_score”(这是默认 Exim 配置中的垃圾邮件标头格式)。您的标头可能看起来不同,例如 “X-Spam-Score”。
- 创建 sieve 脚本:
mkdir -p /var/lib/dovecot/sieve/global_sieves
/var/lib/dovecot/sieve/global_sieves/move_to_spam_folder.sieve
require "spamtestplus"; require "fileinto"; require "relational"; require "comparator-i;ascii-numeric"; if spamtest :value "ge" :comparator "i;ascii-numeric" "5" { fileinto "Junk"; }
- 要编译 sieve,请在 shell 中执行
sievec /var/lib/dovecot/sieve/global_sieves
并确保move_to_spam_folder.sieve
和生成的move_to_spam_folder.svbin
文件是全局可读的。
ManageSieve 服务器
这实现了 ManageSieve 协议,用户可以通过该协议远程管理服务器上的 Sieve 脚本。
- 按照上面的 #Sieve 解释器插件 中的步骤操作。
- 将
sieve
添加到dovecot.conf
中的protocols
。protocols = imap pop3 sieve
- 添加最少的
/etc/dovecot/conf.d/20-managesieve.conf
service managesieve-login { } service managesieve { } protocol sieve { }
- 重启
dovecot
。managesieve 守护程序将默认监听端口 4190。
全文搜索
默认情况下,Dovecot 不会索引完整的邮件内容,这会导致较大邮箱的 IMAP SEARCH
查询响应时间缓慢。Dovecot 可以连接到许多 FTS 后端。
Dovecot 需要所选搜索后端的插件。solr 插件包含在 dovecot 中,但 solr 本身不易设置。有用于 Xapian (dovecot-fts-xapian) 和 Elasticsearch (dovecot-fts-elastic) 的软件包。
启动服务器
启动/启用 dovecot.service
。
技巧与窍门
使用非默认哈希函数生成哈希值
$ doveadm pw -s SHA512-CRYPT -p "password"
确保数据库中的列足够大。如果太小,将发出警告。
记住设置密码密码方案
dovecot-sql.conf
default_pass_scheme = SHA512-CRYPT
故障排除
警告: pipe flag `D' 需要 dovecot_destination_recipient_limit = 1
如果您无法接收到多个收件人的电子邮件,并且您的日志中出现类似这样的内容
mail postfix/pipe[663]: 72A62733: to=<user2@example.com>, relay=dovecot, delay=495, delays=495/0.01/0/0.08, dsn=4.3.5, status=deferred (mail system configuration error) mail postfix/pipe[663]: 72A62733: to=<user1@example.com>, relay=dovecot, delay=495, delays=495/0.01/0/0.06, dsn=4.3.5, status=deferred (mail system configuration error) mail postfix/pipe[1614231]: warning: pipe flag `D' requires dovecot_destination_recipient_limit = 1
将 dovecot_destination_recipient_limit = 1
添加到 /etc/postfix/main.cf
并重新加载 postfix。