vmail-stack/bin/vmail-domains-mod.sh

125 lines
2.8 KiB
Bash
Raw Normal View History

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
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")
2021-04-27 14:07:47 -07:00
echo "Modify an existing domain."
2021-02-10 16:16:23 -08:00
echo ""
2021-04-27 14:07:47 -07:00
echo "usage: $thisfilename domain [OPTIONS]"
2021-02-10 16:16:23 -08:00
echo ""
echo " -h Print this help."
2021-04-27 14:07:47 -07:00
echo " -l LIMIT Mailbox limit - the maximum number of mailboxes under this domain."
echo " -q QUOTA Mailbox Quota Default - default quota for new mailboxes under this domain."
echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1."
2021-02-10 16:16:23 -08:00
exit
}
2021-04-27 14:07:47 -07:00
# check for and verify domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
else
domain=$1
shift
if vmail::validate_domain $domain; then
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE vm_domains.domain='$domain';"`
if [ -z $domain_id ] ; then
echo "ERROR: Email domain $domain does not exist."
exit
fi
else
echo "ERROR: $domain is not a valid domain name."
exit
fi
fi
else
help
fi
2021-02-10 16:16:23 -08:00
# set any options that were passed
2021-04-27 14:07:47 -07:00
while getopts "hl:q:s:" opt; do
2021-02-10 16:16:23 -08:00
case "${opt}" in
2021-04-27 14:07:47 -07:00
h )
help
exit;;
l )
limit=${OPTARG}
2021-02-10 16:16:23 -08:00
;;
2021-04-27 14:07:47 -07:00
q )
quota=${OPTARG}
;;
s )
status=${OPTARG}
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
;;
2021-02-10 16:16:23 -08:00
esac
done
2021-04-27 14:07:47 -07:00
# set update portion of command to empty string
dbset=""
2021-02-10 16:16:23 -08:00
2021-04-27 14:07:47 -07:00
# check for quota update
if [ -n "$quota" ]; then
# make quota uppercase in case it is set to NULL
quota=`echo $quota | tr [:lower:] [:upper:]`
if [[ "$quota" =~ ^[0-9]+$ ]] || [[ "$quota" == "NULL" ]]; then
dbset=" mbox_quota_default=\"$quota\""
else
echo "ERROR: quota (-q) must numeric or NULL"
exit 1
2021-02-10 16:16:23 -08:00
fi
2021-04-27 14:07:47 -07:00
fi
# check for limit update
if [ ! -z "$limit" ]; then
# make limit uppercase in case it is set to NULL
limit=`echo $limit | tr [:lower:] [:upper:]`
if [[ "$limit" =~ ^[0-9]+$ ]] || [[ "$limit" == "NULL" ]]; then
if [ ! -z "$dbset" ]; then
dbset="$dbset,"
2021-02-10 16:16:23 -08:00
fi
2021-04-27 14:07:47 -07:00
dbset="$dbset mbox_limit=\"$limit\""
else
echo "ERROR: limit (-l) must numeric or NULL"
exit 1
2021-02-10 16:16:23 -08:00
fi
2021-04-27 14:07:47 -07:00
fi
# check for status update
if [ ! -z "$status" ]; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
if [ ! -z "$dbset" ]; then
dbset="$dbset,"
2021-02-10 16:16:23 -08:00
fi
2021-04-27 14:07:47 -07:00
dbset="$dbset status=\"$status\""
else
echo "ERROR: status (-s) must be 1 or 0"
exit
2021-02-10 16:16:23 -08:00
fi
2021-04-27 14:07:47 -07:00
fi
if [ -n "$dbset" ]; then
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'UPDATE vm_domains SET $dbset WHERE domain.id=\"$domain_id\";'"
# eval $dbcmd
echo $dbcmd
2021-02-10 16:16:23 -08:00
else
2021-04-27 14:07:47 -07:00
echo "ERROR: No values passed for update."
2021-02-10 16:16:23 -08:00
fi