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 "$thisfilename"
echo "Add email alias to vmail system" echo "Add email alias to vmail system"
echo "" echo ""
echo "usage: $thisfilename email alias [OPTIONS]" echo "usage: $thisfilename -e <email> -a <alias> [-h]"
echo "" echo ""
echo " -h Print this help." 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 ""
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 " '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 " .e.g to have info@example.com delivered (aliased) to joe@example.com do:"
echo " $thisfilename joe@example.com info" echo " $thisfilename -e joe@example.com -a info"
exit
} }
# check for and set alias & email address & split in to local part & domain vmail:getoptions "$@"
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then # check for email
help if [[ -z $email ]]; then
else echo "email address is required"
email=$1 exit
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
fi fi
# set any options that were passed # check for alias
while getopts "h" opt; do if [[ -z $alias ]]; then
case "${opt}" in echo "alias is required"
h ) exit
help fi
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# get domain_id (and thus check if domain already exists) # 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';"` 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 exit 1
elif [ "$mbox_id" -gt '1' ] ; then elif [ "$mbox_id" -gt '1' ] ; then
# verified mbox, check for existing alias # 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 if [ -z $existing_alias ]; then
# existing alias does not exist, add new one now # 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\";'" 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 eval $dbcmd
else else
if [ "$existing_alias" == "$mbox" ]; then
echo "ERROR: Alias for $alias To $email already exists." echo "ERROR: Alias for $alias To $email already exists."
exit 1 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 fi
else else
# db query error # db query error

View File

@ -13,69 +13,68 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Delete email alias from vmail database." echo "Delete email alias from vmail database."
echo "" echo ""
echo "usage: $thisfilename alias" echo "usage: $thisfilename -d <domain>|-e <email> -a <alias>"
echo "" echo ""
echo " -h Print this help." 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 ""
echo " Enter alias in full email address format." echo " Enter <domain> OR <email>. If <email> is specified"
exit 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) vmail:getoptions "$@"
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then # check for alias
help if [[ -z $alias ]]; then
elif [[ ! $1 =~ ^- ]] ; then echo "alias is required"
searchterm=$1 exit
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
fi fi
# set any options that were passed # start building query
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
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e" dbcmdopts="-s -r -N -e"
# get aliases id which also works to confirm alias exists # if email set delete single alias
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'" if [[ -n $email ]]; then
vm_aliases_id=`$dbcmd $dbcmdopts "$dbquery"` # 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'"
if [ -z "$vm_aliases_id" ]; then vm_aliases_id=`$dbcmd $dbcmdopts "$dbquery"`
echo "ERROR: Alias $searchterm does not exist." if [[ -z $vm_aliases_id ]]; then
echo "ERROR: Alias $alias@$domain -> $email does not exist."
exit 1 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'" dbquery="DELETE FROM vm_aliases WHERE vm_aliases.id='$vm_aliases_id'"
echo $dbquery
eval $dbcmd $dbcmdopts "\"$dbquery\"" eval $dbcmd $dbcmdopts "\"$dbquery\""
echo "SUCCESS: Alias $searchterm removed from system." echo "SUCCESS: Alias $alias@$domain -> $email removed from system."
exit 0 exit 0
else else
echo "ERROR: System error querying vmail database" echo "ERROR: System error querying vmail database"
exit 1 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 fi

View File

@ -13,12 +13,14 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Get email alias data from vmail database." echo "Get email alias data from vmail database."
echo "" echo ""
echo "usage: $thisfilename [email|alias|domain] [OPTIONS]" echo "usage: $thisfilename [-a <alias>|-e <email>|-d <domain>] [-c] [-t] [-h]"
echo "" echo ""
echo " -a Return info for specific alias."
echo " -c Output in cvs format."
echo " -h Print this help." 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 ""
echo " Search term is optional. If nothing specified all aliases for all email acccounts for all domains will be returned." 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 email address to get all aliases that route to that email address."
@ -27,69 +29,31 @@ help()
exit exit
} }
# check for and set alias search term(s) vmail:getoptions "$@"
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
# set initial db query data # set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e" 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" 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 if [[ -n $tab ]]; then
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" dbcmdopts="-s -N $dbcmdopts"
;; fi
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query if [[ -n $alias ]]; then
if [ -n "$domain" ]; then if [[ -n $domain ]] ; then
# add specific domain dbquery="$dbquery AND vm_aliases.alias='$alias' AND vm_domains.domain='$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 else
# search for all aliases for specific email address echo "ERROR: alias should be in full email address format."
dbquery="$dbquery AND vm_mboxes.mbox='$localpart'" exit 1
fi
fi 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 fi
# set order by # set order by

