272 lines
7.0 KiB
Bash
Executable File
272 lines
7.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
|
|
script_dir="$(dirname $(realpath $0))"
|
|
conf_dir="${script_dir}/conf"
|
|
conf_file="${conf_dir}/get_addresslist_for_domain.conf"
|
|
|
|
_date="$(date +%Y-%m-%d)"
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Setting Defaults
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
DEFAULT_db_type="pgsql"
|
|
DEFAULT_db_name="postfix"
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Base Function(s)
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
function usage() {
|
|
|
|
echo
|
|
|
|
if [ -n "$1" ];then
|
|
echo -e "Error: $1\n"
|
|
fi
|
|
echo -e "\nPrints a summary of mailboxes and forward addresses for the given domain.\n"
|
|
echo -e "\tusage: `basename $0` <email domain>\n"
|
|
clean_up 1
|
|
}
|
|
trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
|
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
|
echo -n "$var"
|
|
}
|
|
clean_up() {
|
|
|
|
# Perform program exit housekeeping
|
|
exit $1
|
|
}
|
|
echononl(){
|
|
echo X\\c > /tmp/shprompt$$
|
|
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
|
echo "$*\\c" 1>&2
|
|
else
|
|
echo -e -n "$*" 1>&2
|
|
fi
|
|
rm /tmp/shprompt$$
|
|
}
|
|
|
|
warn (){
|
|
echo ""
|
|
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
|
|
echo ""
|
|
}
|
|
|
|
error (){
|
|
echo ""
|
|
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
|
|
echo ""
|
|
}
|
|
|
|
fatal(){
|
|
echo ""
|
|
echo -e "[ \033[31m\033[1mFehler\033[m ]: $*"
|
|
echo -e "\n Script was interupted!\n"
|
|
echo
|
|
clean_up 1
|
|
}
|
|
echo_ok() {
|
|
echo -e "\033[75G[ \033[32mok\033[m ]"
|
|
}
|
|
echo_failed() {
|
|
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
|
}
|
|
echo_skipped() {
|
|
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
|
}
|
|
|
|
trap clean_up SIGHUP SIGINT SIGTERM
|
|
|
|
clear
|
|
echo ""
|
|
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
|
|
|
|
|
|
|
|
|
|
[ $# -eq "0" -o $# -gt "2" ] && usage "wrong number of arguments"
|
|
|
|
address="$1"
|
|
|
|
domain=`echo $address | cut -d'@' -f2`
|
|
|
|
out_file="${script_dir}/log/adresslist-${domain}-${_date}.list"
|
|
> "$out_file"
|
|
|
|
echo ""
|
|
echo ""
|
|
echononl " Loading Configuration values from $(basename ${conf_file}).."
|
|
if [[ ! -f "$conf_file" ]]; then
|
|
echo_skipped
|
|
else
|
|
source "${conf_file}" > /dev/null 2>&1
|
|
if [[ $? -eq 0 ]]; then
|
|
echo_ok
|
|
else
|
|
echo_failed
|
|
fi
|
|
fi
|
|
|
|
[[ -n "$db_type" ]] || db_type="$DEFAULT_db_type"
|
|
|
|
if [[ "$db_type" != "pgsql" ]] && [[ "$db_type" != "mysql" ]]; then
|
|
fatal "Unknown Database Type '$db_type' for Password Database (Parameter db_type)"
|
|
fi
|
|
|
|
[[ -n "$db_name" ]] || db_name="$DEFAULT_db_name"
|
|
|
|
|
|
if [[ "$db_type" = "mysql" ]]; then
|
|
if [[ -z "$mysql_credential_args" ]]; then
|
|
if [[ -f "/etc/mysql/debian.cnf" ]]; then
|
|
mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf"
|
|
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
|
|
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
|
else
|
|
fatal "No credentials for access to MySQL is given!"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
echo ""
|
|
echononl " Create output directory '$(dirname "$out_file")'.."
|
|
if [[ ! -d "$(dirname "$out_file")" ]] ; then
|
|
mkdir "$(dirname "$out_file")"
|
|
if [[ $? -eq 0 ]]; then
|
|
echo_ok
|
|
else
|
|
echo_failed
|
|
fi
|
|
else
|
|
echo_skipped
|
|
fi
|
|
|
|
|
|
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 [[ "$db_type" = "mysql" ]]; then
|
|
_addresses=$(mysql $mysql_credential_args "$db_name" -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 "$db_name" -N -s -e "select goto from alias where address = '$_address'"))
|
|
orders+=("$(trim $_address)")
|
|
done
|
|
else
|
|
|
|
_addresses=$(su - postgres -c"psql "$db_name" -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 "$db_name" -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 "Stand: $(date +%d". "%B" "%Y" "%H":"%M" h")" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
echo "--------------------" >> "$out_file"
|
|
echo "- Zusammenfassung E-Mail Adressen der Domain \"$domain\"" >> "$out_file"
|
|
echo "--------------------" >> "$out_file"
|
|
|
|
echo -e "\n\n\033[32m\033[1mZusammenfassung E-Mail Adressen der Domain \"$domain\":\033[m\n"
|
|
|
|
|
|
echo "" >> "$out_file"
|
|
echo "E-Mail Adressen: Postfach ohne Weiterleitungen:" >> "$out_file"
|
|
echo "-----------------------------------------------" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
|
|
echo -e "\n\033[1mE-Mail Adressen: Postfach ohne Weiterleitungen\033[m\n"
|
|
|
|
for i in ${!mbox_arr[@]} ; do
|
|
echo " ${mbox_arr[$i]}"
|
|
echo " ${mbox_arr[$i]}" >> "$out_file"
|
|
done
|
|
|
|
echo "" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
echo "E-Mail Adressen: Postfach mit Weiterleitungen:" >> "$out_file"
|
|
echo "----------------------------------------------" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
|
|
echo -e "\n\n\033[1mE-Mail Adressen: Postfach mit Weiterleitungen:\033[m\n"
|
|
|
|
for i in ${!mbox_alias_arr[@]} ; do
|
|
echo -e " ${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}"
|
|
echo -e " ${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
done
|
|
|
|
echo "" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
echo "E-Mail Adressen: Nur Weiterleitungen:" >> "$out_file"
|
|
echo "-------------------------------------" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
|
|
echo -e "\n\n\033[1mE-Mail Adressen: Nur Weiterleitungen:\033[m\n"
|
|
|
|
for i in ${!alias_arr[@]} ; do
|
|
[[ ${alias_arr[$i]} =~ ^abuse@ ]] && continue
|
|
[[ ${alias_arr[$i]} =~ ^postmaster@ ]] && continue
|
|
echo -en " ${alias_arr[$i]}\n -->"
|
|
echo -en " ${alias_arr[$i]}\n -->" >> "$out_file"
|
|
|
|
IFS=',' read -a _addr_list <<<"${address_arr[${alias_arr[$i]}]}"
|
|
for j in ${!_addr_list[@]} ; do
|
|
echo -n " ${_addr_list[$j]}"
|
|
echo -n " ${_addr_list[$j]}" >> "$out_file"
|
|
done
|
|
echo ""
|
|
echo ""
|
|
echo "" >> "$out_file"
|
|
echo "" >> "$out_file"
|
|
done
|
|
|
|
echo "" >> "$out_file"
|
|
echo ""
|
|
clean_up 0
|