mailcow-setup-relayhost.sh 4.0 KB

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