161 lines
4.3 KiB
Bash
Executable File
161 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
function usage() {
|
|
|
|
echo
|
|
|
|
if [ -n "$1" ];then
|
|
echo -e "Error: $1\n"
|
|
fi
|
|
echo -e "\nPrints a summary of mailboxes and forward addresse for the given domain.\n"
|
|
echo -e "\tusage: `basename $0` <email domain>\n"
|
|
exit 1
|
|
}
|
|
|
|
|
|
trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
|
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
|
echo -n "$var"
|
|
}
|
|
|
|
|
|
|
|
[ $# -eq "0" -o $# -gt "2" ] && usage "wrong number of arguments"
|
|
|
|
address="$1"
|
|
|
|
domain=`echo $address | cut -d'@' -f2`
|
|
|
|
mysql=false
|
|
# - mysql_credential_args
|
|
# -
|
|
# - MySQL / MariaDB credentials
|
|
# -
|
|
# - Giving password on command line is insecure an sind mysql 5.5
|
|
# - you will get a warning doing so.
|
|
# -
|
|
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
|
|
# - commandline parameter '--defaults-file'.
|
|
# -
|
|
# - Since Mysql Version 5.6, you can read username/password from
|
|
# - encrypted file.
|
|
# -
|
|
# - Create (encrypted) option file:
|
|
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
|
|
# - $ Password:
|
|
# -
|
|
# - Use of option file:
|
|
# - $ mysql --login-path=local ...
|
|
# -
|
|
# - Example
|
|
# - mysql_credential_args="--login-path=local"
|
|
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
|
|
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
|
# -
|
|
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
|
mysql_db=postfix
|
|
|
|
#echo "address...: $address"
|
|
#echo "domain....: $domain"
|
|
|
|
declare -A address_arr
|
|
declare -a orders
|
|
declare -a _tmp_mbox_arr
|
|
declare -a alias_arr
|
|
declare -a mbox_arr
|
|
declare -a mbox_alias_arr
|
|
|
|
|
|
echo ""
|
|
if $mysql ; then
|
|
_addresses=$(mysql $mysql_credential_args $mysql_db -N -s -e "select address from alias where domain = '$domain' ORDER BY address")
|
|
for _address in $_addresses ; do
|
|
address_arr[$_address]=$(trim $(mysql $mysql_credential_args $mysql_db -N -s -e "select goto from alias where address = '$_address'"))
|
|
orders+=("$(trim $_address)")
|
|
done
|
|
else
|
|
|
|
_addresses=$(su - postgres -c"psql postfix -t -q -c\"select address from alias where domain = '$domain' ORDER BY address\"")
|
|
for _address in $_addresses ; do
|
|
address_arr[$_address]=$(trim $(su - postgres -c"psql postfix -t -q -c\"select goto from alias where address = '$_address'\""))
|
|
orders+=("$(trim $_address)")
|
|
done
|
|
fi
|
|
|
|
# - Mailbox or only forward address?
|
|
# -
|
|
for i in ${!orders[@]}; do
|
|
if [[ ${address_arr[${orders[$i]}]} =~ ${orders[$i]} ]]; then
|
|
_tmp_mbox_arr+=(${orders[$i]})
|
|
else
|
|
alias_arr+=(${orders[$i]})
|
|
fi
|
|
done
|
|
|
|
# - Mailbox with or witout forward addresses?
|
|
# -
|
|
for i in ${!_tmp_mbox_arr[@]} ; do
|
|
found=false
|
|
IFS=',' read -a _addr_list <<<"${address_arr[${_tmp_mbox_arr[$i]}]}"
|
|
_forward_addresses=""
|
|
for j in ${!_addr_list[@]} ; do
|
|
[[ ${_addr_list[$j]} =~ ${_tmp_mbox_arr[$i]} ]] && continue
|
|
[[ ${_addr_list[$j]} =~ @autoreply ]] && continue
|
|
found=true
|
|
_forward_addresses="$_forward_addresses ${_addr_list[$j]}"
|
|
done
|
|
if ! $found ; then
|
|
mbox_arr+=(${_tmp_mbox_arr[$i]})
|
|
else
|
|
mbox_alias_arr+=(${_tmp_mbox_arr[$i]})
|
|
address_arr[${_tmp_mbox_arr[$i]}]=$(trim $_forward_addresses)
|
|
fi
|
|
done
|
|
|
|
echo ""
|
|
echo "--------------------"
|
|
echo "- Zusammenfassung E-Mail Adressen der Domain \"$domain\""
|
|
echo "--------------------"
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
echo "E-Mail Adressen: Postfach ohne Weiterleitungen:"
|
|
echo "-----------------------------------------------"
|
|
|
|
for i in ${!mbox_arr[@]} ; do
|
|
echo "${mbox_arr[$i]}"
|
|
done
|
|
|
|
echo ""
|
|
echo "E-Mail Adressen: Postfach mit Weiterleitungen:"
|
|
echo "----------------------------------------------"
|
|
|
|
for i in ${!mbox_alias_arr[@]} ; do
|
|
echo -e "${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}"
|
|
echo
|
|
done
|
|
|
|
echo ""
|
|
echo "E-Mail Adressen: Nur Weiterleitungen:"
|
|
echo "-------------------------------------"
|
|
|
|
for i in ${!alias_arr[@]} ; do
|
|
[[ ${alias_arr[$i]} =~ ^abuse@ ]] && continue
|
|
[[ ${alias_arr[$i]} =~ ^postmaster@ ]] && continue
|
|
echo -en "${alias_arr[$i]}\n -->"
|
|
|
|
IFS=',' read -a _addr_list <<<"${address_arr[${alias_arr[$i]}]}"
|
|
for j in ${!_addr_list[@]} ; do
|
|
echo -n " ${_addr_list[$j]}"
|
|
done
|
|
echo
|
|
echo
|
|
done
|
|
|
|
echo ""
|
|
exit 0
|