I needed to limit the number of emails sent by users through your Postfix server, and store message quota in RDMS systems (maybe you have multiple MX).
I tried policyd or other policy daemons for Postfix; Â but they all miss support for SQL storage.
With thisÂ piece of code you can setup a send rate per users or sender domain on daily/weekly/monthly basis and store data in MySQL (PostgreSQL,…).
To work with mqdaemon you only need a mysql table described like this one:
CREATE TABLE `emails` ( `email` VARCHAR(65) NOT NULL, `messagequota` INT(10) UNSIGNED NOT NULL DEFAULT '0', `messagetally` INT(10) UNSIGNED NOT NULL DEFAULT '0', `timestamp` INT(10) UNSIGNED DEFAULT NULL, )
and setup configuration parameters (db_table, db_wherecol, db_messagequota, etc) inside the Perl script.
You must insert all rows for the emails you have to enforce! The script is configured to use a simple UPDATE.
Modify the postfix data restriction class “smtpd_data_restrictions” like the following:
smtpd_data_restrictions = check_policy_service inet:$IP:$PORT
To print the cache content with update statistics for username into log file just send a SIGHUP to the process PID using “kill -HUP $pid” or invoke the script with printshm paramenter (perl ./daemon.pl printshm).
To enforce per-domain quota set the search key ($s_key_type parameter) to “domain”.
Start the daemon with “# ./daemon.pl” and kill it with “# pkill daemon.pl”.
Take care of using a port higher than 1024 to run the script as non-root.
This daemon caches the quota in memory, so you don’t need to worry about I/O operations.
5 Replies to “Postfix send rate limit per user/domain”
Adding it in main.cf would apply to incoming/outgoing?
I have a domain xyz.com and email@example.com, firstname.lastname@example.org. Now i want to limit user1 to send 30 emails per hour. Is it possible?
Of course u can! Just u have to do only few edits to the code, it’s pretty old and buggy. consider it a proof of concept. For my production envs i had to change many lines of code, starting from the threading model; this online version suffer of memory leaks.
For now this daemon limits only outgoing mails, if u need to limit incoming it’s an easy change to do.
Would you mind posting the version with no(fewer) memory leaks?
Sorry but i dont have plans to release a better tool since i dont have much time to work on this anymore
the following error is seen in the logs.
what could be the problem. Perl is installed in the system.
install_driver(postfix) failed: Can’t locate DBD/postfix.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at (eval 6) line 3.
Perhaps the DBD::postfix perl module hasn’t been fully installed,
or perhaps the capitalisation of ‘postfix’ isn’t right.
Available drivers: DBM, ExampleP, File, Gofer, Proxy, SQLite, Sponge.
at ./daemon.pl line 271
Perl exited with active threads:
3 running and unjoined
0 finished and unjoined
0 running and detached