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