summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 4a67e28711ebafbc120d30bc2f435d00fe2fd4e9 (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.64 2007-12-11 14:44: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. 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. # Support exceptions to default response
  129. # (Day Old Bread (dob) lists need to reject only temporarily)
  130. $postconf -c "$tempdir" -e "rbl_reply_maps = hash:$confdir/rbl_reply_map"
  131. cat /etc/local-COMMON/postfix/rbl_reply_map \
  132. | sed 's/#.*//' \
  133. > "$tempdir/rbl_reply_map"
  134. postmapfiles="$postmapfiles rbl_reply_map"
  135. # Verify senders of common suspicious and known verifiable domains
  136. # (exclude verification of postmaster@ to not verify verification probes)
  137. # (add own domains before peers for (rare) cases of duplicates)
  138. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  139. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  140. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  141. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  142. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  143. postmapfiles="$postmapfiles sender_access"
  144. $postconf -c "$tempdir" -e "unverified_sender_reject_code = 550"
  145. # Trust recipient verification too
  146. $postconf -c "$tempdir" -e "unverified_recipient_reject_code = 550"
  147. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  148. # (change the test if using some other modules and avoid the plain ones)
  149. if [ -n "$sasl2" ] && [ -f /etc/ssl/certs/postfix.pem ]; then
  150. mkdir -p "$tempdir/$saslsubdir"
  151. echo 'mech_list: plain login' > "$tempdir/$saslsubdir/smtpd.conf"
  152. echo 'minimum_layer: 0' >> "$tempdir/$saslsubdir/smtpd.conf"
  153. echo 'sasl_pwcheck_method: saslauthd' >> "$tempdir/$saslsubdir/smtpd.conf"
  154. echo 'auto_transition: false' >> "$tempdir/$saslsubdir/smtpd.conf"
  155. groups postfix | grep -q sasl || echo "adduser postfix sasl" >> "$tempdir/COMMANDS"
  156. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  157. sed --in-place \
  158. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  159. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  160. -e "s/^#\(tlsmgr$sp\)/\1/" \
  161. "$tempdir/master.cf"
  162. cat $tempdir/master.cf | egrep -q "^tlsmgr$sp" || \
  163. echo 'tlsmgr unix - - - 300 1 tlsmgr' >> $tempdir/master.cf
  164. $postconf -c "$tempdir" -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  165. if [ -f /etc/ssl/private/postfix.pem ]; then
  166. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  167. fi
  168. $postconf -c "$tempdir" -e 'smtpd_tls_loglevel = 1'
  169. $postconf -c "$tempdir" -e 'smtpd_use_tls = yes'
  170. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  171. $postconf -c "$tempdir" -e 'smtpd_tls_CApath = /etc/ssl/certs'
  172. $postconf -c "$tempdir" -e 'lmtp_tls_CApath = /etc/ssl/certs'
  173. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  174. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_database = btree:/var/spool/postfix/smtpd_scache'
  175. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_timeout = 3600s'
  176. $postconf -c "$tempdir" -e 'tls_random_exchange_name = /var/spool/postfix/prng_exch'
  177. $postconf -c "$tempdir" -e 'smtpd_tls_auth_only = yes'
  178. $postconf -c "$tempdir" -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  179. $postconf -c "$tempdir" -e 'smtpd_sasl_security_options = noanonymous'
  180. $postconf -c "$tempdir" -e 'smtpd_sasl_local_domain = '
  181. $postconf -c "$tempdir" -e 'smtpd_tls_received_header = yes'
  182. $postconf -c "$tempdir" -e 'broken_sasl_auth_clients = yes'
  183. $postconf -c "$tempdir" -e 'tls_random_source = dev:/dev/urandom'
  184. $postconf -c "$tempdir" -e 'tls_daemon_random_source = dev:/dev/urandom'
  185. # Check if using a proper key exists (not just a self-signed one)
  186. # (it is assumed that a CA certificate is made public if used!)
  187. if [ -f /etc/ssl/certs/cacert.pem ]; then
  188. $postconf -c "$tempdir" -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
  189. # Client side TLS only makes sense if a publicly available certificate is available
  190. # (and DON'T publish a self-signed certificate!)
  191. $postconf -c "$tempdir" -e 'smtp_tls_CAfile = /etc/ssl/certs/cacert.pem'
  192. $postconf -c "$tempdir" -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  193. if [ -f /etc/ssl/private/postfix.pem ]; then
  194. $postconf -c "$tempdir" -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  195. fi
  196. $postconf -c "$tempdir" -e 'smtp_tls_loglevel = 1'
  197. $postconf -c "$tempdir" -e 'smtp_use_tls = yes'
  198. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  199. $postconf -c "$tempdir" -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  200. $postconf -c "$tempdir" -e 'smtp_tls_session_cache_database = btree:/var/spool/postfix/smtp_scache'
  201. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  202. $postconf -c "$tempdir" -e 'smtpd_tls_ask_ccert = no'
  203. # Force using TLS for peers
  204. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  205. postmapfiles="$postmapfiles tls_policy"
  206. $postconf -c "$tempdir" -e "smtp_tls_policy_maps = hash:$confdir/tls_policy"
  207. else
  208. echo >&2 "WARNING: CA certificate not found - consider using proper signed certificates!"
  209. fi
  210. else
  211. echo >&2 'WARNING: TLS not activated - check the script for requirements...'
  212. fi
  213. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  214. $postconf -c "$tempdir" -e 'max_use = 10' # Avoid too much reuse
  215. cat $tempdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  216. cat >> $tempdir/master.cf << EOF
  217. smtp-amavis unix - - n - 5 smtp
  218. -o smtp_data_done_timeout=1200s
  219. -o smtp_never_send_ehlo=yes
  220. -o smtp_send_xforward_command=yes
  221. -o disable_dns_lookups=yes
  222. -o max_use=20
  223. EOF
  224. cat $tempdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  225. cat >> $tempdir/master.cf << EOF
  226. 127.0.0.1:10025 inet n - n - - smtpd
  227. -o content_filter=
  228. -o local_recipient_maps=
  229. -o relay_recipient_maps=
  230. -o smtpd_restriction_classes=
  231. -o smtpd_delay_reject=no
  232. -o smtpd_client_restrictions=permit_mynetworks,reject
  233. -o smtpd_helo_restrictions=
  234. -o smtpd_sender_restrictions=
  235. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  236. -o mynetworks_style=host
  237. -o mynetworks=127.0.0.0/8
  238. -o strict_rfc821_envelopes=yes
  239. -o smtpd_error_sleep_time=0
  240. -o smtpd_soft_error_limit=1001
  241. EOF
  242. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  243. fi
  244. diff -ruN "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; fi
  245. if [ "$force" = "1" ]; then
  246. do_update="y"
  247. else
  248. echo -n "Above is the intended changes. OK to update (y/N)? "
  249. read do_update
  250. fi
  251. case $do_update in
  252. y|Y)
  253. if [ -f "$tempdir/COMMANDS" ]; then
  254. sh -c "$tempdir/COMMANDS"
  255. fi
  256. rm -f "$tempdir/COMMANDS"
  257. cp -a -f -t "$confdir" "$tempdir"/*
  258. rm -rf "$tempdir"
  259. for file in $postmapfiles; do
  260. postmap "$confdir/$file"
  261. done
  262. # FIXME: restart if master.conf has been edited
  263. invoke-rc.d postfix reload
  264. echo >&2 "Changes applied!"
  265. ;;
  266. *)
  267. exit1 "Aborted!"
  268. ;;
  269. esac
  270. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  271. # Support for trusted MX backup networks added
  272. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  273. # RBLs replaced with those recommended by http://www.antispews.org/
  274. # AMaViS tweaks as documented in amavisd-new package
  275. # AUTH-SMTP based on these:
  276. # http://lists.q-linux.com/pipermail/plug/2003-July/029503.html
  277. # http://www.porcupine.org/postfix-mirror/newdoc/SASL_README.html
  278. # Here's a convenient overview of different blackholes:
  279. # http://rbls.org/
  280. # smtpd_tls_CAfile