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.
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <unistd.h>
-
-
int main()
-
{
-
char *rip;
-
-
if ((rip = getenv("TCPREMOTEIP"))) {
-
}
-
-
return 0;
-
}
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:
Follow me on Twitter.
Grab my RSS Feed.