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