reworked getopts

This commit is contained in:
Matthew Saunders Brown 2021-10-15 15:17:29 -07:00
parent 7f58daac77
commit 4bf85f8839
17 changed files with 452 additions and 758 deletions

View File

@ -13,61 +13,32 @@ help()
echo "$thisfilename"
echo "Add email alias to vmail system"
echo ""
echo "usage: $thisfilename email alias [OPTIONS]"
echo "usage: $thisfilename -e <email> -a <alias> [-h]"
echo ""
echo " -h Print this help."
echo " -e <email> Existing email address that will receive alias emails."
echo " -a <alias> Alias that will direct to existing email address."
echo ""
echo " Aliases are for delivering an email to another address in the same domain."
echo " Use Forwarding for directing emails to an address in another domain."
echo " 'email' should be the full address 'alias' just the local part (username)."
echo " .e.g to have info@example.com delivered (aliased) to joe@example.com do:"
echo " $thisfilename joe@example.com info"
exit
echo " $thisfilename -e joe@example.com -a info"
}
# check for and set alias & email address & split in to local part & domain
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then
help
else
email=$1
shift
alias=$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
fi
else
help
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email address is required"
exit
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;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# check for alias
if [[ -z $alias ]]; then
echo "alias is required"
exit
fi
# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
@ -84,19 +55,14 @@ if [ -z $mbox_id ] ; then
exit 1
elif [ "$mbox_id" -gt '1' ] ; then
# verified mbox, check for existing alias
existing_alias=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT vm_mboxes.mbox FROM vm_mboxes, vm_aliases WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id=vm_mboxes.id AND vm_mboxes.domain_id='$domain_id';"`
existing_alias=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT vm_aliases.id FROM vm_aliases WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id='$mbox_id';"`
if [ -z $existing_alias ]; then
# existing alias does not exist, add new one now
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_aliases SET vm_aliases.alias=\"$alias\", vm_aliases.mbox_id=\"$mbox_id\";'"
eval $dbcmd
else
if [ "$existing_alias" == "$mbox" ]; then
echo "ERROR: Alias for $alias To $email already exists."
exit 1
else
echo "ERROR: Alias for $alias already points to another address ($existing_alias@$domain). To update delete that alias first, then add a new one."
exit 1
fi
fi
else
# db query error

View File

@ -13,69 +13,68 @@ help()
echo "$thisfilename"
echo "Delete email alias from vmail database."
echo ""
echo "usage: $thisfilename alias"
echo "usage: $thisfilename -d <domain>|-e <email> -a <alias>"
echo ""
echo " -h Print this help."
echo " -a <alias> Alias to be deleted. Local part only."
echo " -d <domain> Domain of alias to delete."
echo " -e <email> Email that alias directs to to delete."
echo ""
echo " Enter alias in full email address format."
exit
echo " Enter <domain> OR <email>. If <email> is specified"
echo " then just the alias to that address is deleted. If"
echo " domain is specific then all <alias>@<domain> aliases"
echo " are deleted (aliases can direct to multiple emails)."
}
# 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
alias=${searchterm%@*}
domain=${searchterm##*@}
if ! vmail::validate_domain $domain; then
echo "ERROR: $domain is not a valid domain name."
exit 1
fi
else
echo "ERROR: $searchterm is not a valid email alias."
help
fi
fi
vmail:getoptions "$@"
# check for alias
if [[ -z $alias ]]; then
echo "alias is required"
exit
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;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# build query
# start building 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_aliases.id FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id=vm_mboxes.id AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain'"
vm_aliases_id=`$dbcmd $dbcmdopts "$dbquery"`
if [ -z "$vm_aliases_id" ]; then
echo "ERROR: Alias $searchterm does not exist."
# if email set delete single alias
if [[ -n $email ]]; then
# get aliases id which also works to confirm alias exists
dbquery="SELECT vm_aliases.id FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id=vm_mboxes.id AND vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain'"
vm_aliases_id=`$dbcmd $dbcmdopts "$dbquery"`
if [[ -z $vm_aliases_id ]]; then
echo "ERROR: Alias $alias@$domain -> $email does not exist."
exit 1
elif [ "$vm_aliases_id" -gt '0' ]; then
elif [ "$vm_aliases_id" -gt '0' ]; then
dbquery="DELETE FROM vm_aliases WHERE vm_aliases.id='$vm_aliases_id'"
echo $dbquery
eval $dbcmd $dbcmdopts "\"$dbquery\""
echo "SUCCESS: Alias $searchterm removed from system."
echo "SUCCESS: Alias $alias@$domain -> $email removed from system."
exit 0
else
else
echo "ERROR: System error querying vmail database"
exit 1
fi
# if domain set then delete any and all aliases
elif [[ -n $domain ]]; then
# get alias id array
dbquery="SELECT vm_aliases.id FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.alias='$alias' AND vm_aliases.mbox_id=vm_mboxes.id AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain'"
mapfile -t vm_alias_ids < <( $dbcmd $dbcmdopts "$dbquery" )
if [[ -z $vm_alias_ids ]]; then
echo "ERROR: Alias $alias@$domain does not exist."
exit 1
elif [[ ${#vm_alias_ids[@]} -gt '0' ]]; then
for vm_alias_id in "${vm_alias_ids[@]}"; do
dbquery="DELETE FROM vm_aliases WHERE vm_aliases.id='$vm_alias_id'"
eval $dbcmd $dbcmdopts "\"$dbquery\""
done
echo "SUCCESS: Alias(s) for $alias@$domain removed from system."
else
echo "ERROR: Unknown error. vm_alias_count=0?"
exit 1
fi
else
echo "You must specify either <domain> or <email>."
exit 1
fi

View File

@ -13,12 +13,14 @@ help()
echo "$thisfilename"
echo "Get email alias data from vmail database."
echo ""
echo "usage: $thisfilename [email|alias|domain] [OPTIONS]"
echo "usage: $thisfilename [-a <alias>|-e <email>|-d <domain>] [-c] [-t] [-h]"
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 " -a Return info for specific alias."
echo " -e Return all aliases for specified email address."
echo " -d Return all aliases for specified domain."
echo " -c Output in cvs format."
echo " -t 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."
@ -27,69 +29,31 @@ help()
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
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
vmail:getoptions "$@"
# set initial db query data
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"
# 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 )
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
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'"
if [[ -n $alias ]]; then
if [[ -n $domain ]] ; then
dbquery="$dbquery AND vm_aliases.alias='$alias' AND vm_domains.domain='$domain'"
else
# search for all aliases for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$localpart'"
fi
echo "ERROR: alias should be in full email address format."
exit 1
fi
elif [[ -n $email ]]; then
dbquery="$dbquery AND vm_aliases.alias='$mbox' AND vm_domains.domain='$domain'"
elif [[ -n $domain ]]; then
dbquery="$dbquery AND vm_domains.domain='$domain'"
else
echo "You must specify either <alias> or <email> or <domain>."
exit 1
fi
# set order by

View File

@ -7,30 +7,49 @@
# load include file
source $(dirname $0)/vmail.sh
# check for and set virtualhost
if [ -n "$1" ]; then
virtualhost=$1
else
echo "virtualhost not set"
exit 1
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get email alias data from vmail database."
echo ""
echo "usage: $thisfilename -d <domain> [-h]"
echo ""
echo " -h Print this help."
echo " -d Domain name to add DKIM to."
echo ""
echo " This will create a DKIM key that exim will start using immediately for"
echo " all outgoing messages for the specified domain. A DNS entry needs to"
echo " be created for the domain so that DKIM validation works. The DNS entry"
echo " to add is output on the command line and is stored in the file:"
echo " /etc/ssl/dkim/<domain>.dns"
exit
}
vmail:getoptions "$@"
# check for domain
if [[ -z $domain ]]; then
echo "domain name is required"
exit
fi
# check for existing dkim
if [ -f /etc/ssl/dkim/$virtualhost.dkim ]; then
echo "dkim for $virtualhost already exists"
if [ -f /etc/ssl/dkim/$domain.dkim ]; then
echo "dkim for $domain already exists"
exit 1
fi
cd /etc/ssl/dkim
date +%Y%m%d > $virtualhost.selector
openssl genrsa -out $virtualhost.pem 2048
openssl rsa -in $virtualhost.pem -out $virtualhost.pub -pubout
tail -n +2 $virtualhost.pub |head -n -1|tr -d '\n' > $virtualhost.dkim
echo `cat $virtualhost.selector`._domainkey.$virtualhost. 3600 IN TXT \""k=rsa; p=`cat $virtualhost.dkim`"\" > $virtualhost.dns
chown Debian-exim:ssl-cert $virtualhost.*
date +%Y%m%d > $domain.selector
openssl genrsa -out $domain.pem 2048
openssl rsa -in $domain.pem -out $domain.pub -pubout
tail -n +2 $domain.pub |head -n -1|tr -d '\n' > $domain.dkim
echo `cat $domain.selector`._domainkey.$domain. 3600 IN TXT \""k=rsa; p=`cat $domain.dkim`"\" > $domain.dns
chown Debian-exim:ssl-cert $domain.*
echo
echo create this dns record:
echo
cat $virtualhost.dns
cat $domain.dns
echo

View File

@ -7,23 +7,42 @@
# load include file
source $(dirname $0)/vmail.sh
# check for and set virtualhost
if [ -n "$1" ]; then
virtualhost=$1
else
echo "virtualhost not set"
exit 1
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get email alias data from vmail database."
echo ""
echo "usage: $thisfilename -d <domain> [-h]"
echo ""
echo " -h Print this help."
echo " -d Domain name to remove DKIM key from."
echo ""
echo " DKIM key for the specified domain is removed from the server and"
echo " outgoing emails will no longer be signed with DKIM. You should also"
echo " remove the associated DNS entry, however leaving the DNS entry in"
echo " place should not cause any immediate problems."
exit
}
vmail:getoptions "$@"
# check for domain
if [[ -z $domain ]]; then
echo "domain name is required"
exit
fi
# check for existing dkim
if [ ! -f /etc/ssl/dkim/$virtualhost.dkim ]; then
echo "dkim for $virtualhost does not exist"
if [ ! -f /etc/ssl/dkim/$domain.dkim ]; then
echo "dkim for $domain does not exist"
exit 1
fi
echo delete this dkim dns record
echo
cat /etc/ssl/dkim/$virtualhost.dns
cat /etc/ssl/dkim/$domain.dns
echo
rm /etc/ssl/dkim/$virtualhost.*
rm /etc/ssl/dkim/$domain.*

View File

@ -13,56 +13,26 @@ help()
echo "$thisfilename"
echo "Add domain to vmail system"
echo ""
echo "usage: $thisfilename domain [OPTIONS]"
echo "usage: $thisfilename -d <domain> [-l <limit>] [-q <quota>] [-s <status>]"
echo ""
echo " -h Print this help."
echo " -l LIMIT Maximum number of mailboxes for this domain."
echo " -q QUOTA Default mailbox quota in GB."
echo " -s STATUS 1 for enabled, 0 for disabled."
echo " -d Domain to add to vmail system."
echo " -l <limit> Maximum number of mailboxes for this domain."
echo " -q <quota> Default mailbox quota in GB."
echo " -s <status> 1 for enabled, 0 for disabled."
echo ""
echo " Defaults for are all OPTIONS are configured in database."
echo " Defaults for all Options are configured in database."
echo " NULL for LIMIT or QUOTA means no limit."
exit
}
# check for and set domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif vmail::validate_domain $1; then
domain=$1
shift
else
echo "ERROR: Invalid domain name: $1"
exit 1
fi
else
help
fi
vmail:getoptions "$@"
# set any options that were passed
while getopts "hl:q:s:" opt; do
case "${opt}" in
h )
help
exit;;
l )
limit=${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
# check for domain
if [[ -z $domain ]]; then
echo "domain name is required"
exit
fi
# set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"

View File

@ -13,41 +13,20 @@ help()
echo "$thisfilename"
echo "Delete domain and all associated email accounts from the vmail system."
echo ""
echo "usage: $thisfilename domain [OPTIONS]"
echo "usage: $thisfilename -d <domain> [-h]"
echo ""
echo " -h Print this help."
echo " -d Domain to be removed from the vmail system."
exit
}
# check for and set domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif vmail::validate_domain $1; then
domain=$1
shift
else
echo "ERROR: Invalid domain name: $1"
exit 1
fi
else
help
fi
vmail:getoptions "$@"
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
;;
esac
done
# check for domain
if [[ -z $domain ]]; then
echo "domain name is required"
exit
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
@ -59,7 +38,7 @@ rowcount=`$dbcmd $dbcmdopts "$dbquery"`
if [ "$rowcount" -eq '1' ] ; then
# domain exists, delete from all vmail tables
# singled delete should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
# single delete should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
dbquery="DELETE FROM vm_domains WHERE domain='$domain';"
eval $dbcmd $dbcmdopts "\"$dbquery\""
dbquery="DELETE FROM sa_userpref WHERE username LIKE '%@$domain';"

View File

@ -13,68 +13,35 @@ help()
echo "$thisfilename"
echo "Get domain data from vmail database."
echo ""
echo "usage: $thisfilename [domain] [OPTIONS]"
echo "usage: $thisfilename [-d <domain>|-g <glob>] [OPTIONS]"
echo ""
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 " -w Wildcard search when searching for specific domain."
echo " -d Domain to be queried."
echo " -g Glob - Wildcard search instead of specific domain search."
echo " -c Output in cvs format."
echo " -t Use tabs instead of tables for output, do not display column headers."
echo ""
echo " Domain is optional. If not specified all domains will be queried."
echo " By default domain search is for an exact matchs."
echo " Specify -w to turn them in to wildcard search. e.g:"
echo " $thisfilename stack -w # search for all domains that contain 'stack'."
echo " Domain/Glob is optional. If nothing specified all domains will be queried."
echo " Domain search is for an exact match."
echo " Use -g instead of -d for glog (wildcard) search. e.g:"
echo " $thisfilename -g stack # search for all domains that contain 'stack'."
exit
}
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
fi
dbquery="SELECT * from vm_domains"
# check for and set domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif [[ ! $1 =~ ^- ]] ; then
domain=$1
shift
# query="SELECT * from vm_domains WHERE domain LIKE '%$domain%';"
fi
fi
# set any options that were passed
while getopts "chsw" opt; do
case "${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
w )
wildcardsearch=true
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
if [ -n "$domain" ]; then
if [ -n "$wildcardsearch" ]; then
dbquery="$dbquery WHERE domain LIKE '%$domain%'"
else
if vmail::validate_domain $domain; then
if [[ -n $domain ]]; then
dbquery="$dbquery WHERE domain='$domain'"
else
echo "ERROR: Invalid domain name: $domain"
exit 1
fi
fi
elif [[ -n $glob ]]; then
dbquery="$dbquery WHERE domain LIKE '%$domain%'"
fi
# execute mysql query

View File

@ -12,66 +12,35 @@ help()
thisfilename=$(basename -- "$0")
echo "Modify an existing domain."
echo ""
echo "usage: $thisfilename domain [OPTIONS]"
echo "usage: $thisfilename -d domain [OPTIONS]"
echo ""
echo " -h Print this help."
echo " -d Domain to be modified."
echo " -l LIMIT Mailbox limit - the maximum number of mailboxes under this domain."
echo " -q QUOTA Mailbox Quota Default - default quota for new mailboxes under this domain."
echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1."
exit
}
# check for and verify domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
else
domain=$1
shift
if vmail::validate_domain $domain; then
vmail:getoptions "$@"
# check for domain
if [[ -n $domain ]]; then
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE vm_domains.domain='$domain';"`
if [ -z $domain_id ] ; then
echo "ERROR: Email domain $domain does not exist."
exit
fi
else
echo "ERROR: $domain is not a valid domain name."
exit
fi
fi
else
help
echo "domain name is required"
exit
fi
# set any options that were passed
while getopts "hl:q:s:" opt; do
case "${opt}" in
h )
help
exit;;
l )
limit=${OPTARG}
;;
q )
quota=${OPTARG}
;;
s )
status=${OPTARG}
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
;;
esac
done
# set update portion of command to empty string
dbset=""
# check for quota update
if [ -n "$quota" ]; then
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
@ -83,7 +52,7 @@ if [ -n "$quota" ]; then
fi
# check for limit update
if [ ! -z "$limit" ]; then
if [[ -n $limit ]]; then
# make limit uppercase in case it is set to NULL
limit=`echo $limit | tr [:lower:] [:upper:]`
if [[ "$limit" =~ ^[0-9]+$ ]] || [[ "$limit" == "NULL" ]]; then
@ -98,7 +67,7 @@ if [ ! -z "$limit" ]; then
fi
# check for status update
if [ ! -z "$status" ]; then
if [[ -n $status ]]; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
if [ ! -z "$dbset" ]; then
dbset="$dbset,"

View File

@ -13,87 +13,34 @@ help()
echo "$thisfilename"
echo "Add email forward to vmail system"
echo ""
echo "usage: $thisfilename email forward_to [OPTIONS]"
echo "usage: $thisfilename -e <email> -f <forward> [-k <0|1>] [-h]"
echo ""
echo " -h Print this help."
echo " -l Save Local (forward & keep local copy). 0 = no/off, 1 = yes/on. Default is 0."
echo " -e <email> Email address to forward."
echo " -f <forward> Email address to forward to."
echo " -k <0|1> 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"
echo " Forwarding is for delivering an email to another domain. Use aliases"
echo " for directing emails to another address in the same domain."
echo " 'email' and 'forward' should both be in full email address format."
echo " .e.g to have info@example.org delivered (forwarded) to joe@example.com do:"
echo " $thisfilename info@example.org 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
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit
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
# check for forward
if [[ -z $forward ]]; then
echo "forward is required"
exit
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
@ -113,9 +60,9 @@ elif [ "$mbox_id" -gt '0' ]; then
vm_forwards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $vm_forwards_id ]; then
# existing forward does not exist, add it now
dbquery="INSERT INTO vm_forwards SET mbox_id='$mbox_id', forward_to='$forward_to'"
if [ ! -z $save_local ]; then
dbquery="$dbquery, save_local='$save_local'"
dbquery="INSERT INTO vm_forwards SET mbox_id='$mbox_id', forward_to='$forward'"
if [ ! -z $keep ]; then
dbquery="$dbquery, save_local='$keep'"
fi
eval $dbcmd $dbcmdopts \"$dbquery\;\"
else

View File

@ -13,46 +13,20 @@ help()
echo "$thisfilename"
echo "Delete email forward from vmail database."
echo ""
echo "usage: $thisfilename email"
echo "usage: $thisfilename -e email"
echo ""
echo " -h Print this help."
echo ""
echo " Enter email in full email address format to delete forwarding."
echo " -e <email> Email address to remove forwarding from."
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
vmail:getoptions "$@"
# 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
# check for email
if [[ -z $email ]]; then
echo "email name is required"
exit
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"

View File

@ -13,12 +13,14 @@ help()
echo "$thisfilename"
echo "Get email forwards data from vmail database."
echo ""
echo "usage: $thisfilename [email|forward|domain] [OPTIONS]"
echo "usage: $thisfilename [-e email|-f forward|-f 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 " -e <email> Email address to remove forwarding from."
echo " -f <forward> Return info for specific forward."
echo " -d <domain> Domain to be modified."
echo " -c Output in cvs format."
echo " -t 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."
@ -27,69 +29,24 @@ help()
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
vmail:getoptions "$@"
# 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
# AND vm_forwards.forward_to='$mbox@$domain'"
# 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
if [[ -n $forward ]]; 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
dbquery="$dbquery AND vm_forwards.forward_to='$mbox@$domain'"
elif [[ -n $domain ]]; then
# add specific domain
dbquery="$dbquery vm_domains.domain='$domain'"
if [[ -n "$mbox" ]]; then
# search for forward for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
fi
fi
fi
# set order by

