diff --git a/conf/get_mail_domains.conf.sample b/conf/get_mail_domains.conf.sample new file mode 100644 index 0000000..4d2b6a1 --- /dev/null +++ b/conf/get_mail_domains.conf.sample @@ -0,0 +1,40 @@ +# ---------------------------------------------------- +# --- +# - Parameter Settings for script 'get_mail_domains.sh'. +# --- +# ---------------------------------------------------- + + +# - db_type +# - +# - Type of Postfix Database +# - +# - Possible values are 'pgsql' (PostgeSQL) or 'mysql' (MySQL) +# - +# - Defaults to: db_type="pgsql" +# - +#db_type="pgsql" + +# - db_name +# - +# - Database name for the postfix database +# - +# - Defaults to: db_name="postfix" +# - +#db_name="postfix" + +# - mysql_credential_args (root access to MySQL Database) +# - +# - Only used if db_type=mysql +# - +# - 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" +# - +# - Defaults to: +# - '/etc/mysql/debian.cnf' if MySQL is installed from debian package system +# - '/usr/local/mysql/sys-maint.cnf' otherwise +# - +#mysql_credential_args="" + diff --git a/get_mail_domains.sh b/get_mail_domains.sh index 2bf5fa5..54c06c0 100755 --- a/get_mail_domains.sh +++ b/get_mail_domains.sh @@ -1,18 +1,161 @@ #!/bin/sh -mysql=false -mysql_credentilas="--login-path=local" -mysql_db=postfix +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 .." +clear +echo "" +echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .." +echo "" echo "" -if $mysql ; then - domains=$(mysql $mysql_credentilas $mysql_db -N -s -e "SELECT domain FROM domain ORDER by domain") +echo "" +echononl " Loading default Configuration values from $(basename ${conf_file}).." +if [[ ! -f "$conf_file" ]]; then + echo_skipped else - domains=$(su - postgres -c"psql postfix -t -q -c\"SELECT domain FROM domain ORDER by domain\"") + source "${conf_file}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi fi -echo -e "$domains" + +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 $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 -exit 0 +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