Installation:
The starting point for this script was from here:
http://web.archive.org/web/20151128083440/https://www.kutukupret.com/2011/05/29/postfix-geoip-based-rejections/
You need:
- Linux machine with-
- Perl
- Perl Geo::IP module
- and of course "Postfix" (MTA)
-
You will need to add the script above somewhere on your system.
/etc/postfix/scripts/postfix-geoip.pl
would probably be a good place. It doesn't really matter where it is placed, though. Keep in mind the permissions & owner will need to be correct no matter where you put it.Once placed, make sure it's owned by root and can be run by the "nobody" user. (It should be owned by root to avoid postfix warnings):
sudo chown root: /etc/postfix/scripts /etc/postfix/scripts/postfix-geoip.pl sudo chmod 755 /etc/postfix/scripts/postfix-geoip.pl
-
Once the script is owned correctly and executable on the Postfix system, you will need to edit the Postfix configuration.
Edit
sudo nano /etc/postfix/main.cf
and findsmtpd_client_restrictions =
and add a 'check_client_access' directive under it (just make sure it has a comma on end and is above the final 'permit') Leave any other directives you may see (the dots '...') in place.:smtpd_client_restrictions = ... check_client_access tcp:[127.0.0.1]:2528, ... permit
Example:
NOTE: It may be a better idea to place this under
smtpd_helo_restrictions
since this is the very first check. If it's a bad IP, it should go no further. Less system resources would be used to check and 'block' a connected IP under HELO hypothetically. I usedsmtpd_client_restrictions
for my own reasons. Either area should work. I haven't tested it under helo restrictions, though. -
Next, edit the
/etc/postfix/master.cf
file and put this bit at the very bottom of this file:127.0.0.1:2528 inet n n n - 0 spawn user=nobody argv=/etc/postfix/scripts/postfix-geoip.pl
-
Next install GeoIP system wide. Debian/Ubuntu
apt
example:sudo apt update -y && sudo apt install libgeo-ip-perl
OR: If using cpan to install the module:
sudo cpan install Geo::IP
Configuration is complete. Restart Postfix:
sudo systemctl restart postfix
Test / check mail.log / etc.
Hi @FoulFoot
Your problem isn't that big of problem where you would mess anything up. Just don't do anything not mentioned here..
As @ShamimIslam said, the permissions on the file are probably the issue and the script is never spawned on the 2528 port in master.cf.
Therefore there is nothing behind the port listening and you get a refused.
I don't think I read if you checked the port to see if it's open?
The first place to start troubleshooting if you get a connection refused:
sudo lsof -i | grep 2528
(or u can usesudo netstat -ltn | grep :2528
if you don't have lsof)You should see it bound to localhost or 127.0.0.1. Either one is fine. If you don't, the script was never spawned in master.cf on the port.
Assuming the script is actually located where you are saying it is, this could mean it can't read the script due to ownership, or permissions (as mentioned). That amounts to the same thing: It is never used. This "error" master.cf entry should be in your mail.log if you turn up verbosity (as mentioned).
To test if it works, do this (make it 'world executable'):
sudo chmod 755 /etc/postfix/scripts/postfix-geoip.pl
Restart Postfix.
Then try this again:
sudo lsof -i | grep 2528
If you see it bound to the port, try:
telnet 127.0.0.1 2528
(If it connects via telnet, use
CTRL-]
and then type 'quit' to exit.)It works if it connects.
If it is working, you can try to reduce the permissions.
Or, you can leave as-is: It's only a minor security concern if you are the only system user.
If script works, do this if you want to make it more secure - be careful - copy and paste the FULL line:
/scripts/
area should look like this when you do that (my filename is different BTW):(restart Postfix and re-test port)
If you still get a refused, the script was never spawn to the port via master.cf.
More than likely, you still have permission problem, or script can't be found.
If still getting a 'refused' after trying above, let me know.