View File

@ -13,65 +13,28 @@ help()
echo "$thisfilename"
echo "Add email account to vmail system"
echo ""
echo "usage: $thisfilename email password [OPTIONS]"
echo "usage: $thisfilename -e <email> -p <password> [-q <quota>] [-s <0|1>] [-h]"
echo ""
echo " -h Print this help."
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
echo " -e <email> Email address to add."
echo " -p <password> Unencrypted Password for new email address."
echo " -q <quota> Set mailbox quota in GB, otherwise default for this domain is used. NULL means no limit."
echo " -s <0|1> 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1."
}
# check for and set email address, local part & domain, password
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then
help
else
email=$1
shift
passwd=$1
shift
if [[ $email =~ "@" ]] ; then
mbox=${email%@*}
domain=${email##*@}
if [ -z $mbox ] ; then
echo "ERROR: No local part in $email."
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email is required"
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 "hq:s:" opt; do
case "${opt}" in
h )
help
exit;;
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
# check for password
if [[ -z $password ]]; then
echo "password is required"
exit
fi
# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
@ -85,15 +48,10 @@ rowcount=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "S
if [ "$rowcount" -eq '0' ] ; then
# mbox does not exist, build SQL
# first encrypt password
passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$passwd"|sed 's|^{SHA512-CRYPT}||'`
passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$password"|sed 's|^{SHA512-CRYPT}||'`
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_mboxes SET domain_id=\"$domain_id\", mbox=\"$mbox\", passwd=\"$passwd\""
if [ -n "$status" ] ; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
if [[ -n $status ]] ; then
dbcmd="$dbcmd, status=\"$status\""
else
echo "ERROR: status (-s) must be 1 or 0"
exit
fi
fi
if [ -z "$quota" ] ; then
# get mbox_quota_default from domains table

View File

@ -13,50 +13,20 @@ help()
echo "$thisfilename"
echo "Delete email account."
echo ""
echo "usage: $thisfilename fullemail"
echo "usage: $thisfilename -e <email> [-x]"
echo ""
echo " -f Force - don't prompt for confirmation."
echo " -h Print this help."
echo ""
echo " Enter account to delete in full email address format."
exit
echo " -e <email> Email address to delete."
echo " -x Execute (force) - don't prompt for confirmation."
}
# check for and set email address info
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 address."
help
fi
fi
fi
vmail:getoptions "$@"
# set any options that were passed
while getopts "fh" opt; do
case "${opt}" in
f )
force=true
;;
h )
help
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
@ -70,7 +40,7 @@ if [ -z "$mboxes_id" ]; then
echo "ERROR: Email address $email does not exist."
exit 1
elif [ "$mboxes_id" -gt '0' ]; then
if [ -n "$force" ] || vmail::yesno "Delete $email now?"; then
if [[ -n $execute ]] || vmail::yesno "Delete $email now?"; then
# this should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
dbquery="DELETE FROM vm_mboxes WHERE vm_mboxes.id='$mboxes_id';"
eval $dbcmd $dbcmdopts "\"$dbquery\""

View File

