Score:0

Postfix : Only receive emails from specific tlds?

tr flag

I've been searching everywhere and can only find how to block specific tlds.

I've set up a rejected_domains file that blocks tlds which I have to manually add as they appear in my inbox.

Basically, allow only .com, .net, .org, .gov, .edu... type domains.

With so many tlds out there now throwing spam, I'd prefer to have a whitelist of accepted tlds.

I do have checks in place like spamhuas and the like, also spamassaassin, but they still get through.

ws flag
This has very little value in reducing spam. For most aplications of an email server, it does ensure that you will block legitimate emails. And you didn't say if the TLD related to the envelope/email or the connecting IP address.
Score:3
jp flag

This is possible by combining check_sender_access with Postfix PCRE Support.

Your default restriction would in this case be reject, but it might be better to add a human readable explanation on why the message was rejected.

In main.cf you would have something like the following. I usually place the smtpd_sender_restrictions under the smtpd_recipient_restrictions because that would enable logging both the sender and the recipient, which is better for debugging.

smtpd_recipient_restrictions =
    reject_unauth_destination,
    reject_invalid_hostname,
    reject_unauth_pipelining,
    reject_non_fqdn_sender,
    reject_unknown_sender_domain,
    reject_non_fqdn_recipient,
    reject_unknown_recipient_domain,

    reject_rbl_client sbl-xbl.spamhaus.org,

    check_sender_access pcre:/etc/postfix/access/sender_access,
    reject

And in the /etc/postfix/access/sender_access, according to pcre_table(5):

/\.net$/       OK
/\.org$/       OK
/\.gov$/       OK
/\.edu$/       OK
/\.([-\w]+)$/  550 The sender TLD .$1 is not whitelisted.

Here, I left the .com out because I wanted to demonstrate the results of this PCRE map:

$ /usr/sbin/postmap -q "[email protected]" pcre:/etc/postfix/access/sender_access
OK

$ /usr/sbin/postmap -q "[email protected]" pcre:/etc/postfix/access/sender_access
550 The sender TLD .com is not whitelisted.

Please notice that if you use OK in the check_sender_access, all other controls like the Spamhaus blocklist should come before it (as in the example above). Otherwise, mail from the whitelisted TLDs would come through even if, e.g., the IP address was blocked by the RBL. Using DUNNO instead of OK would allow other restrictions after it, but that would also require permit as the last rule (instead of reject).

TGates avatar
tr flag
This is exactly what I needed! Excellent explanation and examples. Thank you very much!
jp flag
I'm glad you found this useful. I have added a clarification regarding how this should be ordered with the Spamhaus blocklist & why.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.