summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 52559598bfac7285ef1052c4f8cde124cd9d9ef1 (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. REDPILL=${REDPILL:-REDPILL}
  28. REALM=${REALM:-ORG}
  29. TLS_CERT=${TLS_CERT:-/etc/ssl/certs/postfix.pem}
  30. TLS_KEY=${TLS_KEY:-/etc/ssl/private/postfix.pem}
  31. realmsdir="/etc/local-$REDPILL"
  32. thisrealm="$(cat "/etc/local-$REALM/realm" || dnsdomainname | tr '[:lower:]' '[:upper:]')"
  33. confdir="/etc/postfix"
  34. if ! [ -d "$realmsdir" ]; then
  35. warn "Realms directory \"$realmsdir\" does not exist."
  36. fi
  37. #postgrey=
  38. #if [ -x /usr/sbin/postgrey ]; then
  39. # # FIXME: Use this somehow, and only warn below
  40. # postgrey=1
  41. #else
  42. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  43. #fi
  44. dovecot=
  45. dovecot_lmtp=
  46. dovecot_deliver=
  47. if [ -x /usr/sbin/dovecot ]; then
  48. dovecot=1
  49. if [ -x /usr/lib/dovecot/lmtp ]; then
  50. dovecot_lmtp=1
  51. elif [ -x /usr/lib/dovecot/deliver ]; then
  52. warn "Dovecot LMTP missing - (Debian package dovecot-lmtp)."
  53. dovecot_deliver=1
  54. else
  55. warn "Dovecot deliver missing."
  56. fi
  57. else
  58. warn "Dovecot missing - (Debian package dovecot-core or dovecot)."
  59. fi
  60. tls_cert=
  61. if [ -f "$TLS_CERT" ] && [ -f "$TLS_KEY" ]; then
  62. tls_cert=1
  63. else
  64. warn "No TLS - requires certificate \"$TLS_CERT\" and key \"$TLS_KEY\"."
  65. fi
  66. # TODO: enable only on systems with user accounts
  67. submission=1
  68. # TODO: check that dovecot SASL is configured
  69. sasl_inbound=
  70. if [ -n "$tls_cert" ] && [ -n "$dovecot" ] && [ -n "$submission" ]; then
  71. sasl_inbound=1
  72. else
  73. warn "No inbound SASL authentication - requires TLS encryption and Dovecot."
  74. fi
  75. amavis=
  76. # TODO: maybe use AMaViS default of 20 (instead tuning spamassassin)
  77. # <https://www.ijs.si/software/amavisd/amavisd-new-docs.html#max_requests>
  78. default_amavis_maxproc=2
  79. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  80. amavis=1
  81. else
  82. warn "AMaViS missing (Debian package amavisd-new)."
  83. fi
  84. dkimproxy=
  85. #dkimproxy_maxproc_in=5
  86. # FIXME: update actual outgoing maxproc from /etc/default/dkimproxy (and /etc/dkimproxy/dkimproxy_out.conf too?)
  87. dkimproxy_maxproc_out=5
  88. if [ -x /usr/sbin/dkimproxy.in ]; then
  89. if [ "1" = "$amavis" ]; then
  90. dkimproxy=1
  91. else
  92. warn "No DKIM/Domainkey - requires DKIMproxy and AMaViS."
  93. fi
  94. else
  95. warn "DKIMproxy missing (Debian package dkimproxy)."
  96. fi
  97. catfilefromownrealm() {
  98. set -e
  99. file="$1"
  100. [ -d "$realmsdir" ] || exit 0
  101. cat "$realmsdir/$thisrealm/$file"
  102. }
  103. catallfilesfromotherrealms() {
  104. set -e
  105. file="$1"
  106. [ -d "$realmsdir" ] || exit 0
  107. [ -f "$realmsdir/realms" ] || exit 0
  108. realms="$(cat "$realmsdir/realms" | sed 's/#.*//')"
  109. for realm in $realms; do
  110. if [ "$thisrealm" != "$realm" ]; then
  111. cat "$realmsdir/$realm/$file"
  112. fi
  113. done
  114. }
  115. catfirstfile() {
  116. set -e
  117. file="$1"
  118. context="${2:-postfix}"
  119. configdir=''
  120. for dir in /etc/local "/etc/local-$REALM" "/etc/local-$REDPILL" /etc/local-COMMON; do
  121. if [ -d "$dir/$context" ] && [ -f "$dir/$context/$file" ]; then
  122. configdir="$dir/$context"
  123. break
  124. fi
  125. done
  126. if [ -z "$configdir" ]; then
  127. exit1 "ERROR: file \"$file\" not found."
  128. fi
  129. cat "$configdir/$file"
  130. }
  131. # TODO: support quoted string, and trailing comment.
  132. getperlvarfromfile() {
  133. set -e
  134. export var="$1"
  135. export default="$2"
  136. file="$3"
  137. context="${4:-postfix}"
  138. catfirstfile "$file" "$context" \
  139. | perl -nE'/^\$$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. echo -n "$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. cat /etc/local-COMMON/postfix/rbl_reply_map \
  212. | sed 's/#.*//' \
  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. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  221. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  222. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  223. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" >> "$tempdir/sender_access"
  224. postmapfiles="$postmapfiles sender_access"
  225. _postconf -e "unverified_sender_reject_code = 550"
  226. # Trust recipient verification too
  227. _postconf -e "unverified_recipient_reject_code = 550"
  228. if [ -n "$dovecot_lmtp" ]; then
  229. _postconf -e mailbox_transport=lmtp:unix:private/dovecot-lmtp
  230. _postconf -X mailbox_command
  231. elif [ -n "$dovecot_deliver" ]; then
  232. _postconf -X mailbox_transport
  233. _postconf -e mailbox_command=/usr/lib/dovecot/deliver
  234. else
  235. _postconf -X mailbox_transport
  236. _postconf -X mailbox_command
  237. fi
  238. # outbound opportunistic encryption
  239. _postconf -e smtp_tls_security_level=may
  240. if [ -n "$tls_cert" ]; then
  241. _postconf -e smtp_tls_cert_file="$TLS_CERT"
  242. _postconf -e smtp_tls_key_file="$TLS_KEY"
  243. else
  244. _postconf -X smtp_tls_cert_file
  245. _postconf -X smtp_tls_key_file
  246. fi
  247. _postconf -e smtp_tls_loglevel=1
  248. # enforce TLS trust path towards peers
  249. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  250. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" >> "$tempdir/tls_policy"
  251. postmapfiles="$postmapfiles tls_policy"
  252. _postconf -e smtp_tls_policy_maps="hash:$confdir/tls_policy"
  253. # inbound opportunistic encryption
  254. if [ -n "$tls_cert" ]; then
  255. _postconf -e smtpd_tls_security_level=may
  256. _postconf -e smtpd_tls_cert_file="$TLS_CERT"
  257. _postconf -e smtpd_tls_key_file="$TLS_KEY"
  258. _postconf -e 'smtpd_tls_loglevel = 1'
  259. _postconf -e 'smtpd_tls_auth_only = yes'
  260. _postconf -e 'smtpd_tls_received_header = yes'
  261. else
  262. _postconf -e smtpd_tls_security_level=none
  263. _postconf -X smtpd_tls_cert_file
  264. _postconf -X smtpd_tls_key_file
  265. _postconf -X smtpd_tls_loglevel
  266. _postconf -X smtpd_tls_auth_only
  267. _postconf -X smtpd_tls_received_header
  268. fi
  269. # Avoid smtpd_tls_CApath or smtpd_tls_CAfile to trick outlook.com
  270. # See <http://postfix.1071664.n5.nabble.com/Problems-with-incoming-mails-from-outlook-com-td78356.html>
  271. _postconf -X smtpd_tls_CApath
  272. _postconf -X smtpd_tls_CAfile
  273. # obsolete TLS-related settings
  274. _postconf -X smtpd_use_tls
  275. _postconf -X smtp_tls_CApath
  276. _postconf -X lmtp_tls_CApath
  277. _postconf -X smtp_tls_CAfile
  278. _postconf -X lmtp_tls_CAfile
  279. _postconf -X smtpd_tls_ask_ccert
  280. _postconf -X smtp_tls_note_starttls_offer
  281. _postconf -X smtpd_tls_session_cache_database
  282. _postconf -X smtpd_tls_session_cache_timeout
  283. _postconf -X smtp_tls_session_cache_database
  284. _postconf -X tls_random_exchange_name
  285. _postconf -X tls_random_source
  286. # submission
  287. # <http://www.postfix.org/SASL_README.html>
  288. # <https://doc.dovecot.org/configuration_manual/howto/postfix_and_dovecot_sasl/#using-sasl-with-postfix-submission-port>
  289. if [ -n "$submission" ]; then
  290. _postconf -Me submission/inet='
  291. submission inet n - y - - smtpd
  292. -o syslog_name=postfix/$service_name
  293. -o smtpd_tls_security_level=encrypt
  294. '
  295. _postconf -Me smtps/inet='
  296. smtps inet n - y - - smtpd
  297. -o syslog_name=postfix/$service_name
  298. -o smtpd_tls_wrappermode=yes
  299. -o smtpd_sasl_auth_enable=yes
  300. '
  301. if [ -n "$sasl_inbound" ]; then
  302. _postconf -Pe submission/inet/smtpd_sasl_auth_enable=yes
  303. _postconf -Pe smtps/inet/smtpd_sasl_auth_enable=yes
  304. else
  305. _postconf -PX submission/inet/smtpd_sasl_auth_enable
  306. _postconf -PX smtps/inet/smtpd_sasl_auth_enable
  307. fi
  308. else
  309. _postconf -MX submission/inet
  310. _postconf -MX smtps/inet
  311. fi
  312. if [ -n "$sasl_inbound" ]; then
  313. _postconf -e smtpd_sasl_local_domain='$mydomain'
  314. _postconf -e smtpd_sasl_type=dovecot
  315. _postconf -e smtpd_sasl_path=private/auth
  316. _postconf -e broken_sasl_auth_clients=yes
  317. else
  318. _postconf -X smtpd_sasl_local_domain
  319. _postconf -X smtpd_sasl_type
  320. _postconf -X smtpd_sasl_path
  321. _postconf -X broken_sasl_auth_clients
  322. fi
  323. # obsolete SASL-related settings
  324. _postconf -X smtpd_sasl_auth_enable
  325. _postconf -X smtpd_sasl_security_options
  326. if [ -n "$amavis" ]; then
  327. amavis_maxproc=$(getperlvarfromfile max_servers "$default_amavis_maxproc" amavisd.conf.addon amavis)
  328. _postconf -Me amavisfeed/unix="
  329. amavisfeed unix - - n - $amavis_maxproc lmtp
  330. -o lmtp_data_done_timeout=1200s
  331. -o lmtp_send_xforward_command=yes
  332. -o disable_dns_lookups=yes
  333. -o max_use=$amavis_maxproc
  334. "
  335. _postconf -Me 127.0.0.1:10025/inet='
  336. 127.0.0.1:10025 inet n - n - - smtpd
  337. -o content_filter=
  338. -o local_recipient_maps=
  339. -o relay_recipient_maps=
  340. -o smtpd_restriction_classes=
  341. -o smtpd_delay_reject=no
  342. -o smtpd_client_restrictions=permit_mynetworks,reject
  343. -o smtpd_helo_restrictions=
  344. -o smtpd_sender_restrictions=
  345. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  346. -o smtpd_data_restrictions=reject_unauth_pipelining
  347. -o smtpd_end_of_data_restrictions=
  348. -o mynetworks=127.0.0.0/8
  349. -o smtpd_error_sleep_time=0
  350. -o smtpd_soft_error_limit=1001
  351. -o smtpd_hard_error_limit=1000
  352. -o smtpd_client_connection_count_limit=0
  353. -o smtpd_client_connection_rate_limit=0
  354. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  355. '
  356. _postconf -e receive_override_options=no_address_mappings
  357. if [ -n "$dkimproxy" ]; then
  358. _postconf -e 'content_filter = amavisfeed:[127.0.0.1]:10028'
  359. _postconf -Pe pickup/fifo/content_filter=dkimsign:127.0.0.1:10028
  360. if [ -n "$submission" ]; then
  361. _postconf -Pe submission/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  362. _postconf -Pe smtps/inet/content_filter='dkimsign:[127.0.0.1]:10028'
  363. fi
  364. _postconf -Me dkimsign/unix="
  365. dkimsign unix - - n - $dkimproxy_maxproc_out smtp
  366. -o smtp_send_xforward_command=yes
  367. -o smtp_discard_ehlo_keywords=8bitmime,starttls
  368. "
  369. _postconf -Me 127.0.0.1:10029/inet='
  370. 127.0.0.1:10029 inet n - n - - smtpd
  371. -o content_filter=
  372. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters
  373. -o smtpd_helo_restrictions=
  374. -o smtpd_client_restrictions=
  375. -o smtpd_sender_restrictions=
  376. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  377. -o mynetworks=127.0.0.0/8
  378. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  379. '
  380. else
  381. _postconf -MX dkimsign/unix
  382. _postconf -MX 127.0.0.1:10029/inet
  383. _postconf -e 'content_filter = amavisfeed:[127.0.0.1]:10024'
  384. _postconf -PX pickup/fifo/content_filter
  385. if [ -n "$submission" ]; then
  386. _postconf -PX submission/inet/content_filter
  387. _postconf -PX smtps/inet/content_filter
  388. fi
  389. fi
  390. else
  391. _postconf -MX amavisfeed/unix
  392. _postconf -MX 127.0.0.1:10025/inet
  393. _postconf -X content_filter
  394. _postconf -X receive_override_options
  395. fi
  396. # obsolete settings
  397. _postconf -X max_use
  398. _postconf -MX smtp-amavis/unix
  399. diff -ruNw "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload="1"; fi
  400. if [ "$force" = "1" ]; then
  401. do_update="y"
  402. elif [ "1" = "$needs_reload" ]; then
  403. echo -n "Above is the intended changes. OK to update (y/N)? "
  404. read do_update
  405. fi
  406. case $do_update in
  407. y|Y)
  408. if [ -f "$tempdir/COMMANDS" ]; then
  409. cat "$tempdir/COMMANDS" | sh -s
  410. fi
  411. rm -f "$tempdir/COMMANDS"
  412. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart="1"; fi
  413. cp -a -f -t "$confdir" "$tempdir"/*
  414. rm -rf "$tempdir"
  415. for file in $postmapfiles; do
  416. postmap "$confdir/$file"
  417. done
  418. if [ "1" = "$needs_restart" ]; then
  419. service postfix restart
  420. else
  421. service postfix force-reload
  422. fi
  423. if [ "1" = "$needs_reload" ]; then
  424. echo >&2 "Changes applied!"
  425. fi
  426. ;;
  427. *)
  428. if [ "1" = "$needs_reload" ]; then
  429. exit1 "Aborted!"
  430. fi
  431. ;;
  432. esac
  433. if [ "1" != "$needs_reload" ]; then
  434. echo >&2 "No changes needed!"
  435. fi
  436. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  437. # Support for trusted MX backup networks added
  438. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  439. # RBLs replaced with those recommended by http://www.antispews.org/
  440. # spam filter based on these: http://www.postfix.org/FILTER_README.html
  441. # https://www.ijs.si/software/amavisd/amavisd-new-docs.html
  442. # TLS based on this: http://www.postfix.org/TLS_README.html
  443. # Here's a convenient overview of different blackholes:
  444. # http://rbls.org/