#!/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 -e -p [-q ] [-r ] [-s <0|1>] [-j <0|1|2>] [-h]" echo "" echo " -h Print this help." echo " -e Email address to add." echo " -p Unencrypted Password for new email address." echo " -q Set mailbox quota in GB, otherwise default for this domain is used. NULL means no limit." echo " -r Hourly rate limit for sending, multiplied by 10 for the daily limit. If unset domain default is used." echo " -s <0|1> Status. 1 for enabled, 0 for disabled. Default is 1 for enabled." echo " -j <0|1|2> Filter Junk/Spam messages. 0 = no filtering. 1 = filter Junk only. 2 = filter Junk & Spam. Default is 2." } vmail:getoptions "$@" # check for email if [[ -z $email ]]; then echo "email is required" exit 1 fi # check for password if [[ -z $password ]]; then echo "password is required" exit 1 fi # 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 1 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 "$password"` 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 dbcmd="$dbcmd, status=\"$status\"" fi if [[ -n $filter ]] ; then dbcmd="$dbcmd, filter=\"$filter\"" 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 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 dbcmd="$dbcmd;'" elif [ "$rowcount" -eq '1' ] ; then echo "ERROR: $email already exists in vmail database." exit 1 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