summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: c5a554689be4310686fb9df0813758f8bd768c2d (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.24 2004-02-03 22:16:17 jonas Exp $
  7. #
  8. # Auto-tweak plain installed postfix Debian package
  9. #
  10. # TODO:
  11. # * Implement stuff from here: http://www.wsrcc.com/spam/
  12. # * Implement stuff from here: http://www.muine.org/~hoang/postfix.html
  13. # * Implement stuff from here: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  14. # * Figure out a way to use chroot jail for TLS stuff.
  15. # * Resolve 1.x/2.x differences in RBL map expansion within getlinesfromfile()
  16. set -e
  17. paramdir='/etc/local-COMMON/postfix'
  18. confdir='/etc/postfix'
  19. postconf=/usr/sbin/postconf
  20. sp='[[:space:]]'
  21. pf2=
  22. if $postconf -d mail_version | grep -q '= 2'; then
  23. pf2=1
  24. fi
  25. #function getlinesfromfile() {
  26. # param="$1"
  27. # echo -n "$param = "
  28. # cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//' -e 's/[, ]\+/,/g' -e 's/,$//'
  29. #}
  30. function getlinesfromfile() {
  31. param="$1"
  32. shift
  33. replacements=
  34. for subparam in $@; do
  35. case "$subparam" in
  36. *=*=*)
  37. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  38. newparam="`echo $subparam | awk -F= '{print $2}'`"
  39. newparamfile="`echo $subparam | awk -F= '{print $3}'`"
  40. shift
  41. ;;
  42. *)
  43. oldparam=$subparam
  44. newparam=$subparam
  45. newparamfile=$subparam
  46. shift
  47. ;;
  48. esac
  49. newparamvalues="`getlinesfromfile $newparamfile | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  50. newstring=
  51. for newparamvalue in $newparamvalues; do
  52. newstring="${newstring}$newparam $newparamvalue,"
  53. done
  54. replacements="$replacements;s/$oldparam/$newstring/"
  55. done
  56. echo -n "$param = "
  57. cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e "s/,\$//$replacements"
  58. }
  59. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  60. $postconf -e 'smtpd_banner = $myhostname NO UCE ESMTP $mail_name (Debian/GNU)'
  61. # Some badly configured setup use hostname instead of FQDN
  62. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  63. #if $postconf myhostname | grep -q '\.'; then
  64. # $postconf -e 'smtpd_helo_required = yes'
  65. #fi
  66. $postconf -e 'smtpd_helo_required = no'
  67. $postconf -e "`getlinesfromfile permit_mx_backup_networks`"
  68. if [ "$pf2" ]; then
  69. $postconf -e "maps_rbl_domains ="
  70. $postconf -e "`getlinesfromfile smtpd_client_restrictions reject_rhsbl_client`"
  71. $postconf -e "`getlinesfromfile smtpd_helo_restrictions`"
  72. $postconf -e "`getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender`"
  73. $postconf -e "`getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains`"
  74. $postconf -e "`getlinesfromfile smtpd_data_restrictions`"
  75. else
  76. $postconf -e "`getlinesfromfile maps_rbl_domains`"
  77. $postconf -e "`getlinesfromfile smtpd_helo_restrictions`"
  78. $postconf -e "`getlinesfromfile smtpd_recipient_restrictions`"
  79. fi
  80. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  81. # (change the test if using some other modules and avoid the plain ones)
  82. if [ -f /usr/lib/postfix/tlsmgr -a -f /usr/lib/sasl2/libplain.so -a -f /etc/ssl/certs/postfix.pem ]; then
  83. mkdir -p $confdir/sasl
  84. echo 'pwcheck_method: pam' >$confdir/sasl/smtpd.conf
  85. echo 'auto_transition: false' >>$confdir/sasl/smtpd.conf
  86. groups postfix | grep -q shadow || adduser postfix shadow
  87. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  88. cp -a $confdir/master.cf $confdir/master.cf.old
  89. cat $confdir/master.cf.old | sed \
  90. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  91. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  92. -e "s/^#\(tlsmgr$sp\)/\1/" \
  93. > $confdir/master.cf
  94. cat $confdir/master.cf | egrep -q "^tlsmgr$sp" || \
  95. echo 'tlsmgr fifo - - - 300 1 tlsmgr' >> $confdir/master.cf
  96. $postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  97. if [ -f /etc/ssl/private/postfix.pem ]; then
  98. $postconf -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  99. fi
  100. $postconf -e 'smtpd_tls_loglevel = 1'
  101. $postconf -e 'smtpd_use_tls = yes'
  102. $postconf -e 'smtpd_tls_session_cache_database = sdbm:/var/spool/postfix/smtpd_scache'
  103. $postconf -e 'tls_random_exchange_name = /var/spool/postfix/prng_exch'
  104. $postconf -e 'smtpd_tls_auth_only = yes'
  105. $postconf -e 'smtpd_sasl_auth_enable = no'
  106. $postconf -e 'smtpd_sasl_security_options = noanonymous'
  107. $postconf -e 'smtpd_sasl_local_domain = $myhostname'
  108. $postconf -e 'smtpd_tls_received_header = yes'
  109. $postconf -e 'broken_sasl_auth_clients = yes'
  110. $postconf -e 'tls_random_source = dev:/dev/urandom'
  111. $postconf -e 'tls_daemon_random_source = dev:/dev/urandom'
  112. # Check if using a proper key exists (not just a self-signed one)
  113. # (it is assumed that a CA certificate is made public if used!)
  114. if [ -f /etc/ssl/certs/cacert.pem ]; then
  115. $postconf -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  116. # Client side TLS only makes sense if a publicly available certificate is available
  117. # (and DON'T publish a self-signed certificate!)
  118. $postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/cacert.pem'
  119. $postconf -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  120. if [ -f /etc/ssl/private/postfix.pem ]; then
  121. $postconf -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  122. fi
  123. $postconf -e 'smtp_tls_loglevel = 1'
  124. $postconf -e 'smtp_use_tls = yes'
  125. $postconf -e 'smtp_tls_CApath = /etc/ssl/certs'
  126. $postconf -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  127. $postconf -e 'smtp_tls_session_cache_database = sdbm:/var/spool/postfix/smtp_scache'
  128. # This makes Netscape ask for a certificate, so make sure it IS public!
  129. $postconf -e 'smtpd_tls_ask_ccert = yes'
  130. fi
  131. else
  132. echo 'TLS not activated - check the script for requirements...'
  133. fi
  134. if [ -x /usr/sbin/amavisd -o -x /usr/sbin/amavisd-new ]; then
  135. localtransport="smtp"
  136. if [ "$pf2" ]; then # lmtp is slightly buggy in earlier versions
  137. localtransport="lmtp" # Provides multi-session mail transaction capability and per-recipient status responses.
  138. $postconf -e 'max_use = 10' # Avoid too much reuse
  139. fi
  140. cat $confdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  141. echo "smtp-amavis unix - - n - 2 $localtransport -o smtp_data_done_timeout=1200s -o smtp_never_send_ehlo=yes -o disable_dns_lookups=yes" >> $confdir/master.cf
  142. cat $confdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  143. 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
  144. $postconf -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  145. $postconf -e 'allow_mail_to_file = yes'
  146. mkdir -p /var/lib/amavis/mboxes
  147. chown nobody.nogroup /var/lib/amavis/mboxes
  148. for mail in virus-quarantine virusalert spam-quarantine spamalert; do
  149. touch /var/lib/amavis/mboxes/$mail
  150. chown nobody.nogroup /var/lib/amavis/mboxes/$mail
  151. if ! grep -q "^$mail:" /etc/aliases; then
  152. echo "$mail: /var/lib/amavis/mboxes/$mail" >> /etc/aliases
  153. newaliases
  154. fi
  155. done
  156. if ! grep -q "^spam.police:" /etc/aliases; then
  157. echo "spam.police: root" >> /etc/aliases
  158. newaliases
  159. fi
  160. fi
  161. /etc/init.d/postfix reload
  162. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  163. # Support for trusted MX backup networks added
  164. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  165. # RBLs replaced with those recommended by http://www.antispews.org/
  166. # AMaViS tweaks as documented in amavisd-new package
  167. # Here's a convenient overview of different blackholes:
  168. # http://rbls.org/
  169. # smtpd_tls_CAfile