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