summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: ebcdbc5cf8b38883964576e631e5e96f950ee97c (plain)
  1. #!/bin/bash
  2. #
  3. # /etc/local-COMMON/postfix/postfix.sh
  4. # Copyright 2002-2004 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: postfix.sh,v 1.29 2004-05-09 20:23:19 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. pftls=
  26. if [ -x /usr/lib/postfix/tlsmgr ]; then
  27. pftls=1
  28. else
  29. echo "WARNING: Not enabling encryption: TLS-patched postfix required."
  30. echo " (on Debian: install the package postfix-tls)"
  31. fi
  32. # FIXME: We really want to check for at least 2.1.1 but that's tricky...
  33. sasl2=
  34. if saslauthd -v 2>&1 | grep -q '^saslauthd 2.1'; then
  35. sasl2=1
  36. else
  37. echo "WARNING: Not enabling encryption: sasl tools 2.1.1 or greater is required."
  38. echo " (on Debian: install the package sasl2-bin)"
  39. fi
  40. #function getlinesfromfile() {
  41. # param="$1"
  42. # echo -n "$param = "
  43. # cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//' -e 's/[, ]\+/,/g' -e 's/,$//'
  44. #}
  45. function getlinesfromfile() {
  46. param="$1"
  47. shift
  48. replacements=
  49. for subparam in $@; do
  50. case "$subparam" in
  51. *=*=*)
  52. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  53. newparam="`echo $subparam | awk -F= '{print $2}'`"
  54. newparamfile="`echo $subparam | awk -F= '{print $3}'`"
  55. shift
  56. ;;
  57. *)
  58. oldparam=$subparam
  59. newparam=$subparam
  60. newparamfile=$subparam
  61. shift
  62. ;;
  63. esac
  64. newparamvalues="`getlinesfromfile $newparamfile | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  65. newstring=
  66. for newparamvalue in $newparamvalues; do
  67. newstring="${newstring}$newparam $newparamvalue,"
  68. done
  69. replacements="$replacements;s/$oldparam/$newstring/"
  70. done
  71. echo -n "$param = "
  72. cat $paramdir/$param | grep -v '^#' | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e "s/,\$//$replacements"
  73. }
  74. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  75. $postconf -e 'smtpd_banner = $myhostname NO UCE ESMTP $mail_name (Debian/GNU)'
  76. # Some badly configured setup use hostname instead of FQDN
  77. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  78. #if $postconf myhostname | grep -q '\.'; then
  79. # $postconf -e 'smtpd_helo_required = yes'
  80. #fi
  81. $postconf -e 'smtpd_helo_required = no'
  82. $postconf -e "`getlinesfromfile permit_mx_backup_networks`"
  83. if [ "$pf2" ]; then
  84. $postconf -e "maps_rbl_domains ="
  85. $postconf -e "`getlinesfromfile smtpd_client_restrictions reject_rhsbl_client`"
  86. $postconf -e "`getlinesfromfile smtpd_helo_restrictions`"
  87. $postconf -e "`getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender`"
  88. $postconf -e "`getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains`"
  89. $postconf -e "`getlinesfromfile smtpd_data_restrictions`"
  90. else
  91. $postconf -e "`getlinesfromfile maps_rbl_domains`"
  92. $postconf -e "`getlinesfromfile smtpd_helo_restrictions`"
  93. $postconf -e "`getlinesfromfile smtpd_recipient_restrictions`"
  94. fi
  95. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  96. # (change the test if using some other modules and avoid the plain ones)
  97. if [ -n "$pftls" -a -n "$sasl2" -a -f /etc/ssl/certs/postfix.pem ]; then
  98. mkdir -p $confdir/sasl
  99. echo 'mech_list: plain login' >$confdir/sasl/smtpd.conf
  100. echo 'minimum_layer: 0' >>$confdir/sasl/smtpd.conf
  101. echo 'pwcheck_method: saslauthd' >>$confdir/sasl/smtpd.conf
  102. echo 'auto_transition: false' >>$confdir/sasl/smtpd.conf
  103. groups postfix | grep -q sasl || adduser postfix sasl
  104. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  105. cp -a $confdir/master.cf $confdir/master.cf.old
  106. cat $confdir/master.cf.old | sed \
  107. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  108. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  109. -e "s/^#\(tlsmgr$sp\)/\1/" \
  110. > $confdir/master.cf
  111. cat $confdir/master.cf | egrep -q "^tlsmgr$sp" || \
  112. echo 'tlsmgr fifo - - - 300 1 tlsmgr' >> $confdir/master.cf
  113. $postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  114. if [ -f /etc/ssl/private/postfix.pem ]; then
  115. $postconf -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  116. fi
  117. $postconf -e 'smtpd_tls_loglevel = 1'
  118. $postconf -e 'smtpd_use_tls = yes'
  119. $postconf -e 'smtpd_tls_session_cache_database = sdbm:/var/spool/postfix/smtpd_scache'
  120. $postconf -e 'smtpd_tls_session_cache_timeout = 3600s'
  121. $postconf -e 'tls_random_exchange_name = /var/spool/postfix/prng_exch'
  122. $postconf -e 'smtpd_tls_auth_only = yes'
  123. $postconf -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  124. $postconf -e 'smtpd_sasl_security_options = noanonymous'
  125. $postconf -e 'smtpd_sasl_local_domain = '
  126. $postconf -e 'smtpd_tls_received_header = yes'
  127. $postconf -e 'broken_sasl_auth_clients = yes'
  128. $postconf -e 'tls_random_source = dev:/dev/urandom'
  129. $postconf -e 'tls_daemon_random_source = dev:/dev/urandom'
  130. # Check if using a proper key exists (not just a self-signed one)
  131. # (it is assumed that a CA certificate is made public if used!)
  132. if [ -f /etc/ssl/certs/cacert.pem ]; then
  133. $postconf -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  134. # Client side TLS only makes sense if a publicly available certificate is available
  135. # (and DON'T publish a self-signed certificate!)
  136. $postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/cacert.pem'
  137. $postconf -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  138. if [ -f /etc/ssl/private/postfix.pem ]; then
  139. $postconf -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  140. fi
  141. $postconf -e 'smtp_tls_loglevel = 1'
  142. $postconf -e 'smtp_use_tls = yes'
  143. $postconf -e 'smtp_tls_CApath = /etc/ssl/certs'
  144. $postconf -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  145. $postconf -e 'smtp_tls_session_cache_database = sdbm:/var/spool/postfix/smtp_scache'
  146. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  147. $postconf -e 'smtpd_tls_ask_ccert = no'
  148. else
  149. echo "WARNING: CA certificate not found - consider using proper signed certificates!"
  150. fi
  151. else
  152. echo 'WARNING: TLS not activated - check the script for requirements...'
  153. fi
  154. if [ -x /usr/sbin/amavisd -o -x /usr/sbin/amavisd-new ]; then
  155. localtransport="smtp"
  156. if [ "$pf2" ]; then # lmtp is slightly buggy in earlier versions
  157. localtransport="lmtp" # Provides multi-session mail transaction capability and per-recipient status responses.
  158. $postconf -e 'max_use = 10' # Avoid too much reuse
  159. fi
  160. cat $confdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  161. 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
  162. cat $confdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  163. 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
  164. $postconf -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  165. $postconf -e 'allow_mail_to_file = yes'
  166. mkdir -p /var/lib/amavis/mboxes
  167. chown nobody.nogroup /var/lib/amavis/mboxes
  168. for mail in virus-quarantine virusalert spam-quarantine spamalert; do
  169. touch /var/lib/amavis/mboxes/$mail
  170. chown nobody.nogroup /var/lib/amavis/mboxes/$mail
  171. if ! grep -q "^$mail:" /etc/aliases; then
  172. echo "$mail: /var/lib/amavis/mboxes/$mail" >> /etc/aliases
  173. newaliases
  174. fi
  175. done
  176. if ! grep -q "^spam.police:" /etc/aliases; then
  177. echo "spam.police: root" >> /etc/aliases
  178. newaliases
  179. fi
  180. fi
  181. /etc/init.d/postfix reload
  182. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  183. # Support for trusted MX backup networks added
  184. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  185. # RBLs replaced with those recommended by http://www.antispews.org/
  186. # AMaViS tweaks as documented in amavisd-new package
  187. # AUTH-SMTP based on these:
  188. # http://lists.q-linux.com/pipermail/plug/2003-July/029503.html
  189. # http://www.porcupine.org/postfix-mirror/newdoc/SASL_README.html
  190. # Here's a convenient overview of different blackholes:
  191. # http://rbls.org/
  192. # smtpd_tls_CAfile