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