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