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