使用 Postfix 将外发邮件限制到发件人地址中的特定域

使用 Postfix 将外发邮件限制到发件人地址中的特定域

出于某些让我想伤害自己的原因,我需要支持的应用程序能够使用任何域的“MAIL FROM”地址发送邮件。由于我无法进一步锁定该应用程序,我希望 Postfix 对所有试图离开我的网络并发往世界其他地方的邮件进行健全性检查。

我只希望允许来自内部的邮件发出,如果其发件人地址为“example.com”。如果来自内部的邮件的发件人地址为“someotherdomain.com”,则应通过 Postfix 阻止该邮件。

为了澄清起见,我该如何配置 Postfix 以仅允许来自我的本地网络内的邮件发出,并且如果该邮件的发件人地址是我的某个域名?

到目前为止,我唯一能想到的方法如下。但是还有更简单的方法吗?

/etc/postfix/main.cf:

smtpd_restriction_classes =
        external_sender_access
        internal_sender_access

# Intended for mail originating from outside our networks
external_sender_access =
        # Verify MAIL_FROM on incoming mail
        check_sender_access hash:/etc/postfix/external_sender_access
        # Allow all other incoming mail
        permit

# Intended for mail originating from within our networks
internal_sender_access =
        # Verify MAIL_FROM on outgoing mail
        check_sender_access hash:/etc/postfix/internal_sender_access
        # Block all other outbound mail
        reject

# Restrictions applied in the context of the MAIL FROM command.
smtpd_sender_restrictions =
        reject_non_fqdn_sender
        reject_unknown_sender_domain
        # Access rules for specific 'sender' data based upon client IP
        check_client_access cidr:/etc/postfix/network_sender_access
        permit
/etc/postfix/network_sender_access:

# Localhost
127.0.0.0/24        internal_sender_access

# Inside Networks
192.168.0.0/16      internal_sender_access

# Everything else
0.0.0.0/0           external_sender_access
/etc/postfix/internal_sender_access:

example.com OK
.example.com OK
/etc/postfix/external_sender_access:

example.com REJECT You're not from here!
.example.com REJECT You're not from here!

此配置的 postconf -n 输出:

alias_database = dbm:/etc/aliases
alias_maps = hash:/etc/aliases
biff = no
body_checks = pcre:/etc/postfix/body_checks
config_directory = /etc/postfix
content_filter = smtp-amavis:[127.0.0.1]:10024
disable_vrfy_command = yes
external_sender_access = check_sender_access hash:/etc/postfix/external_sender_access permit
header_checks = pcre:/etc/postfix/header_checks
home_mailbox = Maildir/
inet_protocols = ipv4,ipv6
internal_sender_access = check_sender_access hash:/etc/postfix/internal_sender_access reject
local_header_rewrite_clients = permit_inet_interfaces,permit_mynetworks
mailbox_command = /usr/bin/procmail -t
mailbox_size_limit = 0
manpage_directory = /usr/share/man
minimal_backoff_time = 1800s
mydestination = $myorigin, $myhostname, localhost.$mydomain, localhost
mynetworks = /etc/postfix/local_networks
queue_directory = /data/postfix
recipient_delimiter = +
smtp_generic_maps = pcre:/etc/postfix/generic
smtpd_banner = $myhostname ESMTP
smtpd_client_restrictions = permit_mynetworks check_client_access hash:/etc/postfix/client_access permit
smtpd_data_restrictions = reject_unauth_pipelining reject_multi_recipient_bounce permit
smtpd_delay_reject = yes
smtpd_helo_required = yes
smtpd_helo_restrictions = reject_invalid_helo_hostname reject_non_fqdn_helo_hostname permit_mynetworks check_client_access hash:/etc/postfix/client_access permit
smtpd_recipient_restrictions = permit_mynetworks check_client_access hash:/etc/postfix/rbl_override reject_rbl_client zen.spamhaus.org permit
smtpd_relay_restrictions = reject_non_fqdn_recipient reject_unknown_recipient_domain regexp:/etc/postfix/regexp_access permit_mynetworks reject_unauth_destination reject_unlisted_recipient check_policy_service inet:127.0.0.1:10023 permit
smtpd_restriction_classes = external_sender_access internal_sender_access
smtpd_sender_restrictions = reject_non_fqdn_sender reject_unknown_sender_domain check_client_access cidr:/etc/postfix/network_sender_access permit
strict_rfc821_envelopes = yes
virtual_alias_domains = example.com
virtual_alias_maps = hash:/etc/postfix/virtual.d/example.com

编辑:下面是我尝试使用“reject_unlisted_sender”的替代配置。

