diff --git a/dovecot_convert_to_lmtp.sh b/dovecot_convert_to_lmtp.sh new file mode 100755 index 0000000..db7e729 --- /dev/null +++ b/dovecot_convert_to_lmtp.sh @@ -0,0 +1,270 @@ +#!/usr/bin/env bash + +postfix_main_cf="/etc/postfix/main.cf" + +backup_date="$(date +%Y-%m-%d-%H%M)" +changed=false + +log_file="$(mktemp)" + +# ------------- +# - Functions an Variable +# ------------- +clean_up() { + + + if [[ -f "$_backup_crontab_file" ]]; then + + echononl "(Re)Install previously saved crontab from '$_backup_crontab_file'.." + + crontab $_backup_crontab_file >> $log_file 2>&1 + + if [[ $? -eq 0 ]]; then + echo -e "$rc_done" + else + echo -e "$rc_failed" + error "$(cat $log_file)" + fi + + fi + + # Perform program exit housekeeping + rm -f $log_file + blank_line + exit $1 +} + +fatal(){ + echo "" + echo -e "\t[ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m" + echo "" + echo -e "\t\033[31m\033[1m Skript wird abgebrochen\033[m\033[m\n" + rm -f $log_file + clean_up 1 +} + + +echononl(){ + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e "$*\\c" 1>&2 + else + echo -en "$*" 1>&2 + fi + rm /tmp/shprompt$$ +} +error(){ + echo "" + echo -e "\t[ \033[31m\033[1mError\033[m ]: $*" + echo "" +} + +warn(){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info(){ + echo "" + echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" + echo "" +} + +echo_done() { + echo -e "\033[75G[ \033[32mdone\033[m ]" +} +echo_ok() { + echo -e "\033[75G[ \033[32mok\033[m ]" +} +echo_warn() { + echo -e "\033[75G[ \033[33mwarn\033[m ]" +} +echo_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + + +if [[ ! -f "$postfix_main_cf" ]] ; then + fatal "Postfix configuration '${postfix_main_cf}' NOT found!" +fi + +# ---------- +# - Some checks .. +# ---------- +# -Is systemd supported on this system? +# - +systemd_supported=false +systemd=$(which systemd) +systemctl=$(which systemctl) + +if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then + systemd_supported=true +fi + +blank_line + +echononl " Backup Postfix Cconfiguration file '${postfix_main_cf}'.." +if [[ ! -f "${postfix_main_cf}.${backup_date}" ]] ; then + cp -a "${postfix_main_cf}" "${postfix_main_cf}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + + + +blank_line + +# - /etc/postfix/main.cf +# - +# - comment in: +# - #virtual_transport = dovecot +# - #dovecot_destination_recipient_limit = .. +# - +# - change: +# - smtpd_sasl_auth_enable = yes +# - smtpd_sasl_type = dovecot +# - smtpd_sasl_path = private/dovecot-auth +# - virtual_transport = lmtp:unix:private/dovecot-lmtp +var="smtpd_sasl_auth_enable" +val="yes" +echononl " ${postfix_main_cf}: adjust '${val}'.." +if ! $(grep -E -q "^\s*${var}\s*=\s*${val}" ${postfix_main_cf} 2> /dev/null) ; then + perl -i -n -p -e "s#^(\s*)(smtpd_sasl_type\ *=.*)#smtpd_sasl_type = dovecot#" \ + /etc/postfix/main.cf > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + changed=true + else + echo_failed + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + +var="smtpd_sasl_type" +val="dovecot" +echononl " ${postfix_main_cf}: adjust '${val}'.." +if ! $(grep -E -q "^\s*${var}\s*=\s*${val}" ${postfix_main_cf} 2> /dev/null) ; then + perl -i -n -p -e "s#^(\s*)(smtpd_sasl_type\ *=.*)#smtpd_sasl_type = dovecot#" \ + /etc/postfix/main.cf > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + changed=true + else + echo_failed + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + +var="smtpd_sasl_path" +val="private/dovecot-auth" +echononl " ${postfix_main_cf}: adjust '${val}'.." +if ! $(grep -E -q "^\s*${var}\s*=\s*${val}" ${postfix_main_cf} 2> /dev/null) ; then + perl -i -n -p -e "s#^(\s*)(smtpd_sasl_type\ *=.*)#smtpd_sasl_type = dovecot#" \ + /etc/postfix/main.cf > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + changed=true + else + echo_failed + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + +var="virtual_transport" +val="lmtp:unix:private/dovecot-lmtp" +echononl " ${postfix_main_cf}: adjust '${val}'.." +if ! $(grep -E -q "^\s*${var}\s*=\s*${val}" ${postfix_main_cf} 2> /dev/null) ; then + perl -i -n -p -e "s#^(\s*)(smtpd_sasl_type\ *=.*)#smtpd_sasl_type = dovecot#" \ + /etc/postfix/main.cf > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + changed=true + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + + +echononl " Comment variable 'dovecot_destination_recipient_limit'.." +if $(grep -E -q "^\s*dovecot_destination_recipient_limit" ${postfix_main_cf} 2> /dev/null) ; then + perl -i-n -p -e "s/^(\s*)(dovecot_destination_recipient_limit.*)/\1\#\2/" /etc/postfix/main.cf > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + changed=true + else + echo_failed + error "$(cat "$log_file")" + fi +else + echo_skipped +fi + +blank_line + +# - restart postfix +# - +echononl " Restart Postfix Service..." +if $changed ; then + if $systemd_supported ; then + systemctl restart postfix > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + fi + else + /etc/init.d/postfix restart > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + fi + fi +else + echo_skipped +fi + + +diff "${postfix_main_cf}" "${postfix_main_cf}.${backup_date}" > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + + info "File \033[1m${postfix_main_cf}\033[m has not changed. + \033[32mGoing to remove previos created backup\033[m.." + + echononl " Remove file \033[1m${postfix_main_cf}.${backup_date}\033[m .." + rm "${postfix_main_cf}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat "$log_file")" + fi +fi + + +clean_up 0