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