135 lines
3.8 KiB
Bash
Executable File
135 lines
3.8 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# vmail-stack
|
|
# https://git.stack-source.com/msb/vmail-stack
|
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
|
|
|
# load include file
|
|
source $(dirname $0)/vmail.sh
|
|
|
|
help()
|
|
{
|
|
thisfilename=$(basename -- "$0")
|
|
echo "$thisfilename"
|
|
echo "Add email account to vmail system"
|
|
echo ""
|
|
echo "usage: $thisfilename email password [OPTIONS]"
|
|
echo ""
|
|
echo " -h Print this help."
|
|
echo " -q QUOTA Set mailbox quota in GB, otherwise default for this domain is used. NULL means no limit."
|
|
echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1."
|
|
exit
|
|
}
|
|
|
|
# check for and set email address, local part & domain, password
|
|
if [ -n "$2" ]; then
|
|
if [ $1 == "-h" ] || [ $2 == "-h" ]; then
|
|
help
|
|
else
|
|
email=$1
|
|
shift
|
|
passwd=$1
|
|
shift
|
|
if [[ $email =~ "@" ]] ; then
|
|
mbox=${email%@*}
|
|
domain=${email##*@}
|
|
if [ -z $mbox ] ; then
|
|
echo "ERROR: No local part in $email."
|
|
exit
|
|
elif [ -z $domain ] ; then
|
|
echo "ERROR: No domain in $email."
|
|
exit
|
|
elif ! vmail::validate_domain $domain; then
|
|
echo "ERROR: $domain is not a valid domain name."
|
|
exit
|
|
fi
|
|
else
|
|
echo "ERROR: $email is not a valid email."
|
|
exit
|
|
fi
|
|
fi
|
|
else
|
|
help
|
|
fi
|
|
|
|
# set any options that were passed
|
|
while getopts "hq:s:" opt; do
|
|
case "${opt}" in
|
|
h )
|
|
help
|
|
exit;;
|
|
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
|
|
|
|
# get domain_id (and thus check if domain already exists)
|
|
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
|
|
if [ -z $domain_id ] ; then
|
|
echo "ERROR: Domain $domain does not exist."
|
|
exit
|
|
fi
|
|
|
|
# make sure mbox doesn't already exist
|
|
rowcount=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT COUNT(*) FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$mbox';"`
|
|
if [ "$rowcount" -eq '0' ] ; then
|
|
# mbox does not exist, build SQL
|
|
# first encrypt password
|
|
passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$passwd"|sed 's|^{SHA512-CRYPT}||'`
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_mboxes SET domain_id=\"$domain_id\", mbox=\"$mbox\", passwd=\"$passwd\""
|
|
if [ -n "$status" ] ; then
|
|
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
|
|
dbcmd="$dbcmd, status=\"$status\""
|
|
else
|
|
echo "ERROR: status (-s) must be 1 or 0"
|
|
exit
|
|
fi
|
|
fi
|
|
if [ -z "$quota" ] ; then
|
|
# 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
|
|
dbcmd="$dbcmd;'"
|
|
elif [ "$rowcount" -eq '1' ] ; then
|
|
echo "ERROR: $email already exists in vmail database."
|
|
exit
|
|
else
|
|
echo "ERROR: System error querying vmail database."
|
|
exit 1
|
|
fi
|
|
|
|
# add mbox
|
|
eval $dbcmd
|
|
|
|
# create all required vmail dirs
|
|
if [ ! -d "/var/vmail/$domain" ] ; then
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain
|
|
fi
|
|
if [ ! -d "/var/vmail/$domain/$mbox" ] ; then
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox
|
|
fi
|
|
if [ ! -d "/var/vmail/$domain/$mbox/Maildir" ] ; then
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox/Maildir
|
|
fi
|
|
if [ ! -d "/var/vmail/$domain/$mbox/Maildir/cur" ] ; then
|
|
install -o vmail -g vmail -m 750 -d /var/vmail/$domain/$mbox/Maildir/cur
|
|
fi
|