From c2410a6004431abb63a9e3d79e83a67087ff6e5c Mon Sep 17 00:00:00 2001 From: andryyy Date: Sun, 23 Apr 2017 19:38:04 +0200 Subject: [PATCH] Move helper scripts, mark executable --- mailcow-reset-admin.sh | 36 ++++++++++++++++++ mailcow-setup-relayhost.sh | 76 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100755 mailcow-reset-admin.sh create mode 100755 mailcow-setup-relayhost.sh diff --git a/mailcow-reset-admin.sh b/mailcow-reset-admin.sh new file mode 100755 index 00000000..7ce1def8 --- /dev/null +++ b/mailcow-reset-admin.sh @@ -0,0 +1,36 @@ +#/bin/bash +if [[ ! -f mailcow.conf ]]; then + echo "Cannot find mailcow.conf, make sure this script is run from within the mailcow folder." + exit 1 +fi + +echo -n "Checking MySQL service... " +docker-compose ps -q mysql-mailcow > /dev/null 2>&1 + +if [[ $? -ne 0 ]]; then + echo "failed" + echo "MySQL (mysql-mailcow) is not up and running, exiting..." + exit 1 +fi + +echo "OK" +read -r -p "Are you sure you want to reset the mailcow administrator account? [y/N] " response +response=${response,,} # tolower +if [[ "$response" =~ ^(yes|y)$ ]]; then + echo -e "\nWorking, please wait..." + source mailcow.conf + docker-compose exec -T mysql-mailcow mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM admin;" + docker-compose exec -T mysql-mailcow mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "INSERT INTO admin (username, password, superadmin, created, modified, active) VALUES ('admin', '{SSHA256}K8eVJ6YsZbQCfuJvSUbaQRLr0HPLz5rC9IAp0PAFl0tmNDBkMDc0NDAyOTAxN2Rk', 1, NOW(), NOW(), 1);" + docker-compose exec -T mysql-mailcow mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM domain_admins WHERE username='admin';" + docker-compose exec -T mysql-mailcow mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "INSERT INTO domain_admins (username, domain, created, active) VALUES ('admin', 'ALL', NOW(), 1);" + docker-compose exec -T mysql-mailcow mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM tfa WHERE username='admin';" + echo " +Reset credentials: +--- +Username: admin +Password: moohoo +TFA: none +" +else + echo "Operation canceled." +fi diff --git a/mailcow-setup-relayhost.sh b/mailcow-setup-relayhost.sh new file mode 100755 index 00000000..6a7f6410 --- /dev/null +++ b/mailcow-setup-relayhost.sh @@ -0,0 +1,76 @@ +#/bin/bash +if [[ ! -f mailcow.conf ]]; then + echo "Cannot find mailcow.conf, make sure this script is run from within the mailcow folder." + exit 1 +fi + +echo -n "Checking Postfix service... " +docker-compose ps -q postfix-mailcow > /dev/null 2>&1 + +if [[ $? -ne 0 ]]; then + echo "failed" + echo "Postfix (postifx-mailcow) is not up and running, exiting..." + exit 1 +fi + +echo "OK" + +if [[ -z ${1} ]]; then + echo "Usage:" + echo + echo "Setup a relayhost:" + echo "./${0} relayhost port (username) (password)" + echo "Username and password are optional parameters." + echo + echo "Reset to defaults:" + echo "./${0} reset" + exit 1 +fi + +if [[ ${1} == "reset" ]]; then + # Reset modified values to their defaults + sed -i "s/^relayhost\ \=.*/relayhost\ \=/" data/conf/postfix/main.cf + sed -i "s/^smtp\_sasl\_password\_maps.*/smtp\_sasl\_password\_maps\ \=/" data/conf/postfix/main.cf + sed -i "s/^smtp\_sasl\_security\_options.*/smtp\_sasl\_security\_options\ \=\ noplaintext\,\ noanonymous/" data/conf/postfix/main.cf + sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ no/" data/conf/postfix/main.cf + # Also delete the plaintext password file + rm -f data/conf/postfix/smarthost_passwd* + docker-compose exec postfix-mailcow postfix reload + # Exit with dc exit code + exit $? +else + # Try a simple connection to host:port but don't recieve any data + # Abort after 3 seconds + if ! nc -z -v -w3 ${1} ${2} 2>/dev/null; then + echo "Connection to relayhost ${1} failed, aborting..." + exit 1 + fi + # Use exact hostname as relayhost, don't lookup the MX record of relayhost + sed -i "s/relayhost\ \=.*/relayhost\ \=\ \[${1}\]\:${2}/" data/conf/postfix/main.cf + if grep -q "smtp_sasl_password_maps" data/conf/postfix/main.cf + then + sed -i "s/^smtp\_sasl\_password\_maps.*/smtp\_sasl\_password\_maps\ \=\ hash\:\/opt\/postfix\/conf\/smarthost\_passwd/" data/conf/postfix/main.cf + else + echo "smtp_sasl_password_maps = hash:/opt/postfix/conf/smarthost_passwd" >> data/conf/postfix/main.cf + fi + if grep -q "smtp_sasl_auth_enable" data/conf/postfix/main.cf + then + sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ yes/" data/conf/postfix/main.cf + else + echo "smtp_sasl_auth_enable = yes" >> data/conf/postfix/main.cf + fi + if grep -q "smtp_sasl_security_options" data/conf/postfix/main.cf + then + sed -i "s/^smtp\_sasl\_security\_options.*/smtp\_sasl\_security\_options\ \=/" data/conf/postfix/main.cf + else + echo "smtp_sasl_security_options =" >> data/conf/postfix/main.cf + fi + if [[ ! -z ${3} ]]; then + echo ${1} ${3}:${4} > data/conf/postfix/smarthost_passwd + docker-compose exec postfix-mailcow postmap /opt/postfix/conf/smarthost_passwd + fi + docker-compose exec postfix-mailcow chown root:postfix /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db + docker-compose exec postfix-mailcow chmod 660 /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db + docker-compose exec postfix-mailcow postfix reload + exit $? +fi