Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for creating a Postfix whitelist for known Social Networks servers (Facebook, Twitter, etc.)
#! /bin/sh
#
# Copyright (c) 2015 Steve Jenkins <steve@stevejenkins.com>
#
# Based on gwhitelist script by Mike Miller <mmiller@mgm51.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# a quick script to output the current list ofoutbound smtp servers for
# known social networks (Facebook, Twitter, etc.) in a format suitable
# for Postfix's postscreen whitelist CIDR file.
#
# the list of CIDR-formatted addresses, each followed by the word "permit"
# is printed to stdout. It is up to you to mesh this output into your
# postscreen processing on a recurring basis.
#
# version history
# 20151125 1.0 sj - initial release
#
# abort on any error
set -e
# Identify nameservers
fbnameServer1="a.ns.facebook.com"
twitnameServer2="ns1.p34.dynect.net"
# look up SPF records
fbspfString=`dig @${fbnameServer} _spf.facebook.com txt | grep spf1 | cut -f5`
twitspfString=`dig @${twitnameServer} twitter.com txt | grep spf1 | cut -f6`
# get the IP addresses directly from the SPF records
printf "%s\n" ${fbspfString} | grep "^ip" | cut -c5- | \
sed s/$/" permit"/
printf "%s\n" ${twitspfString} | grep "^ip" | cut -c5- | \
sed s/$/" permit"/
# Uncomment if you want this script to reload Postfix config
#/usr/sbin/postfix reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment