Skip to content

Instantly share code, notes, and snippets.

@MarcelFox
Created April 8, 2018 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcelFox/e7f6fe6af7cffd785c504cea2aad50d2 to your computer and use it in GitHub Desktop.
Save MarcelFox/e7f6fe6af7cffd785c504cea2aad50d2 to your computer and use it in GitHub Desktop.
dns_cpmail.sh | checks DMARC, DKIM & SPF of a primary domain of a user.
#!/bin/bash
#
# Name: dns_cpmail
# Version: 1.0
# By MarcelFox
#
# HOW TO WORK WITH?
# As 'root' and in a cPanel server run:
# bash dns_cpmail.sh
#
# WHAT DOES?
# Check for DMARC, DKIM & SPF of a primary domain
# of a user direct from his $HOME. If none of those
# entries (or one of those), are not found, dns_cpamail
# will adjust, check the zone and reload the services
# rndc and named.
#
# WHY ROCKS?
# It's flawless so far, and it gives you links to
# check for the entries on MXToolBox, which is a relief!
# It's also pretty, with colors and styles :)
#
# VARIABLES:
var_user=$(pwd | cut -d"/" -f3)
var_email=$(whmapi1 accountsummary user=$var_user | grep "email:" | awk '{gsub(/"|*/, ""); print $2}')
var_domain=$(awk -v user="$var_user" '$0 ~ user && gsub(":", "") {print $1}' /etc/trueuserdomains)
var_zone="/var/named/$var_domain.db"
var_dmarc="_dmarc.$var_domain. 1800 IN TXT \"v=DMARC1;p=quarantine;pct=100;\""
# COLORS & STYLES:
BOLD='\e[1m'
ITALIC='\e[3m'
NO_S='\e[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_C='\033[0m'
function DMARC_ENTRY() {
grep dmarc /var/named/$var_domain.db 1>/dev/null
if [ $? -eq 1 ];
then
echo -e "\tSetting DMARC"
echo $var_dmarc >> $var_zone;
else
echo -e "\tDMARC Already set";
fi
}
function SPF_ENTRY {
grep spf1 /var/named/$var_domain.db 1>/dev/null
if [ $? -eq 1 ];
then
echo -e "\tSetting SPF"
/usr/local/cpanel/bin/spf_installer $var_user;
else
echo -e "\tSPF Already set";
fi
}
function DKIM_ENTRY {
grep default._domainkey /var/named/$var_domain.db 1>/dev/null
if [ $? -eq 1 ];
then
echo -e "\tSetting DKIM"
/usr/local/cpanel/bin/dkim_keys_install $var_user;
else
echo -e "\tDKIM Already set";
fi
}
function reload_named() {
ERROR=$( { named-checkzone $var_domain $var_zone; } 2>&1 );
if [ $? -eq 0 ]; then
rndc flush && rndc reload;
service named reload;
echo -e "${BOLD}\nCheck DKIM, DMARC & SPF:${NO_S}\n"
echo -e "\tDKIM: https://mxtoolbox.com/SuperTool.aspx?action=dkim%3a$var_domain%3adefault&run=toolpage"
echo -e "\tDMARC: https://mxtoolbox.com/SuperTool.aspx?action=dmarc%3a$var_domain&run=toolpage"
echo -e "\tSPF: https://mxtoolbox.com/SuperTool.aspx?action=spf%3a$var_domain&run=toolpage\n"
else
echo -e "There are some errors in your $var_zone file\n"
echo -e "${RED}ERROR:${NO_C}"
echo -e "${RED}$ERROR${NO_C}";
echo -e "\nPlease adjust the file zone $var_zone and reload the services.\n"
fi
}
function check() {
reload_named
}
echo -e "${BOLD}\nChecking domain${GREEN}${BOLD} https://$var_domain${NO_C}${NO_S}"
echo -e "${BOLD}\nChecking Entries:${NO_S}\n"
DKIM_ENTRY
SPF_ENTRY
DMARC_ENTRY
echo -e "${BOLD}\nChecking DNS zone and reloading services:${NO_S}\n"
check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment