summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 698ed2044095ced67ebdb4b9ac4c3239ce9bb115 (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. # Some badly configured setup use hostname instead of FQDN
  188. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  189. #if _postconf myhostname | grep -q '\.'; then
  190. # _postconf -e smtpd_helo_required=yes
  191. #fi
  192. _postconf -e smtpd_helo_required=no
  193. _postconf -e "$(getlinesfromfile permit_mx_backup_networks)"
  194. _postconf -e maps_rbl_domains=
  195. _postconf -e "$(getlinesfromfile smtpd_client_restrictions reject_rhsbl_client)"
  196. _postconf -e "$(getlinesfromfile smtpd_helo_restrictions)"
  197. _postconf -e "$(getlinesfromfile smtpd_sender_restrictions reject_rhsbl_sender ${ENABLE_DKIMPROXY:-sender_access_regex=})"
  198. _postconf -e "$(getlinesfromfile smtpd_recipient_restrictions reject_maps_rbl=reject_rbl_client=maps_rbl_domains)"
  199. _postconf -e "$(getlinesfromfile smtpd_data_restrictions)"
  200. # FIXME: clear only specific line (not whole file) when dkimproxy unused
  201. if [ -f "$tempdir/sender_access_regex" ]; then
  202. if [ -n "$ENABLE_DKIMPROXY" ]; then
  203. grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex" \
  204. || echo '/^/ FILTER dkimsign:[127.0.0.1]:10026' >> "$tempdir/sender_access_regex"
  205. else
  206. if grep -q -F '/^/ FILTER dkimsign:[127.0.0.1]:10026' "$tempdir/sender_access_regex"; then
  207. echo "echo '' > \"$confdir/sender_access_regex\"" >> "$tempdir/COMMANDS"
  208. rm "$tempdir/sender_access_regex"
  209. fi
  210. fi
  211. postmapfiles="$postmapfiles sender_access_regex"
  212. fi
  213. # Support exceptions to default response
  214. # (Day Old Bread (dob) lists need to reject only temporarily)
  215. _postconf -e rbl_reply_maps="hash:$confdir/rbl_reply_map"
  216. sed 's/#.*//' \
  217. < /etc/local-COMMON/postfix/rbl_reply_map \
  218. > "$tempdir/rbl_reply_map"
  219. postmapfiles="$postmapfiles rbl_reply_map"
  220. # Verify senders of common suspicious and known verifiable domains
  221. # (exclude verification of postmaster@ to not verify verification probes)
  222. # (add own domains before peers for (rare) cases of duplicates)
  223. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  224. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  225. grep -v yahoo \
  226. < /etc/local-COMMON/postfix/maildomains \
  227. | sort \
  228. | sed 's/$/ reject_unverified_sender/' \
  229. > "$tempdir/sender_access"
  230. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  231. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' \
  232. >> "$tempdir/sender_access"
  233. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" \
  234. >> "$tempdir/sender_access"
  235. postmapfiles="$postmapfiles sender_access"
  236. _postconf -e unverified_sender_reject_code=550
  237. # Trust recipient verification too
  238. _postconf -e unverified_recipient_reject_code=550
  239. if [ -n "$ENABLE_DOVECOT_LMTP" ]; then
  240. _postconf -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
  241. _postconf -X mailbox_command
  242. elif [ -n "$ENABLE_DOVECOT_DELIVER" ]; then
  243. _postconf -X mailbox_transport
  244. _postconf -e mailbox_command=/usr/lib/dovecot/deliver
  245. else
  246. _postconf -X mailbox_transport
  247. _postconf -X mailbox_command
  248. fi
  249. # outbound opportunistic encryption
  250. _postconf -e smtp_tls_security_level=may
  251. if [ -n "$ENABLE_TLS" ]; then
  252. _postconf -e smtp_tls_chain_files="$TLS_CHAIN_FILES"
  253. _postconf -e smtp_tls_CAPath=/etc/ssl/certs
  254. else
  255. _postconf -X smtp_tls_chain_files
  256. fi
  257. _postconf -e smtp_tls_loglevel=1
  258. # enforce TLS trust path towards peers
  259. catallfilesfromotherrealms mailhost | sort \
  260. | sed 's/^/[/;s/$/]:submission secure/' \
  261. > "$tempdir/tls_policy"
  262. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" \
  263. >> "$tempdir/tls_policy"
  264. postmapfiles="$postmapfiles tls_policy"
  265. _postconf -e smtp_tls_policy_maps="hash:$confdir/tls_policy"
  266. # inbound opportunistic encryption
  267. if [ -n "$ENABLE_TLS" ]; then
  268. _postconf -e smtpd_tls_security_level=may
  269. _postconf -e smtpd_tls_chain_files="$TLS_CHAIN_FILES"
  270. _postconf -e smtpd_tls_loglevel=1
  271. _postconf -e smtpd_tls_auth_only=yes
  272. _postconf -e smtpd_tls_received_header=yes
  273. else
  274. _postconf -e smtpd_tls_security_level=none
  275. _postconf -X smtpd_tls_chain_files
  276. _postconf -X smtpd_tls_loglevel
  277. _postconf -X smtpd_tls_auth_only
  278. _postconf -X smtpd_tls_received_header
  279. fi
  280. # Avoid smtpd_tls_CApath or smtpd_tls_CAfile to trick outlook.com
  281. # See <http://postfix.1071664.n5.nabble.com/Problems-with-incoming-mails-from-outlook-com-td78356.html>
  282. _postconf -X smtpd_tls_CApath
  283. _postconf -X smtpd_tls_CAfile
  284. # obsolete TLS-related settings
  285. _postconf -X smtpd_use_tls
  286. _postconf -X lmtp_tls_CApath
  287. _postconf -X smtp_tls_CAfile
  288. _postconf -X lmtp_tls_CAfile
  289. _postconf -X smtp_tls_cert_file
  290. _postconf -X smtp_tls_key_file
  291. _postconf -X smtpd_tls_cert_file
  292. _postconf -X smtpd_tls_key_file
  293. _postconf -X smtpd_tls_ask_ccert
  294. _postconf -X smtp_tls_note_starttls_offer
  295. _postconf -X smtpd_tls_session_cache_database
  296. _postconf -X smtpd_tls_session_cache_timeout
  297. _postconf -X smtp_tls_session_cache_database
  298. _postconf -X tls_random_exchange_name
  299. _postconf -X tls_random_source
  300. # submission
  301. # <http://www.postfix.org/SASL_README.html>
  302. # <https://doc.dovecot.org/configuration_manual/howto/postfix_and_dovecot_sasl/#using-sasl-with-postfix-submission-port>
  303. if [ -n "$ENABLE_SUBMISSION" ]; then
  304. _postconf -Me submission/inet="
  305. submission inet n - y - - smtpd
  306. -o syslog_name=postfix/\$service_name
  307. -o smtpd_tls_security_level=encrypt
  308. "
  309. _postconf -Me smtps/inet="
  310. smtps inet n - y - - smtpd
  311. -o syslog_name=postfix/\$service_name
  312. -o smtpd_tls_wrappermode=yes
  313. -o smtpd_sasl_auth_enable=yes
  314. "
  315. if [ -n "$ENABLE_SASL_INBOUND" ]; then
  316. _postconf -Pe submission/inet/smtpd_sasl_auth_enable=yes
  317. _postconf -Pe smtps/inet/smtpd_sasl_auth_enable=yes
  318. else
  319. _postconf -PX submission/inet/smtpd_sasl_auth_enable
  320. _postconf -PX smtps/inet/smtpd_sasl_auth_enable
  321. fi
  322. else
  323. _postconf -MX submission/inet
  324. _postconf -MX smtps/inet
  325. fi
  326. if [ -n "$ENABLE_SASL_INBOUND" ]; then
  327. _postconf -e smtpd_sasl_local_domain=\$mydomain
  328. _postconf -e smtpd_sasl_type=dovecot
  329. _postconf -e smtpd_sasl_path=private/auth
  330. _postconf -e broken_sasl_auth_clients=yes
  331. else
  332. _postconf -X smtpd_sasl_local_domain
  333. _postconf -X smtpd_sasl_type
  334. _postconf -X smtpd_sasl_path
  335. _postconf -X broken_sasl_auth_clients
  336. fi
  337. # obsolete SASL-related settings
  338. _postconf -X smtpd_sasl_auth_enable
  339. _postconf -X smtpd_sasl_security_options
  340. if [ -n "$ENABLE_AMAVIS" ]; then
  341. amavis_maxproc=$(getperlvarfromfile max_servers "$AMAVIS_MAXPROC" amavisd.conf.addon amavis)
  342. _postconf -Me amavisfeed/unix="
  343. amavisfeed unix - - n - $amavis_maxproc lmtp
  344. -o lmtp_data_done_timeout=1200s
  345. -o lmtp_send_xforward_command=yes
  346. -o disable_dns_lookups=yes
  347. -o max_use=$amavis_maxproc
  348. "
  349. _postconf -Me 127.0.0.1:10025/inet='
  350. 127.0.0.1:10025 inet n - n - - smtpd
  351. -o content_filter=
  352. -o local_recipient_maps=
  353. -o relay_recipient_maps=
  354. -o smtpd_restriction_classes=
  355. -o smtpd_delay_reject=no
  356. -o smtpd_client_restrictions=permit_mynetworks,reject
  357. -o smtpd_helo_restrictions=
  358. -o smtpd_sender_restrictions=
  359. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  360. -o smtpd_data_restrictions=reject_unauth_pipelining
  361. -o smtpd_end_of_data_restrictions=
  362. -o mynetworks=127.0.0.0/8
  363. -o smtpd_error_sleep_time=0
  364. -o smtpd_soft_error_limit=1001
  365. -o smtpd_hard_error_limit=1000
  366. -o smtpd_client_connection_count_limit=0
  367. -o smtpd_client_connection_rate_limit=0
  368. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  369. '
  370. _postconf -e receive_override_options=no_address_mappings
  371. if [ -n "$ENABLE_DKIMPROXY" ]; then
  372. _postconf -e content_filter='amavisfeed:[127.0.0.1]:10028'
  373. _postconf -Pe pickup/fifo/content_filter='dkimsign:127.0.0.1:10028'
  374. if [ -n "$ENABLE_SUBMISSION" ]; then
  375. _postconf -Pe submission/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  376. _postconf -Pe smtps/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  377. fi
  378. # FIXME: supersede DKIMPROXY_MAXPROC_OUT from /etc/default/dkimproxy
  379. _postconf -Me dkimsign/unix="
  380. dkimsign unix - - n - $DKIMPROXY_MAXPROC_OUT smtp
  381. -o smtp_send_xforward_command=yes
  382. -o smtp_discard_ehlo_keywords=8bitmime,starttls
  383. "
  384. _postconf -Me 127.0.0.1:10029/inet='
  385. 127.0.0.1:10029 inet n - n - - smtpd
  386. -o content_filter=
  387. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  388. -o smtpd_helo_restrictions=
  389. -o smtpd_client_restrictions=
  390. -o smtpd_sender_restrictions=
  391. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  392. -o mynetworks=127.0.0.0/8
  393. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  394. '
  395. else
  396. _postconf -MX dkimsign/unix
  397. _postconf -MX 127.0.0.1:10029/inet
  398. _postconf -e content_filter='amavisfeed:[127.0.0.1]:10024'
  399. _postconf -PX pickup/fifo/content_filter
  400. if [ -n "$ENABLE_SUBMISSION" ]; then
  401. _postconf -PX submission/inet/content_filter
  402. _postconf -PX smtps/inet/content_filter
  403. fi
  404. fi
  405. else
  406. _postconf -MX amavisfeed/unix
  407. _postconf -MX 127.0.0.1:10025/inet
  408. _postconf -X content_filter
  409. _postconf -X receive_override_options
  410. fi
  411. # obsolete settings
  412. _postconf -X max_use
  413. _postconf -MX smtp-amavis/unix
  414. diff -ruNw "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload=1; fi
  415. if [ "1" = "$FORCE" ]; then
  416. do_update=y
  417. elif [ "1" = "$needs_reload" ]; then
  418. printf 'Above is the intended changes. OK to update (y/N)? '
  419. read -r do_update
  420. fi
  421. case $do_update in
  422. y|Y)
  423. if [ -f "$tempdir/COMMANDS" ]; then
  424. sh -s < "$tempdir/COMMANDS"
  425. fi
  426. rm -f "$tempdir/COMMANDS"
  427. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart=1; fi
  428. cp -a -f -t "$confdir" "$tempdir"/*
  429. rm -rf "$tempdir"
  430. for file in $postmapfiles; do
  431. postmap "$confdir/$file"
  432. done
  433. if [ "1" = "$needs_restart" ]; then
  434. service postfix restart
  435. else
  436. service postfix force-reload
  437. fi
  438. if [ "1" = "$needs_reload" ]; then
  439. echo >&2 "Changes applied!"
  440. fi
  441. ;;
  442. *)
  443. if [ "1" = "$needs_reload" ]; then
  444. exit1 "Aborted!"
  445. fi
  446. ;;
  447. esac
  448. if [ "1" != "$needs_reload" ]; then
  449. echo >&2 "No changes needed!"
  450. fi
  451. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  452. # Support for trusted MX backup networks added
  453. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  454. # RBLs replaced with those recommended by http://www.antispews.org/
  455. # spam filter based on these: http://www.postfix.org/FILTER_README.html
  456. # https://www.ijs.si/software/amavisd/amavisd-new-docs.html
  457. # TLS based on this: http://www.postfix.org/TLS_README.html
  458. # Here's a convenient overview of different blackholes:
  459. # http://rbls.org/