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