Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a custom sender config file for spamdyke using a template
#!/usr/bin/env bash
#
# Quinn Comendant <quinn@strangecode.com>
# 06 May 2017 14:45:53
#
# Functions
#
function usage() {
echo "Usage: $(basename $0) ADDR
Create a custom sender config file for spamdyke using a template.
- if ADDR is a hostname add config to /var/qmail/spamdyke/_sender_/com/example/subdom
- if ADDR is an IP address add config to /var/qmail/spamdyke/_ip_/111/222/333/444
Then the config files will be opened in $EDITOR (e.g., subdom or 444 is the config file).
";
exit 1;
}
#
# Main
#
if [[ $(id -u) != 0 ]]; then
echo "You must be root to execute this script";
exit;
fi
# Display help if run without arguments.
[[ $# == 0 ]] && usage && exit 0;
[[ $# != 1 ]] && echo "Invalid arguments. Run with -h for help." && exit 1;
EDITOR=${EDITOR:-vim};
SPAMDYKEBASEDIR='/var/qmail/spamdyke';
SPAMDYKECONFTEMPLATE="reject-missing-sender-mx=0
reject-empty-rdns=0
reject-unresolvable-rdns=0
graylist-level=none
";
ADDR=$1;
if [[ $ADDR =~ [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
# ADDR is a IP address
IFS='.' read -r -a ADDRPARTS <<< "$ADDR";
CONFDIR=$(printf '%s/_ip_/%d/%d/%d' "$SPAMDYKEBASEDIR" "${ADDRPARTS[0]}" "${ADDRPARTS[1]}" "${ADDRPARTS[2]}");
mkdir -p "$CONFDIR";
CONFFILE=$(printf '%s/%d' "$CONFDIR" "${ADDRPARTS[3]}");
cat - > "$CONFFILE" <<<"$SPAMDYKECONFTEMPLATE";
test -f "$CONFFILE" && vim "$CONFFILE";
else
# Otherwise assume is a hostname.
ADDRPARTS=$(echo "$ADDR" | tr '.' $"\n" | tac | tr $"\n" '/');
CONFDIR=$(printf '%s/_sender_/%s' "$SPAMDYKEBASEDIR" "$(dirname "$ADDRPARTS")");
mkdir -p "$CONFDIR";
CONFFILE=$(printf '%s/%s' "$CONFDIR" "$(basename "$ADDRPARTS")");
cat - > "$CONFFILE" <<<"$SPAMDYKECONFTEMPLATE";
test -f "$CONFFILE" && vim "$CONFFILE";
fi
chown -R vpopmail:vchkpw $SPAMDYKEBASEDIR/{_ip_,_sender_};
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment