summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: e1cb635b75e8cd8554e1b260c70e035d8f75eadb (plain)
  1. #!/bin/bash
  2. #
  3. # /etc/local-COMMON/postfix/postfix.sh
  4. # Copyright 2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: postfix.sh,v 1.8 2003-01-14 05:51:52 jonas Exp $
  7. #
  8. # Auto-tweak plain installed postfix Debian package
  9. #
  10. # TODO: Check for postfix 2.0 and include improve RBL logic with new
  11. # options reject_rhsbl_sender and default_rbl_reply
  12. #
  13. # TODO: Figure out a way to use chroot jail for TLS stuff.
  14. set -e
  15. paramdir='/etc/local-COMMON/postfix'
  16. confdir='/etc/postfix'
  17. sp='[[:space:]]'
  18. pf2=
  19. if postconf -d mail_version | grep -q '= 2'; then
  20. pf2=1
  21. fi
  22. function getlinesfromfile() {
  23. param="$1"
  24. echo -n "$param = "
  25. cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//' -e 's/[, ]\+/,/g' -e 's/,$//'
  26. }
  27. # Some badly configured setup use hostname instead of FQDN
  28. if /usr/sbin/postconf myhostname | grep '.' &> /dev/null; then
  29. /usr/sbin/postconf -e 'smtpd_helo_required = yes'
  30. fi
  31. /usr/sbin/postconf -e "`getlinesfromfile permit_mx_backup_networks`"
  32. if [ "$pf2" ]; then
  33. rbl_domains="`getlinesfromfile maps_rbl_domains | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  34. rbl_domain_line=
  35. for rbl_domain in $rbl_domains; do
  36. rbl_domain_line="${rbl_domain_line}reject_rbl_client $rbl_domain,"
  37. done
  38. recipient_restrictions="`getlinesfromfile smtpd_recipient_restrictions | sed 's/.*=[ ]*//' | sed \"s/reject_maps_rbl/$rbl_domain_line/\"`"
  39. /usr/sbin/postconf -e "maps_rbl_domains ="
  40. /usr/sbin/postconf -e "smtpd_recipient_restrictions = $recipient_restrictions"
  41. else
  42. /usr/sbin/postconf -e "`getlinesfromfile maps_rbl_domains`"
  43. /usr/sbin/postconf -e "`getlinesfromfile smtpd_recipient_restrictions`"
  44. fi
  45. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  46. # (change the test if using some other modules and avoid the plain ones)
  47. if [ -f /usr/lib/postfix/tlsmgr -a -f /usr/lib/sasl/libplain.so -a -f /etc/ssl/certs/postfix.pem ]; then
  48. mkdir -p $confdir/sasl
  49. echo 'pwcheck_method: pam' >$confdir/sasl/smtpd.conf
  50. echo 'auto_transition: false' >>$confdir/sasl/smtpd.conf
  51. groups postfix | grep shadow &>/dev/null || adduser postfix shadow
  52. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  53. cp -a $confdir/master.cf $confdir/master.cf.old
  54. cat $confdir/master.cf.old | sed \
  55. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  56. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  57. -e "s/^#\(tlsmgr$sp\)/\1/" \
  58. > $confdir/master.cf
  59. cat $confdir/master.cf | egrep "^tlsmgr$sp" > /dev/null || \
  60. echo 'tlsmgr fifo - - - 300 1 tlsmgr' >> $confdir/master.cf
  61. /usr/sbin/postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  62. if [ -f /etc/ssl/private/postfix.pem ]; then
  63. /usr/sbin/postconf -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  64. fi
  65. /usr/sbin/postconf -e 'smtpd_tls_loglevel = 1'
  66. /usr/sbin/postconf -e 'smtpd_use_tls = yes'
  67. /usr/sbin/postconf -e 'smtpd_tls_session_cache_database = sdbm:/etc/postfix/smtpd_scache'
  68. /usr/sbin/postconf -e 'smtpd_tls_auth_only = yes'
  69. /usr/sbin/postconf -e 'smtpd_sasl_auth_enable = no'
  70. /usr/sbin/postconf -e 'smtpd_sasl_security_options = noanonymous'
  71. /usr/sbin/postconf -e 'smtpd_sasl_local_domain = $myhostname'
  72. /usr/sbin/postconf -e 'smtpd_tls_received_header = yes'
  73. /usr/sbin/postconf -e 'broken_sasl_auth_clients = yes'
  74. /usr/sbin/postconf -e 'tls_random_source = dev:/dev/urandom'
  75. /usr/sbin/postconf -e 'tls_daemon_random_source = dev:/dev/urandom'
  76. # Check if using a proper key exists (not just a self-signed one)
  77. # (it is assumed that a CA certificate is made public if used!)
  78. if [ -f /etc/ssl/certs/cacert.pem ]; then
  79. /usr/sbin/postconf -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  80. # Client side TLS only makes sense if a publicly available certificate is available
  81. # (and DON'T publish a self-signed certificate!)
  82. /usr/sbin/postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/cacert.pem'
  83. /usr/sbin/postconf -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  84. if [ -f /etc/ssl/private/postfix.pem ]; then
  85. /usr/sbin/postconf -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  86. fi
  87. /usr/sbin/postconf -e 'smtp_tls_loglevel = 1'
  88. /usr/sbin/postconf -e 'smtp_use_tls = yes'
  89. /usr/sbin/postconf -e 'smtp_tls_CApath = /etc/ssl/certs'
  90. /usr/sbin/postconf -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  91. /usr/sbin/postconf -e 'smtp_tls_session_cache_database = sdbm:/etc/postfix/smtp_scache'
  92. # This makes Netscape ask for a certificate, so make sure it IS public!
  93. /usr/sbin/postconf -e 'smtpd_tls_ask_ccert = yes'
  94. fi
  95. else
  96. echo 'TLS not activated - check the script for requirements...'
  97. fi
  98. /etc/init.d/postfix reload
  99. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  100. # Support for trusted MX backup networks added
  101. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  102. # RBLs replaced with those recommended by http://www.antispews.org/
  103. # Here's a convenient overview of different blackholes:
  104. # http://rbls.org/
  105. # smtpd_tls_CAfile