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