summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: e863fa24ae86cc1274851139e3bbeb9f526fae36 (plain)
  1. #!/bin/bash
  2. set -e
  3. paramdir='/etc/local-COMMON/postfix'
  4. confdir='/etc/postfix'
  5. sp='[[:space:]]'
  6. function getlinesfromfile() {
  7. param="$1"
  8. echo -n "$param = "
  9. cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//' -e 's/[, ]\+/,/g' -e 's/,$//'
  10. }
  11. # Some badly configured setup use hostname instead of FQDN
  12. if postconf myhostname | grep '.' &> /dev/null; then
  13. postconf -e 'smtpd_helo_required = yes'
  14. fi
  15. postconf -e "`getlinesfromfile permit_mx_backup_networks`"
  16. postconf -e "`getlinesfromfile maps_rbl_domains`"
  17. postconf -e "`getlinesfromfile smtpd_recipient_restrictions`"
  18. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  19. # (change the test if using some other modules and avoid the plain ones)
  20. if dpkg -L libsasl-modules-plain &> /dev/null && [ -f /etc/ssl/certs/postfix.pem ]; then
  21. mkdir -p $confdir/sasl
  22. echo 'pwcheck_method: pam' >$confdir/sasl/smtpd.conf
  23. echo 'auto_transition: false' >>$confdir/sasl/smtpd.conf
  24. groups postfix | grep shadow &>/dev/null || adduser postfix shadow
  25. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  26. cp -a $confdir/master.cf $confdir/master.cf.old
  27. cat $confdir/master.cf.old | sed \
  28. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  29. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  30. -e "s/^#\(tlsmgr$sp\)/\1/" \
  31. > $confdir/master.cf
  32. cat $confdir/master.cf | egrep "^tlsmgr$sp" > /dev/null || \
  33. echo 'tlsmgr fifo - - - 300 1 tlsmgr' >> $confdir/master.cf
  34. postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  35. if [ -f /etc/ssl/private/postfix.pem ]; then
  36. postconf -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  37. fi
  38. postconf -e 'smtpd_tls_loglevel = 1'
  39. postconf -e 'smtpd_use_tls = yes'
  40. postconf -e 'smtpd_tls_session_cache_database = sdbm:/etc/postfix/smtpd_scache'
  41. postconf -e 'smtpd_tls_auth_only = yes'
  42. postconf -e 'smtpd_sasl_auth_enable = no'
  43. postconf -e 'smtpd_sasl_security_options = noanonymous'
  44. postconf -e 'smtpd_sasl_local_domain = $myhostname'
  45. postconf -e 'smtpd_tls_received_header = yes'
  46. postconf -e 'broken_sasl_auth_clients = yes'
  47. postconf -e 'tls_random_source = dev:/dev/urandom'
  48. postconf -e 'tls_daemon_random_source = dev:/dev/urandom'
  49. # Check if using a proper key exists (not just a self-signed one)
  50. # (it is assumed that a CA certificate is made public if used!)
  51. if [ -f /etc/ssl/certs/cacert.pem ]; then
  52. postconf -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  53. postconf -e 'smtp_tls_CAfile = $smtpd_tls_CAfile'
  54. postconf -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  55. # Client side TLS only makes sense if a publicly available certificate is available
  56. # (and DON'T publish a self-signed certificate!)
  57. if [ -f /etc/ssl/private/postfix.pem ]; then
  58. postconf -e 'smtp_tls_key_file = $smtpd_tls_key_file'
  59. fi
  60. postconf -e 'smtp_tls_loglevel = 1'
  61. postconf -e 'smtp_use_tls = yes'
  62. postconf -e 'smtp_tls_CApath = /etc/ssl/certs'
  63. # postconf -e 'smtp_tls_note_starttls_offer = yes' # Useful when collecting info for smtp_tls_per_site option
  64. postconf -e 'smtp_tls_session_cache_database = sdbm:/etc/postfix/smtp_scache'
  65. # This makes Netscape ask for a certificate, so make sure it IS public!
  66. postconf -e 'smtpd_tls_ask_ccert = yes'
  67. fi
  68. else
  69. echo 'TLS not activated - check the script for requirements...'
  70. fi
  71. /etc/init.d/postfix reload
  72. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  73. # Support for trusted MX backup networks added
  74. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  75. # RBLs replaced with those recommended by http://www.antispews.org/
  76. # Here's a convenient overview of different blackholes:
  77. # http://rbls.org/
  78. # smtpd_tls_CAfile