Remote IP Mail Filtering

I recently wanted to write a filter rule based upon the remote IP of the mail server sending the email. A quick search of the internet found that there was no simple way of doing this using my qmail based setup. I did find a mailing list that suggested finding a way of adding an email header contaiing the information I wanted. I decided to do just that.

My qmail installation is built with SPP support. This allows me create plugins that can be activated when the SMTP server receives an email. The following steps will install an plugin designed to add an email header containing the IP of the remote SMTP server.

Create a new file called qmail-spp-remoteip.c and cut and paste the following code into it.

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4.  
  5. int main()
  6. {
  7.     char *rip;
  8.  
  9.     if ((rip = getenv("TCPREMOTEIP"))) {
  10.         printf("HX-TCPREMOTEIP: %s\n", rip);
  11.     }
  12.  
  13.     return 0;
  14. }

Now compile and install the plugin into qmail’s plugin directory.

gcc -g -Wall qmail-spp-remoteip.c -o /var/qmail/plugins/qmail-spp-remoteip

You will probably need to be logged in as root to install the plugin. Replace /var/qmail/plugins with the location of your qmail’s plugin directory if it differs from mine (I use the Gentoo linux distribution).

Now configure qmail to call the plugin when it receives an SMTP connection. Modify the /var/qmail/control/smtpplugins file adding the plugin to the connection section.

[connection]
plugins/qmail-spp-remoteip

If all went well, received emails should now have an extra header called X-TCPREMOTEIP which you can use for filtering your email.

Related posts:

  1. qmail and vpopmail

Leave a Reply

You must be logged in to post a comment.