Greylisting is a method of defending e-mail users against spam. A mail transfer agent (MTA) using greylisting will "temporarily reject" any email from a sender it does not recognize. If the mail is legitimate, the originating server will try again after a delay, and if sufficient time has elapsed, the email will be accepted.
While greylisting is not effective as in the past, it still cut a certain fraction of the total spam.
Changelog
- Apr 18, 2023
Bai Borko showed a different approach in purging the database ofjgreylist
and posted his script here. Basically, instead of deleting the old records his script leaves in the database the records with trusted IPs of the last 30 days based on the qmail-smtpd logs. Have a look!
qmail-spp
greylisting plugin
I introduce here how greylisting
can be implemented on qmail
by means of another qmail-spp plugin, which saves the data in MySQL
. Having the data in MySQL
is useful to measure how much spam is blocked by greylisting
.
- More info here
- Author: Manuel Mausz
Download
cd /usr/local/src wget https://manuel.mausz.at/coding/qmail-spp/greylisting/greylisting-0.5.tgz tar xzf greylisting-0.5.tgz cd greylisting-0.5
Compile and install
gcc -std=c99 -o /var/qmail/plugins/greylisting greylisting.c -I/usr/include -I/usr/include/mysql -I/usr/local/include/mysql -L/usr/lib/mysql -L/usr/lib64/mysql -L/usr/local/lib/mysql -L/usr/local/lib64/mysql -lmysqlclient strip greylisting
Install the configuration files
cp mysql.cnf /var/qmail/control chown vpopmail:vchkpw /var/qmail/control/mysql.cnf chmod 600 /var/qmail/control/mysql.cnf cp greylisting.config /var/qmail/control/greylisting chown root:root /var/qmail/control/greylisting chmod 644 /var/qmail/control/greylisting
Install the mysql database and create the mysql user:
> mysql -u root -p CREATE USER 'greylisting'@'localhost' IDENTIFIED BY '***'; GRANT USAGE ON *.* TO 'greylisting'@'localhost' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; CREATE DATABASE IF NOT EXISTS greylisting; GRANT ALL PRIVILEGES ON greylisting.* TO 'greylisting'@'localhost';
Now install the database schema from the greylisting.sql file (note that in the same file you have examples for whitelist and blacklist queries):
mysql < greylisting.sql -D greylisting -u greylisting -p
Copy your mysql access parameters into your mysql.cnf file. For example:
cat > /var/qmail/control/mysql.cnf << __EOF__ [client] #socket=/run/mysqld/mysqld.sock #host=10.1.2.3 user=greylisting password=secret database=greylisting __EOF__
If the connection has to be done against a local mysql
host, it will be via the mysql socket. If, as in my case, you have mysql
in a different host, uncomment the "host" line.
Now define the "greylist" in the smtpplugin file:
# smtpplugins sample file # other lines here [rcpt] plugins/ifauthskip plugins/greylisting
To enable greylisting
you have to export this variable in your tcpserver environment. So put this in your qmail-smtpd run file
export GREYLISTING=""
or enable a tcprule in your tcp.smtp
, for example:
:allow,GREYLISTING=""
Be aware that greylisting
is never enabled for RELAYCLIENTS
.
Finally, adjust the greylisting parameters in the /var/qmail/control/greylisting file
cat > /var/qmail/control/greylisting << __EOF__ mysql_default_file=control/mysql.cnf block_expire=2 record_expire=2000 record_expire_good=36 loglevel=4 __EOF__
block_expire=2
means that the IP will be blocked for 2 minutes, while record_expire=2000
means that after 2000 minutes it will be greylisted again.
Now install the program which will purge the database in your cronjob
cp greylisting_cleanup.sh /usr/local/sbin/greylisting_cleanup.sh chmod +x /usr/local/sbin/greylisting_cleanup.sh
Here is a crontab example
# greylisting 0 2 * * * /usr/local/sbin/greylisting_cleanup.sh >> /var/log/cron
If you want to use greylisting also in your submission port, it would be a good idea to switch off it in case the client have passed the authentication. In order to do this install the "skipifauth" plugin of the same author
wget https://notes.sagredo.eu/files/qmail/patches/qmail-spp/plugins/ifauthskip.c cc -o /var/qmail/plugins/ifauthskip ifauthskip.c
and install it before greylisting in control/smtpplugin
[rcpt] plugins/ifauthskip plugins/greylisting
jgreylist
This is a greylist program by John Simpson. It is very good and honestly I don't know if I have to suggest this one or the previous one. It stores the data on files (have a look to the page of the author for details). He ships both a Perl and a C version of the program.
Here is how to install and configure the C program:
cd /usr/local/src wget https://notes.sagredo.eu/files/qmail/patches/greylisting/jms/jgreylist.c wget https://notes.sagredo.eu/files/qmail/patches/greylisting/jms/jgreylist-clean
Compile and install
cc -o /var/qmail/bin/jgreylist jgreylist.c chmod 0750 /var/qmail/bin/jgreylist cp jgreylist-clean /usr/local/sbin/jgreylist-clean chmod +x /usr/local/sbin/jgreylist-clean chown root:root /usr/local/sbin/jgreylist-clean
Define your jgreylist
directory (where the data will be saved) and let qmail-smtpd
to write into it
mkdir -m 0700 /var/qmail/jgreylist
chown vpopmail:vchkpw /var/qmail/jgreylist
add these variables to your qmail-smtpd
run file
export JGREYLIST_DIR="/var/qmail/jgreylist" export JGREYLIST_LOG_SMTP=1
finally execute jgrelist
before qmail-smtpd
, for example
exec /usr/local/bin/softlimit -m "$SOFTLIMIT" \ /usr/local/bin/tcpserver -4 -v -R -l "$LOCAL" \ -x /home/vpopmail/etc/tcp.smtp.cdb -c "$MAXSMTPD" \ -u "$QMAILDUID" -g "$NOFILESGID" 0 25 \ /var/qmail/bin/jgreylist \ /var/qmail/bin/qmail-smtpd 2>&1
Install the "clean" program in your cronjob
# jgreylist 0 2 * * * /usr/local/sbin/jgreylist-clean >> /var/log/cron
Now restart qmail
.
Bai Borko has a different approach in purging the database and posted his script here. Basically, instead of deleting the old records his script leaves in the database the records with trusted IPs of the last 30 days based on the qmail-smtpd
logs. Have a look!
Comments
correction
Ivelin Topalov March 5, 2024 23:24 CET
Reply | Permalink
correction
Roberto Puzzanghera Ivelin Topalov March 6, 2024 07:51 CET
thank you. Corrected
Reply | Permalink
small mistake
Mike January 11, 2024 07:22 CET
Hi Roberto,
pls correct name of the file to jgreylist-clean
Thx.
Reply | Permalink
small mistake
Roberto Puzzanghera Mike January 11, 2024 10:31 CET
Thanks, corrected
Reply | Permalink
jgreylist clean DB
Bai Borko April 18, 2023 07:08 CET
Hi Roberto,
i am using jgreylist for a long time. I was decided to change the approach for clean DB for old records.
Instead to clean DB for not used for a long time records , i create completely new DB.
To create the new DB i am using the backup files created in last 30 days in /var/log/qmail/backup/qmail-smtpd.* and /var/log/qmail/smtpd/current.
I parse these files for "result=accepted code=250" and create completely new DB only with IP's of mail servers which my mail server successful received mail in last 30 days.
I am using this script for a few weeks without any issues. The result is very good.
The DB is twice less and there is only trusted IP's of mail servers, also you can see lot of records with spammers IP's like this "greylist[586630]: 113.93.57.136: GREY first time" in /var/log/qmail/smtpd/current.
If somebody decides to use this script must disable in crontab original script /usr/local/bin/jgreylist-clean and to create new job like this:
Feel free to test and share.
Reply | Permalink
jgreylist clean DB
Roberto Puzzanghera Bai Borko April 18, 2023 12:56 CET
Thanks a lot. Appreciated. I added a link to your post in the jgreylist section and at the top of this page.
Reply | Permalink
One more thing
Bai Borko February 25, 2022 11:31 CET
Hi Roberto,
Please update your guide for jgreylist.
This row "/var/qmail/bin/jgreylist \" needs to be added after tcpserver and before qmail-smtpd in /var/qmail/supervise/qmail-smtpd/run script.
Like example below:
Reply | Permalink
One more thing
Roberto Puzzanghera Bai Borko February 25, 2022 14:40 CET
Thank you. Corrected
Reply | Permalink
Greylisting recommendations
Paulo Fragoso January 9, 2022 15:28 CET
Hello,
Congratulations for greate job!
I missed the note on the gray list. What is your recommendation?
Paulo.
Reply | Permalink
Greylisting recommendations
Roberto Puzzanghera Paulo Fragoso January 16, 2022 19:42 CET
I installed the patch by Jan Mojzis and didn't liked it because it seems to me that the postgrey server needs posftix installed, as it looks for its socket.
Then I played with jms' jgreylist program and it works great here, despite of its age. I'm testing it right now. It's easy to install, with no need to patch, and it's well documented.
Reply | Permalink
Greylisting recommendations
Bai Borko Roberto Puzzanghera February 9, 2022 16:38 CET
Hi Roberto,
jgreylist program works great. But in some cases it's not useful agains the spammers.
Оnce spammers IP is already in the jgrey list DB jgreylist is not helping.
Like this example in following log lines:
OR
I made small script to clean jgreylist DB from such spammers IPs. The script works with both example above.
You can run the script depends of your needs . I run it hourly :
Reply | Permalink
Greylisting recommendations
Roberto Puzzanghera Bai Borko February 9, 2022 16:50 CET
Thanks, appreciated. I'll test it in the following days.
I'm not sure that 100% of the example 2 is spam. Look at the latest patch which is more verbose there.
As greylist program, I also tested this plugin, which is great http://qmail-spp.sourceforge.net/plugins/details/?id=30
It saves data to mysql. This can give you a measure of the mail percentage which is definitely blocked as spam and compare it with the spam perc blocked in other ways
Reply | Permalink
Greylisting recommendations
Roberto Puzzanghera Paulo Fragoso January 9, 2022 18:04 CET
I'm not familiar at all with greylisting for qmail. I only played with Jan Mojzis' qmail-postgrey but it was ages ago and I don't remember much. But I know that Erwin Hoffmann grabbed this code to make his own greylisting in s/qmail, so if I wanted to investigate greylisting for qmail, I would look at what e.h. has done there and the differences with Jan Mojzi's patch.
If you find out that this is doable or you find another nice solution please let me know...
Reply | Permalink