- Notifications
You must be signed in to change notification settings - Fork0
Script for generating an allowlist for Postfix's Postscreen based on large senders' SPF records
License
lquidfire/postallow
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A script for generating a Postscreen allowlist (and optionally a blocklist) based on large and presumably trustworthy senders' SPF records.
Postallow uses the published SPF records from domains of known webmailers, social networks, ecommerce providers, and compliant bulk senders to generate a list of outbound mailer IP addresses and CIDR ranges to create a allowlist (and optionally a blocklist) for Postfix's Postscreen.
This allows Postscreen to save time and resources by immediately handing off allowlisted connections from these hosts (which we can somewhat safely presume are properly configured) to Postfix's smtpd process for further action. Blocklisted hosts are rejected before they reach Postfix's smtpd process.
Note this doesnot allowlist (or blocklist) email messages from any of these hosts. An allowlist for Postscreen (which is merely the first line of Postfix's defense) merely allows listed hosts to connect to Postfix without further tests to prove they are properly configured and/or legitimate senders. A Postscreen blocklist does nothing but reject the connection based on the blocklisted host's IP.
If all of the allowlist mailers are selected when Postallow runs, the resulting allowlist includes over 500 outbound mail servers, all of which have a very high probability of being properly configured.
By default, Postallow has blocklisting turned off. Most users will not need to ever turn it on, but it's there if youreally believe you need it. If you choose to enable it, make sure you understand the implications of blocklisting IP addresses based on their hostnames and associated mailers, and re-run Postallow often via cron to make sure you're not inadvertently blocking legitimate senders.
Postallow runs as a shell script (/bin/sh
) and relies on a script from theSPF-Tools project (despf.sh) to help recursively query SPF records. I recommend cloning or copying the entire SPF-Tools repo to a/usr/local/scripts/
directory on your system, then confirming thespftoolspath
value inpostallow
.
In order to runpostallow
you will need:
- A shell
- Perl
- Postfix
- spf-tools
- route-summarization
Please update SPF-Tools whenever you update Postallow, as both are under continuous development, and sometimes new features of Postallow depend upon an updated version of SPF-Tools.
Postallow also assumes that you havePostfix and the appropriatebind-utils package for your Linux / Unix(-y) system installed on your system.
- Make sure you haveSPF-Tools on your system
- Move the
postallow.conf
file to your/etc/
directory - Add any custom hosts in
postallow.conf
- Run
/usr/local/scripts/postallow
from the command line.
You can optionally provide a configuration file via the command line which will override the default configuration file:
/usr/local/scripts/postallow /path/to/config-file
I recommend cloning both the SPF-Tools and the Postallow repos into your/usr/local/scripts/
directory. Once you're satisfied with its performance, set a daily cron job to pick up any new hosts in the mailers' SPF records like this:
@daily /usr/local/scipts/postallow/postallow > /dev/null 2>&1 #Update Postscreen Allowlists
It is still possible to update the list of known Yahoo! IP outbound mailers from their website weekly:
@weekly /usr/local/scripts/postallow/scrape_yahoo > /dev/null 2>&1 #Update Yahoo! IPs for Postscreen Allowlists
(Please read more about Yahoo! hosts below)
When executed, Postallow will generate a file namedpostscreen_spf_allowlist.cidr
, write it to your Postfix directory, then reload Postfix to pick up any changes.
Add the filename of your allowlist (and optionally your blocklist) to thepostscreen_access_list
option in your Postfixmain.cf
file, like this:
postscreen_access_list = permit_mynetworks,... cidr:/etc/postfix/postscreen_spf_allowlist.cidr, cidr:/etc/postfix/postscreen_spf_blocklist.cidr,...
IMPORTANT: If you choose to enable blocklisting, list the blocklist fileafter the allowlist file inmain.cf
, as shown above. If you misconfigure Postallow and an IP address inadvertently finds its way onto both lists, the first entry "wins." Listing the allowlist file first inmain.cf
will assure that allowlisted hosts aren't blocklisted, even if they appear in the blocklist file.
Then do a manualpostfix reload
or re-run/usr/local/scripts/postallow
to build a fresh allowlist and automatically reload Postfix.
- Make sure you haveSPF-Tools on your system
- Create a system group:
groupadd -r postallow
- Create a system user:
useradd -g postallow -M -r -s /usr/sbin/nologin postallow
- Create a directory for postallow in /etc:
mkdir /etc/postallow && chgrp postallow /etc/postallow && chmod g+w /etc/postallow
- Move the
postallow.conf
file to your/etc/postallow
directory - Change the group of the
yahoo_static_hosts
file topostallow
(or move it into the the /etc/postallow directory). - Use
visudo
to add the following line to your sudo config:postallow ALL=(ALL) NOPASSWD: /usr/sbin/postfix reload
- Add any custom hosts in
postallow.conf
- Run
/usr/local/scripts/postallow
from the command line. - Copy the generated file(s)
postscreen_spf_allowlist.cidr
(andpostscreen_spf_blocklist.cidr
) to/etc/postfix/
(or create a symlink).
You can optionally provide a configuration file via the command line which will override the default configuration file:
/usr/local/scripts/postallow /path/to/config-file
I recommend cloning both the SPF-Tools and the Postallow repos into your/usr/local/scripts/
directory. Once you're satisfied with its performance, set a daily cron job to pick up any new hosts in the mailers' SPF records like this:
@daily postallow /usr/local/scipts/postallow/postallow > /dev/null 2>&1 #Update Postscreen Allowlists
It is still possible to update the list of known Yahoo! IP outbound mailers from their website weekly:
@weekly postallow /usr/local/scripts/postallow/scrape_yahoo > /dev/null 2>&1 #Update Yahoo! IPs for Postscreen Allowlists
(Please read more about Yahoo! hosts below)
When executed, Postallow will generate a file namedpostscreen_spf_allowlist.cidr
, write it to your Postfix directory, then reload Postfix to pick up any changes.
Add the filename of your allowlist (and optionally your blocklist) to thepostscreen_access_list
option in your Postfixmain.cf
file, like this:
postscreen_access_list = permit_mynetworks,... cidr:/etc/postfix/postscreen_spf_allowlist.cidr, cidr:/etc/postfix/postscreen_spf_blocklist.cidr,...
IMPORTANT: If you choose to enable blocklisting, list the blocklist fileafter the allowlist file inmain.cf
, as shown above. If you misconfigure Postallow and an IP address inadvertently finds its way onto both lists, the first entry "wins." Listing the allowlist file first inmain.cf
will assure that allowlisted hosts aren't blocklisted, even if they appear in the blocklist file.
Then do a manualpostfix reload
or re-run/usr/local/scripts/postallow
to build a fresh allowlist and automatically reload Postfix.
Options for Postallow are located in thepostallow.conf
file. This file shoud be moved to your system's/etc/
directory before running Postallow for the first time.
By default, Postallow includes a number of well-known (and presumably trustworthy) mailers in six categories:
- Webmailers
- Yahoo Mail
- Ecommerce
- Social Networks
- Bulk Senders
- Miscellaneous
To add your own additional custom hosts, add them to thecustom_hosts
section of/etc/postallow.conf
separated by a single space:
custom_hosts="aol.com google.com microsoft.com"
Additional trusted mailers are added to the script from time to time, so check back periodically for new versions, or "Watch" this repo to receive update notifications.
Because Postallow relies on published SPF records to build its allowlist, mailers who refuse to publish outbound mailer IP addresses via SPF are problematic.
For smaller mailhosts without SPF-published mailer lists, the includedquery_host_ovh
file is a working example of a script that queries a range of hostnames for a specific mailer (mail-out.ovh.net
in the included example), collects valid IP addresses, and includes them in a custom allowlist. The new custom allowlist may then be included in as an additional entry in your Postfix'spostscreen_access_list
parameter (seeUsage above). An example of thequery_host_ovh
file's output is included in the/examples/
folder aspostscreen_ovh_allowlist.cidr
.
To create additional customised query scripts for mailers that don't publish outbound IPs via SPF, copy the examplequery_host_ovh
file to a new unique filename, edit the script's mailhost and numerical range values as required, set a unique output file (/etc/postfix/postscreen_*_allowlist.cidr
), include the output file in Postfix'spostscreen_access_list
parameter, then configure cron to run the new query script periodically.
Depending on the size of the range you wish to query, this script could take a long time to complete. I recommend testing on a small fraction of the mailhost's range before pushing the script to a production environment.
The netblocks for Yahoo! are only to be found on their own nameservers, and manual checking of the querying mechanism is required every now and then.
Yahoo also publishes a list of outbound IP addresseson their website. However, that list does not correspond 100% to the IP addresses obtained from their SPF records via their own nameservers.. Therefore, Postallow offers both a dynamic list of Yahoo mailers, built from the records obtained from their Nameservers, as well as the option to scrape Yahoo's website and add those IP addresses as well.
A list of Yahoo! outbound IP addresses, based on the linked knowledgebase article and formatted for Postallow, is included asyahoo_static_hosts.txt
. By default, the contents of this file are added to the final allowlist. To disable these particular Yahoo! IPs from being included in your allowlist, set theinclude_yahoo
configuration option in/etc/postallow.conf
toinclude_yahoo="no"
.
Theyahoo_static_hosts.txt
file can be periodically updated by running thescrape_yahoo
script, which requires eitherWget orcURL (included on most systems). Thescrape_yahoo
script reads the Postallow config file for the location to write the updated list of Yahoo! oubound IP addresses. Run thescrape_yahoo
script periodically via cron (I recommend no more than weekly) to automatically update the list of Yahoo! IPs used by Postallow.
To enable blocklisting, setenable_blocklist=yes
and then list blocklisted hosts inblocklist_hosts
. Please refer to the blocklisting warning above. Blocklisting is not the primary purpose of Postallow, and most users will never need to turn it on.
You can also choose how to handle malformed or invalid CIDR ranges that appear in the mailers' SPF records (which happens more often than it should). The options are:
- remove - the default action, it removes the invalid CIDR range so it doesn't appear in the allowlist.
- keep - this keeps the invalid CIDR range in the allowlist. Postfix will log a warning about
non-null host address bits
, suggest the closest valid range with a matching prefix length, and harmlessly ignore the rule. Useful only if you want to see which mailers are less than careful about their SPF records. - fix - this option will change the invalid CIDR to the closest valid range (the same one suggested by Postfix, in fact) and include the corrected CIDR range in the allowlist.
Other options inpostallow.conf
include changing the filenames for your allowlist & blocklist, Postfix path, SPF-Tools path, and whether or not to automatically reload Postfix after you've generated a new list.
By the original author:
- Special thanks to Mike Miller for his 2013gallowlist script that initially got me tinkering with SPF-based Postscreen allowlists. The temp file creation and
printf
statement near the end of the Postallow script are remnants of his original script. - Thanks to Jan Sarenik (author ofSPF-Tools).
- Thanks toJose Borges Ferreira for patches and contributions to Postallow, include internal code to validate CIDRs.
- Thanks toRicardo Iván Vieitez Parra for contributions to Postallow, including external config file support, normalization improvements, error handling, and additional modifications that allow Postallow to run on additional systems.
- Thanks to partner (business... not life)Steve Cook for helping me cludge through Bash scripting, and for writing the initial version of the
scrape_yahoo
script. - Thanks to all the generouscontributors right here on GitHub who have helped move the project along!
A blog post by the original author discussing how Postallow came to be is here:
If you're a Postfix admin who sees a good number ofPASS OLD
entries for Postscreen in your mail logs, and have a suggestion for an additional mail host that might be a good candidate to include in Postallow, please open an issue for the host(s) to be added topostallow
.
You are totally responsible for anything this script does to your system. You're on your own. :)
About
Script for generating an allowlist for Postfix's Postscreen based on large senders' SPF records
Resources
License
Stars
Watchers
Forks
Packages0
Languages
- Shell100.0%