@ -13,69 +13,32 @@ help()
echo "$thisfilename"
echo "Get email account data from vmail database."
echo ""
echo "usage: $thisfilename [username|domain|fullemail] [OPTIONS]"
echo "usage: $thisfilename [-m <mbox>|-d <domain>|-e <email>] [OPTIONS]"
echo ""
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 " -p Include encrypt password in output."
echo " -w Wildcard search when searching for username."
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."
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."
echo " Specify -w to turn them in to a wildcard search. Examples:"
echo " $thisfilename joe@example.com # search for specific email address 'joe@example.com'."
echo " $thisfilename joe # search for username 'joe' in all domains."
echo " $thisfilename joe -w # search for usernames containing 'joe' in all domains."
echo " $thisfilename example.com # search for all usernames in the 'example.com' domain."
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."
exit
}
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
# check for and set email 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##*@}
elif vmail::validate_domain $searchterm; then
domain=$searchterm
else
mbox=$searchterm
fi
fi
fi
# set any options that were passed
while getopts "chspw" opt; do
case "${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
p )
includepassword=true
;;
w )
wildcardsearch=true
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain"
if [ -n "$includepassword" ]; then
@ -87,7 +50,7 @@ if [ -n "$mbox" ] && [ -n "$domain" ]; then
dbquery="$dbquery AND vm_domains.domain='$domain' AND vm_mboxes.mbox='$mbox'"
elif [ -n "$mbox" ] && [ -z "$domain" ]; then
# search all domains for username
if [ -n "$wildcardsearch" ]; then
if [ -n "$glob" ]; then
# wildcard search
dbquery="$dbquery AND vm_mboxes.mbox LIKE '%$mbox%'"
else

View File

@ -13,67 +13,23 @@ help()
echo "$thisfilename"
echo "Modify an existing email."
echo ""
echo "usage: $thisfilename email [OPTIONS]"
echo "usage: $thisfilename -e <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."
echo " -e <email> Email account to modify."
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 <0|1> 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
vmail:getoptions "$@"
# 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
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit
fi
# 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;"`
@ -99,7 +55,7 @@ if [ -n "$quota" ]; then
fi
# check for password update
if [ ! -z "$passwd" ]; then
if [ ! -z "$password" ]; then
passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$passwd"|sed 's|^{SHA512-CRYPT}||'`
if [ ! -z "$dbset" ]; then
dbset="$dbset,"
@ -109,15 +65,10 @@ 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

View File

@ -26,7 +26,7 @@ fi
# crude but good enough domain name format validation
function vmail::validate_domain () {
local my_domain=$1
if [[ $my_domain =~ ^(([a-zA-Z](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
if [[ $my_domain =~ ^(([a-zA-Z0-9](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
return 0
else
return 1
@ -72,3 +72,125 @@ function vmail::yesno() {
done
}
function vmail:getoptions () {
local OPTIND
while getopts "ha:b:d:e:f:gcp:q:s:tkf:gl:vx" opt ; do
case "${opt}" in
h ) # display help and exit
help
exit
;;
a ) # alias
alias=${OPTARG,,}
if [[ $alias =~ "@" ]] ; then
domain=${alias##*@}
if vmail::validate_domain $domain; then
alias=${alias%@*}
else
echo "ERROR: $domain is not a valid domain name."
exit
fi
fi
;;
b ) # mbox - local part of email address
mbox=${OPTARG,,}
;;
c ) # cvs - output in cvs format
cvs="| sed 's/\t/,/g'"
;;
d ) # domain name (virtualhost) to act on
domain=${OPTARG,,}
if ! vmail::validate_domain $domain; then
echo "ERROR: $domain is not a valid domain name."
exit
fi
;;
e ) # email address
email=${OPTARG,,}
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
;;
f ) # forward to email address
forward=${OPTARG,,}
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
;;
g ) # glob (wildcard) search
glob=${OPTARG,,}
;;
k ) # keep
if [[ $keep != "0" ]] && [[ $keep != "1" ]]; then
echo "ERROR: Invalid save keep setting: -k $keep."
exit 1
fi
;;
l ) # limit
limit=${OPTARG}
;;
p ) # password
password=${OPTARG}
;;
q ) # quota
quota=${OPTARG}
;;
s ) # status - 0 or 1
status=${OPTARG}
;;
t ) # tab - Use tabs instead of tables for output, do not display column headers
tab=true
;;
n ) # dry-run
dryrun=true
;;
v ) # verbose
verbose=true
;;
w ) # write - store data in file
write=true
;;
x ) # eXecute - don't prompt for confirmation
execute=true
;;
\? )
echo "Invalid option: $OPTARG"
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument"
exit 1
;;
esac
done
shift $((OPTIND-1))
}