spfgenerator.deb/usr/bin/spfgenerator

157 行
3.0 KiB
Bash
実行ファイル

#!/bin/sh
showHelp() {
cat <<EOF
NAME
spfgenerator generate SPF DNS entry
SYNOPSIS
spfgenerator [-h] [-s] [-mx] [-a [domain]] [-i domain] [-p fail|soft|neutral] [-d domain]
DESCRIPTION
spfgenerator generates SPF DNS entry
-h, --help
show this help.
-mx
Allow servers listed as MX to send email for the domain.
-ip <ip>
Allow IP to send email for the domain.
Can be used several times.
-i, --include <domain>
Any domains that may deliver or relay mail for the domain.
Can be used several times.
-a <domain>
Add any other server hostname that may deliver or relay mail for the domain.
If domain is empty, it allow current IP address of the domain to send email for the domain.
Can be used several times.
-p, --policy <fail|soft|neutral
How strict should be the servers treating the emails
neutral:
Mails will be probably accepted
Default value.
fail:
Not compliant will be rejected
soft:
Not compliant will be accepted but marked
-d, --domain <domain>
Default value: @
-s, --short
Only show the value
EOF
}
MX=0
IPv4s=
IPv6s=
INCLUDEs=
POLICY='?all'
DOMAIN=@
SHORT=0
As=
testData() {
if [ -z "$1" ]; then
(
echo "Invalid parameter ($2)\n"
showHelp
) >&2
exit 1
fi
}
addIp() {
if [ -n "$1" ]; then
if [ $(echo "$1" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") ]; then
IPv4s="$IPv4s ip4:$1"
else
IPv6s="$IPv6s ip6:$1"
fi
fi
}
addA() {
if [ -z "$1" ]; then
As="$As a"
else
As="$As a:$1"
fi
}
addInclude() {
if [ -n "$1" ]; then
INCLUDEs="$INCLUDEs include:$1"
fi
}
for i in $@; do
case $1 in
-h|--help)
showHelp
exit 0
;;
-mx)
MX=1
shift
;;
-s|--short)
SHORT=1
shift
;;
-a)
shift
addA $1
shift
;;
-ip)
shift
addIp $1
shift
;;
-i|--include)
shift
addInclude $1
shift
;;
-p|--policy)
shift
case $1 in
neutral) POLICY='?all';;
soft) POLICY='~all';;
fail) POLICY='-all';;
esac
shift
;;
-d|--domain)
shift
DOMAIN=$1
shift
;;
esac
done
testData "$DOMAIN" domain
testData "$IPv4s$IPv6s" ip
(
if [ $MX -eq 1 ]; then
MX=mx
fi
if [ $SHORT -eq 0 ]; then
echo "$DOMAIN IN TXT \"v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY\""
else
echo "v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY"
fi
) | sed 's/ */ /g'