| Server IP : 172.67.149.12 / Your IP : 104.23.243.226 Web Server : Apache System : Linux vm3310.eosdns.net 4.18.0-553.132.1.el8_10.x86_64 #1 SMP Thu Jun 11 14:27:43 UTC 2026 x86_64 User : carnalmedia ( 1000) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /mojo/ |
Upload File : |
#!/bin/bash
#
# Mojohost
# Script: dkim_keygen
#
# Description: Mojohost dkim generation script
#
#
# Requires: opendkim / opendkim-genkey
#
# Author: Joseph Laws
#
# Adapted from the "install_dkim" script by Joseph Laws (Centos6/7).
HOSTNAME=$(hostname)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
COLOROFF=$(tput sgr0)
ODKGK=$(which opendkim-genkey 2>/dev/null)
ODKDIR=/etc/opendkim
ODKKEYT=${ODKDIR}/KeyTable
ODKSIGN=${ODKDIR}/SigningTable
ODKTRUST=${ODKDIR}/TrustedHosts
#Determine the outbout public IPv4 address
BASEIP=$(ip route get 8.8.8.8 2>/dev/null | grep -oP 'src \K\S+')
if [[ -z "${BASEIP}" ]]; then
BASEIP=$(curl -4 -s --max-time 3 ifconfig.co 2>/dev/null)
if [[ ! "${BASEIP}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
BASEIP=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n1)
if [[ -z "${BASEIP}" ]]; then
echo "Can't determine the local outbound IP. Please check network settings"
exit 1
fi
fi
fi
usage() {
echo
echo "Usage: $0 -d DOMAIN [-s SELECTOR]"
echo
echo " -d DOMAIN The domain to create a DKIM key for (required)"
echo " -s SELECTOR Key selector name (default: 'default')"
echo
echo "Example: $0 -d example.com -s default"
echo
exit 0
}
check_inputs() {
if [[ -z "${DOMAIN}" ]]; then
echo "${RED}Error:${COLOROFF} You must provide a domain with -d DOMAIN"
exit 1
fi
if [[ -z "${SELECTOR}" ]]; then
echo "No selector provided — using 'default'."
SELECTOR=default
fi
if [[ -z "${ODKGK}" ]]; then
echo "${RED}Error:${COLOROFF} opendkim-genkey not found. Is OpenDKIM installed?"
exit 1
fi
}
create_key() {
if [[ -d ${ODKDIR}/keys/${DOMAIN} ]]; then
echo "OpenDKIM - ${GREEN}${DOMAIN} key folder already exists${COLOROFF}"
if [[ -f ${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.priv ]] || \
[[ -f ${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.pub ]]; then
echo "OpenDKIM - ${RED}Key files already exist for ${DOMAIN}.${COLOROFF}"
echo "Manually remove ${ODKDIR}/keys/${DOMAIN}/ if you want to regenerate."
exit 1
fi
fi
mkdir -p ${ODKDIR}/keys/${DOMAIN}
${ODKGK} -D ${ODKDIR}/keys/${DOMAIN}/ -d ${DOMAIN} -s ${SELECTOR}
if [[ $? -ne 0 ]] || [[ ! -f ${ODKDIR}/keys/${DOMAIN}/${SELECTOR}.private ]]; then
echo "${RED}Error:${COLOROFF} opendkim-genkey failed to produce key files."
exit 1
fi
chown -R opendkim:opendkim ${ODKDIR}/keys/${DOMAIN}
mv ${ODKDIR}/keys/${DOMAIN}/${SELECTOR}.private ${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.priv
mv ${ODKDIR}/keys/${DOMAIN}/${SELECTOR}.txt ${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.txt
# Append to KeyTable if entry doesn't already exist
if ! grep -q "^${SELECTOR}._domainkey.${DOMAIN} " ${ODKKEYT} 2>/dev/null; then
echo "${SELECTOR}._domainkey.${DOMAIN} ${DOMAIN}:${SELECTOR}:${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.priv" >> ${ODKKEYT}
fi
# Append to SigningTable if entry doesn't already exist
if ! grep -q "^\\*@${DOMAIN} " ${ODKSIGN} 2>/dev/null; then
echo "*@${DOMAIN} ${SELECTOR}._domainkey.${DOMAIN}" >> ${ODKSIGN}
echo "*@*.${DOMAIN} ${SELECTOR}._domainkey.${DOMAIN}" >> ${ODKSIGN}
fi
# Add server IP to TrustedHosts if not already there
if ! grep -q "^${BASEIP}$" ${ODKTRUST} 2>/dev/null; then
echo "${BASEIP}" >> ${ODKTRUST}
fi
systemctl restart opendkim
if [[ $? -ne 0 ]]; then
echo "${RED}Warning:${COLOROFF} opendkim failed to restart. Check: systemctl status opendkim"
fi
output_information
}
output_information() {
echo
echo "DKIM DNS records for ${DOMAIN}:"
echo "=================================================="
echo
cat ${ODKDIR}/keys/${DOMAIN}/${DOMAIN}.txt
echo
echo "Ensure the outbound IP of ${HOSTNAME} [${BASEIP}] is in the SPF record for ${DOMAIN}."
echo
}
##Argument gathering
if [[ -z "${1}" ]] || [[ "${1}" == "--help" ]] || [[ "${1}" == "-h" ]]; then
usage
fi
while getopts ":d:s:" OPTION; do
case "${OPTION}" in
d) DOMAIN="${OPTARG}" ;;
s) SELECTOR="${OPTARG}" ;;
*) usage ;;
esac
done
check_inputs
create_key