summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 225bc09d12926d9150765f3a047f322984645fbb (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. # * Use https://www.dnswl.org/
  16. set -e
  17. # Let's standardize sort
  18. export LC_ALL=C
  19. warn() {
  20. echo >&2 "Warning: $1"
  21. }
  22. exit1() {
  23. echo >&2 "Error: $1"
  24. echo >&2 "Exiting..."
  25. exit 1
  26. }
  27. # Favor specific CA for our own server and client certificates
  28. # (comment out to trust any CA)
  29. #cacert_smtpd="/etc/ssl/certs/cacert.org.pem"
  30. cacert_smtp=
  31. cacert_lmtp=
  32. # File containing all trusted CA certificates
  33. # (comment out if no chroot or it contains all individual files)
  34. cacert_default="/etc/ssl/certs/ca-certificates.crt"
  35. realmsdir='/etc/local-REDPILL'
  36. configdirs='/etc/local /etc/local-ORG /etc/local-REDPILL /etc/local-COMMON'
  37. confdir='/etc/postfix'
  38. postconf=/usr/sbin/postconf
  39. sp='[[:space:]]'
  40. if ! [ -d "$realmsdir" ]; then
  41. warn "Realms directory \"$realmsdir\" does not exist."
  42. fi
  43. #postgrey=
  44. #if [ -x /usr/sbin/postgrey ]; then
  45. # # FIXME: Use this somehow, and only warn below
  46. # postgrey=1
  47. #else
  48. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  49. #fi
  50. dovecot=
  51. dovecot_lmtp=
  52. dovecot_deliver=
  53. if [ -x /usr/sbin/dovecot ]; then
  54. dovecot=1
  55. if [ -x /usr/lib/dovecot/lmtp ]; then
  56. dovecot_lmtp=1
  57. elif [ -x /usr/lib/dovecot/deliver ]; then
  58. warn "Dovecot LMTP missing - (Debian package dovecot-lmtp)."
  59. dovecot_deliver=1
  60. else
  61. warn "Dovecot deliver missing."
  62. fi
  63. else
  64. warn "Dovecot missing - (Debian package dovecot-core or dovecot)."
  65. fi
  66. sslcert=
  67. if [ -f /etc/ssl/certs/postfix.pem ]; then
  68. sslcert=1
  69. else
  70. warn "No TLS encryption - requires SSL certificate at /etc/ssl/certs/postfix.pem."
  71. fi
  72. sasl=
  73. if [ -n "$sslcert" ] && [ -n "$dovecot" ]; then
  74. sasl=1
  75. else
  76. warn "No SASL authentication - requires TLS encryption and Dovecot."
  77. fi
  78. amavis=
  79. default_amavis_maxproc=2
  80. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  81. amavis=1
  82. else
  83. warn "AMaViS missing (Debian package amavisd-new)."
  84. fi
  85. dkimproxy=
  86. #dkimproxy_maxproc_in=5
  87. # FIXME: update actual outgoing maxproc from /etc/default/dkimproxy (and /etc/dkimproxy/dkimproxy_out.conf too?)
  88. dkimproxy_maxproc_out=5
  89. if [ -x /usr/sbin/dkimproxy.in ]; then
  90. if [ "1" = "$amavis" ]; then
  91. dkimproxy=1
  92. else
  93. warn "No DKIM/Domainkey - requires DKIMproxy and AMaViS."
  94. fi
  95. else
  96. warn "DKIMproxy missing (Debian package dkimproxy)."
  97. fi
  98. catfilefromownrealm() {
  99. set -e
  100. file="$1"
  101. [ -d "$realmsdir" ] || exit 0
  102. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  103. cat "$realmsdir/$thisrealm/$file"
  104. }
  105. catallfilesfromotherrealms() {
  106. set -e
  107. file="$1"
  108. [ -d "$realmsdir" ] || exit 0
  109. [ -f "$realmsdir/realms" ] || exit 0
  110. realms="$(cat "$realmsdir/realms" | sed 's/#.*//')"
  111. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  112. for realm in $realms; do
  113. if [ "$thisrealm" != "$realm" ]; then
  114. cat "$realmsdir/$realm/$file"
  115. fi
  116. done
  117. }
  118. catfirstfile() {
  119. set -e
  120. file="$1"
  121. context="${2:-postfix}"
  122. configdir=''
  123. for dir in $configdirs; do
  124. if [ -d "$dir/$context" ] && [ -f "$dir/$context/$file" ]; then
  125. configdir="$dir/$context"
  126. break
  127. fi
  128. done
  129. if [ -z "$configdir" ]; then
  130. exit1 "ERROR: file \"$file\" not found."
  131. fi
  132. cat "$configdir/$file"
  133. }
  134. # TODO: support quoted string, and trailing comment.
  135. getperlvarfromfile() {
  136. set -e
  137. export var="$1"
  138. export default="$2"
  139. file="$3"
  140. context="${4:-postfix}"
  141. catfirstfile "$file" "$context" \
  142. | perl -nE'/^\$$ENV{"var"}\h*=\h*(\d+)/ and $s=$1; END {print length($s) ? $s : $ENV{'default'}}'
  143. }
  144. getlinesfromfile() {
  145. set -e
  146. param="$1"
  147. shift
  148. replacements=
  149. for subparam in $@; do
  150. case "$subparam" in
  151. *=)
  152. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  153. replacements="$replacements;s/,*[^,]*$oldparam[^,]*,*/,/"
  154. continue
  155. ;;
  156. *=*=*)
  157. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  158. newparam="`echo $subparam | awk -F= '{print $2}'`"
  159. newparamfile="`echo $subparam | awk -F= '{print $3}'`"
  160. ;;
  161. *)
  162. oldparam=$subparam
  163. newparam=$subparam
  164. newparamfile=$subparam
  165. ;;
  166. esac
  167. newparamvalues="`getlinesfromfile $newparamfile | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  168. newstring=
  169. for newparamvalue in $newparamvalues; do
  170. newstring="${newstring}$newparam $newparamvalue,"
  171. done
  172. replacements="$replacements;s/$oldparam/$newstring/"
  173. done
  174. echo -n "$param = "
  175. catfirstfile "$param" | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e 's/\^/ /g' -e "s/,\$//$replacements"
  176. }
  177. addoroverwritewithindents() {
  178. set -e
  179. file="$1"
  180. export token="$2"
  181. # provide replacement as STDIN
  182. perl -0777 -pi \
  183. -E 'BEGIN{ $s=<STDIN>; chomp $s }'\
  184. -E 's/$ENV{"token"}.*(?:\n\h.*)+\n/$s/m and $seen=1;'\
  185. -E 'END{ unless ($seen) { print $s } }'\
  186. "$file"
  187. }
  188. postmapfiles=
  189. tempdir="$(mktemp -td postfix.XXXXXX)"
  190. cp -a -t "$tempdir" "$confdir"/*
  191. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  192. $postconf -c "$tempdir" -e 'smtpd_banner = $myhostname NO UCE ESMTP $mail_name (Debian/GNU)'
  193. # Some badly configured setup use hostname instead of FQDN
  194. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  195. #if $postconf -c "$tempdir" myhostname | grep -q '\.'; then
  196. # $postconf -c "$tempdir" -e 'smtpd_helo_required = yes'
  197. #fi
  198. $postconf -c "$tempdir" -e 'smtpd_helo_required = no'
  199. $postconf -c "$tempdir" -e "`getlinesfromfile permit_mx_backup_networks`"
  200. $postconf -c "$tempdir" -e "maps_rbl_domains ="
  201. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_client_restrictions reject_rhsbl_client`"
  202. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_helo_restrictions`"
  203. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender ${dkimproxy:-sender_access_regex=}`"
  204. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains`"
  205. $postconf -c "$tempdir" -e "`getlinesfromfile smtpd_data_restrictions`"
  206. # FIXME: clear only specific line (not whole file) when dkimproxy unused
  207. if [ -f "$tempdir/sender_access_regex" ]; then
  208. if [ -n "$dkimproxy" ]; then
  209. grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex" \
  210. || echo '/^/ FILTER dkimsign:[127.0.0.1]:10026' >> "$tempdir/sender_access_regex"
  211. else
  212. if grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex"; then
  213. echo "echo '' > \"$confdir/sender_access_regex\"" >> "$tempdir/COMMANDS"
  214. rm "$tempdir/sender_access_regex"
  215. fi
  216. fi
  217. postmapfiles="$postmapfiles sender_access_regex"
  218. fi
  219. # Support exceptions to default response
  220. # (Day Old Bread (dob) lists need to reject only temporarily)
  221. $postconf -c "$tempdir" -e "rbl_reply_maps = hash:$confdir/rbl_reply_map"
  222. cat /etc/local-COMMON/postfix/rbl_reply_map \
  223. | sed 's/#.*//' \
  224. > "$tempdir/rbl_reply_map"
  225. postmapfiles="$postmapfiles rbl_reply_map"
  226. # Verify senders of common suspicious and known verifiable domains
  227. # (exclude verification of postmaster@ to not verify verification probes)
  228. # (add own domains before peers for (rare) cases of duplicates)
  229. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  230. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  231. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  232. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  233. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  234. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" >> "$tempdir/sender_access"
  235. postmapfiles="$postmapfiles sender_access"
  236. $postconf -c "$tempdir" -e "unverified_sender_reject_code = 550"
  237. # Trust recipient verification too
  238. $postconf -c "$tempdir" -e "unverified_recipient_reject_code = 550"
  239. if [ -n "$dovecot_lmtp" ]; then
  240. $postconf -c "$tempdir" -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
  241. elif [ -n "$dovecot_deliver" ]; then
  242. $postconf -c "$tempdir" -e mailbox_command=/usr/lib/dovecot/deliver
  243. fi
  244. if [ -n "$sasl" ]; then
  245. $postconf -c "$tempdir" -e smtpd_sasl_type=dovecot
  246. $postconf -c "$tempdir" -e smtpd_sasl_path=private/auth
  247. fi
  248. # Avoid smtpd_tls_CApath or smtpd_tls_CAfile to trick outlook.com
  249. # See <http://postfix.1071664.n5.nabble.com/Problems-with-incoming-mails-from-outlook-com-td78356.html>
  250. if [ -n "$sslcert" ]; then
  251. $postconf -c "$tempdir" -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  252. $postconf -c "$tempdir" -e 'smtpd_tls_loglevel = 1'
  253. $postconf -c "$tempdir" -e 'smtpd_use_tls = yes'
  254. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  255. $postconf -c "$tempdir" -e 'smtpd_tls_CApath = '
  256. $postconf -c "$tempdir" -e 'lmtp_tls_CApath = /etc/ssl/certs'
  257. $postconf -c "$tempdir" -e 'smtpd_tls_CAfile = '
  258. $postconf -c "$tempdir" -e smtp_tls_CAfile="${cacert_smtp:-$cacert_default}"
  259. $postconf -c "$tempdir" -e lmtp_tls_CAfile="${cacert_lmtp:-$cacert_default}"
  260. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache'
  261. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_timeout = 3600s'
  262. $postconf -c "$tempdir" -e 'tls_random_exchange_name = ${data_directory}/prng_exch'
  263. $postconf -c "$tempdir" -e 'smtpd_tls_auth_only = yes'
  264. $postconf -c "$tempdir" -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  265. $postconf -c "$tempdir" -e 'smtpd_sasl_security_options = noanonymous'
  266. $postconf -c "$tempdir" -e 'smtpd_sasl_local_domain = '
  267. $postconf -c "$tempdir" -e 'smtpd_tls_received_header = yes'
  268. $postconf -c "$tempdir" -e 'broken_sasl_auth_clients = yes'
  269. $postconf -c "$tempdir" -e 'tls_random_source = dev:/dev/urandom'
  270. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  271. $postconf -c "$tempdir" -e 'smtpd_tls_ask_ccert = no'
  272. if [ -e /etc/ssl/private/postfix.pem ]; then
  273. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  274. # Enable client side TLS only when private certificate is present
  275. $postconf -c "$tempdir" -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  276. $postconf -c "$tempdir" -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  277. $postconf -c "$tempdir" -e 'smtp_tls_loglevel = 1'
  278. $postconf -c "$tempdir" -e 'smtp_use_tls = yes'
  279. $postconf -c "$tempdir" -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  280. $postconf -c "$tempdir" -e 'smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache'
  281. # Force using TLS for peers
  282. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  283. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" >> "$tempdir/tls_policy"
  284. postmapfiles="$postmapfiles tls_policy"
  285. $postconf -c "$tempdir" -e "smtp_tls_policy_maps = hash:$confdir/tls_policy"
  286. else
  287. $postconf -c "$tempdir" -e 'smtp_use_tls = no'
  288. warn "No client-side TLS - requires private SSL certificate at /etc/ssl/private/postfix.pem."
  289. fi
  290. fi
  291. if [ -n "$amavis" ]; then
  292. $postconf -c "$tempdir" -e 'max_use = 10' # Avoid too much reuse
  293. amavis_maxproc=$(getperlvarfromfile max_servers "$default_amavis_maxproc" amavisd.conf.addon amavis)
  294. addoroverwritewithindents $tempdir/master.cf '^amavisfeed\h' << EOF
  295. amavisfeed unix - - n - $amavis_maxproc lmtp
  296. -o lmtp_data_done_timeout=1200s
  297. -o lmtp_never_send_ehlo=yes
  298. -o lmtp_send_xforward_command=yes
  299. -o disable_dns_lookups=yes
  300. -o max_use=20
  301. EOF
  302. addoroverwritewithindents $tempdir/master.cf '^127.0.0.1:10025\h' << EOF
  303. 127.0.0.1:10025 inet n - n - - smtpd
  304. -o content_filter=
  305. -o local_recipient_maps=
  306. -o relay_recipient_maps=
  307. -o smtpd_restriction_classes=
  308. -o smtpd_delay_reject=no
  309. -o smtpd_client_restrictions=permit_mynetworks,reject
  310. -o smtpd_helo_restrictions=
  311. -o smtpd_sender_restrictions=
  312. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  313. -o smtpd_data_restrictions=reject_unauth_pipelining
  314. -o smtpd_end_of_data_restrictions=
  315. -o mynetworks=127.0.0.0/8
  316. -o smtpd_error_sleep_time=0
  317. -o smtpd_soft_error_limit=1001
  318. -o smtpd_hard_error_limit=1000
  319. -o smtpd_client_connection_count_limit=0
  320. -o smtpd_client_connection_rate_limit=0
  321. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  322. EOF
  323. if [ -n "$dkimproxy" ]; then
  324. $postconf -c "$tempdir" -e 'content_filter = amavisfeed:[127.0.0.1]:10028'
  325. $postconf -c "$tempdir" -e receive_override_options=no_address_mappings
  326. addoroverwritewithindents $tempdir/master.cf '^submission\h' << EOF
  327. submission inet n - n - - smtpd
  328. -o syslog_name=postfix/submission
  329. -o smtpd_tls_security_level=encrypt
  330. -o smtpd_sasl_auth_enable=yes
  331. -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  332. -o content_filter=dkimsign:[127.0.0.1]:10028
  333. EOF
  334. addoroverwritewithindents $tempdir/master.cf '^pickup\h' << EOF
  335. pickup fifo n - - 60 1 pickup
  336. -o content_filter=dkimsign:127.0.0.1:10028
  337. EOF
  338. addoroverwritewithindents $tempdir/master.cf '^dkimsign\h' << EOF
  339. dkimsign unix - - n - $dkimproxy_maxproc_out smtp
  340. -o smtp_send_xforward_command=yes
  341. -o smtp_discard_ehlo_keywords=8bitmime,starttls
  342. EOF
  343. addoroverwritewithindents $tempdir/master.cf '^127\.0\.0\.1:10029\h' << EOF
  344. 127.0.0.1:10029 inet n - n - - smtpd
  345. -o content_filter=
  346. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  347. -o smtpd_helo_restrictions=
  348. -o smtpd_client_restrictions=
  349. -o smtpd_sender_restrictions=
  350. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  351. -o mynetworks=127.0.0.0/8
  352. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  353. EOF
  354. else
  355. $postconf -c "$tempdir" -e 'content_filter = amavisfeed:[127.0.0.1]:10024'
  356. $postconf -c "$tempdir" -e 'receive_override_options = no_address_mappings'
  357. addoroverwritewithindents $tempdir/master.cf '^submission\h' << EOF
  358. submission inet n - n - - smtpd
  359. -o syslog_name=postfix/submission
  360. -o smtpd_tls_security_level=encrypt
  361. -o smtpd_sasl_auth_enable=yes
  362. -o smtpd_sasl_local_domain=\$mydomain
  363. EOF
  364. addoroverwritewithindents $tempdir/master.cf '^pickup\h' << EOF
  365. pickup fifo n - - 60 1 pickup
  366. EOF
  367. fi
  368. else
  369. $postconf -c "$tempdir" -e content_filter=
  370. $postconf -c "$tempdir" -e receive_override_options=
  371. fi
  372. diff -ruN "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload="1"; fi
  373. if [ "$force" = "1" ]; then
  374. do_update="y"
  375. elif [ "1" = "$needs_reload" ]; then
  376. echo -n "Above is the intended changes. OK to update (y/N)? "
  377. read do_update
  378. fi
  379. case $do_update in
  380. y|Y)
  381. if [ -f "$tempdir/COMMANDS" ]; then
  382. cat "$tempdir/COMMANDS" | sh -s
  383. fi
  384. rm -f "$tempdir/COMMANDS"
  385. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart="1"; fi
  386. cp -a -f -t "$confdir" "$tempdir"/*
  387. rm -rf "$tempdir"
  388. for file in $postmapfiles; do
  389. postmap "$confdir/$file"
  390. done
  391. if [ "1" = "$needs_restart" ]; then
  392. invoke-rc.d postfix restart
  393. else
  394. invoke-rc.d postfix force-reload
  395. fi
  396. if [ "1" = "$needs_reload" ]; then
  397. echo >&2 "Changes applied!"
  398. fi
  399. ;;
  400. *)
  401. if [ "1" = "$needs_reload" ]; then
  402. exit1 "Aborted!"
  403. fi
  404. ;;
  405. esac
  406. if [ "1" != "$needs_reload" ]; then
  407. echo >&2 "No changes needed!"
  408. fi
  409. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  410. # Support for trusted MX backup networks added
  411. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  412. # RBLs replaced with those recommended by http://www.antispews.org/
  413. # spam filter based on these: http://www.postfix.org/FILTER_README.html
  414. # https://www.ijs.si/software/amavisd/amavisd-new-docs.html
  415. # AUTH-SMTP based on this: http://www.postfix.org/SASL_README.html
  416. # Here's a convenient overview of different blackholes:
  417. # http://rbls.org/