postfix/get_mail_domains.sh
2017-11-03 14:20:12 +01:00

160 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
script_dir="$(dirname $(realpath $0))"
conf_dir="${script_dir}/conf"
conf_file="${conf_dir}/get_mail_domains.conf"
_date="$(date +%Y-%m-%d)"
tmp_dir="$(mktemp -d)"
log_messages="${tmp_dir}/get_mail_domains.log"
out_file="${script_dir}/log/mail-domains-${_date}.list"
#---------------------------------------
#-----------------------------
# Setting Defaults
#-----------------------------
#---------------------------------------
DEFAULT_db_type="pgsql"
DEFAULT_db_name="postfix"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
clean_up() {
# Perform program exit housekeeping
rm -rf $tmp_dir
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 .."
echo ""
echo ""
echononl " Loading default 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
echo ""
echononl " Collect supported domains at this server.."
if [[ "$db_type" = "mysql" ]] ; then
domains=$(mysql $mysql_credential_args "$db_name" -N -s -e \
"SELECT domain FROM domain ORDER by domain" 2> "$log_messages")
else
domains=$(su - postgres -c"psql \"$db_name\" -t -q -c\"SELECT domain FROM domain ORDER by domain\"")
fi
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_messages")"
clean_up 1
fi
echo -e "\n\n \033[37m\033[1mMail Domains supported by this server ("$(hostname -f)"):\033[m\n"
echo "Mail Domains supported by this server ("$(hostname -f)"):" > $out_file
echo "" >> $out_file
for domain in $domains ; do
[[ "$domain" = "ALL" ]] && continue
echo -e " \033[32m$domain\033[m"
echo " $domain" >> $out_file
done
echo "" >> $out_file
echo ""
clean_up 0