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 email account to vmail system"
|
|
|
|
echo ""
|
2021-12-30 16:50:23 -08:00
|
|
|
echo "usage: $thisfilename -e <email> -p <password> [-q <quota>] [-r <ratelimit>] [-s <0|1>] [-j <0|1|2>] [-h]"
|
2021-02-10 16:16:23 -08:00
|
|
|
echo ""
|
2021-12-30 16:50:23 -08:00
|
|
|
echo " -h Print this help."
|
|
|
|
echo " -e <email> Email address to add."
|
|
|
|
echo " -p <password> Unencrypted Password for new email address."
|
|
|
|
echo " -q <quota> Set mailbox quota in GB, otherwise default for this domain is used. NULL means no limit."
|
2022-05-11 12:34:19 -07:00
|
|
|
echo " -r <ratelimit> Hourly rate limit for sending, multiplied by 10 for the daily limit. If unset domain default is used."
|
2022-06-30 08:24:35 -07:00
|
|
|
echo " -s <0|1|2> Status. 0 = disabled, 1 = enabled, 2 = enabled with admin privileges. Default is 1."
|
2021-12-30 16:50:23 -08:00
|
|
|
echo " -j <0|1|2> Filter Junk/Spam messages. 0 = no filtering. 1 = filter Junk only. 2 = filter Junk & Spam. Default is 2."
|
2021-02-10 16:16:23 -08:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
vmail:getoptions "$@"
|
|
|
|
|
|
|
|
# check for email
|
|
|
|
if [[ -z $email ]]; then
|
|
|
|
echo "email is required"
|
2022-05-11 12:35:36 -07:00
|
|
|
exit 1
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
# check for password
|
|
|
|
if [[ -z $password ]]; then
|
|
|
|
echo "password is required"
|
2022-05-11 12:35:36 -07:00
|
|
|
exit 1
|
2021-10-15 15:17:29 -07:00
|
|
|
fi
|
2021-02-12 09:47:26 -08:00
|
|
|
|
2022-11-29 16:33:15 -08:00
|
|
|
# get domain_id to check if domain already exists
|
2021-02-10 16:16:23 -08:00
|
|
|
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
|
2022-11-29 16:33:15 -08:00
|
|
|
if [[ -z $domain_id ]] ; then
|
2021-02-10 16:16:23 -08:00
|
|
|
echo "ERROR: Domain $domain does not exist."
|
2022-05-11 12:35:36 -07:00
|
|
|
exit 1
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure mbox doesn't already exist
|
2022-11-29 16:33:15 -08:00
|
|
|
mbox_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id FROM vm_mboxes WHERE mbox='$mbox' AND domain='$domain';"`
|
|
|
|
if [[ -z $mbox_id ]] ; then
|
2021-02-10 16:16:23 -08:00
|
|
|
# mbox does not exist, build SQL
|
|
|
|
# first encrypt password
|
2022-09-12 16:27:21 -07:00
|
|
|
passwd=`/usr/bin/openssl passwd -6 "$password"`
|
|
|
|
passwd="{SHA512-CRYPT}$passwd"
|
2022-11-29 16:33:15 -08:00
|
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_mboxes SET mbox=\"$mbox\", domain=\"$domain\", passwd=\"$passwd\""
|
2021-10-15 15:17:29 -07:00
|
|
|
if [[ -n $status ]] ; then
|
|
|
|
dbcmd="$dbcmd, status=\"$status\""
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
2021-11-20 15:20:24 -08:00
|
|
|
if [[ -n $filter ]] ; then
|
|
|
|
dbcmd="$dbcmd, filter=\"$filter\""
|
|
|
|
fi
|
2022-11-29 16:33:15 -08:00
|
|
|
if [[ -z "$quota" ]] ; then
|
2021-02-10 16:16:23 -08:00
|
|
|
# get mbox_quota_default from domains table
|
|
|
|
quota=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT mbox_quota_default from vm_domains WHERE domain='$domain';"`
|
|
|
|
fi
|
|
|
|
if [[ "$quota" == "NULL" ]]; then
|
|
|
|
dbcmd="$dbcmd, quota=NULL"
|
|
|
|
elif [[ "$quota" =~ ^[0-9]+$ ]]; then
|
|
|
|
dbcmd="$dbcmd, quota=\"$quota\""
|
|
|
|
else
|
|
|
|
echo "ERROR: quota (-q) must numeric or NULL"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-12-30 16:50:23 -08:00
|
|
|
if [ -z "$ratelimit" ] ; then
|
|
|
|
# get mbox_ratelimit_default from domains table
|
|
|
|
ratelimit=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT mbox_ratelimit_default from vm_domains WHERE domain='$domain';"`
|
|
|
|
fi
|
|
|
|
if [[ "$ratelimit" == "NULL" ]]; then
|
|
|
|
dbcmd="$dbcmd, ratelimit=NULL"
|
|
|
|
elif [[ "$ratelimit" =~ ^[0-9]+$ ]]; then
|
|
|
|
dbcmd="$dbcmd, ratelimit=\"$ratelimit\""
|
|
|
|
else
|
|
|
|
echo "ERROR: ratelimit (-r) must numeric or NULL"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-02-10 16:16:23 -08:00
|
|
|
dbcmd="$dbcmd;'"
|
2022-11-29 16:33:15 -08:00
|
|
|
elif [[ $mbox_id -gt '0' ]] ; then
|
2021-02-10 16:16:23 -08:00
|
|
|
echo "ERROR: $email already exists in vmail database."
|
2022-05-11 12:35:36 -07:00
|
|
|
exit 1
|
2021-02-10 16:16:23 -08:00
|
|
|
else
|
|
|
|
echo "ERROR: System error querying vmail database."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# add mbox
|
2021-02-19 17:03:56 -08:00
|
|
|
eval $dbcmd
|
2021-02-10 16:16:23 -08:00
|
|
|
|
|
|
|
# create all required vmail dirs
|
|
|
|
if [ ! -d "/var/vmail/$domain" ] ; then
|
2021-02-19 17:03:56 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
if [ ! -d "/var/vmail/$domain/$mbox" ] ; then
|
2021-02-19 17:03:56 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
if [ ! -d "/var/vmail/$domain/$mbox/Maildir" ] ; then
|
2021-02-19 17:03:56 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox/Maildir
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|
|
|
|
if [ ! -d "/var/vmail/$domain/$mbox/Maildir/cur" ] ; then
|
2021-02-19 17:03:56 -08:00
|
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox/Maildir/cur
|
2021-02-10 16:16:23 -08:00
|
|
|
fi
|