From 4bf85f883988bc4ceedba3ebc2c375d6f374a387 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Fri, 15 Oct 2021 15:17:29 -0700 Subject: [PATCH] reworked getopts --- bin/vmail-aliases-add.sh | 74 ++++++----------------- bin/vmail-aliases-del.sh | 107 ++++++++++++++++---------------- bin/vmail-aliases-get.sh | 82 +++++++------------------ bin/vmail-dkim-add.sh | 49 ++++++++++----- bin/vmail-dkim-del.sh | 39 +++++++++--- bin/vmail-domains-add.sh | 56 ++++------------- bin/vmail-domains-del.sh | 39 +++--------- bin/vmail-domains-get.sh | 69 ++++++--------------- bin/vmail-domains-mod.sh | 61 +++++-------------- bin/vmail-forwards-add.sh | 101 ++++++++----------------------- bin/vmail-forwards-del.sh | 42 +++---------- bin/vmail-forwards-get.sh | 85 +++++++------------------- bin/vmail-mboxes-add.sh | 82 ++++++------------------- bin/vmail-mboxes-del.sh | 50 +++------------ bin/vmail-mboxes-get.sh | 69 +++++---------------- bin/vmail-mboxes-mod.sh | 81 +++++-------------------- bin/vmail.sh | 124 +++++++++++++++++++++++++++++++++++++- 17 files changed, 452 insertions(+), 758 deletions(-) diff --git a/bin/vmail-aliases-add.sh b/bin/vmail-aliases-add.sh index 8776128..0739835 100755 --- a/bin/vmail-aliases-add.sh +++ b/bin/vmail-aliases-add.sh @@ -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 -a [-h]" echo "" echo " -h Print this help." + echo " -e Existing email address that will receive alias emails." + echo " -a 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 + echo "ERROR: Alias for $alias To $email already exists." + exit 1 fi else # db query error diff --git a/bin/vmail-aliases-del.sh b/bin/vmail-aliases-del.sh index be429c0..a74da4e 100755 --- a/bin/vmail-aliases-del.sh +++ b/bin/vmail-aliases-del.sh @@ -13,69 +13,68 @@ help() echo "$thisfilename" echo "Delete email alias from vmail database." echo "" - echo "usage: $thisfilename alias" + echo "usage: $thisfilename -d |-e -a " echo "" - echo " -h Print this help." + echo " -h Print this help." + echo " -a Alias to be deleted. Local part only." + echo " -d Domain of alias to delete." + echo " -e Email that alias directs to to delete." echo "" - echo " Enter alias in full email address format." - exit + echo " Enter OR . If is specified" + echo " then just the alias to that address is deleted. If" + echo " domain is specific then all @ 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." - exit 1 -elif [ "$vm_aliases_id" -gt '0' ]; then - dbquery="DELETE FROM vm_aliases WHERE vm_aliases.id='$vm_aliases_id'" - eval $dbcmd $dbcmdopts "\"$dbquery\"" - echo "SUCCESS: Alias $searchterm removed from system." - exit 0 +# 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 + dbquery="DELETE FROM vm_aliases WHERE vm_aliases.id='$vm_aliases_id'" + echo $dbquery + eval $dbcmd $dbcmdopts "\"$dbquery\"" + echo "SUCCESS: Alias $alias@$domain -> $email removed from system." + exit 0 + 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 "ERROR: System error querying vmail database" + echo "You must specify either or ." exit 1 fi - - diff --git a/bin/vmail-aliases-get.sh b/bin/vmail-aliases-get.sh index 55b9095..60eec50 100755 --- a/bin/vmail-aliases-get.sh +++ b/bin/vmail-aliases-get.sh @@ -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 |-e |-d ] [-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 ) - dbcmdopts="-s -N $dbcmdopts" - ;; - \? ) - echo "Invalid option: $OPTARG" 1>&2 - exit;; - esac -done +if [[ -n $tab ]]; then + dbcmdopts="-s -N $dbcmdopts" +fi -# build query -if [ -n "$domain" ]; then - # add specific domain - dbquery="$dbquery AND vm_domains.domain='$domain'" - if [ -n "$localpart" ]; then - # search for specific alias or mbox - if [ -n "$aliassearch" ]; then - # search for specific alias address - dbquery="$dbquery AND vm_aliases.alias='$localpart'" - else - # search for all aliases for specific email address - dbquery="$dbquery AND vm_mboxes.mbox='$localpart'" - fi +if [[ -n $alias ]]; then + if [[ -n $domain ]] ; then + dbquery="$dbquery AND vm_aliases.alias='$alias' AND vm_domains.domain='$domain'" + else + 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 or or ." + exit 1 fi # set order by diff --git a/bin/vmail-dkim-add.sh b/bin/vmail-dkim-add.sh index e0a7459..05f8b25 100755 --- a/bin/vmail-dkim-add.sh +++ b/bin/vmail-dkim-add.sh @@ -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 [-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/.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 diff --git a/bin/vmail-dkim-del.sh b/bin/vmail-dkim-del.sh index bd751f1..060f77f 100755 --- a/bin/vmail-dkim-del.sh +++ b/bin/vmail-dkim-del.sh @@ -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 [-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.* diff --git a/bin/vmail-domains-add.sh b/bin/vmail-domains-add.sh index 02767f7..899d5d6 100755 --- a/bin/vmail-domains-add.sh +++ b/bin/vmail-domains-add.sh @@ -13,56 +13,26 @@ help() echo "$thisfilename" echo "Add domain to vmail system" echo "" - echo "usage: $thisfilename domain [OPTIONS]" + echo "usage: $thisfilename -d [-l ] [-q ] [-s ]" 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 Maximum number of mailboxes for this domain." + echo " -q Default mailbox quota in GB." + echo " -s 1 for enabled, 0 for disabled." echo "" - echo " Defaults for are all OPTIONS are configured in database." - echo " NULL for LIMIT or QUOTA means no limit." + 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" diff --git a/bin/vmail-domains-del.sh b/bin/vmail-domains-del.sh index 17435b1..ee7a6f3 100755 --- a/bin/vmail-domains-del.sh +++ b/bin/vmail-domains-del.sh @@ -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 [-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';" diff --git a/bin/vmail-domains-get.sh b/bin/vmail-domains-get.sh index f4b5244..941520d 100755 --- a/bin/vmail-domains-get.sh +++ b/bin/vmail-domains-get.sh @@ -13,68 +13,35 @@ help() echo "$thisfilename" echo "Get domain data from vmail database." echo "" - echo "usage: $thisfilename [domain] [OPTIONS]" + echo "usage: $thisfilename [-d |-g ] [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 - dbquery="$dbquery WHERE domain='$domain'" - else - echo "ERROR: Invalid domain name: $domain" - exit 1 - fi - fi +if [[ -n $domain ]]; then + dbquery="$dbquery WHERE domain='$domain'" +elif [[ -n $glob ]]; then + dbquery="$dbquery WHERE domain LIKE '%$domain%'" fi # execute mysql query diff --git a/bin/vmail-domains-mod.sh b/bin/vmail-domains-mod.sh index 6eba2bb..0ef0b59 100755 --- a/bin/vmail-domains-mod.sh +++ b/bin/vmail-domains-mod.sh @@ -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 - 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 +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 - 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," diff --git a/bin/vmail-forwards-add.sh b/bin/vmail-forwards-add.sh index 8c926a7..9fdb6da 100755 --- a/bin/vmail-forwards-add.sh +++ b/bin/vmail-forwards-add.sh @@ -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 -f [-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 " -h Print this help." + echo " -e Email address to forward." + echo " -f 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 diff --git a/bin/vmail-forwards-del.sh b/bin/vmail-forwards-del.sh index ad4334a..affdd7a 100755 --- a/bin/vmail-forwards-del.sh +++ b/bin/vmail-forwards-del.sh @@ -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 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" diff --git a/bin/vmail-forwards-get.sh b/bin/vmail-forwards-get.sh index 30869b1..947a1d7 100755 --- a/bin/vmail-forwards-get.sh +++ b/bin/vmail-forwards-get.sh @@ -13,82 +13,39 @@ 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 " -h Print this help." + echo " -e Email address to remove forwarding from." + echo " -f Return info for specific forward." + echo " -d 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." - echo " Enter forwarding address (in full email address format) with the -f option to get address(es) that forward to specified address." - echo " Enter domain name to get all forwards for all email addresses under that domain." + 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 forwarding address (in full email address format) with the -f option to get address(es) that forward to specified address." + echo " Enter domain name to get all forwards for all email addresses under that domain." 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 +if [[ -n $forward ]]; then + # search for specific forward to address + dbquery="$dbquery AND vm_forwards.forward_to='$mbox@$domain'" +elif [[ -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 - # 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 - # search for forward for specific email address - dbquery="$dbquery AND vm_mboxes.mbox='$mbox'" - fi + 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 diff --git a/bin/vmail-mboxes-add.sh b/bin/vmail-mboxes-add.sh index e2dcca4..0f35c16 100755 --- a/bin/vmail-mboxes-add.sh +++ b/bin/vmail-mboxes-add.sh @@ -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 -p [-q ] [-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 " -h Print this help." + echo " -e Email address to add." + echo " -p Unencrypted Password for new email address." + echo " -q 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." - 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 +vmail:getoptions "$@" + +# check for email +if [[ -z $email ]]; then + echo "email is required" + exit 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 - dbcmd="$dbcmd, status=\"$status\"" - else - echo "ERROR: status (-s) must be 1 or 0" - exit - fi + if [[ -n $status ]] ; then + dbcmd="$dbcmd, status=\"$status\"" fi if [ -z "$quota" ] ; then # get mbox_quota_default from domains table diff --git a/bin/vmail-mboxes-del.sh b/bin/vmail-mboxes-del.sh index cda71c1..002546b 100755 --- a/bin/vmail-mboxes-del.sh +++ b/bin/vmail-mboxes-del.sh @@ -13,50 +13,20 @@ help() echo "$thisfilename" echo "Delete email account." echo "" - echo "usage: $thisfilename fullemail" + echo "usage: $thisfilename -e [-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 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\"" diff --git a/bin/vmail-mboxes-get.sh b/bin/vmail-mboxes-get.sh index 41f8c44..9fc43ba 100755 --- a/bin/vmail-mboxes-get.sh +++ b/bin/vmail-mboxes-get.sh @@ -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 |-d |-e ] [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 Return all email accounts, accross all domains, with for the local part." + echo " -d Return all email accounts from specified domain." + echo " -e 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 diff --git a/bin/vmail-mboxes-mod.sh b/bin/vmail-mboxes-mod.sh index 9dca1bc..b372606 100755 --- a/bin/vmail-mboxes-mod.sh +++ b/bin/vmail-mboxes-mod.sh @@ -13,67 +13,23 @@ help() echo "$thisfilename" echo "Modify an existing email." echo "" - echo "usage: $thisfilename email [OPTIONS]" + echo "usage: $thisfilename -e [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 " -h Print this help." + echo " -e Email account to modify." + echo " -p Set new password." + echo " -q 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 + if [ ! -z "$dbset" ]; then + dbset="$dbset," fi + dbset="$dbset status=\"$status\"" fi if [ -n "$dbset" ]; then diff --git a/bin/vmail.sh b/bin/vmail.sh index 07f6520..ce3e992 100755 --- a/bin/vmail.sh +++ b/bin/vmail.sh @@ -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)) +}