#!/bin/bash
USER=xxxxxxxxxxx
PASSWORD="xxxxxxxxxxxxxxxxxxxxxx"
DATABASE=xxxxxxxxxxx
CONNECTION="postgresql://$USER:$PASSWORD@localhost:5432/$DATABASE?sslmode=require"
DOMAIN="example.com"
function usage() {
cat << EOF
This script is used for alias managment for postfix with PostgreSQL installation .
Usage: aliases <command>
Commands:
list
output all aliases
all
output all aliases and emails without aliases
add <valid email> <alias>
add new alias <alias> for email <valid email>
remove <valid email> <alias> [--force]
remove alias <alias> for email <valid email>
find <something>
shows all email and aliases contains text <something>
check
shows all broken aliases.
EOF
}
function error() {
echo "Error: $1"
exit
}
function check_email_exists() {
if [ -n "$1" ]; then
local RES=$(psql -q -n -t ${CONNECTION} -c "select count(*) from forwardings where address='$1' and forwarding=address and is_alias=0") #" MC Parser FIX
echo $RES | tr -d "\r\n\t "
else
echo 0
fi;
}
function check_alias_exists() {
if [ -z "$1" -o -z "$2" ]; then
echo 0
else
local RES=$(psql -q -n -t ${CONNECTION} -c "select count(*) from forwardings where address='$2' and forwarding='$1' and is_alias=1") #" MC Parser FIX
echo $RES | tr -d "\r\n\t "
fi;
}
function run_sql() {
psql -1 -q -n $2 ${CONNECTION} -c "$1"
}
CMD=$1
EMAIL=$2
ALIAS=$3
FORCE=$4
case "x$CMD" in
"xall")
run_sql "select address \"virtual email\",forwarding \"original email\",is_alias from forwardings"
exit
;;
"xlist")
run_sql "select address \"virtual email\",forwarding \"original email\" from forwardings where is_alias = 1"
exit
;;
"xcheck")
run_sql "select a.address \"virtual email\",a.forwarding \"original email\",a.is_alias from forwardings a left join forwardings b on a.forwarding=b.address where b.forwarding is null"
exit
;;
"xfind")
EMAIL=$(echo ${EMAIL} | tr -d '\"\r\n\t\a\%\$\!\\\/\^\&\*{}[]#()~') # ' MC PARSER FIX
echo "Searchong for '${EMAIL}'"
run_sql "select address \"virtual email\",forwarding \"original email\", is_alias from forwardings where address like '%${EMAIL}%' or forwarding like '%${EMAIL}%'" "-t"
exit
;;
"xadd")
if [ "$EMAIL" == "$ALIAS" ]; then
error "email and alias can't be equal!"
fi;
if [ "$(check_email_exists "$EMAIL")" == "0" ]; then
error "email '$EMAIL' doesn't exists! Try lowercase."
fi;
if [ "$(check_alias_exists "$EMAIL" "$ALIAS")" != "0" ]; then
error "alias '$ALIAS' for email '$EMAIL' already exists!"
fi;
run_sql "INSERT INTO forwardings (address, forwarding, domain, is_alias,active) VALUES ('$ALIAS', '$EMAIL', '$DOMAIN', 1,1);" "-t"
exit
;;
"xremove")
if [ "$(check_email_exists "$EMAIL")" == "0" ]; then
if [ "$FORCE" != "--force" ]; then
error "email '$EMAIL' doesn't exists! Try lowercase."
fi;
fi;
if [ "$(check_alias_exists "$EMAIL" "$ALIAS")" == "0" ]; then
error "alias '$ALIAS' for email '$EMAIL' doesn't exists!"
fi;
if [ "$EMAIL" == "$ALIAS" ]; then
error "email and alias can't be equal!"
fi;
run_sql "delete from forwardings where address ='$ALIAS' and forwarding='$EMAIL' and is_alias=1;" "-t"
exit
;;
*)
usage
exit
;;
esac;