added initial vm-forwards scripts

This commit is contained in:
Matthew Saunders Brown 2021-02-16 13:27:27 -08:00
parent a95e6fec83
commit d6f227565d
3 changed files with 305 additions and 0 deletions

129
bin/vmail-forwards-add.sh Executable file
View File

@ -0,0 +1,129 @@
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /usr/local/etc/vmail.conf
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Add email forward to vmail system"
echo ""
echo "usage: $thisfilename email forward_to [OPTIONS]"
echo ""
echo " -h Print this help."
echo " -l Save Local (forward & keep local copy). 0 = no/off, 1 = yes/on. Default is 0."
echo ""
echo " 'email' and 'forward_to' should both be in full email address format."
echo " .e.g to have info@example.com delivered (forwarded) to joe@example.com do:"
echo " $thisfilename info@example.com joe@example.com"
exit
}
# check for and set forward_to & email address & split email in to mbox (local part) & domain
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-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 1
elif [ -z $domain ] ; then
echo "ERROR: No domain in $email."
exit 1
elif ! vmail::validate_domain $domain; then
echo "ERROR: $domain is not a valid domain name."
exit 1
fi
else
echo "ERROR: $email is not a valid email."
exit 1
fi
forward_to=$1
shift
if [[ $forward_to =~ "@" ]] ; then
forward_to_mbox=${forward_to%@*}
forward_to_domain=${forward_to##*@}
if [ -z $forward_to_mbox ] ; then
echo "ERROR: No local part in $forward_to."
exit 1
elif [ -z $forward_to_domain ] ; then
echo "ERROR: No domain in $forward_to."
exit 1
elif ! vmail::validate_domain $forward_to_domain; then
echo "ERROR: $forward_to_domain is not a valid domain name."
exit 1
fi
else
echo "ERROR: $forward_to is not a valid email."
exit 1
fi
fi
else
help
fi
# set any options that were passed
while getopts "hl:" opt; do
case "${opt}" in
h )
help
exit;;
l )
save_local=${OPTARG}
if [ "$save_local" != "0" ] && [ "$save_local" != "1" ]; then
echo "ERROR: Invalid save local setting: -l $save_local."
help
exit 1
fi
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# get mbox id, which also verfies that email address exists
dbquery="SELECT vm_mboxes.id FROM vm_mboxes, vm_domains WHERE vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain';"
mbox_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $mbox_id ]; then
# mbox does not exist, can't create forward
echo "ERROR: Address to Forward ($email) does not exist."
exit 1
elif [ "$mbox_id" -gt '0' ]; then
# verified mbox, check for existing forward
dbquery="SELECT id FROM vm_forwards WHERE mbox_id='$mbox_id';"
vm_forwards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $vm_forwards_id ]; then
# existing forward does not exist, add it now
dbcommand="INSERT INTO vm_forwards SET mbox_id='$mbox_id', forward_to='$forward_to'"
if [ -n $save_local ]; then
dbquery="$dbquery, save_local='$save_local'"
fi
echo $dbcmd $dbcmdopts \"$dbquery;\"
else
echo "ERROR: Forward for $email already exists. To change either del then add or use mod script."
exit 1
fi
else
# db query error
echo "ERROR: System error querying vmail database."
exit 1
fi

77
bin/vmail-forwards-del.sh Executable file
View File

@ -0,0 +1,77 @@
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /usr/local/etc/vmail.conf
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Delete email forward from vmail database."
echo ""
echo "usage: $thisfilename email"
echo ""
echo " -h Print this help."
echo ""
echo " Enter email in full email address format to delete forwarding."
exit
}
# check for and set alias search term(s)
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif [[ ! $1 =~ ^- ]] ; then
email=$1
shift
if [[ $email =~ "@" ]] ; then
mbox=${email%@*}
domain=${email##*@}
if ! vmail::validate_domain $domain; then
echo "ERROR: $domain is not a valid domain name."
exit 1
fi
else
echo "ERROR: $email is not a valid email alias."
help
fi
fi
fi
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# get aliases id which also works to confirm alias exists
dbquery="SELECT vm_forwards.id FROM vm_forwards, vm_mboxes, vm_domains WHERE vm_forwards.mbox_id=vm_mboxes.id AND vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain';"
vm_forwards_id=`$dbcmd $dbcmdopts "$dbquery"`
if [ -z "$vm_forwards_id" ]; then
echo "ERROR: Forward for $email does not exist."
exit 1
elif [ "$vm_forwards_id" -gt '0' ]; then
dbquery="DELETE FROM vm_forwards WHERE vm_forwards.id='$vm_forwards_id';"
eval $dbcmd $dbcmdopts \"$dbquery\"
echo "SUCCESS: Forward for $email removed from system."
exit 0
else
echo "ERROR: System error querying vmail database"
exit 1
fi

99
bin/vmail-forwards-get.sh Executable file
View File

@ -0,0 +1,99 @@
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /usr/local/etc/vmail.conf
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get email forwards data from vmail database."
echo ""
echo "usage: $thisfilename [email|forward|domain] [OPTIONS]"
echo ""
echo " -c Output in cvs format."
echo " -f Return info for specific forward."
echo " -h Print this help."
echo " -s Be more silent - use tabs instead of tables for output, do not display column headers."
echo ""
echo " Search term is optional. If nothing specified all forwards for all email acccounts for all domains will be returned."
echo " Enter email address to get forward info for that email address."
echo " Enter forwarding address (in full email address format) with the -f option to get address(es) that forward to specified address."
echo " Enter domain name to get all forwards for all email addresses under that domain."
exit
}
# check for and set alias search term(s)
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif [[ ! $1 =~ ^- ]] ; then
searchterm=$1
shift
if [[ $searchterm =~ "@" ]] ; then
mbox=${searchterm%@*}
domain=${searchterm##*@}
if ! vmail::validate_domain $domain; then
echo "ERROR: $searchterm is not a valid email address."
exit
fi
elif vmail::validate_domain $searchterm; then
domain=$searchterm
else
echo "ERROR: $searchterm is not a valid search term."
help
fi
fi
fi
# set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain, vm_forwards.forward_to, vm_forwards.save_local FROM vm_forwards, vm_mboxes, vm_domains WHERE vm_forwards.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id"
# set any options that were passed
while getopts "cfhs" opt; do
case "${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
f )
forwardsearch=true
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
if [ -n "$domain" ]; then
# add specific domain
dbquery="$dbquery AND vm_domains.domain='$domain'"
if [ -n "$mbox" ]; then
# search for specific alias or mbox
if [ -n "$forwardsearch" ]; then
# search for specific forward to address
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain, vm_forwards.forward_to, vm_forwards.save_local FROM vm_forwards, vm_mboxes, vm_domains WHERE vm_forwards.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id AND vm_forwards.forward_to='$mbox@$domain'"
else
# search for forward for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
fi
fi
fi
# set order by
dbquery="$dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox, vm_forwards.forward_to;";
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs