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 "Add domain to vmail system"
|
|
|
|
echo ""
|
|
|
|
echo "usage: $thisfilename domain [OPTIONS]"
|
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
|
|
|
echo " -l LIMIT Maximum number of mailboxes for this domain."
|
|
|
|
echo " -q QUOTA Default mailbox quota in GB."
|
|
|
|
echo " -s STATUS 1 for enabled, 0 for disabled."
|
|
|
|
echo ""
|
|
|
|
echo " Defaults for are all OPTIONS are configured in database."
|
|
|
|
echo " NULL for LIMIT or QUOTA means no limit."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
# check for and set domain
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
if [ $1 == "-h" ]; then
|
|
|
|
help
|
|
|
|
elif vmail::validate_domain $1; then
|
|
|
|
domain=$1
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
echo "ERROR: Invalid domain name: $1"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set any options that were passed
|
|
|
|
while getopts "hl:q:s:" opt; do
|
|
|
|
case "${opt}" in
|
|
|
|
h )
|
|
|
|
help
|
|
|
|
exit;;
|
|
|
|
l )
|
|
|
|
limit=${OPTARG}
|
|
|
|
;;
|
|
|
|
q )
|
|
|
|
quota=${OPTARG}
|
|
|
|
;;
|
|
|
|
s )
|
|
|
|
status=${OPTARG}
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
|
|
exit;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument" 1>&2
|
|
|
|
exit;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-02-19 14:28:26 -08:00
|
|
|
# set initial db query data
|
|
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
|
|
|
|
dbcmdopts=" -s -r -N -e"
|
|
|
|
dbquery="SELECT * from vm_domains"
|
|
|
|
|
2021-02-10 16:16:23 -08:00
|
|
|
# check if vmail domain dir exits
|
|
|
|
if [ -d $VMAIL_DIR/$domain ]; then
|
|
|
|
echo "ERROR: Directory $VMAIL_DIR/$domain already exists."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check if domain exists in vmail database
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="SELECT COUNT(*) from vm_domains WHERE domain='$domain';";
|
|
|
|
rowcount=`$dbcmd $dbcmdopts "$dbquery"`
|
2021-02-10 16:16:23 -08:00
|
|
|
if [ "$rowcount" -eq '0' ] ; then
|
|
|
|
# looks good, build SQL
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="INSERT INTO vm_domains SET domain='$domain'";
|
2021-02-10 16:16:23 -08:00
|
|
|
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
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery, status='$status'"
|
2021-02-10 16:16:23 -08:00
|
|
|
else
|
|
|
|
echo "ERROR: status (-s) must be 1 or 0"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ! -z "$limit" ] ; then
|
|
|
|
if [[ "$limit" == "NULL" ]]; then
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery, mbox_limit=NULL"
|
2021-02-10 16:16:23 -08:00
|
|
|
elif [[ "$limit" =~ ^[0-9]+$ ]]; then
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery, mbox_limit='$limit'"
|
2021-02-10 16:16:23 -08:00
|
|
|
else
|
|
|
|
echo "ERROR: limit (-l) must numeric or NULL"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ! -z "$quota" ] ; then
|
|
|
|
if [[ "$quota" == "NULL" ]]; then
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery, mbox_quota_default=NULL"
|
2021-02-10 16:16:23 -08:00
|
|
|
elif [[ "$quota" =~ ^[0-9]+$ ]]; then
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery, mbox_quota_default='$quota'"
|
2021-02-10 16:16:23 -08:00
|
|
|
else
|
|
|
|
echo "ERROR: quota (-q) must numeric or NULL"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2021-02-19 14:28:26 -08:00
|
|
|
dbquery="$dbquery;"
|
2021-02-10 16:16:23 -08:00
|
|
|
# add domain to vmail database
|
2021-02-19 14:28:26 -08:00
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\""
|
2021-02-10 16:16:23 -08:00
|
|
|
# create vmail directory for domain
|
|
|
|
if [ ! -d "$VMAIL_DIR" ] ; then
|
2021-02-19 14:28:26 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d $VMAIL_DIR
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
if [ ! -d "$VMAIL_DIR/$domain" ] ; then
|
2021-02-19 14:28:26 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d $VMAIL_DIR/$domain
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
elif [ "$rowcount" -eq '1' ] ; then
|
|
|
|
echo "ERROR: $domain already exists in vmail database."
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "ERROR: System error querying vmail database"
|
|
|
|
exit 1
|
|
|
|
fi
|