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