added error codes, eval instead of echo dbcmd

This commit is contained in:
Matthew Saunders Brown 2021-02-11 14:20:30 -08:00
parent 319e8208ef
commit bb87bf83f1

View File

@ -52,17 +52,17 @@ if [ -n "$2" ]; then
domain=${email##*@}
if [ -z $mbox ] ; then
echo "ERROR: No local part in $email."
exit
exit 1
elif [ -z $domain ] ; then
echo "ERROR: No domain in $email."
exit
exit 1
elif [[ ! $domain =~ ^(([a-zA-Z](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
echo "ERROR: $domain is not a valid domain name."
exit
exit 1
fi
else
echo "ERROR: $email is not a valid email."
exit
exit 1
fi
fi
else
@ -70,33 +70,32 @@ else
fi
# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=/usr/local/etc/vmail.conf -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';"`
if [ -z $domain_id ] ; then
echo "ERROR: Domain $domain does not exist."
exit
exit 1
fi
# get mbox id, which also verfies that email address exists
mbox_id=`mysql --defaults-extra-file=/usr/local/etc/vmail.conf -s -r -N -e "SELECT id FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$mbox';"`
# rowcount=`mysql --defaults-extra-file=/usr/local/etc/vmail.conf -s -r -N -e "SELECT COUNT(*) FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$mbox';"`
mbox_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id FROM vm_mboxes WHERE domain_id='$domain_id' AND mbox='$mbox';"`
if [ -z $mbox_id ] ; then
# mbox does not exist, can't create alias
echo "ERROR: Address to Alias To ($email) does not exist."
exit
exit 1
elif [ "$mbox_id" -gt '1' ] ; then
# verified mbox, check for existing alias
existing_alias=`mysql --defaults-extra-file=/usr/local/etc/vmail.conf -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_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';"`
if [ -z $existing_alias ]; then
# existing alias does not exist, add new one now
dbcmd="mysql --defaults-extra-file=/usr/local/etc/vmail.conf -e 'INSERT INTO vm_aliases SET vm_aliases.alias=\"$alias\", vm_aliases.mbox_id=\"$mbox_id\";'"
echo $dbcmd
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
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
exit 1
fi
fi
else