View File

@ -7,30 +7,49 @@
# load include file # load include file
source $(dirname $0)/vmail.sh source $(dirname $0)/vmail.sh
# check for and set virtualhost help()
if [ -n "$1" ]; then {
virtualhost=$1 thisfilename=$(basename -- "$0")
else echo "$thisfilename"
echo "virtualhost not set" echo "Get email alias data from vmail database."
exit 1 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 fi
# check for existing dkim # check for existing dkim
if [ -f /etc/ssl/dkim/$virtualhost.dkim ]; then if [ -f /etc/ssl/dkim/$domain.dkim ]; then
echo "dkim for $virtualhost already exists" echo "dkim for $domain already exists"
exit 1 exit 1
fi fi
cd /etc/ssl/dkim cd /etc/ssl/dkim
date +%Y%m%d > $virtualhost.selector date +%Y%m%d > $domain.selector
openssl genrsa -out $virtualhost.pem 2048 openssl genrsa -out $domain.pem 2048
openssl rsa -in $virtualhost.pem -out $virtualhost.pub -pubout openssl rsa -in $domain.pem -out $domain.pub -pubout
tail -n +2 $virtualhost.pub |head -n -1|tr -d '\n' > $virtualhost.dkim tail -n +2 $domain.pub |head -n -1|tr -d '\n' > $domain.dkim
echo `cat $virtualhost.selector`._domainkey.$virtualhost. 3600 IN TXT \""k=rsa; p=`cat $virtualhost.dkim`"\" > $virtualhost.dns echo `cat $domain.selector`._domainkey.$domain. 3600 IN TXT \""k=rsa; p=`cat $domain.dkim`"\" > $domain.dns
chown Debian-exim:ssl-cert $virtualhost.* chown Debian-exim:ssl-cert $domain.*
echo echo
echo create this dns record: echo create this dns record:
echo echo
cat $virtualhost.dns cat $domain.dns
echo echo

View File

@ -7,23 +7,42 @@
# load include file # load include file
source $(dirname $0)/vmail.sh source $(dirname $0)/vmail.sh
# check for and set virtualhost
if [ -n "$1" ]; then help()
virtualhost=$1 {
else thisfilename=$(basename -- "$0")
echo "virtualhost not set" echo "$thisfilename"
exit 1 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 fi
# check for existing dkim # check for existing dkim
if [ ! -f /etc/ssl/dkim/$virtualhost.dkim ]; then if [ ! -f /etc/ssl/dkim/$domain.dkim ]; then
echo "dkim for $virtualhost does not exist" echo "dkim for $domain does not exist"
exit 1 exit 1
fi fi
echo delete this dkim dns record echo delete this dkim dns record
echo echo
cat /etc/ssl/dkim/$virtualhost.dns cat /etc/ssl/dkim/$domain.dns
echo echo
rm /etc/ssl/dkim/$virtualhost.* rm /etc/ssl/dkim/$domain.*

View File

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

View File

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

View File

@ -13,68 +13,35 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Get domain data from vmail database." echo "Get domain data from vmail database."
echo "" echo ""
echo "usage: $thisfilename [domain] [OPTIONS]" echo "usage: $thisfilename [-d <domain>|-g <glob>] [OPTIONS]"
echo "" echo ""
echo " -c Output in cvs format."
echo " -h Print this help." echo " -h Print this help."
echo " -s Be more silent - use tabs instead of tables for output, do not display column headers." echo " -d Domain to be queried."
echo " -w Wildcard search when searching for specific domain." 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 ""
echo " Domain is optional. If not specified all domains will be queried." echo " Domain/Glob is optional. If nothing specified all domains will be queried."
echo " By default domain search is for an exact matchs." echo " Domain search is for an exact match."
echo " Specify -w to turn them in to wildcard search. e.g:" echo " Use -g instead of -d for glog (wildcard) search. e.g:"
echo " $thisfilename stack -w # search for all domains that contain 'stack'." echo " $thisfilename -g stack # search for all domains that contain 'stack'."
exit exit
} }
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e" dbcmdopts="-e"
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
fi
dbquery="SELECT * from vm_domains" 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 # build query
if [ -n "$domain" ]; then if [[ -n $domain ]]; then
if [ -n "$wildcardsearch" ]; then
dbquery="$dbquery WHERE domain LIKE '%$domain%'"
else
if vmail::validate_domain $domain; then
dbquery="$dbquery WHERE domain='$domain'" dbquery="$dbquery WHERE domain='$domain'"
else elif [[ -n $glob ]]; then
echo "ERROR: Invalid domain name: $domain" dbquery="$dbquery WHERE domain LIKE '%$domain%'"
exit 1
fi
fi
fi fi
# execute mysql query # execute mysql query

