2021-02-10 16:16:23 -08:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vmail-stack
|
|
|
|
# https://git.stack-source.com/msb/vmail-stack
|
2022-08-22 13:34:20 -07:00
|
|
|
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2021-02-10 16:16:23 -08:00
|
|
|
|
2021-04-02 12:02:50 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vmail.sh
|
2021-02-10 16:16:23 -08:00
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "$thisfilename"
|
|
|
|
echo "Get email account data from vmail database."
|
|
|
|
echo ""
|
2021-10-15 15:17:29 -07:00
|
|
|
echo "usage: $thisfilename [-m <mbox>|-d <domain>|-e <email>] [OPTIONS]"
|
2021-02-10 16:16:23 -08:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
2021-10-15 15:17:29 -07:00
|
|
|
echo " -m <mbox> Return all email accounts, accross all domains, with <mbox> for the local part."
|
|
|
|
echo " -d <domain> Return all email accounts from specified domain."
|
|
|
|
echo " -e <email> Specific email address to search for."
|
|
|
|
echo " -c Output in cvs format."
|
|
|
|
echo " -t Use tabs instead of tables for output, do not display column headers."
|
|
|
|
echo " -v Include encrypt password in output."
|
2021-02-10 16:16:23 -08:00
|
|
|
echo ""
|
|
|
|
echo " Search term is optional. If nothing specified all email acccounts for all domains will be returned."
|
2022-11-29 16:33:15 -08:00
|
|
|
echo " Examples:"
|
2021-10-15 15:17:29 -07:00
|
|
|
echo " $thisfilename -e joe@example.com # search for specific email address 'joe@example.com'."
|
|
|
|
echo " $thisfilename -m joe # search for username 'joe' in all domains."
|
|
|
|
echo " $thisfilename -d example.com # search for all usernames in the 'example.com' domain."
|
2021-02-10 16:16:23 -08:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
vmail:getoptions "$@"
|
|
|
|
|
2021-02-10 16:16:23 -08:00
|
|
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
|
|
|
|
dbcmdopts="-e"
|
|
|
|
|
2021-10-15 16:04:29 -07:00
|
|
|
if [[ -n $tab ]]; then
|
|
|
|
dbcmdopts="-s -N $dbcmdopts"
|
|
|
|
fi
|
|
|
|
|
2021-02-10 16:16:23 -08:00
|
|
|
# build query
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="SELECT mbox, domain"
|
|
|
|
if [[ -n $verbose ]]; then
|
|
|
|
dbquery="$dbquery, passwd"
|
2021-09-04 15:42:13 -07:00
|
|
|
fi
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="$dbquery, status, quota, ratelimit, filter FROM vm_mboxes"
|
|
|
|
if [[ -n $mbox ]] && [[ -n $domain ]]; then
|
2021-02-10 16:16:23 -08:00
|
|
|
# search for specific email address
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="$dbquery WHERE mbox='$mbox' AND domain='$domain'"
|
|
|
|
elif [[ -n $mbox ]] && [[ -z $domain ]]; then
|
2021-02-10 16:16:23 -08:00
|
|
|
# search all domains for username
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="$dbquery WHERE mbox='$mbox'"
|
|
|
|
elif [[ -z $mbox ]] && [[ -n $domain ]]; then
|
2021-02-10 16:16:23 -08:00
|
|
|
# get all usernames for domain
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="$dbquery WHERE domain='$domain'"
|
2021-02-10 16:16:23 -08:00
|
|
|
# elif [ -z "$mbox" ] && [ -z "$domain" ]; then
|
|
|
|
# echo "ERROR: No username or domain specified."
|
|
|
|
# help
|
|
|
|
# uncomment above 3 lines to force search term, otherwise all email addresses for all domains will be returned
|
|
|
|
fi
|
|
|
|
|
2022-11-29 16:33:15 -08:00
|
|
|
echo $dbquery
|
2021-02-10 16:16:23 -08:00
|
|
|
# set order by
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="$dbquery ORDER BY domain, mbox;";
|
2021-02-10 16:16:23 -08:00
|
|
|
|
|
|
|
# execute mysql query
|
|
|
|
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs
|