Postfix 与 SASL

出自 ArchWiki

SMTP 协议规范包含用户身份验证的可能性,但没有提供协议消息交换的确切细节,而是推迟到 SASL(简单身份验证和安全层)标准(参见 RFC 4954RFC 4422)。SASL 是用于身份验证机制的通用身份验证框架,其中有很多机制,并且每种机制都有其自己的特定过程,该过程规定了使用身份验证数据执行必要的加密步骤以及通过连接交换的消息。因此,为了避免对可以与 Postfix 一起使用的身份验证机制施加人为限制,Postfix 本身不使用用户名和密码或通过任何其他方式对 SMTP 用户进行身份验证。它将此任务卸载到必须单独安装的 SASL 实现。SASL 身份验证守护程序负责策略(即,有效用户名和密码等密钥的保存位置)和机制(客户端如何准确提供凭据)。这与例如 OpenSMTPD 形成对比,后者仅支持 PLAIN 和 LOGIN SASL 机制,但不依赖于任何外部库或守护程序。

简介

在本文中,您将学习如何为 Postfix 设置 SASL 身份验证。

一旦 Postfix 启动并运行,您可以添加 SASL 身份验证以避免中继。为了防止匿名用户发送垃圾邮件,只有经过身份验证和受信任的用户才能发送电子邮件。

由于 [extra] 中的 postfix 软件包已经编译了 SASL 支持,要启用 SASL 身份验证,您有两种选择

  • 使用 cyrus-sasl 软件包。
  • 或启用您已配置的 Dovecot 来处理 Postfix 身份验证(以及其自身的身份验证)。

来自 Postfix 站点

那些费力安装 Postfix 的人可能期望 Postfix 比其他一些邮件程序更安全。Cyrus SASL 库包含大量代码。有了这个,Postfix 变得与其他使用 Cyrus SASL 库的邮件系统一样安全。Dovecot 提供了一种可能值得考虑的替代方案。

使用 cyrus-sasl 软件包配置

安装 cyrus-sasl 软件包。

要启用 SASL 以接受来自其他用户的邮件,请在 /etc/postfix/master.cf 中取消注释这些行(默认情况下已存在,只是被注释掉),以打开 “消息提交” 端口 (TCP 587)

submission inet n       -       n       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -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=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

请注意,这也启用了 SSL,因此如果您没有 SSL 证书,请将 "smtpd_tls_security_level" 选项注释掉。

三个限制选项(client、helo、sender)也可以保留注释,因为 smtpd_recipient_restrictions 已经处理 SASL 用户。

SASL 可以使用不同的身份验证方法。默认方法是 PAM(在 /etc/conf.d/saslauthd 中配置),但是要正确设置它,您必须创建 /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
log_level: 7

由于 pambase 20190105.1-1 及更高版本对 "other" PAM 服务使用限制性回退,因此现在需要 pam 配置文件。[1][2]

创建 /etc/pam.d/smtp

#%PAM-1.0
auth            required        pam_unix.so
account         required        pam_unix.so

如果将 PAM 与 openldap 结合使用进行身份验证,则可以构建一个替代 pam 配置文件 (/etc/pam.d/smtp),类似于:LDAP 身份验证#PAM 配置

#%PAM-1.0
auth            sufficient      pam_ldap.so     try_first_pass minimum_uid=2000
auth            required        pam_unix.so

account         sufficient      pam_ldap.so     minimum_uid=2000
account         required        pam_unix.so

启动/启用 saslauthd.service

重启 postfix.service

如果想要验证 SASL-PAM 身份验证过程,可以运行以下命令来确定 SASL 是否可以通过 PAM 进行身份验证

# testsaslauthd -u <username> -p <password> -s smtp

希望您应该能够使用 telnet 连接到您的 Postfix 服务器

telnet localhost 587

然后您应该输入

EHLO example.com

这大致是您应该看到的

Trying 127.0.0.1...

Connected to localhost.localdomain
Escape character is '^]'

220 justin ESMTP Postfix
EHLO example.com
250-justin
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN OTP DIGEST-MD5 CRAM-MD5
250 8BITMIME

使用 Dovecot 配置

如果您正在使用 Dovecot 作为您的 IMAP 或 POP 邮件服务器,并且您的用户已经通过身份验证(可能使用 PAM),则无需配置另一个软件包。

只需编辑 /etc/postfix/master.cf 并在 submissionsmtp 部分(取决于您正在使用的部分)下添加以下行

  # SASL authentication with dovecot
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_sasl_type=dovecot
  -o smtpd_sasl_path=private/auth
  -o smtpd_sasl_security_options=noanonymous
  -o smtpd_sasl_local_domain=$myhostname
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o smtpd_recipient_restrictions=reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject

使用此配置意味着只有经过身份验证的用户才能发送邮件。您可以从 smtpd_client_restrictions 选项中看到这一点。

现在将以下内容添加到 /etc/dovecot/conf.d/10-master.conf 中的 Dovecot 配置文件中

service auth {
  unix_listener /var/spool/postfix/private/auth {
    group = postfix
    mode = 0660
    user = postfix
  }
  user = root
}

如您所见,在 /var/spool/postfix/private/auth 中创建了一个 unix 套接字,这与 master.cfsmtpd_sasl_path 选项中指定的相同

最后重启 postfix 和 dovecot 服务。

参见