QMail: Limit connections from an IP address or range
Submitted by Bram Schoenmakers on 15 December, 2008 - 14:16.
In order to limit the connections made from a certain IP address, one needs to patch tcpserver, the process which receives all incoming connections and passes them through to QMail. These steps describe how to patch tcpserver on a FreeBSD system, where the QMail SMTP and POP servers are executed with the supervise (svc) daemon.
- Download the patch here.
- Rename the patch to patch-tcpserver.c and put it in /usr/ports/sysutils/ucspi-tcp/files.
- I had to modify the patch in order to let FreeBSD process the patch without mocking about filenames and such:
-+++ ucspi-tcp-0.88/tcpserver.c 2006-01-26 18:51:03.000000000 +0100
+--- tcpserver.c 2000-03-18 16:18:42.000000000 +0100So what I basically did is remove the directory name from the file specifier.
- Make and install the port just as normal.
- Adapt your tcpserver rules file(s), read by tcpserver with the -x flag. With the patch installed, tcpserver understands a few more variables. We focus on MAXCONNIP and MAXCONNC. These allow you to have a certain amount of connections per IP or per IP range (12.34.56.xx) respectively. To allow only 30 connections per range write the following in your rules file:
:allow,MAXCONNC="30" - Restart the services:
svc -t /var/qmail/qmail-smtpd
svc -t /var/qmail/qmail-pop3
And now, these servers only allow a limited amount of connections. You can raise the amount of concurrent connections (-c) if you want to. Note that this concurrency limit is not superseeded by the tcpserver patch we just installed.