2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /usr/local/etc/vmail.conf
help( )
{
thisfilename = $( basename -- " $0 " )
echo " $thisfilename "
echo "Add email alias to vmail system"
echo ""
echo " usage: $thisfilename email alias [OPTIONS] "
echo ""
echo " -h Print this help."
echo ""
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
}
# 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 . "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
elif [ -z $domain ] ; then
echo " ERROR: No domain in $email . "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
elif [ [ ! $domain = ~ ^( ( [ a-zA-Z] ( -?[ a-zA-Z0-9] ) *) \. ) +[ a-zA-Z] { 2,} $ ] ] ; then
echo " ERROR: $domain is not a valid domain name. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
else
echo " ERROR: $email is not a valid email. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
fi
else
help
fi
2021-02-12 09:47:26 -08:00
# 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
2021-02-10 16:16:23 -08:00
# get domain_id (and thus check if domain already exists)
2021-02-11 14:20:30 -08:00
domain_id = ` mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -s -r -N -e " SELECT id from vm_domains WHERE domain=' $domain '; " `
2021-02-10 16:16:23 -08:00
if [ -z $domain_id ] ; then
echo " ERROR: Domain $domain does not exist. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
# get mbox id, which also verfies that email address exists
2021-02-11 14:20:30 -08:00
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 '; " `
2021-02-10 16:16:23 -08:00
if [ -z $mbox_id ] ; then
# mbox does not exist, can't create alias
echo " ERROR: Address to Alias To ( $email ) does not exist. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
elif [ " $mbox_id " -gt '1' ] ; then
# verified mbox, check for existing alias
2021-02-11 14:20:30 -08:00
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 '; " `
2021-02-10 16:16:23 -08:00
if [ -z $existing_alias ] ; then
# existing alias does not exist, add new one now
2021-02-11 14:20:30 -08:00
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
2021-02-10 16:16:23 -08:00
else
if [ " $existing_alias " = = " $mbox " ] ; then
echo " ERROR: Alias for $alias To $email already exists. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
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. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
fi
else
# db query error
echo "ERROR: System error querying vmail database."
exit 1
fi