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