summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 4a54774bf0c6d076ae89131205e1c6b50327daea (plain)
  1. #!/bin/sh
  2. #
  3. # /etc/local-COMMON/postfix/postfix.sh
  4. # Copyright 2002-2007 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: postfix.sh,v 1.76 2008-05-25 19:00:16 jonas Exp $
  7. #
  8. # Auto-tweak plain installed postfix Debian package
  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. TLS_CERT=${TLS_CERT:-/etc/ssl/certs/postfix.pem}
  31. TLS_KEY=${TLS_KEY:-/etc/ssl/private/postfix.pem}
  32. realmsdir="/etc/local-$REDPILL"
  33. thisrealm=$(cat "/etc/local-$REALM/realm" || dnsdomainname | tr '[:lower:]' '[:upper:]')
  34. confdir=/etc/postfix
  35. if ! [ -d "$realmsdir" ]; then
  36. warn "Realms directory \"$realmsdir\" does not exist."
  37. fi
  38. #postgrey=
  39. #if [ -x /usr/sbin/postgrey ]; then
  40. # # FIXME: Use this somehow, and only warn below
  41. # postgrey=1
  42. #else
  43. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  44. #fi
  45. dovecot=
  46. dovecot_lmtp=
  47. dovecot_deliver=
  48. if [ -x /usr/sbin/dovecot ]; then
  49. dovecot=1
  50. if [ -x /usr/lib/dovecot/lmtp ]; then
  51. dovecot_lmtp=1
  52. elif [ -x /usr/lib/dovecot/deliver ]; then
  53. warn "Dovecot LMTP missing - (Debian package dovecot-lmtp)."
  54. dovecot_deliver=1
  55. else
  56. warn "Dovecot deliver missing."
  57. fi
  58. else
  59. warn "Dovecot missing - (Debian package dovecot-core or dovecot)."
  60. fi
  61. tls_cert=
  62. if [ -f "$TLS_CERT" ] && [ -f "$TLS_KEY" ]; then
  63. tls_cert=1
  64. else
  65. warn "No TLS - requires certificate \"$TLS_CERT\" and key \"$TLS_KEY\"."
  66. fi
  67. # TODO: enable only on systems with user accounts
  68. submission=1
  69. # TODO: check that dovecot SASL is configured
  70. sasl_inbound=
  71. if [ -n "$tls_cert" ] && [ -n "$dovecot" ] && [ -n "$submission" ]; then
  72. sasl_inbound=1
  73. else
  74. warn "No inbound SASL authentication - requires TLS encryption and Dovecot."
  75. fi
  76. amavis=
  77. # TODO: maybe use AMaViS default of 20 (instead tuning spamassassin)
  78. # <https://www.ijs.si/software/amavisd/amavisd-new-docs.html#max_requests>
  79. default_amavis_maxproc=2
  80. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  81. amavis=1
  82. else
  83. warn "AMaViS missing (Debian package amavisd-new)."
  84. fi
  85. dkimproxy=
  86. #dkimproxy_maxproc_in=5
  87. # FIXME: update actual outgoing maxproc from /etc/default/dkimproxy (and /etc/dkimproxy/dkimproxy_out.conf too?)
  88. dkimproxy_maxproc_out=5
  89. if [ -x /usr/sbin/dkimproxy.in ]; then
  90. if [ "1" = "$amavis" ]; then
  91. dkimproxy=1
  92. else
  93. warn "No DKIM/Domainkey - requires DKIMproxy and AMaViS."
  94. fi
  95. else
  96. warn "DKIMproxy missing (Debian package dkimproxy)."
  97. fi
  98. catfilefromownrealm() {
  99. set -e
  100. file=$1
  101. [ -d "$realmsdir" ] || exit 0
  102. cat "$realmsdir/$thisrealm/$file"
  103. }
  104. catallfilesfromotherrealms() {
  105. set -e
  106. file=$1
  107. [ -d "$realmsdir" ] || exit 0
  108. [ -f "$realmsdir/realms" ] || exit 0
  109. realms=$(sed 's/#.*//' < "$realmsdir/realms")
  110. for realm in $realms; do
  111. if [ "$thisrealm" != "$realm" ]; then
  112. cat "$realmsdir/$realm/$file"
  113. fi
  114. done
  115. }
  116. catfirstfile() {
  117. set -e
  118. file=$1
  119. context=${2:-postfix}
  120. configdir=
  121. for dir in /etc/local "/etc/local-$REALM" "/etc/local-$REDPILL" /etc/local-COMMON; do
  122. if [ -d "$dir/$context" ] && [ -f "$dir/$context/$file" ]; then
  123. configdir="$dir/$context"
  124. break
  125. fi
  126. done
  127. if [ -z "$configdir" ]; then
  128. exit1 "ERROR: file \"$file\" not found."
  129. fi
  130. cat "$configdir/$file"
  131. }
  132. # TODO: support trailing comment.
  133. getperlvarfromfile() {
  134. set -e
  135. var=$1
  136. default=$2
  137. file=$3
  138. context=${4:-postfix}
  139. catfirstfile "$file" "$context" \
  140. | var=$var default=$default perl -n \
  141. -e '/^\$$ENV{"var"}\h*=\h*(\d+)/ and $s=$1; END {print length($s) ? $s : $ENV{"default"}}'
  142. }
  143. getlinesfromfile() {
  144. set -e
  145. param=$1
  146. shift
  147. replacements=
  148. for subparam in "$@"; do
  149. case "$subparam" in
  150. *=)
  151. oldparam=$(echo "$subparam" | awk -F= '{print $1}')
  152. replacements="$replacements;s/,*[^,]*${oldparam}[^,]*,*/,/"
  153. continue
  154. ;;
  155. *=*=*)
  156. oldparam=$(echo "$subparam" | awk -F= '{print $1}')
  157. newparam=$(echo "$subparam" | awk -F= '{print $2}')
  158. newparamfile=$(echo "$subparam" | awk -F= '{print $3}')
  159. ;;
  160. *)
  161. oldparam=$subparam
  162. newparam=$subparam
  163. newparamfile=$subparam
  164. ;;
  165. esac
  166. newparamvalues=$(getlinesfromfile "$newparamfile" | sed -e 's/.*=[ ]*//' -e 's/,/ /g')
  167. newstring=
  168. for newparamvalue in $newparamvalues; do
  169. newstring="${newstring}$newparam $newparamvalue,"
  170. done
  171. replacements="$replacements;s/$oldparam/$newstring/"
  172. done
  173. printf "%s = " "$param"
  174. catfirstfile "$param" | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e 's/\^/ /g' -e "s/,\$//$replacements"
  175. }
  176. _postconf() {
  177. postconf -c "$tempdir" "$1" "$(echo "$2" | tr '\n' ' ')"
  178. }
  179. postmapfiles=
  180. tempdir=$(mktemp -td postfix.XXXXXX)
  181. cp -a -t "$tempdir" "$confdir"/*
  182. # Inspired by D. J. Bernstein: http://cr.yp.to/smtp/greeting.html
  183. _postconf -e smtpd_banner="\$myhostname NO UCE ESMTP \$mail_name (Debian/GNU)"
  184. # Some badly configured setup use hostname instead of FQDN
  185. # Disable completely: Effective, but hurts executive type guys using windows servers... :-(
  186. #if _postconf myhostname | grep -q '\.'; then
  187. # _postconf -e smtpd_helo_required=yes
  188. #fi
  189. _postconf -e smtpd_helo_required=no
  190. _postconf -e "$(getlinesfromfile permit_mx_backup_networks)"
  191. _postconf -e maps_rbl_domains=
  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 ${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 "$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 "$dovecot_lmtp" ]; then
  237. _postconf -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
  238. _postconf -X mailbox_command
  239. elif [ -n "$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 "$tls_cert" ]; then
  249. _postconf -e smtp_tls_cert_file="$TLS_CERT"
  250. _postconf -e smtp_tls_key_file="$TLS_KEY"
  251. else
  252. _postconf -X smtp_tls_cert_file
  253. _postconf -X smtp_tls_key_file
  254. fi
  255. _postconf -e smtp_tls_loglevel=1
  256. # enforce TLS trust path towards peers
  257. catallfilesfromotherrealms mailhost | sort \
  258. | sed 's/^/[/;s/$/]:submission secure/' \
  259. > "$tempdir/tls_policy"
  260. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" \
  261. >> "$tempdir/tls_policy"
  262. postmapfiles="$postmapfiles tls_policy"
  263. _postconf -e smtp_tls_policy_maps="hash:$confdir/tls_policy"
  264. # inbound opportunistic encryption
  265. if [ -n "$tls_cert" ]; then
  266. _postconf -e smtpd_tls_security_level=may
  267. _postconf -e smtpd_tls_cert_file="$TLS_CERT"
  268. _postconf -e smtpd_tls_key_file="$TLS_KEY"
  269. _postconf -e smtpd_tls_loglevel=1
  270. _postconf -e smtpd_tls_auth_only=yes
  271. _postconf -e smtpd_tls_received_header=yes
  272. else
  273. _postconf -e smtpd_tls_security_level=none
  274. _postconf -X smtpd_tls_cert_file
  275. _postconf -X smtpd_tls_key_file
  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 smtp_tls_CApath
  287. _postconf -X lmtp_tls_CApath
  288. _postconf -X smtp_tls_CAfile
  289. _postconf -X lmtp_tls_CAfile
  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 "$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 "$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 "$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 "$amavis" ]; then
  338. amavis_maxproc=$(getperlvarfromfile max_servers "$default_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 "$dkimproxy" ]; then
  369. _postconf -e content_filter='amavisfeed:[127.0.0.1]:10028'
  370. _postconf -Pe pickup/fifo/content_filter='dkimsign:127.0.0.1:10028'
  371. if [ -n "$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. _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/fifo/content_filter
  396. if [ -n "$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 max_use
  409. _postconf -MX smtp-amavis/unix
  410. diff -ruNw "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload=1; fi
  411. if [ "1" = "$FORCE" ]; then
  412. do_update=y
  413. elif [ "1" = "$needs_reload" ]; then
  414. printf 'Above is the intended changes. OK to update (y/N)? '
  415. read -r do_update
  416. fi
  417. case $do_update in
  418. y|Y)
  419. if [ -f "$tempdir/COMMANDS" ]; then
  420. sh -s < "$tempdir/COMMANDS"
  421. fi
  422. rm -f "$tempdir/COMMANDS"
  423. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart=1; fi
  424. cp -a -f -t "$confdir" "$tempdir"/*
  425. rm -rf "$tempdir"
  426. for file in $postmapfiles; do
  427. postmap "$confdir/$file"
  428. done
  429. if [ "1" = "$needs_restart" ]; then
  430. service postfix restart
  431. else
  432. service postfix force-reload
  433. fi
  434. if [ "1" = "$needs_reload" ]; then
  435. echo >&2 "Changes applied!"
  436. fi
  437. ;;
  438. *)
  439. if [ "1" = "$needs_reload" ]; then
  440. exit1 "Aborted!"
  441. fi
  442. ;;
  443. esac
  444. if [ "1" != "$needs_reload" ]; then
  445. echo >&2 "No changes needed!"
  446. fi
  447. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  448. # Support for trusted MX backup networks added
  449. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  450. # RBLs replaced with those recommended by http://www.antispews.org/
  451. # spam filter based on these: http://www.postfix.org/FILTER_README.html
  452. # https://www.ijs.si/software/amavisd/amavisd-new-docs.html
  453. # TLS based on this: http://www.postfix.org/TLS_README.html
  454. # Here's a convenient overview of different blackholes:
  455. # http://rbls.org/