115 lines
3.5 KiB
Bash
115 lines
3.5 KiB
Bash
#!/bin/bash
|
|
|
|
# shellcheck disable=SC1091
|
|
[ -r /var/lib/from_repos/ad_scripts/shared_functions.sh ] && . /var/lib/from_repos/ad_scripts/shared_functions.sh
|
|
function usage() {
|
|
echo "Usage:"
|
|
echo "$(basename "${0}") -p|--porpouse <porpouse> [-o|--domain <DOMAIN>] [-l|--length <ALIAS_LENGTH>] [-e|--destination <DESTINATION>]"
|
|
echo ""
|
|
echo " -d|--debug Show more debug information."
|
|
echo " -p|--porpouse <PORPOUSE> Porpouse of this forwarding that will help you remember why you created it."
|
|
echo " -o|--domain <DOMAIN> Domain name for the alias. From the list:"
|
|
while read -r DOMAIN
|
|
do
|
|
echo " # ${DOMAIN}"
|
|
done <<< "$(echo 'select domain FROM mail.domains' | mysql mail|grep -v '^domain$')"
|
|
echo " -l|--length <ALIAS_LENGTH> Length of pseudo-random character of the alias."
|
|
echo " -e|--destination <DESTINATION> Destination of the forwarding."
|
|
echo " -a|--alias <ALIAS> Use a defined alias."
|
|
}
|
|
domain="susurrando.com"
|
|
alias_length=6
|
|
destination="ad@susurrando.com"
|
|
alias=""
|
|
while [ $# -gt 0 ]
|
|
do
|
|
case "${1}" in
|
|
"-h"|"-?"|"--help")
|
|
shift
|
|
usage
|
|
exit 0
|
|
;;
|
|
"-d"|"--debug")
|
|
shift
|
|
export DEBUG=true
|
|
;;
|
|
"-o"|"--domain")
|
|
shift
|
|
domain="${1}"
|
|
existing=$(echo "select domain FROM mail.domains WHERE domain = '${domain}';" | mysql mail | grep -v '^domain$')
|
|
if [ -z "${existing}" ]; then
|
|
message "The domain '${domain}' doesn't exist in the database. Add it first." p
|
|
echo "Current list of domains:"
|
|
while read -r DOMAIN
|
|
do
|
|
echo " # ${DOMAIN}"
|
|
done <<< "$(echo 'select domain FROM mail.domains' | mysql mail|grep -v '^domain$')"
|
|
exit 4
|
|
fi
|
|
shift
|
|
;;
|
|
"-l"|"--length")
|
|
shift
|
|
alias_length="${1}"
|
|
shift
|
|
;;
|
|
"-e"|"--destination")
|
|
shift
|
|
destination="${1}"
|
|
shift
|
|
;;
|
|
"-p"|"--porpouse")
|
|
shift
|
|
porpouse="${1}"
|
|
shift
|
|
;;
|
|
"-a"|"--alias")
|
|
shift
|
|
alias="${1}"
|
|
shift
|
|
;;
|
|
*)
|
|
message "Unknown parameter '$1'" p
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ -z "${porpouse}" ]; then
|
|
message "You have to provide a porpouse." p
|
|
usage
|
|
exit 1
|
|
fi
|
|
existing=$(echo "SELECT * FROM mail.forwardings where porpouse = '${porpouse}';" | mysql mail)
|
|
if [ -n "${existing}" ]; then
|
|
message "There is already an alias for that porpouse:" p
|
|
message "${existing}" p
|
|
exit 2
|
|
fi
|
|
|
|
if [ -n "${alias}" ]; then
|
|
existing=$(echo "SELECT source FROM mail.forwardings where source = '${alias}';" |mysql mail | grep -v '^source$')
|
|
if [ -n "${existing}" ]; then
|
|
message "Alias already exist in the database:" p
|
|
message "'${existing}'" p
|
|
exit 3
|
|
fi
|
|
fi
|
|
|
|
while [ "${alias}" == "" ]
|
|
do
|
|
random_string=$(LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c "${alias_length}")
|
|
existing=$(echo "SELECT source FROM mail.forwardings where source = '${random_string}@${domain}';" |mysql mail |grep -v '^source$')
|
|
if [ -z "${existing}" ]; then
|
|
alias="${random_string}@${domain}"
|
|
message "Generated alias: '${alias}'"
|
|
else
|
|
message "Generated alias '${random_string}@${domain}' exists in the database."
|
|
message "'${existing}'"
|
|
fi
|
|
done
|
|
echo "INSERT INTO mail.forwardings (source, destination, porpouse) VALUES ('${alias}','${destination}','${porpouse}');"| mysql mail
|
|
message "Inserted forwarding from:\n${alias}\nto:\n${destination}\nfor the porpouse of '${porpouse}'." p
|
|
|
|
result=$(dbmail-users -q -y -c "${destination}" -s "${alias}")
|
|
message "Added alias in dbmail. Result: ${result}"
|