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