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