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

109 lines
3.3 KiB
Bash
Raw Permalink Normal View History

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 "Add domain to vmail system"
echo ""
2021-12-30 16:50:23 -08:00
echo "usage: $thisfilename -d <domain> [-l <limit>] [-q <quota>] [-r <ratelimit>] [-s <status>]"
2021-02-10 16:16:23 -08:00
echo ""
2021-12-30 16:50:23 -08:00
echo " -h Print this help."
echo " -d Domain to add to vmail system."
echo " -l <limit> Maximum number of mailboxes for this domain."
echo " -q <quota> Default mailbox quota in GB."
echo " -r <ratelimit> Default mailbox sending rate limit per hour, multiplied by 10 for limit per day."
echo " -s <status> 1 for enabled, 0 for disabled."
2021-02-10 16:16:23 -08:00
echo ""
2021-12-30 16:50:23 -08:00
echo " Defaults for all Options are configured in database."
echo " NULL for LIMIT or QUOTA means no limit."
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 "ERROR: domain name is required"
exit 1
2021-10-15 15:17:29 -07:00
fi
2021-02-10 16:16:23 -08:00
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"
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
2022-11-29 16:33:15 -08:00
dbquery="SELECT id from vm_domains WHERE domain='$domain';";
domains_id=`$dbcmd $dbcmdopts "$dbquery"`
if [[ -z $domains_id ]]; then
2021-02-10 16:16:23 -08:00
# looks good, build SQL
2021-02-19 14:28:26 -08:00
dbquery="INSERT INTO vm_domains SET domain='$domain'";
2022-11-29 16:33:15 -08:00
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
2022-11-29 16:33:15 -08:00
if [[ ! -z "$limit" ]] ; then
2021-02-10 16:16:23 -08:00
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
2022-11-29 16:33:15 -08:00
if [[ ! -z "$quota" ]] ; then
2021-02-10 16:16:23 -08:00
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
2022-11-29 16:33:15 -08:00
if [[ ! -z "$ratelimit" ]] ; then
2021-12-30 16:50:23 -08:00
if [[ "$ratelimit" == "NULL" ]]; then
dbquery="$dbquery, mbox_ratelimit_default=NULL"
elif [[ "$ratelimit" =~ ^[0-9]+$ ]]; then
dbquery="$dbquery, mbox_ratelimit_default='$ratelimit'"
2021-12-30 16:50:23 -08:00
else
echo "ERROR: ratelimit (-r) must numeric or NULL"
exit 1
fi
fi
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
2022-11-29 16:33:15 -08:00
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
2022-11-29 16:33:15 -08:00
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
2022-11-29 16:33:15 -08:00
elif [[ "$domains_id" -gt 0 ]] ; then
2021-02-10 16:16:23 -08:00
echo "ERROR: $domain already exists in vmail database."
exit 1
else
echo "ERROR: System error querying vmail database"
exit 1
fi