Script sent_userinfo_postfix.sh: add support for MySQL database.

This commit is contained in:
Christoph 2017-11-02 02:17:51 +01:00
parent 4a923a025f
commit a82069ec6c
2 changed files with 95 additions and 17 deletions

View File

@ -22,14 +22,46 @@
# -
# - Example: 'oo@oopen.de'
# -
#email_from=""
email_from=""
# - email_from_org
# -
# - Example: email_from_org="O.OPEN"
# -
#email_from_org=""
email_from_org=""
# - 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)
# -
# - 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=""
# - mail_user

View File

@ -14,6 +14,8 @@ logfile="${script_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log"
#---------------------------------------
DEFAULT_user_info_file="${conf_dir}/sent_userinfo_postfix.email"
DEFAULT_db_type="pgsql"
DEFAULT_db_name="postfix"
DEFAULT_mail_user="vmail"
DEFAULT_mail_group="vmail"
@ -49,7 +51,7 @@ warn (){
fatal(){
echo ""
echo -e "[ \033[31m\033[1mFehler\033[m ]: $*"
echo -e "\n\t Script was interupted!\n"
echo -e "\n Script was interupted!\n"
echo
clean_up 1
}
@ -94,6 +96,26 @@ fi
[[ -n "$user_info_file" ]] || user_info_file="$DEFAULT_user_info_file"
[[ -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
[[ -n "$mail_user" ]] || mail_user="$DEFAULT_mail_user"
[[ -n "$mail_group" ]] || mail_group="$DEFAULT_mail_group"
@ -150,24 +172,30 @@ if $TEST_RUN ; then
fi
echo ""
echo ""
echo -e "\033[32mSettings for script \033[37m\033[1msent_userinfo_postfix.sh\033[m"
echo ""
echo " File containing the mail-body.....: $user_info_file"
echo " File containing the mail-body.........: $user_info_file"
echo ""
echo " Mail Sender Address...............: $email_from"
echo " Mail Sender Organisation..........: $email_from_org"
if [[ "$db_type" = "pgsql" ]] ; then
echo " Type of postfix databae...............: PostgreSQL ($db_type)"
echo " Name of Postfix Database..............: $db_name"
elif [[ "$db_type" = "mysql" ]] ; then
echo " Type of postfix databae...............: MySQL ($db_type)"
echo " Name of Postfix Database..............: $db_name"
echo " MySQL credential args.................: $mysql_credential_args"
fi
echo ""
echo " Mail User.........................: $mail_user"
echo " Mail Group........................: $mail_group"
echo " Mail Sender Address...................: $email_from"
echo " Mail Sender Organisation..............: $email_from_org"
echo ""
echo " Test Run..........................: $TEST_RUN"
echo " Mail User.............................: $mail_user"
echo " Mail Group............................: $mail_group"
echo ""
echo " Test Run..............................: $TEST_RUN"
if $TEST_RUN ; then
echo " Recipient address for test run....: $test_email"
echo " Recipient address for test run........: $test_email"
fi
echo ""
@ -234,8 +262,15 @@ if $TEST_RUN ; then
fatal "The given recipient address (${test_email}) for test run does not look like a correct e-mail address!"
fi
domains="${test_email##*@}"
else
domains=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain\""`
elif [[ "$db_type" = "pgsql" ]] ; then
domains=`su postgres -c"psql -At -F ' ' $db_name -c\"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain\""`
elif [[ "$db_type" = "mysql" ]] ; then
domains="$(mysql "$mysql_credential_args" "$db_name" \
-N -s -e"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain")"
fi
if [[ -z "$domains" ]] ; then
fatal "No mail domains found!"
fi
declare -i num_dom=0;
@ -250,9 +285,19 @@ for domain in $domains ;do
echo -e "\nDOMAIN: $domain\n" | tee -a $logfile
if $TEST_RUN ; then
local_parts="${test_email%%@*}"
else
elif [[ "$db_type" = "pgsql" ]] ; then
local_parts=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT local_part FROM mailbox WHERE domain = '$domain'\""`
elif [[ "$db_type" = "mysql" ]] ; then
local_parts="$(mysql "$mysql_credential_args" "$db_name" \
-N -s -e"SELECT local_part FROM mailbox WHERE domain = '$domain'")"
fi
if [[ -z "$local_parts" ]] ; then
warn "No mailbox found for domain '$domain'!"
echo " [ WARNING ]: No mailbox found for domain '$domain'" >> $logfile
continue
fi
for local_part in $local_parts ; do
cp "$user_info_file" "$tmp_dir"
perl -i -n -p -e "s/%email_to%/$local_part\@$domain/" "${tmp_dir}/$(basename $user_info_file)"
@ -263,9 +308,10 @@ for domain in $domains ;do
if [ "$?" = "0" ]; then
num_mbox=num_mbox+1
echo -e "$rc_done"
echo " [ OK ]: Send userinfo to $local_part@$domain" >> $logfile
else
echo -e "$rc_failed"
echo " [ERROR]: Cannot sent userinfo to \"$local_part\@$domain\"!" >> $logfile
echo " [ ERROR ]: Cannot sent userinfo to \"$local_part\@$domain\"!" >> $logfile
num_mbox_failed=num_mbox_failed+1
fi
done