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