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.plwould 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.cfand 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, ... permitExample:
NOTE: It may be a better idea to place this under
smtpd_helo_restrictionssince 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_restrictionsfor my own reasons. Either area should work. I haven't tested it under helo restrictions, though. -
Next, edit the
/etc/postfix/master.cffile 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
aptexample:sudo apt update -y && sudo apt install libgeo-ip-perlOR: 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.



ls -al of /etc/postfix/scripts: -rwxr-x--- 1 root root 2998 Feb 13 18:46 postfix-geoip.pl
Postfix runs as root/root.
master.cf:
smtp inet n - n - - smtpd -o smtpd_sasl_auth_enable=yes -o smtpd_tls_security_level=may -v
pickup unix n - n 60 1 pickup
cleanup unix n - n - 0 cleanup
qmgr unix n - n 300 1 qmgr
tlsmgr unix - - n 1000? 1 tlsmgr
rewrite unix - - n - - trivial-rewrite
bounce unix - - n - 0 bounce
defer unix - - n - 0 bounce
trace unix - - n - 0 bounce
verify unix - - n - 1 verify
flush unix n - n 1000? 0 flush
proxymap unix - - n - - proxymap
proxywrite unix - - n - 1 proxymap
smtp unix - - n - - smtp
relay unix - - n - - smtp
-o syslog_name=postfix/$service_name
showq unix n - n - - showq
error unix - - n - - error
retry unix - - n - - error
discard unix - - n - - discard
local unix - n n - - local
virtual unix - n n - - virtual
lmtp unix - - n - - lmtp
anvil unix - - n - 1 anvil
scache unix - - n - 1 scache
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_tls_security_level=may
-o smtp_tls_mandatory_protocols=TLSv1
spamassassin unix - n n - - pipe flags=R user=spamd argv=/usr/bin/spamc -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
smtps inet n - n - - smtpd -o smtpd_sasl_auth_enable=yes -o smtpd_tls_security_level=may -o smtpd_tls_wrappermode=yes
postlog unix-dgram n - n - 1 postlogd
127.0.0.1:2528 inet n n n - 0 spawn
user=nobody argv=/etc/postfix/scripts/postfix-geoip.pl