View File

@ -12,66 +12,35 @@ help()
thisfilename=$(basename -- "$0") thisfilename=$(basename -- "$0")
echo "Modify an existing domain." echo "Modify an existing domain."
echo "" echo ""
echo "usage: $thisfilename domain [OPTIONS]" echo "usage: $thisfilename -d domain [OPTIONS]"
echo "" echo ""
echo " -h Print this help." 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 " -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 " -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." echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1."
exit exit
} }
# check for and verify domain vmail:getoptions "$@"
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then # check for domain
help if [[ -n $domain ]]; then
else
domain=$1
shift
if vmail::validate_domain $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';"` 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 if [ -z $domain_id ] ; then
echo "ERROR: Email domain $domain does not exist." echo "ERROR: Email domain $domain does not exist."
exit exit
fi fi
else
echo "ERROR: $domain is not a valid domain name."
exit
fi
fi
else else
help echo "domain name is required"
exit
fi 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 # set update portion of command to empty string
dbset="" dbset=""
# check for quota update # check for quota update
if [ -n "$quota" ]; then if [[ -n $quota ]]; then
# make quota uppercase in case it is set to NULL # make quota uppercase in case it is set to NULL
quota=`echo $quota | tr [:lower:] [:upper:]` quota=`echo $quota | tr [:lower:] [:upper:]`
if [[ "$quota" =~ ^[0-9]+$ ]] || [[ "$quota" == "NULL" ]]; then if [[ "$quota" =~ ^[0-9]+$ ]] || [[ "$quota" == "NULL" ]]; then
@ -83,7 +52,7 @@ if [ -n "$quota" ]; then
fi fi
# check for limit update # check for limit update
if [ ! -z "$limit" ]; then if [[ -n $limit ]]; then
# make limit uppercase in case it is set to NULL # make limit uppercase in case it is set to NULL
limit=`echo $limit | tr [:lower:] [:upper:]` limit=`echo $limit | tr [:lower:] [:upper:]`
if [[ "$limit" =~ ^[0-9]+$ ]] || [[ "$limit" == "NULL" ]]; then if [[ "$limit" =~ ^[0-9]+$ ]] || [[ "$limit" == "NULL" ]]; then
@ -98,7 +67,7 @@ if [ ! -z "$limit" ]; then
fi fi
# check for status update # check for status update
if [ ! -z "$status" ]; then if [[ -n $status ]]; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then if [ "$status" == 0 ] || [ "$status" == 1 ]; then
if [ ! -z "$dbset" ]; then if [ ! -z "$dbset" ]; then
dbset="$dbset," dbset="$dbset,"

View File

@ -13,87 +13,34 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Add email forward to vmail system" echo "Add email forward to vmail system"
echo "" echo ""
echo "usage: $thisfilename email forward_to [OPTIONS]" echo "usage: $thisfilename -e <email> -f <forward> [-k <0|1>] [-h]"
echo "" echo ""
echo " -h Print this help." 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 ""
echo " 'email' and 'forward_to' should both be in full email address format." echo " Forwarding is for delivering an email to another domain. Use aliases"
echo " .e.g to have info@example.com delivered (forwarded) to joe@example.com do:" echo " for directing emails to another address in the same domain."
echo " $thisfilename info@example.com joe@example.com" 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 exit
} }
# check for and set forward_to & email address & split email in to mbox (local part) & domain vmail:getoptions "$@"
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then # check for email
help if [[ -z $email ]]; then
else echo "email is required"
email=$1 exit
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 fi
# set any options that were passed # check for forward
while getopts "hl:" opt; do if [[ -z $forward ]]; then
case "${opt}" in echo "forward is required"
h ) exit
help fi
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 # build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" 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\"` vm_forwards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $vm_forwards_id ]; then if [ -z $vm_forwards_id ]; then
# existing forward does not exist, add it now # existing forward does not exist, add it now
dbquery="INSERT INTO vm_forwards SET mbox_id='$mbox_id', forward_to='$forward_to'" dbquery="INSERT INTO vm_forwards SET mbox_id='$mbox_id', forward_to='$forward'"
if [ ! -z $save_local ]; then if [ ! -z $keep ]; then
dbquery="$dbquery, save_local='$save_local'" dbquery="$dbquery, save_local='$keep'"
fi fi
eval $dbcmd $dbcmdopts \"$dbquery\;\" eval $dbcmd $dbcmdopts \"$dbquery\;\"
else else

