summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: e015ac905d0567c17f3ddfdcb36f5613eccf7bc7 (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.76 2008-05-25 19:00: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. # Favor specific CA for our own server and client certificates
  24. # (comment out to trust any CA)
  25. cacert="/etc/ssl/certs/cacert.org.pem"
  26. realmsdir='/etc/local-REDPILL'
  27. configdirs='/etc/local/postfix /etc/local-ORG/postfix /etc/local-REDPILL/postfix /etc/local-COMMON/postfix'
  28. confdir='/etc/postfix'
  29. postconf=/usr/sbin/postconf
  30. sp='[[:space:]]'
  31. if ! $postconf -d mail_version | egrep -q '= 2\.[2-9]'; then
  32. exit1 "ERROR: Bad postfix version - this script is known to work only for postfix 2.2 and later"
  33. fi
  34. if ! [ -d "$realmsdir" ]; then
  35. echo >&2 "WARNING: Realms directory \"$realmsdir\" does not exist."
  36. fi
  37. #postgrey=
  38. #if [ -x /usr/sbin/postgrey ]; then
  39. # # FIXME: Use this somehow, and only warn below
  40. # postgrey=1
  41. #else
  42. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  43. #fi
  44. # FIXME: We really want to check for at least 2.1.1 but that's tricky...
  45. sasl2=
  46. if saslauthd -v 2>&1 | grep -q '^saslauthd 2.1'; then
  47. sasl2=1
  48. else
  49. echo >&2 "WARNING: Encryption requires sasl tools 2.1.1 (Debian package sasl2-bin)."
  50. fi
  51. saslsubdir="sasl"
  52. sslcert=
  53. if [ -n "$sasl2" ] && [ -f /etc/ssl/certs/postfix.pem ]; then
  54. sslcert=1
  55. else
  56. echo >&2 "WARNING: Encryption requires SSL certificate at /etc/ssl/certs/postfix.pem."
  57. fi
  58. amavis=
  59. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  60. amavis=1
  61. else
  62. echo >&2 "WARNING: Avoiding AMaViS setup: not installed."
  63. fi
  64. dkimproxy=
  65. if [ "1" = "$amavis" ] && [ -x /usr/bin/dkimproxy-sign ]; then
  66. dkimproxy=1
  67. else
  68. echo >&2 "WARNING: Avoiding/disabling DKIMproxy setup: not installed."
  69. fi
  70. catfilefromownrealm() {
  71. set -e
  72. file="$1"
  73. [ -d "$realmsdir" ] || exit 0
  74. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  75. cat "$realmsdir/$thisrealm/$file"
  76. }
  77. catallfilesfromotherrealms() {
  78. set -e
  79. file="$1"
  80. [ -d "$realmsdir" ] || exit 0
  81. [ -f "$realmsdir/realms" ] || exit 0
  82. realms="$(cat "$realmsdir/realms" | sed 's/#.*//')"
  83. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  84. for realm in $realms; do
  85. if [ "$thisrealm" != "$realm" ]; then
  86. cat "$realmsdir/$realm/$file"
  87. fi
  88. done
  89. }
  90. catfirstfile() {
  91. set -e
  92. file="$1"
  93. configdir=''
  94. for dir in $configdirs; do
  95. if [ -d "$dir" ] && [ -f "$dir/$file" ]; then
  96. configdir="$dir"
  97. break
  98. fi
  99. done
  100. if [ -z "$configdir" ]; then
  101. exit1 "ERROR: file \"$file\" not found."
  102. fi
  103. cat "$configdir/$file"
  104. }
  105. getlinesfromfile() {
  106. set -e
  107. param="$1"
  108. shift
  109. replacements=
  110. for subparam in $@; do
  111. case "$subparam" in
  112. *=*=*)
  113. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  114. newparam="`echo $subparam | awk -F= '{print $2}'`"
  115. newparamfile="`echo $subparam | awk -F= '{print $3}'`"
  116. shift
  117. ;;
  118. *)
  119. oldparam=$subparam
  120. newparam=$subparam
  121. newparamfile=$subparam
  122. shift
  123. ;;
  124. esac
  125. newparamvalues="`getlinesfromfile $newparamfile | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  126. newstring=
  127. for newparamvalue in $newparamvalues; do
  128. newstring="${newstring}$newparam $newparamvalue,"
  129. done
  130. replacements="$replacements;s/$oldparam/$newstring/"
  131. done
  132. echo -n "$param = "
  133. catfirstfile "$param" | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e 's/\^/ /g' -e "s/,\$//$replacements"
  134. }
  135. postmapfiles=
  136. tempdir="$(mktemp -td postfix.XXXXXX)"
  137. cp -a -t "$tempdir" "$confdir"/*
  138. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  139. $postconf -c "$tempdir" -e 'smtpd_banner = $myhostname NO UCE ESMTP $mail_name (Debian/GNU)'
  140. # Some badly configured setup use hostname instead of FQDN
  141. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  142. #if $postconf -c "$tempdir" myhostname | grep -q '\.'; then
  143. # $postconf -c "$tempdir" -e 'smtpd_helo_required = yes'
  144. #fi
  145. $postconf -c "$tempdir" -e 'smtpd_helo_required = no'
  146. $postconf -c "$tempdir" -e "`getlinesfromfile permit_mx_backup_networks`"
  147. $postconf -c "$tempdir" -e "maps_rbl_domains ="
  148. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_client_restrictions reject_rhsbl_client`"
  149. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_helo_restrictions`"
  150. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender`"
  151. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains`"
  152. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_data_restrictions`"
  153. if [ -n "$dkimproxy" ]; then
  154. [ -f "$tempdir/sender_access_regex" ] \
  155. && grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex" \
  156. || echo '/^/ FILTER dkimsign:[127.0.0.1]:10026' >> "$tempdir/sender_access_regex"
  157. else
  158. [ -f "$tempdir/sender_access_regex" ] \
  159. || echo "touch \"$confdir/sender_access_regex\"" >> "$tempdir/COMMANDS"
  160. fi
  161. # Support exceptions to default response
  162. # (Day Old Bread (dob) lists need to reject only temporarily)
  163. $postconf -c "$tempdir" -e "rbl_reply_maps = hash:$confdir/rbl_reply_map"
  164. cat /etc/local-COMMON/postfix/rbl_reply_map \
  165. | sed 's/#.*//' \
  166. > "$tempdir/rbl_reply_map"
  167. postmapfiles="$postmapfiles rbl_reply_map"
  168. # Verify senders of common suspicious and known verifiable domains
  169. # (exclude verification of postmaster@ to not verify verification probes)
  170. # (add own domains before peers for (rare) cases of duplicates)
  171. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  172. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  173. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  174. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  175. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  176. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" >> "$tempdir/sender_access"
  177. postmapfiles="$postmapfiles sender_access"
  178. $postconf -c "$tempdir" -e "unverified_sender_reject_code = 550"
  179. # Trust recipient verification too
  180. $postconf -c "$tempdir" -e "unverified_recipient_reject_code = 550"
  181. # TLS breaks postfix if no SASL modules available (and doesn't make sense either)
  182. # (change the test if using some other modules and avoid the plain ones)
  183. if [ -n "$sasl2" ] && [ -n "$sslcert" ]; then
  184. mkdir -p "$tempdir/$saslsubdir"
  185. echo 'mech_list: plain login' > "$tempdir/$saslsubdir/smtpd.conf"
  186. echo 'minimum_layer: 0' >> "$tempdir/$saslsubdir/smtpd.conf"
  187. echo 'sasl_pwcheck_method: saslauthd' >> "$tempdir/$saslsubdir/smtpd.conf"
  188. echo 'auto_transition: false' >> "$tempdir/$saslsubdir/smtpd.conf"
  189. groups postfix | grep -q sasl || echo "adduser postfix sasl" >> "$tempdir/COMMANDS"
  190. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  191. sed --in-place \
  192. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  193. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  194. -e "s/^#\(tlsmgr$sp\)/\1/" \
  195. "$tempdir/master.cf"
  196. cat $tempdir/master.cf | egrep -q "^tlsmgr$sp" || \
  197. echo 'tlsmgr unix - - - 300 1 tlsmgr' >> $tempdir/master.cf
  198. $postconf -c "$tempdir" -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  199. $postconf -c "$tempdir" -e 'smtpd_tls_loglevel = 1'
  200. $postconf -c "$tempdir" -e 'smtpd_use_tls = yes'
  201. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  202. $postconf -c "$tempdir" -e 'smtpd_tls_CApath = /etc/ssl/certs'
  203. $postconf -c "$tempdir" -e 'lmtp_tls_CApath = /etc/ssl/certs'
  204. $postconf -c "$tempdir" -e smtpd_tls_CAfile="$cacert"
  205. $postconf -c "$tempdir" -e smtp_tls_CAfile=
  206. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache'
  207. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_timeout = 3600s'
  208. $postconf -c "$tempdir" -e 'tls_random_exchange_name = ${data_directory}/prng_exch'
  209. $postconf -c "$tempdir" -e 'smtpd_tls_auth_only = yes'
  210. $postconf -c "$tempdir" -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  211. $postconf -c "$tempdir" -e 'smtpd_sasl_security_options = noanonymous'
  212. $postconf -c "$tempdir" -e 'smtpd_sasl_local_domain = '
  213. $postconf -c "$tempdir" -e 'smtpd_tls_received_header = yes'
  214. $postconf -c "$tempdir" -e 'broken_sasl_auth_clients = yes'
  215. $postconf -c "$tempdir" -e 'tls_random_source = dev:/dev/urandom'
  216. $postconf -c "$tempdir" -e 'tls_daemon_random_source = dev:/dev/urandom'
  217. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  218. $postconf -c "$tempdir" -e 'smtpd_tls_ask_ccert = no'
  219. if [ -e /etc/ssl/private/postfix.pem ]; then
  220. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  221. # Enable client side TLS only when private certificate is present
  222. $postconf -c "$tempdir" -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  223. $postconf -c "$tempdir" -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  224. $postconf -c "$tempdir" -e 'smtp_tls_loglevel = 1'
  225. $postconf -c "$tempdir" -e 'smtp_use_tls = yes'
  226. $postconf -c "$tempdir" -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  227. $postconf -c "$tempdir" -e 'smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache'
  228. # Force using TLS for peers
  229. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  230. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" >> "$tempdir/tls_policy"
  231. postmapfiles="$postmapfiles tls_policy"
  232. $postconf -c "$tempdir" -e "smtp_tls_policy_maps = hash:$confdir/tls_policy"
  233. else
  234. $postconf -c "$tempdir" -e 'smtp_use_tls = no'
  235. echo >&2 "WARNING: Private certificate not found - client side TLS not enabled!"
  236. fi
  237. else
  238. echo >&2 'WARNING: TLS not activated due to missing requirements...'
  239. fi
  240. if [ -n "$amavis" ]; then
  241. $postconf -c "$tempdir" -e 'max_use = 10' # Avoid too much reuse
  242. cat $tempdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  243. cat >> $tempdir/master.cf << EOF
  244. smtp-amavis unix - - n - 5 smtp
  245. -o smtp_data_done_timeout=1200s
  246. -o smtp_never_send_ehlo=yes
  247. -o smtp_send_xforward_command=yes
  248. -o disable_dns_lookups=yes
  249. -o max_use=20
  250. EOF
  251. cat $tempdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  252. cat >> $tempdir/master.cf << EOF
  253. 127.0.0.1:10025 inet n - n - - smtpd
  254. -o content_filter=
  255. -o local_recipient_maps=
  256. -o relay_recipient_maps=
  257. -o smtpd_restriction_classes=
  258. -o smtpd_delay_reject=no
  259. -o smtpd_client_restrictions=permit_mynetworks,reject
  260. -o smtpd_helo_restrictions=
  261. -o smtpd_sender_restrictions=
  262. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  263. -o mynetworks_style=host
  264. -o mynetworks=127.0.0.0/8
  265. -o strict_rfc821_envelopes=yes
  266. -o smtpd_error_sleep_time=0
  267. -o smtpd_soft_error_limit=1001
  268. EOF
  269. if [ -n "$dkimproxy" ]; then
  270. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10028'
  271. # FIXME: needs multiline replacementroutine (using perl?)
  272. # cat $tempdir/master.cf | egrep -q "^submission$sp" || \
  273. # cat >> $tempdir/master.cf << EOF
  274. #submission inet n - n - - smtpd
  275. # -o smtpd_etrn_restrictions=reject
  276. # -o smtpd_enforce_tls=yes
  277. # -o smtpd_sasl_auth_enable=yes
  278. # -o content_filter=dkimsign:[127.0.0.1]:10028
  279. # -o receive_override_options=no_address_mappings
  280. # -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  281. #EOF
  282. # cat $tempdir/master.cf | egrep -q "^pickup$sp" || \
  283. # cat >> $tempdir/master.cf << EOF
  284. #pickup fifo n - - 60 1 pickup
  285. # -o content_filter=dkimsign:127.0.0.1:10028
  286. #EOF
  287. cat $tempdir/master.cf | egrep -q "^dkimsign$sp" || \
  288. cat >> $tempdir/master.cf << EOF
  289. dkimsign unix - - n - 10 smtp
  290. -o smtp_send_xforward_command=yes
  291. -o smtp_discard_ehlo_keywords=8bitmime
  292. EOF
  293. cat $tempdir/master.cf | egrep -q "^127\.0\.0\.1:10029$sp" || \
  294. cat >> $tempdir/master.cf << EOF
  295. 127.0.0.1:10029 inet n - n - 10 smtpd
  296. -o content_filter=
  297. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
  298. -o smtpd_helo_restrictions=
  299. -o smtpd_client_restrictions=
  300. -o smtpd_sender_restrictions=
  301. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  302. -o mynetworks=127.0.0.0/8
  303. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  304. EOF
  305. else
  306. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  307. fi
  308. fi
  309. diff -ruN "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload="1"; fi
  310. if [ "$force" = "1" ]; then
  311. do_update="y"
  312. elif [ "1" = "$needs_reload" ]; then
  313. echo -n "Above is the intended changes. OK to update (y/N)? "
  314. read do_update
  315. fi
  316. case $do_update in
  317. y|Y)
  318. if [ -f "$tempdir/COMMANDS" ]; then
  319. cat "$tempdir/COMMANDS" | sh -s
  320. fi
  321. rm -f "$tempdir/COMMANDS"
  322. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart="1"; fi
  323. cp -a -f -t "$confdir" "$tempdir"/*
  324. rm -rf "$tempdir"
  325. for file in $postmapfiles; do
  326. postmap "$confdir/$file"
  327. done
  328. if [ "1" = "$needs_restart" ]; then
  329. invoke-rc.d postfix restart
  330. else
  331. invoke-rc.d postfix force-reload
  332. fi
  333. if [ "1" = "$needs_reload" ]; then
  334. echo >&2 "Changes applied!"
  335. fi
  336. ;;
  337. *)
  338. if [ "1" = "$needs_reload" ]; then
  339. exit1 "Aborted!"
  340. fi
  341. ;;
  342. esac
  343. if [ "1" != "$needs_reload" ]; then
  344. echo >&2 "No changes needed!"
  345. fi
  346. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  347. # Support for trusted MX backup networks added
  348. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  349. # RBLs replaced with those recommended by http://www.antispews.org/
  350. # AMaViS tweaks as documented in amavisd-new package
  351. # AUTH-SMTP based on these:
  352. # http://lists.q-linux.com/pipermail/plug/2003-July/029503.html
  353. # http://www.porcupine.org/postfix-mirror/newdoc/SASL_README.html
  354. # Here's a convenient overview of different blackholes:
  355. # http://rbls.org/