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