View File

@ -13,46 +13,20 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Delete email forward from vmail database." echo "Delete email forward from vmail database."
echo "" echo ""
echo "usage: $thisfilename email" echo "usage: $thisfilename -e email"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo "" echo " -e <email> Email address to remove forwarding from."
echo " Enter email in full email address format to delete forwarding."
exit exit
} }
# check for and set alias search term(s) vmail:getoptions "$@"
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 # check for email
while getopts "h" opt; do if [[ -z $email ]]; then
case "${opt}" in echo "email name is required"
h ) exit
help fi
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query # build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"

View File

@ -13,12 +13,14 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Get email forwards data from vmail database." echo "Get email forwards data from vmail database."
echo "" echo ""
echo "usage: $thisfilename [email|forward|domain] [OPTIONS]" echo "usage: $thisfilename [-e email|-f forward|-f domain] [OPTIONS]"
echo "" echo ""
echo " -c Output in cvs format."
echo " -f Return info for specific forward."
echo " -h Print this help." 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 ""
echo " Search term is optional. If nothing specified all forwards for all email acccounts for all domains will be returned." 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 email address to get forward info for that email address."
@ -27,69 +29,24 @@ help()
exit exit
} }
# check for and set alias search term(s) vmail:getoptions "$@"
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 # set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e" 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" 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'"
# 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 # build query
if [ -n "$domain" ]; then if [[ -n $forward ]]; 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 # 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'" dbquery="$dbquery AND vm_forwards.forward_to='$mbox@$domain'"
else elif [[ -n $domain ]]; then
# add specific domain
dbquery="$dbquery vm_domains.domain='$domain'"
if [[ -n "$mbox" ]]; then
# search for forward for specific email address # search for forward for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'" dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
fi fi
fi
fi fi
# set order by # set order by

View File

@ -13,65 +13,28 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Add email account to vmail system" echo "Add email account to vmail system"
echo "" echo ""
echo "usage: $thisfilename email password [OPTIONS]" echo "usage: $thisfilename -e <email> -p <password> [-q <quota>] [-s <0|1>] [-h]"
echo "" echo ""
echo " -h Print this help." 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 " -e <email> Email address to add."
echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1." echo " -p <password> Unencrypted Password for new email address."
exit 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 vmail:getoptions "$@"
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then # check for email
help if [[ -z $email ]]; then
else echo "email is required"
email=$1
shift
passwd=$1
shift
if [[ $email =~ "@" ]] ; then
mbox=${email%@*}
domain=${email##*@}
if [ -z $mbox ] ; then
echo "ERROR: No local part in $email."
exit 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 fi
# set any options that were passed # check for password
while getopts "hq:s:" opt; do if [[ -z $password ]]; then
case "${opt}" in echo "password is required"
h ) exit
help fi
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
# get domain_id (and thus check if domain already exists) # 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';"` 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 if [ "$rowcount" -eq '0' ] ; then
# mbox does not exist, build SQL # mbox does not exist, build SQL
# first encrypt password # 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\"" 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 [[ -n $status ]] ; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
dbcmd="$dbcmd, status=\"$status\"" dbcmd="$dbcmd, status=\"$status\""
else
echo "ERROR: status (-s) must be 1 or 0"
exit
fi
fi fi
if [ -z "$quota" ] ; then if [ -z "$quota" ] ; then
# get mbox_quota_default from domains table # get mbox_quota_default from domains table

View File

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

View File