当我尝试使用此配置时,邮件发送“来自:[电子邮件保护]' 被反弹(正如预期的那样),但发送“发件人:blah@not_my_domain.com”的邮件却没有任何问题,这正是我不想要的。

# Restrictions applied in the context of the MAIL FROM command.
smtpd_sender_restrictions =
        reject_non_fqdn_sender
        reject_unknown_sender_domain
        check_client_access cidr:/etc/postfix/outgoing_senders
        # Access rules for specific 'sender' data
        check_sender_access hash:/etc/postfix/sender_access
        permit
/etc/postfix/outgoing_senders:
192.168.0.0/16  reject_unlisted_sender, permit
/etc/postfix/sender_access:

example.com REJECT You're not from here!
.example.com REJECT You're not from here!

此配置的 postconf -n 输出:

alias_database = dbm:/etc/aliases
alias_maps = hash:/etc/aliases
biff = no
body_checks = pcre:/etc/postfix/body_checks
config_directory = /etc/postfix
content_filter = smtp-amavis:[127.0.0.1]:10024
disable_vrfy_command = yes
header_checks = pcre:/etc/postfix/header_checks
home_mailbox = Maildir/
inet_protocols = ipv4,ipv6
local_header_rewrite_clients = permit_inet_interfaces,permit_mynetworks
mailbox_command = /usr/bin/procmail -t
mailbox_size_limit = 0
manpage_directory = /usr/share/man
minimal_backoff_time = 1800s
mydestination = $myorigin, $myhostname, localhost.$mydomain, localhost
mynetworks = /etc/postfix/local_networks
queue_directory = /data/postfix
recipient_delimiter = +
smtp_generic_maps = pcre:/etc/postfix/generic
smtpd_banner = $myhostname ESMTP
smtpd_client_restrictions = permit_mynetworks check_client_access hash:/etc/postfix/client_access permit
smtpd_data_restrictions = reject_unauth_pipelining reject_multi_recipient_bounce permit
smtpd_delay_reject = yes
smtpd_helo_required = yes
smtpd_helo_restrictions = reject_invalid_helo_hostname reject_non_fqdn_helo_hostname permit_mynetworks check_client_access hash:/etc/postfix/client_access permit
smtpd_recipient_restrictions = permit_mynetworks check_client_access hash:/etc/postfix/rbl_override reject_rbl_client zen.spamhaus.org permit
smtpd_relay_restrictions = reject_non_fqdn_recipient reject_unknown_recipient_domain regexp:/etc/postfix/regexp_access permit_mynetworks reject_unauth_destination reject_unlisted_recipient check_policy_service inet:127.0.0.1:10023 permit
smtpd_sender_restrictions = reject_non_fqdn_sender reject_unknown_sender_domain check_client_access cidr:/etc/postfix/outgoing_senders check_sender_access hash:/etc/postfix/sender_access permit
strict_rfc821_envelopes = yes
virtual_alias_domains = example.com
virtual_alias_maps = hash:/etc/postfix/virtual.d/example.com

答案1

我可以确认您在后缀中看到的内容是预期的行为reject_unlisted_sender本文档页面明确说明 Postfix 拒绝电子邮件的 4 种情况

  • 发件人域名匹配$我的目的地$inet_interfaces或者$proxy_interfaces,但发件人未列在$local_recipient_maps, 和$local_recipient_maps不为空。
  • 发件人域名匹配$虚拟别名域名但发件人未列在$虚拟别名映射
  • 发件人域名匹配$虚拟邮箱域名但发件人未列在$虚拟邮箱映射, 和$虚拟邮箱映射不为空。
  • 发件人域名匹配$relay_domains但发件人未列在$relay_recipient_maps, 和$relay_recipient_maps不为空。

当发件人地址不符合上述任何条件时,默认情况下后缀将允许它。


回到你最初的问题:到目前为止我知道的唯一方法如下。但还有什么更简单的吗?

,您唯一的选择可能是 SMTPD 限制类。对于其他解决方案,您可以使用任何策略服务器插件例如 postfwd、policyd 和其他。

答案2

是的我认为。

创建一个新的Postscript正则表达式访问文件/etc/postfix/allowed_senders,例如:

/@microsoft.com$/ OK
/@bbc.co.uk$/ OK
/@abc.net.au$/ OK
// REJECT

测试一下

postmap -q [email protected] regexp:/etc/postfix/allowed_senders

通过添加行来使用它

smtpd_sender_restrictions = check_sender_access regexp:/etc/postfix/allowed_senders

/etc/postfix/main.cf

应该可以。这对我来说是可行的,并且是在提出此问题之前的旧 Postfix 版本上,因此我认为我不会依赖此后添加的任何内容。

相关内容