mailcow-setup-relayhost.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # Postfix smtp_tls_security_level should be set to "may" to try an
  3. # encrypted connection.
  4. if [ "$EUID" -ne 0 ]
  5. then echo "Please run as root"
  6. exit 1
  7. fi
  8. # move into mailcow-dockerized base directory
  9. cd ../../
  10. if [[ ${1} == "reset" ]]; then
  11. # Reset modified values to their defaults
  12. sed -i "s/^relayhost\ \=.*/relayhost\ \=/" data/conf/postfix/main.cf
  13. sed -i "s/^smtp\_sasl\_password\_maps.*/smtp\_sasl\_password\_maps\ \=/" data/conf/postfix/main.cf
  14. sed -i "s/^smtp\_sasl\_security\_options.*/smtp\_sasl\_security\_options\ \=\ noplaintext\,\ noanonymous/" data/conf/postfix/main.cf
  15. sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ no/" data/conf/postfix/main.cf
  16. # Also delete the plaintext password file
  17. rm -f data/conf/postfix/smarthost_passwd*
  18. docker-compose exec postfix-mailcow postfix reload
  19. # Exit last exit code
  20. exit $?
  21. elif [[ ${1} == "restore-string" ]]; then
  22. # Set parameter value of smtp_sasl_password_maps
  23. SMTPSASLPWDMAP="data/conf/postfix/smarthost_passwd"
  24. # Get parameter value of relayhost
  25. RELAYHOSTCFG=$(grep "relayhost\ =" data/conf/postfix/main.cf | awk '{print $3}')
  26. # Exit if empty/unset
  27. [[ -z ${RELAYHOSTCFG} ]] && exit 0
  28. # Replace ':' by ' ' (white space)
  29. RELAYHOSTCFG=${RELAYHOSTCFG//\:/ }
  30. # Replace '[' by '' (empty)
  31. RELAYHOSTCFG=${RELAYHOSTCFG//\[/}
  32. # Replace ']' by '' (empty) and create array of result
  33. RELAYHOSTCFGARR=(${RELAYHOSTCFG//\]/})
  34. # Get 'username:password' from SASL password maps
  35. # Grep relayhost without port and '[', ']' or ':' from SASL password map file without map type (e.g. 'hash:')
  36. USRPWD=$(grep ${RELAYHOSTCFGARR[0]} $SMTPSASLPWDMAP | awk {'print $2'})
  37. # Replace ':' by ' ' and create array of result
  38. USRPWDARR=(${USRPWD//:/ })
  39. # Echo script name, all values in RELAYHOSTCFGARR, first and second value in USRPWDARR
  40. # Why?
  41. # Host and port are required, so we can print the whole array RELAYHOSTCFGARR.
  42. # Password might be empty, so we print them separately.
  43. echo ${0} ${RELAYHOSTCFGARR[@]} \'${USRPWDARR[0]}\' \'${USRPWDARR[1]}\'
  44. exit 0
  45. elif [[ -z ${1} ]] || [[ -z ${2} ]]; then
  46. # Exit with code 1 if host and port are missing
  47. echo "Usage: ${0} relayhost port (username) (password)"
  48. echo "Username and password are optional parameters."
  49. exit 1
  50. else
  51. # Try a simple connection to host:port but don't recieve any data
  52. # Abort after 3 seconds
  53. if ! nc -z -v -w3 ${1} ${2} 2>/dev/null; then
  54. echo "Connection to relayhost ${1} failed, aborting..."
  55. exit 1
  56. fi
  57. # Use exact hostname as relayhost, don't lookup the MX record of relayhost
  58. sed -i "s/relayhost\ \=.*/relayhost\ \=\ \[${1}\]\:${2}/" data/conf/postfix/main.cf
  59. if grep -q "smtp_sasl_password_maps" data/conf/postfix/main.cf
  60. then
  61. sed -i "s/^smtp\_sasl\_password\_maps.*/smtp_sasl\_password\_maps\ \=\ hash\:\/opt\/postfix\/conf\/smarthost\_passwd/" data/conf/postfix/main.cf
  62. else
  63. echo "smtp_sasl_password_maps = hash:/opt/postfix/conf/smarthost_passwd" >> data/conf/postfix/main.cf
  64. fi
  65. if grep -q "smtp_sasl_auth_enable" data/conf/postfix/main.cf
  66. then
  67. sed -i "s/^smtp\_sasl\_auth\_enable.*/smtp\_sasl\_auth\_enable\ \=\ yes/" data/conf/postfix/main.cf
  68. else
  69. echo "smtp_sasl_auth_enable = yes" >> data/conf/postfix/main.cf
  70. fi
  71. docker-compose exec postfix-mailcow postconf -e "smtp_sasl_password_maps = hash:/opt/postfix/conf/smarthost_passwd"
  72. # We can use anonymous and plain-text authentication, too (be warned)
  73. docker-compose exec postfix-mailcow postconf -e "smtp_sasl_security_options = "
  74. docker-compose exec postfix-mailcow postconf -e "smtp_sasl_auth_enable = yes"
  75. if [[ ! -z ${3} ]]; then
  76. echo ${1} ${3}:${4} > data/conf/postfix/smarthost_passwd
  77. docker-compose exec postfix-mailcow postmap /opt/postfix/conf/smarthost_passwd
  78. fi
  79. docker-compose exec postfix-mailcow chown root:postfix /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db
  80. docker-compose exec postfix-mailcow chmod 660 /opt/postfix/conf/smarthost_passwd /opt/postfix/conf/smarthost_passwd.db
  81. docker-compose exec postfix-mailcow postfix reload
  82. exit $?
  83. fi