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