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