2021-02-10 16:16:23 -08:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vmail-stack
|
|
|
|
# https://git.stack-source.com/msb/vmail-stack
|
2022-08-22 13:34:20 -07:00
|
|
|
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2021-02-10 16:16:23 -08:00
|
|
|
|
2021-04-02 12:02:50 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vmail.sh
|
2021-02-10 16:16:23 -08:00
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "$thisfilename"
|
|
|
|
echo "Delete domain and all associated email accounts from the vmail system."
|
|
|
|
echo ""
|
2021-10-15 15:17:29 -07:00
|
|
|
echo "usage: $thisfilename -d <domain> [-h]"
|
2021-02-10 16:16:23 -08:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
2021-10-15 15:17:29 -07:00
|
|
|
echo " -d Domain to be removed from the vmail system."
|
2021-02-10 16:16:23 -08:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
vmail:getoptions "$@"
|
2021-02-10 16:16:23 -08:00
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
# check for domain
|
|
|
|
if [[ -z $domain ]]; then
|
|
|
|
echo "domain name is required"
|
|
|
|
exit
|
|
|
|
fi
|
2021-02-10 16:16:23 -08:00
|
|
|
|
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
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="SELECT id FROM vm_domains WHERE domain='$domain';"
|
|
|
|
domains_id=`$dbcmd $dbcmdopts "$dbquery"`
|
2021-02-15 17:10:31 -08:00
|
|
|
|
2022-11-29 16:33:15 -08:00
|
|
|
if [[ "$domains_id" -gt '0' ]] ; then
|
2021-02-15 17:10:31 -08:00
|
|
|
# domain exists, delete from all vmail tables
|
2021-10-15 15:17:29 -07:00
|
|
|
# single delete should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
|
2021-02-15 17:10:31 -08:00
|
|
|
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\""
|
2022-11-29 16:33:15 -08:00
|
|
|
elif [[ -z $domains_id ]] ; then
|
2021-02-15 17:10:31 -08:00
|
|
|
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
|
|
|
|
|
2023-10-10 13:16:32 -07:00
|
|
|
# disable webmail
|
|
|
|
if [[ -f /etc/apache2/sites-available/mail.$domain.conf ]]; then
|
|
|
|
/usr/local/bin/vmail-webmail-disable.sh -d $domain
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove the dovecot config
|
|
|
|
if [[ -f /etc/dovecot/sites.d/mail.$domain.conf ]]; then
|
|
|
|
/usr/local/bin/vmail-dovecot-disable.sh -d $domain
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove dkim files
|
|
|
|
if [[ -f /etc/ssl/dkim/$domain.selector ]]; then
|
|
|
|
/usr/local/bin/vmail-dkim-del.sh -d $domain
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove let's encrypt files
|
|
|
|
if [[ -f /etc/letsencrypt/renewal/mail.$domain.conf ]]; then
|
|
|
|
if [[ -f /usr/local/bin/letsencrypt-del.sh ]]; then
|
|
|
|
/usr/local/bin/letsencrypt-del.sh -d mail.$domain
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove maildirs
|
|
|
|
if [[ -d "$VMAIL_DIR/$domain" ]] ; then
|
|
|
|
/usr/bin/rm --recursive --force $VMAIL_DIR/$domain
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|