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