summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: e3f8e0d73ee1e86d35e076faa55dceb8b4059c3b (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.63 2007-12-11 14:29:54 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. exit1() {
  19. echo >&2 "Error: $1"
  20. echo >&2 "Exiting..."
  21. exit 1
  22. }
  23. realmsdir='/etc/local-REDPILL'
  24. configdirs='/etc/local/postfix /etc/local-ORG/postfix /etc/local-REDPILL/postfix /etc/local-COMMON/postfix'
  25. confdir='/etc/postfix'
  26. postconf=/usr/sbin/postconf
  27. sp='[[:space:]]'
  28. if ! $postconf -d mail_version | egrep -q '= 2\.[2-9]'; then
  29. exit1 "ERROR: Bad postfix version - this script is known to work only for postfix 2.2 and later"
  30. fi
  31. if ! [ -d "$realmsdir" ]; then
  32. echo >&2 "WARNING: Realms directory \"$realmsdir\" does not exist."
  33. fi
  34. #postgrey=
  35. #if [ -x /usr/sbin/postgrey ]; then
  36. # # FIXME: Use this somehow, and only warn below
  37. # postgrey=1
  38. #else
  39. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  40. #fi
  41. # FIXME: We really want to check for at least 2.1.1 but that's tricky...
  42. sasl2=
  43. if saslauthd -v 2>&1 | grep -q '^saslauthd 2.1'; then
  44. sasl2=1
  45. else
  46. echo >&2 "WARNING: Encryption requires sasl tools 2.1.1 (Debian package sasl2-bin)."
  47. fi
  48. saslsubdir="sasl"
  49. catfilefromownrealm() {
  50. file="$1"
  51. [ -d "$realmsdir" ] || exit 0
  52. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  53. cat "$realmsdir/$thisrealm/$file"
  54. }
  55. catallfilesfromotherrealms() {
  56. file="$1"
  57. [ -d "$realmsdir" ] || exit 0
  58. [ -f "$realmsdir/realms" ] || exit 0
  59. realms="$(cat "$realmsdir/realms" | sed 's/#.*//')"
  60. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  61. for realm in $realms; do
  62. if [ "$thisrealm" != "$realm" ]; then
  63. cat "$realmsdir/$realm/$file"
  64. fi
  65. done
  66. }
  67. catfirstfile() {
  68. file="$1"
  69. configdir=''
  70. for dir in $configdirs; do
  71. if [ -d "$dir" ] && [ -f "$dir/$file" ]; then
  72. configdir="$dir"
  73. break
  74. fi
  75. done
  76. if [ -z "$configdir" ]; then
  77. exit1 "ERROR: file \"$file\" not found."
  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. postmapfiles=
  111. tempdir="$(mktemp -td postfix.XXXXXX)"
  112. cp -a -t "$tempdir" "$confdir"/*
  113. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  114. $postconf -c "$tempdir" -e 'smtpd_banner = $myhostname NO UCE ESMTP $mail_name (Debian/GNU)'
  115. # Some badly configured setup use hostname instead of FQDN
  116. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  117. #if $postconf -c "$tempdir" myhostname | grep -q '\.'; then
  118. # $postconf -c "$tempdir" -e 'smtpd_helo_required = yes'
  119. #fi
  120. $postconf -c "$tempdir" -e 'smtpd_helo_required = no'
  121. $postconf -c "$tempdir" -e "`getlinesfromfile permit_mx_backup_networks`"
  122. $postconf -c "$tempdir" -e "maps_rbl_domains ="
  123. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_client_restrictions reject_rhsbl_client`"
  124. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_helo_restrictions`"
  125. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender`"
  126. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains`"
  127. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_data_restrictions`"
  128. # Verify senders of common suspicious and known verifiable domains
  129. # (exclude verification of postmaster@ to not verify verification probes)
  130. # (add own domains before peers for (rare) cases of duplicates)
  131. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  132. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  133. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  134. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  135. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  136. postmapfiles="$postmapfiles sender_access"
  137. $postconf -c "$tempdir" -e "unverified_sender_reject_code = 550"
  138. # Trust recipient verification too
  139. $postconf -c "$tempdir" -e "unverified_recipient_reject_code = 550"
  140. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  141. # (change the test if using some other modules and avoid the plain ones)
  142. if [ -n "$sasl2" ] && [ -f /etc/ssl/certs/postfix.pem ]; then
  143. mkdir -p "$tempdir/$saslsubdir"
  144. echo 'mech_list: plain login' > "$tempdir/$saslsubdir/smtpd.conf"
  145. echo 'minimum_layer: 0' >> "$tempdir/$saslsubdir/smtpd.conf"
  146. echo 'sasl_pwcheck_method: saslauthd' >> "$tempdir/$saslsubdir/smtpd.conf"
  147. echo 'auto_transition: false' >> "$tempdir/$saslsubdir/smtpd.conf"
  148. groups postfix | grep -q sasl || echo "adduser postfix sasl" >> "$tempdir/COMMANDS"
  149. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  150. sed --in-place \
  151. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  152. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  153. -e "s/^#\(tlsmgr$sp\)/\1/" \
  154. "$tempdir/master.cf"
  155. cat $tempdir/master.cf | egrep -q "^tlsmgr$sp" || \
  156. echo 'tlsmgr unix - - - 300 1 tlsmgr' >> $tempdir/master.cf
  157. $postconf -c "$tempdir" -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  158. if [ -f /etc/ssl/private/postfix.pem ]; then
  159. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  160. fi
  161. $postconf -c "$tempdir" -e 'smtpd_tls_loglevel = 1'
  162. $postconf -c "$tempdir" -e 'smtpd_use_tls = yes'
  163. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  164. $postconf -c "$tempdir" -e 'smtpd_tls_CApath = /etc/ssl/certs'
  165. $postconf -c "$tempdir" -e 'lmtp_tls_CApath = /etc/ssl/certs'
  166. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  167. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_database = btree:/var/spool/postfix/smtpd_scache'
  168. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_timeout = 3600s'
  169. $postconf -c "$tempdir" -e 'tls_random_exchange_name = /var/spool/postfix/prng_exch'
  170. $postconf -c "$tempdir" -e 'smtpd_tls_auth_only = yes'
  171. $postconf -c "$tempdir" -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  172. $postconf -c "$tempdir" -e 'smtpd_sasl_security_options = noanonymous'
  173. $postconf -c "$tempdir" -e 'smtpd_sasl_local_domain = '
  174. $postconf -c "$tempdir" -e 'smtpd_tls_received_header = yes'
  175. $postconf -c "$tempdir" -e 'broken_sasl_auth_clients = yes'
  176. $postconf -c "$tempdir" -e 'tls_random_source = dev:/dev/urandom'
  177. $postconf -c "$tempdir" -e 'tls_daemon_random_source = dev:/dev/urandom'
  178. # Check if using a proper key exists (not just a self-signed one)
  179. # (it is assumed that a CA certificate is made public if used!)
  180. if [ -f /etc/ssl/certs/cacert.pem ]; then
  181. $postconf -c "$tempdir" -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  182. # Client side TLS only makes sense if a publicly available certificate is available
  183. # (and DON'T publish a self-signed certificate!)
  184. $postconf -c "$tempdir" -e 'smtp_tls_CAfile = /etc/ssl/certs/cacert.pem'
  185. $postconf -c "$tempdir" -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  186. if [ -f /etc/ssl/private/postfix.pem ]; then
  187. $postconf -c "$tempdir" -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  188. fi
  189. $postconf -c "$tempdir" -e 'smtp_tls_loglevel = 1'
  190. $postconf -c "$tempdir" -e 'smtp_use_tls = yes'
  191. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  192. $postconf -c "$tempdir" -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  193. $postconf -c "$tempdir" -e 'smtp_tls_session_cache_database = btree:/var/spool/postfix/smtp_scache'
  194. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  195. $postconf -c "$tempdir" -e 'smtpd_tls_ask_ccert = no'
  196. # Force using TLS for peers
  197. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  198. postmapfiles="$postmapfiles tls_policy"
  199. $postconf -c "$tempdir" -e "smtp_tls_policy_maps = hash:$confdir/tls_policy"
  200. else
  201. echo >&2 "WARNING: CA certificate not found - consider using proper signed certificates!"
  202. fi
  203. else
  204. echo >&2 'WARNING: TLS not activated - check the script for requirements...'
  205. fi
  206. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  207. $postconf -c "$tempdir" -e 'max_use = 10' # Avoid too much reuse
  208. cat $tempdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  209. cat >> $tempdir/master.cf << EOF
  210. smtp-amavis unix - - n - 5 smtp
  211. -o smtp_data_done_timeout=1200s
  212. -o smtp_never_send_ehlo=yes
  213. -o smtp_send_xforward_command=yes
  214. -o disable_dns_lookups=yes
  215. -o max_use=20
  216. EOF
  217. cat $tempdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  218. cat >> $tempdir/master.cf << EOF
  219. 127.0.0.1:10025 inet n - n - - smtpd
  220. -o content_filter=
  221. -o local_recipient_maps=
  222. -o relay_recipient_maps=
  223. -o smtpd_restriction_classes=
  224. -o smtpd_delay_reject=no
  225. -o smtpd_client_restrictions=permit_mynetworks,reject
  226. -o smtpd_helo_restrictions=
  227. -o smtpd_sender_restrictions=
  228. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  229. -o mynetworks_style=host
  230. -o mynetworks=127.0.0.0/8
  231. -o strict_rfc821_envelopes=yes
  232. -o smtpd_error_sleep_time=0
  233. -o smtpd_soft_error_limit=1001
  234. EOF
  235. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  236. fi
  237. diff -ruN "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; fi
  238. if [ "$force" = "1" ]; then
  239. do_update="y"
  240. else
  241. echo -n "Above is the intended changes. OK to update (y/N)? "
  242. read do_update
  243. fi
  244. case $do_update in
  245. y|Y)
  246. if [ -f "$tempdir/COMMANDS" ]; then
  247. sh -c "$tempdir/COMMANDS"
  248. fi
  249. rm -f "$tempdir/COMMANDS"
  250. cp -a -f -t "$confdir" "$tempdir"/*
  251. rm -rf "$tempdir"
  252. for file in $postmapfiles; do
  253. postmap "$confdir/$file"
  254. done
  255. # FIXME: restart if master.conf has been edited
  256. invoke-rc.d postfix reload
  257. echo >&2 "Changes applied!"
  258. ;;
  259. *)
  260. exit1 "Aborted!"
  261. ;;
  262. esac
  263. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  264. # Support for trusted MX backup networks added
  265. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  266. # RBLs replaced with those recommended by http://www.antispews.org/
  267. # AMaViS tweaks as documented in amavisd-new package
  268. # AUTH-SMTP based on these:
  269. # http://lists.q-linux.com/pipermail/plug/2003-July/029503.html
  270. # http://www.porcupine.org/postfix-mirror/newdoc/SASL_README.html
  271. # Here's a convenient overview of different blackholes:
  272. # http://rbls.org/
  273. # smtpd_tls_CAfile