2021-02-10 16:16:23 -08:00
|
|
|
#!/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 alias data from vmail database."
|
|
|
|
echo ""
|
|
|
|
echo "usage: $thisfilename [email|alias|domain] [OPTIONS]"
|
|
|
|
echo ""
|
|
|
|
echo " -a Return info for specific alias."
|
|
|
|
echo " -c Output in cvs format."
|
|
|
|
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 aliases for all email acccounts for all domains will be returned."
|
|
|
|
echo " Enter email address to get all aliases that route to that email address."
|
|
|
|
echo " Enter alias (in full email address format) with the -a option to get address that alias delivers to."
|
|
|
|
echo " Enter domain name to get all aliases for all email addresses under that domain."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-02-11 14:09:07 -08:00
|
|
|
# set any options that were passed
|
|
|
|
while getopts "achs" opt; do
|
|
|
|
case "${opt}" in
|
|
|
|
a )
|
|
|
|
aliassearch=true
|
|
|
|
;;
|
|
|
|
c )
|
|
|
|
cvs="| sed 's/\t/,/g'"
|
|
|
|
;;
|
|
|
|
h )
|
|
|
|
help
|
|
|
|
exit;;
|
|
|
|
s )
|
|
|
|
dbcmdopts="-s -N $dbcmdopts"
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
|
|
exit;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-02-10 16:16:23 -08:00
|
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
|
|
|
|
dbcmdopts="-e"
|
|
|
|
dbquery="SELECT vm_aliases.alias, vm_mboxes.mbox, vm_domains.domain FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id"
|
|
|
|
|
|
|
|
# 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
|
|
|
|
localpart=${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
|
|
|
|
|
|
|
|
# build query
|
|
|
|
if [ -n "$domain" ]; then
|
|
|
|
# add specific domain
|
|
|
|
dbquery="$dbquery AND vm_domains.domain='$domain'"
|
|
|
|
if [ -n "$localpart" ]; then
|
|
|
|
# search for specific alias or mbox
|
|
|
|
if [ -n "$aliassearch" ]; then
|
|
|
|
# search for specific alias address
|
|
|
|
dbquery="$dbquery AND vm_aliases.alias='$localpart'"
|
|
|
|
else
|
|
|
|
# search for all aliases for specific email address
|
|
|
|
dbquery="$dbquery AND vm_mboxes.mbox='$localpart'"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set order by
|
|
|
|
dbquery="$dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox, vm_aliases.alias;";
|
|
|
|
|
|
|
|
# execute mysql query
|
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs
|