Home > Software engineering >  How To Rewrite Unqualified Address In Postfix So That The Local Domain Is Not Used Automatically
How To Rewrite Unqualified Address In Postfix So That The Local Domain Is Not Used Automatically

Time:06-06

I would like to configure Postfix to rewrite only unqualified recipient email addresses (that is addresses that would go into the To: section of an email), to use the commercial domain name (as in, .com) of our client and not the local intranet domain of our client's environment, which is Postfix's default behavior for rewriting invalid addresses.

Just to be on the same page, invalid addresses here to me are addresses that do not include an '@' sign so are not valid SMTP addresses

Please also note, as an unfortunate, confounding circumstance, the local intranet domain of this clients environment is actually a named .local domain which can cause an unknown amount of issues as ".local" is a special-use reserved top-level domain by the IETF so, while I don't think that is actually causing an issue here, it should be stated. And this has caused issues with us before with regards to multicast vs unicast-dns

Details:

Unqualified Recipient Example: sam0161

Qualified Recipient Example: [email protected]

Local Intranet Domain: ClientDomain.local

Public Commerical Domain: ClientDomain.com

Postfix Version: 3.4.13

Server OS: Ubuntu 20.04.4 LTS Focal

Expected Behavior:

The command

date | mail -s "Test Email" sam0161

Should email to

[email protected]

The command

date | mail -s "Test Email" [email protected]

Should email to

[email protected] (I.E it is left alone)

Actual Results:

date | mail -s "Test Email" sam0161

is sent to

sam0161.Hostname.local

date | mail -s "Test Email" [email protected]

is sent to [email protected]

(Note: This part is correct, we want to leave it as is, I'm including it here just for background and completeness)

I have tried the following In order to achieve this

in /etc/postfix/main.cf

  1. Configured

    mydomain = ClientDomain.com

    append_dot_mydomain = yes

    append_at_myorigin = no

    myorigin = $mydomain

    inet_interfaces = all

The Logic for these configuration options is described by the local_header_rewrite_clients in postfix.org

Rewrite message header addresses in mail from these clients and update incomplete addresses with the domain name in $myorigin or $mydomain; either don't rewrite message headers from other clients at all, or rewrite message headers and update incomplete addresses with the domain specified in the remote_header_rewrite_domain parameter.

  1. Configured

    remote_header_rewrite_domain = "clientDomain.com"

The Logic is to cover all mail traffic to ensure it is rewritten, even though in this environment and setup none of it should technically be considered "Remote" clients.

  1. Configured

    recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical

Logic is to "force" the recipient to be rewritten via Regular Expression. Please note, this is where things are a bit odd for me, as a manual test of the lookup table shows that the recipient should be rewritten, but when mail is sent, the recipient address is still shown to be sam0161.Hostname.local

Example:

/etc/postfix/recipient_canonical

!/.*@.*/ @clientDomain.com

postmap -q "sam0161" regexp:/etc/postfix/recipient_canonical

@clientDomain.com

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

(returns nothing, desired behavior)

date | mail -s "Test" sam0161

postfix ignores canonical mapping and sends to hostname.domain.local

postfix/local[65681]: A6E459K616: to=<[email protected]>, relay=local, delay=0.09, delays=0.07/0/0/0.01,

In short, it looks like I cannot circumvent the default behavior of appending the "myhostname" value, as returned by the gethostname() method call used by Postfix, to unqualified recipients. Even if I explicitly redefine the logic to use a specific domain different than the local one and or attempt to manually force the use of a different domain through canonical address rewriting .

Could anyone please advise if what I'm trying to do is possible? Or does Postfix simply not let you rewrite unqualified addresses to use a specific domain?

CodePudding user response:

This has been corrected - It turns out the mail client not postfix was appending the .hostname.local to the traffic before it hit postfix. Meaning the Regular Expression Logic would never evaluate to "true" as an '@' sign was always present.

The fix, therefore, is to do something like

/.*@hostname.local/ clientDomain.com

It goes without saying this a gross failure in troubleshooting on my part with assumptions being made that the problem started and ended with Postfix.

  • Related