2021-02-10 16:16:23 -08:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vmail-stack
|
|
|
|
# https://git.stack-source.com/msb/vmail-stack
|
|
|
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
|
|
|
|
|
|
|
# load config
|
|
|
|
source /usr/local/etc/vmail.conf
|
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "$thisfilename"
|
|
|
|
echo "Delete domain and all associated email accounts from the vmail system."
|
|
|
|
echo ""
|
|
|
|
echo "usage: $thisfilename domain [OPTIONS]"
|
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
# check for and set domain
|
|
|
|
if [ -n "$1" ]; then
|
2021-02-15 17:10:31 -08:00
|
|
|
if [ $1 == "-h" ]; then
|
|
|
|
help
|
|
|
|
elif vmail::validate_domain $1; then
|
2021-02-10 16:16:23 -08:00
|
|
|
domain=$1
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
echo "ERROR: Invalid domain name: $1"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set any options that were passed
|
|
|
|
while getopts "h" opt; do
|
|
|
|
case "${opt}" in
|
|
|
|
h )
|
|
|
|
help
|
|
|
|
exit;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-02-15 17:10:31 -08:00
|
|
|
# build query
|
|
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
|
|
|
|
dbcmdopts="-s -r -N -e"
|
|
|
|
|
2021-02-10 16:16:23 -08:00
|
|
|
# check if domain exists in vmail database
|
2021-02-15 17:10:31 -08:00
|
|
|
dbquery="SELECT COUNT(*) FROM vm_domains WHERE domain='$domain';"
|
|
|
|
rowcount=`$dbcmd $dbcmdopts "$dbquery"`
|
|
|
|
|
|
|
|
if [ "$rowcount" -eq '1' ] ; then
|
|
|
|
# domain exists, delete from all vmail tables
|
|
|
|
# singled delete should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
|
|
|
|
dbquery="DELETE FROM vm_domains WHERE domain='$domain';"
|
2021-02-19 17:03:56 -08:00
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\""
|
|
|
|
dbquery="DELETE FROM sa_userpref WHERE username LIKE '%@$domain';"
|
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\""
|
|
|
|
dbquery="DELETE FROM vm_greylisting WHERE recipient LIKE '%@$domain';"
|
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\""
|
2021-02-15 17:10:31 -08:00
|
|
|
# this should be sufficient for rc due to ON DELETE CASCADE foreign key references
|
2021-02-19 17:03:56 -08:00
|
|
|
dbquery="DELETE FROM rc_users WHERE username LIKE '%@$domain';"
|
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\""
|
2021-02-15 17:10:31 -08:00
|
|
|
elif [ "$rowcount" -eq '0' ] ; then
|
|
|
|
echo "ERROR: $domain does not exist in vmail database."
|
2021-02-10 16:16:23 -08:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "ERROR: System error querying vmail database"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-15 17:10:31 -08:00
|
|
|
if [ -d "$VMAIL_DIR/$domain" ] ; then
|
2021-02-19 17:03:56 -08:00
|
|
|
/usr/bin/rm -r $VMAIL_DIR/$domain
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|