#!/bin/bash # # vmail-stack # https://git.stack-source.com/msb/vmail-stack # Copyright (c) 2022 Matthew Saunders Brown # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # load include file source $(dirname $0)/vmail.sh help() { thisfilename=$(basename -- "$0") echo "$thisfilename" echo "Delete domain and all associated email accounts from the vmail system." echo "" echo "usage: $thisfilename -d [-h]" echo "" echo " -h Print this help." echo " -d Domain to be removed from the vmail system." exit } vmail:getoptions "$@" # check for domain if [[ -z $domain ]]; then echo "domain name is required" exit fi # build query dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmdopts="-s -r -N -e" # check if domain exists in vmail database dbquery="SELECT id FROM vm_domains WHERE domain='$domain';" domains_id=`$dbcmd $dbcmdopts "$dbquery"` if [[ "$domains_id" -gt '0' ]] ; then # domain exists, delete from all vmail tables # single delete should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references dbquery="DELETE FROM vm_domains WHERE domain='$domain';" 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\"" # this should be sufficient for rc due to ON DELETE CASCADE foreign key references dbquery="DELETE FROM rc_users WHERE username LIKE '%@$domain';" eval $dbcmd $dbcmdopts "\"$dbquery\"" elif [[ -z $domains_id ]] ; then echo "ERROR: $domain does not exist in vmail database." exit 1 else echo "ERROR: System error querying vmail database" exit 1 fi if [ -d "$VMAIL_DIR/$domain" ] ; then /usr/bin/rm -r $VMAIL_DIR/$domain fi