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