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