@ -13,69 +13,32 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Get email account data from vmail database." echo "Get email account data from vmail database."
echo "" echo ""
echo "usage: $thisfilename [username|domain|fullemail] [OPTIONS]" echo "usage: $thisfilename [-m <mbox>|-d <domain>|-e <email>] [OPTIONS]"
echo "" echo ""
echo " -c Output in cvs format."
echo " -h Print this help." echo " -h Print this help."
echo " -s Be more silent - use tabs instead of tables for output, do not display column headers." echo " -m <mbox> Return all email accounts, accross all domains, with <mbox> for the local part."
echo " -p Include encrypt password in output." echo " -d <domain> Return all email accounts from specified domain."
echo " -w Wildcard search when searching for username." 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 ""
echo " Search term is optional. If nothing specified all email acccounts for all domains will be returned." 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 " By default username searches are for exact matchs."
echo " Specify -w to turn them in to a wildcard search. Examples:" echo " Specify -g to turn them in to a wildcard (glob) search. Examples:"
echo " $thisfilename joe@example.com # search for specific email address 'joe@example.com'." echo " $thisfilename -e joe@example.com # search for specific email address 'joe@example.com'."
echo " $thisfilename joe # search for username 'joe' in all domains." echo " $thisfilename -m joe # search for username 'joe' in all domains."
echo " $thisfilename joe -w # search for usernames containing 'joe' in all domains." echo " $thisfilename -m joe -g # search for usernames containing 'joe' in all domains."
echo " $thisfilename example.com # search for all usernames in the 'example.com' domain." echo " $thisfilename -d example.com # search for all usernames in the 'example.com' domain."
exit exit
} }
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e" 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 # build query
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain" dbquery="SELECT vm_mboxes.mbox, vm_domains.domain"
if [ -n "$includepassword" ]; then 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'" dbquery="$dbquery AND vm_domains.domain='$domain' AND vm_mboxes.mbox='$mbox'"
elif [ -n "$mbox" ] && [ -z "$domain" ]; then elif [ -n "$mbox" ] && [ -z "$domain" ]; then
# search all domains for username # search all domains for username
if [ -n "$wildcardsearch" ]; then if [ -n "$glob" ]; then
# wildcard search # wildcard search
dbquery="$dbquery AND vm_mboxes.mbox LIKE '%$mbox%'" dbquery="$dbquery AND vm_mboxes.mbox LIKE '%$mbox%'"
else else

View File

@ -13,67 +13,23 @@ help()
echo "$thisfilename" echo "$thisfilename"
echo "Modify an existing email." echo "Modify an existing email."
echo "" echo ""
echo "usage: $thisfilename email [OPTIONS]" echo "usage: $thisfilename -e <email> [OPTIONS]"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -p PASSWORD Set new password." echo " -e <email> Email account to modify."
echo " -q QUOTA Set mailbox quota in GB, otherwise default for this domain is used. NULL means no limit." echo " -p <password> Set new password."
echo " -s STATUS 1 for enabled, 0 for disabled. Default is in db structure and is normally set to 1." 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 exit
} }
# check for and set email address, local part & domain vmail:getoptions "$@"
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
# set any options that were passed # check for email
while getopts "hp:q:s:" opt; do if [[ -z $email ]]; then
case "${opt}" in echo "email is required"
h ) exit
help fi
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
# get mbox id (and thus check if email account already exists) # 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;"` 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 fi
# check for password update # 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}||'` passwd=`doveadm -o stats_writer_socket_path= pw -s sha512-crypt -p "$passwd"|sed 's|^{SHA512-CRYPT}||'`
if [ ! -z "$dbset" ]; then if [ ! -z "$dbset" ]; then
dbset="$dbset," dbset="$dbset,"
@ -109,15 +65,10 @@ fi
# check for status update # check for status update
if [ ! -z "$status" ]; then if [ ! -z "$status" ]; then
if [ "$status" == 0 ] || [ "$status" == 1 ]; then
if [ ! -z "$dbset" ]; then if [ ! -z "$dbset" ]; then
dbset="$dbset," dbset="$dbset,"
fi fi
dbset="$dbset status=\"$status\"" dbset="$dbset status=\"$status\""
else
echo "ERROR: status (-s) must be 1 or 0"
exit
fi
fi fi
if [ -n "$dbset" ]; then if [ -n "$dbset" ]; then

View File

@ -26,7 +26,7 @@ fi
# crude but good enough domain name format validation # crude but good enough domain name format validation
function vmail::validate_domain () { function vmail::validate_domain () {
local my_domain=$1 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 return 0
else else
return 1 return 1
@ -72,3 +72,125 @@ function vmail::yesno() {
done 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))
}