Postfix - Configuring Postfix to block outgoing mail to all but one domain ?

Zarshed Ali
Zarshed Ali
5000
| 2 1 2
Asked on 2/26/15, 12:34 PM
0
vote
3236 Views

Don't use IPTables

You could, of course, add two IPTables rules to the outgoing chain. The first of which would allow connections on Port 25 to the domain you wish to allow, the second blocking connection to any server on Port 25.

It'll block the mail from being sent, but will mean that every one of those messages sits in the mail queue for 60 days until it's disregarded. You could reconfigure the timeout, but given the ease of the steps below, what's the point?

 

Use Transport Mapping

Using this method, we can tell Postfix to either reject the mail, or disregard it. The latter is generally the preferred method as we want the sending application to believe the mail has been sent.

First we need to edit the postfix configuration file

nano /etc/postfix/main.cf

Now search for transport_maps (Ctrl-W, transport_maps -> Enter). You shouldn't find anything (if you do, skip the next step, but make note of which file is listed)

Add a line reading

transport_maps = hash:/etc/postfix/transport

Note: Depending on the version of Postfix you're running, you could use texthash which would avoid needing to run postmap on the file. It's up to the reader to work out which route they'd prefer, but if your postfix version is < 2.8 you can't use texthash.

Save and close (Ctrl-X, Y -> Enter)

Now we need to update the transport maps (if you didn't need to add a line, subsitute the relevant file path here)

nano /etc/postfix/transport

Now we need to add a line specifying which domain to allow sending to (I'm going to allow to bentasker.co.uk). To do so, insert the following lines

bentasker.co.uk : * discard:

This will simply discard messages to any email address not of the domain bentasker.co.uk. If you wanted to reject with an error you'd use (set the error text to suit your needs)

bentasker.co.uk: * error: Only allowing one domain

Note: Simply add additional domains on the line after bentasker.co.uk:, one line per domain.

Save and close (Ctrl-X, Y -> Enter)

Now we need to create a hash of the file (unless you used texthash in main.cf)

postmap /etc/postfix/transport

Finally, we need Postfix to reload it's rules

/etc/init.d/postfix reload

Job done! You should now find that emails sent to domains not specified are silently discarded, whilst those for whitelisted domains go through as normal. Be sure to check that the change has worked though, mailq and the maillog are your friends!

For non-whitelisted domains you should see the message being removed in the maillog but with no connection taking place before hand, and relay showing as none (there may be no sign of a connection, but if relay != none there's a very good chance the mail was sent). 

Zarshed Ali
Zarshed Ali
5000
| 2 1 2
Answered on 2/26/15, 12:36 PM
0
vote

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This forum is for HiTechnologia Employees & just Odoo general knowledge purpose only.

Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 2/26/15, 12:34 PM
Seen: 3236 times
Last updated: 2/26/15, 12:36 PM