Restricting outgoing mail to specific domains in From Address using Postfix

postfix

For reasons that make me want to hurt myself, an application I am required to support has the ability to send mail using a 'MAIL FROM' address of any domain. Because I am unable to lock down the application any further I want to have Postfix sanity check all mail that tries to leave my network destined for the rest of the world.

I only want mail that originates from within to be allowed out if it has a from address of 'example.com'. If mail that originates from within has a from address of 'someotherdomain.com' that mail should be blocked via Postfix.

To clarify, how do I configure Postfix to only allow mail, which originated within my local network, to be allowed out IF that mail has a FROM address of one of my domain names?

The only way I've figured out how to do this so far is as follows. But is there anything simpler?

/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 output for this configuration:

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

EDIT: Below is alternative configuration I tried using 'reject_unlisted_sender'.

When I try using this configuration, mail sent 'From: does_not_exist@example.com' bounces (as expected), but mail sent 'From: blah@not_my_domain.com' is allowed out without problem, which is exactly what I do not want.

# 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 output for this configuration:

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

Best Answer

I can confirm that what you see in your postfix was intended behavior of reject_unlisted_sender. This documentation page clearly states 4 conditions when postfix rejected your email

  • The sender domain matches $mydestination, $inet_interfaces or $proxy_interfaces, but the sender is not listed in $local_recipient_maps, and $local_recipient_maps is not null.
  • The sender domain matches $virtual_alias_domains but the sender is not listed in $virtual_alias_maps.
  • The sender domain matches $virtual_mailbox_domains but the sender is not listed in $virtual_mailbox_maps, and $virtual_mailbox_maps is not null.
  • The sender domain matches $relay_domains but the sender is not listed in $relay_recipient_maps, and $relay_recipient_maps is not null.

When the sender address doesn't match any condition above, by default postfix will permit it.


Back to your original question: The only way I've figured out how to do this so far is as follows. But is there anything simpler?

NO, your only options is likely SMTPD restiction classes. For other solution, you can use any policy server addon such as postfwd, policyd and others.