2
0

mailcow-setup-relayhost.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #/bin/bash
  2. if [[ ! -f mailcow.conf ]]; then
  3. echo "Cannot find mailcow.conf, make sure this script is run from within the mailcow folder."
  4. exit 1
  5. fi
  6. echo -n "Checking Postfix service... "
  7. docker-compose ps -q postfix-mailcow > /dev/null 2>&1
  8. if [[ $? -ne 0 ]]; then
  9. echo "failed"
  10. echo "Postfix (postifx-mailcow) is not up and running, exiting..."
  11. exit 1
  12. fi
  13. echo "OK"
  14. if [[ -z ${1} ]]; then
  15. echo "Usage:"
  16. echo
  17. echo "Setup a relayhost:"
  18. echo "${0} relayhost port (username) (password)"
  19. echo "Username and password are optional parameters."
  20. echo
  21. echo "Reset to defaults:"
  22. echo "${0} reset"
  23. exit 1
  24. fi
  25. if [[ ${1} == "reset" ]]; then
  26. # Reset modified values to their defaults
  27. sed -i "s/^relayhost\ \=.*/relayhost\ \=/" data/conf/postfix/main.cf
  28. sed -i "s/^smtp\_sasl\_password\_maps.*/smtp\_sasl\_password\_maps\ \=/" data/conf/postfix/main.cf
  29. sed -i "s/^smtp\_sasl\_security\_options.*/smtp\_sasl\_security\_options\ \=\ noplaintext\,\ noanonymous/" data/conf/postfix/main.cf
  30. sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ no/" data/conf/postfix/main.cf
  31. # Also delete the plaintext password file
  32. rm -f data/conf/postfix/smarthost_passwd*
  33. docker-compose exec postfix-mailcow postfix reload
  34. # Exit with dc exit code
  35. exit $?
  36. else
  37. # Try a simple connection to host:port but don't recieve any data
  38. # Abort after 3 seconds
  39. if ! nc -z -v -w3 ${1} ${2} 2>/dev/null; then
  40. echo "Connection to relayhost ${1} failed, aborting..."
  41. exit 1
  42. fi
  43. # Use exact hostname as relayhost, don't lookup the MX record of relayhost
  44. sed -i "s/relayhost\ \=.*/relayhost\ \=\ \[${1}\]\:${2}/" data/conf/postfix/main.cf
  45. if grep -q "smtp_sasl_password_maps" data/conf/postfix/main.cf
  46. then
  47. sed -i "s/^smtp\_sasl\_password\_maps.*/smtp\_sasl\_password\_maps\ \=\ hash\:\/opt\/postfix\/conf\/smarthost\_passwd/" data/conf/postfix/main.cf
  48. else
  49. echo "smtp_sasl_password_maps = hash:/opt/postfix/conf/smarthost_passwd" >> data/conf/postfix/main.cf
  50. fi
  51. if grep -q "smtp_sasl_auth_enable" data/conf/postfix/main.cf
  52. then
  53. sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ yes/" data/conf/postfix/main.cf
  54. else
  55. echo "smtp_sasl_auth_enable = yes" >> data/conf/postfix/main.cf
  56. fi
  57. if grep -q "smtp_sasl_security_options" data/conf/postfix/main.cf
  58. then
  59. sed -i "s/^smtp\_sasl\_security\_options.*/smtp\_sasl\_security\_options\ \=/" data/conf/postfix/main.cf
  60. else
  61. echo "smtp_sasl_security_options =" >> data/conf/postfix/main.cf
  62. fi
  63. if [[ ! -z ${3} ]]; then
  64. echo ${1} ${3}:${4} > data/conf/postfix/smarthost_passwd
  65. docker-compose exec postfix-mailcow postmap /opt/postfix/conf/smarthost_passwd
  66. fi
  67. docker-compose exec postfix-mailcow chown root:postfix /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db
  68. docker-compose exec postfix-mailcow chmod 660 /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db
  69. docker-compose exec postfix-mailcow postfix reload
  70. exit $?
  71. fi