Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script submitted by Almir Duarte Jr. for automating OpenDKIM key generation and configuration for multiple domains.
#!/bin/bash
# USAGE
# Install opendkim
# List all your domains in file /etc/opendkim/TrustedHosts, one per line
CHOWN="$(which chown)"
MKDIR="$(which mkdir)"
REMOVE="$(which rm)"
ECHO="$(which echo)"
COPY="$(which cp)"
MOVE="$(which mv)"
CAT="$(which cat)"
OPENDKIM="$(which opendkim-genkey)"
BASE_PATH="/etc/opendkim"
FILE="$BASE_PATH/TrustedHosts"
while read -r LINE;
do
if [[ ! $LINE = \#* ]]; then
$ECHO "Creating private and public keys for domain $LINE"
if [ ! -d $BASE_PATH/keys/$LINE ]; then
$MKDIR $BASE_PATH/keys/$LINE
fi
$OPENDKIM -D $BASE_PATH/keys/$LINE/ -d $LINE -s default
$CHOWN -R opendkim:opendkim $BASE_PATH/keys/$LINE
$MOVE $BASE_PATH/keys/$LINE/default.private $BASE_PATH/keys/$LINE/default
NEW_KEY="default._dkim.$LINE $LINE:default:$BASE_PATH/keys/$LINE/default"
$ECHO "$NEW_KEY" >> $BASE_PATH/KeyTable
NEW_SIGN="*@$LINE default._dkim.$LINE"
$ECHO "$NEW_SIGN" >> $BASE_PATH/SigningTable
DNS_FILE="/var/named/$LINE.hosts"
if [ -f $DNS_FILE ]; then
DNS_PUBLIC_KEY="$($CAT $BASE_PATH/keys/$LINE/default.txt)"
$ECHO $DNS_PUBLIC_KEY >> $DNS_FILE
fi
fi
done <$FILE
service named restart
service opendkim restart
postfix reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.