diff --git a/bin/vmail-mboxes-mod.sh b/bin/vmail-mboxes-mod.sh new file mode 100755 index 0000000..9dca1bc --- /dev/null +++ b/bin/vmail-mboxes-mod.sh @@ -0,0 +1,134 @@ +#!/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 "Modify an existing email." + echo "" + echo "usage: $thisfilename email [OPTIONS]" + echo "" + echo " -h Print this help." + echo " -p PASSWORD Set new password." + 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 +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + else + email=$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 "hp:q:s:" opt; do + case "${opt}" in + h ) + help + exit;; + p ) + passwd=${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 + +# get mbox id (and thus check if email account already exists) +mbox_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT vm_mboxes.id from vm_mboxes, vm_domains WHERE vm_domains.domain='$domain' AND vm_mboxes.mbox='$mbox' AND vm_domains.id=vm_mboxes.domain_id;"` + +if [ -z $mbox_id ] ; then + echo "ERROR: Email address $email does not exist." + exit +fi + +# set update portion of command to empty string +dbset="" + +# check for quota update +if [ -n "$quota" ]; then + # make quota uppercase in case it is set to NULL + quota=`echo $quota | tr [:lower:] [:upper:]` + if [[ "$quota" =~ ^[0-9]+$ ]] || [[ "$quota" == "NULL" ]]; then + dbset=" quota=\"$quota\"" + else + echo "ERROR: quota (-q) must numeric or NULL" + exit 1 + fi +fi + +# check for password update +if [ ! -z "$passwd" ]; then + passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$passwd"|sed 's|^{SHA512-CRYPT}||'` + if [ ! -z "$dbset" ]; then + dbset="$dbset," + fi + dbset=" $dbset passwd=\"$passwd\"" +fi + +# check for status update +if [ ! -z "$status" ]; then + if [ "$status" == 0 ] || [ "$status" == 1 ]; then + if [ ! -z "$dbset" ]; then + dbset="$dbset," + fi + dbset="$dbset status=\"$status\"" + else + echo "ERROR: status (-s) must be 1 or 0" + exit + fi +fi + +if [ -n "$dbset" ]; then + + # build query + dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'UPDATE vm_mboxes SET $dbset WHERE mbox.id=\"$mbox_id\";'" +# eval $dbcmd + echo $dbcmd + +else + + echo "ERROR: No values passed for update." + +fi