initial working version of vmail-domains-del

This commit is contained in:
Matthew Saunders Brown 2021-02-15 17:10:31 -08:00
parent 1018028f31
commit 5ffaff2c96

View File

@ -21,7 +21,9 @@ help()
# check for and set domain
if [ -n "$1" ]; then
if vmail::validate_domain $1; then
if [ $1 == "-h" ]; then
help
elif vmail::validate_domain $1; then
domain=$1
shift
else
@ -47,67 +49,34 @@ while getopts "h" opt; do
esac
done
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# check if domain exists in vmail database
rowcount=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT COUNT(*) from vm_domains WHERE domain='$domain';"`
if [ "$rowcount" -eq '0' ] ; then
# looks good, build SQL
cmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"INSERT INTO vm_domains SET domain='$domain'\""
if [ ! -z "$status" ] ; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
cmd="$cmd, status='$status'"
else
echo "ERROR: status (-s) must be 1 or 0"
exit 1
fi
fi
if [ ! -z "$limit" ] ; then
if [[ "$limit" == "NULL" ]]; then
cmd="$cmd, mbox_limit=NULL"
elif [[ "$limit" =~ ^[0-9]+$ ]]; then
cmd="$cmd, mbox_limit='$limit'"
else
echo "ERROR: limit (-l) must numeric or NULL"
exit 1
fi
fi
if [ ! -z "$quota" ] ; then
if [[ "$quota" == "NULL" ]]; then
cmd="$cmd, mbox_quota_default=NULL"
elif [[ "$quota" =~ ^[0-9]+$ ]]; then
cmd="$cmd, mbox_quota_default='$quota'"
else
echo "ERROR: quota (-q) must numeric or NULL"
exit 1
fi
fi
cmd="$cmd;"
# add domain to vmail database
echo $cmd
elif [ "$rowcount" -eq '1' ] ; then
echo "ERROR: $domain already exists in vmail database."
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';"
echo $dbcmd $dbcmdopts "\"$dbquery\""
dbquery="DELETE FROM sa_userpref WHERE username LIKE '%@$domain'"
echo $dbcmd $dbcmdopts "\"$dbquery\""
dbquery="DELETE FROM vm_greylisting WHERE recipient LIKE '%@$domain'"
echo $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'"
echo $dbcmd $dbcmdopts "\"$dbquery\""
elif [ "$rowcount" -eq '0' ] ; then
echo "ERROR: $domain does not exist in vmail database."
exit 1
else
echo "ERROR: System error querying vmail database"
exit 1
fi
# delete confirmed
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_aliases WHERE mbox_id='$mboxes_id';\""
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_autoresponders WHERE mbox_id='$mboxes_id';\""
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_filters WHERE mbox_id='$mboxes_id';\""
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_forwards WHERE domain_id='$domain_id' AND local_part='$mbox';\""
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_greylisting WHERE recipient='$mbox@$domain';\""
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vmail.sa_userpref WHERE username='$mbox@$domain';\""
# delete from rc_* tables
echo "mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"DELETE FROM vm_mboxes WHERE id='$mboxes_id';\""
if [ -d "$VMAIL_DIR/$domain/$mbox" ] ; then
echo "rm -r $VMAIL_DIR/$domain/$mbox"
fi
# delete from roundcube too
# check if vmail domain dir exits
if [ -d $VMAIL_DIR/$domain ]; then
echo "ERROR: Directory $VMAIL_DIR/$domain already exists."
exit 1
if [ -d "$VMAIL_DIR/$domain" ] ; then
echo "/usr/bin/rm -r $VMAIL_DIR/$domain"
fi