Package: policyd-rate-limit
Postfix policyd server allowing to limit the number of mails accepted by
postfix over several time periods, by sasl usernames and/or ip addresses.InstallationFirst, create the user that will run the daemon:adduser --system --group --home /run/policyd-rate-limit --no-create-home policyd-rate-limitSince version 0.6.0, the configuration file is written using the yaml, so you need the following
package:pyyaml(sudoapt-getinstallpython3-yamlon debian like systems)Depending of the backend storage you planning to use, you may need to install additional packages.
(The default settings use the sqlite3 bakends and do not need extra packages).mysqldb(sudoapt-getinstallpython3-mysqldbon debian like systems) for the mysql backend.psycopg2(sudoapt-getinstallpython3-psycopg2on debian like systems) fot the postgresql backendInstall with pip:sudo pip3 install policyd-rate-limitor from source code:sudo make installThis will install thepolicyd_rate_limitmodule, thepolicyd-rate-limitbinary,
copy the default config to/etc/policyd-rate-limit.confif the file do not exists,
copy an init script to/etc/init.d/policyd-rate-limitand an unit file to/etc/systemd/system/policyd-rate-limit.service.After the installation, you may need to runsudo systemctldaemon-reloadfor make the unit
file visible by systemd.You should runpolicyd-rate-limit--cleanon a regular basis to delete old records from the
database. It could be wise to put it in a daily cron, for example:0 0 * * * policyd-rate-limit /usr/local/bin/policyd-rate-limit --clean >/dev/nullOptions of thepolicyd-rate-limitbinary-h,--help: show the help message and exit--clean: clean old records from the database--get-configPARAMETER_NAMEreturn the value of a config parameter--fileCONFIG_PATH,-fCONFIG_PATH: path to a config fileSettingsIf the option--fileis not specified,policyd-rate-limittry to read its configuration from
the following path and choose the first existing file:~/.config/policyd-rate-limit.conf~/.config/policyd-rate-limit.yaml/etc/policyd-rate-limit.conf/etc/policyd-rate-limit.yamlThe.confare the old configuration format. It was a python module and should not be used.
The.yamlare the new configuration format using the YAML syntax.debug: makepolicyd-rate-limitoutput logs to stderr.
The default isTrue.user: The userpolicyd-rate-limitwill use to drop privileges.
The default is"policyd-rate-limit".group: The grouppolicyd-rate-limitwill use to drop privileges.
The defaut is"policyd-rate-limit".pidfile: path where the program will try to write its pid to.
The default is"/var/run/policyd-rate-limit/policyd-rate-limit.pid".policyd-rate-limitwill try to create the parent directory and chown it if it do not exists.mysql_config: The config to connect to a mysql serverpgsql_config: The config to connect to a postgresql serversqlite_config: The config to connect to a sqlite3 database.backend: Which data backend to use. Possible values are0for sqlite3,1for mysql
and2for postgresql. The default is0, use the sqlite3 backend.SOCKET: The socket to bind to. Can be a path to an unix socket or a couple [ip, port].
The default is"/var/spool/postfix/ratelimit/policy".policyd-rate-limitwill try to create the parent directory and chown it if it do not exists.socket_permission: Permissions on the unix socket (if unix socket used).
The default is0o666.limits: A list of couple [number of emails, number of seconds]. If one of the element of the
list is exeeded (more than ‘number of emails’ on ‘number of seconds’ for an ip address or an sasl
username), postfix will return a temporary failure.limits_by_id: A dictionary of id -> limit list (see limits). Used to override limits and use
custom limits for a particular id. Use an empty list for no limits for a particular id.
Ids are sasl usernames or ip addresses. The default is{}.limit_by_sasl: Apply limits by sasl usernames. The default isTrue.limit_by_sender: Apply limits by sender addresses if sasl username is not found.
The defaut isFalse.limit_by_ip: Apply limits by ip addresses if sasl username and sender address are not found.
The default isFalse.limited_networks: A list of ip networks in cidr notation on which limits are applied. An empty
list is equal tolimit_by_ip = False, put"0.0.0.0/0"and::/0for every ip addresses.success_action: If not limits are reach, which action postfix should do. The default is"dunno". Seehttp://www.postfix.org/access.5.htmlfor possible actions.fail_action: If a limit is reach, which action postfix should do. The default is"defer_if_permit Rate limit reach, retry later".db_error_action: If we are unable to to contect the database backend, which action postfix
should do. The default is"dunno". Seehttp://www.postfix.org/access.5.htmlfor possible
actions.
Seehttp://www.postfix.org/access.5.htmlfor possible actions.config_file: This parameter is automatically set to the path of the configuration file
currently in use. You can call it conjunction with–get-configto known which configuration
file is used.report: ifTrue, send a report toreport_toabout users reaching limits each time
–clean is called. The default isFalse.report_from: From who to send emails reports. It must be defined whenreportisTrue.report_to: Address to send emails reports to. It must be defined whenreportisTrue.
It can be a single email address or a list of email adresses.report_subject: Subject of the report email. The default is"policyd-rate-limitreport".report_limits: List of number of seconds from the limits list for which you want to be reported.
The default is[86400].report_only_if_needed: Only send a report if some users have reach a reported limit.
The default isTrue.smtp_server: The smtp server to use to send emails["host", port].
The default is["localhost", 25].smtp_starttls: Should we use starttls to send mails ? (you should set this toTrueif
you usesmtp_credentials). The default isFalse.smtp_credentials: Should we use credentials to connect to smtp_server ?
if yes set["user", "password"], elsenull. The default isnull.count_mode: How sent mail are counted0: each RCPT TO are counted individualy. This is the how it was done historically. If set to 0,
the postfix check_policy_service must be set in smtpd_recipient_restrictions.
This is deprecated and should not be used anymore1: recipient are counted in the DATA stage. The postfix parameter check_policy_service must be
defined in smtpd_data_restrictions.
This is the new default.2: Number of submited mails a counted, not recipients. The postfix parameter
check_policy_service must be defined in smtpd_data_restrictions.
This means that if a use send a single mail with 100 recipient, it’s count for 1
in the quota instead of 100.Postfix settingsFor postfix 3.0 and later I recommend using the example below. It ensure that if policyd-rate-limit
become unavailable for any reason, postfix will ignore it and keep accepting mail as if the rule
was not here. I find it nice has in my opinion, policyd-rate-limit is a “non-critical” policy
service./etc/postfix/main.cf:smtpd_data_restrictions =
    ...,
    check_policy_service { unix:ratelimit/policy, default_action=DUNNO },
    ...On previous postfix versions, you must use:/etc/postfix/main.cf:smtpd_data_restrictions =
    ...,
    check_policy_service unix:ratelimit/policy,
    ...