summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: cfa3d8a4de7de4630089685c7277eefb0150c183 (plain)
  1. #!/bin/sh
  2. #
  3. # /etc/local-COMMON/postfix/postfix.sh
  4. # Copyright 2002-2010,2013-2016,2020 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # Auto-tweak plain installed postfix Debian package
  7. #
  8. # Depends: postfix (>= 3.4)
  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. FORCE=${FORCE:-}
  28. REDPILL=${REDPILL:-REDPILL}
  29. REALM=${REALM:-ORG}
  30. # superseded by redpill config mailhost
  31. TLS_CHAIN_FILES=${TLS_CHAIN_FILES:-}
  32. # TODO: maybe use AMaViS default of 20 (and instead tune spamassassin)
  33. # <https://www.ijs.si/software/amavisd/amavisd-new-docs.html#max_requests>
  34. AMAVIS_MAXPROC=${AMAVIS_MAXPROC:-2}
  35. # TODO: check if DKIMPROXY_MAXPROC_IN should be handled too
  36. #DKIMPROXY_MAXPROC_IN=5
  37. DKIMPROXY_MAXPROC_OUT=5
  38. # resolve redpill config paths
  39. realmsdir="/etc/local-$REDPILL"
  40. thisrealm=$(cat "/etc/local-$REALM/realm" || dnsdomainname | tr '[:lower:]' '[:upper:]')
  41. if ! [ -d "$realmsdir" ]; then
  42. warn "Realms directory \"$realmsdir\" does not exist."
  43. fi
  44. catfilefromownrealm() {
  45. set -e
  46. file=$1
  47. [ -d "$realmsdir" ] || exit 0
  48. cat "$realmsdir/$thisrealm/$file"
  49. }
  50. catallfilesfromotherrealms() {
  51. set -e
  52. file=$1
  53. [ -d "$realmsdir" ] || exit 0
  54. [ -f "$realmsdir/realms" ] || exit 0
  55. realms=$(sed 's/#.*//' < "$realmsdir/realms")
  56. for realm in $realms; do
  57. if [ "$thisrealm" != "$realm" ]; then
  58. cat "$realmsdir/$realm/$file"
  59. fi
  60. done
  61. }
  62. catfirstfile() {
  63. set -e
  64. file=$1
  65. context=${2:-postfix}
  66. configdir=
  67. for dir in /etc/local "/etc/local-$REALM" "/etc/local-$REDPILL" /etc/local-COMMON; do
  68. if [ -d "$dir/$context" ] && [ -f "$dir/$context/$file" ]; then
  69. configdir="$dir/$context"
  70. break
  71. fi
  72. done
  73. if [ -z "$configdir" ]; then
  74. exit1 "ERROR: file \"$file\" not found."
  75. fi
  76. cat "$configdir/$file"
  77. }
  78. # TODO: support trailing comment.
  79. getperlvarfromfile() {
  80. set -e
  81. var=$1
  82. default=$2
  83. file=$3
  84. context=${4:-postfix}
  85. catfirstfile "$file" "$context" \
  86. | var=$var default=$default perl -n \
  87. -e '/^\$$ENV{"var"}\h*=\h*(\d+)/ and $s=$1; END {print length($s) ? $s : $ENV{"default"}}'
  88. }
  89. getlinesfromfile() {
  90. set -e
  91. param=$1
  92. shift
  93. replacements=
  94. for subparam in "$@"; do
  95. case "$subparam" in
  96. *=)
  97. oldparam=$(echo "$subparam" | awk -F= '{print $1}')
  98. replacements="$replacements;s/,*[^,]*${oldparam}[^,]*,*/,/"
  99. continue
  100. ;;
  101. *=*=*)
  102. oldparam=$(echo "$subparam" | awk -F= '{print $1}')
  103. newparam=$(echo "$subparam" | awk -F= '{print $2}')
  104. newparamfile=$(echo "$subparam" | awk -F= '{print $3}')
  105. ;;
  106. *)
  107. oldparam=$subparam
  108. newparam=$subparam
  109. newparamfile=$subparam
  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. printf "%s = " "$param"
  120. catfirstfile "$param" | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e 's/\^/ /g' -e "s/,\$//$replacements"
  121. }
  122. thismailhost="$(cat /etc/local/mailhost || catfilefromownrealm mailhost)"
  123. #ENABLE_POSTGREY=
  124. #if [ -x /usr/sbin/postgrey ]; then
  125. # # FIXME: Use this somehow, and only warn below
  126. # ENABLE_POSTGREY=1
  127. #else
  128. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  129. #fi
  130. dovecot=
  131. ENABLE_DOVECOT_LMTP=
  132. ENABLE_DOVECOT_DELIVER=
  133. if [ -x /usr/sbin/dovecot ]; then
  134. dovecot=1
  135. if [ -x /usr/lib/dovecot/lmtp ]; then
  136. ENABLE_DOVECOT_LMTP=1
  137. elif [ -x /usr/lib/dovecot/deliver ]; then
  138. warn "Dovecot LMTP missing - (Debian package dovecot-lmtp)."
  139. ENABLE_DOVECOT_DELIVER=1
  140. else
  141. warn "Dovecot deliver missing."
  142. fi
  143. else
  144. warn "Dovecot missing - (Debian package dovecot-core or dovecot)."
  145. fi
  146. ENABLE_TLS=
  147. [ -z "$thismailhost" ] || TLS_CHAIN_FILES="/etc/ssl/private/$thismailhost.chain.key"
  148. if [ -f "$TLS_CHAIN_FILES" ]; then
  149. ENABLE_TLS=1
  150. else
  151. warn "No TLS - requires key chain \"$TLS_CHAIN_FILES\"."
  152. fi
  153. # TODO: enable only on systems with user accounts
  154. ENABLE_SUBMISSION=1
  155. # TODO: check that dovecot SASL is configured
  156. ENABLE_SASL_INBOUND=
  157. if [ -n "$ENABLE_TLS" ] && [ -n "$dovecot" ] && [ -n "$ENABLE_SUBMISSION" ]; then
  158. ENABLE_SASL_INBOUND=1
  159. else
  160. warn "No inbound SASL authentication - requires TLS encryption and Dovecot."
  161. fi
  162. ENABLE_AMAVIS=
  163. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  164. ENABLE_AMAVIS=1
  165. else
  166. warn "AMaViS missing (Debian package amavisd-new)."
  167. fi
  168. ENABLE_DKIMPROXY=
  169. if [ -x /usr/sbin/dkimproxy.in ]; then
  170. if [ "1" = "$ENABLE_AMAVIS" ]; then
  171. ENABLE_DKIMPROXY=1
  172. else
  173. warn "No DKIM/Domainkey - requires DKIMproxy and AMaViS."
  174. fi
  175. else
  176. warn "DKIMproxy missing (Debian package dkimproxy)."
  177. fi
  178. confdir=/etc/postfix
  179. _postconf() {
  180. postconf -c "$tempdir" "$1" "$(echo "$2" | tr '\n' ' ' | sed -e 's/ $//')"
  181. }
  182. postmapfiles=
  183. tempdir=$(mktemp -td postfix.XXXXXX)
  184. cp -a -t "$tempdir" "$confdir"/*
  185. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  186. _postconf -e smtpd_banner="\$myhostname NO UCE ESMTP \$mail_name (Debian/GNU)"
  187. _postconf -e "$(getlinesfromfile permit_mx_backup_networks)"
  188. _postconf -e "$(getlinesfromfile smtpd_client_restrictions reject_rhsbl_client)"
  189. _postconf -e "$(getlinesfromfile smtpd_helo_restrictions)"
  190. _postconf -e "$(getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender ${ENABLE_DKIMPROXY:-sender_access_regex=})"
  191. _postconf -e "$(getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains)"
  192. _postconf -e "$(getlinesfromfile smtpd_data_restrictions)"
  193. # FIXME: clear only specific line (not whole file) when dkimproxy unused
  194. if [ -f "$tempdir/sender_access_regex" ]; then
  195. if [ -n "$ENABLE_DKIMPROXY" ]; then
  196. grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex" \
  197. || echo '/^/ FILTER dkimsign:[127.0.0.1]:10026' >> "$tempdir/sender_access_regex"
  198. else
  199. if grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex"; then
  200. echo "echo '' > \"$confdir/sender_access_regex\"" >> "$tempdir/COMMANDS"
  201. rm "$tempdir/sender_access_regex"
  202. fi
  203. fi
  204. postmapfiles="$postmapfiles sender_access_regex"
  205. fi
  206. # Support exceptions to default response
  207. # (Day Old Bread (dob) lists need to reject only temporarily)
  208. _postconf -e rbl_reply_maps="hash:$confdir/rbl_reply_map"
  209. sed 's/#.*//' \
  210. < /etc/local-COMMON/postfix/rbl_reply_map \
  211. > "$tempdir/rbl_reply_map"
  212. postmapfiles="$postmapfiles rbl_reply_map"
  213. # Verify senders of common suspicious and known verifiable domains
  214. # (exclude verification of postmaster@ to not verify verification probes)
  215. # (add own domains before peers for (rare) cases of duplicates)
  216. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  217. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  218. grep -v yahoo \
  219. < /etc/local-COMMON/postfix/maildomains \
  220. | sort \
  221. | sed 's/$/ reject_unverified_sender/' \
  222. > "$tempdir/sender_access"
  223. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  224. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' \
  225. >> "$tempdir/sender_access"
  226. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" \
  227. >> "$tempdir/sender_access"
  228. postmapfiles="$postmapfiles sender_access"
  229. _postconf -e unverified_sender_reject_code=550
  230. # Trust recipient verification too
  231. _postconf -e unverified_recipient_reject_code=550
  232. if [ -n "$ENABLE_DOVECOT_LMTP" ]; then
  233. _postconf -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
  234. _postconf -X mailbox_command
  235. elif [ -n "$ENABLE_DOVECOT_DELIVER" ]; then
  236. _postconf -X mailbox_transport
  237. _postconf -e mailbox_command=/usr/lib/dovecot/deliver
  238. else
  239. _postconf -X mailbox_transport
  240. _postconf -X mailbox_command
  241. fi
  242. # outbound opportunistic encryption
  243. _postconf -e smtp_tls_security_level=may
  244. if [ -n "$ENABLE_TLS" ]; then
  245. _postconf -e smtp_tls_chain_files="$TLS_CHAIN_FILES"
  246. _postconf -e smtp_tls_CApath=/etc/ssl/certs
  247. else
  248. _postconf -X smtp_tls_chain_files
  249. fi
  250. _postconf -e smtp_tls_loglevel=1
  251. # enforce TLS trust path towards peers
  252. catallfilesfromotherrealms mailhost | sort \
  253. | sed 's/^/[/;s/$/]:submission secure/' \
  254. > "$tempdir/tls_policy"
  255. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" \
  256. >> "$tempdir/tls_policy"
  257. postmapfiles="$postmapfiles tls_policy"
  258. _postconf -e smtp_tls_policy_maps="hash:$confdir/tls_policy"
  259. # inbound opportunistic encryption
  260. if [ -n "$ENABLE_TLS" ]; then
  261. _postconf -e smtpd_tls_security_level=may
  262. _postconf -e smtpd_tls_chain_files="$TLS_CHAIN_FILES"
  263. _postconf -e smtpd_tls_loglevel=1
  264. _postconf -e smtpd_tls_auth_only=yes
  265. _postconf -e smtpd_tls_received_header=yes
  266. else
  267. _postconf -e smtpd_tls_security_level=none
  268. _postconf -X smtpd_tls_chain_files
  269. _postconf -X smtpd_tls_loglevel
  270. _postconf -X smtpd_tls_auth_only
  271. _postconf -X smtpd_tls_received_header
  272. fi
  273. # Avoid smtpd_tls_CApath or smtpd_tls_CAfile to trick outlook.com
  274. # See <http://postfix.1071664.n5.nabble.com/Problems-with-incoming-mails-from-outlook-com-td78356.html>
  275. _postconf -X smtpd_tls_CApath
  276. _postconf -X smtpd_tls_CAfile
  277. # obsolete TLS-related settings
  278. _postconf -X smtpd_use_tls
  279. _postconf -X lmtp_tls_CApath
  280. _postconf -X smtp_tls_CAfile
  281. _postconf -X lmtp_tls_CAfile
  282. _postconf -X smtp_tls_cert_file
  283. _postconf -X smtp_tls_key_file
  284. _postconf -X smtpd_tls_cert_file
  285. _postconf -X smtpd_tls_key_file
  286. _postconf -X smtpd_tls_ask_ccert
  287. _postconf -X smtp_tls_note_starttls_offer
  288. _postconf -X smtpd_tls_session_cache_database
  289. _postconf -X smtpd_tls_session_cache_timeout
  290. _postconf -X smtp_tls_session_cache_database
  291. _postconf -X tls_random_exchange_name
  292. _postconf -X tls_random_source
  293. # submission
  294. # <http://www.postfix.org/SASL_README.html>
  295. # <https://doc.dovecot.org/configuration_manual/howto/postfix_and_dovecot_sasl/#using-sasl-with-postfix-submission-port>
  296. if [ -n "$ENABLE_SUBMISSION" ]; then
  297. _postconf -Me submission/inet="
  298. submission inet n - y - - smtpd
  299. -o syslog_name=postfix/\$service_name
  300. -o smtpd_tls_security_level=encrypt
  301. "
  302. _postconf -Me smtps/inet="
  303. smtps inet n - y - - smtpd
  304. -o syslog_name=postfix/\$service_name
  305. -o smtpd_tls_wrappermode=yes
  306. -o smtpd_sasl_auth_enable=yes
  307. "
  308. if [ -n "$ENABLE_SASL_INBOUND" ]; then
  309. _postconf -Pe submission/inet/smtpd_sasl_auth_enable=yes
  310. _postconf -Pe smtps/inet/smtpd_sasl_auth_enable=yes
  311. else
  312. _postconf -PX submission/inet/smtpd_sasl_auth_enable
  313. _postconf -PX smtps/inet/smtpd_sasl_auth_enable
  314. fi
  315. else
  316. _postconf -MX submission/inet
  317. _postconf -MX smtps/inet
  318. fi
  319. if [ -n "$ENABLE_SASL_INBOUND" ]; then
  320. _postconf -e smtpd_sasl_local_domain=\$mydomain
  321. _postconf -e smtpd_sasl_type=dovecot
  322. _postconf -e smtpd_sasl_path=private/auth
  323. _postconf -e broken_sasl_auth_clients=yes
  324. else
  325. _postconf -X smtpd_sasl_local_domain
  326. _postconf -X smtpd_sasl_type
  327. _postconf -X smtpd_sasl_path
  328. _postconf -X broken_sasl_auth_clients
  329. fi
  330. # obsolete SASL-related settings
  331. _postconf -X smtpd_sasl_auth_enable
  332. _postconf -X smtpd_sasl_security_options
  333. if [ -n "$ENABLE_AMAVIS" ]; then
  334. amavis_maxproc=$(getperlvarfromfile max_servers "$AMAVIS_MAXPROC" amavisd.conf.addon amavis)
  335. _postconf -Me amavisfeed/unix="
  336. amavisfeed unix - - n - $amavis_maxproc lmtp
  337. -o lmtp_data_done_timeout=1200s
  338. -o lmtp_send_xforward_command=yes
  339. -o disable_dns_lookups=yes
  340. -o max_use=$amavis_maxproc
  341. "
  342. _postconf -Me 127.0.0.1:10025/inet='
  343. 127.0.0.1:10025 inet n - n - - smtpd
  344. -o content_filter=
  345. -o local_recipient_maps=
  346. -o relay_recipient_maps=
  347. -o smtpd_restriction_classes=
  348. -o smtpd_delay_reject=no
  349. -o smtpd_client_restrictions=permit_mynetworks,reject
  350. -o smtpd_helo_restrictions=
  351. -o smtpd_sender_restrictions=
  352. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  353. -o smtpd_data_restrictions=reject_unauth_pipelining
  354. -o smtpd_end_of_data_restrictions=
  355. -o mynetworks=127.0.0.0/8
  356. -o smtpd_error_sleep_time=0
  357. -o smtpd_soft_error_limit=1001
  358. -o smtpd_hard_error_limit=1000
  359. -o smtpd_client_connection_count_limit=0
  360. -o smtpd_client_connection_rate_limit=0
  361. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  362. '
  363. _postconf -e receive_override_options=no_address_mappings
  364. if [ -n "$ENABLE_DKIMPROXY" ]; then
  365. _postconf -e content_filter='amavisfeed:[127.0.0.1]:10028'
  366. _postconf -Pe pickup/unix/content_filter='dkimsign:127.0.0.1:10028'
  367. if [ -n "$ENABLE_SUBMISSION" ]; then
  368. _postconf -Pe submission/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  369. _postconf -Pe smtps/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  370. fi
  371. # FIXME: supersede DKIMPROXY_MAXPROC_OUT from /etc/default/dkimproxy
  372. _postconf -Me dkimsign/unix="
  373. dkimsign unix - - n - $DKIMPROXY_MAXPROC_OUT smtp
  374. -o smtp_send_xforward_command=yes
  375. -o smtp_discard_ehlo_keywords=8bitmime,starttls
  376. "
  377. _postconf -Me 127.0.0.1:10029/inet='
  378. 127.0.0.1:10029 inet n - n - - smtpd
  379. -o content_filter=
  380. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  381. -o smtpd_helo_restrictions=
  382. -o smtpd_client_restrictions=
  383. -o smtpd_sender_restrictions=
  384. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  385. -o mynetworks=127.0.0.0/8
  386. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  387. '
  388. else
  389. _postconf -MX dkimsign/unix
  390. _postconf -MX 127.0.0.1:10029/inet
  391. _postconf -e content_filter='amavisfeed:[127.0.0.1]:10024'
  392. _postconf -PX pickup/unix/content_filter
  393. if [ -n "$ENABLE_SUBMISSION" ]; then
  394. _postconf -PX submission/inet/content_filter
  395. _postconf -PX smtps/inet/content_filter
  396. fi
  397. fi
  398. else
  399. _postconf -MX amavisfeed/unix
  400. _postconf -MX 127.0.0.1:10025/inet
  401. _postconf -X content_filter
  402. _postconf -X receive_override_options
  403. fi
  404. # obsolete settings
  405. _postconf -X smtpd_helo_required
  406. _postconf -X maps_rbl_domains
  407. _postconf -X max_use
  408. _postconf -MX smtp-amavis/unix
  409. diff -ruNw "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload=1; fi
  410. if [ "1" = "$FORCE" ]; then
  411. do_update=y
  412. elif [ "1" = "$needs_reload" ]; then
  413. printf 'Above is the intended changes. OK to update (y/N)? '
  414. read -r do_update
  415. fi
  416. case $do_update in
  417. y|Y)
  418. if [ -f "$tempdir/COMMANDS" ]; then
  419. sh -s < "$tempdir/COMMANDS"
  420. fi
  421. rm -f "$tempdir/COMMANDS"
  422. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart=1; fi
  423. cp -a -f -t "$confdir" "$tempdir"/*
  424. rm -rf "$tempdir"
  425. for file in $postmapfiles; do
  426. postmap "$confdir/$file"
  427. done
  428. if [ "1" = "$needs_restart" ]; then
  429. service postfix restart
  430. else
  431. service postfix force-reload
  432. fi
  433. if [ "1" = "$needs_reload" ]; then
  434. echo >&2 "Changes applied!"
  435. fi
  436. ;;
  437. *)
  438. if [ "1" = "$needs_reload" ]; then
  439. exit1 "Aborted!"
  440. fi
  441. ;;
  442. esac
  443. if [ "1" != "$needs_reload" ]; then
  444. echo >&2 "No changes needed!"
  445. fi
  446. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  447. # Support for trusted MX backup networks added
  448. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  449. # RBLs replaced with those recommended by http://www.antispews.org/
  450. # spam filter based on these: http://www.postfix.org/FILTER_README.html
  451. # https://www.ijs.si/software/amavisd/amavisd-new-docs.html
  452. # TLS based on this: http://www.postfix.org/TLS_README.html
  453. # Here's a convenient overview of different blackholes:
  454. # http://rbls.org/