Mail Rate Limiting

I would like to apply mail rate limiting per domain automatically, instead of manually having to add each domain to the Mail Rate Limiting.

Is it possible to do this by script or can you add that as an option?

Status: 
Closed (fixed)

Comments

Do you mean you want to apply a limit to all domains? This can be done already on the Mail Rate Limiting page, via the "Global message limit" field.

Hi Jamie,

I am aware of the global message limit. That's not what I want. I want the mail rate limiting applied per domain. I know it's possible to do it manually with the drop down menu, to choose the domain, and then defining the rate limit.

For example, I would like every virtual-server to be able to send a maximum 500 emails per hour. Could this be done by script?

Currently, there is no automated way to set a separate limit for each of 500 domains, sorry.

Where is the config file for the rate limiting stored if you were to define each domain manually?

The file is /etc/milter-greylist/greylist.conf

I'm trying to set the Per sender domain overrides, and I don't see any domains being created in /etc/milter-greylist/greylist.conf

Is that stored somewhere else?

Did you add at least one per-domain limit in the UI?

Thanks Jamie, may I ask you where the domain_* followed by the string of numbers is defined for the domain (from my greylist.conf example below)?

ratelimit "domain_140058829116865" rcpt 200 / 1h racl blacklist from /.@wptraveler.com/ ratelimit "domain_140058829116865" msg "Message quota exceeded" racl whitelist from /.@wptraveler.com/ ratelimit "domain_13988505709854" rcpt 200 / 1h racl blacklist from /.@tobacco.com/ ratelimit "domain_13988505709854" msg "Message quota exceeded" racl whitelist from /.@tobacco.com/ ratelimit "domain_13990426466505" rcpt 200 / 1h racl blacklist from /.@limofinder.net/ ratelimit "domain_13990426466505" msg "Message quota exceeded" racl whitelist from /.@limofinder.net/

Do you mean how to convert from a domain name to the ID number? This can be done with the Virtualmin API command virtualmin list-domains --domain example.com --id-only

thanks, I'll try my novice scripting skills to see if I can get this working.

Can you advise if something like this would work. I really apologize that I'm pretty much a padawan when it comes to scripting.

If I wanted to add the following to /etc/milter-greylisting/greylist.conf above "racl whitelist default" on changes to virtual-server:


ratelimit "domain_140179426116421" rcpt 500 / 1h
racl blacklist from /.*@example.com/ ratelimit "domain_140179426116421" msg "Message quota exceeded"
racl whitelist from /.*@example.com/

I came up with:


#!/bin/bash

## Add new domain to milter-greylist for rate limiting
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only) &&
sed -i '/racl whitelist default/i \ratelimit "domain_"$ID rcpt 500 / 1h\n
racl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit "domain_"$ID msg "Message quota exceeded"\n
racl whitelist from /.*@$VIRTUALSERVER_DOM/\n' /etc/milter-greylist/greylist.conf
fi

## Remove domain from milter-greylist on domain deletion
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only) &&
sed -i '/$ID/d' /etc/milter-greylist/greylist.conf &&
sed -i '/$VIRTUALSERVER_DOM/d' /etc/milter-greylist/greylist.conf
fi

Can you give me some advice on how to get this working?

OK... I posted on Stack Overflow and someone corrected it. I used ' instead of " so no bash expansion.

This works if anyone wants to use it:

#!/bin/bash

## Add new domain to milter-greylist for rate limiting
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only)
sed -i "/racl whitelist default/i \ratelimit \"domain_$ID\" rcpt 500 / 1h\nracl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit \"domain_$ID\" msg \"Message quota exceeded\"\nracl whitelist from /.*@$VIRTUALSERVER_DOM/\n" /etc/milter-greylist/greylist.conf
fi

## Remove domain from milter-greylist on domain deletion
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
sed -i "/ratelimit/d" /etc/milter-greylist/greylist.conf
sed -i "/$VIRTUALSERVER_DOM/d" /etc/milter-greylist/greylist.conf
fi

The above script has a bug in removing entries.

Corrected as follows.

## Add new domain to greylist for rate limiting
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only)
sed -i "/racl whitelist default/i \ratelimit \"domain_$ID\" rcpt 500 / 1h\nracl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit \"domain_$ID\" msg \"Message quota exceeded\"\nracl whitelist from /.*@$VIRTUALSERVER_DOM/\n" /etc/mail/greylist.conf
fi

## Remove domain from milter-greylist on domain deletion
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only)
sed -i "s|ratelimit \"domain_$ID\" rcpt 500 / 1h||" /etc/mail/greylist.conf
sed -i "s|racl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit \"domain_$ID\" msg \"Message quota exceeded\"||" /etc/mail/greylist.conf
sed -i "s|racl whitelist from /.*@$VIRTUALSERVER_DOM/||" /etc/mail/greylist.conf

fi