QMail: Limit connections from an IP address or range
Tagged:  •    •    •    •    •  

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.

  1. Download the patch here.
  2. Rename the patch to patch-tcpserver.c and put it in /usr/ports/sysutils/ucspi-tcp/files.
  3. 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 +0100

    So what I basically did is remove the directory name from the file specifier.

  4. Make and install the port just as normal.
  5. 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"
  6. Restart the services:
    svc -t /var/qmail/qmail-smtpd
    svc -t /var/qmail/qmail-pop3
  7. 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.