summaryrefslogtreecommitdiff
path: root/postfix/postfix.sh
blob: 27efb9f66d03ba91d1410d0b7e5ccf0e4b02a293 (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. set -e
  16. # Let's standardize sort
  17. export LC_ALL=C
  18. warn() {
  19. echo >&2 "Warning: $1"
  20. }
  21. exit1() {
  22. echo >&2 "Error: $1"
  23. echo >&2 "Exiting..."
  24. exit 1
  25. }
  26. # Favor specific CA for our own server and client certificates
  27. # (comment out to trust any CA)
  28. #cacert_smtpd="/etc/ssl/certs/cacert.org.pem"
  29. cacert_smtp=
  30. cacert_lmtp=
  31. # File containing all trusted CA certificates
  32. # (comment out if no chroot or it contains all individual files)
  33. cacert_default="/etc/ssl/certs/ca-certificates.crt"
  34. realmsdir='/etc/local-REDPILL'
  35. configdirs='/etc/local/postfix /etc/local-ORG/postfix /etc/local-REDPILL/postfix /etc/local-COMMON/postfix'
  36. confdir='/etc/postfix'
  37. postconf=/usr/sbin/postconf
  38. sp='[[:space:]]'
  39. pfver="$($postconf -d mail_version | cut -d= -f2 -s)"
  40. echo "$pfver" | grep -q '2\.[2-9]' \
  41. || exit1 "ERROR: Bad postfix version - this script is known to work only for postfix 2.2 and later"
  42. pf23=
  43. ! echo "$pfver" | grep -q '2\.[3-9]' || pf23=1
  44. if ! [ -d "$realmsdir" ]; then
  45. warn "Realms directory \"$realmsdir\" does not exist."
  46. fi
  47. #postgrey=
  48. #if [ -x /usr/sbin/postgrey ]; then
  49. # # FIXME: Use this somehow, and only warn below
  50. # postgrey=1
  51. #else
  52. # exit1 "ERROR: Greylisting support (Debian package postgrey) missing."
  53. #fi
  54. cyrus=
  55. if [ -x /usr/sbin/saslauthd ]; then
  56. if saslauthd -v 2>&1 | grep -q '^saslauthd 2.1'; then
  57. cyrus=1
  58. else
  59. warn "Cyrus SASL too old - requires >= 2.1.1)."
  60. fi
  61. else
  62. warn "Cyrus SASL missing - (Debian package sasl2-bin)."
  63. fi
  64. dovecot=
  65. if [ -x /usr/sbin/dovecot ]; then
  66. dovecot=1
  67. else
  68. warn "Dovecot missing - (Debian package dovecot-core or dovecot)."
  69. fi
  70. sslcert=
  71. if [ -f /etc/ssl/certs/postfix.pem ]; then
  72. sslcert=1
  73. else
  74. warn "No TLS encryption - requires SSL certificate at /etc/ssl/certs/postfix.pem."
  75. fi
  76. sasl_main_type=
  77. sasl_dovecot=
  78. sasl_cyrus=
  79. if [ -n "$sslcert" ]; then
  80. # FIXME: We really want to check for at least 2.1.1 but that's tricky...
  81. if [ -n "$cyrus" ]; then
  82. sasl_cyrus=1
  83. sasl_main_type=cyrus
  84. fi
  85. if [ -n "$dovecot" ]; then
  86. if [ -n "$pf23" ]; then
  87. sasl_dovecot=1
  88. sasl_main_type=dovecot
  89. else
  90. warn "No Dovecot SASL - requires postfix >= 2.3."
  91. fi
  92. fi
  93. if [ -z "$sasl_main_type" ]; then
  94. warn "No SASL authentication - requires Cyrus SASL or Dovecot."
  95. fi
  96. else
  97. warn "No SASL authentication - requires TLS encryption."
  98. fi
  99. amavis=
  100. if [ -x /usr/sbin/amavisd ] || [ -x /usr/sbin/amavisd-new ]; then
  101. amavis=1
  102. else
  103. warn "AMaViS missing (Debian package amavisd-new)."
  104. fi
  105. dkimproxy=
  106. if [ -x /usr/bin/dkimproxy-sign ]; then
  107. if [ "1" = "$amavis" ]; then
  108. dkimproxy=1
  109. else
  110. warn "No DKIM/Domainkey - requires DKIMproxy and AMaViS."
  111. fi
  112. else
  113. warn "DKIMproxy missing (Debian package dkimproxy)."
  114. fi
  115. catfilefromownrealm() {
  116. set -e
  117. file="$1"
  118. [ -d "$realmsdir" ] || exit 0
  119. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  120. cat "$realmsdir/$thisrealm/$file"
  121. }
  122. catallfilesfromotherrealms() {
  123. set -e
  124. file="$1"
  125. [ -d "$realmsdir" ] || exit 0
  126. [ -f "$realmsdir/realms" ] || exit 0
  127. realms="$(cat "$realmsdir/realms" | sed 's/#.*//')"
  128. thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  129. for realm in $realms; do
  130. if [ "$thisrealm" != "$realm" ]; then
  131. cat "$realmsdir/$realm/$file"
  132. fi
  133. done
  134. }
  135. catfirstfile() {
  136. set -e
  137. file="$1"
  138. configdir=''
  139. for dir in $configdirs; do
  140. if [ -d "$dir" ] && [ -f "$dir/$file" ]; then
  141. configdir="$dir"
  142. break
  143. fi
  144. done
  145. if [ -z "$configdir" ]; then
  146. exit1 "ERROR: file \"$file\" not found."
  147. fi
  148. cat "$configdir/$file"
  149. }
  150. getlinesfromfile() {
  151. set -e
  152. param="$1"
  153. shift
  154. replacements=
  155. for subparam in $@; do
  156. case "$subparam" in
  157. *=*=*)
  158. oldparam="`echo $subparam | awk -F= '{print $1}'`"
  159. newparam="`echo $subparam | awk -F= '{print $2}'`"
  160. newparamfile="`echo $subparam | awk -F= '{print $3}'`"
  161. shift
  162. ;;
  163. *)
  164. oldparam=$subparam
  165. newparam=$subparam
  166. newparamfile=$subparam
  167. shift
  168. ;;
  169. esac
  170. newparamvalues="`getlinesfromfile $newparamfile | sed -e 's/.*=[ ]*//' -e 's/,/ /g'`"
  171. newstring=
  172. for newparamvalue in $newparamvalues; do
  173. newstring="${newstring}$newparam $newparamvalue,"
  174. done
  175. replacements="$replacements;s/$oldparam/$newstring/"
  176. done
  177. echo -n "$param = "
  178. catfirstfile "$param" | sed 's/#.*//' | tr '\n' ',' | sed -e 's/^[, ]*//;s/[, ]\+/,/g' -e 's/\^/ /g' -e "s/,\$//$replacements"
  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`"
  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. if [ -n "$dkimproxy" ]; then
  199. [ -f "$tempdir/sender_access_regex" ] \
  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. [ -f "$tempdir/sender_access_regex" ] \
  204. || echo "touch \"$confdir/sender_access_regex\"" >> "$tempdir/COMMANDS"
  205. fi
  206. # Support exceptions to default response
  207. # (Day Old Bread (dob) lists need to reject only temporarily)
  208. $postconf -c "$tempdir" -e "rbl_reply_maps = hash:$confdir/rbl_reply_map"
  209. cat /etc/local-COMMON/postfix/rbl_reply_map \
  210. | sed 's/#.*//' \
  211. > "$tempdir/rbl_reply_map"
  212. postmapfiles="$postmapfiles rbl_reply_map"
  213. # Verify senders of common suspicious and known verifiable domains
  214. # (exclude verification of postmaster@ to not verify verification probes)
  215. # (add own domains before peers for (rare) cases of duplicates)
  216. # FIXME: somehow do this step only if enabled in smtpd_sender_restrictions
  217. # TODO: Properly implement exception exclusion like yahoo (which does not want to be checked any longer!)
  218. cat /etc/local-COMMON/postfix/maildomains | grep -v yahoo | sort | sed 's/$/ reject_unverified_sender/' > "$tempdir/sender_access"
  219. ( catfilefromownrealm maildomains | sort; catallfilesfromotherrealms maildomains | sort ) \
  220. | sed 's/\(.*\)$/postmaster@\1 permit\n\1 reject_unverified_sender/' >> "$tempdir/sender_access"
  221. [ ! -f "$tempdir/sender_access.addon" ] || cat "$tempdir/sender_access.addon" >> "$tempdir/sender_access"
  222. postmapfiles="$postmapfiles sender_access"
  223. $postconf -c "$tempdir" -e "unverified_sender_reject_code = 550"
  224. # Trust recipient verification too
  225. $postconf -c "$tempdir" -e "unverified_recipient_reject_code = 550"
  226. if [ -n "$sasl_cyrus" ]; then
  227. saslsubdir="sasl"
  228. mkdir -p "$tempdir/$saslsubdir"
  229. echo 'mech_list: plain login' > "$tempdir/$saslsubdir/smtpd.conf"
  230. echo 'minimum_layer: 0' >> "$tempdir/$saslsubdir/smtpd.conf"
  231. echo 'sasl_pwcheck_method: saslauthd' >> "$tempdir/$saslsubdir/smtpd.conf"
  232. echo 'auto_transition: false' >> "$tempdir/$saslsubdir/smtpd.conf"
  233. groups postfix | grep -q sasl || echo "adduser postfix sasl" >> "$tempdir/COMMANDS"
  234. # Release TLS-related daemons from chroot jail (bringing SASL into the jail is just too messy)
  235. sed --in-place \
  236. -e "s/^\(smtp$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]\(\($sp\+-\)\{2\}$sp\+smtpd\).*/\1n\3 -o smtpd_sasl_auth_enable=yes/" \
  237. -e "s/^#\?\(\(smtps\|587\)$sp\+inet\($sp\+[n-]\)\{2\}$sp\+\)[n-]/\1n/" \
  238. -e "s/^#\(tlsmgr$sp\)/\1/" \
  239. "$tempdir/master.cf"
  240. cat $tempdir/master.cf | egrep -q "^tlsmgr$sp" || \
  241. echo 'tlsmgr unix - - - 300 1 tlsmgr' >> $tempdir/master.cf
  242. fi
  243. case "$sasl_main_type" in
  244. dovecot)
  245. $postconf -c "$tempdir" -e smtpd_sasl_type=dovecot
  246. $postconf -c "$tempdir" -e smtpd_sasl_path=private/auth
  247. ;;
  248. cyrus)
  249. $postconf -c "$tempdir" -e smtpd_sasl_type=cyrus
  250. [ -z "$pf23" ] || $postconf -c "$tempdir" -e smtpd_sasl_path=smtpd
  251. [ -n "$pf23" ] || $postconf -c "$tempdir" -e smtpd_sasl_application_name=smtpd
  252. ;;
  253. *) exit1 "ERROR: Wrong SASL type \"$sasl_main_type\"!"
  254. esac
  255. if [ -n "$sslcert" ]; then
  256. $postconf -c "$tempdir" -e 'smtpd_tls_cert_file = /etc/ssl/certs/postfix.pem'
  257. $postconf -c "$tempdir" -e 'smtpd_tls_loglevel = 1'
  258. $postconf -c "$tempdir" -e 'smtpd_use_tls = yes'
  259. $postconf -c "$tempdir" -e 'smtp_tls_CApath = /etc/ssl/certs'
  260. $postconf -c "$tempdir" -e 'smtpd_tls_CApath = /etc/ssl/certs'
  261. $postconf -c "$tempdir" -e 'lmtp_tls_CApath = /etc/ssl/certs'
  262. $postconf -c "$tempdir" -e smtpd_tls_CAfile="${cacert_smtpd:-$cacert_default}"
  263. $postconf -c "$tempdir" -e smtp_tls_CAfile="${cacert_smtp:-$cacert_default}"
  264. $postconf -c "$tempdir" -e lmtp_tls_CAfile="${cacert_lmtp:-$cacert_default}"
  265. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache'
  266. $postconf -c "$tempdir" -e 'smtpd_tls_session_cache_timeout = 3600s'
  267. $postconf -c "$tempdir" -e 'tls_random_exchange_name = ${data_directory}/prng_exch'
  268. $postconf -c "$tempdir" -e 'smtpd_tls_auth_only = yes'
  269. $postconf -c "$tempdir" -e 'smtpd_sasl_auth_enable = no' # SASL is enabled explicitly with TLS transport
  270. $postconf -c "$tempdir" -e 'smtpd_sasl_security_options = noanonymous'
  271. $postconf -c "$tempdir" -e 'smtpd_sasl_local_domain = '
  272. $postconf -c "$tempdir" -e 'smtpd_tls_received_header = yes'
  273. $postconf -c "$tempdir" -e 'broken_sasl_auth_clients = yes'
  274. $postconf -c "$tempdir" -e 'tls_random_source = dev:/dev/urandom'
  275. $postconf -c "$tempdir" -e 'tls_daemon_random_source = dev:/dev/urandom'
  276. # Accepting client certificates breaks SMTP AUTH on OutLook Express on Mac (Classic)
  277. $postconf -c "$tempdir" -e 'smtpd_tls_ask_ccert = no'
  278. if [ -e /etc/ssl/private/postfix.pem ]; then
  279. $postconf -c "$tempdir" -e 'smtpd_tls_key_file = /etc/ssl/private/postfix.pem'
  280. # Enable client side TLS only when private certificate is present
  281. $postconf -c "$tempdir" -e 'smtp_tls_cert_file = /etc/ssl/certs/postfix.pem'
  282. $postconf -c "$tempdir" -e 'smtp_tls_key_file = /etc/ssl/private/postfix.pem'
  283. $postconf -c "$tempdir" -e 'smtp_tls_loglevel = 1'
  284. $postconf -c "$tempdir" -e 'smtp_use_tls = yes'
  285. $postconf -c "$tempdir" -e 'smtp_tls_note_starttls_offer = no' # Enable to collect info for smtp_tls_per_site option
  286. $postconf -c "$tempdir" -e 'smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache'
  287. # Force using TLS for peers
  288. catallfilesfromotherrealms mailhost | sort | sed 's/^/[/;s/$/]:submission secure/' > "$tempdir/tls_policy"
  289. [ ! -f "$tempdir/tls_policy.addon" ] || cat "$tempdir/tls_policy.addon" >> "$tempdir/tls_policy"
  290. postmapfiles="$postmapfiles tls_policy"
  291. $postconf -c "$tempdir" -e "smtp_tls_policy_maps = hash:$confdir/tls_policy"
  292. else
  293. $postconf -c "$tempdir" -e 'smtp_use_tls = no'
  294. warn "No client-side TLS - requires private SSL certificate at /etc/ssl/private/postfix.pem."
  295. fi
  296. fi
  297. if [ -n "$amavis" ]; then
  298. $postconf -c "$tempdir" -e 'max_use = 10' # Avoid too much reuse
  299. cat $tempdir/master.cf | egrep -q "^smtp-amavis$sp" || \
  300. cat >> $tempdir/master.cf << EOF
  301. smtp-amavis unix - - n - 5 smtp
  302. -o smtp_data_done_timeout=1200s
  303. -o smtp_never_send_ehlo=yes
  304. -o smtp_send_xforward_command=yes
  305. -o disable_dns_lookups=yes
  306. -o max_use=20
  307. EOF
  308. cat $tempdir/master.cf | egrep -q "^127.0.0.1:10025$sp" || \
  309. cat >> $tempdir/master.cf << EOF
  310. 127.0.0.1:10025 inet n - n - - smtpd
  311. -o content_filter=
  312. -o local_recipient_maps=
  313. -o relay_recipient_maps=
  314. -o smtpd_restriction_classes=
  315. -o smtpd_delay_reject=no
  316. -o smtpd_client_restrictions=permit_mynetworks,reject
  317. -o smtpd_helo_restrictions=
  318. -o smtpd_sender_restrictions=
  319. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  320. -o mynetworks_style=host
  321. -o mynetworks=127.0.0.0/8
  322. -o strict_rfc821_envelopes=yes
  323. -o smtpd_error_sleep_time=0
  324. -o smtpd_soft_error_limit=1001
  325. EOF
  326. if [ -n "$dkimproxy" ]; then
  327. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10028'
  328. # FIXME: needs multiline replacementroutine (using perl?)
  329. # cat $tempdir/master.cf | egrep -q "^submission$sp" || \
  330. # cat >> $tempdir/master.cf << EOF
  331. #submission inet n - n - - smtpd
  332. # -o smtpd_etrn_restrictions=reject
  333. # -o smtpd_enforce_tls=yes
  334. # -o smtpd_sasl_auth_enable=yes
  335. # -o content_filter=dkimsign:[127.0.0.1]:10028
  336. # -o receive_override_options=no_address_mappings
  337. # -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  338. #EOF
  339. # cat $tempdir/master.cf | egrep -q "^pickup$sp" || \
  340. # cat >> $tempdir/master.cf << EOF
  341. #pickup fifo n - - 60 1 pickup
  342. # -o content_filter=dkimsign:127.0.0.1:10028
  343. #EOF
  344. cat $tempdir/master.cf | egrep -q "^dkimsign$sp" || \
  345. cat >> $tempdir/master.cf << EOF
  346. dkimsign unix - - n - 10 smtp
  347. -o smtp_send_xforward_command=yes
  348. -o smtp_discard_ehlo_keywords=8bitmime
  349. EOF
  350. cat $tempdir/master.cf | egrep -q "^127\.0\.0\.1:10029$sp" || \
  351. cat >> $tempdir/master.cf << EOF
  352. 127.0.0.1:10029 inet n - n - 10 smtpd
  353. -o content_filter=
  354. -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
  355. -o smtpd_helo_restrictions=
  356. -o smtpd_client_restrictions=
  357. -o smtpd_sender_restrictions=
  358. -o smtpd_recipient_restrictions=permit_mynetworks,reject
  359. -o mynetworks=127.0.0.0/8
  360. -o smtpd_authorized_xforward_hosts=127.0.0.0/8
  361. EOF
  362. else
  363. $postconf -c "$tempdir" -e 'content_filter = smtp-amavis:[127.0.0.1]:10024'
  364. fi
  365. fi
  366. diff -ruN "$confdir" "$tempdir" || if [ $? -gt 1 ]; then exit $?; else needs_reload="1"; fi
  367. if [ "$force" = "1" ]; then
  368. do_update="y"
  369. elif [ "1" = "$needs_reload" ]; then
  370. echo -n "Above is the intended changes. OK to update (y/N)? "
  371. read do_update
  372. fi
  373. case $do_update in
  374. y|Y)
  375. if [ -f "$tempdir/COMMANDS" ]; then
  376. cat "$tempdir/COMMANDS" | sh -s
  377. fi
  378. rm -f "$tempdir/COMMANDS"
  379. diff -q "$confdir/master.cf" "$tempdir/master.cf" || if [ $? -gt 1 ]; then exit $?; else needs_restart="1"; fi
  380. cp -a -f -t "$confdir" "$tempdir"/*
  381. rm -rf "$tempdir"
  382. for file in $postmapfiles; do
  383. postmap "$confdir/$file"
  384. done
  385. if [ "1" = "$needs_restart" ]; then
  386. invoke-rc.d postfix restart
  387. else
  388. invoke-rc.d postfix force-reload
  389. fi
  390. if [ "1" = "$needs_reload" ]; then
  391. echo >&2 "Changes applied!"
  392. fi
  393. ;;
  394. *)
  395. if [ "1" = "$needs_reload" ]; then
  396. exit1 "Aborted!"
  397. fi
  398. ;;
  399. esac
  400. if [ "1" != "$needs_reload" ]; then
  401. echo >&2 "No changes needed!"
  402. fi
  403. # Based on this: http://jimsun.linxnet.com/misc/postfix-anti-UCE.txt
  404. # Support for trusted MX backup networks added
  405. # PCRE stuff avoided, as PCRE is only optional on newest Debian packages
  406. # RBLs replaced with those recommended by http://www.antispews.org/
  407. # AMaViS tweaks as documented in amavisd-new package
  408. # AUTH-SMTP based on these:
  409. # http://lists.q-linux.com/pipermail/plug/2003-July/029503.html
  410. # http://www.porcupine.org/postfix-mirror/newdoc/SASL_README.html
  411. # Here's a convenient overview of different blackholes:
  412. # http://rbls.org/