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
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."
echo " -g Wildcard (blog) search when searching for username."
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."
echo " By default username searches are for exact matchs."
2021-10-15 15:17:29 -07:00
echo " Specify -g to turn them in to a wildcard (glob) search. Examples:"
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 -m joe -g # search for usernames containing '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
2021-09-04 15:42:13 -07:00
dbquery = "SELECT vm_mboxes.mbox, vm_domains.domain"
2021-11-11 07:58:04 -08:00
if [ -n " $verbose " ] ; then
2021-09-04 15:42:13 -07:00
dbquery = " $dbquery , vm_mboxes.passwd "
fi
2021-12-30 16:50:23 -08:00
dbquery = " $dbquery , vm_mboxes.status, vm_mboxes.quota, vm_mboxes.ratelimit, vm_mboxes.filter FROM vm_domains, vm_mboxes WHERE vm_domains.id=vm_mboxes.domain_id "
2021-02-10 16:16:23 -08:00
if [ -n " $mbox " ] && [ -n " $domain " ] ; then
# search for specific email address
dbquery = " $dbquery AND vm_domains.domain=' $domain ' AND vm_mboxes.mbox=' $mbox ' "
elif [ -n " $mbox " ] && [ -z " $domain " ] ; then
# search all domains for username
2021-10-15 15:17:29 -07:00
if [ -n " $glob " ] ; then
2021-02-10 16:16:23 -08:00
# wildcard search
dbquery = " $dbquery AND vm_mboxes.mbox LIKE '% $mbox %' "
else
# exact match
dbquery = " $dbquery AND vm_mboxes.mbox=' $mbox ' "
fi
elif [ -z " $mbox " ] && [ -n " $domain " ] ; then
# get all usernames for domain
dbquery = " $dbquery AND vm_domains.domain=' $domain ' "
# 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
# set order by
dbquery = " $dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox; " ;
# execute mysql query
eval $dbcmd $dbcmdopts " \" $dbquery \" " $cvs